blob: e7de370d4d78f4d778e297a77ab03b4f4588b0c9 [file] [log] [blame]
markus@openbsd.orgc2212192017-05-30 08:49:58 +00001/* $OpenBSD: channels.c,v 1.362 2017/05/30 08:49:58 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <netdb.h>
Darren Tucker37f92202015-02-23 03:07:24 +110059#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000060#include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100062#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100063#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100064#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110065#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100066#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100067#include <stdarg.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Millerb84886b2008-05-19 15:05:07 +100069#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000073#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "log.h"
76#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100077#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100081#include "key.h"
82#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110083#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Ben Lindstrome9c99912001-06-09 00:41:05 +000085/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller5428f641999-11-25 11:54:57 +110087/*
88 * Pointer to an array containing all allocated channels. The array is
89 * dynamically extended as needed.
90 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000091static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller5428f641999-11-25 11:54:57 +110093/*
94 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000095 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110096 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100097static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000101 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100102 */
Damien Miller5e953212001-01-30 09:14:00 +1100103static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Ben Lindstrome9c99912001-06-09 00:41:05 +0000106/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Data structure for storing which hosts are permitted for forward requests.
110 * The local sides of any remote forwards are stored in this array to prevent
111 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
112 * network (which might be behind a firewall).
113 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000114/* XXX: streamlocal wants a path instead of host:port */
115/* Overload host_to_connect; we could just make this match Forward */
116/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100117typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000118 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000119 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000120 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000121 char *listen_path; /* Remote side should listen path. */
122 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000123 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124} ForwardPermission;
125
Damien Miller9b439df2006-07-24 14:04:00 +1000126/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000127static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000128
Damien Miller9b439df2006-07-24 14:04:00 +1000129/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000130static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000131
132/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
135/* Number of permitted host/port pair in the array permitted by the admin. */
136static int num_adm_permitted_opens = 0;
137
Darren Tucker1338b9e2011-10-02 18:57:35 +1100138/* special-case port number meaning allow any port */
139#define FWD_PERMIT_ANY_PORT 0
140
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000141/* special-case wildcard meaning allow any host */
142#define FWD_PERMIT_ANY_HOST "*"
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * If this is true, all opens are permitted. This is the case on the server
146 * on which we have to trust the client anyway, and the user could do
147 * anything after logging in anyway.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149static int all_opens_permitted = 0;
150
Ben Lindstrome9c99912001-06-09 00:41:05 +0000151
152/* -- X11 forwarding */
153
154/* Maximum number of fake X11 displays to try. */
155#define MAX_DISPLAYS 1000
156
Damien Miller13390022005-07-06 09:44:19 +1000157/* Saved X11 local (client) display. */
158static char *x11_saved_display = NULL;
159
Ben Lindstrome9c99912001-06-09 00:41:05 +0000160/* Saved X11 authentication protocol name. */
161static char *x11_saved_proto = NULL;
162
163/* Saved X11 authentication data. This is the real data. */
164static char *x11_saved_data = NULL;
165static u_int x11_saved_data_len = 0;
166
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000167/* Deadline after which all X11 connections are refused */
168static u_int x11_refuse_time;
169
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170/*
171 * Fake X11 authentication data. This is what the server will be sending us;
172 * we should replace any occurrences of this by the real data.
173 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100174static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000175static u_int x11_fake_data_len;
176
177
178/* -- agent forwarding */
179
180#define NUM_SOCKS 10
181
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000182/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000183static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000184
Ben Lindstrome9c99912001-06-09 00:41:05 +0000185/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static void port_open_helper(Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000187static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000188
Damien Millerbd740252008-05-19 15:37:09 +1000189/* non-blocking connect helpers */
190static int connect_next(struct channel_connect *);
191static void channel_connect_ctx_free(struct channel_connect *);
192
Ben Lindstrome9c99912001-06-09 00:41:05 +0000193/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000194
195Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100196channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000197{
198 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000199
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000200 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100201 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000202 return NULL;
203 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000204 c = channels[id];
205 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100206 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000207 return NULL;
208 }
209 return c;
210}
211
markus@openbsd.org8d057842016-09-30 09:19:13 +0000212Channel *
213channel_by_remote_id(int remote_id)
214{
215 Channel *c;
216 u_int i;
217
218 for (i = 0; i < channels_alloc; i++) {
219 c = channels[i];
220 if (c != NULL && c->remote_id == remote_id)
221 return c;
222 }
223 return NULL;
224}
225
Damien Miller5428f641999-11-25 11:54:57 +1100226/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100227 * Returns the channel if it is allowed to receive protocol messages.
228 * Private channels, like listening sockets, may not receive messages.
229 */
230Channel *
231channel_lookup(int id)
232{
233 Channel *c;
234
235 if ((c = channel_by_id(id)) == NULL)
236 return (NULL);
237
Damien Millerd62f2ca2006-03-26 13:57:41 +1100238 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100239 case SSH_CHANNEL_X11_OPEN:
240 case SSH_CHANNEL_LARVAL:
241 case SSH_CHANNEL_CONNECTING:
242 case SSH_CHANNEL_DYNAMIC:
243 case SSH_CHANNEL_OPENING:
244 case SSH_CHANNEL_OPEN:
Damien Miller36187092013-06-10 13:07:11 +1000245 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000246 case SSH_CHANNEL_MUX_PROXY:
Damien Millerd47c62a2005-12-13 19:33:57 +1100247 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100248 }
249 logit("Non-public channel %d, type %d.", id, c->type);
250 return (NULL);
251}
252
253/*
Damien Millere247cc42000-05-07 12:03:14 +1000254 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000255 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100256 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000257static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100258channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000259 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260{
Damien Miller95def091999-11-25 00:26:21 +1100261 /* Update the maximum file descriptor value. */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000262 channel_max_fd = MAXIMUM(channel_max_fd, rfd);
263 channel_max_fd = MAXIMUM(channel_max_fd, wfd);
264 channel_max_fd = MAXIMUM(channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100265
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100266 if (rfd != -1)
267 fcntl(rfd, F_SETFD, FD_CLOEXEC);
268 if (wfd != -1 && wfd != rfd)
269 fcntl(wfd, F_SETFD, FD_CLOEXEC);
270 if (efd != -1 && efd != rfd && efd != wfd)
271 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000272
Damien Miller6f83b8e2000-05-02 09:23:45 +1000273 c->rfd = rfd;
274 c->wfd = wfd;
275 c->sock = (rfd == wfd) ? rfd : -1;
276 c->efd = efd;
277 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100278
Darren Tuckered3cdc02008-06-16 23:29:18 +1000279 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000280 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000281#ifdef _AIX
282 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000283 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000284#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100285
Damien Miller69b69aa2000-10-28 14:19:58 +1100286 /* enable nonblocking mode */
287 if (nonblock) {
288 if (rfd != -1)
289 set_nonblock(rfd);
290 if (wfd != -1)
291 set_nonblock(wfd);
292 if (efd != -1)
293 set_nonblock(efd);
294 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000295}
296
297/*
298 * Allocate a new channel object and set its type and socket. This will cause
299 * remote_name to be freed.
300 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000301Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000302channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000303 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000304{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000305 int found;
306 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000307 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Miller95def091999-11-25 00:26:21 +1100309 /* Do initial allocation if this is the first call. */
310 if (channels_alloc == 0) {
311 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100312 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100313 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000314 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100315 }
316 /* Try to find a free slot where to put the new channel. */
317 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000318 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100319 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000320 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100321 break;
322 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000323 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100324 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100325 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000326 if (channels_alloc > 10000)
327 fatal("channel_new: internal error: channels_alloc %d "
328 "too big.", channels_alloc);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000329 channels = xreallocarray(channels, channels_alloc + 10,
Damien Miller36812092006-03-26 14:22:47 +1100330 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000331 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100332 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100333 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000334 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000336 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100337 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100338 buffer_init(&c->input);
339 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000340 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100341 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000342 c->listening_addr = NULL;
343 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100344 c->ostate = CHAN_OUTPUT_OPEN;
345 c->istate = CHAN_INPUT_OPEN;
346 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000347 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000348 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100349 c->self = found;
350 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000351 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000352 c->local_window = window;
353 c->local_window_max = window;
354 c->local_consumed = 0;
355 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100356 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000357 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000358 c->remote_window = 0;
359 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000360 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100361 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000362 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100363 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000364 c->open_confirm = NULL;
365 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000366 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100367 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000368 c->filter_ctx = NULL;
369 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100370 c->ctl_chan = -1;
371 c->mux_rcb = NULL;
372 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000373 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100374 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000375 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100376 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000377 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000379
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000380static int
381channel_find_maxfd(void)
382{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000383 u_int i;
384 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000385 Channel *c;
386
387 for (i = 0; i < channels_alloc; i++) {
388 c = channels[i];
389 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000390 max = MAXIMUM(max, c->rfd);
391 max = MAXIMUM(max, c->wfd);
392 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000393 }
394 }
395 return max;
396}
397
398int
399channel_close_fd(int *fdp)
400{
401 int ret = 0, fd = *fdp;
402
403 if (fd != -1) {
404 ret = close(fd);
405 *fdp = -1;
406 if (fd == channel_max_fd)
407 channel_max_fd = channel_find_maxfd();
408 }
409 return ret;
410}
411
Damien Miller6f83b8e2000-05-02 09:23:45 +1000412/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000414channel_close_fds(Channel *c)
415{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000416 channel_close_fd(&c->sock);
417 channel_close_fd(&c->rfd);
418 channel_close_fd(&c->wfd);
419 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000420}
421
422/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000423void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000424channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000426 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000427 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000428 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000429 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000430
markus@openbsd.org8d057842016-09-30 09:19:13 +0000431 for (n = 0, i = 0; i < channels_alloc; i++) {
432 if ((other = channels[i]) != NULL) {
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000433 n++;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000434
435 /* detach from mux client and prepare for closing */
436 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
437 other->type == SSH_CHANNEL_MUX_PROXY &&
438 other->mux_ctx == c) {
439 other->mux_ctx = NULL;
440 other->type = SSH_CHANNEL_OPEN;
441 other->istate = CHAN_INPUT_CLOSED;
442 other->ostate = CHAN_OUTPUT_CLOSED;
443 }
444 }
445 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000446 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000447 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000448
markus@openbsd.org8d057842016-09-30 09:19:13 +0000449 /* XXX more MUX cleanup: remove remote forwardings */
450 if (c->type == SSH_CHANNEL_MUX_CLIENT) {
451 for (i = 0; i < (u_int)num_permitted_opens; i++) {
452 if (permitted_opens[i].downstream != c)
453 continue;
454 /* cancel on the server, since mux client is gone */
455 debug("channel %d: cleanup remote forward for %s:%u",
456 c->self,
457 permitted_opens[i].listen_host,
458 permitted_opens[i].listen_port);
459 packet_start(SSH2_MSG_GLOBAL_REQUEST);
460 packet_put_cstring("cancel-tcpip-forward");
461 packet_put_char(0);
462 packet_put_cstring(channel_rfwd_bind_host(
463 permitted_opens[i].listen_host));
464 packet_put_int(permitted_opens[i].listen_port);
465 packet_send();
466 /* unregister */
467 permitted_opens[i].listen_port = 0;
468 permitted_opens[i].port_to_connect = 0;
469 free(permitted_opens[i].host_to_connect);
470 permitted_opens[i].host_to_connect = NULL;
471 free(permitted_opens[i].listen_host);
472 permitted_opens[i].listen_host = NULL;
473 permitted_opens[i].listen_path = NULL;
474 permitted_opens[i].downstream = NULL;
475 }
476 }
477
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000478 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000479 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000480 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000481
Damien Miller6f83b8e2000-05-02 09:23:45 +1000482 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000483 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000484 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000485 buffer_free(&c->input);
486 buffer_free(&c->output);
487 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000488 free(c->remote_name);
489 c->remote_name = NULL;
490 free(c->path);
491 c->path = NULL;
492 free(c->listening_addr);
493 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000494 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
495 if (cc->abandon_cb != NULL)
496 cc->abandon_cb(c, cc->ctx);
497 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100498 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000499 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000500 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000501 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
502 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000503 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000504 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505}
506
Ben Lindstrome9c99912001-06-09 00:41:05 +0000507void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000508channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000509{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000510 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000511
Ben Lindstrom601e4362001-06-21 03:19:23 +0000512 for (i = 0; i < channels_alloc; i++)
513 if (channels[i] != NULL)
514 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000515}
516
517/*
518 * Closes the sockets/fds of all channels. This is used to close extra file
519 * descriptors after a fork.
520 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000521void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000522channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000523{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000524 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000525
526 for (i = 0; i < channels_alloc; i++)
527 if (channels[i] != NULL)
528 channel_close_fds(channels[i]);
529}
530
531/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000532 * Stop listening to channels.
533 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000534void
535channel_stop_listening(void)
536{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000537 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000538 Channel *c;
539
540 for (i = 0; i < channels_alloc; i++) {
541 c = channels[i];
542 if (c != NULL) {
543 switch (c->type) {
544 case SSH_CHANNEL_AUTH_SOCKET:
545 case SSH_CHANNEL_PORT_LISTENER:
546 case SSH_CHANNEL_RPORT_LISTENER:
547 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000548 case SSH_CHANNEL_UNIX_LISTENER:
549 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000550 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000551 channel_free(c);
552 break;
553 }
554 }
555 }
556}
557
558/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000559 * Returns true if no channel has too much buffered data, and false if one or
560 * more channel is overfull.
561 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000562int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000563channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000564{
565 u_int i;
566 Channel *c;
567
568 for (i = 0; i < channels_alloc; i++) {
569 c = channels[i];
570 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000571 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000572 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000573 c->self, buffer_len(&c->output),
574 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000575 return 0;
576 }
577 }
578 }
579 return 1;
580}
581
582/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000583int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000584channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000585{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000586 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000587 Channel *c;
588
589 for (i = 0; i < channels_alloc; i++) {
590 c = channels[i];
591 if (c == NULL)
592 continue;
593 switch (c->type) {
594 case SSH_CHANNEL_X11_LISTENER:
595 case SSH_CHANNEL_PORT_LISTENER:
596 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100597 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 case SSH_CHANNEL_CLOSED:
599 case SSH_CHANNEL_AUTH_SOCKET:
600 case SSH_CHANNEL_DYNAMIC:
601 case SSH_CHANNEL_CONNECTING:
602 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000603 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000604 case SSH_CHANNEL_UNIX_LISTENER:
605 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000606 continue;
607 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000608 continue;
609 case SSH_CHANNEL_OPENING:
610 case SSH_CHANNEL_OPEN:
611 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100612 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000613 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000614 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000615 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000616 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000617 /* NOTREACHED */
618 }
619 }
620 return 0;
621}
622
623/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000624int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000625channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000626{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000627 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000628 Channel *c;
629
630 for (i = 0; i < channels_alloc; i++) {
631 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000632 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633 continue;
634 switch (c->type) {
635 case SSH_CHANNEL_CLOSED:
636 case SSH_CHANNEL_DYNAMIC:
637 case SSH_CHANNEL_X11_LISTENER:
638 case SSH_CHANNEL_PORT_LISTENER:
639 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100640 case SSH_CHANNEL_MUX_LISTENER:
641 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000642 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000643 case SSH_CHANNEL_OPENING:
644 case SSH_CHANNEL_CONNECTING:
645 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000646 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000647 case SSH_CHANNEL_UNIX_LISTENER:
648 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000649 continue;
650 case SSH_CHANNEL_LARVAL:
651 case SSH_CHANNEL_AUTH_SOCKET:
652 case SSH_CHANNEL_OPEN:
653 case SSH_CHANNEL_X11_OPEN:
654 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000655 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000656 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000657 /* NOTREACHED */
658 }
659 }
660 return -1;
661}
662
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663/*
664 * Returns a message describing the currently open forwarded connections,
665 * suitable for sending to the client. The message contains crlf pairs for
666 * newlines.
667 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000669channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000670{
671 Buffer buffer;
672 Channel *c;
673 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000674 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000675
676 buffer_init(&buffer);
677 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
678 buffer_append(&buffer, buf, strlen(buf));
679 for (i = 0; i < channels_alloc; i++) {
680 c = channels[i];
681 if (c == NULL)
682 continue;
683 switch (c->type) {
684 case SSH_CHANNEL_X11_LISTENER:
685 case SSH_CHANNEL_PORT_LISTENER:
686 case SSH_CHANNEL_RPORT_LISTENER:
687 case SSH_CHANNEL_CLOSED:
688 case SSH_CHANNEL_AUTH_SOCKET:
689 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000690 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100691 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000692 case SSH_CHANNEL_UNIX_LISTENER:
693 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 continue;
695 case SSH_CHANNEL_LARVAL:
696 case SSH_CHANNEL_OPENING:
697 case SSH_CHANNEL_CONNECTING:
698 case SSH_CHANNEL_DYNAMIC:
699 case SSH_CHANNEL_OPEN:
700 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000701 case SSH_CHANNEL_MUX_PROXY:
702 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000703 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000704 " #%d %.300s (t%d r%d i%u/%d o%u/%d fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000705 c->self, c->remote_name,
706 c->type, c->remote_id,
707 c->istate, buffer_len(&c->input),
708 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100709 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000710 buffer_append(&buffer, buf, strlen(buf));
711 continue;
712 default:
713 fatal("channel_open_message: bad channel type %d", c->type);
714 /* NOTREACHED */
715 }
716 }
717 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000718 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000719 buffer_free(&buffer);
720 return cp;
721}
722
723void
724channel_send_open(int id)
725{
726 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000727
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000729 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000730 return;
731 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000732 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000733 packet_start(SSH2_MSG_CHANNEL_OPEN);
734 packet_put_cstring(c->ctype);
735 packet_put_int(c->self);
736 packet_put_int(c->local_window);
737 packet_put_int(c->local_maxpacket);
738 packet_send();
739}
740
741void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000742channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000744 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000745
Ben Lindstrome9c99912001-06-09 00:41:05 +0000746 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000747 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000748 return;
749 }
Damien Miller0e220db2004-06-15 10:34:08 +1000750 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000751 packet_start(SSH2_MSG_CHANNEL_REQUEST);
752 packet_put_int(c->remote_id);
753 packet_put_cstring(service);
754 packet_put_char(wantconfirm);
755}
Damien Miller4f7becb2006-03-26 14:10:14 +1100756
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757void
Damien Millerb84886b2008-05-19 15:05:07 +1000758channel_register_status_confirm(int id, channel_confirm_cb *cb,
759 channel_confirm_abandon_cb *abandon_cb, void *ctx)
760{
761 struct channel_confirm *cc;
762 Channel *c;
763
764 if ((c = channel_lookup(id)) == NULL)
765 fatal("channel_register_expect: %d: bad id", id);
766
Damien Miller6c81fee2013-11-08 12:19:55 +1100767 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000768 cc->cb = cb;
769 cc->abandon_cb = abandon_cb;
770 cc->ctx = ctx;
771 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
772}
773
774void
Damien Millerd530f5f2010-05-21 14:57:10 +1000775channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000776{
777 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000778
Ben Lindstrome9c99912001-06-09 00:41:05 +0000779 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100780 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000781 return;
782 }
Damien Millerb84886b2008-05-19 15:05:07 +1000783 c->open_confirm = fn;
784 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000785}
Damien Miller4f7becb2006-03-26 14:10:14 +1100786
Ben Lindstrome9c99912001-06-09 00:41:05 +0000787void
Damien Miller39eda6e2005-11-05 14:52:50 +1100788channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000789{
Damien Millerd47c62a2005-12-13 19:33:57 +1100790 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000791
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000793 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000794 return;
795 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000796 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100797 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000798}
Damien Miller4f7becb2006-03-26 14:10:14 +1100799
Ben Lindstrome9c99912001-06-09 00:41:05 +0000800void
801channel_cancel_cleanup(int id)
802{
Damien Millerd47c62a2005-12-13 19:33:57 +1100803 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000804
Ben Lindstrome9c99912001-06-09 00:41:05 +0000805 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000806 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000807 return;
808 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000809 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100810 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000811}
Damien Miller4f7becb2006-03-26 14:10:14 +1100812
Ben Lindstrome9c99912001-06-09 00:41:05 +0000813void
Damien Miller077b2382005-12-31 16:22:32 +1100814channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000815 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000816{
817 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000818
Ben Lindstrome9c99912001-06-09 00:41:05 +0000819 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000820 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000821 return;
822 }
Damien Miller077b2382005-12-31 16:22:32 +1100823 c->input_filter = ifn;
824 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000825 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000826 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000827}
828
829void
830channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000831 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000832{
833 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000834
Ben Lindstrome9c99912001-06-09 00:41:05 +0000835 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
836 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000837 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100839 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000840 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
841 packet_put_int(c->remote_id);
842 packet_put_int(c->local_window);
843 packet_send();
844}
845
Damien Miller5428f641999-11-25 11:54:57 +1100846/*
Damien Millerb38eff82000-04-01 11:09:21 +1000847 * 'channel_pre*' are called just before select() to add any bits relevant to
848 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100849 */
Damien Millerb38eff82000-04-01 11:09:21 +1000850/*
851 * 'channel_post*': perform any appropriate operations for channels which
852 * have events pending.
853 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100854typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000855chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
856chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
857
Damien Miller8473dd82006-07-24 14:08:32 +1000858/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100860channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000861{
862 FD_SET(c->sock, readset);
863}
864
Damien Miller8473dd82006-07-24 14:08:32 +1000865/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000866static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100867channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000868{
869 debug3("channel %d: waiting for connection", c->self);
870 FD_SET(c->sock, writeset);
871}
872
Ben Lindstrombba81212001-06-25 05:01:22 +0000873static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100874channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000875{
Damien Miller33b13562000-04-04 14:38:59 +1000876 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000877 c->remote_window > 0 &&
878 buffer_len(&c->input) < c->remote_window &&
Damien Miller499a0d52006-04-23 12:06:03 +1000879 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000880 FD_SET(c->rfd, readset);
881 if (c->ostate == CHAN_OUTPUT_OPEN ||
882 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
883 if (buffer_len(&c->output) > 0) {
884 FD_SET(c->wfd, writeset);
885 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000886 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000887 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
888 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000889 else
890 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000891 }
892 }
893 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000894 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
895 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000896 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
897 buffer_len(&c->extended) > 0)
898 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000899 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
900 (c->extended_usage == CHAN_EXTENDED_READ ||
901 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000902 buffer_len(&c->extended) < c->remote_window)
903 FD_SET(c->efd, readset);
904 }
Damien Miller0e220db2004-06-15 10:34:08 +1000905 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000906}
907
Damien Millerb38eff82000-04-01 11:09:21 +1000908/*
909 * This is a special state for X11 authentication spoofing. An opened X11
910 * connection (when authentication spoofing is being done) remains in this
911 * state until the first packet has been completely read. The authentication
912 * data in that packet is then substituted by the real data if it matches the
913 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000914 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000915 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000916 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000917static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000918x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000919{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000920 u_char *ucp;
921 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000922
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000923 /* Is this being called after the refusal deadline? */
924 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
925 verbose("Rejected X11 connection after ForwardX11Timeout "
926 "expired");
927 return -1;
928 }
929
Damien Millerb38eff82000-04-01 11:09:21 +1000930 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000931 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000932 return 0;
933
934 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100935 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000936 if (ucp[0] == 0x42) { /* Byte order MSB first. */
937 proto_len = 256 * ucp[6] + ucp[7];
938 data_len = 256 * ucp[8] + ucp[9];
939 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
940 proto_len = ucp[6] + 256 * ucp[7];
941 data_len = ucp[8] + 256 * ucp[9];
942 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000943 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100944 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000945 return -1;
946 }
947
948 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000949 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000950 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
951 return 0;
952
953 /* Check if authentication protocol matches. */
954 if (proto_len != strlen(x11_saved_proto) ||
955 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000956 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000957 return -1;
958 }
959 /* Check if authentication data matches our fake data. */
960 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000961 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000962 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000963 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000964 return -1;
965 }
966 /* Check fake data length */
967 if (x11_fake_data_len != x11_saved_data_len) {
968 error("X11 fake_data_len %d != saved_data_len %d",
969 x11_fake_data_len, x11_saved_data_len);
970 return -1;
971 }
972 /*
973 * Received authentication protocol and data match
974 * our fake data. Substitute the fake data with real
975 * data.
976 */
977 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
978 x11_saved_data, x11_saved_data_len);
979 return 1;
980}
981
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100983channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000984{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000985 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000986
987 /* c->force_drain = 1; */
988
Damien Millerb38eff82000-04-01 11:09:21 +1000989 if (ret == 1) {
990 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100991 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000992 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000993 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000994 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100995 chan_read_failed(c);
996 buffer_clear(&c->input);
997 chan_ibuf_empty(c);
998 buffer_clear(&c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000999 chan_write_failed(c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001000 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001001 }
1002}
1003
Damien Millere1537f92010-01-26 13:26:22 +11001004static void
1005channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1006{
Damien Millerd530f5f2010-05-21 14:57:10 +10001007 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001008 buffer_check_alloc(&c->input, CHAN_RBUF))
1009 FD_SET(c->rfd, readset);
1010 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1011 /* clear buffer immediately (discard any partial packet) */
1012 buffer_clear(&c->input);
1013 chan_ibuf_empty(c);
1014 /* Start output drain. XXX just kill chan? */
1015 chan_rcvd_oclose(c);
1016 }
1017 if (c->ostate == CHAN_OUTPUT_OPEN ||
1018 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1019 if (buffer_len(&c->output) > 0)
1020 FD_SET(c->wfd, writeset);
1021 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1022 chan_obuf_empty(c);
1023 }
1024}
1025
Ben Lindstromb3921512001-04-11 15:57:50 +00001026/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001027/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001028static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001029channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001030{
Damien Millera10f5612002-09-12 09:49:15 +10001031 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001032 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001033 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001034 struct {
1035 u_int8_t version;
1036 u_int8_t command;
1037 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001038 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001039 } s4_req, s4_rsp;
1040
Ben Lindstromb3921512001-04-11 15:57:50 +00001041 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001042
1043 have = buffer_len(&c->input);
1044 len = sizeof(s4_req);
1045 if (have < len)
1046 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001047 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001048
1049 need = 1;
1050 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1051 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1052 debug2("channel %d: socks4a request", c->self);
1053 /* ... and needs an extra string (the hostname) */
1054 need = 2;
1055 }
1056 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001057 for (found = 0, i = len; i < have; i++) {
1058 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001059 found++;
1060 if (found == need)
1061 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001062 }
1063 if (i > 1024) {
1064 /* the peer is probably sending garbage */
1065 debug("channel %d: decode socks4: too long",
1066 c->self);
1067 return -1;
1068 }
1069 }
Damien Miller1781f532009-01-28 16:24:41 +11001070 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001071 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001072 buffer_get(&c->input, (char *)&s4_req.version, 1);
1073 buffer_get(&c->input, (char *)&s4_req.command, 1);
1074 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001075 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001076 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001077 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001078 if (memchr(p, '\0', have) == NULL)
1079 fatal("channel %d: decode socks4: user not nul terminated",
1080 c->self);
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);
Damien Miller1781f532009-01-28 16:24:41 +11001098 len = strlen(p);
1099 debug2("channel %d: decode socks4a: host %s/%d",
1100 c->self, p, len);
1101 len++; /* trailing '\0' */
1102 if (len > have)
1103 fatal("channel %d: decode socks4a: len %d > have %d",
1104 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001105 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001106 error("channel %d: hostname \"%.100s\" too long",
1107 c->self, p);
1108 return -1;
1109 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001110 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001111 buffer_consume(&c->input, len);
1112 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001113 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001114
Damien Millerfbdeece2003-09-02 22:52:31 +10001115 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001116 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001117
Ben Lindstromb3921512001-04-11 15:57:50 +00001118 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001119 debug("channel %d: cannot handle: %s cn %d",
1120 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001121 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001122 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001123 s4_rsp.version = 0; /* vn: 0 for reply */
1124 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001125 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001126 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001127 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001128 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001129}
1130
Darren Tucker46471c92003-07-03 13:55:19 +10001131/* try to decode a socks5 header */
1132#define SSH_SOCKS5_AUTHDONE 0x1000
1133#define SSH_SOCKS5_NOAUTH 0x00
1134#define SSH_SOCKS5_IPV4 0x01
1135#define SSH_SOCKS5_DOMAIN 0x03
1136#define SSH_SOCKS5_IPV6 0x04
1137#define SSH_SOCKS5_CONNECT 0x01
1138#define SSH_SOCKS5_SUCCESS 0x00
1139
Damien Miller8473dd82006-07-24 14:08:32 +10001140/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001141static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001142channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001143{
1144 struct {
1145 u_int8_t version;
1146 u_int8_t command;
1147 u_int8_t reserved;
1148 u_int8_t atyp;
1149 } s5_req, s5_rsp;
1150 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001151 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1152 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001153 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001154
1155 debug2("channel %d: decode socks5", c->self);
1156 p = buffer_ptr(&c->input);
1157 if (p[0] != 0x05)
1158 return -1;
1159 have = buffer_len(&c->input);
1160 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1161 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001162 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001163 return 0;
1164 nmethods = p[1];
1165 if (have < nmethods + 2)
1166 return 0;
1167 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001168 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001169 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001170 found = 1;
1171 break;
1172 }
1173 }
1174 if (!found) {
1175 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1176 c->self);
1177 return -1;
1178 }
1179 buffer_consume(&c->input, nmethods + 2);
1180 buffer_put_char(&c->output, 0x05); /* version */
1181 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1182 FD_SET(c->sock, writeset);
1183 c->flags |= SSH_SOCKS5_AUTHDONE;
1184 debug2("channel %d: socks5 auth done", c->self);
1185 return 0; /* need more */
1186 }
1187 debug2("channel %d: socks5 post auth", c->self);
1188 if (have < sizeof(s5_req)+1)
1189 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001190 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001191 if (s5_req.version != 0x05 ||
1192 s5_req.command != SSH_SOCKS5_CONNECT ||
1193 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001194 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001195 return -1;
1196 }
Darren Tucker47eede72005-03-14 23:08:12 +11001197 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001198 case SSH_SOCKS5_IPV4:
1199 addrlen = 4;
1200 af = AF_INET;
1201 break;
1202 case SSH_SOCKS5_DOMAIN:
1203 addrlen = p[sizeof(s5_req)];
1204 af = -1;
1205 break;
1206 case SSH_SOCKS5_IPV6:
1207 addrlen = 16;
1208 af = AF_INET6;
1209 break;
1210 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001211 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001212 return -1;
1213 }
Damien Miller0f077072006-07-10 22:21:02 +10001214 need = sizeof(s5_req) + addrlen + 2;
1215 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1216 need++;
1217 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001218 return 0;
1219 buffer_consume(&c->input, sizeof(s5_req));
1220 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1221 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001222 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001223 buffer_get(&c->input, (char *)&dest_port, 2);
1224 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001225 free(c->path);
1226 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001227 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001228 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001229 error("channel %d: dynamic request: socks5 hostname "
1230 "\"%.100s\" too long", c->self, dest_addr);
1231 return -1;
1232 }
1233 c->path = xstrdup(dest_addr);
1234 } else {
1235 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1236 return -1;
1237 c->path = xstrdup(ntop);
1238 }
Darren Tucker46471c92003-07-03 13:55:19 +10001239 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001240
Damien Millerfbdeece2003-09-02 22:52:31 +10001241 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001242 c->self, c->path, c->host_port, s5_req.command);
1243
1244 s5_rsp.version = 0x05;
1245 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1246 s5_rsp.reserved = 0; /* ignored */
1247 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001248 dest_port = 0; /* ignored */
1249
Damien Millera0fdce92006-03-26 14:28:50 +11001250 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001251 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001252 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001253 return 1;
1254}
1255
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001256Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001257channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1258 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001259{
1260 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001261
1262 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1263 port_to_connect);
1264
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001265 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1266 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1267 0, "stdio-forward", /*nonblock*/0);
1268
1269 c->path = xstrdup(host_to_connect);
1270 c->host_port = port_to_connect;
1271 c->listening_port = 0;
1272 c->force_drain = 1;
1273
1274 channel_register_fds(c, in, out, -1, 0, 1, 0);
1275 port_open_helper(c, "direct-tcpip");
1276
1277 return c;
1278}
1279
Ben Lindstromb3921512001-04-11 15:57:50 +00001280/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001281static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001282channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001283{
1284 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001285 u_int have;
1286 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001287
1288 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001289 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001290 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001291 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001292 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001293 /* need more */
1294 FD_SET(c->sock, readset);
1295 return;
1296 }
1297 /* try to guess the protocol */
1298 p = buffer_ptr(&c->input);
1299 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001300 case 0x04:
1301 ret = channel_decode_socks4(c, readset, writeset);
1302 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001303 case 0x05:
1304 ret = channel_decode_socks5(c, readset, writeset);
1305 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001306 default:
1307 ret = -1;
1308 break;
1309 }
1310 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001311 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001312 } else if (ret == 0) {
1313 debug2("channel %d: pre_dynamic: need more", c->self);
1314 /* need more */
1315 FD_SET(c->sock, readset);
1316 } else {
1317 /* switch to the next state */
1318 c->type = SSH_CHANNEL_OPENING;
1319 port_open_helper(c, "direct-tcpip");
1320 }
1321}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001322
Damien Millerb38eff82000-04-01 11:09:21 +10001323/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001324/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001325static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001326channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001327{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001328 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001329 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001330 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001331 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001332 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001333 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001334
1335 if (FD_ISSET(c->sock, readset)) {
1336 debug("X11 connection requested.");
1337 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001338 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001339 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001340 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001341 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001342 channel_close_fd(&c->sock);
1343 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001344 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001345 }
Damien Millerb38eff82000-04-01 11:09:21 +10001346 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001347 if (errno != EINTR && errno != EWOULDBLOCK &&
1348 errno != ECONNABORTED)
1349 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001350 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001351 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001352 return;
1353 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001354 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001355 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001356 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001357 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001358 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001359
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001360 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001361 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001362 c->local_window_max, c->local_maxpacket, 0, buf, 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001363 packet_start(SSH2_MSG_CHANNEL_OPEN);
1364 packet_put_cstring("x11");
1365 packet_put_int(nc->self);
1366 packet_put_int(nc->local_window_max);
1367 packet_put_int(nc->local_maxpacket);
1368 /* originator ipaddr and port */
1369 packet_put_cstring(remote_ipaddr);
1370 if (datafellows & SSH_BUG_X11FWD) {
1371 debug2("ssh2 x11 bug compat mode");
Damien Millerbd483e72000-04-30 10:00:53 +10001372 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001373 packet_put_int(remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001374 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001375 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001376 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001377 }
1378}
1379
Ben Lindstrombba81212001-06-25 05:01:22 +00001380static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001381port_open_helper(Channel *c, char *rtype)
1382{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001383 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001384 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001385 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001386 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001387 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001388
Damien Millerd6369432010-02-02 17:02:07 +11001389 if (remote_port == -1) {
1390 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001391 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001392 remote_ipaddr = xstrdup("127.0.0.1");
1393 remote_port = 65535;
1394 }
1395
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001396 snprintf(buf, sizeof buf,
1397 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001398 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001399 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001400 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001401
Darren Tuckera627d422013-06-02 07:31:17 +10001402 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001403 c->remote_name = xstrdup(buf);
1404
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001405 packet_start(SSH2_MSG_CHANNEL_OPEN);
1406 packet_put_cstring(rtype);
1407 packet_put_int(c->self);
1408 packet_put_int(c->local_window_max);
1409 packet_put_int(c->local_maxpacket);
1410 if (strcmp(rtype, "direct-tcpip") == 0) {
1411 /* target host, port */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001412 packet_put_cstring(c->path);
1413 packet_put_int(c->host_port);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001414 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1415 /* target path */
1416 packet_put_cstring(c->path);
1417 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1418 /* listen path */
1419 packet_put_cstring(c->path);
1420 } else {
1421 /* listen address, port */
1422 packet_put_cstring(c->path);
1423 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001424 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001425 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1426 /* reserved for future owner/mode info */
1427 packet_put_cstring("");
1428 } else {
1429 /* originator host and port */
1430 packet_put_cstring(remote_ipaddr);
1431 packet_put_int((u_int)remote_port);
1432 }
1433 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001434 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001435 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001436}
1437
Damien Miller5e7fd072005-11-05 14:53:39 +11001438static void
1439channel_set_reuseaddr(int fd)
1440{
1441 int on = 1;
1442
1443 /*
1444 * Set socket options.
1445 * Allow local port reuse in TIME_WAIT.
1446 */
1447 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1448 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1449}
1450
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001451void
1452channel_set_x11_refuse_time(u_int refuse_time)
1453{
1454 x11_refuse_time = refuse_time;
1455}
1456
Damien Millerb38eff82000-04-01 11:09:21 +10001457/*
1458 * This socket is listening for connections to a forwarded TCP/IP port.
1459 */
Damien Miller8473dd82006-07-24 14:08:32 +10001460/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001461static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001462channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001463{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001464 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001465 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001466 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001467 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001468 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001469
Damien Millerb38eff82000-04-01 11:09:21 +10001470 if (FD_ISSET(c->sock, readset)) {
1471 debug("Connection to port %d forwarding "
1472 "to %.100s port %d requested.",
1473 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001474
Damien Miller4623a752001-10-10 15:03:58 +10001475 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1476 nextstate = SSH_CHANNEL_OPENING;
1477 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001478 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1479 nextstate = SSH_CHANNEL_OPENING;
1480 rtype = "forwarded-streamlocal@openssh.com";
1481 } else if (c->host_port == PORT_STREAMLOCAL) {
1482 nextstate = SSH_CHANNEL_OPENING;
1483 rtype = "direct-streamlocal@openssh.com";
1484 } else if (c->host_port == 0) {
1485 nextstate = SSH_CHANNEL_DYNAMIC;
1486 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001487 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001488 nextstate = SSH_CHANNEL_OPENING;
1489 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001490 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001491
Damien Millerb38eff82000-04-01 11:09:21 +10001492 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001493 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001494 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001495 if (errno != EINTR && errno != EWOULDBLOCK &&
1496 errno != ECONNABORTED)
1497 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001498 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001499 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001500 return;
1501 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001502 if (c->host_port != PORT_STREAMLOCAL)
1503 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001504 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1505 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001506 nc->listening_port = c->listening_port;
1507 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001508 if (c->path != NULL)
1509 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001510
Darren Tucker876045b2010-01-08 17:08:00 +11001511 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001512 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001513 }
1514}
1515
1516/*
1517 * This is the authentication agent socket listening for connections from
1518 * clients.
1519 */
Damien Miller8473dd82006-07-24 14:08:32 +10001520/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001521static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001522channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001523{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001524 Channel *nc;
1525 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001526 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001527 socklen_t addrlen;
1528
1529 if (FD_ISSET(c->sock, readset)) {
1530 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001531 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001532 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001533 error("accept from auth socket: %.100s",
1534 strerror(errno));
1535 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001536 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001537 return;
1538 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001539 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001540 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1541 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001542 0, "accepted auth socket", 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001543 packet_start(SSH2_MSG_CHANNEL_OPEN);
1544 packet_put_cstring("auth-agent@openssh.com");
1545 packet_put_int(nc->self);
1546 packet_put_int(c->local_window_max);
1547 packet_put_int(c->local_maxpacket);
Damien Millerb38eff82000-04-01 11:09:21 +10001548 packet_send();
1549 }
1550}
1551
Damien Miller8473dd82006-07-24 14:08:32 +10001552/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001553static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001554channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001555{
Damien Millerbd740252008-05-19 15:37:09 +10001556 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001557 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001558
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001559 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001560 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001561 err = errno;
1562 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001563 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001564 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001565 debug("channel %d: connected to %s port %d",
1566 c->self, c->connect_ctx.host, c->connect_ctx.port);
1567 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001568 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001569 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1570 packet_put_int(c->remote_id);
1571 packet_put_int(c->self);
1572 packet_put_int(c->local_window);
1573 packet_put_int(c->local_maxpacket);
Ben Lindstrom69128662001-05-08 20:07:39 +00001574 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001575 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001576 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001577 /* Try next address, if any */
1578 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1579 close(c->sock);
1580 c->sock = c->rfd = c->wfd = sock;
1581 channel_max_fd = channel_find_maxfd();
1582 return;
1583 }
1584 /* Exhausted all addresses */
1585 error("connect_to %.100s port %d: failed.",
1586 c->connect_ctx.host, c->connect_ctx.port);
1587 channel_connect_ctx_free(&c->connect_ctx);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001588 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1589 packet_put_int(c->remote_id);
1590 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1591 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1592 packet_put_cstring(strerror(err));
1593 packet_put_cstring("");
Ben Lindstrom69128662001-05-08 20:07:39 +00001594 }
1595 chan_mark_dead(c);
1596 }
1597 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001598 }
1599}
1600
Damien Miller8473dd82006-07-24 14:08:32 +10001601/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001602static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001603channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001604{
Darren Tucker11327cc2005-03-14 23:22:25 +11001605 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001606 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001607
Damien Miller835284b2007-06-11 13:03:16 +10001608 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1609 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001610 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001611 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001612 if (len < 0 && (errno == EINTR ||
1613 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001614 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001615#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001616 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001617#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001618 if ((!c->isatty && len <= 0) ||
1619 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001620#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001621 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001622 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001623 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001624 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001625 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001626 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001627 } else {
1628 chan_read_failed(c);
1629 }
1630 return -1;
1631 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001632 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001633 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001634 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001635 chan_read_failed(c);
1636 }
Damien Millerd27b9472005-12-13 19:29:02 +11001637 } else if (c->datagram) {
1638 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001639 } else {
1640 buffer_append(&c->input, buf, len);
1641 }
Damien Millerb38eff82000-04-01 11:09:21 +10001642 }
1643 return 1;
1644}
Damien Miller4f7becb2006-03-26 14:10:14 +11001645
Damien Miller8473dd82006-07-24 14:08:32 +10001646/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001647static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001648channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001649{
Ben Lindstrome229b252001-03-05 06:28:06 +00001650 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001651 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001652 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001653 int len;
1654
1655 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001656 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001657 FD_ISSET(c->wfd, writeset) &&
1658 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001659 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001660 if (c->output_filter != NULL) {
1661 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1662 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001663 if (c->type != SSH_CHANNEL_OPEN)
1664 chan_mark_dead(c);
1665 else
1666 chan_write_failed(c);
1667 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001668 }
1669 } else if (c->datagram) {
1670 buf = data = buffer_get_string(&c->output, &dlen);
1671 } else {
1672 buf = data = buffer_ptr(&c->output);
1673 dlen = buffer_len(&c->output);
1674 }
1675
Damien Millerd27b9472005-12-13 19:29:02 +11001676 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001677 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001678 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001679 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001680 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1681 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001682 return 1;
1683 if (len <= 0) {
1684 if (c->type != SSH_CHANNEL_OPEN)
1685 chan_mark_dead(c);
1686 else
1687 chan_write_failed(c);
1688 return -1;
1689 }
Damien Miller7d457182010-08-05 23:09:48 +10001690 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001691 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001692#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001693 /* XXX: Later AIX versions can't push as much data to tty */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001694 if (c->wfd_isatty)
Darren Tucker240fdfa2003-11-22 14:10:02 +11001695 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001696#endif
Damien Miller077b2382005-12-31 16:22:32 +11001697
1698 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001699 if (len < 0 &&
1700 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001701 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001702 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001703 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001704 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001705 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001706 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001707 } else {
1708 chan_write_failed(c);
1709 }
1710 return -1;
1711 }
Darren Tucker3980b632009-08-28 11:02:37 +10001712#ifndef BROKEN_TCGETATTR_ICANON
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001713 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001714 if (tcgetattr(c->wfd, &tio) == 0 &&
1715 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1716 /*
1717 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001718 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001719 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001720 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001721 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001722 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001723 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001724 }
1725 }
Darren Tucker3980b632009-08-28 11:02:37 +10001726#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001727 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001728 }
Damien Miller7d457182010-08-05 23:09:48 +10001729 out:
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001730 if (olen > 0)
Damien Miller7d457182010-08-05 23:09:48 +10001731 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001732 return 1;
1733}
Damien Miller4f7becb2006-03-26 14:10:14 +11001734
Ben Lindstrombba81212001-06-25 05:01:22 +00001735static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001736channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001737{
Darren Tucker11327cc2005-03-14 23:22:25 +11001738 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001739 int len;
1740
Damien Miller1383bd82000-04-06 12:32:37 +10001741/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001742 if (c->efd != -1) {
1743 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1744 FD_ISSET(c->efd, writeset) &&
1745 buffer_len(&c->extended) > 0) {
1746 len = write(c->efd, buffer_ptr(&c->extended),
1747 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001748 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001749 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001750 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1751 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001752 return 1;
1753 if (len <= 0) {
1754 debug2("channel %d: closing write-efd %d",
1755 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001756 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001757 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001758 buffer_consume(&c->extended, len);
1759 c->local_consumed += len;
1760 }
Damien Miller8853ca52010-06-26 10:00:14 +10001761 } else if (c->efd != -1 &&
1762 (c->extended_usage == CHAN_EXTENDED_READ ||
1763 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001764 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001765 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001766 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001767 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001768 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1769 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001770 return 1;
1771 if (len <= 0) {
1772 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001773 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001774 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001775 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001776 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1777 debug3("channel %d: discard efd",
1778 c->self);
1779 } else
1780 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001781 }
Damien Miller33b13562000-04-04 14:38:59 +10001782 }
1783 }
1784 return 1;
1785}
Damien Miller4f7becb2006-03-26 14:10:14 +11001786
Damien Miller0e220db2004-06-15 10:34:08 +10001787static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001788channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001789{
Ben Lindstromb3921512001-04-11 15:57:50 +00001790 if (c->type == SSH_CHANNEL_OPEN &&
1791 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001792 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001793 c->local_maxpacket*3) ||
1794 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001795 c->local_consumed > 0) {
1796 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1797 packet_put_int(c->remote_id);
1798 packet_put_int(c->local_consumed);
1799 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001800 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001801 c->self, c->local_window,
1802 c->local_consumed);
1803 c->local_window += c->local_consumed;
1804 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001805 }
1806 return 1;
1807}
1808
Ben Lindstrombba81212001-06-25 05:01:22 +00001809static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001810channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001811{
1812 channel_handle_rfd(c, readset, writeset);
1813 channel_handle_wfd(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +10001814 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001815 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001816}
1817
Damien Millere1537f92010-01-26 13:26:22 +11001818static u_int
1819read_mux(Channel *c, u_int need)
1820{
1821 char buf[CHAN_RBUF];
1822 int len;
1823 u_int rlen;
1824
1825 if (buffer_len(&c->input) < need) {
1826 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001827 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001828 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1829 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001830 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001831 debug2("channel %d: ctl read<=0 rfd %d len %d",
1832 c->self, c->rfd, len);
1833 chan_read_failed(c);
1834 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001835 } else
1836 buffer_append(&c->input, buf, len);
1837 }
1838 return buffer_len(&c->input);
1839}
1840
1841static void
1842channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1843{
1844 u_int need;
1845 ssize_t len;
1846
Damien Millerd530f5f2010-05-21 14:57:10 +10001847 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001848 (c->istate == CHAN_INPUT_OPEN ||
1849 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1850 /*
1851 * Don't not read past the precise end of packets to
1852 * avoid disrupting fd passing.
1853 */
1854 if (read_mux(c, 4) < 4) /* read header */
1855 return;
1856 need = get_u32(buffer_ptr(&c->input));
1857#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1858 if (need > CHANNEL_MUX_MAX_PACKET) {
1859 debug2("channel %d: packet too big %u > %u",
1860 c->self, CHANNEL_MUX_MAX_PACKET, need);
1861 chan_rcvd_oclose(c);
1862 return;
1863 }
1864 if (read_mux(c, need + 4) < need + 4) /* read body */
1865 return;
1866 if (c->mux_rcb(c) != 0) {
1867 debug("channel %d: mux_rcb failed", c->self);
1868 chan_mark_dead(c);
1869 return;
1870 }
1871 }
1872
1873 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1874 buffer_len(&c->output) > 0) {
1875 len = write(c->wfd, buffer_ptr(&c->output),
1876 buffer_len(&c->output));
1877 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1878 return;
1879 if (len <= 0) {
1880 chan_mark_dead(c);
1881 return;
1882 }
1883 buffer_consume(&c->output, len);
1884 }
1885}
1886
1887static void
1888channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1889{
1890 Channel *nc;
1891 struct sockaddr_storage addr;
1892 socklen_t addrlen;
1893 int newsock;
1894 uid_t euid;
1895 gid_t egid;
1896
1897 if (!FD_ISSET(c->sock, readset))
1898 return;
1899
1900 debug("multiplexing control connection");
1901
1902 /*
1903 * Accept connection on control socket
1904 */
1905 memset(&addr, 0, sizeof(addr));
1906 addrlen = sizeof(addr);
1907 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1908 &addrlen)) == -1) {
1909 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001910 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001911 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001912 return;
1913 }
1914
1915 if (getpeereid(newsock, &euid, &egid) < 0) {
1916 error("%s getpeereid failed: %s", __func__,
1917 strerror(errno));
1918 close(newsock);
1919 return;
1920 }
1921 if ((euid != 0) && (getuid() != euid)) {
1922 error("multiplex uid mismatch: peer euid %u != uid %u",
1923 (u_int)euid, (u_int)getuid());
1924 close(newsock);
1925 return;
1926 }
1927 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
1928 newsock, newsock, -1, c->local_window_max,
1929 c->local_maxpacket, 0, "mux-control", 1);
1930 nc->mux_rcb = c->mux_rcb;
1931 debug3("%s: new mux channel %d fd %d", __func__,
1932 nc->self, nc->sock);
1933 /* establish state */
1934 nc->mux_rcb(nc);
1935 /* mux state transitions must not elicit protocol messages */
1936 nc->flags |= CHAN_LOCAL;
1937}
1938
Ben Lindstrombba81212001-06-25 05:01:22 +00001939static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001940channel_handler_init(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001941{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001942 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001943
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001944 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1945 channel_pre[i] = NULL;
1946 channel_post[i] = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001947 }
Damien Millerde6987c2002-01-22 23:20:40 +11001948 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001949 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001950 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001951 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001952 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
1953 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001954 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001955 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001956 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001957 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11001958 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
1959 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001960
Damien Millerde6987c2002-01-22 23:20:40 +11001961 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001962 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001963 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001964 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
1965 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001966 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001967 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001968 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001969 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11001970 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
1971 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001972}
1973
Damien Miller3ec27592001-10-12 11:35:04 +10001974/* gc dead channels */
1975static void
1976channel_garbage_collect(Channel *c)
1977{
1978 if (c == NULL)
1979 return;
1980 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001981 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001982 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001983 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001984 c->detach_user(c->self, NULL);
1985 /* if we still have a callback */
1986 if (c->detach_user != NULL)
1987 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001988 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001989 }
1990 if (!chan_is_dead(c, 1))
1991 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001992 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001993 channel_free(c);
1994}
1995
Ben Lindstrombba81212001-06-25 05:01:22 +00001996static void
Damien Millera6508752012-04-22 11:21:10 +10001997channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
1998 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10001999{
2000 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002001 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002002 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002003 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002004
2005 if (!did_init) {
2006 channel_handler_init();
2007 did_init = 1;
2008 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002009 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002010 if (unpause_secs != NULL)
2011 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002012 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002013 c = channels[i];
2014 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002015 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002016 if (c->delayed) {
2017 if (ftab == channel_pre)
2018 c->delayed = 0;
2019 else
2020 continue;
2021 }
Damien Millera6508752012-04-22 11:21:10 +10002022 if (ftab[c->type] != NULL) {
2023 /*
2024 * Run handlers that are not paused.
2025 */
2026 if (c->notbefore <= now)
2027 (*ftab[c->type])(c, readset, writeset);
2028 else if (unpause_secs != NULL) {
2029 /*
2030 * Collect the time that the earliest
2031 * channel comes off pause.
2032 */
2033 debug3("%s: chan %d: skip for %d more seconds",
2034 __func__, c->self,
2035 (int)(c->notbefore - now));
2036 if (*unpause_secs == 0 ||
2037 (c->notbefore - now) < *unpause_secs)
2038 *unpause_secs = c->notbefore - now;
2039 }
2040 }
Damien Miller3ec27592001-10-12 11:35:04 +10002041 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002042 }
Damien Millera6508752012-04-22 11:21:10 +10002043 if (unpause_secs != NULL && *unpause_secs != 0)
2044 debug3("%s: first channel unpauses in %d seconds",
2045 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002046}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002047
Ben Lindstrome9c99912001-06-09 00:41:05 +00002048/*
2049 * Allocate/update select bitmasks and add any bits relevant to channels in
2050 * select bitmasks.
2051 */
Damien Miller4af51302000-04-16 11:18:38 +10002052void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002053channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002054 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002055{
Damien Miller36812092006-03-26 14:22:47 +11002056 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002057
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002058 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002059
Damien Miller36812092006-03-26 14:22:47 +11002060 nfdset = howmany(n+1, NFDBITS);
2061 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002062 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002063 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2064 sz = nfdset * sizeof(fd_mask);
2065
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002066 /* perhaps check sz < nalloc/2 and shrink? */
2067 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002068 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2069 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002070 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002071 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002072 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002073 memset(*readsetp, 0, sz);
2074 memset(*writesetp, 0, sz);
2075
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002076 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002077 channel_handler(channel_pre, *readsetp, *writesetp,
2078 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002079}
2080
Ben Lindstrome9c99912001-06-09 00:41:05 +00002081/*
2082 * After select, perform any appropriate operations for channels which have
2083 * events pending.
2084 */
Damien Miller4af51302000-04-16 11:18:38 +10002085void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002086channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002087{
Damien Millera6508752012-04-22 11:21:10 +10002088 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002089}
2090
Ben Lindstrome9c99912001-06-09 00:41:05 +00002091
Damien Miller5e953212001-01-30 09:14:00 +11002092/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002093void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002094channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002095{
Damien Millerb38eff82000-04-01 11:09:21 +10002096 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002097 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002098
Damien Miller95def091999-11-25 00:26:21 +11002099 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002100 c = channels[i];
2101 if (c == NULL)
2102 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002103
Ben Lindstrome9c99912001-06-09 00:41:05 +00002104 /*
2105 * We are only interested in channels that can have buffered
2106 * incoming data.
2107 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002108 if (c->type != SSH_CHANNEL_OPEN)
2109 continue;
2110 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002111 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002112 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002113 continue;
2114 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002115
Damien Miller95def091999-11-25 00:26:21 +11002116 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002117 if ((c->istate == CHAN_INPUT_OPEN ||
2118 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2119 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002120 if (c->datagram) {
2121 if (len > 0) {
2122 u_char *data;
2123 u_int dlen;
2124
2125 data = buffer_get_string(&c->input,
2126 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002127 if (dlen > c->remote_window ||
2128 dlen > c->remote_maxpacket) {
2129 debug("channel %d: datagram "
2130 "too big for channel",
2131 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002132 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002133 continue;
2134 }
Damien Millerd27b9472005-12-13 19:29:02 +11002135 packet_start(SSH2_MSG_CHANNEL_DATA);
2136 packet_put_int(c->remote_id);
2137 packet_put_string(data, dlen);
2138 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002139 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002140 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002141 }
2142 continue;
2143 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002144 /*
2145 * Send some data for the other side over the secure
2146 * connection.
2147 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002148 if (len > c->remote_window)
2149 len = c->remote_window;
2150 if (len > c->remote_maxpacket)
2151 len = c->remote_maxpacket;
Damien Millerb38eff82000-04-01 11:09:21 +10002152 if (len > 0) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002153 packet_start(SSH2_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002154 packet_put_int(c->remote_id);
2155 packet_put_string(buffer_ptr(&c->input), len);
2156 packet_send();
2157 buffer_consume(&c->input, len);
2158 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002159 }
2160 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller5428f641999-11-25 11:54:57 +11002161 /*
2162 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002163 * tell peer, that we will not send more data: send IEOF.
2164 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002165 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002166 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002167 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2168 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002169 else
2170 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002171 }
Damien Miller33b13562000-04-04 14:38:59 +10002172 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002173 if (!(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002174 c->remote_window > 0 &&
2175 (len = buffer_len(&c->extended)) > 0 &&
2176 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002177 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002178 c->self, c->remote_window, buffer_len(&c->extended),
2179 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002180 if (len > c->remote_window)
2181 len = c->remote_window;
2182 if (len > c->remote_maxpacket)
2183 len = c->remote_maxpacket;
2184 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2185 packet_put_int(c->remote_id);
2186 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2187 packet_put_string(buffer_ptr(&c->extended), len);
2188 packet_send();
2189 buffer_consume(&c->extended, len);
2190 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002191 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002192 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002193 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002194}
2195
markus@openbsd.org8d057842016-09-30 09:19:13 +00002196/* -- mux proxy support */
2197
2198/*
2199 * When multiplexing channel messages for mux clients we have to deal
2200 * with downstream messages from the mux client and upstream messages
2201 * from the ssh server:
2202 * 1) Handling downstream messages is straightforward and happens
2203 * in channel_proxy_downstream():
2204 * - We forward all messages (mostly) unmodified to the server.
2205 * - However, in order to route messages from upstream to the correct
2206 * downstream client, we have to replace the channel IDs used by the
2207 * mux clients with a unique channel ID because the mux clients might
2208 * use conflicting channel IDs.
2209 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2210 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2211 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2212 * with the newly allocated channel ID.
2213 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2214 * channels and procesed by channel_proxy_upstream(). The local channel ID
2215 * is then translated back to the original mux client ID.
2216 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2217 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2218 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2219 * downstream mux client are removed.
2220 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2221 * requires more work, because they are not addressed to a specific
2222 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2223 * out whether the request is addressed to the local client or a
2224 * specific downstream client based on the listen-address/port.
2225 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2226 * not supported as the matching session/channel cannot be identified
2227 * easily.
2228 */
2229
2230/*
2231 * receive packets from downstream mux clients:
2232 * channel callback fired on read from mux client, creates
2233 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2234 * on channel creation.
2235 */
2236int
2237channel_proxy_downstream(Channel *downstream)
2238{
2239 Channel *c = NULL;
2240 struct ssh *ssh = active_state;
2241 struct sshbuf *original = NULL, *modified = NULL;
2242 const u_char *cp;
2243 char *ctype = NULL, *listen_host = NULL;
2244 u_char type;
2245 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002246 int ret = -1, r, idx;
2247 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002248
2249 /* sshbuf_dump(&downstream->input, stderr); */
2250 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2251 != 0) {
2252 error("%s: malformed message: %s", __func__, ssh_err(r));
2253 return -1;
2254 }
2255 if (have < 2) {
2256 error("%s: short message", __func__);
2257 return -1;
2258 }
2259 type = cp[1];
2260 /* skip padlen + type */
2261 cp += 2;
2262 have -= 2;
2263 if (ssh_packet_log_type(type))
2264 debug3("%s: channel %u: down->up: type %u", __func__,
2265 downstream->self, type);
2266
2267 switch (type) {
2268 case SSH2_MSG_CHANNEL_OPEN:
2269 if ((original = sshbuf_from(cp, have)) == NULL ||
2270 (modified = sshbuf_new()) == NULL) {
2271 error("%s: alloc", __func__);
2272 goto out;
2273 }
2274 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2275 (r = sshbuf_get_u32(original, &id)) != 0) {
2276 error("%s: parse error %s", __func__, ssh_err(r));
2277 goto out;
2278 }
2279 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2280 -1, -1, -1, 0, 0, 0, ctype, 1);
2281 c->mux_ctx = downstream; /* point to mux client */
2282 c->mux_downstream_id = id; /* original downstream id */
2283 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2284 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2285 (r = sshbuf_putb(modified, original)) != 0) {
2286 error("%s: compose error %s", __func__, ssh_err(r));
2287 channel_free(c);
2288 goto out;
2289 }
2290 break;
2291 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2292 /*
2293 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2294 * need to parse 'remote_id' instead of 'ctype'.
2295 */
2296 if ((original = sshbuf_from(cp, have)) == NULL ||
2297 (modified = sshbuf_new()) == NULL) {
2298 error("%s: alloc", __func__);
2299 goto out;
2300 }
2301 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2302 (r = sshbuf_get_u32(original, &id)) != 0) {
2303 error("%s: parse error %s", __func__, ssh_err(r));
2304 goto out;
2305 }
2306 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2307 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2308 c->mux_ctx = downstream; /* point to mux client */
2309 c->mux_downstream_id = id;
2310 c->remote_id = remote_id;
2311 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2312 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2313 (r = sshbuf_putb(modified, original)) != 0) {
2314 error("%s: compose error %s", __func__, ssh_err(r));
2315 channel_free(c);
2316 goto out;
2317 }
2318 break;
2319 case SSH2_MSG_GLOBAL_REQUEST:
2320 if ((original = sshbuf_from(cp, have)) == NULL) {
2321 error("%s: alloc", __func__);
2322 goto out;
2323 }
2324 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2325 error("%s: parse error %s", __func__, ssh_err(r));
2326 goto out;
2327 }
2328 if (strcmp(ctype, "tcpip-forward") != 0) {
2329 error("%s: unsupported request %s", __func__, ctype);
2330 goto out;
2331 }
2332 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2333 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2334 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2335 error("%s: parse error %s", __func__, ssh_err(r));
2336 goto out;
2337 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002338 if (listen_port > 65535) {
2339 error("%s: tcpip-forward for %s: bad port %u",
2340 __func__, listen_host, listen_port);
2341 goto out;
2342 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002343 /* Record that connection to this host/port is permitted. */
2344 permitted_opens = xreallocarray(permitted_opens,
2345 num_permitted_opens + 1, sizeof(*permitted_opens));
2346 idx = num_permitted_opens++;
2347 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2348 permitted_opens[idx].port_to_connect = -1;
2349 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002350 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002351 permitted_opens[idx].downstream = downstream;
2352 listen_host = NULL;
2353 break;
2354 case SSH2_MSG_CHANNEL_CLOSE:
2355 if (have < 4)
2356 break;
2357 remote_id = PEEK_U32(cp);
2358 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2359 if (c->flags & CHAN_CLOSE_RCVD)
2360 channel_free(c);
2361 else
2362 c->flags |= CHAN_CLOSE_SENT;
2363 }
2364 break;
2365 }
2366 if (modified) {
2367 if ((r = sshpkt_start(ssh, type)) != 0 ||
2368 (r = sshpkt_putb(ssh, modified)) != 0 ||
2369 (r = sshpkt_send(ssh)) != 0) {
2370 error("%s: send %s", __func__, ssh_err(r));
2371 goto out;
2372 }
2373 } else {
2374 if ((r = sshpkt_start(ssh, type)) != 0 ||
2375 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2376 (r = sshpkt_send(ssh)) != 0) {
2377 error("%s: send %s", __func__, ssh_err(r));
2378 goto out;
2379 }
2380 }
2381 ret = 0;
2382 out:
2383 free(ctype);
2384 free(listen_host);
2385 sshbuf_free(original);
2386 sshbuf_free(modified);
2387 return ret;
2388}
2389
2390/*
2391 * receive packets from upstream server and de-multiplex packets
2392 * to correct downstream:
2393 * implemented as a helper for channel input handlers,
2394 * replaces local (proxy) channel ID with downstream channel ID.
2395 */
2396int
2397channel_proxy_upstream(Channel *c, int type, u_int32_t seq, void *ctxt)
2398{
2399 struct ssh *ssh = active_state;
2400 struct sshbuf *b = NULL;
2401 Channel *downstream;
2402 const u_char *cp = NULL;
2403 size_t len;
2404 int r;
2405
2406 /*
2407 * When receiving packets from the peer we need to check whether we
2408 * need to forward the packets to the mux client. In this case we
2409 * restore the orignal channel id and keep track of CLOSE messages,
2410 * so we can cleanup the channel.
2411 */
2412 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2413 return 0;
2414 if ((downstream = c->mux_ctx) == NULL)
2415 return 0;
2416 switch (type) {
2417 case SSH2_MSG_CHANNEL_CLOSE:
2418 case SSH2_MSG_CHANNEL_DATA:
2419 case SSH2_MSG_CHANNEL_EOF:
2420 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2421 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2422 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2423 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2424 case SSH2_MSG_CHANNEL_SUCCESS:
2425 case SSH2_MSG_CHANNEL_FAILURE:
2426 case SSH2_MSG_CHANNEL_REQUEST:
2427 break;
2428 default:
2429 debug2("%s: channel %u: unsupported type %u", __func__,
2430 c->self, type);
2431 return 0;
2432 }
2433 if ((b = sshbuf_new()) == NULL) {
2434 error("%s: alloc reply", __func__);
2435 goto out;
2436 }
2437 /* get remaining payload (after id) */
2438 cp = sshpkt_ptr(ssh, &len);
2439 if (cp == NULL) {
2440 error("%s: no packet", __func__);
2441 goto out;
2442 }
2443 /* translate id and send to muxclient */
2444 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2445 (r = sshbuf_put_u8(b, type)) != 0 ||
2446 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2447 (r = sshbuf_put(b, cp, len)) != 0 ||
2448 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2449 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2450 goto out;
2451 }
2452 /* sshbuf_dump(b, stderr); */
2453 if (ssh_packet_log_type(type))
2454 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2455 type);
2456 out:
2457 /* update state */
2458 switch (type) {
2459 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2460 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2461 if (cp && len > 4)
2462 c->remote_id = PEEK_U32(cp);
2463 break;
2464 case SSH2_MSG_CHANNEL_CLOSE:
2465 if (c->flags & CHAN_CLOSE_SENT)
2466 channel_free(c);
2467 else
2468 c->flags |= CHAN_CLOSE_RCVD;
2469 break;
2470 }
2471 sshbuf_free(b);
2472 return 1;
2473}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002474
2475/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002476
2477/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002478int
Damien Miller630d6f42002-01-22 23:17:30 +11002479channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002480{
Damien Miller34132e52000-01-14 15:45:46 +11002481 int id;
Damien Miller633de332014-05-15 13:48:26 +10002482 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002483 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002484 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002485
Damien Miller95def091999-11-25 00:26:21 +11002486 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002487 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002488 c = channel_lookup(id);
2489 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002490 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002491 if (channel_proxy_upstream(c, type, seq, ctxt))
2492 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002493
Damien Miller95def091999-11-25 00:26:21 +11002494 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002495 if (c->type != SSH_CHANNEL_OPEN &&
2496 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002497 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002498
Damien Miller95def091999-11-25 00:26:21 +11002499 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002500 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002501 win_len = data_len;
2502 if (c->datagram)
2503 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002504
Damien Millera04ad492004-01-21 11:02:09 +11002505 /*
2506 * Ignore data for protocol > 1.3 if output end is no longer open.
2507 * For protocol 2 the sending side is reducing its window as it sends
2508 * data, so we must 'fake' consumption of the data in order to ensure
2509 * that window updates are sent back. Otherwise the connection might
2510 * deadlock.
2511 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002512 if (c->ostate != CHAN_OUTPUT_OPEN) {
2513 c->local_window -= win_len;
2514 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002515 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002516 }
2517
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002518 if (win_len > c->local_maxpacket) {
2519 logit("channel %d: rcvd big packet %d, maxpack %d",
2520 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002521 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002522 if (win_len > c->local_window) {
2523 logit("channel %d: rcvd too much data %d, win %d",
2524 c->self, win_len, c->local_window);
2525 return 0;
2526 }
2527 c->local_window -= win_len;
2528
Damien Millerd27b9472005-12-13 19:29:02 +11002529 if (c->datagram)
2530 buffer_put_string(&c->output, data, data_len);
2531 else
2532 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002533 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002534 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002535}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002536
Damien Millerd79b4242006-03-31 23:11:44 +11002537/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002538int
Damien Miller630d6f42002-01-22 23:17:30 +11002539channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002540{
2541 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002542 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002543 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002544 Channel *c;
2545
2546 /* Get the channel number and verify it. */
2547 id = packet_get_int();
2548 c = channel_lookup(id);
2549
2550 if (c == NULL)
2551 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002552 if (channel_proxy_upstream(c, type, seq, ctxt))
2553 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002554 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002555 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002556 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002557 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002558 if (c->flags & CHAN_EOF_RCVD) {
2559 if (datafellows & SSH_BUG_EXTEOF)
2560 debug("channel %d: accepting ext data after eof", id);
2561 else
2562 packet_disconnect("Received extended_data after EOF "
2563 "on channel %d.", id);
2564 }
Damien Miller33b13562000-04-04 14:38:59 +10002565 tcode = packet_get_int();
2566 if (c->efd == -1 ||
2567 c->extended_usage != CHAN_EXTENDED_WRITE ||
2568 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002569 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002570 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002571 }
2572 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002573 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002574 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002575 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002576 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002577 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002578 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002579 }
Damien Millerd3444942000-09-30 14:20:03 +11002580 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002581 c->local_window -= data_len;
2582 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002583 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002584 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002585}
2586
Damien Millerd79b4242006-03-31 23:11:44 +11002587/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002588int
Damien Miller630d6f42002-01-22 23:17:30 +11002589channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002590{
2591 int id;
2592 Channel *c;
2593
Damien Millerb38eff82000-04-01 11:09:21 +10002594 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002595 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002596 c = channel_lookup(id);
2597 if (c == NULL)
2598 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002599 if (channel_proxy_upstream(c, type, seq, ctxt))
2600 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002601 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002602
2603 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002604 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002605 debug("channel %d: FORCE input drain", c->self);
2606 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002607 if (buffer_len(&c->input) == 0)
2608 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002609 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002610 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002611}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002612
Damien Millerd79b4242006-03-31 23:11:44 +11002613/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002614int
Damien Miller630d6f42002-01-22 23:17:30 +11002615channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002616{
Damien Millerb38eff82000-04-01 11:09:21 +10002617 int id = packet_get_int();
2618 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002619
Damien Millerb38eff82000-04-01 11:09:21 +10002620 if (c == NULL)
2621 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002622 if (channel_proxy_upstream(c, type, seq, ctxt))
2623 return 0;
2624 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002625 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002626 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002627}
2628
Damien Millerd79b4242006-03-31 23:11:44 +11002629/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002630int
Damien Miller630d6f42002-01-22 23:17:30 +11002631channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002632{
Damien Millerb38eff82000-04-01 11:09:21 +10002633 int id, remote_id;
2634 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002635
Damien Millerb38eff82000-04-01 11:09:21 +10002636 id = packet_get_int();
2637 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002638
markus@openbsd.org8d057842016-09-30 09:19:13 +00002639 if (c==NULL)
2640 packet_disconnect("Received open confirmation for "
2641 "unknown channel %d.", id);
2642 if (channel_proxy_upstream(c, type, seq, ctxt))
2643 return 0;
2644 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002645 packet_disconnect("Received open confirmation for "
2646 "non-opening channel %d.", id);
2647 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002648 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002649 c->remote_id = remote_id;
2650 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002651
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002652 c->remote_window = packet_get_int();
2653 c->remote_maxpacket = packet_get_int();
2654 if (c->open_confirm) {
2655 debug2("callback start");
2656 c->open_confirm(c->self, 1, c->open_confirm_ctx);
2657 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002658 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002659 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2660 c->remote_window, c->remote_maxpacket);
Damien Miller48b03fc2002-01-22 23:11:40 +11002661 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002662 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002663}
2664
Ben Lindstrombba81212001-06-25 05:01:22 +00002665static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002666reason2txt(int reason)
2667{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002668 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002669 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2670 return "administratively prohibited";
2671 case SSH2_OPEN_CONNECT_FAILED:
2672 return "connect failed";
2673 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2674 return "unknown channel type";
2675 case SSH2_OPEN_RESOURCE_SHORTAGE:
2676 return "resource shortage";
2677 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002678 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002679}
2680
Damien Millerd79b4242006-03-31 23:11:44 +11002681/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002682int
Damien Miller630d6f42002-01-22 23:17:30 +11002683channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002684{
Kevin Steves12057502001-02-05 14:54:34 +00002685 int id, reason;
2686 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002687 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002688
Damien Millerb38eff82000-04-01 11:09:21 +10002689 id = packet_get_int();
2690 c = channel_lookup(id);
2691
markus@openbsd.org8d057842016-09-30 09:19:13 +00002692 if (c==NULL)
2693 packet_disconnect("Received open failure for "
2694 "unknown channel %d.", id);
2695 if (channel_proxy_upstream(c, type, seq, ctxt))
2696 return 0;
2697 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002698 packet_disconnect("Received open failure for "
2699 "non-opening channel %d.", id);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002700 reason = packet_get_int();
2701 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2702 msg = packet_get_string(NULL);
2703 lang = packet_get_string(NULL);
2704 }
2705 logit("channel %d: open failed: %s%s%s", id,
2706 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2707 free(msg);
2708 free(lang);
2709 if (c->open_confirm) {
2710 debug2("callback start");
2711 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2712 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002713 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002714 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002715 /* Schedule the channel for cleanup/deletion. */
2716 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002717 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002718}
2719
Damien Millerd79b4242006-03-31 23:11:44 +11002720/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002721int
Damien Miller630d6f42002-01-22 23:17:30 +11002722channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002723{
2724 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002725 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00002726 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10002727
Damien Miller33b13562000-04-04 14:38:59 +10002728 /* Get the channel number and verify it. */
2729 id = packet_get_int();
2730 c = channel_lookup(id);
2731
Damien Millerd47c62a2005-12-13 19:33:57 +11002732 if (c == NULL) {
2733 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002734 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002735 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002736 if (channel_proxy_upstream(c, type, seq, ctxt))
2737 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002738 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002739 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002740 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00002741 if ((tmp = c->remote_window + adjust) < c->remote_window)
2742 fatal("channel %d: adjust %u overflows remote window %u",
2743 id, adjust, c->remote_window);
2744 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002745 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002746}
2747
Damien Millerd79b4242006-03-31 23:11:44 +11002748/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002749int
Damien Millerb84886b2008-05-19 15:05:07 +10002750channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2751{
2752 Channel *c;
2753 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002754 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002755
2756 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002757 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002758
Damien Miller16a73072008-12-08 09:55:25 +11002759 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11002760 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002761
Damien Miller16a73072008-12-08 09:55:25 +11002762 if ((c = channel_lookup(id)) == NULL) {
2763 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002764 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002765 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002766 if (channel_proxy_upstream(c, type, seq, ctxt))
2767 return 0;
2768 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10002769 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002770 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002771 cc->cb(type, c, cc->ctx);
2772 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002773 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002774 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002775 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002776}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002777
Ben Lindstrome9c99912001-06-09 00:41:05 +00002778/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002779
Ben Lindstrom908afed2001-10-03 17:34:59 +00002780void
2781channel_set_af(int af)
2782{
2783 IPv4or6 = af;
2784}
2785
Damien Millerf6dff7c2011-09-22 21:38:52 +10002786
2787/*
2788 * Determine whether or not a port forward listens to loopback, the
2789 * specified address or wildcard. On the client, a specified bind
2790 * address will always override gateway_ports. On the server, a
2791 * gateway_ports of 1 (``yes'') will override the client's specification
2792 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2793 * will bind to whatever address the client asked for.
2794 *
2795 * Special-case listen_addrs are:
2796 *
2797 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2798 * "" (empty string), "*" -> wildcard v4/v6
2799 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002800 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002801 */
2802static const char *
2803channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002804 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002805{
2806 const char *addr = NULL;
2807 int wildcard = 0;
2808
2809 if (listen_addr == NULL) {
2810 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002811 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002812 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002813 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002814 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2815 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2816 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002817 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002818 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002819 /*
2820 * Notify client if they requested a specific listen
2821 * address and it was overridden.
2822 */
2823 if (*listen_addr != '\0' &&
2824 strcmp(listen_addr, "0.0.0.0") != 0 &&
2825 strcmp(listen_addr, "*") != 0) {
2826 packet_send_debug("Forwarding listen address "
2827 "\"%s\" overridden by server "
2828 "GatewayPorts", listen_addr);
2829 }
Damien Miller602943d2014-07-04 08:59:41 +10002830 } else if (strcmp(listen_addr, "localhost") != 0 ||
2831 strcmp(listen_addr, "127.0.0.1") == 0 ||
2832 strcmp(listen_addr, "::1") == 0) {
2833 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002834 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002835 }
2836 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2837 strcmp(listen_addr, "::1") == 0) {
2838 /*
2839 * If a specific IPv4/IPv6 localhost address has been
2840 * requested then accept it even if gateway_ports is in
2841 * effect. This allows the client to prefer IPv4 or IPv6.
2842 */
2843 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002844 }
2845 if (wildcardp != NULL)
2846 *wildcardp = wildcard;
2847 return addr;
2848}
2849
Damien Millerb16461c2002-01-22 23:29:22 +11002850static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002851channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2852 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002853{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002854 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002855 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002856 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002857 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002858 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002859 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002860
Damien Millerf91ee4c2005-03-01 21:24:33 +11002861 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002862
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00002863 if (is_client && fwd->connect_path != NULL) {
2864 host = fwd->connect_path;
2865 } else {
2866 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2867 fwd->listen_host : fwd->connect_host;
2868 if (host == NULL) {
2869 error("No forward host name.");
2870 return 0;
2871 }
2872 if (strlen(host) >= NI_MAXHOST) {
2873 error("Forward host name too long.");
2874 return 0;
2875 }
Kevin Steves12057502001-02-05 14:54:34 +00002876 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002877
Damien Millerf6dff7c2011-09-22 21:38:52 +10002878 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002879 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2880 is_client, fwd_opts);
2881 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002882 type, wildcard, (addr == NULL) ? "NULL" : addr);
2883
2884 /*
Damien Miller34132e52000-01-14 15:45:46 +11002885 * getaddrinfo returns a loopback address if the hostname is
2886 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002887 */
Damien Miller34132e52000-01-14 15:45:46 +11002888 memset(&hints, 0, sizeof(hints));
2889 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002890 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002891 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002892 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002893 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2894 if (addr == NULL) {
2895 /* This really shouldn't happen */
2896 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002897 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002898 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002899 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11002900 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002901 }
Damien Millera7270302005-07-06 09:36:05 +10002902 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002903 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002904 if (allocated_listen_port != NULL)
2905 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11002906 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11002907 switch (ai->ai_family) {
2908 case AF_INET:
2909 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2910 sin_port;
2911 break;
2912 case AF_INET6:
2913 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2914 sin6_port;
2915 break;
2916 default:
Damien Miller34132e52000-01-14 15:45:46 +11002917 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11002918 }
2919 /*
2920 * If allocating a port for -R forwards, then use the
2921 * same port for all address families.
2922 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002923 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002924 allocated_listen_port != NULL && *allocated_listen_port > 0)
2925 *lport_p = htons(*allocated_listen_port);
2926
Damien Miller34132e52000-01-14 15:45:46 +11002927 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2928 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10002929 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11002930 continue;
2931 }
2932 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11002933 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002934 if (sock < 0) {
2935 /* this is no error since kernel may not support ipv6 */
2936 verbose("socket: %.100s", strerror(errno));
2937 continue;
2938 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002939
2940 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11002941 if (ai->ai_family == AF_INET6)
2942 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002943
Damien Miller4bf648f2009-02-14 16:28:21 +11002944 debug("Local forwarding listening on %s port %s.",
2945 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002946
Damien Miller34132e52000-01-14 15:45:46 +11002947 /* Bind the socket to the address. */
2948 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2949 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002950 if (!ai->ai_next)
2951 error("bind: %.100s", strerror(errno));
2952 else
2953 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002954
Damien Miller34132e52000-01-14 15:45:46 +11002955 close(sock);
2956 continue;
2957 }
2958 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002959 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002960 error("listen: %.100s", strerror(errno));
2961 close(sock);
2962 continue;
2963 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002964
2965 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10002966 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11002967 * record what we got.
2968 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002969 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002970 allocated_listen_port != NULL &&
2971 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00002972 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11002973 debug("Allocated listen port %d",
2974 *allocated_listen_port);
2975 }
2976
Damien Miller34132e52000-01-14 15:45:46 +11002977 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002978 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002979 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002980 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11002981 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10002982 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002983 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10002984 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11002985 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
2986 c->listening_port = *allocated_listen_port;
2987 else
Damien Miller7acefbb2014-07-18 14:11:24 +10002988 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002989 success = 1;
2990 }
2991 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10002992 error("%s: cannot listen to port: %d", __func__,
2993 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002994 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002995 return success;
Damien Miller95def091999-11-25 00:26:21 +11002996}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002997
Damien Miller7acefbb2014-07-18 14:11:24 +10002998static int
2999channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3000 struct ForwardOptions *fwd_opts)
3001{
3002 struct sockaddr_un sunaddr;
3003 const char *path;
3004 Channel *c;
3005 int port, sock;
3006 mode_t omask;
3007
3008 switch (type) {
3009 case SSH_CHANNEL_UNIX_LISTENER:
3010 if (fwd->connect_path != NULL) {
3011 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3012 error("Local connecting path too long: %s",
3013 fwd->connect_path);
3014 return 0;
3015 }
3016 path = fwd->connect_path;
3017 port = PORT_STREAMLOCAL;
3018 } else {
3019 if (fwd->connect_host == NULL) {
3020 error("No forward host name.");
3021 return 0;
3022 }
3023 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3024 error("Forward host name too long.");
3025 return 0;
3026 }
3027 path = fwd->connect_host;
3028 port = fwd->connect_port;
3029 }
3030 break;
3031 case SSH_CHANNEL_RUNIX_LISTENER:
3032 path = fwd->listen_path;
3033 port = PORT_STREAMLOCAL;
3034 break;
3035 default:
3036 error("%s: unexpected channel type %d", __func__, type);
3037 return 0;
3038 }
3039
3040 if (fwd->listen_path == NULL) {
3041 error("No forward path name.");
3042 return 0;
3043 }
3044 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3045 error("Local listening path too long: %s", fwd->listen_path);
3046 return 0;
3047 }
3048
3049 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3050
3051 /* Start a Unix domain listener. */
3052 omask = umask(fwd_opts->streamlocal_bind_mask);
3053 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3054 fwd_opts->streamlocal_bind_unlink);
3055 umask(omask);
3056 if (sock < 0)
3057 return 0;
3058
3059 debug("Local forwarding listening on path %s.", fwd->listen_path);
3060
3061 /* Allocate a channel number for the socket. */
3062 c = channel_new("unix listener", type, sock, sock, -1,
3063 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3064 0, "unix listener", 1);
3065 c->path = xstrdup(path);
3066 c->host_port = port;
3067 c->listening_port = PORT_STREAMLOCAL;
3068 c->listening_addr = xstrdup(fwd->listen_path);
3069 return 1;
3070}
3071
3072static int
3073channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003074{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003075 u_int i;
3076 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003077
Darren Tucker47eede72005-03-14 23:08:12 +11003078 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003079 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003080 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3081 continue;
3082 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3083 debug2("%s: close channel %d", __func__, i);
3084 channel_free(c);
3085 found = 1;
3086 }
3087 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003088
Damien Millerf6dff7c2011-09-22 21:38:52 +10003089 return (found);
3090}
3091
Damien Miller7acefbb2014-07-18 14:11:24 +10003092static int
3093channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003094{
3095 u_int i;
3096 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003097
3098 for (i = 0; i < channels_alloc; i++) {
3099 Channel *c = channels[i];
3100 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3101 continue;
3102 if (c->path == NULL)
3103 continue;
3104 if (strcmp(c->path, path) == 0) {
3105 debug2("%s: close channel %d", __func__, i);
3106 channel_free(c);
3107 found = 1;
3108 }
3109 }
3110
3111 return (found);
3112}
3113
3114int
3115channel_cancel_rport_listener(struct Forward *fwd)
3116{
3117 if (fwd->listen_path != NULL)
3118 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3119 else
3120 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3121}
3122
3123static int
3124channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3125 int cport, struct ForwardOptions *fwd_opts)
3126{
3127 u_int i;
3128 int found = 0;
3129 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003130
3131 for (i = 0; i < channels_alloc; i++) {
3132 Channel *c = channels[i];
3133 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3134 continue;
Damien Millerff773642011-09-22 21:39:48 +10003135 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003136 continue;
Damien Millerff773642011-09-22 21:39:48 +10003137 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3138 /* skip dynamic forwardings */
3139 if (c->host_port == 0)
3140 continue;
3141 } else {
3142 if (c->host_port != cport)
3143 continue;
3144 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003145 if ((c->listening_addr == NULL && addr != NULL) ||
3146 (c->listening_addr != NULL && addr == NULL))
3147 continue;
3148 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003149 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003150 channel_free(c);
3151 found = 1;
3152 }
3153 }
3154
3155 return (found);
3156}
3157
Damien Miller7acefbb2014-07-18 14:11:24 +10003158static int
3159channel_cancel_lport_listener_streamlocal(const char *path)
3160{
3161 u_int i;
3162 int found = 0;
3163
3164 if (path == NULL) {
3165 error("%s: no path specified.", __func__);
3166 return 0;
3167 }
3168
3169 for (i = 0; i < channels_alloc; i++) {
3170 Channel *c = channels[i];
3171 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3172 continue;
3173 if (c->listening_addr == NULL)
3174 continue;
3175 if (strcmp(c->listening_addr, path) == 0) {
3176 debug2("%s: close channel %d", __func__, i);
3177 channel_free(c);
3178 found = 1;
3179 }
3180 }
3181
3182 return (found);
3183}
3184
3185int
3186channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3187{
3188 if (fwd->listen_path != NULL)
3189 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3190 else
3191 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3192}
3193
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003194/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003195int
Damien Miller7acefbb2014-07-18 14:11:24 +10003196channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003197{
Damien Miller7acefbb2014-07-18 14:11:24 +10003198 if (fwd->listen_path != NULL) {
3199 return channel_setup_fwd_listener_streamlocal(
3200 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3201 } else {
3202 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3203 fwd, NULL, fwd_opts);
3204 }
Damien Millerb16461c2002-01-22 23:29:22 +11003205}
3206
3207/* protocol v2 remote port fwd, used by sshd */
3208int
Damien Miller7acefbb2014-07-18 14:11:24 +10003209channel_setup_remote_fwd_listener(struct Forward *fwd,
3210 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003211{
Damien Miller7acefbb2014-07-18 14:11:24 +10003212 if (fwd->listen_path != NULL) {
3213 return channel_setup_fwd_listener_streamlocal(
3214 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3215 } else {
3216 return channel_setup_fwd_listener_tcpip(
3217 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3218 fwd_opts);
3219 }
Damien Millerb16461c2002-01-22 23:29:22 +11003220}
3221
Damien Miller5428f641999-11-25 11:54:57 +11003222/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003223 * Translate the requested rfwd listen host to something usable for
3224 * this server.
3225 */
3226static const char *
3227channel_rfwd_bind_host(const char *listen_host)
3228{
3229 if (listen_host == NULL) {
3230 if (datafellows & SSH_BUG_RFWD_ADDR)
3231 return "127.0.0.1";
3232 else
3233 return "localhost";
3234 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3235 if (datafellows & SSH_BUG_RFWD_ADDR)
3236 return "0.0.0.0";
3237 else
3238 return "";
3239 } else
3240 return listen_host;
3241}
3242
3243/*
Damien Miller5428f641999-11-25 11:54:57 +11003244 * Initiate forwarding of connections to port "port" on remote host through
3245 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003246 * Returns handle (index) for updating the dynamic listen port with
3247 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003248 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003249int
Damien Miller7acefbb2014-07-18 14:11:24 +10003250channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003251{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003252 int success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003253
Damien Miller95def091999-11-25 00:26:21 +11003254 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003255 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3256 if (fwd->listen_path != NULL) {
3257 packet_put_cstring("streamlocal-forward@openssh.com");
3258 packet_put_char(1); /* boolean: want reply */
3259 packet_put_cstring(fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003260 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003261 packet_put_cstring("tcpip-forward");
3262 packet_put_char(1); /* boolean: want reply */
3263 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3264 packet_put_int(fwd->listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11003265 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003266 packet_send();
3267 packet_write_wait();
3268 /* Assume that server accepts the request */
3269 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003270 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003271 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003272 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003273 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003274 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003275 if (fwd->connect_path != NULL) {
3276 permitted_opens[idx].host_to_connect =
3277 xstrdup(fwd->connect_path);
3278 permitted_opens[idx].port_to_connect =
3279 PORT_STREAMLOCAL;
3280 } else {
3281 permitted_opens[idx].host_to_connect =
3282 xstrdup(fwd->connect_host);
3283 permitted_opens[idx].port_to_connect =
3284 fwd->connect_port;
3285 }
3286 if (fwd->listen_path != NULL) {
3287 permitted_opens[idx].listen_host = NULL;
3288 permitted_opens[idx].listen_path =
3289 xstrdup(fwd->listen_path);
3290 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3291 } else {
3292 permitted_opens[idx].listen_host =
3293 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3294 permitted_opens[idx].listen_path = NULL;
3295 permitted_opens[idx].listen_port = fwd->listen_port;
3296 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003297 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003298 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003299 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003300}
3301
Damien Miller4b3ed642014-07-02 15:29:40 +10003302static int
3303open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003304 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003305{
3306 if (allowed_open->host_to_connect == NULL)
3307 return 0;
3308 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3309 allowed_open->port_to_connect != requestedport)
3310 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003311 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3312 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003313 return 0;
3314 return 1;
3315}
3316
3317/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003318 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003319 * we don't support FWD_PERMIT_ANY_PORT and
3320 * need to translate between the configured-host (listen_host)
3321 * and what we've sent to the remote server (channel_rfwd_bind_host)
3322 */
3323static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003324open_listen_match_tcpip(ForwardPermission *allowed_open,
3325 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003326{
3327 const char *allowed_host;
3328
3329 if (allowed_open->host_to_connect == NULL)
3330 return 0;
3331 if (allowed_open->listen_port != requestedport)
3332 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003333 if (!translate && allowed_open->listen_host == NULL &&
3334 requestedhost == NULL)
3335 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003336 allowed_host = translate ?
3337 channel_rfwd_bind_host(allowed_open->listen_host) :
3338 allowed_open->listen_host;
3339 if (allowed_host == NULL ||
3340 strcmp(allowed_host, requestedhost) != 0)
3341 return 0;
3342 return 1;
3343}
3344
Damien Miller7acefbb2014-07-18 14:11:24 +10003345static int
3346open_listen_match_streamlocal(ForwardPermission *allowed_open,
3347 const char *requestedpath)
3348{
3349 if (allowed_open->host_to_connect == NULL)
3350 return 0;
3351 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3352 return 0;
3353 if (allowed_open->listen_path == NULL ||
3354 strcmp(allowed_open->listen_path, requestedpath) != 0)
3355 return 0;
3356 return 1;
3357}
3358
Damien Miller5428f641999-11-25 11:54:57 +11003359/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003360 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003361 * local side.
3362 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003363static int
3364channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003365{
3366 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003367
Darren Tuckere7066df2004-05-24 10:18:05 +10003368 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003369 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003370 break;
3371 }
3372 if (i >= num_permitted_opens) {
3373 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003374 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003375 }
3376 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3377 packet_put_cstring("cancel-tcpip-forward");
3378 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003379 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003380 packet_put_int(port);
3381 packet_send();
3382
Damien Miller4b3ed642014-07-02 15:29:40 +10003383 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003384 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003385 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003386 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003387 free(permitted_opens[i].listen_host);
3388 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003389 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003390 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003391
3392 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003393}
3394
3395/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003396 * Request cancellation of remote forwarding of Unix domain socket
3397 * path from local side.
3398 */
3399static int
3400channel_request_rforward_cancel_streamlocal(const char *path)
3401{
3402 int i;
3403
Damien Miller7acefbb2014-07-18 14:11:24 +10003404 for (i = 0; i < num_permitted_opens; i++) {
3405 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3406 break;
3407 }
3408 if (i >= num_permitted_opens) {
3409 debug("%s: requested forward not found", __func__);
3410 return -1;
3411 }
3412 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3413 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3414 packet_put_char(0);
3415 packet_put_cstring(path);
3416 packet_send();
3417
3418 permitted_opens[i].listen_port = 0;
3419 permitted_opens[i].port_to_connect = 0;
3420 free(permitted_opens[i].host_to_connect);
3421 permitted_opens[i].host_to_connect = NULL;
3422 permitted_opens[i].listen_host = NULL;
3423 free(permitted_opens[i].listen_path);
3424 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003425 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003426
3427 return 0;
3428}
3429
3430/*
3431 * Request cancellation of remote forwarding of a connection from local side.
3432 */
3433int
3434channel_request_rforward_cancel(struct Forward *fwd)
3435{
3436 if (fwd->listen_path != NULL) {
3437 return (channel_request_rforward_cancel_streamlocal(
3438 fwd->listen_path));
3439 } else {
3440 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3441 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3442 }
3443}
3444
3445/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003446 * Permits opening to any host/port if permitted_opens[] is empty. This is
3447 * usually called by the server, because the user could connect to any port
3448 * anyway, and the server has no way to know but to trust the client anyway.
3449 */
3450void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003451channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003452{
3453 if (num_permitted_opens == 0)
3454 all_opens_permitted = 1;
3455}
3456
Ben Lindstroma3700052001-04-05 23:26:32 +00003457void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003458channel_add_permitted_opens(char *host, int port)
3459{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003460 debug("allow port forwarding to host %s port %d", host, port);
3461
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003462 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003463 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003464 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3465 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003466 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003467 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003468 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003469 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003470 num_permitted_opens++;
3471
3472 all_opens_permitted = 0;
3473}
3474
Darren Tucker68afb8c2011-10-02 18:59:03 +11003475/*
3476 * Update the listen port for a dynamic remote forward, after
3477 * the actual 'newport' has been allocated. If 'newport' < 0 is
3478 * passed then they entry will be invalidated.
3479 */
3480void
3481channel_update_permitted_opens(int idx, int newport)
3482{
3483 if (idx < 0 || idx >= num_permitted_opens) {
3484 debug("channel_update_permitted_opens: index out of range:"
3485 " %d num_permitted_opens %d", idx, num_permitted_opens);
3486 return;
3487 }
3488 debug("%s allowed port %d for forwarding to host %s port %d",
3489 newport > 0 ? "Updating" : "Removing",
3490 newport,
3491 permitted_opens[idx].host_to_connect,
3492 permitted_opens[idx].port_to_connect);
3493 if (newport >= 0) {
3494 permitted_opens[idx].listen_port =
3495 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3496 } else {
3497 permitted_opens[idx].listen_port = 0;
3498 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003499 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003500 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003501 free(permitted_opens[idx].listen_host);
3502 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003503 free(permitted_opens[idx].listen_path);
3504 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003505 }
3506}
3507
Damien Millera765cf42006-07-24 14:08:13 +10003508int
Damien Miller9b439df2006-07-24 14:04:00 +10003509channel_add_adm_permitted_opens(char *host, int port)
3510{
Damien Millera765cf42006-07-24 14:08:13 +10003511 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003512
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003513 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003514 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003515 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3516 = xstrdup(host);
3517 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003518 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003519 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003520 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003521 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003522}
3523
3524void
Damien Millerc6081482012-04-22 11:18:53 +10003525channel_disable_adm_local_opens(void)
3526{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003527 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003528 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003529 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3530 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003531}
3532
3533void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003534channel_clear_permitted_opens(void)
3535{
3536 int i;
3537
Damien Miller4b3ed642014-07-02 15:29:40 +10003538 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003539 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003540 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003541 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003542 }
Darren Tuckera627d422013-06-02 07:31:17 +10003543 free(permitted_opens);
3544 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003545 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003546}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003547
Damien Miller9b439df2006-07-24 14:04:00 +10003548void
3549channel_clear_adm_permitted_opens(void)
3550{
3551 int i;
3552
Damien Miller4b3ed642014-07-02 15:29:40 +10003553 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003554 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003555 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003556 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003557 }
Darren Tuckera627d422013-06-02 07:31:17 +10003558 free(permitted_adm_opens);
3559 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003560 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003561}
3562
Darren Tuckere7140f22008-06-10 23:01:51 +10003563void
3564channel_print_adm_permitted_opens(void)
3565{
Damien Miller6ef17492008-07-16 22:42:06 +10003566 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003567
Damien Millerb53d8a12009-01-28 16:13:04 +11003568 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003569 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003570 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003571 return;
3572 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003573 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003574 if (permitted_adm_opens[i].host_to_connect == NULL)
3575 printf(" none");
3576 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003577 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3578 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003579 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003580}
3581
Darren Tucker1338b9e2011-10-02 18:57:35 +11003582/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3583int
3584permitopen_port(const char *p)
3585{
3586 int port;
3587
3588 if (strcmp(p, "*") == 0)
3589 return FWD_PERMIT_ANY_PORT;
3590 if ((port = a2port(p)) > 0)
3591 return port;
3592 return -1;
3593}
3594
Damien Millerbd740252008-05-19 15:37:09 +10003595/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003596static int
Damien Millerbd740252008-05-19 15:37:09 +10003597connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003598{
Damien Millerbd740252008-05-19 15:37:09 +10003599 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003600 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00003601 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003602
Damien Millerbd740252008-05-19 15:37:09 +10003603 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003604 switch (cctx->ai->ai_family) {
3605 case AF_UNIX:
3606 /* unix:pathname instead of host:port */
3607 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3608 strlcpy(ntop, "unix", sizeof(ntop));
3609 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3610 break;
3611 case AF_INET:
3612 case AF_INET6:
3613 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3614 ntop, sizeof(ntop), strport, sizeof(strport),
3615 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3616 error("connect_next: getnameinfo failed");
3617 continue;
3618 }
3619 break;
3620 default:
Damien Miller34132e52000-01-14 15:45:46 +11003621 continue;
3622 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003623 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3624 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003625 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003626 error("socket: %.100s", strerror(errno));
3627 else
3628 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003629 continue;
3630 }
Damien Miller232711f2004-06-15 10:35:30 +10003631 if (set_nonblock(sock) == -1)
3632 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003633 if (connect(sock, cctx->ai->ai_addr,
3634 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3635 debug("connect_next: host %.100s ([%.100s]:%s): "
3636 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003637 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003638 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003639 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003640 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003641 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003642 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003643 if (cctx->ai->ai_family != AF_UNIX)
3644 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003645 debug("connect_next: host %.100s ([%.100s]:%s) "
3646 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3647 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003648 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003649 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003650 return -1;
3651}
Damien Millerb38eff82000-04-01 11:09:21 +10003652
Damien Millerbd740252008-05-19 15:37:09 +10003653static void
3654channel_connect_ctx_free(struct channel_connect *cctx)
3655{
Darren Tuckera627d422013-06-02 07:31:17 +10003656 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003657 if (cctx->aitop) {
3658 if (cctx->aitop->ai_family == AF_UNIX)
3659 free(cctx->aitop);
3660 else
3661 freeaddrinfo(cctx->aitop);
3662 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003663 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003664}
3665
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003666/*
3667 * Return CONNECTING channel to remote host:port or local socket path,
3668 * passing back the failure reason if appropriate.
3669 */
Damien Millerbd740252008-05-19 15:37:09 +10003670static Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003671connect_to_reason(const char *name, int port, char *ctype, char *rname,
3672 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003673{
3674 struct addrinfo hints;
3675 int gaierr;
3676 int sock = -1;
3677 char strport[NI_MAXSERV];
3678 struct channel_connect cctx;
3679 Channel *c;
3680
Damien Miller2bcb8662008-07-12 17:12:29 +10003681 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003682
3683 if (port == PORT_STREAMLOCAL) {
3684 struct sockaddr_un *sunaddr;
3685 struct addrinfo *ai;
3686
3687 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3688 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3689 return (NULL);
3690 }
3691
3692 /*
3693 * Fake up a struct addrinfo for AF_UNIX connections.
3694 * channel_connect_ctx_free() must check ai_family
3695 * and use free() not freeaddirinfo() for AF_UNIX.
3696 */
3697 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3698 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3699 ai->ai_addr = (struct sockaddr *)(ai + 1);
3700 ai->ai_addrlen = sizeof(*sunaddr);
3701 ai->ai_family = AF_UNIX;
3702 ai->ai_socktype = SOCK_STREAM;
3703 ai->ai_protocol = PF_UNSPEC;
3704 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3705 sunaddr->sun_family = AF_UNIX;
3706 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3707 cctx.aitop = ai;
3708 } else {
3709 memset(&hints, 0, sizeof(hints));
3710 hints.ai_family = IPv4or6;
3711 hints.ai_socktype = SOCK_STREAM;
3712 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003713 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3714 != 0) {
3715 if (errmsg != NULL)
3716 *errmsg = ssh_gai_strerror(gaierr);
3717 if (reason != NULL)
3718 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003719 error("connect_to %.100s: unknown host (%s)", name,
3720 ssh_gai_strerror(gaierr));
3721 return NULL;
3722 }
Damien Millerbd740252008-05-19 15:37:09 +10003723 }
3724
Damien Miller7acefbb2014-07-18 14:11:24 +10003725 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003726 cctx.port = port;
3727 cctx.ai = cctx.aitop;
3728
3729 if ((sock = connect_next(&cctx)) == -1) {
3730 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003731 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003732 channel_connect_ctx_free(&cctx);
3733 return NULL;
3734 }
3735 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3736 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3737 c->connect_ctx = cctx;
3738 return c;
3739}
3740
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003741/* Return CONNECTING channel to remote host:port or local socket path */
3742static Channel *
3743connect_to(const char *name, int port, char *ctype, char *rname)
3744{
3745 return connect_to_reason(name, port, ctype, rname, NULL, NULL);
3746}
3747
markus@openbsd.org8d057842016-09-30 09:19:13 +00003748/*
3749 * returns either the newly connected channel or the downstream channel
3750 * that needs to deal with this connection.
3751 */
Damien Millerbd740252008-05-19 15:37:09 +10003752Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003753channel_connect_by_listen_address(const char *listen_host,
3754 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003755{
3756 int i;
3757
3758 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003759 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003760 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00003761 if (permitted_opens[i].downstream)
3762 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10003763 return connect_to(
3764 permitted_opens[i].host_to_connect,
3765 permitted_opens[i].port_to_connect, ctype, rname);
3766 }
3767 }
3768 error("WARNING: Server requests forwarding for unknown listen_port %d",
3769 listen_port);
3770 return NULL;
3771}
3772
Damien Miller7acefbb2014-07-18 14:11:24 +10003773Channel *
3774channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3775{
3776 int i;
3777
3778 for (i = 0; i < num_permitted_opens; i++) {
3779 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3780 return connect_to(
3781 permitted_opens[i].host_to_connect,
3782 permitted_opens[i].port_to_connect, ctype, rname);
3783 }
3784 }
3785 error("WARNING: Server requests forwarding for unknown path %.100s",
3786 path);
3787 return NULL;
3788}
3789
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003790/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003791Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003792channel_connect_to_port(const char *host, u_short port, char *ctype,
3793 char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003794{
Damien Miller9b439df2006-07-24 14:04:00 +10003795 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003796
3797 permit = all_opens_permitted;
3798 if (!permit) {
3799 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003800 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003801 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003802 break;
3803 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003804 }
Damien Miller9b439df2006-07-24 14:04:00 +10003805
3806 if (num_adm_permitted_opens > 0) {
3807 permit_adm = 0;
3808 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003809 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003810 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003811 break;
3812 }
Damien Miller9b439df2006-07-24 14:04:00 +10003813 }
3814
3815 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003816 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003817 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003818 if (reason != NULL)
3819 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10003820 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003821 }
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003822 return connect_to_reason(host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003823}
3824
Damien Miller7acefbb2014-07-18 14:11:24 +10003825/* Check if connecting to that path is permitted and connect. */
3826Channel *
3827channel_connect_to_path(const char *path, char *ctype, char *rname)
3828{
3829 int i, permit, permit_adm = 1;
3830
3831 permit = all_opens_permitted;
3832 if (!permit) {
3833 for (i = 0; i < num_permitted_opens; i++)
3834 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3835 permit = 1;
3836 break;
3837 }
3838 }
3839
3840 if (num_adm_permitted_opens > 0) {
3841 permit_adm = 0;
3842 for (i = 0; i < num_adm_permitted_opens; i++)
3843 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3844 permit_adm = 1;
3845 break;
3846 }
3847 }
3848
3849 if (!permit || !permit_adm) {
3850 logit("Received request to connect to path %.100s, "
3851 "but the request was denied.", path);
3852 return NULL;
3853 }
3854 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3855}
3856
Damien Miller0e220db2004-06-15 10:34:08 +10003857void
3858channel_send_window_changes(void)
3859{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003860 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003861 struct winsize ws;
3862
3863 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003864 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003865 channels[i]->type != SSH_CHANNEL_OPEN)
3866 continue;
3867 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3868 continue;
3869 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003870 packet_put_int((u_int)ws.ws_col);
3871 packet_put_int((u_int)ws.ws_row);
3872 packet_put_int((u_int)ws.ws_xpixel);
3873 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003874 packet_send();
3875 }
3876}
3877
Ben Lindstrome9c99912001-06-09 00:41:05 +00003878/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003879
Damien Miller5428f641999-11-25 11:54:57 +11003880/*
3881 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003882 * Returns 0 and a suitable display number for the DISPLAY variable
3883 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003884 */
Kevin Steves366298c2001-12-19 17:58:01 +00003885int
Damien Miller95c249f2002-02-05 12:11:34 +11003886x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003887 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003888{
Damien Millere7378562001-12-21 14:58:35 +11003889 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003890 int display_number, sock;
3891 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003892 struct addrinfo hints, *ai, *aitop;
3893 char strport[NI_MAXSERV];
3894 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003895
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003896 if (chanids == NULL)
3897 return -1;
3898
Damien Millera34a28b1999-12-14 10:47:15 +11003899 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11003900 display_number < MAX_DISPLAYS;
3901 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11003902 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11003903 memset(&hints, 0, sizeof(hints));
3904 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11003905 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11003906 hints.ai_socktype = SOCK_STREAM;
3907 snprintf(strport, sizeof strport, "%d", port);
3908 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003909 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00003910 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003911 }
Damien Miller34132e52000-01-14 15:45:46 +11003912 for (ai = aitop; ai; ai = ai->ai_next) {
3913 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3914 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11003915 sock = socket(ai->ai_family, ai->ai_socktype,
3916 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003917 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11003918 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
3919#ifdef EPFNOSUPPORT
3920 && (errno != EPFNOSUPPORT)
3921#endif
3922 ) {
Damien Millere2192732000-01-17 13:22:55 +11003923 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10003924 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00003925 return -1;
Damien Millere2192732000-01-17 13:22:55 +11003926 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11003927 debug("x11_create_display_inet: Socket family %d not supported",
3928 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11003929 continue;
3930 }
Damien Miller34132e52000-01-14 15:45:46 +11003931 }
Damien Miller04ee0f82009-11-18 17:48:30 +11003932 if (ai->ai_family == AF_INET6)
3933 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10003934 if (x11_use_localhost)
3935 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11003936 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003937 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003938 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11003939
Damien Miller34132e52000-01-14 15:45:46 +11003940 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11003941 close(socks[n]);
3942 }
3943 num_socks = 0;
3944 break;
3945 }
3946 socks[num_socks++] = sock;
3947 if (num_socks == NUM_SOCKS)
3948 break;
Damien Miller95def091999-11-25 00:26:21 +11003949 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00003950 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11003951 if (num_socks > 0)
3952 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003953 }
Damien Miller95def091999-11-25 00:26:21 +11003954 if (display_number >= MAX_DISPLAYS) {
3955 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00003956 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003957 }
Damien Miller95def091999-11-25 00:26:21 +11003958 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11003959 for (n = 0; n < num_socks; n++) {
3960 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11003961 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003962 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003963 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00003964 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11003965 }
Damien Miller95def091999-11-25 00:26:21 +11003966 }
Damien Miller34132e52000-01-14 15:45:46 +11003967
Damien Miller34132e52000-01-14 15:45:46 +11003968 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11003969 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11003970 for (n = 0; n < num_socks; n++) {
3971 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11003972 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10003973 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3974 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003975 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11003976 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003977 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11003978 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003979 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003980
Kevin Steves366298c2001-12-19 17:58:01 +00003981 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003982 *display_numberp = display_number;
3983 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003984}
3985
Ben Lindstrombba81212001-06-25 05:01:22 +00003986static int
Damien Miller819dbb62009-01-21 16:46:26 +11003987connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003988{
Damien Miller95def091999-11-25 00:26:21 +11003989 int sock;
3990 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003991
Damien Miller3afe3752001-12-21 12:39:51 +11003992 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3993 if (sock < 0)
3994 error("socket: %.100s", strerror(errno));
3995 memset(&addr, 0, sizeof(addr));
3996 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11003997 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11003998 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11003999 return sock;
4000 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004001 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4002 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004003}
4004
Damien Miller819dbb62009-01-21 16:46:26 +11004005static int
4006connect_local_xsocket(u_int dnr)
4007{
4008 char buf[1024];
4009 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4010 return connect_local_xsocket_path(buf);
4011}
4012
Darren Tuckerda39b092017-03-10 13:22:32 +11004013#ifdef __APPLE__
4014static int
4015is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4016{
4017 struct stat sbuf;
4018
4019 if (strlcpy(path, display, pathlen) >= pathlen) {
4020 error("%s: display path too long", __func__);
4021 return 0;
4022 }
4023 if (display[0] != '/')
4024 return 0;
4025 if (stat(path, &sbuf) == 0) {
4026 return 1;
4027 } else {
4028 char *dot = strrchr(path, '.');
4029 if (dot != NULL) {
4030 *dot = '\0';
4031 if (stat(path, &sbuf) == 0) {
4032 return 1;
4033 }
4034 }
4035 }
4036 return 0;
4037}
4038#endif
4039
Damien Millerbd483e72000-04-30 10:00:53 +10004040int
4041x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004042{
Damien Miller57c4e872006-03-31 23:11:07 +11004043 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004044 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004045 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004046 struct addrinfo hints, *ai, *aitop;
4047 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004048 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004049
Damien Miller95def091999-11-25 00:26:21 +11004050 /* Try to open a socket for the local X server. */
4051 display = getenv("DISPLAY");
4052 if (!display) {
4053 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004054 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004055 }
Damien Miller5428f641999-11-25 11:54:57 +11004056 /*
4057 * Now we decode the value of the DISPLAY variable and make a
4058 * connection to the real X server.
4059 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004060
Damien Miller819dbb62009-01-21 16:46:26 +11004061#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004062 /* Check if display is a path to a socket (as set by launchd). */
4063 {
4064 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004065
Darren Tuckerda39b092017-03-10 13:22:32 +11004066 if (is_path_to_xsocket(display, path, sizeof(path))) {
4067 debug("x11_connect_display: $DISPLAY is launchd");
4068
4069 /* Create a socket. */
4070 sock = connect_local_xsocket_path(path);
4071 if (sock < 0)
4072 return -1;
4073
4074 /* OK, we now have a connection to the display. */
4075 return sock;
4076 }
Damien Miller819dbb62009-01-21 16:46:26 +11004077 }
4078#endif
Damien Miller5428f641999-11-25 11:54:57 +11004079 /*
4080 * Check if it is a unix domain socket. Unix domain displays are in
4081 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4082 */
Damien Miller95def091999-11-25 00:26:21 +11004083 if (strncmp(display, "unix:", 5) == 0 ||
4084 display[0] == ':') {
4085 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004086 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004087 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004088 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004089 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004090 }
4091 /* Create a socket. */
4092 sock = connect_local_xsocket(display_number);
4093 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004094 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004095
4096 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004097 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004098 }
Damien Miller5428f641999-11-25 11:54:57 +11004099 /*
4100 * Connect to an inet socket. The DISPLAY value is supposedly
4101 * hostname:d[.s], where hostname may also be numeric IP address.
4102 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004103 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004104 cp = strchr(buf, ':');
4105 if (!cp) {
4106 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004107 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004108 }
Damien Miller95def091999-11-25 00:26:21 +11004109 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004110 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004111 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004112 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004113 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004114 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004115 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004116
Damien Miller34132e52000-01-14 15:45:46 +11004117 /* Look up the host address */
4118 memset(&hints, 0, sizeof(hints));
4119 hints.ai_family = IPv4or6;
4120 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004121 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004122 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004123 error("%.100s: unknown host. (%s)", buf,
4124 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004125 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004126 }
Damien Miller34132e52000-01-14 15:45:46 +11004127 for (ai = aitop; ai; ai = ai->ai_next) {
4128 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004129 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004130 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004131 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004132 continue;
4133 }
4134 /* Connect it to the display. */
4135 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004136 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004137 6000 + display_number, strerror(errno));
4138 close(sock);
4139 continue;
4140 }
4141 /* Success */
4142 break;
Damien Miller34132e52000-01-14 15:45:46 +11004143 }
Damien Miller34132e52000-01-14 15:45:46 +11004144 freeaddrinfo(aitop);
4145 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004146 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004147 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004148 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004149 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004150 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004151 return sock;
4152}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004153
Damien Millerbd483e72000-04-30 10:00:53 +10004154/*
Damien Miller5428f641999-11-25 11:54:57 +11004155 * Requests forwarding of X11 connections, generates fake authentication
4156 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004157 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004158 */
Damien Miller4af51302000-04-16 11:18:38 +10004159void
Damien Miller17e7ed02005-06-17 12:54:33 +10004160x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004161 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004162{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004163 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004164 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004165 char *new_data;
4166 int screen_number;
4167 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004168
Damien Millerf92c0792005-07-06 09:45:26 +10004169 if (x11_saved_display == NULL)
4170 x11_saved_display = xstrdup(disp);
4171 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004172 error("x11_request_forwarding_with_spoofing: different "
4173 "$DISPLAY already forwarded");
4174 return;
4175 }
4176
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004177 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004178 if (cp)
4179 cp = strchr(cp, '.');
4180 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004181 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004182 else
4183 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004184
Damien Miller13390022005-07-06 09:44:19 +10004185 if (x11_saved_proto == NULL) {
4186 /* Save protocol name. */
4187 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004188
4189 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004190 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004191 for (i = 0; i < data_len; i++) {
4192 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4193 fatal("x11_request_forwarding: bad "
4194 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004195 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004196 }
4197 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004198
4199 /* Generate fake data of the same length. */
4200 x11_fake_data = xmalloc(data_len);
4201 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004202 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004204
Damien Miller95def091999-11-25 00:26:21 +11004205 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004206 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004207
Damien Miller95def091999-11-25 00:26:21 +11004208 /* Send the request packet. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00004209 channel_request_start(client_session_id, "x11-req", want_reply);
4210 packet_put_char(0); /* XXX bool single connection */
Damien Millerbd483e72000-04-30 10:00:53 +10004211 packet_put_cstring(proto);
4212 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004213 packet_put_int(screen_number);
4214 packet_send();
4215 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004216 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004217}