blob: d9e81b5faa377aa149004814d262f1e4f67e2e37 [file] [log] [blame]
djm@openbsd.org71e5a532017-08-30 03:59:08 +00001/* $OpenBSD: channels.c,v 1.366 2017/08/30 03:59:08 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 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000483 buffer_free(&c->input);
484 buffer_free(&c->output);
485 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000486 free(c->remote_name);
487 c->remote_name = NULL;
488 free(c->path);
489 c->path = NULL;
490 free(c->listening_addr);
491 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000492 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
493 if (cc->abandon_cb != NULL)
494 cc->abandon_cb(c, cc->ctx);
495 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100496 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000497 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000498 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000499 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
500 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000501 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000502 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503}
504
Ben Lindstrome9c99912001-06-09 00:41:05 +0000505void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000506channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000507{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000508 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000509
Ben Lindstrom601e4362001-06-21 03:19:23 +0000510 for (i = 0; i < channels_alloc; i++)
511 if (channels[i] != NULL)
512 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000513}
514
515/*
516 * Closes the sockets/fds of all channels. This is used to close extra file
517 * descriptors after a fork.
518 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000519void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000520channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000521{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000522 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000523
524 for (i = 0; i < channels_alloc; i++)
525 if (channels[i] != NULL)
526 channel_close_fds(channels[i]);
527}
528
529/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000530 * Stop listening to channels.
531 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000532void
533channel_stop_listening(void)
534{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000535 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000536 Channel *c;
537
538 for (i = 0; i < channels_alloc; i++) {
539 c = channels[i];
540 if (c != NULL) {
541 switch (c->type) {
542 case SSH_CHANNEL_AUTH_SOCKET:
543 case SSH_CHANNEL_PORT_LISTENER:
544 case SSH_CHANNEL_RPORT_LISTENER:
545 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000546 case SSH_CHANNEL_UNIX_LISTENER:
547 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000548 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000549 channel_free(c);
550 break;
551 }
552 }
553 }
554}
555
556/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000557 * Returns true if no channel has too much buffered data, and false if one or
558 * more channel is overfull.
559 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000560int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000561channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000562{
563 u_int i;
564 Channel *c;
565
566 for (i = 0; i < channels_alloc; i++) {
567 c = channels[i];
568 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000569 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000570 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000571 c->self, buffer_len(&c->output),
572 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000573 return 0;
574 }
575 }
576 }
577 return 1;
578}
579
580/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000581int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000582channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000583{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000584 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000585 Channel *c;
586
587 for (i = 0; i < channels_alloc; i++) {
588 c = channels[i];
589 if (c == NULL)
590 continue;
591 switch (c->type) {
592 case SSH_CHANNEL_X11_LISTENER:
593 case SSH_CHANNEL_PORT_LISTENER:
594 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100595 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000596 case SSH_CHANNEL_CLOSED:
597 case SSH_CHANNEL_AUTH_SOCKET:
598 case SSH_CHANNEL_DYNAMIC:
599 case SSH_CHANNEL_CONNECTING:
600 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000601 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000602 case SSH_CHANNEL_UNIX_LISTENER:
603 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000604 continue;
605 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000606 continue;
607 case SSH_CHANNEL_OPENING:
608 case SSH_CHANNEL_OPEN:
609 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100610 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000611 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000612 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000613 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000614 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000615 /* NOTREACHED */
616 }
617 }
618 return 0;
619}
620
621/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000622int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000623channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000624{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000625 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000626 Channel *c;
627
628 for (i = 0; i < channels_alloc; i++) {
629 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000630 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000631 continue;
632 switch (c->type) {
633 case SSH_CHANNEL_CLOSED:
634 case SSH_CHANNEL_DYNAMIC:
635 case SSH_CHANNEL_X11_LISTENER:
636 case SSH_CHANNEL_PORT_LISTENER:
637 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100638 case SSH_CHANNEL_MUX_LISTENER:
639 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000640 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000641 case SSH_CHANNEL_OPENING:
642 case SSH_CHANNEL_CONNECTING:
643 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000644 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000645 case SSH_CHANNEL_UNIX_LISTENER:
646 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000647 continue;
648 case SSH_CHANNEL_LARVAL:
649 case SSH_CHANNEL_AUTH_SOCKET:
650 case SSH_CHANNEL_OPEN:
651 case SSH_CHANNEL_X11_OPEN:
652 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000653 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000654 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000655 /* NOTREACHED */
656 }
657 }
658 return -1;
659}
660
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661/*
662 * Returns a message describing the currently open forwarded connections,
663 * suitable for sending to the client. The message contains crlf pairs for
664 * newlines.
665 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000667channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668{
669 Buffer buffer;
670 Channel *c;
671 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000672 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000673
674 buffer_init(&buffer);
675 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
676 buffer_append(&buffer, buf, strlen(buf));
677 for (i = 0; i < channels_alloc; i++) {
678 c = channels[i];
679 if (c == NULL)
680 continue;
681 switch (c->type) {
682 case SSH_CHANNEL_X11_LISTENER:
683 case SSH_CHANNEL_PORT_LISTENER:
684 case SSH_CHANNEL_RPORT_LISTENER:
685 case SSH_CHANNEL_CLOSED:
686 case SSH_CHANNEL_AUTH_SOCKET:
687 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000688 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100689 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000690 case SSH_CHANNEL_UNIX_LISTENER:
691 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000692 continue;
693 case SSH_CHANNEL_LARVAL:
694 case SSH_CHANNEL_OPENING:
695 case SSH_CHANNEL_CONNECTING:
696 case SSH_CHANNEL_DYNAMIC:
697 case SSH_CHANNEL_OPEN:
698 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000699 case SSH_CHANNEL_MUX_PROXY:
700 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000701 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000702 " #%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 +0000703 c->self, c->remote_name,
704 c->type, c->remote_id,
705 c->istate, buffer_len(&c->input),
706 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100707 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000708 buffer_append(&buffer, buf, strlen(buf));
709 continue;
710 default:
711 fatal("channel_open_message: bad channel type %d", c->type);
712 /* NOTREACHED */
713 }
714 }
715 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000716 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000717 buffer_free(&buffer);
718 return cp;
719}
720
721void
722channel_send_open(int id)
723{
724 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000725
Ben Lindstrome9c99912001-06-09 00:41:05 +0000726 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000727 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728 return;
729 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000730 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000731 packet_start(SSH2_MSG_CHANNEL_OPEN);
732 packet_put_cstring(c->ctype);
733 packet_put_int(c->self);
734 packet_put_int(c->local_window);
735 packet_put_int(c->local_maxpacket);
736 packet_send();
737}
738
739void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000740channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000741{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000742 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000743
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000745 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000746 return;
747 }
Damien Miller0e220db2004-06-15 10:34:08 +1000748 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000749 packet_start(SSH2_MSG_CHANNEL_REQUEST);
750 packet_put_int(c->remote_id);
751 packet_put_cstring(service);
752 packet_put_char(wantconfirm);
753}
Damien Miller4f7becb2006-03-26 14:10:14 +1100754
Ben Lindstrome9c99912001-06-09 00:41:05 +0000755void
Damien Millerb84886b2008-05-19 15:05:07 +1000756channel_register_status_confirm(int id, channel_confirm_cb *cb,
757 channel_confirm_abandon_cb *abandon_cb, void *ctx)
758{
759 struct channel_confirm *cc;
760 Channel *c;
761
762 if ((c = channel_lookup(id)) == NULL)
763 fatal("channel_register_expect: %d: bad id", id);
764
Damien Miller6c81fee2013-11-08 12:19:55 +1100765 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000766 cc->cb = cb;
767 cc->abandon_cb = abandon_cb;
768 cc->ctx = ctx;
769 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
770}
771
772void
Damien Millerd530f5f2010-05-21 14:57:10 +1000773channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000774{
775 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000776
Ben Lindstrome9c99912001-06-09 00:41:05 +0000777 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100778 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000779 return;
780 }
Damien Millerb84886b2008-05-19 15:05:07 +1000781 c->open_confirm = fn;
782 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000783}
Damien Miller4f7becb2006-03-26 14:10:14 +1100784
Ben Lindstrome9c99912001-06-09 00:41:05 +0000785void
Damien Miller39eda6e2005-11-05 14:52:50 +1100786channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000787{
Damien Millerd47c62a2005-12-13 19:33:57 +1100788 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000789
Ben Lindstrome9c99912001-06-09 00:41:05 +0000790 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000791 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 return;
793 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000794 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100795 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796}
Damien Miller4f7becb2006-03-26 14:10:14 +1100797
Ben Lindstrome9c99912001-06-09 00:41:05 +0000798void
799channel_cancel_cleanup(int id)
800{
Damien Millerd47c62a2005-12-13 19:33:57 +1100801 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000802
Ben Lindstrome9c99912001-06-09 00:41:05 +0000803 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000804 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000805 return;
806 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000807 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100808 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000809}
Damien Miller4f7becb2006-03-26 14:10:14 +1100810
Ben Lindstrome9c99912001-06-09 00:41:05 +0000811void
Damien Miller077b2382005-12-31 16:22:32 +1100812channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000813 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000814{
815 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000816
Ben Lindstrome9c99912001-06-09 00:41:05 +0000817 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000818 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000819 return;
820 }
Damien Miller077b2382005-12-31 16:22:32 +1100821 c->input_filter = ifn;
822 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000823 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000824 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000825}
826
827void
828channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000829 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000830{
831 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000832
Ben Lindstrome9c99912001-06-09 00:41:05 +0000833 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
834 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000835 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000836 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100837 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
839 packet_put_int(c->remote_id);
840 packet_put_int(c->local_window);
841 packet_send();
842}
843
Damien Miller5428f641999-11-25 11:54:57 +1100844/*
Damien Millerb38eff82000-04-01 11:09:21 +1000845 * 'channel_pre*' are called just before select() to add any bits relevant to
846 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100847 */
Damien Millerb38eff82000-04-01 11:09:21 +1000848/*
849 * 'channel_post*': perform any appropriate operations for channels which
850 * have events pending.
851 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100852typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000853chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
854chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
855
Damien Miller8473dd82006-07-24 14:08:32 +1000856/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000857static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100858channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000859{
860 FD_SET(c->sock, readset);
861}
862
Damien Miller8473dd82006-07-24 14:08:32 +1000863/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000864static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100865channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000866{
867 debug3("channel %d: waiting for connection", c->self);
868 FD_SET(c->sock, writeset);
869}
870
Ben Lindstrombba81212001-06-25 05:01:22 +0000871static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100872channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000873{
Damien Miller33b13562000-04-04 14:38:59 +1000874 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000875 c->remote_window > 0 &&
876 buffer_len(&c->input) < c->remote_window &&
Damien Miller499a0d52006-04-23 12:06:03 +1000877 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000878 FD_SET(c->rfd, readset);
879 if (c->ostate == CHAN_OUTPUT_OPEN ||
880 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
881 if (buffer_len(&c->output) > 0) {
882 FD_SET(c->wfd, writeset);
883 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000884 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000885 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
886 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000887 else
888 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000889 }
890 }
891 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000892 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
893 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000894 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
895 buffer_len(&c->extended) > 0)
896 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000897 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
898 (c->extended_usage == CHAN_EXTENDED_READ ||
899 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000900 buffer_len(&c->extended) < c->remote_window)
901 FD_SET(c->efd, readset);
902 }
Damien Miller0e220db2004-06-15 10:34:08 +1000903 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000904}
905
Damien Millerb38eff82000-04-01 11:09:21 +1000906/*
907 * This is a special state for X11 authentication spoofing. An opened X11
908 * connection (when authentication spoofing is being done) remains in this
909 * state until the first packet has been completely read. The authentication
910 * data in that packet is then substituted by the real data if it matches the
911 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000912 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000913 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000914 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000915static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000916x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000917{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000918 u_char *ucp;
919 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000920
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000921 /* Is this being called after the refusal deadline? */
922 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
923 verbose("Rejected X11 connection after ForwardX11Timeout "
924 "expired");
925 return -1;
926 }
927
Damien Millerb38eff82000-04-01 11:09:21 +1000928 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000929 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000930 return 0;
931
932 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100933 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000934 if (ucp[0] == 0x42) { /* Byte order MSB first. */
935 proto_len = 256 * ucp[6] + ucp[7];
936 data_len = 256 * ucp[8] + ucp[9];
937 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
938 proto_len = ucp[6] + 256 * ucp[7];
939 data_len = ucp[8] + 256 * ucp[9];
940 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000941 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100942 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000943 return -1;
944 }
945
946 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000947 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000948 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
949 return 0;
950
951 /* Check if authentication protocol matches. */
952 if (proto_len != strlen(x11_saved_proto) ||
953 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000954 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000955 return -1;
956 }
957 /* Check if authentication data matches our fake data. */
958 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000959 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000960 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000961 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000962 return -1;
963 }
964 /* Check fake data length */
965 if (x11_fake_data_len != x11_saved_data_len) {
966 error("X11 fake_data_len %d != saved_data_len %d",
967 x11_fake_data_len, x11_saved_data_len);
968 return -1;
969 }
970 /*
971 * Received authentication protocol and data match
972 * our fake data. Substitute the fake data with real
973 * data.
974 */
975 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
976 x11_saved_data, x11_saved_data_len);
977 return 1;
978}
979
Ben Lindstrombba81212001-06-25 05:01:22 +0000980static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100981channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000982{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000983 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000984
985 /* c->force_drain = 1; */
986
Damien Millerb38eff82000-04-01 11:09:21 +1000987 if (ret == 1) {
988 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100989 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000990 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000991 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000992 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100993 chan_read_failed(c);
994 buffer_clear(&c->input);
995 chan_ibuf_empty(c);
996 buffer_clear(&c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000997 chan_write_failed(c);
Damien Millerfbdeece2003-09-02 22:52:31 +1000998 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000999 }
1000}
1001
Damien Millere1537f92010-01-26 13:26:22 +11001002static void
1003channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1004{
Damien Millerd530f5f2010-05-21 14:57:10 +10001005 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001006 buffer_check_alloc(&c->input, CHAN_RBUF))
1007 FD_SET(c->rfd, readset);
1008 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1009 /* clear buffer immediately (discard any partial packet) */
1010 buffer_clear(&c->input);
1011 chan_ibuf_empty(c);
1012 /* Start output drain. XXX just kill chan? */
1013 chan_rcvd_oclose(c);
1014 }
1015 if (c->ostate == CHAN_OUTPUT_OPEN ||
1016 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1017 if (buffer_len(&c->output) > 0)
1018 FD_SET(c->wfd, writeset);
1019 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1020 chan_obuf_empty(c);
1021 }
1022}
1023
Ben Lindstromb3921512001-04-11 15:57:50 +00001024/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001025/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001026static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001027channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001028{
Damien Millera10f5612002-09-12 09:49:15 +10001029 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001030 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001031 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001032 struct {
1033 u_int8_t version;
1034 u_int8_t command;
1035 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001036 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001037 } s4_req, s4_rsp;
1038
Ben Lindstromb3921512001-04-11 15:57:50 +00001039 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001040
1041 have = buffer_len(&c->input);
1042 len = sizeof(s4_req);
1043 if (have < len)
1044 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001045 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001046
1047 need = 1;
1048 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1049 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1050 debug2("channel %d: socks4a request", c->self);
1051 /* ... and needs an extra string (the hostname) */
1052 need = 2;
1053 }
1054 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001055 for (found = 0, i = len; i < have; i++) {
1056 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001057 found++;
1058 if (found == need)
1059 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001060 }
1061 if (i > 1024) {
1062 /* the peer is probably sending garbage */
1063 debug("channel %d: decode socks4: too long",
1064 c->self);
1065 return -1;
1066 }
1067 }
Damien Miller1781f532009-01-28 16:24:41 +11001068 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001069 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001070 buffer_get(&c->input, (char *)&s4_req.version, 1);
1071 buffer_get(&c->input, (char *)&s4_req.command, 1);
1072 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001073 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001074 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001075 p = (char *)buffer_ptr(&c->input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001076 if (memchr(p, '\0', have) == NULL) {
1077 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001078 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001079 return -1;
1080 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001081 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001082 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001083 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001084 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001085 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001086 c->self, len, have);
1087 strlcpy(username, p, sizeof(username));
1088 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001089
Darren Tuckera627d422013-06-02 07:31:17 +10001090 free(c->path);
1091 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001092 if (need == 1) { /* SOCKS4: one string */
1093 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001094 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001095 } else { /* SOCKS4A: two strings */
1096 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001097 p = (char *)buffer_ptr(&c->input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001098 if (memchr(p, '\0', have) == NULL) {
1099 error("channel %d: decode socks4a: host not nul "
1100 "terminated", c->self);
1101 return -1;
1102 }
Damien Miller1781f532009-01-28 16:24:41 +11001103 len = strlen(p);
1104 debug2("channel %d: decode socks4a: host %s/%d",
1105 c->self, p, len);
1106 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001107 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001108 error("channel %d: hostname \"%.100s\" too long",
1109 c->self, p);
1110 return -1;
1111 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001112 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001113 buffer_consume(&c->input, len);
1114 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001115 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001116
Damien Millerfbdeece2003-09-02 22:52:31 +10001117 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001118 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001119
Ben Lindstromb3921512001-04-11 15:57:50 +00001120 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001121 debug("channel %d: cannot handle: %s cn %d",
1122 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001123 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001124 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001125 s4_rsp.version = 0; /* vn: 0 for reply */
1126 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001127 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001128 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001129 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001130 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001131}
1132
Darren Tucker46471c92003-07-03 13:55:19 +10001133/* try to decode a socks5 header */
1134#define SSH_SOCKS5_AUTHDONE 0x1000
1135#define SSH_SOCKS5_NOAUTH 0x00
1136#define SSH_SOCKS5_IPV4 0x01
1137#define SSH_SOCKS5_DOMAIN 0x03
1138#define SSH_SOCKS5_IPV6 0x04
1139#define SSH_SOCKS5_CONNECT 0x01
1140#define SSH_SOCKS5_SUCCESS 0x00
1141
Damien Miller8473dd82006-07-24 14:08:32 +10001142/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001143static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001144channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001145{
1146 struct {
1147 u_int8_t version;
1148 u_int8_t command;
1149 u_int8_t reserved;
1150 u_int8_t atyp;
1151 } s5_req, s5_rsp;
1152 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001153 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1154 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001155 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001156
1157 debug2("channel %d: decode socks5", c->self);
1158 p = buffer_ptr(&c->input);
1159 if (p[0] != 0x05)
1160 return -1;
1161 have = buffer_len(&c->input);
1162 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1163 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001164 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001165 return 0;
1166 nmethods = p[1];
1167 if (have < nmethods + 2)
1168 return 0;
1169 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001170 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001171 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001172 found = 1;
1173 break;
1174 }
1175 }
1176 if (!found) {
1177 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1178 c->self);
1179 return -1;
1180 }
1181 buffer_consume(&c->input, nmethods + 2);
1182 buffer_put_char(&c->output, 0x05); /* version */
1183 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1184 FD_SET(c->sock, writeset);
1185 c->flags |= SSH_SOCKS5_AUTHDONE;
1186 debug2("channel %d: socks5 auth done", c->self);
1187 return 0; /* need more */
1188 }
1189 debug2("channel %d: socks5 post auth", c->self);
1190 if (have < sizeof(s5_req)+1)
1191 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001192 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001193 if (s5_req.version != 0x05 ||
1194 s5_req.command != SSH_SOCKS5_CONNECT ||
1195 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001196 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001197 return -1;
1198 }
Darren Tucker47eede72005-03-14 23:08:12 +11001199 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001200 case SSH_SOCKS5_IPV4:
1201 addrlen = 4;
1202 af = AF_INET;
1203 break;
1204 case SSH_SOCKS5_DOMAIN:
1205 addrlen = p[sizeof(s5_req)];
1206 af = -1;
1207 break;
1208 case SSH_SOCKS5_IPV6:
1209 addrlen = 16;
1210 af = AF_INET6;
1211 break;
1212 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001213 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001214 return -1;
1215 }
Damien Miller0f077072006-07-10 22:21:02 +10001216 need = sizeof(s5_req) + addrlen + 2;
1217 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1218 need++;
1219 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001220 return 0;
1221 buffer_consume(&c->input, sizeof(s5_req));
1222 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1223 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001224 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001225 buffer_get(&c->input, (char *)&dest_port, 2);
1226 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001227 free(c->path);
1228 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001229 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001230 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001231 error("channel %d: dynamic request: socks5 hostname "
1232 "\"%.100s\" too long", c->self, dest_addr);
1233 return -1;
1234 }
1235 c->path = xstrdup(dest_addr);
1236 } else {
1237 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1238 return -1;
1239 c->path = xstrdup(ntop);
1240 }
Darren Tucker46471c92003-07-03 13:55:19 +10001241 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001242
Damien Millerfbdeece2003-09-02 22:52:31 +10001243 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001244 c->self, c->path, c->host_port, s5_req.command);
1245
1246 s5_rsp.version = 0x05;
1247 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1248 s5_rsp.reserved = 0; /* ignored */
1249 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001250 dest_port = 0; /* ignored */
1251
Damien Millera0fdce92006-03-26 14:28:50 +11001252 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001253 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001254 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001255 return 1;
1256}
1257
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001258Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001259channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1260 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001261{
1262 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001263
1264 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1265 port_to_connect);
1266
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001267 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1268 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1269 0, "stdio-forward", /*nonblock*/0);
1270
1271 c->path = xstrdup(host_to_connect);
1272 c->host_port = port_to_connect;
1273 c->listening_port = 0;
1274 c->force_drain = 1;
1275
1276 channel_register_fds(c, in, out, -1, 0, 1, 0);
1277 port_open_helper(c, "direct-tcpip");
1278
1279 return c;
1280}
1281
Ben Lindstromb3921512001-04-11 15:57:50 +00001282/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001283static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001284channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001285{
1286 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001287 u_int have;
1288 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001289
1290 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001291 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001292 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001293 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001294 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001295 /* need more */
1296 FD_SET(c->sock, readset);
1297 return;
1298 }
1299 /* try to guess the protocol */
1300 p = buffer_ptr(&c->input);
1301 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001302 case 0x04:
1303 ret = channel_decode_socks4(c, readset, writeset);
1304 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001305 case 0x05:
1306 ret = channel_decode_socks5(c, readset, writeset);
1307 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001308 default:
1309 ret = -1;
1310 break;
1311 }
1312 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001313 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001314 } else if (ret == 0) {
1315 debug2("channel %d: pre_dynamic: need more", c->self);
1316 /* need more */
1317 FD_SET(c->sock, readset);
1318 } else {
1319 /* switch to the next state */
1320 c->type = SSH_CHANNEL_OPENING;
1321 port_open_helper(c, "direct-tcpip");
1322 }
1323}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001324
Damien Millerb38eff82000-04-01 11:09:21 +10001325/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001326/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001327static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001328channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001329{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001330 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001331 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001332 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001333 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001334 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001335 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001336
1337 if (FD_ISSET(c->sock, readset)) {
1338 debug("X11 connection requested.");
1339 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001340 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001341 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001342 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001343 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001344 channel_close_fd(&c->sock);
1345 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001346 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001347 }
Damien Millerb38eff82000-04-01 11:09:21 +10001348 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001349 if (errno != EINTR && errno != EWOULDBLOCK &&
1350 errno != ECONNABORTED)
1351 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001352 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001353 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001354 return;
1355 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001356 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001357 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001358 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001359 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001360 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001361
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001362 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001363 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001364 c->local_window_max, c->local_maxpacket, 0, buf, 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001365 packet_start(SSH2_MSG_CHANNEL_OPEN);
1366 packet_put_cstring("x11");
1367 packet_put_int(nc->self);
1368 packet_put_int(nc->local_window_max);
1369 packet_put_int(nc->local_maxpacket);
1370 /* originator ipaddr and port */
1371 packet_put_cstring(remote_ipaddr);
1372 if (datafellows & SSH_BUG_X11FWD) {
1373 debug2("ssh2 x11 bug compat mode");
Damien Millerbd483e72000-04-30 10:00:53 +10001374 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001375 packet_put_int(remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001376 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001377 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001378 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001379 }
1380}
1381
Ben Lindstrombba81212001-06-25 05:01:22 +00001382static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001383port_open_helper(Channel *c, char *rtype)
1384{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001385 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001386 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001387 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001388 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001389 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001390
Damien Millerd6369432010-02-02 17:02:07 +11001391 if (remote_port == -1) {
1392 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001393 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001394 remote_ipaddr = xstrdup("127.0.0.1");
1395 remote_port = 65535;
1396 }
1397
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001398 snprintf(buf, sizeof buf,
1399 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001400 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001401 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001402 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001403
Darren Tuckera627d422013-06-02 07:31:17 +10001404 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001405 c->remote_name = xstrdup(buf);
1406
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001407 packet_start(SSH2_MSG_CHANNEL_OPEN);
1408 packet_put_cstring(rtype);
1409 packet_put_int(c->self);
1410 packet_put_int(c->local_window_max);
1411 packet_put_int(c->local_maxpacket);
1412 if (strcmp(rtype, "direct-tcpip") == 0) {
1413 /* target host, port */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001414 packet_put_cstring(c->path);
1415 packet_put_int(c->host_port);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001416 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1417 /* target path */
1418 packet_put_cstring(c->path);
1419 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1420 /* listen path */
1421 packet_put_cstring(c->path);
1422 } else {
1423 /* listen address, port */
1424 packet_put_cstring(c->path);
1425 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001426 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001427 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1428 /* reserved for future owner/mode info */
1429 packet_put_cstring("");
1430 } else {
1431 /* originator host and port */
1432 packet_put_cstring(remote_ipaddr);
1433 packet_put_int((u_int)remote_port);
1434 }
1435 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001436 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001437 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001438}
1439
Damien Miller5e7fd072005-11-05 14:53:39 +11001440static void
1441channel_set_reuseaddr(int fd)
1442{
1443 int on = 1;
1444
1445 /*
1446 * Set socket options.
1447 * Allow local port reuse in TIME_WAIT.
1448 */
1449 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1450 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1451}
1452
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001453void
1454channel_set_x11_refuse_time(u_int refuse_time)
1455{
1456 x11_refuse_time = refuse_time;
1457}
1458
Damien Millerb38eff82000-04-01 11:09:21 +10001459/*
1460 * This socket is listening for connections to a forwarded TCP/IP port.
1461 */
Damien Miller8473dd82006-07-24 14:08:32 +10001462/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001463static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001464channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001465{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001466 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001467 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001468 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001469 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001470 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001471
Damien Millerb38eff82000-04-01 11:09:21 +10001472 if (FD_ISSET(c->sock, readset)) {
1473 debug("Connection to port %d forwarding "
1474 "to %.100s port %d requested.",
1475 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001476
Damien Miller4623a752001-10-10 15:03:58 +10001477 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1478 nextstate = SSH_CHANNEL_OPENING;
1479 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001480 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1481 nextstate = SSH_CHANNEL_OPENING;
1482 rtype = "forwarded-streamlocal@openssh.com";
1483 } else if (c->host_port == PORT_STREAMLOCAL) {
1484 nextstate = SSH_CHANNEL_OPENING;
1485 rtype = "direct-streamlocal@openssh.com";
1486 } else if (c->host_port == 0) {
1487 nextstate = SSH_CHANNEL_DYNAMIC;
1488 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001489 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001490 nextstate = SSH_CHANNEL_OPENING;
1491 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001492 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001493
Damien Millerb38eff82000-04-01 11:09:21 +10001494 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001495 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001496 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001497 if (errno != EINTR && errno != EWOULDBLOCK &&
1498 errno != ECONNABORTED)
1499 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001500 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001501 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001502 return;
1503 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001504 if (c->host_port != PORT_STREAMLOCAL)
1505 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001506 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1507 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001508 nc->listening_port = c->listening_port;
1509 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001510 if (c->path != NULL)
1511 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001512
Darren Tucker876045b2010-01-08 17:08:00 +11001513 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001514 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001515 }
1516}
1517
1518/*
1519 * This is the authentication agent socket listening for connections from
1520 * clients.
1521 */
Damien Miller8473dd82006-07-24 14:08:32 +10001522/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001523static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001524channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001525{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001526 Channel *nc;
1527 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001528 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001529 socklen_t addrlen;
1530
1531 if (FD_ISSET(c->sock, readset)) {
1532 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001533 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001534 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001535 error("accept from auth socket: %.100s",
1536 strerror(errno));
1537 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001538 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001539 return;
1540 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001541 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001542 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1543 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001544 0, "accepted auth socket", 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001545 packet_start(SSH2_MSG_CHANNEL_OPEN);
1546 packet_put_cstring("auth-agent@openssh.com");
1547 packet_put_int(nc->self);
1548 packet_put_int(c->local_window_max);
1549 packet_put_int(c->local_maxpacket);
Damien Millerb38eff82000-04-01 11:09:21 +10001550 packet_send();
1551 }
1552}
1553
Damien Miller8473dd82006-07-24 14:08:32 +10001554/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001555static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001556channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001557{
Damien Millerbd740252008-05-19 15:37:09 +10001558 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001559 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001560
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001561 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001562 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001563 err = errno;
1564 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001565 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001566 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001567 debug("channel %d: connected to %s port %d",
1568 c->self, c->connect_ctx.host, c->connect_ctx.port);
1569 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001570 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001571 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1572 packet_put_int(c->remote_id);
1573 packet_put_int(c->self);
1574 packet_put_int(c->local_window);
1575 packet_put_int(c->local_maxpacket);
Ben Lindstrom69128662001-05-08 20:07:39 +00001576 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001577 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001578 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001579 /* Try next address, if any */
1580 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1581 close(c->sock);
1582 c->sock = c->rfd = c->wfd = sock;
1583 channel_max_fd = channel_find_maxfd();
1584 return;
1585 }
1586 /* Exhausted all addresses */
1587 error("connect_to %.100s port %d: failed.",
1588 c->connect_ctx.host, c->connect_ctx.port);
1589 channel_connect_ctx_free(&c->connect_ctx);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001590 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1591 packet_put_int(c->remote_id);
1592 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1593 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1594 packet_put_cstring(strerror(err));
1595 packet_put_cstring("");
Ben Lindstrom69128662001-05-08 20:07:39 +00001596 }
1597 chan_mark_dead(c);
1598 }
1599 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001600 }
1601}
1602
Damien Miller8473dd82006-07-24 14:08:32 +10001603/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001604static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001605channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001606{
Darren Tucker11327cc2005-03-14 23:22:25 +11001607 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001608 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001609
Damien Miller835284b2007-06-11 13:03:16 +10001610 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1611 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001612 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001613 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001614 if (len < 0 && (errno == EINTR ||
1615 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001616 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001617#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001618 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001619#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001620 if ((!c->isatty && len <= 0) ||
1621 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001622#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001623 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001624 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001625 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001626 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001627 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001628 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001629 } else {
1630 chan_read_failed(c);
1631 }
1632 return -1;
1633 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001634 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001635 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001636 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001637 chan_read_failed(c);
1638 }
Damien Millerd27b9472005-12-13 19:29:02 +11001639 } else if (c->datagram) {
1640 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001641 } else {
1642 buffer_append(&c->input, buf, len);
1643 }
Damien Millerb38eff82000-04-01 11:09:21 +10001644 }
1645 return 1;
1646}
Damien Miller4f7becb2006-03-26 14:10:14 +11001647
Damien Miller8473dd82006-07-24 14:08:32 +10001648/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001649static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001650channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001651{
Ben Lindstrome229b252001-03-05 06:28:06 +00001652 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001653 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001654 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001655 int len;
1656
1657 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001658 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001659 FD_ISSET(c->wfd, writeset) &&
1660 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001661 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001662 if (c->output_filter != NULL) {
1663 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1664 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001665 if (c->type != SSH_CHANNEL_OPEN)
1666 chan_mark_dead(c);
1667 else
1668 chan_write_failed(c);
1669 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001670 }
1671 } else if (c->datagram) {
1672 buf = data = buffer_get_string(&c->output, &dlen);
1673 } else {
1674 buf = data = buffer_ptr(&c->output);
1675 dlen = buffer_len(&c->output);
1676 }
1677
Damien Millerd27b9472005-12-13 19:29:02 +11001678 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001679 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001680 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001681 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001682 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1683 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001684 return 1;
1685 if (len <= 0) {
1686 if (c->type != SSH_CHANNEL_OPEN)
1687 chan_mark_dead(c);
1688 else
1689 chan_write_failed(c);
1690 return -1;
1691 }
Damien Miller7d457182010-08-05 23:09:48 +10001692 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001693 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001694#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001695 /* XXX: Later AIX versions can't push as much data to tty */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001696 if (c->wfd_isatty)
Darren Tucker240fdfa2003-11-22 14:10:02 +11001697 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001698#endif
Damien Miller077b2382005-12-31 16:22:32 +11001699
1700 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001701 if (len < 0 &&
1702 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001703 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001704 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001705 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001706 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001707 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001708 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001709 } else {
1710 chan_write_failed(c);
1711 }
1712 return -1;
1713 }
Darren Tucker3980b632009-08-28 11:02:37 +10001714#ifndef BROKEN_TCGETATTR_ICANON
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001715 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001716 if (tcgetattr(c->wfd, &tio) == 0 &&
1717 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1718 /*
1719 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001720 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001721 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001722 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001723 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001724 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001725 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001726 }
1727 }
Darren Tucker3980b632009-08-28 11:02:37 +10001728#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001729 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001730 }
Damien Miller7d457182010-08-05 23:09:48 +10001731 out:
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001732 if (olen > 0)
Damien Miller7d457182010-08-05 23:09:48 +10001733 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001734 return 1;
1735}
Damien Miller4f7becb2006-03-26 14:10:14 +11001736
Ben Lindstrombba81212001-06-25 05:01:22 +00001737static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001738channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001739{
Darren Tucker11327cc2005-03-14 23:22:25 +11001740 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001741 int len;
1742
Damien Miller1383bd82000-04-06 12:32:37 +10001743/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001744 if (c->efd != -1) {
1745 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1746 FD_ISSET(c->efd, writeset) &&
1747 buffer_len(&c->extended) > 0) {
1748 len = write(c->efd, buffer_ptr(&c->extended),
1749 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001750 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001751 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001752 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1753 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001754 return 1;
1755 if (len <= 0) {
1756 debug2("channel %d: closing write-efd %d",
1757 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001758 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001759 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001760 buffer_consume(&c->extended, len);
1761 c->local_consumed += len;
1762 }
Damien Miller8853ca52010-06-26 10:00:14 +10001763 } else if (c->efd != -1 &&
1764 (c->extended_usage == CHAN_EXTENDED_READ ||
1765 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001766 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001767 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001768 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001769 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001770 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1771 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001772 return 1;
1773 if (len <= 0) {
1774 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001775 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001776 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001777 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001778 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1779 debug3("channel %d: discard efd",
1780 c->self);
1781 } else
1782 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001783 }
Damien Miller33b13562000-04-04 14:38:59 +10001784 }
1785 }
1786 return 1;
1787}
Damien Miller4f7becb2006-03-26 14:10:14 +11001788
Damien Miller0e220db2004-06-15 10:34:08 +10001789static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001790channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001791{
Ben Lindstromb3921512001-04-11 15:57:50 +00001792 if (c->type == SSH_CHANNEL_OPEN &&
1793 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001794 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001795 c->local_maxpacket*3) ||
1796 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001797 c->local_consumed > 0) {
1798 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1799 packet_put_int(c->remote_id);
1800 packet_put_int(c->local_consumed);
1801 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001802 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001803 c->self, c->local_window,
1804 c->local_consumed);
1805 c->local_window += c->local_consumed;
1806 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001807 }
1808 return 1;
1809}
1810
Ben Lindstrombba81212001-06-25 05:01:22 +00001811static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001812channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001813{
1814 channel_handle_rfd(c, readset, writeset);
1815 channel_handle_wfd(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +10001816 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001817 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001818}
1819
Damien Millere1537f92010-01-26 13:26:22 +11001820static u_int
1821read_mux(Channel *c, u_int need)
1822{
1823 char buf[CHAN_RBUF];
1824 int len;
1825 u_int rlen;
1826
1827 if (buffer_len(&c->input) < need) {
1828 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001829 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001830 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1831 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001832 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001833 debug2("channel %d: ctl read<=0 rfd %d len %d",
1834 c->self, c->rfd, len);
1835 chan_read_failed(c);
1836 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001837 } else
1838 buffer_append(&c->input, buf, len);
1839 }
1840 return buffer_len(&c->input);
1841}
1842
1843static void
1844channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1845{
1846 u_int need;
1847 ssize_t len;
1848
Damien Millerd530f5f2010-05-21 14:57:10 +10001849 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001850 (c->istate == CHAN_INPUT_OPEN ||
1851 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1852 /*
1853 * Don't not read past the precise end of packets to
1854 * avoid disrupting fd passing.
1855 */
1856 if (read_mux(c, 4) < 4) /* read header */
1857 return;
1858 need = get_u32(buffer_ptr(&c->input));
1859#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1860 if (need > CHANNEL_MUX_MAX_PACKET) {
1861 debug2("channel %d: packet too big %u > %u",
1862 c->self, CHANNEL_MUX_MAX_PACKET, need);
1863 chan_rcvd_oclose(c);
1864 return;
1865 }
1866 if (read_mux(c, need + 4) < need + 4) /* read body */
1867 return;
1868 if (c->mux_rcb(c) != 0) {
1869 debug("channel %d: mux_rcb failed", c->self);
1870 chan_mark_dead(c);
1871 return;
1872 }
1873 }
1874
1875 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1876 buffer_len(&c->output) > 0) {
1877 len = write(c->wfd, buffer_ptr(&c->output),
1878 buffer_len(&c->output));
1879 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1880 return;
1881 if (len <= 0) {
1882 chan_mark_dead(c);
1883 return;
1884 }
1885 buffer_consume(&c->output, len);
1886 }
1887}
1888
1889static void
1890channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1891{
1892 Channel *nc;
1893 struct sockaddr_storage addr;
1894 socklen_t addrlen;
1895 int newsock;
1896 uid_t euid;
1897 gid_t egid;
1898
1899 if (!FD_ISSET(c->sock, readset))
1900 return;
1901
1902 debug("multiplexing control connection");
1903
1904 /*
1905 * Accept connection on control socket
1906 */
1907 memset(&addr, 0, sizeof(addr));
1908 addrlen = sizeof(addr);
1909 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1910 &addrlen)) == -1) {
1911 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001912 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001913 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001914 return;
1915 }
1916
1917 if (getpeereid(newsock, &euid, &egid) < 0) {
1918 error("%s getpeereid failed: %s", __func__,
1919 strerror(errno));
1920 close(newsock);
1921 return;
1922 }
1923 if ((euid != 0) && (getuid() != euid)) {
1924 error("multiplex uid mismatch: peer euid %u != uid %u",
1925 (u_int)euid, (u_int)getuid());
1926 close(newsock);
1927 return;
1928 }
1929 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
1930 newsock, newsock, -1, c->local_window_max,
1931 c->local_maxpacket, 0, "mux-control", 1);
1932 nc->mux_rcb = c->mux_rcb;
1933 debug3("%s: new mux channel %d fd %d", __func__,
1934 nc->self, nc->sock);
1935 /* establish state */
1936 nc->mux_rcb(nc);
1937 /* mux state transitions must not elicit protocol messages */
1938 nc->flags |= CHAN_LOCAL;
1939}
1940
Ben Lindstrombba81212001-06-25 05:01:22 +00001941static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001942channel_handler_init(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001943{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001944 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001945
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001946 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1947 channel_pre[i] = NULL;
1948 channel_post[i] = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001949 }
Damien Millerde6987c2002-01-22 23:20:40 +11001950 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001951 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001952 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001953 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001954 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
1955 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001956 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001957 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001958 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001959 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11001960 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
1961 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001962
Damien Millerde6987c2002-01-22 23:20:40 +11001963 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001964 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001965 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001966 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
1967 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001968 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001969 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001970 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001971 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11001972 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
1973 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001974}
1975
Damien Miller3ec27592001-10-12 11:35:04 +10001976/* gc dead channels */
1977static void
1978channel_garbage_collect(Channel *c)
1979{
1980 if (c == NULL)
1981 return;
1982 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001983 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001984 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001985 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001986 c->detach_user(c->self, NULL);
1987 /* if we still have a callback */
1988 if (c->detach_user != NULL)
1989 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001990 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001991 }
1992 if (!chan_is_dead(c, 1))
1993 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001994 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001995 channel_free(c);
1996}
1997
Ben Lindstrombba81212001-06-25 05:01:22 +00001998static void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00001999channel_handler(struct ssh *ssh, chan_fn *ftab[],
2000 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002001{
2002 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002003 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002004 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002005 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002006
2007 if (!did_init) {
2008 channel_handler_init();
2009 did_init = 1;
2010 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002011 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002012 if (unpause_secs != NULL)
2013 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002014 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002015 c = channels[i];
2016 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002017 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002018 if (c->delayed) {
2019 if (ftab == channel_pre)
2020 c->delayed = 0;
2021 else
2022 continue;
2023 }
Damien Millera6508752012-04-22 11:21:10 +10002024 if (ftab[c->type] != NULL) {
2025 /*
2026 * Run handlers that are not paused.
2027 */
2028 if (c->notbefore <= now)
2029 (*ftab[c->type])(c, readset, writeset);
2030 else if (unpause_secs != NULL) {
2031 /*
2032 * Collect the time that the earliest
2033 * channel comes off pause.
2034 */
2035 debug3("%s: chan %d: skip for %d more seconds",
2036 __func__, c->self,
2037 (int)(c->notbefore - now));
2038 if (*unpause_secs == 0 ||
2039 (c->notbefore - now) < *unpause_secs)
2040 *unpause_secs = c->notbefore - now;
2041 }
2042 }
Damien Miller3ec27592001-10-12 11:35:04 +10002043 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002044 }
Damien Millera6508752012-04-22 11:21:10 +10002045 if (unpause_secs != NULL && *unpause_secs != 0)
2046 debug3("%s: first channel unpauses in %d seconds",
2047 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002048}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002049
Ben Lindstrome9c99912001-06-09 00:41:05 +00002050/*
2051 * Allocate/update select bitmasks and add any bits relevant to channels in
2052 * select bitmasks.
2053 */
Damien Miller4af51302000-04-16 11:18:38 +10002054void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002055channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2056 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002057{
Damien Miller36812092006-03-26 14:22:47 +11002058 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002059
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002060 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002061
Damien Miller36812092006-03-26 14:22:47 +11002062 nfdset = howmany(n+1, NFDBITS);
2063 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002064 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002065 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2066 sz = nfdset * sizeof(fd_mask);
2067
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002068 /* perhaps check sz < nalloc/2 and shrink? */
2069 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002070 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2071 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002072 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002073 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002074 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002075 memset(*readsetp, 0, sz);
2076 memset(*writesetp, 0, sz);
2077
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002078 if (!ssh_packet_is_rekeying(ssh))
2079 channel_handler(ssh, channel_pre, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002080 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002081}
2082
Ben Lindstrome9c99912001-06-09 00:41:05 +00002083/*
2084 * After select, perform any appropriate operations for channels which have
2085 * events pending.
2086 */
Damien Miller4af51302000-04-16 11:18:38 +10002087void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002088channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002089{
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002090 channel_handler(ssh, channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091}
2092
Ben Lindstrome9c99912001-06-09 00:41:05 +00002093
Damien Miller5e953212001-01-30 09:14:00 +11002094/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002095void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002096channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002097{
Damien Millerb38eff82000-04-01 11:09:21 +10002098 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002099 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100
Damien Miller95def091999-11-25 00:26:21 +11002101 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002102 c = channels[i];
2103 if (c == NULL)
2104 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002105
Ben Lindstrome9c99912001-06-09 00:41:05 +00002106 /*
2107 * We are only interested in channels that can have buffered
2108 * incoming data.
2109 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002110 if (c->type != SSH_CHANNEL_OPEN)
2111 continue;
2112 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002113 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002114 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002115 continue;
2116 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002117
Damien Miller95def091999-11-25 00:26:21 +11002118 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002119 if ((c->istate == CHAN_INPUT_OPEN ||
2120 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2121 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002122 if (c->datagram) {
2123 if (len > 0) {
2124 u_char *data;
2125 u_int dlen;
2126
2127 data = buffer_get_string(&c->input,
2128 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002129 if (dlen > c->remote_window ||
2130 dlen > c->remote_maxpacket) {
2131 debug("channel %d: datagram "
2132 "too big for channel",
2133 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002134 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002135 continue;
2136 }
Damien Millerd27b9472005-12-13 19:29:02 +11002137 packet_start(SSH2_MSG_CHANNEL_DATA);
2138 packet_put_int(c->remote_id);
2139 packet_put_string(data, dlen);
2140 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002141 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002142 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002143 }
2144 continue;
2145 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002146 /*
2147 * Send some data for the other side over the secure
2148 * connection.
2149 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002150 if (len > c->remote_window)
2151 len = c->remote_window;
2152 if (len > c->remote_maxpacket)
2153 len = c->remote_maxpacket;
Damien Millerb38eff82000-04-01 11:09:21 +10002154 if (len > 0) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002155 packet_start(SSH2_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002156 packet_put_int(c->remote_id);
2157 packet_put_string(buffer_ptr(&c->input), len);
2158 packet_send();
2159 buffer_consume(&c->input, len);
2160 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002161 }
2162 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller5428f641999-11-25 11:54:57 +11002163 /*
2164 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002165 * tell peer, that we will not send more data: send IEOF.
2166 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002167 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002168 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002169 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2170 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002171 else
2172 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002173 }
Damien Miller33b13562000-04-04 14:38:59 +10002174 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002175 if (!(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002176 c->remote_window > 0 &&
2177 (len = buffer_len(&c->extended)) > 0 &&
2178 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002179 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002180 c->self, c->remote_window, buffer_len(&c->extended),
2181 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002182 if (len > c->remote_window)
2183 len = c->remote_window;
2184 if (len > c->remote_maxpacket)
2185 len = c->remote_maxpacket;
2186 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2187 packet_put_int(c->remote_id);
2188 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2189 packet_put_string(buffer_ptr(&c->extended), len);
2190 packet_send();
2191 buffer_consume(&c->extended, len);
2192 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002193 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002194 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002195 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002196}
2197
markus@openbsd.org8d057842016-09-30 09:19:13 +00002198/* -- mux proxy support */
2199
2200/*
2201 * When multiplexing channel messages for mux clients we have to deal
2202 * with downstream messages from the mux client and upstream messages
2203 * from the ssh server:
2204 * 1) Handling downstream messages is straightforward and happens
2205 * in channel_proxy_downstream():
2206 * - We forward all messages (mostly) unmodified to the server.
2207 * - However, in order to route messages from upstream to the correct
2208 * downstream client, we have to replace the channel IDs used by the
2209 * mux clients with a unique channel ID because the mux clients might
2210 * use conflicting channel IDs.
2211 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2212 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2213 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2214 * with the newly allocated channel ID.
2215 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2216 * channels and procesed by channel_proxy_upstream(). The local channel ID
2217 * is then translated back to the original mux client ID.
2218 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2219 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2220 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2221 * downstream mux client are removed.
2222 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2223 * requires more work, because they are not addressed to a specific
2224 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2225 * out whether the request is addressed to the local client or a
2226 * specific downstream client based on the listen-address/port.
2227 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2228 * not supported as the matching session/channel cannot be identified
2229 * easily.
2230 */
2231
2232/*
2233 * receive packets from downstream mux clients:
2234 * channel callback fired on read from mux client, creates
2235 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2236 * on channel creation.
2237 */
2238int
2239channel_proxy_downstream(Channel *downstream)
2240{
2241 Channel *c = NULL;
2242 struct ssh *ssh = active_state;
2243 struct sshbuf *original = NULL, *modified = NULL;
2244 const u_char *cp;
2245 char *ctype = NULL, *listen_host = NULL;
2246 u_char type;
2247 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002248 int ret = -1, r, idx;
2249 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002250
2251 /* sshbuf_dump(&downstream->input, stderr); */
2252 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2253 != 0) {
2254 error("%s: malformed message: %s", __func__, ssh_err(r));
2255 return -1;
2256 }
2257 if (have < 2) {
2258 error("%s: short message", __func__);
2259 return -1;
2260 }
2261 type = cp[1];
2262 /* skip padlen + type */
2263 cp += 2;
2264 have -= 2;
2265 if (ssh_packet_log_type(type))
2266 debug3("%s: channel %u: down->up: type %u", __func__,
2267 downstream->self, type);
2268
2269 switch (type) {
2270 case SSH2_MSG_CHANNEL_OPEN:
2271 if ((original = sshbuf_from(cp, have)) == NULL ||
2272 (modified = sshbuf_new()) == NULL) {
2273 error("%s: alloc", __func__);
2274 goto out;
2275 }
2276 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2277 (r = sshbuf_get_u32(original, &id)) != 0) {
2278 error("%s: parse error %s", __func__, ssh_err(r));
2279 goto out;
2280 }
2281 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2282 -1, -1, -1, 0, 0, 0, ctype, 1);
2283 c->mux_ctx = downstream; /* point to mux client */
2284 c->mux_downstream_id = id; /* original downstream id */
2285 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2286 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2287 (r = sshbuf_putb(modified, original)) != 0) {
2288 error("%s: compose error %s", __func__, ssh_err(r));
2289 channel_free(c);
2290 goto out;
2291 }
2292 break;
2293 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2294 /*
2295 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2296 * need to parse 'remote_id' instead of 'ctype'.
2297 */
2298 if ((original = sshbuf_from(cp, have)) == NULL ||
2299 (modified = sshbuf_new()) == NULL) {
2300 error("%s: alloc", __func__);
2301 goto out;
2302 }
2303 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2304 (r = sshbuf_get_u32(original, &id)) != 0) {
2305 error("%s: parse error %s", __func__, ssh_err(r));
2306 goto out;
2307 }
2308 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2309 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2310 c->mux_ctx = downstream; /* point to mux client */
2311 c->mux_downstream_id = id;
2312 c->remote_id = remote_id;
2313 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2314 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2315 (r = sshbuf_putb(modified, original)) != 0) {
2316 error("%s: compose error %s", __func__, ssh_err(r));
2317 channel_free(c);
2318 goto out;
2319 }
2320 break;
2321 case SSH2_MSG_GLOBAL_REQUEST:
2322 if ((original = sshbuf_from(cp, have)) == NULL) {
2323 error("%s: alloc", __func__);
2324 goto out;
2325 }
2326 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2327 error("%s: parse error %s", __func__, ssh_err(r));
2328 goto out;
2329 }
2330 if (strcmp(ctype, "tcpip-forward") != 0) {
2331 error("%s: unsupported request %s", __func__, ctype);
2332 goto out;
2333 }
2334 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2335 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2336 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2337 error("%s: parse error %s", __func__, ssh_err(r));
2338 goto out;
2339 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002340 if (listen_port > 65535) {
2341 error("%s: tcpip-forward for %s: bad port %u",
2342 __func__, listen_host, listen_port);
2343 goto out;
2344 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002345 /* Record that connection to this host/port is permitted. */
2346 permitted_opens = xreallocarray(permitted_opens,
2347 num_permitted_opens + 1, sizeof(*permitted_opens));
2348 idx = num_permitted_opens++;
2349 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2350 permitted_opens[idx].port_to_connect = -1;
2351 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002352 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002353 permitted_opens[idx].downstream = downstream;
2354 listen_host = NULL;
2355 break;
2356 case SSH2_MSG_CHANNEL_CLOSE:
2357 if (have < 4)
2358 break;
2359 remote_id = PEEK_U32(cp);
2360 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2361 if (c->flags & CHAN_CLOSE_RCVD)
2362 channel_free(c);
2363 else
2364 c->flags |= CHAN_CLOSE_SENT;
2365 }
2366 break;
2367 }
2368 if (modified) {
2369 if ((r = sshpkt_start(ssh, type)) != 0 ||
2370 (r = sshpkt_putb(ssh, modified)) != 0 ||
2371 (r = sshpkt_send(ssh)) != 0) {
2372 error("%s: send %s", __func__, ssh_err(r));
2373 goto out;
2374 }
2375 } else {
2376 if ((r = sshpkt_start(ssh, type)) != 0 ||
2377 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2378 (r = sshpkt_send(ssh)) != 0) {
2379 error("%s: send %s", __func__, ssh_err(r));
2380 goto out;
2381 }
2382 }
2383 ret = 0;
2384 out:
2385 free(ctype);
2386 free(listen_host);
2387 sshbuf_free(original);
2388 sshbuf_free(modified);
2389 return ret;
2390}
2391
2392/*
2393 * receive packets from upstream server and de-multiplex packets
2394 * to correct downstream:
2395 * implemented as a helper for channel input handlers,
2396 * replaces local (proxy) channel ID with downstream channel ID.
2397 */
2398int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002399channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002400{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002401 struct sshbuf *b = NULL;
2402 Channel *downstream;
2403 const u_char *cp = NULL;
2404 size_t len;
2405 int r;
2406
2407 /*
2408 * When receiving packets from the peer we need to check whether we
2409 * need to forward the packets to the mux client. In this case we
2410 * restore the orignal channel id and keep track of CLOSE messages,
2411 * so we can cleanup the channel.
2412 */
2413 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2414 return 0;
2415 if ((downstream = c->mux_ctx) == NULL)
2416 return 0;
2417 switch (type) {
2418 case SSH2_MSG_CHANNEL_CLOSE:
2419 case SSH2_MSG_CHANNEL_DATA:
2420 case SSH2_MSG_CHANNEL_EOF:
2421 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2422 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2423 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2424 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2425 case SSH2_MSG_CHANNEL_SUCCESS:
2426 case SSH2_MSG_CHANNEL_FAILURE:
2427 case SSH2_MSG_CHANNEL_REQUEST:
2428 break;
2429 default:
2430 debug2("%s: channel %u: unsupported type %u", __func__,
2431 c->self, type);
2432 return 0;
2433 }
2434 if ((b = sshbuf_new()) == NULL) {
2435 error("%s: alloc reply", __func__);
2436 goto out;
2437 }
2438 /* get remaining payload (after id) */
2439 cp = sshpkt_ptr(ssh, &len);
2440 if (cp == NULL) {
2441 error("%s: no packet", __func__);
2442 goto out;
2443 }
2444 /* translate id and send to muxclient */
2445 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2446 (r = sshbuf_put_u8(b, type)) != 0 ||
2447 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2448 (r = sshbuf_put(b, cp, len)) != 0 ||
2449 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2450 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2451 goto out;
2452 }
2453 /* sshbuf_dump(b, stderr); */
2454 if (ssh_packet_log_type(type))
2455 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2456 type);
2457 out:
2458 /* update state */
2459 switch (type) {
2460 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2461 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2462 if (cp && len > 4)
2463 c->remote_id = PEEK_U32(cp);
2464 break;
2465 case SSH2_MSG_CHANNEL_CLOSE:
2466 if (c->flags & CHAN_CLOSE_SENT)
2467 channel_free(c);
2468 else
2469 c->flags |= CHAN_CLOSE_RCVD;
2470 break;
2471 }
2472 sshbuf_free(b);
2473 return 1;
2474}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002475
2476/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002477
2478/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002479int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002480channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002481{
Damien Miller34132e52000-01-14 15:45:46 +11002482 int id;
Damien Miller633de332014-05-15 13:48:26 +10002483 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002484 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002485 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002486
Damien Miller95def091999-11-25 00:26:21 +11002487 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002488 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002489 c = channel_lookup(id);
2490 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002491 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002492 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002493 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002494
Damien Miller95def091999-11-25 00:26:21 +11002495 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002496 if (c->type != SSH_CHANNEL_OPEN &&
2497 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002498 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002499
Damien Miller95def091999-11-25 00:26:21 +11002500 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002501 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002502 win_len = data_len;
2503 if (c->datagram)
2504 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002505
Damien Millera04ad492004-01-21 11:02:09 +11002506 /*
2507 * Ignore data for protocol > 1.3 if output end is no longer open.
2508 * For protocol 2 the sending side is reducing its window as it sends
2509 * data, so we must 'fake' consumption of the data in order to ensure
2510 * that window updates are sent back. Otherwise the connection might
2511 * deadlock.
2512 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002513 if (c->ostate != CHAN_OUTPUT_OPEN) {
2514 c->local_window -= win_len;
2515 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002516 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002517 }
2518
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002519 if (win_len > c->local_maxpacket) {
2520 logit("channel %d: rcvd big packet %d, maxpack %d",
2521 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002522 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002523 if (win_len > c->local_window) {
2524 logit("channel %d: rcvd too much data %d, win %d",
2525 c->self, win_len, c->local_window);
2526 return 0;
2527 }
2528 c->local_window -= win_len;
2529
Damien Millerd27b9472005-12-13 19:29:02 +11002530 if (c->datagram)
2531 buffer_put_string(&c->output, data, data_len);
2532 else
2533 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002534 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002535 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002536}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002537
Damien Millerd79b4242006-03-31 23:11:44 +11002538/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002539int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002540channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002541{
2542 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002543 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002544 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002545 Channel *c;
2546
2547 /* Get the channel number and verify it. */
2548 id = packet_get_int();
2549 c = channel_lookup(id);
2550
2551 if (c == NULL)
2552 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002553 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002554 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002555 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002556 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002557 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002558 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002559 if (c->flags & CHAN_EOF_RCVD) {
2560 if (datafellows & SSH_BUG_EXTEOF)
2561 debug("channel %d: accepting ext data after eof", id);
2562 else
2563 packet_disconnect("Received extended_data after EOF "
2564 "on channel %d.", id);
2565 }
Damien Miller33b13562000-04-04 14:38:59 +10002566 tcode = packet_get_int();
2567 if (c->efd == -1 ||
2568 c->extended_usage != CHAN_EXTENDED_WRITE ||
2569 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002570 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002571 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002572 }
2573 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002574 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002575 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002576 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002577 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002578 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002579 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002580 }
Damien Millerd3444942000-09-30 14:20:03 +11002581 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002582 c->local_window -= data_len;
2583 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002584 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002585 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002586}
2587
Damien Millerd79b4242006-03-31 23:11:44 +11002588/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002589int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002590channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10002591{
2592 int id;
2593 Channel *c;
2594
Damien Millerb38eff82000-04-01 11:09:21 +10002595 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002596 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002597 c = channel_lookup(id);
2598 if (c == NULL)
2599 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002600 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002601 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002602 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002603
2604 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002605 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002606 debug("channel %d: FORCE input drain", c->self);
2607 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002608 if (buffer_len(&c->input) == 0)
2609 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002610 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002611 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002612}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002613
Damien Millerd79b4242006-03-31 23:11:44 +11002614/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002615int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002616channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002617{
Damien Millerb38eff82000-04-01 11:09:21 +10002618 int id = packet_get_int();
2619 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002620
Damien Millerb38eff82000-04-01 11:09:21 +10002621 if (c == NULL)
2622 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002623 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002624 return 0;
2625 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002626 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002627 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002628}
2629
Damien Millerd79b4242006-03-31 23:11:44 +11002630/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002631int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002632channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002633{
Damien Millerb38eff82000-04-01 11:09:21 +10002634 int id, remote_id;
2635 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002636
Damien Millerb38eff82000-04-01 11:09:21 +10002637 id = packet_get_int();
2638 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002639
markus@openbsd.org8d057842016-09-30 09:19:13 +00002640 if (c==NULL)
2641 packet_disconnect("Received open confirmation for "
2642 "unknown channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002643 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002644 return 0;
2645 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002646 packet_disconnect("Received open confirmation for "
2647 "non-opening channel %d.", id);
2648 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002649 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002650 c->remote_id = remote_id;
2651 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002652
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002653 c->remote_window = packet_get_int();
2654 c->remote_maxpacket = packet_get_int();
2655 if (c->open_confirm) {
2656 debug2("callback start");
2657 c->open_confirm(c->self, 1, c->open_confirm_ctx);
2658 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002659 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002660 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2661 c->remote_window, c->remote_maxpacket);
Damien Miller48b03fc2002-01-22 23:11:40 +11002662 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002663 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002664}
2665
Ben Lindstrombba81212001-06-25 05:01:22 +00002666static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002667reason2txt(int reason)
2668{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002669 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002670 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2671 return "administratively prohibited";
2672 case SSH2_OPEN_CONNECT_FAILED:
2673 return "connect failed";
2674 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2675 return "unknown channel type";
2676 case SSH2_OPEN_RESOURCE_SHORTAGE:
2677 return "resource shortage";
2678 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002679 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002680}
2681
Damien Millerd79b4242006-03-31 23:11:44 +11002682/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002683int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002684channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002685{
Kevin Steves12057502001-02-05 14:54:34 +00002686 int id, reason;
2687 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002688 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002689
Damien Millerb38eff82000-04-01 11:09:21 +10002690 id = packet_get_int();
2691 c = channel_lookup(id);
2692
markus@openbsd.org8d057842016-09-30 09:19:13 +00002693 if (c==NULL)
2694 packet_disconnect("Received open failure for "
2695 "unknown channel %d.", id);
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002696 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002697 return 0;
2698 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002699 packet_disconnect("Received open failure for "
2700 "non-opening channel %d.", id);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002701 reason = packet_get_int();
2702 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2703 msg = packet_get_string(NULL);
2704 lang = packet_get_string(NULL);
2705 }
2706 logit("channel %d: open failed: %s%s%s", id,
2707 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2708 free(msg);
2709 free(lang);
2710 if (c->open_confirm) {
2711 debug2("callback start");
2712 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2713 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002714 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002715 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002716 /* Schedule the channel for cleanup/deletion. */
2717 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002718 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002719}
2720
Damien Millerd79b4242006-03-31 23:11:44 +11002721/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002722int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002723channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002724{
2725 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002726 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00002727 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10002728
Damien Miller33b13562000-04-04 14:38:59 +10002729 /* Get the channel number and verify it. */
2730 id = packet_get_int();
2731 c = channel_lookup(id);
2732
Damien Millerd47c62a2005-12-13 19:33:57 +11002733 if (c == NULL) {
2734 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002735 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002736 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002737 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002738 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002739 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002740 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002741 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00002742 if ((tmp = c->remote_window + adjust) < c->remote_window)
2743 fatal("channel %d: adjust %u overflows remote window %u",
2744 id, adjust, c->remote_window);
2745 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002746 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002747}
2748
Damien Millerd79b4242006-03-31 23:11:44 +11002749/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002750int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002751channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10002752{
2753 Channel *c;
2754 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002755 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002756
2757 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002758 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002759
Damien Miller16a73072008-12-08 09:55:25 +11002760 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11002761 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002762
Damien Miller16a73072008-12-08 09:55:25 +11002763 if ((c = channel_lookup(id)) == NULL) {
2764 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002765 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002766 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002767 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002768 return 0;
2769 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10002770 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002771 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002772 cc->cb(type, c, cc->ctx);
2773 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002774 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002775 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002776 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002777}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002778
Ben Lindstrome9c99912001-06-09 00:41:05 +00002779/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002780
Ben Lindstrom908afed2001-10-03 17:34:59 +00002781void
2782channel_set_af(int af)
2783{
2784 IPv4or6 = af;
2785}
2786
Damien Millerf6dff7c2011-09-22 21:38:52 +10002787
2788/*
2789 * Determine whether or not a port forward listens to loopback, the
2790 * specified address or wildcard. On the client, a specified bind
2791 * address will always override gateway_ports. On the server, a
2792 * gateway_ports of 1 (``yes'') will override the client's specification
2793 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2794 * will bind to whatever address the client asked for.
2795 *
2796 * Special-case listen_addrs are:
2797 *
2798 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2799 * "" (empty string), "*" -> wildcard v4/v6
2800 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002801 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002802 */
2803static const char *
2804channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002805 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002806{
2807 const char *addr = NULL;
2808 int wildcard = 0;
2809
2810 if (listen_addr == NULL) {
2811 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002812 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002813 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002814 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002815 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2816 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2817 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002818 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002819 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002820 /*
2821 * Notify client if they requested a specific listen
2822 * address and it was overridden.
2823 */
2824 if (*listen_addr != '\0' &&
2825 strcmp(listen_addr, "0.0.0.0") != 0 &&
2826 strcmp(listen_addr, "*") != 0) {
2827 packet_send_debug("Forwarding listen address "
2828 "\"%s\" overridden by server "
2829 "GatewayPorts", listen_addr);
2830 }
Damien Miller602943d2014-07-04 08:59:41 +10002831 } else if (strcmp(listen_addr, "localhost") != 0 ||
2832 strcmp(listen_addr, "127.0.0.1") == 0 ||
2833 strcmp(listen_addr, "::1") == 0) {
2834 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002835 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002836 }
2837 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2838 strcmp(listen_addr, "::1") == 0) {
2839 /*
2840 * If a specific IPv4/IPv6 localhost address has been
2841 * requested then accept it even if gateway_ports is in
2842 * effect. This allows the client to prefer IPv4 or IPv6.
2843 */
2844 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002845 }
2846 if (wildcardp != NULL)
2847 *wildcardp = wildcard;
2848 return addr;
2849}
2850
Damien Millerb16461c2002-01-22 23:29:22 +11002851static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002852channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2853 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002854{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002855 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002856 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002857 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002858 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002859 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002860 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002861
Damien Millerf91ee4c2005-03-01 21:24:33 +11002862 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002863
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00002864 if (is_client && fwd->connect_path != NULL) {
2865 host = fwd->connect_path;
2866 } else {
2867 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2868 fwd->listen_host : fwd->connect_host;
2869 if (host == NULL) {
2870 error("No forward host name.");
2871 return 0;
2872 }
2873 if (strlen(host) >= NI_MAXHOST) {
2874 error("Forward host name too long.");
2875 return 0;
2876 }
Kevin Steves12057502001-02-05 14:54:34 +00002877 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002878
Damien Millerf6dff7c2011-09-22 21:38:52 +10002879 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002880 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2881 is_client, fwd_opts);
2882 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002883 type, wildcard, (addr == NULL) ? "NULL" : addr);
2884
2885 /*
Damien Miller34132e52000-01-14 15:45:46 +11002886 * getaddrinfo returns a loopback address if the hostname is
2887 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002888 */
Damien Miller34132e52000-01-14 15:45:46 +11002889 memset(&hints, 0, sizeof(hints));
2890 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002891 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002892 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002893 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002894 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2895 if (addr == NULL) {
2896 /* This really shouldn't happen */
2897 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002898 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002899 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002900 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11002901 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002902 }
Damien Millera7270302005-07-06 09:36:05 +10002903 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002904 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002905 if (allocated_listen_port != NULL)
2906 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11002907 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11002908 switch (ai->ai_family) {
2909 case AF_INET:
2910 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2911 sin_port;
2912 break;
2913 case AF_INET6:
2914 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2915 sin6_port;
2916 break;
2917 default:
Damien Miller34132e52000-01-14 15:45:46 +11002918 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11002919 }
2920 /*
2921 * If allocating a port for -R forwards, then use the
2922 * same port for all address families.
2923 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002924 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002925 allocated_listen_port != NULL && *allocated_listen_port > 0)
2926 *lport_p = htons(*allocated_listen_port);
2927
Damien Miller34132e52000-01-14 15:45:46 +11002928 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2929 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10002930 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11002931 continue;
2932 }
2933 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11002934 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002935 if (sock < 0) {
2936 /* this is no error since kernel may not support ipv6 */
2937 verbose("socket: %.100s", strerror(errno));
2938 continue;
2939 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002940
2941 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11002942 if (ai->ai_family == AF_INET6)
2943 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002944
Damien Miller4bf648f2009-02-14 16:28:21 +11002945 debug("Local forwarding listening on %s port %s.",
2946 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002947
Damien Miller34132e52000-01-14 15:45:46 +11002948 /* Bind the socket to the address. */
2949 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2950 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002951 if (!ai->ai_next)
2952 error("bind: %.100s", strerror(errno));
2953 else
2954 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002955
Damien Miller34132e52000-01-14 15:45:46 +11002956 close(sock);
2957 continue;
2958 }
2959 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002960 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002961 error("listen: %.100s", strerror(errno));
2962 close(sock);
2963 continue;
2964 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002965
2966 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10002967 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11002968 * record what we got.
2969 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002970 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002971 allocated_listen_port != NULL &&
2972 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00002973 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11002974 debug("Allocated listen port %d",
2975 *allocated_listen_port);
2976 }
2977
Damien Miller34132e52000-01-14 15:45:46 +11002978 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002979 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002980 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002981 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11002982 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10002983 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002984 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10002985 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11002986 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
2987 c->listening_port = *allocated_listen_port;
2988 else
Damien Miller7acefbb2014-07-18 14:11:24 +10002989 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002990 success = 1;
2991 }
2992 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10002993 error("%s: cannot listen to port: %d", __func__,
2994 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002995 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002996 return success;
Damien Miller95def091999-11-25 00:26:21 +11002997}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002998
Damien Miller7acefbb2014-07-18 14:11:24 +10002999static int
3000channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3001 struct ForwardOptions *fwd_opts)
3002{
3003 struct sockaddr_un sunaddr;
3004 const char *path;
3005 Channel *c;
3006 int port, sock;
3007 mode_t omask;
3008
3009 switch (type) {
3010 case SSH_CHANNEL_UNIX_LISTENER:
3011 if (fwd->connect_path != NULL) {
3012 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3013 error("Local connecting path too long: %s",
3014 fwd->connect_path);
3015 return 0;
3016 }
3017 path = fwd->connect_path;
3018 port = PORT_STREAMLOCAL;
3019 } else {
3020 if (fwd->connect_host == NULL) {
3021 error("No forward host name.");
3022 return 0;
3023 }
3024 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3025 error("Forward host name too long.");
3026 return 0;
3027 }
3028 path = fwd->connect_host;
3029 port = fwd->connect_port;
3030 }
3031 break;
3032 case SSH_CHANNEL_RUNIX_LISTENER:
3033 path = fwd->listen_path;
3034 port = PORT_STREAMLOCAL;
3035 break;
3036 default:
3037 error("%s: unexpected channel type %d", __func__, type);
3038 return 0;
3039 }
3040
3041 if (fwd->listen_path == NULL) {
3042 error("No forward path name.");
3043 return 0;
3044 }
3045 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3046 error("Local listening path too long: %s", fwd->listen_path);
3047 return 0;
3048 }
3049
3050 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3051
3052 /* Start a Unix domain listener. */
3053 omask = umask(fwd_opts->streamlocal_bind_mask);
3054 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3055 fwd_opts->streamlocal_bind_unlink);
3056 umask(omask);
3057 if (sock < 0)
3058 return 0;
3059
3060 debug("Local forwarding listening on path %s.", fwd->listen_path);
3061
3062 /* Allocate a channel number for the socket. */
3063 c = channel_new("unix listener", type, sock, sock, -1,
3064 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3065 0, "unix listener", 1);
3066 c->path = xstrdup(path);
3067 c->host_port = port;
3068 c->listening_port = PORT_STREAMLOCAL;
3069 c->listening_addr = xstrdup(fwd->listen_path);
3070 return 1;
3071}
3072
3073static int
3074channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003075{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003076 u_int i;
3077 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003078
Darren Tucker47eede72005-03-14 23:08:12 +11003079 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003080 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003081 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3082 continue;
3083 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3084 debug2("%s: close channel %d", __func__, i);
3085 channel_free(c);
3086 found = 1;
3087 }
3088 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003089
Damien Millerf6dff7c2011-09-22 21:38:52 +10003090 return (found);
3091}
3092
Damien Miller7acefbb2014-07-18 14:11:24 +10003093static int
3094channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003095{
3096 u_int i;
3097 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003098
3099 for (i = 0; i < channels_alloc; i++) {
3100 Channel *c = channels[i];
3101 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3102 continue;
3103 if (c->path == NULL)
3104 continue;
3105 if (strcmp(c->path, path) == 0) {
3106 debug2("%s: close channel %d", __func__, i);
3107 channel_free(c);
3108 found = 1;
3109 }
3110 }
3111
3112 return (found);
3113}
3114
3115int
3116channel_cancel_rport_listener(struct Forward *fwd)
3117{
3118 if (fwd->listen_path != NULL)
3119 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3120 else
3121 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3122}
3123
3124static int
3125channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3126 int cport, struct ForwardOptions *fwd_opts)
3127{
3128 u_int i;
3129 int found = 0;
3130 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003131
3132 for (i = 0; i < channels_alloc; i++) {
3133 Channel *c = channels[i];
3134 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3135 continue;
Damien Millerff773642011-09-22 21:39:48 +10003136 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003137 continue;
Damien Millerff773642011-09-22 21:39:48 +10003138 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3139 /* skip dynamic forwardings */
3140 if (c->host_port == 0)
3141 continue;
3142 } else {
3143 if (c->host_port != cport)
3144 continue;
3145 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003146 if ((c->listening_addr == NULL && addr != NULL) ||
3147 (c->listening_addr != NULL && addr == NULL))
3148 continue;
3149 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003150 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003151 channel_free(c);
3152 found = 1;
3153 }
3154 }
3155
3156 return (found);
3157}
3158
Damien Miller7acefbb2014-07-18 14:11:24 +10003159static int
3160channel_cancel_lport_listener_streamlocal(const char *path)
3161{
3162 u_int i;
3163 int found = 0;
3164
3165 if (path == NULL) {
3166 error("%s: no path specified.", __func__);
3167 return 0;
3168 }
3169
3170 for (i = 0; i < channels_alloc; i++) {
3171 Channel *c = channels[i];
3172 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3173 continue;
3174 if (c->listening_addr == NULL)
3175 continue;
3176 if (strcmp(c->listening_addr, path) == 0) {
3177 debug2("%s: close channel %d", __func__, i);
3178 channel_free(c);
3179 found = 1;
3180 }
3181 }
3182
3183 return (found);
3184}
3185
3186int
3187channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3188{
3189 if (fwd->listen_path != NULL)
3190 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3191 else
3192 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3193}
3194
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003195/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003196int
Damien Miller7acefbb2014-07-18 14:11:24 +10003197channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003198{
Damien Miller7acefbb2014-07-18 14:11:24 +10003199 if (fwd->listen_path != NULL) {
3200 return channel_setup_fwd_listener_streamlocal(
3201 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3202 } else {
3203 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3204 fwd, NULL, fwd_opts);
3205 }
Damien Millerb16461c2002-01-22 23:29:22 +11003206}
3207
3208/* protocol v2 remote port fwd, used by sshd */
3209int
Damien Miller7acefbb2014-07-18 14:11:24 +10003210channel_setup_remote_fwd_listener(struct Forward *fwd,
3211 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003212{
Damien Miller7acefbb2014-07-18 14:11:24 +10003213 if (fwd->listen_path != NULL) {
3214 return channel_setup_fwd_listener_streamlocal(
3215 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3216 } else {
3217 return channel_setup_fwd_listener_tcpip(
3218 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3219 fwd_opts);
3220 }
Damien Millerb16461c2002-01-22 23:29:22 +11003221}
3222
Damien Miller5428f641999-11-25 11:54:57 +11003223/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003224 * Translate the requested rfwd listen host to something usable for
3225 * this server.
3226 */
3227static const char *
3228channel_rfwd_bind_host(const char *listen_host)
3229{
3230 if (listen_host == NULL) {
3231 if (datafellows & SSH_BUG_RFWD_ADDR)
3232 return "127.0.0.1";
3233 else
3234 return "localhost";
3235 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3236 if (datafellows & SSH_BUG_RFWD_ADDR)
3237 return "0.0.0.0";
3238 else
3239 return "";
3240 } else
3241 return listen_host;
3242}
3243
3244/*
Damien Miller5428f641999-11-25 11:54:57 +11003245 * Initiate forwarding of connections to port "port" on remote host through
3246 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003247 * Returns handle (index) for updating the dynamic listen port with
3248 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003249 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003250int
Damien Miller7acefbb2014-07-18 14:11:24 +10003251channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003252{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003253 int success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003254
Damien Miller95def091999-11-25 00:26:21 +11003255 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003256 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3257 if (fwd->listen_path != NULL) {
3258 packet_put_cstring("streamlocal-forward@openssh.com");
3259 packet_put_char(1); /* boolean: want reply */
3260 packet_put_cstring(fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003261 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003262 packet_put_cstring("tcpip-forward");
3263 packet_put_char(1); /* boolean: want reply */
3264 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3265 packet_put_int(fwd->listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11003266 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003267 packet_send();
3268 packet_write_wait();
3269 /* Assume that server accepts the request */
3270 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003271 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003272 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003273 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003274 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003275 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003276 if (fwd->connect_path != NULL) {
3277 permitted_opens[idx].host_to_connect =
3278 xstrdup(fwd->connect_path);
3279 permitted_opens[idx].port_to_connect =
3280 PORT_STREAMLOCAL;
3281 } else {
3282 permitted_opens[idx].host_to_connect =
3283 xstrdup(fwd->connect_host);
3284 permitted_opens[idx].port_to_connect =
3285 fwd->connect_port;
3286 }
3287 if (fwd->listen_path != NULL) {
3288 permitted_opens[idx].listen_host = NULL;
3289 permitted_opens[idx].listen_path =
3290 xstrdup(fwd->listen_path);
3291 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3292 } else {
3293 permitted_opens[idx].listen_host =
3294 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3295 permitted_opens[idx].listen_path = NULL;
3296 permitted_opens[idx].listen_port = fwd->listen_port;
3297 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003298 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003299 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003300 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003301}
3302
Damien Miller4b3ed642014-07-02 15:29:40 +10003303static int
3304open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003305 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003306{
3307 if (allowed_open->host_to_connect == NULL)
3308 return 0;
3309 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3310 allowed_open->port_to_connect != requestedport)
3311 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003312 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3313 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003314 return 0;
3315 return 1;
3316}
3317
3318/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003319 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003320 * we don't support FWD_PERMIT_ANY_PORT and
3321 * need to translate between the configured-host (listen_host)
3322 * and what we've sent to the remote server (channel_rfwd_bind_host)
3323 */
3324static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003325open_listen_match_tcpip(ForwardPermission *allowed_open,
3326 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003327{
3328 const char *allowed_host;
3329
3330 if (allowed_open->host_to_connect == NULL)
3331 return 0;
3332 if (allowed_open->listen_port != requestedport)
3333 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003334 if (!translate && allowed_open->listen_host == NULL &&
3335 requestedhost == NULL)
3336 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003337 allowed_host = translate ?
3338 channel_rfwd_bind_host(allowed_open->listen_host) :
3339 allowed_open->listen_host;
3340 if (allowed_host == NULL ||
3341 strcmp(allowed_host, requestedhost) != 0)
3342 return 0;
3343 return 1;
3344}
3345
Damien Miller7acefbb2014-07-18 14:11:24 +10003346static int
3347open_listen_match_streamlocal(ForwardPermission *allowed_open,
3348 const char *requestedpath)
3349{
3350 if (allowed_open->host_to_connect == NULL)
3351 return 0;
3352 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3353 return 0;
3354 if (allowed_open->listen_path == NULL ||
3355 strcmp(allowed_open->listen_path, requestedpath) != 0)
3356 return 0;
3357 return 1;
3358}
3359
Damien Miller5428f641999-11-25 11:54:57 +11003360/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003361 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003362 * local side.
3363 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003364static int
3365channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003366{
3367 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003368
Darren Tuckere7066df2004-05-24 10:18:05 +10003369 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003370 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003371 break;
3372 }
3373 if (i >= num_permitted_opens) {
3374 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003375 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003376 }
3377 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3378 packet_put_cstring("cancel-tcpip-forward");
3379 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003380 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003381 packet_put_int(port);
3382 packet_send();
3383
Damien Miller4b3ed642014-07-02 15:29:40 +10003384 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003385 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003386 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003387 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003388 free(permitted_opens[i].listen_host);
3389 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003390 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003391 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003392
3393 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003394}
3395
3396/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003397 * Request cancellation of remote forwarding of Unix domain socket
3398 * path from local side.
3399 */
3400static int
3401channel_request_rforward_cancel_streamlocal(const char *path)
3402{
3403 int i;
3404
Damien Miller7acefbb2014-07-18 14:11:24 +10003405 for (i = 0; i < num_permitted_opens; i++) {
3406 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3407 break;
3408 }
3409 if (i >= num_permitted_opens) {
3410 debug("%s: requested forward not found", __func__);
3411 return -1;
3412 }
3413 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3414 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3415 packet_put_char(0);
3416 packet_put_cstring(path);
3417 packet_send();
3418
3419 permitted_opens[i].listen_port = 0;
3420 permitted_opens[i].port_to_connect = 0;
3421 free(permitted_opens[i].host_to_connect);
3422 permitted_opens[i].host_to_connect = NULL;
3423 permitted_opens[i].listen_host = NULL;
3424 free(permitted_opens[i].listen_path);
3425 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003426 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003427
3428 return 0;
3429}
3430
3431/*
3432 * Request cancellation of remote forwarding of a connection from local side.
3433 */
3434int
3435channel_request_rforward_cancel(struct Forward *fwd)
3436{
3437 if (fwd->listen_path != NULL) {
3438 return (channel_request_rforward_cancel_streamlocal(
3439 fwd->listen_path));
3440 } else {
3441 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3442 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3443 }
3444}
3445
3446/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003447 * Permits opening to any host/port if permitted_opens[] is empty. This is
3448 * usually called by the server, because the user could connect to any port
3449 * anyway, and the server has no way to know but to trust the client anyway.
3450 */
3451void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003452channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003453{
3454 if (num_permitted_opens == 0)
3455 all_opens_permitted = 1;
3456}
3457
Ben Lindstroma3700052001-04-05 23:26:32 +00003458void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003459channel_add_permitted_opens(char *host, int port)
3460{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003461 debug("allow port forwarding to host %s port %d", host, port);
3462
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003463 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003464 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003465 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3466 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003467 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003468 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003469 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003470 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003471 num_permitted_opens++;
3472
3473 all_opens_permitted = 0;
3474}
3475
Darren Tucker68afb8c2011-10-02 18:59:03 +11003476/*
3477 * Update the listen port for a dynamic remote forward, after
3478 * the actual 'newport' has been allocated. If 'newport' < 0 is
3479 * passed then they entry will be invalidated.
3480 */
3481void
3482channel_update_permitted_opens(int idx, int newport)
3483{
3484 if (idx < 0 || idx >= num_permitted_opens) {
3485 debug("channel_update_permitted_opens: index out of range:"
3486 " %d num_permitted_opens %d", idx, num_permitted_opens);
3487 return;
3488 }
3489 debug("%s allowed port %d for forwarding to host %s port %d",
3490 newport > 0 ? "Updating" : "Removing",
3491 newport,
3492 permitted_opens[idx].host_to_connect,
3493 permitted_opens[idx].port_to_connect);
3494 if (newport >= 0) {
3495 permitted_opens[idx].listen_port =
3496 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3497 } else {
3498 permitted_opens[idx].listen_port = 0;
3499 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003500 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003501 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003502 free(permitted_opens[idx].listen_host);
3503 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003504 free(permitted_opens[idx].listen_path);
3505 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003506 }
3507}
3508
Damien Millera765cf42006-07-24 14:08:13 +10003509int
Damien Miller9b439df2006-07-24 14:04:00 +10003510channel_add_adm_permitted_opens(char *host, int port)
3511{
Damien Millera765cf42006-07-24 14:08:13 +10003512 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003513
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003514 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003515 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003516 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3517 = xstrdup(host);
3518 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003519 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003520 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003521 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003522 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003523}
3524
3525void
Damien Millerc6081482012-04-22 11:18:53 +10003526channel_disable_adm_local_opens(void)
3527{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003528 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003529 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003530 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3531 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003532}
3533
3534void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003535channel_clear_permitted_opens(void)
3536{
3537 int i;
3538
Damien Miller4b3ed642014-07-02 15:29:40 +10003539 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003540 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003541 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003542 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003543 }
Darren Tuckera627d422013-06-02 07:31:17 +10003544 free(permitted_opens);
3545 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003546 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003547}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003548
Damien Miller9b439df2006-07-24 14:04:00 +10003549void
3550channel_clear_adm_permitted_opens(void)
3551{
3552 int i;
3553
Damien Miller4b3ed642014-07-02 15:29:40 +10003554 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003555 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003556 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003557 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003558 }
Darren Tuckera627d422013-06-02 07:31:17 +10003559 free(permitted_adm_opens);
3560 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003561 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003562}
3563
Darren Tuckere7140f22008-06-10 23:01:51 +10003564void
3565channel_print_adm_permitted_opens(void)
3566{
Damien Miller6ef17492008-07-16 22:42:06 +10003567 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003568
Damien Millerb53d8a12009-01-28 16:13:04 +11003569 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003570 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003571 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003572 return;
3573 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003574 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003575 if (permitted_adm_opens[i].host_to_connect == NULL)
3576 printf(" none");
3577 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003578 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3579 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003580 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003581}
3582
Darren Tucker1338b9e2011-10-02 18:57:35 +11003583/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3584int
3585permitopen_port(const char *p)
3586{
3587 int port;
3588
3589 if (strcmp(p, "*") == 0)
3590 return FWD_PERMIT_ANY_PORT;
3591 if ((port = a2port(p)) > 0)
3592 return port;
3593 return -1;
3594}
3595
Damien Millerbd740252008-05-19 15:37:09 +10003596/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003597static int
Damien Millerbd740252008-05-19 15:37:09 +10003598connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003599{
Damien Millerbd740252008-05-19 15:37:09 +10003600 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003601 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00003602 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003603
Damien Millerbd740252008-05-19 15:37:09 +10003604 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003605 switch (cctx->ai->ai_family) {
3606 case AF_UNIX:
3607 /* unix:pathname instead of host:port */
3608 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3609 strlcpy(ntop, "unix", sizeof(ntop));
3610 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3611 break;
3612 case AF_INET:
3613 case AF_INET6:
3614 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3615 ntop, sizeof(ntop), strport, sizeof(strport),
3616 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3617 error("connect_next: getnameinfo failed");
3618 continue;
3619 }
3620 break;
3621 default:
Damien Miller34132e52000-01-14 15:45:46 +11003622 continue;
3623 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003624 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3625 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003626 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003627 error("socket: %.100s", strerror(errno));
3628 else
3629 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003630 continue;
3631 }
Damien Miller232711f2004-06-15 10:35:30 +10003632 if (set_nonblock(sock) == -1)
3633 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003634 if (connect(sock, cctx->ai->ai_addr,
3635 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3636 debug("connect_next: host %.100s ([%.100s]:%s): "
3637 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003638 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003639 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003640 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003641 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003642 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003643 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003644 if (cctx->ai->ai_family != AF_UNIX)
3645 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003646 debug("connect_next: host %.100s ([%.100s]:%s) "
3647 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3648 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003649 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003650 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003651 return -1;
3652}
Damien Millerb38eff82000-04-01 11:09:21 +10003653
Damien Millerbd740252008-05-19 15:37:09 +10003654static void
3655channel_connect_ctx_free(struct channel_connect *cctx)
3656{
Darren Tuckera627d422013-06-02 07:31:17 +10003657 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003658 if (cctx->aitop) {
3659 if (cctx->aitop->ai_family == AF_UNIX)
3660 free(cctx->aitop);
3661 else
3662 freeaddrinfo(cctx->aitop);
3663 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003664 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003665}
3666
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003667/*
3668 * Return CONNECTING channel to remote host:port or local socket path,
3669 * passing back the failure reason if appropriate.
3670 */
Damien Millerbd740252008-05-19 15:37:09 +10003671static Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003672connect_to_reason(const char *name, int port, char *ctype, char *rname,
3673 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003674{
3675 struct addrinfo hints;
3676 int gaierr;
3677 int sock = -1;
3678 char strport[NI_MAXSERV];
3679 struct channel_connect cctx;
3680 Channel *c;
3681
Damien Miller2bcb8662008-07-12 17:12:29 +10003682 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003683
3684 if (port == PORT_STREAMLOCAL) {
3685 struct sockaddr_un *sunaddr;
3686 struct addrinfo *ai;
3687
3688 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3689 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3690 return (NULL);
3691 }
3692
3693 /*
3694 * Fake up a struct addrinfo for AF_UNIX connections.
3695 * channel_connect_ctx_free() must check ai_family
3696 * and use free() not freeaddirinfo() for AF_UNIX.
3697 */
3698 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3699 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3700 ai->ai_addr = (struct sockaddr *)(ai + 1);
3701 ai->ai_addrlen = sizeof(*sunaddr);
3702 ai->ai_family = AF_UNIX;
3703 ai->ai_socktype = SOCK_STREAM;
3704 ai->ai_protocol = PF_UNSPEC;
3705 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3706 sunaddr->sun_family = AF_UNIX;
3707 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3708 cctx.aitop = ai;
3709 } else {
3710 memset(&hints, 0, sizeof(hints));
3711 hints.ai_family = IPv4or6;
3712 hints.ai_socktype = SOCK_STREAM;
3713 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003714 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3715 != 0) {
3716 if (errmsg != NULL)
3717 *errmsg = ssh_gai_strerror(gaierr);
3718 if (reason != NULL)
3719 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003720 error("connect_to %.100s: unknown host (%s)", name,
3721 ssh_gai_strerror(gaierr));
3722 return NULL;
3723 }
Damien Millerbd740252008-05-19 15:37:09 +10003724 }
3725
Damien Miller7acefbb2014-07-18 14:11:24 +10003726 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003727 cctx.port = port;
3728 cctx.ai = cctx.aitop;
3729
3730 if ((sock = connect_next(&cctx)) == -1) {
3731 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003732 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003733 channel_connect_ctx_free(&cctx);
3734 return NULL;
3735 }
3736 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3737 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3738 c->connect_ctx = cctx;
3739 return c;
3740}
3741
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003742/* Return CONNECTING channel to remote host:port or local socket path */
3743static Channel *
3744connect_to(const char *name, int port, char *ctype, char *rname)
3745{
3746 return connect_to_reason(name, port, ctype, rname, NULL, NULL);
3747}
3748
markus@openbsd.org8d057842016-09-30 09:19:13 +00003749/*
3750 * returns either the newly connected channel or the downstream channel
3751 * that needs to deal with this connection.
3752 */
Damien Millerbd740252008-05-19 15:37:09 +10003753Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003754channel_connect_by_listen_address(const char *listen_host,
3755 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003756{
3757 int i;
3758
3759 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003760 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003761 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00003762 if (permitted_opens[i].downstream)
3763 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10003764 return connect_to(
3765 permitted_opens[i].host_to_connect,
3766 permitted_opens[i].port_to_connect, ctype, rname);
3767 }
3768 }
3769 error("WARNING: Server requests forwarding for unknown listen_port %d",
3770 listen_port);
3771 return NULL;
3772}
3773
Damien Miller7acefbb2014-07-18 14:11:24 +10003774Channel *
3775channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3776{
3777 int i;
3778
3779 for (i = 0; i < num_permitted_opens; i++) {
3780 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3781 return connect_to(
3782 permitted_opens[i].host_to_connect,
3783 permitted_opens[i].port_to_connect, ctype, rname);
3784 }
3785 }
3786 error("WARNING: Server requests forwarding for unknown path %.100s",
3787 path);
3788 return NULL;
3789}
3790
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003791/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003792Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003793channel_connect_to_port(const char *host, u_short port, char *ctype,
3794 char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003795{
Damien Miller9b439df2006-07-24 14:04:00 +10003796 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003797
3798 permit = all_opens_permitted;
3799 if (!permit) {
3800 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003801 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003802 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003803 break;
3804 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003805 }
Damien Miller9b439df2006-07-24 14:04:00 +10003806
3807 if (num_adm_permitted_opens > 0) {
3808 permit_adm = 0;
3809 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003810 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003811 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003812 break;
3813 }
Damien Miller9b439df2006-07-24 14:04:00 +10003814 }
3815
3816 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003817 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003818 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003819 if (reason != NULL)
3820 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10003821 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003822 }
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003823 return connect_to_reason(host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003824}
3825
Damien Miller7acefbb2014-07-18 14:11:24 +10003826/* Check if connecting to that path is permitted and connect. */
3827Channel *
3828channel_connect_to_path(const char *path, char *ctype, char *rname)
3829{
3830 int i, permit, permit_adm = 1;
3831
3832 permit = all_opens_permitted;
3833 if (!permit) {
3834 for (i = 0; i < num_permitted_opens; i++)
3835 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3836 permit = 1;
3837 break;
3838 }
3839 }
3840
3841 if (num_adm_permitted_opens > 0) {
3842 permit_adm = 0;
3843 for (i = 0; i < num_adm_permitted_opens; i++)
3844 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3845 permit_adm = 1;
3846 break;
3847 }
3848 }
3849
3850 if (!permit || !permit_adm) {
3851 logit("Received request to connect to path %.100s, "
3852 "but the request was denied.", path);
3853 return NULL;
3854 }
3855 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3856}
3857
Damien Miller0e220db2004-06-15 10:34:08 +10003858void
3859channel_send_window_changes(void)
3860{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003861 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003862 struct winsize ws;
3863
3864 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003865 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003866 channels[i]->type != SSH_CHANNEL_OPEN)
3867 continue;
3868 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3869 continue;
3870 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003871 packet_put_int((u_int)ws.ws_col);
3872 packet_put_int((u_int)ws.ws_row);
3873 packet_put_int((u_int)ws.ws_xpixel);
3874 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003875 packet_send();
3876 }
3877}
3878
Ben Lindstrome9c99912001-06-09 00:41:05 +00003879/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003880
Damien Miller5428f641999-11-25 11:54:57 +11003881/*
3882 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003883 * Returns 0 and a suitable display number for the DISPLAY variable
3884 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003885 */
Kevin Steves366298c2001-12-19 17:58:01 +00003886int
Damien Miller95c249f2002-02-05 12:11:34 +11003887x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003888 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003889{
Damien Millere7378562001-12-21 14:58:35 +11003890 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003891 int display_number, sock;
3892 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003893 struct addrinfo hints, *ai, *aitop;
3894 char strport[NI_MAXSERV];
3895 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003896
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003897 if (chanids == NULL)
3898 return -1;
3899
Damien Millera34a28b1999-12-14 10:47:15 +11003900 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11003901 display_number < MAX_DISPLAYS;
3902 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11003903 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11003904 memset(&hints, 0, sizeof(hints));
3905 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11003906 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11003907 hints.ai_socktype = SOCK_STREAM;
3908 snprintf(strport, sizeof strport, "%d", port);
3909 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003910 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00003911 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003912 }
Damien Miller34132e52000-01-14 15:45:46 +11003913 for (ai = aitop; ai; ai = ai->ai_next) {
3914 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3915 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11003916 sock = socket(ai->ai_family, ai->ai_socktype,
3917 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003918 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11003919 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
3920#ifdef EPFNOSUPPORT
3921 && (errno != EPFNOSUPPORT)
3922#endif
3923 ) {
Damien Millere2192732000-01-17 13:22:55 +11003924 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10003925 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00003926 return -1;
Damien Millere2192732000-01-17 13:22:55 +11003927 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11003928 debug("x11_create_display_inet: Socket family %d not supported",
3929 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11003930 continue;
3931 }
Damien Miller34132e52000-01-14 15:45:46 +11003932 }
Damien Miller04ee0f82009-11-18 17:48:30 +11003933 if (ai->ai_family == AF_INET6)
3934 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10003935 if (x11_use_localhost)
3936 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11003937 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003938 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003939 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11003940
Damien Miller34132e52000-01-14 15:45:46 +11003941 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11003942 close(socks[n]);
3943 }
3944 num_socks = 0;
3945 break;
3946 }
3947 socks[num_socks++] = sock;
3948 if (num_socks == NUM_SOCKS)
3949 break;
Damien Miller95def091999-11-25 00:26:21 +11003950 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00003951 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11003952 if (num_socks > 0)
3953 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003954 }
Damien Miller95def091999-11-25 00:26:21 +11003955 if (display_number >= MAX_DISPLAYS) {
3956 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00003957 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003958 }
Damien Miller95def091999-11-25 00:26:21 +11003959 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11003960 for (n = 0; n < num_socks; n++) {
3961 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11003962 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003963 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003964 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00003965 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11003966 }
Damien Miller95def091999-11-25 00:26:21 +11003967 }
Damien Miller34132e52000-01-14 15:45:46 +11003968
Damien Miller34132e52000-01-14 15:45:46 +11003969 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11003970 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11003971 for (n = 0; n < num_socks; n++) {
3972 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11003973 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10003974 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3975 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003976 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11003977 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003978 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11003979 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003980 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003981
Kevin Steves366298c2001-12-19 17:58:01 +00003982 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003983 *display_numberp = display_number;
3984 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003985}
3986
Ben Lindstrombba81212001-06-25 05:01:22 +00003987static int
Damien Miller819dbb62009-01-21 16:46:26 +11003988connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003989{
Damien Miller95def091999-11-25 00:26:21 +11003990 int sock;
3991 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003992
Damien Miller3afe3752001-12-21 12:39:51 +11003993 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3994 if (sock < 0)
3995 error("socket: %.100s", strerror(errno));
3996 memset(&addr, 0, sizeof(addr));
3997 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11003998 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11003999 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004000 return sock;
4001 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004002 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4003 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004004}
4005
Damien Miller819dbb62009-01-21 16:46:26 +11004006static int
4007connect_local_xsocket(u_int dnr)
4008{
4009 char buf[1024];
4010 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4011 return connect_local_xsocket_path(buf);
4012}
4013
Darren Tuckerda39b092017-03-10 13:22:32 +11004014#ifdef __APPLE__
4015static int
4016is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4017{
4018 struct stat sbuf;
4019
4020 if (strlcpy(path, display, pathlen) >= pathlen) {
4021 error("%s: display path too long", __func__);
4022 return 0;
4023 }
4024 if (display[0] != '/')
4025 return 0;
4026 if (stat(path, &sbuf) == 0) {
4027 return 1;
4028 } else {
4029 char *dot = strrchr(path, '.');
4030 if (dot != NULL) {
4031 *dot = '\0';
4032 if (stat(path, &sbuf) == 0) {
4033 return 1;
4034 }
4035 }
4036 }
4037 return 0;
4038}
4039#endif
4040
Damien Millerbd483e72000-04-30 10:00:53 +10004041int
4042x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004043{
Damien Miller57c4e872006-03-31 23:11:07 +11004044 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004045 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004046 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004047 struct addrinfo hints, *ai, *aitop;
4048 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004049 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004050
Damien Miller95def091999-11-25 00:26:21 +11004051 /* Try to open a socket for the local X server. */
4052 display = getenv("DISPLAY");
4053 if (!display) {
4054 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004055 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004056 }
Damien Miller5428f641999-11-25 11:54:57 +11004057 /*
4058 * Now we decode the value of the DISPLAY variable and make a
4059 * connection to the real X server.
4060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004061
Damien Miller819dbb62009-01-21 16:46:26 +11004062#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004063 /* Check if display is a path to a socket (as set by launchd). */
4064 {
4065 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004066
Darren Tuckerda39b092017-03-10 13:22:32 +11004067 if (is_path_to_xsocket(display, path, sizeof(path))) {
4068 debug("x11_connect_display: $DISPLAY is launchd");
4069
4070 /* Create a socket. */
4071 sock = connect_local_xsocket_path(path);
4072 if (sock < 0)
4073 return -1;
4074
4075 /* OK, we now have a connection to the display. */
4076 return sock;
4077 }
Damien Miller819dbb62009-01-21 16:46:26 +11004078 }
4079#endif
Damien Miller5428f641999-11-25 11:54:57 +11004080 /*
4081 * Check if it is a unix domain socket. Unix domain displays are in
4082 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4083 */
Damien Miller95def091999-11-25 00:26:21 +11004084 if (strncmp(display, "unix:", 5) == 0 ||
4085 display[0] == ':') {
4086 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004087 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004088 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004089 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004090 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004091 }
4092 /* Create a socket. */
4093 sock = connect_local_xsocket(display_number);
4094 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004095 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004096
4097 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004098 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004099 }
Damien Miller5428f641999-11-25 11:54:57 +11004100 /*
4101 * Connect to an inet socket. The DISPLAY value is supposedly
4102 * hostname:d[.s], where hostname may also be numeric IP address.
4103 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004104 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004105 cp = strchr(buf, ':');
4106 if (!cp) {
4107 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004108 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004109 }
Damien Miller95def091999-11-25 00:26:21 +11004110 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004111 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004112 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004113 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004114 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004115 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004116 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004117
Damien Miller34132e52000-01-14 15:45:46 +11004118 /* Look up the host address */
4119 memset(&hints, 0, sizeof(hints));
4120 hints.ai_family = IPv4or6;
4121 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004122 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004123 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004124 error("%.100s: unknown host. (%s)", buf,
4125 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004126 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004127 }
Damien Miller34132e52000-01-14 15:45:46 +11004128 for (ai = aitop; ai; ai = ai->ai_next) {
4129 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004130 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004131 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004132 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004133 continue;
4134 }
4135 /* Connect it to the display. */
4136 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004137 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004138 6000 + display_number, strerror(errno));
4139 close(sock);
4140 continue;
4141 }
4142 /* Success */
4143 break;
Damien Miller34132e52000-01-14 15:45:46 +11004144 }
Damien Miller34132e52000-01-14 15:45:46 +11004145 freeaddrinfo(aitop);
4146 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004147 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004148 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004149 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004150 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004151 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004152 return sock;
4153}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004154
Damien Millerbd483e72000-04-30 10:00:53 +10004155/*
Damien Miller5428f641999-11-25 11:54:57 +11004156 * Requests forwarding of X11 connections, generates fake authentication
4157 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004158 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004159 */
Damien Miller4af51302000-04-16 11:18:38 +10004160void
Damien Miller17e7ed02005-06-17 12:54:33 +10004161x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004162 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004163{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004164 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004165 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004166 char *new_data;
4167 int screen_number;
4168 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004169
Damien Millerf92c0792005-07-06 09:45:26 +10004170 if (x11_saved_display == NULL)
4171 x11_saved_display = xstrdup(disp);
4172 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004173 error("x11_request_forwarding_with_spoofing: different "
4174 "$DISPLAY already forwarded");
4175 return;
4176 }
4177
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004178 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004179 if (cp)
4180 cp = strchr(cp, '.');
4181 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004182 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004183 else
4184 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004185
Damien Miller13390022005-07-06 09:44:19 +10004186 if (x11_saved_proto == NULL) {
4187 /* Save protocol name. */
4188 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004189
4190 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004191 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004192 for (i = 0; i < data_len; i++) {
4193 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4194 fatal("x11_request_forwarding: bad "
4195 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004196 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004197 }
4198 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004199
4200 /* Generate fake data of the same length. */
4201 x11_fake_data = xmalloc(data_len);
4202 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004203 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004204 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004205
Damien Miller95def091999-11-25 00:26:21 +11004206 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004207 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004208
Damien Miller95def091999-11-25 00:26:21 +11004209 /* Send the request packet. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00004210 channel_request_start(client_session_id, "x11-req", want_reply);
4211 packet_put_char(0); /* XXX bool single connection */
Damien Millerbd483e72000-04-30 10:00:53 +10004212 packet_put_cstring(proto);
4213 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004214 packet_put_int(screen_number);
4215 packet_send();
4216 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004217 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004218}