blob: 111a2cfa42ccf45181a0d5b5b840bf3e5918c744 [file] [log] [blame]
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001/* $OpenBSD: channels.c,v 1.364 2017/05/31 00:43:04 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 "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000073#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "log.h"
76#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100077#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100081#include "key.h"
82#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110083#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Ben Lindstrome9c99912001-06-09 00:41:05 +000085/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller5428f641999-11-25 11:54:57 +110087/*
88 * Pointer to an array containing all allocated channels. The array is
89 * dynamically extended as needed.
90 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000091static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller5428f641999-11-25 11:54:57 +110093/*
94 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000095 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110096 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100097static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000101 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100102 */
Damien Miller5e953212001-01-30 09:14:00 +1100103static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Ben Lindstrome9c99912001-06-09 00:41:05 +0000106/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Data structure for storing which hosts are permitted for forward requests.
110 * The local sides of any remote forwards are stored in this array to prevent
111 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
112 * network (which might be behind a firewall).
113 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000114/* XXX: streamlocal wants a path instead of host:port */
115/* Overload host_to_connect; we could just make this match Forward */
116/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100117typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000118 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000119 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000120 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000121 char *listen_path; /* Remote side should listen path. */
122 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000123 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124} ForwardPermission;
125
Damien Miller9b439df2006-07-24 14:04:00 +1000126/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000127static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000128
Damien Miller9b439df2006-07-24 14:04:00 +1000129/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000130static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000131
132/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
135/* Number of permitted host/port pair in the array permitted by the admin. */
136static int num_adm_permitted_opens = 0;
137
Darren Tucker1338b9e2011-10-02 18:57:35 +1100138/* special-case port number meaning allow any port */
139#define FWD_PERMIT_ANY_PORT 0
140
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000141/* special-case wildcard meaning allow any host */
142#define FWD_PERMIT_ANY_HOST "*"
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * If this is true, all opens are permitted. This is the case on the server
146 * on which we have to trust the client anyway, and the user could do
147 * anything after logging in anyway.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149static int all_opens_permitted = 0;
150
Ben Lindstrome9c99912001-06-09 00:41:05 +0000151
152/* -- X11 forwarding */
153
154/* Maximum number of fake X11 displays to try. */
155#define MAX_DISPLAYS 1000
156
Damien Miller13390022005-07-06 09:44:19 +1000157/* Saved X11 local (client) display. */
158static char *x11_saved_display = NULL;
159
Ben Lindstrome9c99912001-06-09 00:41:05 +0000160/* Saved X11 authentication protocol name. */
161static char *x11_saved_proto = NULL;
162
163/* Saved X11 authentication data. This is the real data. */
164static char *x11_saved_data = NULL;
165static u_int x11_saved_data_len = 0;
166
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000167/* Deadline after which all X11 connections are refused */
168static u_int x11_refuse_time;
169
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170/*
171 * Fake X11 authentication data. This is what the server will be sending us;
172 * we should replace any occurrences of this by the real data.
173 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100174static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000175static u_int x11_fake_data_len;
176
177
178/* -- agent forwarding */
179
180#define NUM_SOCKS 10
181
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000182/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000183static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000184
Ben Lindstrome9c99912001-06-09 00:41:05 +0000185/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static void port_open_helper(Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000187static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000188
Damien Millerbd740252008-05-19 15:37:09 +1000189/* non-blocking connect helpers */
190static int connect_next(struct channel_connect *);
191static void channel_connect_ctx_free(struct channel_connect *);
192
Ben Lindstrome9c99912001-06-09 00:41:05 +0000193/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000194
195Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100196channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000197{
198 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000199
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000200 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100201 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000202 return NULL;
203 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000204 c = channels[id];
205 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100206 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000207 return NULL;
208 }
209 return c;
210}
211
markus@openbsd.org8d057842016-09-30 09:19:13 +0000212Channel *
213channel_by_remote_id(int remote_id)
214{
215 Channel *c;
216 u_int i;
217
218 for (i = 0; i < channels_alloc; i++) {
219 c = channels[i];
220 if (c != NULL && c->remote_id == remote_id)
221 return c;
222 }
223 return NULL;
224}
225
Damien Miller5428f641999-11-25 11:54:57 +1100226/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100227 * Returns the channel if it is allowed to receive protocol messages.
228 * Private channels, like listening sockets, may not receive messages.
229 */
230Channel *
231channel_lookup(int id)
232{
233 Channel *c;
234
235 if ((c = channel_by_id(id)) == NULL)
236 return (NULL);
237
Damien Millerd62f2ca2006-03-26 13:57:41 +1100238 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100239 case SSH_CHANNEL_X11_OPEN:
240 case SSH_CHANNEL_LARVAL:
241 case SSH_CHANNEL_CONNECTING:
242 case SSH_CHANNEL_DYNAMIC:
243 case SSH_CHANNEL_OPENING:
244 case SSH_CHANNEL_OPEN:
Damien Miller36187092013-06-10 13:07:11 +1000245 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000246 case SSH_CHANNEL_MUX_PROXY:
Damien Millerd47c62a2005-12-13 19:33:57 +1100247 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100248 }
249 logit("Non-public channel %d, type %d.", id, c->type);
250 return (NULL);
251}
252
253/*
Damien Millere247cc42000-05-07 12:03:14 +1000254 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000255 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100256 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000257static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100258channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000259 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260{
Damien Miller95def091999-11-25 00:26:21 +1100261 /* Update the maximum file descriptor value. */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000262 channel_max_fd = MAXIMUM(channel_max_fd, rfd);
263 channel_max_fd = MAXIMUM(channel_max_fd, wfd);
264 channel_max_fd = MAXIMUM(channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100265
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100266 if (rfd != -1)
267 fcntl(rfd, F_SETFD, FD_CLOEXEC);
268 if (wfd != -1 && wfd != rfd)
269 fcntl(wfd, F_SETFD, FD_CLOEXEC);
270 if (efd != -1 && efd != rfd && efd != wfd)
271 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000272
Damien Miller6f83b8e2000-05-02 09:23:45 +1000273 c->rfd = rfd;
274 c->wfd = wfd;
275 c->sock = (rfd == wfd) ? rfd : -1;
276 c->efd = efd;
277 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100278
Darren Tuckered3cdc02008-06-16 23:29:18 +1000279 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000280 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000281#ifdef _AIX
282 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000283 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000284#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100285
Damien Miller69b69aa2000-10-28 14:19:58 +1100286 /* enable nonblocking mode */
287 if (nonblock) {
288 if (rfd != -1)
289 set_nonblock(rfd);
290 if (wfd != -1)
291 set_nonblock(wfd);
292 if (efd != -1)
293 set_nonblock(efd);
294 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000295}
296
297/*
298 * Allocate a new channel object and set its type and socket. This will cause
299 * remote_name to be freed.
300 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000301Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000302channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000303 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000304{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000305 int found;
306 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000307 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller95def091999-11-25 00:26:21 +1100309 /* Do initial allocation if this is the first call. */
310 if (channels_alloc == 0) {
311 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100312 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100313 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000314 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100315 }
316 /* Try to find a free slot where to put the new channel. */
317 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000318 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100319 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000320 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100321 break;
322 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000323 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100324 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100325 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000326 if (channels_alloc > 10000)
327 fatal("channel_new: internal error: channels_alloc %d "
328 "too big.", channels_alloc);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000329 channels = xreallocarray(channels, channels_alloc + 10,
Damien Miller36812092006-03-26 14:22:47 +1100330 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000331 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100332 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100333 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000334 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000336 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100337 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100338 buffer_init(&c->input);
339 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000340 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100341 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000342 c->listening_addr = NULL;
343 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100344 c->ostate = CHAN_OUTPUT_OPEN;
345 c->istate = CHAN_INPUT_OPEN;
346 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000347 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000348 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100349 c->self = found;
350 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000351 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000352 c->local_window = window;
353 c->local_window_max = window;
354 c->local_consumed = 0;
355 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100356 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000357 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000358 c->remote_window = 0;
359 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000360 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100361 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000362 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100363 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000364 c->open_confirm = NULL;
365 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000366 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100367 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000368 c->filter_ctx = NULL;
369 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100370 c->ctl_chan = -1;
371 c->mux_rcb = NULL;
372 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000373 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100374 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000375 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100376 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000377 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000379
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000380static int
381channel_find_maxfd(void)
382{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000383 u_int i;
384 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000385 Channel *c;
386
387 for (i = 0; i < channels_alloc; i++) {
388 c = channels[i];
389 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000390 max = MAXIMUM(max, c->rfd);
391 max = MAXIMUM(max, c->wfd);
392 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000393 }
394 }
395 return max;
396}
397
398int
399channel_close_fd(int *fdp)
400{
401 int ret = 0, fd = *fdp;
402
403 if (fd != -1) {
404 ret = close(fd);
405 *fdp = -1;
406 if (fd == channel_max_fd)
407 channel_max_fd = channel_find_maxfd();
408 }
409 return ret;
410}
411
Damien Miller6f83b8e2000-05-02 09:23:45 +1000412/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000414channel_close_fds(Channel *c)
415{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000416 channel_close_fd(&c->sock);
417 channel_close_fd(&c->rfd);
418 channel_close_fd(&c->wfd);
419 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000420}
421
422/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000423void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000424channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000426 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000427 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000428 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000429 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000430
markus@openbsd.org8d057842016-09-30 09:19:13 +0000431 for (n = 0, i = 0; i < channels_alloc; i++) {
432 if ((other = channels[i]) != NULL) {
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000433 n++;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000434
435 /* detach from mux client and prepare for closing */
436 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
437 other->type == SSH_CHANNEL_MUX_PROXY &&
438 other->mux_ctx == c) {
439 other->mux_ctx = NULL;
440 other->type = SSH_CHANNEL_OPEN;
441 other->istate = CHAN_INPUT_CLOSED;
442 other->ostate = CHAN_OUTPUT_CLOSED;
443 }
444 }
445 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000446 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000447 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000448
markus@openbsd.org8d057842016-09-30 09:19:13 +0000449 /* XXX more MUX cleanup: remove remote forwardings */
450 if (c->type == SSH_CHANNEL_MUX_CLIENT) {
451 for (i = 0; i < (u_int)num_permitted_opens; i++) {
452 if (permitted_opens[i].downstream != c)
453 continue;
454 /* cancel on the server, since mux client is gone */
455 debug("channel %d: cleanup remote forward for %s:%u",
456 c->self,
457 permitted_opens[i].listen_host,
458 permitted_opens[i].listen_port);
459 packet_start(SSH2_MSG_GLOBAL_REQUEST);
460 packet_put_cstring("cancel-tcpip-forward");
461 packet_put_char(0);
462 packet_put_cstring(channel_rfwd_bind_host(
463 permitted_opens[i].listen_host));
464 packet_put_int(permitted_opens[i].listen_port);
465 packet_send();
466 /* unregister */
467 permitted_opens[i].listen_port = 0;
468 permitted_opens[i].port_to_connect = 0;
469 free(permitted_opens[i].host_to_connect);
470 permitted_opens[i].host_to_connect = NULL;
471 free(permitted_opens[i].listen_host);
472 permitted_opens[i].listen_host = NULL;
473 permitted_opens[i].listen_path = NULL;
474 permitted_opens[i].downstream = NULL;
475 }
476 }
477
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000478 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000479 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000481
Damien Miller6f83b8e2000-05-02 09:23:45 +1000482 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000483 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000484 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000485 buffer_free(&c->input);
486 buffer_free(&c->output);
487 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000488 free(c->remote_name);
489 c->remote_name = NULL;
490 free(c->path);
491 c->path = NULL;
492 free(c->listening_addr);
493 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000494 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
495 if (cc->abandon_cb != NULL)
496 cc->abandon_cb(c, cc->ctx);
497 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100498 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000499 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000500 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000501 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
502 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000503 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000504 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505}
506
Ben Lindstrome9c99912001-06-09 00:41:05 +0000507void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000508channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000509{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000510 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000511
Ben Lindstrom601e4362001-06-21 03:19:23 +0000512 for (i = 0; i < channels_alloc; i++)
513 if (channels[i] != NULL)
514 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000515}
516
517/*
518 * Closes the sockets/fds of all channels. This is used to close extra file
519 * descriptors after a fork.
520 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000521void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000522channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000523{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000524 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000525
526 for (i = 0; i < channels_alloc; i++)
527 if (channels[i] != NULL)
528 channel_close_fds(channels[i]);
529}
530
531/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000532 * Stop listening to channels.
533 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000534void
535channel_stop_listening(void)
536{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000537 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000538 Channel *c;
539
540 for (i = 0; i < channels_alloc; i++) {
541 c = channels[i];
542 if (c != NULL) {
543 switch (c->type) {
544 case SSH_CHANNEL_AUTH_SOCKET:
545 case SSH_CHANNEL_PORT_LISTENER:
546 case SSH_CHANNEL_RPORT_LISTENER:
547 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000548 case SSH_CHANNEL_UNIX_LISTENER:
549 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000550 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000551 channel_free(c);
552 break;
553 }
554 }
555 }
556}
557
558/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000559 * Returns true if no channel has too much buffered data, and false if one or
560 * more channel is overfull.
561 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000562int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000563channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000564{
565 u_int i;
566 Channel *c;
567
568 for (i = 0; i < channels_alloc; i++) {
569 c = channels[i];
570 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000571 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000572 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000573 c->self, buffer_len(&c->output),
574 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000575 return 0;
576 }
577 }
578 }
579 return 1;
580}
581
582/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000583int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000584channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000585{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000586 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000587 Channel *c;
588
589 for (i = 0; i < channels_alloc; i++) {
590 c = channels[i];
591 if (c == NULL)
592 continue;
593 switch (c->type) {
594 case SSH_CHANNEL_X11_LISTENER:
595 case SSH_CHANNEL_PORT_LISTENER:
596 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100597 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 case SSH_CHANNEL_CLOSED:
599 case SSH_CHANNEL_AUTH_SOCKET:
600 case SSH_CHANNEL_DYNAMIC:
601 case SSH_CHANNEL_CONNECTING:
602 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000603 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000604 case SSH_CHANNEL_UNIX_LISTENER:
605 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000606 continue;
607 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000608 continue;
609 case SSH_CHANNEL_OPENING:
610 case SSH_CHANNEL_OPEN:
611 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100612 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000613 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000614 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000615 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000616 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000617 /* NOTREACHED */
618 }
619 }
620 return 0;
621}
622
623/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000624int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000625channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000626{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000627 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000628 Channel *c;
629
630 for (i = 0; i < channels_alloc; i++) {
631 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000632 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633 continue;
634 switch (c->type) {
635 case SSH_CHANNEL_CLOSED:
636 case SSH_CHANNEL_DYNAMIC:
637 case SSH_CHANNEL_X11_LISTENER:
638 case SSH_CHANNEL_PORT_LISTENER:
639 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100640 case SSH_CHANNEL_MUX_LISTENER:
641 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000642 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000643 case SSH_CHANNEL_OPENING:
644 case SSH_CHANNEL_CONNECTING:
645 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000646 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000647 case SSH_CHANNEL_UNIX_LISTENER:
648 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000649 continue;
650 case SSH_CHANNEL_LARVAL:
651 case SSH_CHANNEL_AUTH_SOCKET:
652 case SSH_CHANNEL_OPEN:
653 case SSH_CHANNEL_X11_OPEN:
654 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000655 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000656 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000657 /* NOTREACHED */
658 }
659 }
660 return -1;
661}
662
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663/*
664 * Returns a message describing the currently open forwarded connections,
665 * suitable for sending to the client. The message contains crlf pairs for
666 * newlines.
667 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000669channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000670{
671 Buffer buffer;
672 Channel *c;
673 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000674 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000675
676 buffer_init(&buffer);
677 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
678 buffer_append(&buffer, buf, strlen(buf));
679 for (i = 0; i < channels_alloc; i++) {
680 c = channels[i];
681 if (c == NULL)
682 continue;
683 switch (c->type) {
684 case SSH_CHANNEL_X11_LISTENER:
685 case SSH_CHANNEL_PORT_LISTENER:
686 case SSH_CHANNEL_RPORT_LISTENER:
687 case SSH_CHANNEL_CLOSED:
688 case SSH_CHANNEL_AUTH_SOCKET:
689 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000690 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100691 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000692 case SSH_CHANNEL_UNIX_LISTENER:
693 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 continue;
695 case SSH_CHANNEL_LARVAL:
696 case SSH_CHANNEL_OPENING:
697 case SSH_CHANNEL_CONNECTING:
698 case SSH_CHANNEL_DYNAMIC:
699 case SSH_CHANNEL_OPEN:
700 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000701 case SSH_CHANNEL_MUX_PROXY:
702 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000703 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000704 " #%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 +0000705 c->self, c->remote_name,
706 c->type, c->remote_id,
707 c->istate, buffer_len(&c->input),
708 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100709 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000710 buffer_append(&buffer, buf, strlen(buf));
711 continue;
712 default:
713 fatal("channel_open_message: bad channel type %d", c->type);
714 /* NOTREACHED */
715 }
716 }
717 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000718 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000719 buffer_free(&buffer);
720 return cp;
721}
722
723void
724channel_send_open(int id)
725{
726 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000727
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000729 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000730 return;
731 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000732 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000733 packet_start(SSH2_MSG_CHANNEL_OPEN);
734 packet_put_cstring(c->ctype);
735 packet_put_int(c->self);
736 packet_put_int(c->local_window);
737 packet_put_int(c->local_maxpacket);
738 packet_send();
739}
740
741void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000742channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000744 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000745
Ben Lindstrome9c99912001-06-09 00:41:05 +0000746 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000747 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000748 return;
749 }
Damien Miller0e220db2004-06-15 10:34:08 +1000750 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000751 packet_start(SSH2_MSG_CHANNEL_REQUEST);
752 packet_put_int(c->remote_id);
753 packet_put_cstring(service);
754 packet_put_char(wantconfirm);
755}
Damien Miller4f7becb2006-03-26 14:10:14 +1100756
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757void
Damien Millerb84886b2008-05-19 15:05:07 +1000758channel_register_status_confirm(int id, channel_confirm_cb *cb,
759 channel_confirm_abandon_cb *abandon_cb, void *ctx)
760{
761 struct channel_confirm *cc;
762 Channel *c;
763
764 if ((c = channel_lookup(id)) == NULL)
765 fatal("channel_register_expect: %d: bad id", id);
766
Damien Miller6c81fee2013-11-08 12:19:55 +1100767 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000768 cc->cb = cb;
769 cc->abandon_cb = abandon_cb;
770 cc->ctx = ctx;
771 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
772}
773
774void
Damien Millerd530f5f2010-05-21 14:57:10 +1000775channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000776{
777 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000778
Ben Lindstrome9c99912001-06-09 00:41:05 +0000779 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100780 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000781 return;
782 }
Damien Millerb84886b2008-05-19 15:05:07 +1000783 c->open_confirm = fn;
784 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000785}
Damien Miller4f7becb2006-03-26 14:10:14 +1100786
Ben Lindstrome9c99912001-06-09 00:41:05 +0000787void
Damien Miller39eda6e2005-11-05 14:52:50 +1100788channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000789{
Damien Millerd47c62a2005-12-13 19:33:57 +1100790 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000791
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000793 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000794 return;
795 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000796 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100797 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000798}
Damien Miller4f7becb2006-03-26 14:10:14 +1100799
Ben Lindstrome9c99912001-06-09 00:41:05 +0000800void
801channel_cancel_cleanup(int id)
802{
Damien Millerd47c62a2005-12-13 19:33:57 +1100803 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000804
Ben Lindstrome9c99912001-06-09 00:41:05 +0000805 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000806 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000807 return;
808 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000809 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100810 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000811}
Damien Miller4f7becb2006-03-26 14:10:14 +1100812
Ben Lindstrome9c99912001-06-09 00:41:05 +0000813void
Damien Miller077b2382005-12-31 16:22:32 +1100814channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000815 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000816{
817 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000818
Ben Lindstrome9c99912001-06-09 00:41:05 +0000819 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000820 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000821 return;
822 }
Damien Miller077b2382005-12-31 16:22:32 +1100823 c->input_filter = ifn;
824 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000825 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000826 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000827}
828
829void
830channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000831 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000832{
833 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000834
Ben Lindstrome9c99912001-06-09 00:41:05 +0000835 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
836 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000837 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100839 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000840 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
841 packet_put_int(c->remote_id);
842 packet_put_int(c->local_window);
843 packet_send();
844}
845
Damien Miller5428f641999-11-25 11:54:57 +1100846/*
Damien Millerb38eff82000-04-01 11:09:21 +1000847 * 'channel_pre*' are called just before select() to add any bits relevant to
848 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100849 */
Damien Millerb38eff82000-04-01 11:09:21 +1000850/*
851 * 'channel_post*': perform any appropriate operations for channels which
852 * have events pending.
853 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100854typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000855chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
856chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
857
Damien Miller8473dd82006-07-24 14:08:32 +1000858/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100860channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000861{
862 FD_SET(c->sock, readset);
863}
864
Damien Miller8473dd82006-07-24 14:08:32 +1000865/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000866static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100867channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000868{
869 debug3("channel %d: waiting for connection", c->self);
870 FD_SET(c->sock, writeset);
871}
872
Ben Lindstrombba81212001-06-25 05:01:22 +0000873static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100874channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000875{
Damien Miller33b13562000-04-04 14:38:59 +1000876 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000877 c->remote_window > 0 &&
878 buffer_len(&c->input) < c->remote_window &&
Damien Miller499a0d52006-04-23 12:06:03 +1000879 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000880 FD_SET(c->rfd, readset);
881 if (c->ostate == CHAN_OUTPUT_OPEN ||
882 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
883 if (buffer_len(&c->output) > 0) {
884 FD_SET(c->wfd, writeset);
885 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000886 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000887 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
888 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000889 else
890 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000891 }
892 }
893 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000894 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
895 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000896 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
897 buffer_len(&c->extended) > 0)
898 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000899 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
900 (c->extended_usage == CHAN_EXTENDED_READ ||
901 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000902 buffer_len(&c->extended) < c->remote_window)
903 FD_SET(c->efd, readset);
904 }
Damien Miller0e220db2004-06-15 10:34:08 +1000905 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000906}
907
Damien Millerb38eff82000-04-01 11:09:21 +1000908/*
909 * This is a special state for X11 authentication spoofing. An opened X11
910 * connection (when authentication spoofing is being done) remains in this
911 * state until the first packet has been completely read. The authentication
912 * data in that packet is then substituted by the real data if it matches the
913 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000914 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000915 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000916 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000917static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000918x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000919{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000920 u_char *ucp;
921 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000922
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000923 /* Is this being called after the refusal deadline? */
924 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
925 verbose("Rejected X11 connection after ForwardX11Timeout "
926 "expired");
927 return -1;
928 }
929
Damien Millerb38eff82000-04-01 11:09:21 +1000930 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000931 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000932 return 0;
933
934 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100935 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000936 if (ucp[0] == 0x42) { /* Byte order MSB first. */
937 proto_len = 256 * ucp[6] + ucp[7];
938 data_len = 256 * ucp[8] + ucp[9];
939 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
940 proto_len = ucp[6] + 256 * ucp[7];
941 data_len = ucp[8] + 256 * ucp[9];
942 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000943 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100944 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000945 return -1;
946 }
947
948 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000949 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000950 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
951 return 0;
952
953 /* Check if authentication protocol matches. */
954 if (proto_len != strlen(x11_saved_proto) ||
955 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000956 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000957 return -1;
958 }
959 /* Check if authentication data matches our fake data. */
960 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000961 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000962 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000963 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000964 return -1;
965 }
966 /* Check fake data length */
967 if (x11_fake_data_len != x11_saved_data_len) {
968 error("X11 fake_data_len %d != saved_data_len %d",
969 x11_fake_data_len, x11_saved_data_len);
970 return -1;
971 }
972 /*
973 * Received authentication protocol and data match
974 * our fake data. Substitute the fake data with real
975 * data.
976 */
977 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
978 x11_saved_data, x11_saved_data_len);
979 return 1;
980}
981
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100983channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000984{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000985 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000986
987 /* c->force_drain = 1; */
988
Damien Millerb38eff82000-04-01 11:09:21 +1000989 if (ret == 1) {
990 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100991 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000992 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000993 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000994 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100995 chan_read_failed(c);
996 buffer_clear(&c->input);
997 chan_ibuf_empty(c);
998 buffer_clear(&c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000999 chan_write_failed(c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001000 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001001 }
1002}
1003
Damien Millere1537f92010-01-26 13:26:22 +11001004static void
1005channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1006{
Damien Millerd530f5f2010-05-21 14:57:10 +10001007 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001008 buffer_check_alloc(&c->input, CHAN_RBUF))
1009 FD_SET(c->rfd, readset);
1010 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1011 /* clear buffer immediately (discard any partial packet) */
1012 buffer_clear(&c->input);
1013 chan_ibuf_empty(c);
1014 /* Start output drain. XXX just kill chan? */
1015 chan_rcvd_oclose(c);
1016 }
1017 if (c->ostate == CHAN_OUTPUT_OPEN ||
1018 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1019 if (buffer_len(&c->output) > 0)
1020 FD_SET(c->wfd, writeset);
1021 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1022 chan_obuf_empty(c);
1023 }
1024}
1025
Ben Lindstromb3921512001-04-11 15:57:50 +00001026/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001027/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001028static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001029channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001030{
Damien Millera10f5612002-09-12 09:49:15 +10001031 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001032 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001033 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001034 struct {
1035 u_int8_t version;
1036 u_int8_t command;
1037 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001038 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001039 } s4_req, s4_rsp;
1040
Ben Lindstromb3921512001-04-11 15:57:50 +00001041 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001042
1043 have = buffer_len(&c->input);
1044 len = sizeof(s4_req);
1045 if (have < len)
1046 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001047 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001048
1049 need = 1;
1050 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1051 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1052 debug2("channel %d: socks4a request", c->self);
1053 /* ... and needs an extra string (the hostname) */
1054 need = 2;
1055 }
1056 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001057 for (found = 0, i = len; i < have; i++) {
1058 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001059 found++;
1060 if (found == need)
1061 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001062 }
1063 if (i > 1024) {
1064 /* the peer is probably sending garbage */
1065 debug("channel %d: decode socks4: too long",
1066 c->self);
1067 return -1;
1068 }
1069 }
Damien Miller1781f532009-01-28 16:24:41 +11001070 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001071 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001072 buffer_get(&c->input, (char *)&s4_req.version, 1);
1073 buffer_get(&c->input, (char *)&s4_req.command, 1);
1074 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001075 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001076 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001077 p = (char *)buffer_ptr(&c->input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001078 if (memchr(p, '\0', have) == NULL) {
1079 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001080 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001081 return -1;
1082 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001083 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001084 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001085 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001086 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001087 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001088 c->self, len, have);
1089 strlcpy(username, p, sizeof(username));
1090 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001091
Darren Tuckera627d422013-06-02 07:31:17 +10001092 free(c->path);
1093 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001094 if (need == 1) { /* SOCKS4: one string */
1095 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001096 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001097 } else { /* SOCKS4A: two strings */
1098 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001099 p = (char *)buffer_ptr(&c->input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001100 if (memchr(p, '\0', have) == NULL) {
1101 error("channel %d: decode socks4a: host not nul "
1102 "terminated", c->self);
1103 return -1;
1104 }
Damien Miller1781f532009-01-28 16:24:41 +11001105 len = strlen(p);
1106 debug2("channel %d: decode socks4a: host %s/%d",
1107 c->self, p, len);
1108 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001109 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001110 error("channel %d: hostname \"%.100s\" too long",
1111 c->self, p);
1112 return -1;
1113 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001114 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001115 buffer_consume(&c->input, len);
1116 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001117 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001118
Damien Millerfbdeece2003-09-02 22:52:31 +10001119 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001120 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001121
Ben Lindstromb3921512001-04-11 15:57:50 +00001122 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001123 debug("channel %d: cannot handle: %s cn %d",
1124 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001125 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001126 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001127 s4_rsp.version = 0; /* vn: 0 for reply */
1128 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001129 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001130 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001131 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001132 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001133}
1134
Darren Tucker46471c92003-07-03 13:55:19 +10001135/* try to decode a socks5 header */
1136#define SSH_SOCKS5_AUTHDONE 0x1000
1137#define SSH_SOCKS5_NOAUTH 0x00
1138#define SSH_SOCKS5_IPV4 0x01
1139#define SSH_SOCKS5_DOMAIN 0x03
1140#define SSH_SOCKS5_IPV6 0x04
1141#define SSH_SOCKS5_CONNECT 0x01
1142#define SSH_SOCKS5_SUCCESS 0x00
1143
Damien Miller8473dd82006-07-24 14:08:32 +10001144/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001145static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001146channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001147{
1148 struct {
1149 u_int8_t version;
1150 u_int8_t command;
1151 u_int8_t reserved;
1152 u_int8_t atyp;
1153 } s5_req, s5_rsp;
1154 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001155 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1156 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001157 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001158
1159 debug2("channel %d: decode socks5", c->self);
1160 p = buffer_ptr(&c->input);
1161 if (p[0] != 0x05)
1162 return -1;
1163 have = buffer_len(&c->input);
1164 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1165 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001166 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001167 return 0;
1168 nmethods = p[1];
1169 if (have < nmethods + 2)
1170 return 0;
1171 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001172 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001173 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001174 found = 1;
1175 break;
1176 }
1177 }
1178 if (!found) {
1179 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1180 c->self);
1181 return -1;
1182 }
1183 buffer_consume(&c->input, nmethods + 2);
1184 buffer_put_char(&c->output, 0x05); /* version */
1185 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1186 FD_SET(c->sock, writeset);
1187 c->flags |= SSH_SOCKS5_AUTHDONE;
1188 debug2("channel %d: socks5 auth done", c->self);
1189 return 0; /* need more */
1190 }
1191 debug2("channel %d: socks5 post auth", c->self);
1192 if (have < sizeof(s5_req)+1)
1193 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001194 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001195 if (s5_req.version != 0x05 ||
1196 s5_req.command != SSH_SOCKS5_CONNECT ||
1197 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001198 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001199 return -1;
1200 }
Darren Tucker47eede72005-03-14 23:08:12 +11001201 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001202 case SSH_SOCKS5_IPV4:
1203 addrlen = 4;
1204 af = AF_INET;
1205 break;
1206 case SSH_SOCKS5_DOMAIN:
1207 addrlen = p[sizeof(s5_req)];
1208 af = -1;
1209 break;
1210 case SSH_SOCKS5_IPV6:
1211 addrlen = 16;
1212 af = AF_INET6;
1213 break;
1214 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001215 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001216 return -1;
1217 }
Damien Miller0f077072006-07-10 22:21:02 +10001218 need = sizeof(s5_req) + addrlen + 2;
1219 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1220 need++;
1221 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001222 return 0;
1223 buffer_consume(&c->input, sizeof(s5_req));
1224 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1225 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001226 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001227 buffer_get(&c->input, (char *)&dest_port, 2);
1228 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001229 free(c->path);
1230 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001231 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001232 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001233 error("channel %d: dynamic request: socks5 hostname "
1234 "\"%.100s\" too long", c->self, dest_addr);
1235 return -1;
1236 }
1237 c->path = xstrdup(dest_addr);
1238 } else {
1239 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1240 return -1;
1241 c->path = xstrdup(ntop);
1242 }
Darren Tucker46471c92003-07-03 13:55:19 +10001243 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001244
Damien Millerfbdeece2003-09-02 22:52:31 +10001245 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001246 c->self, c->path, c->host_port, s5_req.command);
1247
1248 s5_rsp.version = 0x05;
1249 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1250 s5_rsp.reserved = 0; /* ignored */
1251 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001252 dest_port = 0; /* ignored */
1253
Damien Millera0fdce92006-03-26 14:28:50 +11001254 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001255 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001256 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001257 return 1;
1258}
1259
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001260Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001261channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1262 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001263{
1264 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001265
1266 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1267 port_to_connect);
1268
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001269 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1270 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1271 0, "stdio-forward", /*nonblock*/0);
1272
1273 c->path = xstrdup(host_to_connect);
1274 c->host_port = port_to_connect;
1275 c->listening_port = 0;
1276 c->force_drain = 1;
1277
1278 channel_register_fds(c, in, out, -1, 0, 1, 0);
1279 port_open_helper(c, "direct-tcpip");
1280
1281 return c;
1282}
1283
Ben Lindstromb3921512001-04-11 15:57:50 +00001284/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001285static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001286channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001287{
1288 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001289 u_int have;
1290 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001291
1292 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001293 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001294 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001295 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001296 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001297 /* need more */
1298 FD_SET(c->sock, readset);
1299 return;
1300 }
1301 /* try to guess the protocol */
1302 p = buffer_ptr(&c->input);
1303 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001304 case 0x04:
1305 ret = channel_decode_socks4(c, readset, writeset);
1306 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001307 case 0x05:
1308 ret = channel_decode_socks5(c, readset, writeset);
1309 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001310 default:
1311 ret = -1;
1312 break;
1313 }
1314 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001315 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001316 } else if (ret == 0) {
1317 debug2("channel %d: pre_dynamic: need more", c->self);
1318 /* need more */
1319 FD_SET(c->sock, readset);
1320 } else {
1321 /* switch to the next state */
1322 c->type = SSH_CHANNEL_OPENING;
1323 port_open_helper(c, "direct-tcpip");
1324 }
1325}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001326
Damien Millerb38eff82000-04-01 11:09:21 +10001327/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001328/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001329static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001330channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001331{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001332 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001333 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001334 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001335 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001336 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001337 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001338
1339 if (FD_ISSET(c->sock, readset)) {
1340 debug("X11 connection requested.");
1341 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001342 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001343 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001344 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001345 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001346 channel_close_fd(&c->sock);
1347 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001348 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001349 }
Damien Millerb38eff82000-04-01 11:09:21 +10001350 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001351 if (errno != EINTR && errno != EWOULDBLOCK &&
1352 errno != ECONNABORTED)
1353 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001354 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001355 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001356 return;
1357 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001358 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001359 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001360 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001361 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001362 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001363
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001364 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001365 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001366 c->local_window_max, c->local_maxpacket, 0, buf, 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001367 packet_start(SSH2_MSG_CHANNEL_OPEN);
1368 packet_put_cstring("x11");
1369 packet_put_int(nc->self);
1370 packet_put_int(nc->local_window_max);
1371 packet_put_int(nc->local_maxpacket);
1372 /* originator ipaddr and port */
1373 packet_put_cstring(remote_ipaddr);
1374 if (datafellows & SSH_BUG_X11FWD) {
1375 debug2("ssh2 x11 bug compat mode");
Damien Millerbd483e72000-04-30 10:00:53 +10001376 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001377 packet_put_int(remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001378 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001379 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001380 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001381 }
1382}
1383
Ben Lindstrombba81212001-06-25 05:01:22 +00001384static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001385port_open_helper(Channel *c, char *rtype)
1386{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001387 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001388 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001389 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001390 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001391 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001392
Damien Millerd6369432010-02-02 17:02:07 +11001393 if (remote_port == -1) {
1394 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001395 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001396 remote_ipaddr = xstrdup("127.0.0.1");
1397 remote_port = 65535;
1398 }
1399
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001400 snprintf(buf, sizeof buf,
1401 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001402 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001403 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001404 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001405
Darren Tuckera627d422013-06-02 07:31:17 +10001406 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001407 c->remote_name = xstrdup(buf);
1408
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001409 packet_start(SSH2_MSG_CHANNEL_OPEN);
1410 packet_put_cstring(rtype);
1411 packet_put_int(c->self);
1412 packet_put_int(c->local_window_max);
1413 packet_put_int(c->local_maxpacket);
1414 if (strcmp(rtype, "direct-tcpip") == 0) {
1415 /* target host, port */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001416 packet_put_cstring(c->path);
1417 packet_put_int(c->host_port);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001418 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1419 /* target path */
1420 packet_put_cstring(c->path);
1421 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1422 /* listen path */
1423 packet_put_cstring(c->path);
1424 } else {
1425 /* listen address, port */
1426 packet_put_cstring(c->path);
1427 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001428 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001429 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1430 /* reserved for future owner/mode info */
1431 packet_put_cstring("");
1432 } else {
1433 /* originator host and port */
1434 packet_put_cstring(remote_ipaddr);
1435 packet_put_int((u_int)remote_port);
1436 }
1437 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001438 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001439 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001440}
1441
Damien Miller5e7fd072005-11-05 14:53:39 +11001442static void
1443channel_set_reuseaddr(int fd)
1444{
1445 int on = 1;
1446
1447 /*
1448 * Set socket options.
1449 * Allow local port reuse in TIME_WAIT.
1450 */
1451 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1452 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1453}
1454
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001455void
1456channel_set_x11_refuse_time(u_int refuse_time)
1457{
1458 x11_refuse_time = refuse_time;
1459}
1460
Damien Millerb38eff82000-04-01 11:09:21 +10001461/*
1462 * This socket is listening for connections to a forwarded TCP/IP port.
1463 */
Damien Miller8473dd82006-07-24 14:08:32 +10001464/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001465static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001466channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001467{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001468 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001469 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001470 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001471 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001472 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001473
Damien Millerb38eff82000-04-01 11:09:21 +10001474 if (FD_ISSET(c->sock, readset)) {
1475 debug("Connection to port %d forwarding "
1476 "to %.100s port %d requested.",
1477 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001478
Damien Miller4623a752001-10-10 15:03:58 +10001479 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1480 nextstate = SSH_CHANNEL_OPENING;
1481 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001482 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1483 nextstate = SSH_CHANNEL_OPENING;
1484 rtype = "forwarded-streamlocal@openssh.com";
1485 } else if (c->host_port == PORT_STREAMLOCAL) {
1486 nextstate = SSH_CHANNEL_OPENING;
1487 rtype = "direct-streamlocal@openssh.com";
1488 } else if (c->host_port == 0) {
1489 nextstate = SSH_CHANNEL_DYNAMIC;
1490 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001491 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001492 nextstate = SSH_CHANNEL_OPENING;
1493 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001494 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001495
Damien Millerb38eff82000-04-01 11:09:21 +10001496 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001497 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001498 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001499 if (errno != EINTR && errno != EWOULDBLOCK &&
1500 errno != ECONNABORTED)
1501 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001502 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001503 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001504 return;
1505 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001506 if (c->host_port != PORT_STREAMLOCAL)
1507 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001508 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1509 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001510 nc->listening_port = c->listening_port;
1511 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001512 if (c->path != NULL)
1513 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001514
Darren Tucker876045b2010-01-08 17:08:00 +11001515 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001516 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001517 }
1518}
1519
1520/*
1521 * This is the authentication agent socket listening for connections from
1522 * clients.
1523 */
Damien Miller8473dd82006-07-24 14:08:32 +10001524/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001525static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001526channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001527{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001528 Channel *nc;
1529 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001530 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001531 socklen_t addrlen;
1532
1533 if (FD_ISSET(c->sock, readset)) {
1534 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001535 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001536 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001537 error("accept from auth socket: %.100s",
1538 strerror(errno));
1539 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001540 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001541 return;
1542 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001543 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001544 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1545 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001546 0, "accepted auth socket", 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001547 packet_start(SSH2_MSG_CHANNEL_OPEN);
1548 packet_put_cstring("auth-agent@openssh.com");
1549 packet_put_int(nc->self);
1550 packet_put_int(c->local_window_max);
1551 packet_put_int(c->local_maxpacket);
Damien Millerb38eff82000-04-01 11:09:21 +10001552 packet_send();
1553 }
1554}
1555
Damien Miller8473dd82006-07-24 14:08:32 +10001556/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001557static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001558channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001559{
Damien Millerbd740252008-05-19 15:37:09 +10001560 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001561 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001562
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001563 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001564 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001565 err = errno;
1566 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001567 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001568 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001569 debug("channel %d: connected to %s port %d",
1570 c->self, c->connect_ctx.host, c->connect_ctx.port);
1571 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001572 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001573 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1574 packet_put_int(c->remote_id);
1575 packet_put_int(c->self);
1576 packet_put_int(c->local_window);
1577 packet_put_int(c->local_maxpacket);
Ben Lindstrom69128662001-05-08 20:07:39 +00001578 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001579 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001580 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001581 /* Try next address, if any */
1582 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1583 close(c->sock);
1584 c->sock = c->rfd = c->wfd = sock;
1585 channel_max_fd = channel_find_maxfd();
1586 return;
1587 }
1588 /* Exhausted all addresses */
1589 error("connect_to %.100s port %d: failed.",
1590 c->connect_ctx.host, c->connect_ctx.port);
1591 channel_connect_ctx_free(&c->connect_ctx);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001592 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1593 packet_put_int(c->remote_id);
1594 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1595 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1596 packet_put_cstring(strerror(err));
1597 packet_put_cstring("");
Ben Lindstrom69128662001-05-08 20:07:39 +00001598 }
1599 chan_mark_dead(c);
1600 }
1601 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001602 }
1603}
1604
Damien Miller8473dd82006-07-24 14:08:32 +10001605/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001606static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001607channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001608{
Darren Tucker11327cc2005-03-14 23:22:25 +11001609 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001610 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001611
Damien Miller835284b2007-06-11 13:03:16 +10001612 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1613 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001614 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001615 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001616 if (len < 0 && (errno == EINTR ||
1617 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001618 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001619#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001620 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001621#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001622 if ((!c->isatty && len <= 0) ||
1623 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001624#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001625 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001626 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001627 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001628 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001629 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001630 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001631 } else {
1632 chan_read_failed(c);
1633 }
1634 return -1;
1635 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001636 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001637 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001638 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001639 chan_read_failed(c);
1640 }
Damien Millerd27b9472005-12-13 19:29:02 +11001641 } else if (c->datagram) {
1642 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001643 } else {
1644 buffer_append(&c->input, buf, len);
1645 }
Damien Millerb38eff82000-04-01 11:09:21 +10001646 }
1647 return 1;
1648}
Damien Miller4f7becb2006-03-26 14:10:14 +11001649
Damien Miller8473dd82006-07-24 14:08:32 +10001650/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001651static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001652channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001653{
Ben Lindstrome229b252001-03-05 06:28:06 +00001654 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001655 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001656 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001657 int len;
1658
1659 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001660 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001661 FD_ISSET(c->wfd, writeset) &&
1662 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001663 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001664 if (c->output_filter != NULL) {
1665 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1666 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001667 if (c->type != SSH_CHANNEL_OPEN)
1668 chan_mark_dead(c);
1669 else
1670 chan_write_failed(c);
1671 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001672 }
1673 } else if (c->datagram) {
1674 buf = data = buffer_get_string(&c->output, &dlen);
1675 } else {
1676 buf = data = buffer_ptr(&c->output);
1677 dlen = buffer_len(&c->output);
1678 }
1679
Damien Millerd27b9472005-12-13 19:29:02 +11001680 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001681 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001682 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001683 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001684 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1685 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001686 return 1;
1687 if (len <= 0) {
1688 if (c->type != SSH_CHANNEL_OPEN)
1689 chan_mark_dead(c);
1690 else
1691 chan_write_failed(c);
1692 return -1;
1693 }
Damien Miller7d457182010-08-05 23:09:48 +10001694 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001695 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001696#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001697 /* XXX: Later AIX versions can't push as much data to tty */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001698 if (c->wfd_isatty)
Darren Tucker240fdfa2003-11-22 14:10:02 +11001699 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001700#endif
Damien Miller077b2382005-12-31 16:22:32 +11001701
1702 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001703 if (len < 0 &&
1704 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001705 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001706 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001707 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001708 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001709 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001710 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001711 } else {
1712 chan_write_failed(c);
1713 }
1714 return -1;
1715 }
Darren Tucker3980b632009-08-28 11:02:37 +10001716#ifndef BROKEN_TCGETATTR_ICANON
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001717 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001718 if (tcgetattr(c->wfd, &tio) == 0 &&
1719 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1720 /*
1721 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001722 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001723 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001724 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001725 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001726 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001727 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001728 }
1729 }
Darren Tucker3980b632009-08-28 11:02:37 +10001730#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001731 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001732 }
Damien Miller7d457182010-08-05 23:09:48 +10001733 out:
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001734 if (olen > 0)
Damien Miller7d457182010-08-05 23:09:48 +10001735 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001736 return 1;
1737}
Damien Miller4f7becb2006-03-26 14:10:14 +11001738
Ben Lindstrombba81212001-06-25 05:01:22 +00001739static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001740channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001741{
Darren Tucker11327cc2005-03-14 23:22:25 +11001742 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001743 int len;
1744
Damien Miller1383bd82000-04-06 12:32:37 +10001745/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001746 if (c->efd != -1) {
1747 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1748 FD_ISSET(c->efd, writeset) &&
1749 buffer_len(&c->extended) > 0) {
1750 len = write(c->efd, buffer_ptr(&c->extended),
1751 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001752 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001753 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001754 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1755 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001756 return 1;
1757 if (len <= 0) {
1758 debug2("channel %d: closing write-efd %d",
1759 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001760 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001761 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001762 buffer_consume(&c->extended, len);
1763 c->local_consumed += len;
1764 }
Damien Miller8853ca52010-06-26 10:00:14 +10001765 } else if (c->efd != -1 &&
1766 (c->extended_usage == CHAN_EXTENDED_READ ||
1767 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001768 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001769 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001770 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001771 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001772 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1773 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001774 return 1;
1775 if (len <= 0) {
1776 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001777 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001778 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001779 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001780 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1781 debug3("channel %d: discard efd",
1782 c->self);
1783 } else
1784 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001785 }
Damien Miller33b13562000-04-04 14:38:59 +10001786 }
1787 }
1788 return 1;
1789}
Damien Miller4f7becb2006-03-26 14:10:14 +11001790
Damien Miller0e220db2004-06-15 10:34:08 +10001791static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001792channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001793{
Ben Lindstromb3921512001-04-11 15:57:50 +00001794 if (c->type == SSH_CHANNEL_OPEN &&
1795 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001796 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001797 c->local_maxpacket*3) ||
1798 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001799 c->local_consumed > 0) {
1800 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1801 packet_put_int(c->remote_id);
1802 packet_put_int(c->local_consumed);
1803 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001804 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001805 c->self, c->local_window,
1806 c->local_consumed);
1807 c->local_window += c->local_consumed;
1808 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001809 }
1810 return 1;
1811}
1812
Ben Lindstrombba81212001-06-25 05:01:22 +00001813static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001814channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001815{
1816 channel_handle_rfd(c, readset, writeset);
1817 channel_handle_wfd(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +10001818 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001819 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001820}
1821
Damien Millere1537f92010-01-26 13:26:22 +11001822static u_int
1823read_mux(Channel *c, u_int need)
1824{
1825 char buf[CHAN_RBUF];
1826 int len;
1827 u_int rlen;
1828
1829 if (buffer_len(&c->input) < need) {
1830 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001831 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001832 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1833 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001834 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001835 debug2("channel %d: ctl read<=0 rfd %d len %d",
1836 c->self, c->rfd, len);
1837 chan_read_failed(c);
1838 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001839 } else
1840 buffer_append(&c->input, buf, len);
1841 }
1842 return buffer_len(&c->input);
1843}
1844
1845static void
1846channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1847{
1848 u_int need;
1849 ssize_t len;
1850
Damien Millerd530f5f2010-05-21 14:57:10 +10001851 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001852 (c->istate == CHAN_INPUT_OPEN ||
1853 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1854 /*
1855 * Don't not read past the precise end of packets to
1856 * avoid disrupting fd passing.
1857 */
1858 if (read_mux(c, 4) < 4) /* read header */
1859 return;
1860 need = get_u32(buffer_ptr(&c->input));
1861#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1862 if (need > CHANNEL_MUX_MAX_PACKET) {
1863 debug2("channel %d: packet too big %u > %u",
1864 c->self, CHANNEL_MUX_MAX_PACKET, need);
1865 chan_rcvd_oclose(c);
1866 return;
1867 }
1868 if (read_mux(c, need + 4) < need + 4) /* read body */
1869 return;
1870 if (c->mux_rcb(c) != 0) {
1871 debug("channel %d: mux_rcb failed", c->self);
1872 chan_mark_dead(c);
1873 return;
1874 }
1875 }
1876
1877 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1878 buffer_len(&c->output) > 0) {
1879 len = write(c->wfd, buffer_ptr(&c->output),
1880 buffer_len(&c->output));
1881 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1882 return;
1883 if (len <= 0) {
1884 chan_mark_dead(c);
1885 return;
1886 }
1887 buffer_consume(&c->output, len);
1888 }
1889}
1890
1891static void
1892channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1893{
1894 Channel *nc;
1895 struct sockaddr_storage addr;
1896 socklen_t addrlen;
1897 int newsock;
1898 uid_t euid;
1899 gid_t egid;
1900
1901 if (!FD_ISSET(c->sock, readset))
1902 return;
1903
1904 debug("multiplexing control connection");
1905
1906 /*
1907 * Accept connection on control socket
1908 */
1909 memset(&addr, 0, sizeof(addr));
1910 addrlen = sizeof(addr);
1911 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1912 &addrlen)) == -1) {
1913 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001914 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001915 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001916 return;
1917 }
1918
1919 if (getpeereid(newsock, &euid, &egid) < 0) {
1920 error("%s getpeereid failed: %s", __func__,
1921 strerror(errno));
1922 close(newsock);
1923 return;
1924 }
1925 if ((euid != 0) && (getuid() != euid)) {
1926 error("multiplex uid mismatch: peer euid %u != uid %u",
1927 (u_int)euid, (u_int)getuid());
1928 close(newsock);
1929 return;
1930 }
1931 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
1932 newsock, newsock, -1, c->local_window_max,
1933 c->local_maxpacket, 0, "mux-control", 1);
1934 nc->mux_rcb = c->mux_rcb;
1935 debug3("%s: new mux channel %d fd %d", __func__,
1936 nc->self, nc->sock);
1937 /* establish state */
1938 nc->mux_rcb(nc);
1939 /* mux state transitions must not elicit protocol messages */
1940 nc->flags |= CHAN_LOCAL;
1941}
1942
Ben Lindstrombba81212001-06-25 05:01:22 +00001943static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001944channel_handler_init(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001945{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001946 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001947
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001948 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1949 channel_pre[i] = NULL;
1950 channel_post[i] = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001951 }
Damien Millerde6987c2002-01-22 23:20:40 +11001952 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001953 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001954 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001955 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001956 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
1957 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001958 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001959 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001960 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001961 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11001962 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
1963 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001964
Damien Millerde6987c2002-01-22 23:20:40 +11001965 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001966 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001967 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001968 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
1969 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001970 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001971 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001972 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001973 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11001974 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
1975 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001976}
1977
Damien Miller3ec27592001-10-12 11:35:04 +10001978/* gc dead channels */
1979static void
1980channel_garbage_collect(Channel *c)
1981{
1982 if (c == NULL)
1983 return;
1984 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001985 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001986 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001987 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001988 c->detach_user(c->self, NULL);
1989 /* if we still have a callback */
1990 if (c->detach_user != NULL)
1991 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001992 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001993 }
1994 if (!chan_is_dead(c, 1))
1995 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001996 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001997 channel_free(c);
1998}
1999
Ben Lindstrombba81212001-06-25 05:01:22 +00002000static void
Damien Millera6508752012-04-22 11:21:10 +10002001channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2002 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002003{
2004 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002005 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002006 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002007 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002008
2009 if (!did_init) {
2010 channel_handler_init();
2011 did_init = 1;
2012 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002013 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002014 if (unpause_secs != NULL)
2015 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002016 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002017 c = channels[i];
2018 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002019 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002020 if (c->delayed) {
2021 if (ftab == channel_pre)
2022 c->delayed = 0;
2023 else
2024 continue;
2025 }
Damien Millera6508752012-04-22 11:21:10 +10002026 if (ftab[c->type] != NULL) {
2027 /*
2028 * Run handlers that are not paused.
2029 */
2030 if (c->notbefore <= now)
2031 (*ftab[c->type])(c, readset, writeset);
2032 else if (unpause_secs != NULL) {
2033 /*
2034 * Collect the time that the earliest
2035 * channel comes off pause.
2036 */
2037 debug3("%s: chan %d: skip for %d more seconds",
2038 __func__, c->self,
2039 (int)(c->notbefore - now));
2040 if (*unpause_secs == 0 ||
2041 (c->notbefore - now) < *unpause_secs)
2042 *unpause_secs = c->notbefore - now;
2043 }
2044 }
Damien Miller3ec27592001-10-12 11:35:04 +10002045 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002046 }
Damien Millera6508752012-04-22 11:21:10 +10002047 if (unpause_secs != NULL && *unpause_secs != 0)
2048 debug3("%s: first channel unpauses in %d seconds",
2049 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002050}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002051
Ben Lindstrome9c99912001-06-09 00:41:05 +00002052/*
2053 * Allocate/update select bitmasks and add any bits relevant to channels in
2054 * select bitmasks.
2055 */
Damien Miller4af51302000-04-16 11:18:38 +10002056void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002057channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002058 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002059{
Damien Miller36812092006-03-26 14:22:47 +11002060 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002061
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002062 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002063
Damien Miller36812092006-03-26 14:22:47 +11002064 nfdset = howmany(n+1, NFDBITS);
2065 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002066 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002067 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2068 sz = nfdset * sizeof(fd_mask);
2069
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002070 /* perhaps check sz < nalloc/2 and shrink? */
2071 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002072 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2073 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002074 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002075 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002076 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002077 memset(*readsetp, 0, sz);
2078 memset(*writesetp, 0, sz);
2079
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002080 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002081 channel_handler(channel_pre, *readsetp, *writesetp,
2082 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002083}
2084
Ben Lindstrome9c99912001-06-09 00:41:05 +00002085/*
2086 * After select, perform any appropriate operations for channels which have
2087 * events pending.
2088 */
Damien Miller4af51302000-04-16 11:18:38 +10002089void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002090channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091{
Damien Millera6508752012-04-22 11:21:10 +10002092 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002093}
2094
Ben Lindstrome9c99912001-06-09 00:41:05 +00002095
Damien Miller5e953212001-01-30 09:14:00 +11002096/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002097void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002098channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099{
Damien Millerb38eff82000-04-01 11:09:21 +10002100 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002101 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002102
Damien Miller95def091999-11-25 00:26:21 +11002103 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002104 c = channels[i];
2105 if (c == NULL)
2106 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002107
Ben Lindstrome9c99912001-06-09 00:41:05 +00002108 /*
2109 * We are only interested in channels that can have buffered
2110 * incoming data.
2111 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002112 if (c->type != SSH_CHANNEL_OPEN)
2113 continue;
2114 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002115 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002116 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002117 continue;
2118 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119
Damien Miller95def091999-11-25 00:26:21 +11002120 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002121 if ((c->istate == CHAN_INPUT_OPEN ||
2122 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2123 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002124 if (c->datagram) {
2125 if (len > 0) {
2126 u_char *data;
2127 u_int dlen;
2128
2129 data = buffer_get_string(&c->input,
2130 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002131 if (dlen > c->remote_window ||
2132 dlen > c->remote_maxpacket) {
2133 debug("channel %d: datagram "
2134 "too big for channel",
2135 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002136 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002137 continue;
2138 }
Damien Millerd27b9472005-12-13 19:29:02 +11002139 packet_start(SSH2_MSG_CHANNEL_DATA);
2140 packet_put_int(c->remote_id);
2141 packet_put_string(data, dlen);
2142 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002143 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002144 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002145 }
2146 continue;
2147 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002148 /*
2149 * Send some data for the other side over the secure
2150 * connection.
2151 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002152 if (len > c->remote_window)
2153 len = c->remote_window;
2154 if (len > c->remote_maxpacket)
2155 len = c->remote_maxpacket;
Damien Millerb38eff82000-04-01 11:09:21 +10002156 if (len > 0) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002157 packet_start(SSH2_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002158 packet_put_int(c->remote_id);
2159 packet_put_string(buffer_ptr(&c->input), len);
2160 packet_send();
2161 buffer_consume(&c->input, len);
2162 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002163 }
2164 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller5428f641999-11-25 11:54:57 +11002165 /*
2166 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002167 * tell peer, that we will not send more data: send IEOF.
2168 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002169 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002170 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002171 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2172 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002173 else
2174 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002175 }
Damien Miller33b13562000-04-04 14:38:59 +10002176 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002177 if (!(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002178 c->remote_window > 0 &&
2179 (len = buffer_len(&c->extended)) > 0 &&
2180 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002181 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002182 c->self, c->remote_window, buffer_len(&c->extended),
2183 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002184 if (len > c->remote_window)
2185 len = c->remote_window;
2186 if (len > c->remote_maxpacket)
2187 len = c->remote_maxpacket;
2188 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2189 packet_put_int(c->remote_id);
2190 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2191 packet_put_string(buffer_ptr(&c->extended), len);
2192 packet_send();
2193 buffer_consume(&c->extended, len);
2194 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002195 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002196 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002197 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002198}
2199
markus@openbsd.org8d057842016-09-30 09:19:13 +00002200/* -- mux proxy support */
2201
2202/*
2203 * When multiplexing channel messages for mux clients we have to deal
2204 * with downstream messages from the mux client and upstream messages
2205 * from the ssh server:
2206 * 1) Handling downstream messages is straightforward and happens
2207 * in channel_proxy_downstream():
2208 * - We forward all messages (mostly) unmodified to the server.
2209 * - However, in order to route messages from upstream to the correct
2210 * downstream client, we have to replace the channel IDs used by the
2211 * mux clients with a unique channel ID because the mux clients might
2212 * use conflicting channel IDs.
2213 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2214 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2215 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2216 * with the newly allocated channel ID.
2217 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2218 * channels and procesed by channel_proxy_upstream(). The local channel ID
2219 * is then translated back to the original mux client ID.
2220 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2221 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2222 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2223 * downstream mux client are removed.
2224 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2225 * requires more work, because they are not addressed to a specific
2226 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2227 * out whether the request is addressed to the local client or a
2228 * specific downstream client based on the listen-address/port.
2229 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2230 * not supported as the matching session/channel cannot be identified
2231 * easily.
2232 */
2233
2234/*
2235 * receive packets from downstream mux clients:
2236 * channel callback fired on read from mux client, creates
2237 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2238 * on channel creation.
2239 */
2240int
2241channel_proxy_downstream(Channel *downstream)
2242{
2243 Channel *c = NULL;
2244 struct ssh *ssh = active_state;
2245 struct sshbuf *original = NULL, *modified = NULL;
2246 const u_char *cp;
2247 char *ctype = NULL, *listen_host = NULL;
2248 u_char type;
2249 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002250 int ret = -1, r, idx;
2251 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002252
2253 /* sshbuf_dump(&downstream->input, stderr); */
2254 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2255 != 0) {
2256 error("%s: malformed message: %s", __func__, ssh_err(r));
2257 return -1;
2258 }
2259 if (have < 2) {
2260 error("%s: short message", __func__);
2261 return -1;
2262 }
2263 type = cp[1];
2264 /* skip padlen + type */
2265 cp += 2;
2266 have -= 2;
2267 if (ssh_packet_log_type(type))
2268 debug3("%s: channel %u: down->up: type %u", __func__,
2269 downstream->self, type);
2270
2271 switch (type) {
2272 case SSH2_MSG_CHANNEL_OPEN:
2273 if ((original = sshbuf_from(cp, have)) == NULL ||
2274 (modified = sshbuf_new()) == NULL) {
2275 error("%s: alloc", __func__);
2276 goto out;
2277 }
2278 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2279 (r = sshbuf_get_u32(original, &id)) != 0) {
2280 error("%s: parse error %s", __func__, ssh_err(r));
2281 goto out;
2282 }
2283 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2284 -1, -1, -1, 0, 0, 0, ctype, 1);
2285 c->mux_ctx = downstream; /* point to mux client */
2286 c->mux_downstream_id = id; /* original downstream id */
2287 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2288 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2289 (r = sshbuf_putb(modified, original)) != 0) {
2290 error("%s: compose error %s", __func__, ssh_err(r));
2291 channel_free(c);
2292 goto out;
2293 }
2294 break;
2295 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2296 /*
2297 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2298 * need to parse 'remote_id' instead of 'ctype'.
2299 */
2300 if ((original = sshbuf_from(cp, have)) == NULL ||
2301 (modified = sshbuf_new()) == NULL) {
2302 error("%s: alloc", __func__);
2303 goto out;
2304 }
2305 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2306 (r = sshbuf_get_u32(original, &id)) != 0) {
2307 error("%s: parse error %s", __func__, ssh_err(r));
2308 goto out;
2309 }
2310 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2311 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2312 c->mux_ctx = downstream; /* point to mux client */
2313 c->mux_downstream_id = id;
2314 c->remote_id = remote_id;
2315 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2316 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2317 (r = sshbuf_putb(modified, original)) != 0) {
2318 error("%s: compose error %s", __func__, ssh_err(r));
2319 channel_free(c);
2320 goto out;
2321 }
2322 break;
2323 case SSH2_MSG_GLOBAL_REQUEST:
2324 if ((original = sshbuf_from(cp, have)) == NULL) {
2325 error("%s: alloc", __func__);
2326 goto out;
2327 }
2328 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2329 error("%s: parse error %s", __func__, ssh_err(r));
2330 goto out;
2331 }
2332 if (strcmp(ctype, "tcpip-forward") != 0) {
2333 error("%s: unsupported request %s", __func__, ctype);
2334 goto out;
2335 }
2336 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2337 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2338 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2339 error("%s: parse error %s", __func__, ssh_err(r));
2340 goto out;
2341 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002342 if (listen_port > 65535) {
2343 error("%s: tcpip-forward for %s: bad port %u",
2344 __func__, listen_host, listen_port);
2345 goto out;
2346 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002347 /* Record that connection to this host/port is permitted. */
2348 permitted_opens = xreallocarray(permitted_opens,
2349 num_permitted_opens + 1, sizeof(*permitted_opens));
2350 idx = num_permitted_opens++;
2351 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2352 permitted_opens[idx].port_to_connect = -1;
2353 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002354 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002355 permitted_opens[idx].downstream = downstream;
2356 listen_host = NULL;
2357 break;
2358 case SSH2_MSG_CHANNEL_CLOSE:
2359 if (have < 4)
2360 break;
2361 remote_id = PEEK_U32(cp);
2362 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2363 if (c->flags & CHAN_CLOSE_RCVD)
2364 channel_free(c);
2365 else
2366 c->flags |= CHAN_CLOSE_SENT;
2367 }
2368 break;
2369 }
2370 if (modified) {
2371 if ((r = sshpkt_start(ssh, type)) != 0 ||
2372 (r = sshpkt_putb(ssh, modified)) != 0 ||
2373 (r = sshpkt_send(ssh)) != 0) {
2374 error("%s: send %s", __func__, ssh_err(r));
2375 goto out;
2376 }
2377 } else {
2378 if ((r = sshpkt_start(ssh, type)) != 0 ||
2379 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2380 (r = sshpkt_send(ssh)) != 0) {
2381 error("%s: send %s", __func__, ssh_err(r));
2382 goto out;
2383 }
2384 }
2385 ret = 0;
2386 out:
2387 free(ctype);
2388 free(listen_host);
2389 sshbuf_free(original);
2390 sshbuf_free(modified);
2391 return ret;
2392}
2393
2394/*
2395 * receive packets from upstream server and de-multiplex packets
2396 * to correct downstream:
2397 * implemented as a helper for channel input handlers,
2398 * replaces local (proxy) channel ID with downstream channel ID.
2399 */
2400int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002401channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002402{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002403 struct sshbuf *b = NULL;
2404 Channel *downstream;
2405 const u_char *cp = NULL;
2406 size_t len;
2407 int r;
2408
2409 /*
2410 * When receiving packets from the peer we need to check whether we
2411 * need to forward the packets to the mux client. In this case we
2412 * restore the orignal channel id and keep track of CLOSE messages,
2413 * so we can cleanup the channel.
2414 */
2415 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2416 return 0;
2417 if ((downstream = c->mux_ctx) == NULL)
2418 return 0;
2419 switch (type) {
2420 case SSH2_MSG_CHANNEL_CLOSE:
2421 case SSH2_MSG_CHANNEL_DATA:
2422 case SSH2_MSG_CHANNEL_EOF:
2423 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2424 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2425 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2426 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2427 case SSH2_MSG_CHANNEL_SUCCESS:
2428 case SSH2_MSG_CHANNEL_FAILURE:
2429 case SSH2_MSG_CHANNEL_REQUEST:
2430 break;
2431 default:
2432 debug2("%s: channel %u: unsupported type %u", __func__,
2433 c->self, type);
2434 return 0;
2435 }
2436 if ((b = sshbuf_new()) == NULL) {
2437 error("%s: alloc reply", __func__);
2438 goto out;
2439 }
2440 /* get remaining payload (after id) */
2441 cp = sshpkt_ptr(ssh, &len);
2442 if (cp == NULL) {
2443 error("%s: no packet", __func__);
2444 goto out;
2445 }
2446 /* translate id and send to muxclient */
2447 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2448 (r = sshbuf_put_u8(b, type)) != 0 ||
2449 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2450 (r = sshbuf_put(b, cp, len)) != 0 ||
2451 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2452 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2453 goto out;
2454 }
2455 /* sshbuf_dump(b, stderr); */
2456 if (ssh_packet_log_type(type))
2457 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2458 type);
2459 out:
2460 /* update state */
2461 switch (type) {
2462 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2463 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2464 if (cp && len > 4)
2465 c->remote_id = PEEK_U32(cp);
2466 break;
2467 case SSH2_MSG_CHANNEL_CLOSE:
2468 if (c->flags & CHAN_CLOSE_SENT)
2469 channel_free(c);
2470 else
2471 c->flags |= CHAN_CLOSE_RCVD;
2472 break;
2473 }
2474 sshbuf_free(b);
2475 return 1;
2476}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002477
2478/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002479
2480/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002481int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002482channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002483{
Damien Miller34132e52000-01-14 15:45:46 +11002484 int id;
Damien Miller633de332014-05-15 13:48:26 +10002485 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002486 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002487 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002488
Damien Miller95def091999-11-25 00:26:21 +11002489 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002490 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002491 c = channel_lookup(id);
2492 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002493 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002494 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002495 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002496
Damien Miller95def091999-11-25 00:26:21 +11002497 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002498 if (c->type != SSH_CHANNEL_OPEN &&
2499 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002500 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002501
Damien Miller95def091999-11-25 00:26:21 +11002502 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002503 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002504 win_len = data_len;
2505 if (c->datagram)
2506 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002507
Damien Millera04ad492004-01-21 11:02:09 +11002508 /*
2509 * Ignore data for protocol > 1.3 if output end is no longer open.
2510 * For protocol 2 the sending side is reducing its window as it sends
2511 * data, so we must 'fake' consumption of the data in order to ensure
2512 * that window updates are sent back. Otherwise the connection might
2513 * deadlock.
2514 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002515 if (c->ostate != CHAN_OUTPUT_OPEN) {
2516 c->local_window -= win_len;
2517 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002518 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002519 }
2520
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002521 if (win_len > c->local_maxpacket) {
2522 logit("channel %d: rcvd big packet %d, maxpack %d",
2523 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002524 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002525 if (win_len > c->local_window) {
2526 logit("channel %d: rcvd too much data %d, win %d",
2527 c->self, win_len, c->local_window);
2528 return 0;
2529 }
2530 c->local_window -= win_len;
2531
Damien Millerd27b9472005-12-13 19:29:02 +11002532 if (c->datagram)
2533 buffer_put_string(&c->output, data, data_len);
2534 else
2535 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002536 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002537 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002538}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002539
Damien Millerd79b4242006-03-31 23:11:44 +11002540/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002541int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002542channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002543{
2544 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002545 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002546 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002547 Channel *c;
2548
2549 /* Get the channel number and verify it. */
2550 id = packet_get_int();
2551 c = channel_lookup(id);
2552
2553 if (c == NULL)
2554 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002555 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002556 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002557 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002558 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002559 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002560 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002561 if (c->flags & CHAN_EOF_RCVD) {
2562 if (datafellows & SSH_BUG_EXTEOF)
2563 debug("channel %d: accepting ext data after eof", id);
2564 else
2565 packet_disconnect("Received extended_data after EOF "
2566 "on channel %d.", id);
2567 }
Damien Miller33b13562000-04-04 14:38:59 +10002568 tcode = packet_get_int();
2569 if (c->efd == -1 ||
2570 c->extended_usage != CHAN_EXTENDED_WRITE ||
2571 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002572 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002573 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002574 }
2575 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002576 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002577 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002578 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002579 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002580 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002581 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002582 }
Damien Millerd3444942000-09-30 14:20:03 +11002583 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002584 c->local_window -= data_len;
2585 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002586 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002587 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002588}
2589
Damien Millerd79b4242006-03-31 23:11:44 +11002590/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002591int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002592channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10002593{
2594 int id;
2595 Channel *c;
2596
Damien Millerb38eff82000-04-01 11:09:21 +10002597 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002598 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002599 c = channel_lookup(id);
2600 if (c == NULL)
2601 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002602 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002603 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002604 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002605
2606 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002607 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002608 debug("channel %d: FORCE input drain", c->self);
2609 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002610 if (buffer_len(&c->input) == 0)
2611 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002612 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002613 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002614}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002615
Damien Millerd79b4242006-03-31 23:11:44 +11002616/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002617int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002618channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002619{
Damien Millerb38eff82000-04-01 11:09:21 +10002620 int id = packet_get_int();
2621 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002622
Damien Millerb38eff82000-04-01 11:09:21 +10002623 if (c == NULL)
2624 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002625 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002626 return 0;
2627 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002628 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002629 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002630}
2631
Damien Millerd79b4242006-03-31 23:11:44 +11002632/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002633int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002634channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002635{
Damien Millerb38eff82000-04-01 11:09:21 +10002636 int id, remote_id;
2637 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002638
Damien Millerb38eff82000-04-01 11:09:21 +10002639 id = packet_get_int();
2640 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002641
markus@openbsd.org8d057842016-09-30 09:19:13 +00002642 if (c==NULL)
2643 packet_disconnect("Received open confirmation for "
2644 "unknown channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002645 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002646 return 0;
2647 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002648 packet_disconnect("Received open confirmation for "
2649 "non-opening channel %d.", id);
2650 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002651 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002652 c->remote_id = remote_id;
2653 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002654
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002655 c->remote_window = packet_get_int();
2656 c->remote_maxpacket = packet_get_int();
2657 if (c->open_confirm) {
2658 debug2("callback start");
2659 c->open_confirm(c->self, 1, c->open_confirm_ctx);
2660 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002661 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002662 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2663 c->remote_window, c->remote_maxpacket);
Damien Miller48b03fc2002-01-22 23:11:40 +11002664 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002665 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002666}
2667
Ben Lindstrombba81212001-06-25 05:01:22 +00002668static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002669reason2txt(int reason)
2670{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002671 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002672 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2673 return "administratively prohibited";
2674 case SSH2_OPEN_CONNECT_FAILED:
2675 return "connect failed";
2676 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2677 return "unknown channel type";
2678 case SSH2_OPEN_RESOURCE_SHORTAGE:
2679 return "resource shortage";
2680 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002681 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002682}
2683
Damien Millerd79b4242006-03-31 23:11:44 +11002684/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002685int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002686channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002687{
Kevin Steves12057502001-02-05 14:54:34 +00002688 int id, reason;
2689 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002690 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002691
Damien Millerb38eff82000-04-01 11:09:21 +10002692 id = packet_get_int();
2693 c = channel_lookup(id);
2694
markus@openbsd.org8d057842016-09-30 09:19:13 +00002695 if (c==NULL)
2696 packet_disconnect("Received open failure for "
2697 "unknown channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002698 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002699 return 0;
2700 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002701 packet_disconnect("Received open failure for "
2702 "non-opening channel %d.", id);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002703 reason = packet_get_int();
2704 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2705 msg = packet_get_string(NULL);
2706 lang = packet_get_string(NULL);
2707 }
2708 logit("channel %d: open failed: %s%s%s", id,
2709 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2710 free(msg);
2711 free(lang);
2712 if (c->open_confirm) {
2713 debug2("callback start");
2714 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2715 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002716 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002717 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002718 /* Schedule the channel for cleanup/deletion. */
2719 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002720 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002721}
2722
Damien Millerd79b4242006-03-31 23:11:44 +11002723/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002724int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002725channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002726{
2727 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002728 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00002729 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10002730
Damien Miller33b13562000-04-04 14:38:59 +10002731 /* Get the channel number and verify it. */
2732 id = packet_get_int();
2733 c = channel_lookup(id);
2734
Damien Millerd47c62a2005-12-13 19:33:57 +11002735 if (c == NULL) {
2736 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002737 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002738 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002739 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002740 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002741 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002742 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002743 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00002744 if ((tmp = c->remote_window + adjust) < c->remote_window)
2745 fatal("channel %d: adjust %u overflows remote window %u",
2746 id, adjust, c->remote_window);
2747 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002748 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002749}
2750
Damien Millerd79b4242006-03-31 23:11:44 +11002751/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002752int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002753channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10002754{
2755 Channel *c;
2756 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002757 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002758
2759 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002760 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002761
Damien Miller16a73072008-12-08 09:55:25 +11002762 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11002763 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002764
Damien Miller16a73072008-12-08 09:55:25 +11002765 if ((c = channel_lookup(id)) == NULL) {
2766 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002767 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002768 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002769 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002770 return 0;
2771 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10002772 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002773 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002774 cc->cb(type, c, cc->ctx);
2775 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002776 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002777 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002778 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002779}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002780
Ben Lindstrome9c99912001-06-09 00:41:05 +00002781/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002782
Ben Lindstrom908afed2001-10-03 17:34:59 +00002783void
2784channel_set_af(int af)
2785{
2786 IPv4or6 = af;
2787}
2788
Damien Millerf6dff7c2011-09-22 21:38:52 +10002789
2790/*
2791 * Determine whether or not a port forward listens to loopback, the
2792 * specified address or wildcard. On the client, a specified bind
2793 * address will always override gateway_ports. On the server, a
2794 * gateway_ports of 1 (``yes'') will override the client's specification
2795 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2796 * will bind to whatever address the client asked for.
2797 *
2798 * Special-case listen_addrs are:
2799 *
2800 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2801 * "" (empty string), "*" -> wildcard v4/v6
2802 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002803 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002804 */
2805static const char *
2806channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002807 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002808{
2809 const char *addr = NULL;
2810 int wildcard = 0;
2811
2812 if (listen_addr == NULL) {
2813 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002814 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002815 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002816 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002817 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2818 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2819 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002820 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002821 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002822 /*
2823 * Notify client if they requested a specific listen
2824 * address and it was overridden.
2825 */
2826 if (*listen_addr != '\0' &&
2827 strcmp(listen_addr, "0.0.0.0") != 0 &&
2828 strcmp(listen_addr, "*") != 0) {
2829 packet_send_debug("Forwarding listen address "
2830 "\"%s\" overridden by server "
2831 "GatewayPorts", listen_addr);
2832 }
Damien Miller602943d2014-07-04 08:59:41 +10002833 } else if (strcmp(listen_addr, "localhost") != 0 ||
2834 strcmp(listen_addr, "127.0.0.1") == 0 ||
2835 strcmp(listen_addr, "::1") == 0) {
2836 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002837 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002838 }
2839 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2840 strcmp(listen_addr, "::1") == 0) {
2841 /*
2842 * If a specific IPv4/IPv6 localhost address has been
2843 * requested then accept it even if gateway_ports is in
2844 * effect. This allows the client to prefer IPv4 or IPv6.
2845 */
2846 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002847 }
2848 if (wildcardp != NULL)
2849 *wildcardp = wildcard;
2850 return addr;
2851}
2852
Damien Millerb16461c2002-01-22 23:29:22 +11002853static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002854channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2855 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002856{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002857 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002858 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002859 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002860 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002861 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002862 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002863
Damien Millerf91ee4c2005-03-01 21:24:33 +11002864 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002865
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00002866 if (is_client && fwd->connect_path != NULL) {
2867 host = fwd->connect_path;
2868 } else {
2869 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2870 fwd->listen_host : fwd->connect_host;
2871 if (host == NULL) {
2872 error("No forward host name.");
2873 return 0;
2874 }
2875 if (strlen(host) >= NI_MAXHOST) {
2876 error("Forward host name too long.");
2877 return 0;
2878 }
Kevin Steves12057502001-02-05 14:54:34 +00002879 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002880
Damien Millerf6dff7c2011-09-22 21:38:52 +10002881 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002882 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2883 is_client, fwd_opts);
2884 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002885 type, wildcard, (addr == NULL) ? "NULL" : addr);
2886
2887 /*
Damien Miller34132e52000-01-14 15:45:46 +11002888 * getaddrinfo returns a loopback address if the hostname is
2889 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002890 */
Damien Miller34132e52000-01-14 15:45:46 +11002891 memset(&hints, 0, sizeof(hints));
2892 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002893 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002894 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002895 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002896 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2897 if (addr == NULL) {
2898 /* This really shouldn't happen */
2899 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002900 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002901 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002902 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11002903 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002904 }
Damien Millera7270302005-07-06 09:36:05 +10002905 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002906 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002907 if (allocated_listen_port != NULL)
2908 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11002909 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11002910 switch (ai->ai_family) {
2911 case AF_INET:
2912 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2913 sin_port;
2914 break;
2915 case AF_INET6:
2916 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2917 sin6_port;
2918 break;
2919 default:
Damien Miller34132e52000-01-14 15:45:46 +11002920 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11002921 }
2922 /*
2923 * If allocating a port for -R forwards, then use the
2924 * same port for all address families.
2925 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002926 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002927 allocated_listen_port != NULL && *allocated_listen_port > 0)
2928 *lport_p = htons(*allocated_listen_port);
2929
Damien Miller34132e52000-01-14 15:45:46 +11002930 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2931 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10002932 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11002933 continue;
2934 }
2935 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11002936 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002937 if (sock < 0) {
2938 /* this is no error since kernel may not support ipv6 */
2939 verbose("socket: %.100s", strerror(errno));
2940 continue;
2941 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002942
2943 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11002944 if (ai->ai_family == AF_INET6)
2945 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002946
Damien Miller4bf648f2009-02-14 16:28:21 +11002947 debug("Local forwarding listening on %s port %s.",
2948 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002949
Damien Miller34132e52000-01-14 15:45:46 +11002950 /* Bind the socket to the address. */
2951 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2952 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002953 if (!ai->ai_next)
2954 error("bind: %.100s", strerror(errno));
2955 else
2956 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002957
Damien Miller34132e52000-01-14 15:45:46 +11002958 close(sock);
2959 continue;
2960 }
2961 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002962 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002963 error("listen: %.100s", strerror(errno));
2964 close(sock);
2965 continue;
2966 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002967
2968 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10002969 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11002970 * record what we got.
2971 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002972 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002973 allocated_listen_port != NULL &&
2974 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00002975 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11002976 debug("Allocated listen port %d",
2977 *allocated_listen_port);
2978 }
2979
Damien Miller34132e52000-01-14 15:45:46 +11002980 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002981 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002982 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002983 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11002984 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10002985 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002986 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10002987 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11002988 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
2989 c->listening_port = *allocated_listen_port;
2990 else
Damien Miller7acefbb2014-07-18 14:11:24 +10002991 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002992 success = 1;
2993 }
2994 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10002995 error("%s: cannot listen to port: %d", __func__,
2996 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002997 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002998 return success;
Damien Miller95def091999-11-25 00:26:21 +11002999}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003000
Damien Miller7acefbb2014-07-18 14:11:24 +10003001static int
3002channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3003 struct ForwardOptions *fwd_opts)
3004{
3005 struct sockaddr_un sunaddr;
3006 const char *path;
3007 Channel *c;
3008 int port, sock;
3009 mode_t omask;
3010
3011 switch (type) {
3012 case SSH_CHANNEL_UNIX_LISTENER:
3013 if (fwd->connect_path != NULL) {
3014 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3015 error("Local connecting path too long: %s",
3016 fwd->connect_path);
3017 return 0;
3018 }
3019 path = fwd->connect_path;
3020 port = PORT_STREAMLOCAL;
3021 } else {
3022 if (fwd->connect_host == NULL) {
3023 error("No forward host name.");
3024 return 0;
3025 }
3026 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3027 error("Forward host name too long.");
3028 return 0;
3029 }
3030 path = fwd->connect_host;
3031 port = fwd->connect_port;
3032 }
3033 break;
3034 case SSH_CHANNEL_RUNIX_LISTENER:
3035 path = fwd->listen_path;
3036 port = PORT_STREAMLOCAL;
3037 break;
3038 default:
3039 error("%s: unexpected channel type %d", __func__, type);
3040 return 0;
3041 }
3042
3043 if (fwd->listen_path == NULL) {
3044 error("No forward path name.");
3045 return 0;
3046 }
3047 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3048 error("Local listening path too long: %s", fwd->listen_path);
3049 return 0;
3050 }
3051
3052 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3053
3054 /* Start a Unix domain listener. */
3055 omask = umask(fwd_opts->streamlocal_bind_mask);
3056 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3057 fwd_opts->streamlocal_bind_unlink);
3058 umask(omask);
3059 if (sock < 0)
3060 return 0;
3061
3062 debug("Local forwarding listening on path %s.", fwd->listen_path);
3063
3064 /* Allocate a channel number for the socket. */
3065 c = channel_new("unix listener", type, sock, sock, -1,
3066 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3067 0, "unix listener", 1);
3068 c->path = xstrdup(path);
3069 c->host_port = port;
3070 c->listening_port = PORT_STREAMLOCAL;
3071 c->listening_addr = xstrdup(fwd->listen_path);
3072 return 1;
3073}
3074
3075static int
3076channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003077{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003078 u_int i;
3079 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003080
Darren Tucker47eede72005-03-14 23:08:12 +11003081 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003082 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003083 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3084 continue;
3085 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3086 debug2("%s: close channel %d", __func__, i);
3087 channel_free(c);
3088 found = 1;
3089 }
3090 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003091
Damien Millerf6dff7c2011-09-22 21:38:52 +10003092 return (found);
3093}
3094
Damien Miller7acefbb2014-07-18 14:11:24 +10003095static int
3096channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003097{
3098 u_int i;
3099 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003100
3101 for (i = 0; i < channels_alloc; i++) {
3102 Channel *c = channels[i];
3103 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3104 continue;
3105 if (c->path == NULL)
3106 continue;
3107 if (strcmp(c->path, path) == 0) {
3108 debug2("%s: close channel %d", __func__, i);
3109 channel_free(c);
3110 found = 1;
3111 }
3112 }
3113
3114 return (found);
3115}
3116
3117int
3118channel_cancel_rport_listener(struct Forward *fwd)
3119{
3120 if (fwd->listen_path != NULL)
3121 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3122 else
3123 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3124}
3125
3126static int
3127channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3128 int cport, struct ForwardOptions *fwd_opts)
3129{
3130 u_int i;
3131 int found = 0;
3132 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003133
3134 for (i = 0; i < channels_alloc; i++) {
3135 Channel *c = channels[i];
3136 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3137 continue;
Damien Millerff773642011-09-22 21:39:48 +10003138 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003139 continue;
Damien Millerff773642011-09-22 21:39:48 +10003140 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3141 /* skip dynamic forwardings */
3142 if (c->host_port == 0)
3143 continue;
3144 } else {
3145 if (c->host_port != cport)
3146 continue;
3147 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003148 if ((c->listening_addr == NULL && addr != NULL) ||
3149 (c->listening_addr != NULL && addr == NULL))
3150 continue;
3151 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003152 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003153 channel_free(c);
3154 found = 1;
3155 }
3156 }
3157
3158 return (found);
3159}
3160
Damien Miller7acefbb2014-07-18 14:11:24 +10003161static int
3162channel_cancel_lport_listener_streamlocal(const char *path)
3163{
3164 u_int i;
3165 int found = 0;
3166
3167 if (path == NULL) {
3168 error("%s: no path specified.", __func__);
3169 return 0;
3170 }
3171
3172 for (i = 0; i < channels_alloc; i++) {
3173 Channel *c = channels[i];
3174 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3175 continue;
3176 if (c->listening_addr == NULL)
3177 continue;
3178 if (strcmp(c->listening_addr, path) == 0) {
3179 debug2("%s: close channel %d", __func__, i);
3180 channel_free(c);
3181 found = 1;
3182 }
3183 }
3184
3185 return (found);
3186}
3187
3188int
3189channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3190{
3191 if (fwd->listen_path != NULL)
3192 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3193 else
3194 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3195}
3196
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003197/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003198int
Damien Miller7acefbb2014-07-18 14:11:24 +10003199channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003200{
Damien Miller7acefbb2014-07-18 14:11:24 +10003201 if (fwd->listen_path != NULL) {
3202 return channel_setup_fwd_listener_streamlocal(
3203 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3204 } else {
3205 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3206 fwd, NULL, fwd_opts);
3207 }
Damien Millerb16461c2002-01-22 23:29:22 +11003208}
3209
3210/* protocol v2 remote port fwd, used by sshd */
3211int
Damien Miller7acefbb2014-07-18 14:11:24 +10003212channel_setup_remote_fwd_listener(struct Forward *fwd,
3213 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003214{
Damien Miller7acefbb2014-07-18 14:11:24 +10003215 if (fwd->listen_path != NULL) {
3216 return channel_setup_fwd_listener_streamlocal(
3217 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3218 } else {
3219 return channel_setup_fwd_listener_tcpip(
3220 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3221 fwd_opts);
3222 }
Damien Millerb16461c2002-01-22 23:29:22 +11003223}
3224
Damien Miller5428f641999-11-25 11:54:57 +11003225/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003226 * Translate the requested rfwd listen host to something usable for
3227 * this server.
3228 */
3229static const char *
3230channel_rfwd_bind_host(const char *listen_host)
3231{
3232 if (listen_host == NULL) {
3233 if (datafellows & SSH_BUG_RFWD_ADDR)
3234 return "127.0.0.1";
3235 else
3236 return "localhost";
3237 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3238 if (datafellows & SSH_BUG_RFWD_ADDR)
3239 return "0.0.0.0";
3240 else
3241 return "";
3242 } else
3243 return listen_host;
3244}
3245
3246/*
Damien Miller5428f641999-11-25 11:54:57 +11003247 * Initiate forwarding of connections to port "port" on remote host through
3248 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003249 * Returns handle (index) for updating the dynamic listen port with
3250 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003251 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003252int
Damien Miller7acefbb2014-07-18 14:11:24 +10003253channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003254{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003255 int success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003256
Damien Miller95def091999-11-25 00:26:21 +11003257 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003258 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3259 if (fwd->listen_path != NULL) {
3260 packet_put_cstring("streamlocal-forward@openssh.com");
3261 packet_put_char(1); /* boolean: want reply */
3262 packet_put_cstring(fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003263 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003264 packet_put_cstring("tcpip-forward");
3265 packet_put_char(1); /* boolean: want reply */
3266 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3267 packet_put_int(fwd->listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11003268 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003269 packet_send();
3270 packet_write_wait();
3271 /* Assume that server accepts the request */
3272 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003273 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003274 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003275 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003276 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003277 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003278 if (fwd->connect_path != NULL) {
3279 permitted_opens[idx].host_to_connect =
3280 xstrdup(fwd->connect_path);
3281 permitted_opens[idx].port_to_connect =
3282 PORT_STREAMLOCAL;
3283 } else {
3284 permitted_opens[idx].host_to_connect =
3285 xstrdup(fwd->connect_host);
3286 permitted_opens[idx].port_to_connect =
3287 fwd->connect_port;
3288 }
3289 if (fwd->listen_path != NULL) {
3290 permitted_opens[idx].listen_host = NULL;
3291 permitted_opens[idx].listen_path =
3292 xstrdup(fwd->listen_path);
3293 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3294 } else {
3295 permitted_opens[idx].listen_host =
3296 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3297 permitted_opens[idx].listen_path = NULL;
3298 permitted_opens[idx].listen_port = fwd->listen_port;
3299 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003300 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003301 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003302 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003303}
3304
Damien Miller4b3ed642014-07-02 15:29:40 +10003305static int
3306open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003307 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003308{
3309 if (allowed_open->host_to_connect == NULL)
3310 return 0;
3311 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3312 allowed_open->port_to_connect != requestedport)
3313 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003314 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3315 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003316 return 0;
3317 return 1;
3318}
3319
3320/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003321 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003322 * we don't support FWD_PERMIT_ANY_PORT and
3323 * need to translate between the configured-host (listen_host)
3324 * and what we've sent to the remote server (channel_rfwd_bind_host)
3325 */
3326static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003327open_listen_match_tcpip(ForwardPermission *allowed_open,
3328 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003329{
3330 const char *allowed_host;
3331
3332 if (allowed_open->host_to_connect == NULL)
3333 return 0;
3334 if (allowed_open->listen_port != requestedport)
3335 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003336 if (!translate && allowed_open->listen_host == NULL &&
3337 requestedhost == NULL)
3338 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003339 allowed_host = translate ?
3340 channel_rfwd_bind_host(allowed_open->listen_host) :
3341 allowed_open->listen_host;
3342 if (allowed_host == NULL ||
3343 strcmp(allowed_host, requestedhost) != 0)
3344 return 0;
3345 return 1;
3346}
3347
Damien Miller7acefbb2014-07-18 14:11:24 +10003348static int
3349open_listen_match_streamlocal(ForwardPermission *allowed_open,
3350 const char *requestedpath)
3351{
3352 if (allowed_open->host_to_connect == NULL)
3353 return 0;
3354 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3355 return 0;
3356 if (allowed_open->listen_path == NULL ||
3357 strcmp(allowed_open->listen_path, requestedpath) != 0)
3358 return 0;
3359 return 1;
3360}
3361
Damien Miller5428f641999-11-25 11:54:57 +11003362/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003363 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003364 * local side.
3365 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003366static int
3367channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003368{
3369 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003370
Darren Tuckere7066df2004-05-24 10:18:05 +10003371 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003372 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003373 break;
3374 }
3375 if (i >= num_permitted_opens) {
3376 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003377 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003378 }
3379 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3380 packet_put_cstring("cancel-tcpip-forward");
3381 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003382 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003383 packet_put_int(port);
3384 packet_send();
3385
Damien Miller4b3ed642014-07-02 15:29:40 +10003386 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003387 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003388 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003389 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003390 free(permitted_opens[i].listen_host);
3391 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003392 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003393 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003394
3395 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003396}
3397
3398/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003399 * Request cancellation of remote forwarding of Unix domain socket
3400 * path from local side.
3401 */
3402static int
3403channel_request_rforward_cancel_streamlocal(const char *path)
3404{
3405 int i;
3406
Damien Miller7acefbb2014-07-18 14:11:24 +10003407 for (i = 0; i < num_permitted_opens; i++) {
3408 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3409 break;
3410 }
3411 if (i >= num_permitted_opens) {
3412 debug("%s: requested forward not found", __func__);
3413 return -1;
3414 }
3415 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3416 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3417 packet_put_char(0);
3418 packet_put_cstring(path);
3419 packet_send();
3420
3421 permitted_opens[i].listen_port = 0;
3422 permitted_opens[i].port_to_connect = 0;
3423 free(permitted_opens[i].host_to_connect);
3424 permitted_opens[i].host_to_connect = NULL;
3425 permitted_opens[i].listen_host = NULL;
3426 free(permitted_opens[i].listen_path);
3427 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003428 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003429
3430 return 0;
3431}
3432
3433/*
3434 * Request cancellation of remote forwarding of a connection from local side.
3435 */
3436int
3437channel_request_rforward_cancel(struct Forward *fwd)
3438{
3439 if (fwd->listen_path != NULL) {
3440 return (channel_request_rforward_cancel_streamlocal(
3441 fwd->listen_path));
3442 } else {
3443 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3444 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3445 }
3446}
3447
3448/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003449 * Permits opening to any host/port if permitted_opens[] is empty. This is
3450 * usually called by the server, because the user could connect to any port
3451 * anyway, and the server has no way to know but to trust the client anyway.
3452 */
3453void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003454channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003455{
3456 if (num_permitted_opens == 0)
3457 all_opens_permitted = 1;
3458}
3459
Ben Lindstroma3700052001-04-05 23:26:32 +00003460void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003461channel_add_permitted_opens(char *host, int port)
3462{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003463 debug("allow port forwarding to host %s port %d", host, port);
3464
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003465 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003466 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003467 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3468 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003469 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003470 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003471 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003472 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003473 num_permitted_opens++;
3474
3475 all_opens_permitted = 0;
3476}
3477
Darren Tucker68afb8c2011-10-02 18:59:03 +11003478/*
3479 * Update the listen port for a dynamic remote forward, after
3480 * the actual 'newport' has been allocated. If 'newport' < 0 is
3481 * passed then they entry will be invalidated.
3482 */
3483void
3484channel_update_permitted_opens(int idx, int newport)
3485{
3486 if (idx < 0 || idx >= num_permitted_opens) {
3487 debug("channel_update_permitted_opens: index out of range:"
3488 " %d num_permitted_opens %d", idx, num_permitted_opens);
3489 return;
3490 }
3491 debug("%s allowed port %d for forwarding to host %s port %d",
3492 newport > 0 ? "Updating" : "Removing",
3493 newport,
3494 permitted_opens[idx].host_to_connect,
3495 permitted_opens[idx].port_to_connect);
3496 if (newport >= 0) {
3497 permitted_opens[idx].listen_port =
3498 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3499 } else {
3500 permitted_opens[idx].listen_port = 0;
3501 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003502 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003503 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003504 free(permitted_opens[idx].listen_host);
3505 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003506 free(permitted_opens[idx].listen_path);
3507 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003508 }
3509}
3510
Damien Millera765cf42006-07-24 14:08:13 +10003511int
Damien Miller9b439df2006-07-24 14:04:00 +10003512channel_add_adm_permitted_opens(char *host, int port)
3513{
Damien Millera765cf42006-07-24 14:08:13 +10003514 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003515
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003516 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003517 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003518 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3519 = xstrdup(host);
3520 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003521 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003522 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003523 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003524 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003525}
3526
3527void
Damien Millerc6081482012-04-22 11:18:53 +10003528channel_disable_adm_local_opens(void)
3529{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003530 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003531 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003532 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3533 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003534}
3535
3536void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003537channel_clear_permitted_opens(void)
3538{
3539 int i;
3540
Damien Miller4b3ed642014-07-02 15:29:40 +10003541 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003542 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003543 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003544 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003545 }
Darren Tuckera627d422013-06-02 07:31:17 +10003546 free(permitted_opens);
3547 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003548 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003549}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003550
Damien Miller9b439df2006-07-24 14:04:00 +10003551void
3552channel_clear_adm_permitted_opens(void)
3553{
3554 int i;
3555
Damien Miller4b3ed642014-07-02 15:29:40 +10003556 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003557 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003558 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003559 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003560 }
Darren Tuckera627d422013-06-02 07:31:17 +10003561 free(permitted_adm_opens);
3562 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003563 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003564}
3565
Darren Tuckere7140f22008-06-10 23:01:51 +10003566void
3567channel_print_adm_permitted_opens(void)
3568{
Damien Miller6ef17492008-07-16 22:42:06 +10003569 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003570
Damien Millerb53d8a12009-01-28 16:13:04 +11003571 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003572 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003573 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003574 return;
3575 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003576 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003577 if (permitted_adm_opens[i].host_to_connect == NULL)
3578 printf(" none");
3579 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003580 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3581 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003582 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003583}
3584
Darren Tucker1338b9e2011-10-02 18:57:35 +11003585/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3586int
3587permitopen_port(const char *p)
3588{
3589 int port;
3590
3591 if (strcmp(p, "*") == 0)
3592 return FWD_PERMIT_ANY_PORT;
3593 if ((port = a2port(p)) > 0)
3594 return port;
3595 return -1;
3596}
3597
Damien Millerbd740252008-05-19 15:37:09 +10003598/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003599static int
Damien Millerbd740252008-05-19 15:37:09 +10003600connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003601{
Damien Millerbd740252008-05-19 15:37:09 +10003602 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003603 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00003604 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003605
Damien Millerbd740252008-05-19 15:37:09 +10003606 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003607 switch (cctx->ai->ai_family) {
3608 case AF_UNIX:
3609 /* unix:pathname instead of host:port */
3610 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3611 strlcpy(ntop, "unix", sizeof(ntop));
3612 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3613 break;
3614 case AF_INET:
3615 case AF_INET6:
3616 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3617 ntop, sizeof(ntop), strport, sizeof(strport),
3618 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3619 error("connect_next: getnameinfo failed");
3620 continue;
3621 }
3622 break;
3623 default:
Damien Miller34132e52000-01-14 15:45:46 +11003624 continue;
3625 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003626 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3627 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003628 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003629 error("socket: %.100s", strerror(errno));
3630 else
3631 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003632 continue;
3633 }
Damien Miller232711f2004-06-15 10:35:30 +10003634 if (set_nonblock(sock) == -1)
3635 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003636 if (connect(sock, cctx->ai->ai_addr,
3637 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3638 debug("connect_next: host %.100s ([%.100s]:%s): "
3639 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003640 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003641 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003642 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003643 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003644 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003645 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003646 if (cctx->ai->ai_family != AF_UNIX)
3647 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003648 debug("connect_next: host %.100s ([%.100s]:%s) "
3649 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3650 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003651 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003652 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003653 return -1;
3654}
Damien Millerb38eff82000-04-01 11:09:21 +10003655
Damien Millerbd740252008-05-19 15:37:09 +10003656static void
3657channel_connect_ctx_free(struct channel_connect *cctx)
3658{
Darren Tuckera627d422013-06-02 07:31:17 +10003659 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003660 if (cctx->aitop) {
3661 if (cctx->aitop->ai_family == AF_UNIX)
3662 free(cctx->aitop);
3663 else
3664 freeaddrinfo(cctx->aitop);
3665 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003666 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003667}
3668
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003669/*
3670 * Return CONNECTING channel to remote host:port or local socket path,
3671 * passing back the failure reason if appropriate.
3672 */
Damien Millerbd740252008-05-19 15:37:09 +10003673static Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003674connect_to_reason(const char *name, int port, char *ctype, char *rname,
3675 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003676{
3677 struct addrinfo hints;
3678 int gaierr;
3679 int sock = -1;
3680 char strport[NI_MAXSERV];
3681 struct channel_connect cctx;
3682 Channel *c;
3683
Damien Miller2bcb8662008-07-12 17:12:29 +10003684 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003685
3686 if (port == PORT_STREAMLOCAL) {
3687 struct sockaddr_un *sunaddr;
3688 struct addrinfo *ai;
3689
3690 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3691 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3692 return (NULL);
3693 }
3694
3695 /*
3696 * Fake up a struct addrinfo for AF_UNIX connections.
3697 * channel_connect_ctx_free() must check ai_family
3698 * and use free() not freeaddirinfo() for AF_UNIX.
3699 */
3700 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3701 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3702 ai->ai_addr = (struct sockaddr *)(ai + 1);
3703 ai->ai_addrlen = sizeof(*sunaddr);
3704 ai->ai_family = AF_UNIX;
3705 ai->ai_socktype = SOCK_STREAM;
3706 ai->ai_protocol = PF_UNSPEC;
3707 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3708 sunaddr->sun_family = AF_UNIX;
3709 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3710 cctx.aitop = ai;
3711 } else {
3712 memset(&hints, 0, sizeof(hints));
3713 hints.ai_family = IPv4or6;
3714 hints.ai_socktype = SOCK_STREAM;
3715 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003716 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3717 != 0) {
3718 if (errmsg != NULL)
3719 *errmsg = ssh_gai_strerror(gaierr);
3720 if (reason != NULL)
3721 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003722 error("connect_to %.100s: unknown host (%s)", name,
3723 ssh_gai_strerror(gaierr));
3724 return NULL;
3725 }
Damien Millerbd740252008-05-19 15:37:09 +10003726 }
3727
Damien Miller7acefbb2014-07-18 14:11:24 +10003728 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003729 cctx.port = port;
3730 cctx.ai = cctx.aitop;
3731
3732 if ((sock = connect_next(&cctx)) == -1) {
3733 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003734 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003735 channel_connect_ctx_free(&cctx);
3736 return NULL;
3737 }
3738 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3739 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3740 c->connect_ctx = cctx;
3741 return c;
3742}
3743
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003744/* Return CONNECTING channel to remote host:port or local socket path */
3745static Channel *
3746connect_to(const char *name, int port, char *ctype, char *rname)
3747{
3748 return connect_to_reason(name, port, ctype, rname, NULL, NULL);
3749}
3750
markus@openbsd.org8d057842016-09-30 09:19:13 +00003751/*
3752 * returns either the newly connected channel or the downstream channel
3753 * that needs to deal with this connection.
3754 */
Damien Millerbd740252008-05-19 15:37:09 +10003755Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003756channel_connect_by_listen_address(const char *listen_host,
3757 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003758{
3759 int i;
3760
3761 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003762 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003763 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00003764 if (permitted_opens[i].downstream)
3765 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10003766 return connect_to(
3767 permitted_opens[i].host_to_connect,
3768 permitted_opens[i].port_to_connect, ctype, rname);
3769 }
3770 }
3771 error("WARNING: Server requests forwarding for unknown listen_port %d",
3772 listen_port);
3773 return NULL;
3774}
3775
Damien Miller7acefbb2014-07-18 14:11:24 +10003776Channel *
3777channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3778{
3779 int i;
3780
3781 for (i = 0; i < num_permitted_opens; i++) {
3782 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3783 return connect_to(
3784 permitted_opens[i].host_to_connect,
3785 permitted_opens[i].port_to_connect, ctype, rname);
3786 }
3787 }
3788 error("WARNING: Server requests forwarding for unknown path %.100s",
3789 path);
3790 return NULL;
3791}
3792
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003793/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003794Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003795channel_connect_to_port(const char *host, u_short port, char *ctype,
3796 char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003797{
Damien Miller9b439df2006-07-24 14:04:00 +10003798 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003799
3800 permit = all_opens_permitted;
3801 if (!permit) {
3802 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003803 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003804 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003805 break;
3806 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003807 }
Damien Miller9b439df2006-07-24 14:04:00 +10003808
3809 if (num_adm_permitted_opens > 0) {
3810 permit_adm = 0;
3811 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003812 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003813 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003814 break;
3815 }
Damien Miller9b439df2006-07-24 14:04:00 +10003816 }
3817
3818 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003819 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003820 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003821 if (reason != NULL)
3822 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10003823 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003824 }
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003825 return connect_to_reason(host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003826}
3827
Damien Miller7acefbb2014-07-18 14:11:24 +10003828/* Check if connecting to that path is permitted and connect. */
3829Channel *
3830channel_connect_to_path(const char *path, char *ctype, char *rname)
3831{
3832 int i, permit, permit_adm = 1;
3833
3834 permit = all_opens_permitted;
3835 if (!permit) {
3836 for (i = 0; i < num_permitted_opens; i++)
3837 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3838 permit = 1;
3839 break;
3840 }
3841 }
3842
3843 if (num_adm_permitted_opens > 0) {
3844 permit_adm = 0;
3845 for (i = 0; i < num_adm_permitted_opens; i++)
3846 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3847 permit_adm = 1;
3848 break;
3849 }
3850 }
3851
3852 if (!permit || !permit_adm) {
3853 logit("Received request to connect to path %.100s, "
3854 "but the request was denied.", path);
3855 return NULL;
3856 }
3857 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3858}
3859
Damien Miller0e220db2004-06-15 10:34:08 +10003860void
3861channel_send_window_changes(void)
3862{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003863 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003864 struct winsize ws;
3865
3866 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003867 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003868 channels[i]->type != SSH_CHANNEL_OPEN)
3869 continue;
3870 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3871 continue;
3872 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003873 packet_put_int((u_int)ws.ws_col);
3874 packet_put_int((u_int)ws.ws_row);
3875 packet_put_int((u_int)ws.ws_xpixel);
3876 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003877 packet_send();
3878 }
3879}
3880
Ben Lindstrome9c99912001-06-09 00:41:05 +00003881/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003882
Damien Miller5428f641999-11-25 11:54:57 +11003883/*
3884 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003885 * Returns 0 and a suitable display number for the DISPLAY variable
3886 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003887 */
Kevin Steves366298c2001-12-19 17:58:01 +00003888int
Damien Miller95c249f2002-02-05 12:11:34 +11003889x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003890 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003891{
Damien Millere7378562001-12-21 14:58:35 +11003892 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003893 int display_number, sock;
3894 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003895 struct addrinfo hints, *ai, *aitop;
3896 char strport[NI_MAXSERV];
3897 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003898
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003899 if (chanids == NULL)
3900 return -1;
3901
Damien Millera34a28b1999-12-14 10:47:15 +11003902 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11003903 display_number < MAX_DISPLAYS;
3904 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11003905 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11003906 memset(&hints, 0, sizeof(hints));
3907 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11003908 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11003909 hints.ai_socktype = SOCK_STREAM;
3910 snprintf(strport, sizeof strport, "%d", port);
3911 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003912 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00003913 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003914 }
Damien Miller34132e52000-01-14 15:45:46 +11003915 for (ai = aitop; ai; ai = ai->ai_next) {
3916 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3917 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11003918 sock = socket(ai->ai_family, ai->ai_socktype,
3919 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003920 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11003921 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
3922#ifdef EPFNOSUPPORT
3923 && (errno != EPFNOSUPPORT)
3924#endif
3925 ) {
Damien Millere2192732000-01-17 13:22:55 +11003926 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10003927 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00003928 return -1;
Damien Millere2192732000-01-17 13:22:55 +11003929 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11003930 debug("x11_create_display_inet: Socket family %d not supported",
3931 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11003932 continue;
3933 }
Damien Miller34132e52000-01-14 15:45:46 +11003934 }
Damien Miller04ee0f82009-11-18 17:48:30 +11003935 if (ai->ai_family == AF_INET6)
3936 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10003937 if (x11_use_localhost)
3938 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11003939 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003940 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003941 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11003942
Damien Miller34132e52000-01-14 15:45:46 +11003943 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11003944 close(socks[n]);
3945 }
3946 num_socks = 0;
3947 break;
3948 }
3949 socks[num_socks++] = sock;
3950 if (num_socks == NUM_SOCKS)
3951 break;
Damien Miller95def091999-11-25 00:26:21 +11003952 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00003953 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11003954 if (num_socks > 0)
3955 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003956 }
Damien Miller95def091999-11-25 00:26:21 +11003957 if (display_number >= MAX_DISPLAYS) {
3958 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00003959 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003960 }
Damien Miller95def091999-11-25 00:26:21 +11003961 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11003962 for (n = 0; n < num_socks; n++) {
3963 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11003964 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003965 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003966 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00003967 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11003968 }
Damien Miller95def091999-11-25 00:26:21 +11003969 }
Damien Miller34132e52000-01-14 15:45:46 +11003970
Damien Miller34132e52000-01-14 15:45:46 +11003971 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11003972 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11003973 for (n = 0; n < num_socks; n++) {
3974 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11003975 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10003976 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3977 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003978 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11003979 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003980 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11003981 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003982 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003983
Kevin Steves366298c2001-12-19 17:58:01 +00003984 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003985 *display_numberp = display_number;
3986 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003987}
3988
Ben Lindstrombba81212001-06-25 05:01:22 +00003989static int
Damien Miller819dbb62009-01-21 16:46:26 +11003990connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003991{
Damien Miller95def091999-11-25 00:26:21 +11003992 int sock;
3993 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003994
Damien Miller3afe3752001-12-21 12:39:51 +11003995 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3996 if (sock < 0)
3997 error("socket: %.100s", strerror(errno));
3998 memset(&addr, 0, sizeof(addr));
3999 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004000 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004001 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004002 return sock;
4003 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004004 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4005 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004006}
4007
Damien Miller819dbb62009-01-21 16:46:26 +11004008static int
4009connect_local_xsocket(u_int dnr)
4010{
4011 char buf[1024];
4012 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4013 return connect_local_xsocket_path(buf);
4014}
4015
Darren Tuckerda39b092017-03-10 13:22:32 +11004016#ifdef __APPLE__
4017static int
4018is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4019{
4020 struct stat sbuf;
4021
4022 if (strlcpy(path, display, pathlen) >= pathlen) {
4023 error("%s: display path too long", __func__);
4024 return 0;
4025 }
4026 if (display[0] != '/')
4027 return 0;
4028 if (stat(path, &sbuf) == 0) {
4029 return 1;
4030 } else {
4031 char *dot = strrchr(path, '.');
4032 if (dot != NULL) {
4033 *dot = '\0';
4034 if (stat(path, &sbuf) == 0) {
4035 return 1;
4036 }
4037 }
4038 }
4039 return 0;
4040}
4041#endif
4042
Damien Millerbd483e72000-04-30 10:00:53 +10004043int
4044x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004045{
Damien Miller57c4e872006-03-31 23:11:07 +11004046 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004047 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004048 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004049 struct addrinfo hints, *ai, *aitop;
4050 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004051 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004052
Damien Miller95def091999-11-25 00:26:21 +11004053 /* Try to open a socket for the local X server. */
4054 display = getenv("DISPLAY");
4055 if (!display) {
4056 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004057 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004058 }
Damien Miller5428f641999-11-25 11:54:57 +11004059 /*
4060 * Now we decode the value of the DISPLAY variable and make a
4061 * connection to the real X server.
4062 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004063
Damien Miller819dbb62009-01-21 16:46:26 +11004064#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004065 /* Check if display is a path to a socket (as set by launchd). */
4066 {
4067 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004068
Darren Tuckerda39b092017-03-10 13:22:32 +11004069 if (is_path_to_xsocket(display, path, sizeof(path))) {
4070 debug("x11_connect_display: $DISPLAY is launchd");
4071
4072 /* Create a socket. */
4073 sock = connect_local_xsocket_path(path);
4074 if (sock < 0)
4075 return -1;
4076
4077 /* OK, we now have a connection to the display. */
4078 return sock;
4079 }
Damien Miller819dbb62009-01-21 16:46:26 +11004080 }
4081#endif
Damien Miller5428f641999-11-25 11:54:57 +11004082 /*
4083 * Check if it is a unix domain socket. Unix domain displays are in
4084 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4085 */
Damien Miller95def091999-11-25 00:26:21 +11004086 if (strncmp(display, "unix:", 5) == 0 ||
4087 display[0] == ':') {
4088 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004089 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004090 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004091 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004092 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004093 }
4094 /* Create a socket. */
4095 sock = connect_local_xsocket(display_number);
4096 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004097 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004098
4099 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004100 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004101 }
Damien Miller5428f641999-11-25 11:54:57 +11004102 /*
4103 * Connect to an inet socket. The DISPLAY value is supposedly
4104 * hostname:d[.s], where hostname may also be numeric IP address.
4105 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004106 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004107 cp = strchr(buf, ':');
4108 if (!cp) {
4109 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004110 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004111 }
Damien Miller95def091999-11-25 00:26:21 +11004112 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004113 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004114 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004115 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004116 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004117 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004118 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004119
Damien Miller34132e52000-01-14 15:45:46 +11004120 /* Look up the host address */
4121 memset(&hints, 0, sizeof(hints));
4122 hints.ai_family = IPv4or6;
4123 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004124 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004125 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004126 error("%.100s: unknown host. (%s)", buf,
4127 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004128 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004129 }
Damien Miller34132e52000-01-14 15:45:46 +11004130 for (ai = aitop; ai; ai = ai->ai_next) {
4131 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004132 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004133 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004134 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004135 continue;
4136 }
4137 /* Connect it to the display. */
4138 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004139 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004140 6000 + display_number, strerror(errno));
4141 close(sock);
4142 continue;
4143 }
4144 /* Success */
4145 break;
Damien Miller34132e52000-01-14 15:45:46 +11004146 }
Damien Miller34132e52000-01-14 15:45:46 +11004147 freeaddrinfo(aitop);
4148 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004149 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004150 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004151 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004152 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004153 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004154 return sock;
4155}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004156
Damien Millerbd483e72000-04-30 10:00:53 +10004157/*
Damien Miller5428f641999-11-25 11:54:57 +11004158 * Requests forwarding of X11 connections, generates fake authentication
4159 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004160 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004161 */
Damien Miller4af51302000-04-16 11:18:38 +10004162void
Damien Miller17e7ed02005-06-17 12:54:33 +10004163x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004164 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004165{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004166 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004167 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004168 char *new_data;
4169 int screen_number;
4170 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004171
Damien Millerf92c0792005-07-06 09:45:26 +10004172 if (x11_saved_display == NULL)
4173 x11_saved_display = xstrdup(disp);
4174 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004175 error("x11_request_forwarding_with_spoofing: different "
4176 "$DISPLAY already forwarded");
4177 return;
4178 }
4179
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004180 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004181 if (cp)
4182 cp = strchr(cp, '.');
4183 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004184 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004185 else
4186 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004187
Damien Miller13390022005-07-06 09:44:19 +10004188 if (x11_saved_proto == NULL) {
4189 /* Save protocol name. */
4190 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004191
4192 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004193 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004194 for (i = 0; i < data_len; i++) {
4195 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4196 fatal("x11_request_forwarding: bad "
4197 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004198 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004199 }
4200 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004201
4202 /* Generate fake data of the same length. */
4203 x11_fake_data = xmalloc(data_len);
4204 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004205 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004206 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004207
Damien Miller95def091999-11-25 00:26:21 +11004208 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004209 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004210
Damien Miller95def091999-11-25 00:26:21 +11004211 /* Send the request packet. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00004212 channel_request_start(client_session_id, "x11-req", want_reply);
4213 packet_put_char(0); /* XXX bool single connection */
Damien Millerbd483e72000-04-30 10:00:53 +10004214 packet_put_cstring(proto);
4215 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004216 packet_put_int(screen_number);
4217 packet_send();
4218 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004219 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004220}