blob: 4092a67109e2366d80d4949152552dc3c78d6c25 [file] [log] [blame]
djm@openbsd.org930e8d22017-04-30 23:28:41 +00001/* $OpenBSD: channels.c,v 1.359 2017/04/30 23:28:41 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <netdb.h>
Darren Tucker37f92202015-02-23 03:07:24 +110059#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000060#include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100062#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100063#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100064#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110065#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100066#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100067#include <stdarg.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Millerb84886b2008-05-19 15:05:07 +100069#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000073#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "log.h"
76#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100077#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100081#include "key.h"
82#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110083#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Ben Lindstrome9c99912001-06-09 00:41:05 +000085/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller5428f641999-11-25 11:54:57 +110087/*
88 * Pointer to an array containing all allocated channels. The array is
89 * dynamically extended as needed.
90 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000091static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller5428f641999-11-25 11:54:57 +110093/*
94 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000095 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110096 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100097static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000101 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100102 */
Damien Miller5e953212001-01-30 09:14:00 +1100103static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Ben Lindstrome9c99912001-06-09 00:41:05 +0000106/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Data structure for storing which hosts are permitted for forward requests.
110 * The local sides of any remote forwards are stored in this array to prevent
111 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
112 * network (which might be behind a firewall).
113 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000114/* XXX: streamlocal wants a path instead of host:port */
115/* Overload host_to_connect; we could just make this match Forward */
116/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100117typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000118 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000119 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000120 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000121 char *listen_path; /* Remote side should listen path. */
122 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000123 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124} ForwardPermission;
125
Damien Miller9b439df2006-07-24 14:04:00 +1000126/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000127static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000128
Damien Miller9b439df2006-07-24 14:04:00 +1000129/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000130static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000131
132/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
135/* Number of permitted host/port pair in the array permitted by the admin. */
136static int num_adm_permitted_opens = 0;
137
Darren Tucker1338b9e2011-10-02 18:57:35 +1100138/* special-case port number meaning allow any port */
139#define FWD_PERMIT_ANY_PORT 0
140
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000141/* special-case wildcard meaning allow any host */
142#define FWD_PERMIT_ANY_HOST "*"
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * If this is true, all opens are permitted. This is the case on the server
146 * on which we have to trust the client anyway, and the user could do
147 * anything after logging in anyway.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149static int all_opens_permitted = 0;
150
Ben Lindstrome9c99912001-06-09 00:41:05 +0000151
152/* -- X11 forwarding */
153
154/* Maximum number of fake X11 displays to try. */
155#define MAX_DISPLAYS 1000
156
Damien Miller13390022005-07-06 09:44:19 +1000157/* Saved X11 local (client) display. */
158static char *x11_saved_display = NULL;
159
Ben Lindstrome9c99912001-06-09 00:41:05 +0000160/* Saved X11 authentication protocol name. */
161static char *x11_saved_proto = NULL;
162
163/* Saved X11 authentication data. This is the real data. */
164static char *x11_saved_data = NULL;
165static u_int x11_saved_data_len = 0;
166
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000167/* Deadline after which all X11 connections are refused */
168static u_int x11_refuse_time;
169
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170/*
171 * Fake X11 authentication data. This is what the server will be sending us;
172 * we should replace any occurrences of this by the real data.
173 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100174static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000175static u_int x11_fake_data_len;
176
177
178/* -- agent forwarding */
179
180#define NUM_SOCKS 10
181
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000182/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000183static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000184
Ben Lindstrome9c99912001-06-09 00:41:05 +0000185/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static void port_open_helper(Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000187static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000188
Damien Millerbd740252008-05-19 15:37:09 +1000189/* non-blocking connect helpers */
190static int connect_next(struct channel_connect *);
191static void channel_connect_ctx_free(struct channel_connect *);
192
Ben Lindstrome9c99912001-06-09 00:41:05 +0000193/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000194
195Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100196channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000197{
198 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000199
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000200 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100201 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000202 return NULL;
203 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000204 c = channels[id];
205 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100206 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000207 return NULL;
208 }
209 return c;
210}
211
markus@openbsd.org8d057842016-09-30 09:19:13 +0000212Channel *
213channel_by_remote_id(int remote_id)
214{
215 Channel *c;
216 u_int i;
217
218 for (i = 0; i < channels_alloc; i++) {
219 c = channels[i];
220 if (c != NULL && c->remote_id == remote_id)
221 return c;
222 }
223 return NULL;
224}
225
Damien Miller5428f641999-11-25 11:54:57 +1100226/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100227 * Returns the channel if it is allowed to receive protocol messages.
228 * Private channels, like listening sockets, may not receive messages.
229 */
230Channel *
231channel_lookup(int id)
232{
233 Channel *c;
234
235 if ((c = channel_by_id(id)) == NULL)
236 return (NULL);
237
Damien Millerd62f2ca2006-03-26 13:57:41 +1100238 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100239 case SSH_CHANNEL_X11_OPEN:
240 case SSH_CHANNEL_LARVAL:
241 case SSH_CHANNEL_CONNECTING:
242 case SSH_CHANNEL_DYNAMIC:
243 case SSH_CHANNEL_OPENING:
244 case SSH_CHANNEL_OPEN:
245 case SSH_CHANNEL_INPUT_DRAINING:
246 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller36187092013-06-10 13:07:11 +1000247 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000248 case SSH_CHANNEL_MUX_PROXY:
Damien Millerd47c62a2005-12-13 19:33:57 +1100249 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100250 }
251 logit("Non-public channel %d, type %d.", id, c->type);
252 return (NULL);
253}
254
255/*
Damien Millere247cc42000-05-07 12:03:14 +1000256 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000257 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100258 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000259static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100260channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000261 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262{
Damien Miller95def091999-11-25 00:26:21 +1100263 /* Update the maximum file descriptor value. */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000264 channel_max_fd = MAXIMUM(channel_max_fd, rfd);
265 channel_max_fd = MAXIMUM(channel_max_fd, wfd);
266 channel_max_fd = MAXIMUM(channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100267
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100268 if (rfd != -1)
269 fcntl(rfd, F_SETFD, FD_CLOEXEC);
270 if (wfd != -1 && wfd != rfd)
271 fcntl(wfd, F_SETFD, FD_CLOEXEC);
272 if (efd != -1 && efd != rfd && efd != wfd)
273 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000274
Damien Miller6f83b8e2000-05-02 09:23:45 +1000275 c->rfd = rfd;
276 c->wfd = wfd;
277 c->sock = (rfd == wfd) ? rfd : -1;
278 c->efd = efd;
279 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100280
Darren Tuckered3cdc02008-06-16 23:29:18 +1000281 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000282 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000283#ifdef _AIX
284 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000285 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000286#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100287
Damien Miller69b69aa2000-10-28 14:19:58 +1100288 /* enable nonblocking mode */
289 if (nonblock) {
290 if (rfd != -1)
291 set_nonblock(rfd);
292 if (wfd != -1)
293 set_nonblock(wfd);
294 if (efd != -1)
295 set_nonblock(efd);
296 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000297}
298
299/*
300 * Allocate a new channel object and set its type and socket. This will cause
301 * remote_name to be freed.
302 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000303Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000304channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000305 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000306{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000307 int found;
308 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000309 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310
Damien Miller95def091999-11-25 00:26:21 +1100311 /* Do initial allocation if this is the first call. */
312 if (channels_alloc == 0) {
313 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100314 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100315 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000316 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100317 }
318 /* Try to find a free slot where to put the new channel. */
319 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000320 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100321 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000322 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100323 break;
324 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000325 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100326 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100327 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000328 if (channels_alloc > 10000)
329 fatal("channel_new: internal error: channels_alloc %d "
330 "too big.", channels_alloc);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000331 channels = xreallocarray(channels, channels_alloc + 10,
Damien Miller36812092006-03-26 14:22:47 +1100332 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000333 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100334 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100335 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000336 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100337 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000338 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100339 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100340 buffer_init(&c->input);
341 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000342 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100343 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000344 c->listening_addr = NULL;
345 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100346 c->ostate = CHAN_OUTPUT_OPEN;
347 c->istate = CHAN_INPUT_OPEN;
348 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000349 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000350 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100351 c->self = found;
352 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000353 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000354 c->local_window = window;
355 c->local_window_max = window;
356 c->local_consumed = 0;
357 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100358 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000359 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000360 c->remote_window = 0;
361 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000362 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100363 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000364 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100365 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000366 c->open_confirm = NULL;
367 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000368 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100369 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000370 c->filter_ctx = NULL;
371 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100372 c->ctl_chan = -1;
373 c->mux_rcb = NULL;
374 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000375 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100376 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000377 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100378 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000379 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000380}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000381
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000382static int
383channel_find_maxfd(void)
384{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000385 u_int i;
386 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000387 Channel *c;
388
389 for (i = 0; i < channels_alloc; i++) {
390 c = channels[i];
391 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000392 max = MAXIMUM(max, c->rfd);
393 max = MAXIMUM(max, c->wfd);
394 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000395 }
396 }
397 return max;
398}
399
400int
401channel_close_fd(int *fdp)
402{
403 int ret = 0, fd = *fdp;
404
405 if (fd != -1) {
406 ret = close(fd);
407 *fdp = -1;
408 if (fd == channel_max_fd)
409 channel_max_fd = channel_find_maxfd();
410 }
411 return ret;
412}
413
Damien Miller6f83b8e2000-05-02 09:23:45 +1000414/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000415static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000416channel_close_fds(Channel *c)
417{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000418 channel_close_fd(&c->sock);
419 channel_close_fd(&c->rfd);
420 channel_close_fd(&c->wfd);
421 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000422}
423
424/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000425void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000426channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000428 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000429 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000430 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000431 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000432
markus@openbsd.org8d057842016-09-30 09:19:13 +0000433 for (n = 0, i = 0; i < channels_alloc; i++) {
434 if ((other = channels[i]) != NULL) {
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000435 n++;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000436
437 /* detach from mux client and prepare for closing */
438 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
439 other->type == SSH_CHANNEL_MUX_PROXY &&
440 other->mux_ctx == c) {
441 other->mux_ctx = NULL;
442 other->type = SSH_CHANNEL_OPEN;
443 other->istate = CHAN_INPUT_CLOSED;
444 other->ostate = CHAN_OUTPUT_CLOSED;
445 }
446 }
447 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000448 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000449 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000450
markus@openbsd.org8d057842016-09-30 09:19:13 +0000451 /* XXX more MUX cleanup: remove remote forwardings */
452 if (c->type == SSH_CHANNEL_MUX_CLIENT) {
453 for (i = 0; i < (u_int)num_permitted_opens; i++) {
454 if (permitted_opens[i].downstream != c)
455 continue;
456 /* cancel on the server, since mux client is gone */
457 debug("channel %d: cleanup remote forward for %s:%u",
458 c->self,
459 permitted_opens[i].listen_host,
460 permitted_opens[i].listen_port);
461 packet_start(SSH2_MSG_GLOBAL_REQUEST);
462 packet_put_cstring("cancel-tcpip-forward");
463 packet_put_char(0);
464 packet_put_cstring(channel_rfwd_bind_host(
465 permitted_opens[i].listen_host));
466 packet_put_int(permitted_opens[i].listen_port);
467 packet_send();
468 /* unregister */
469 permitted_opens[i].listen_port = 0;
470 permitted_opens[i].port_to_connect = 0;
471 free(permitted_opens[i].host_to_connect);
472 permitted_opens[i].host_to_connect = NULL;
473 free(permitted_opens[i].listen_host);
474 permitted_opens[i].listen_host = NULL;
475 permitted_opens[i].listen_path = NULL;
476 permitted_opens[i].downstream = NULL;
477 }
478 }
479
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000480 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000481 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000482 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000483
Damien Miller6f83b8e2000-05-02 09:23:45 +1000484 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000485 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000486 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000487 buffer_free(&c->input);
488 buffer_free(&c->output);
489 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000490 free(c->remote_name);
491 c->remote_name = NULL;
492 free(c->path);
493 c->path = NULL;
494 free(c->listening_addr);
495 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000496 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
497 if (cc->abandon_cb != NULL)
498 cc->abandon_cb(c, cc->ctx);
499 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100500 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000501 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000502 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000503 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
504 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000505 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000506 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507}
508
Ben Lindstrome9c99912001-06-09 00:41:05 +0000509void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000510channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000511{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000512 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000513
Ben Lindstrom601e4362001-06-21 03:19:23 +0000514 for (i = 0; i < channels_alloc; i++)
515 if (channels[i] != NULL)
516 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000517}
518
519/*
520 * Closes the sockets/fds of all channels. This is used to close extra file
521 * descriptors after a fork.
522 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000523void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000524channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000525{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000526 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000527
528 for (i = 0; i < channels_alloc; i++)
529 if (channels[i] != NULL)
530 channel_close_fds(channels[i]);
531}
532
533/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000534 * Stop listening to channels.
535 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000536void
537channel_stop_listening(void)
538{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000539 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000540 Channel *c;
541
542 for (i = 0; i < channels_alloc; i++) {
543 c = channels[i];
544 if (c != NULL) {
545 switch (c->type) {
546 case SSH_CHANNEL_AUTH_SOCKET:
547 case SSH_CHANNEL_PORT_LISTENER:
548 case SSH_CHANNEL_RPORT_LISTENER:
549 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000550 case SSH_CHANNEL_UNIX_LISTENER:
551 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000552 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000553 channel_free(c);
554 break;
555 }
556 }
557 }
558}
559
560/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000561 * Returns true if no channel has too much buffered data, and false if one or
562 * more channel is overfull.
563 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000564int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000565channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000566{
567 u_int i;
568 Channel *c;
569
570 for (i = 0; i < channels_alloc; i++) {
571 c = channels[i];
572 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000573 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000574 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000575 c->self, buffer_len(&c->output),
576 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000577 return 0;
578 }
579 }
580 }
581 return 1;
582}
583
584/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000585int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000586channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000587{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000588 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000589 Channel *c;
590
591 for (i = 0; i < channels_alloc; i++) {
592 c = channels[i];
593 if (c == NULL)
594 continue;
595 switch (c->type) {
596 case SSH_CHANNEL_X11_LISTENER:
597 case SSH_CHANNEL_PORT_LISTENER:
598 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100599 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000600 case SSH_CHANNEL_CLOSED:
601 case SSH_CHANNEL_AUTH_SOCKET:
602 case SSH_CHANNEL_DYNAMIC:
603 case SSH_CHANNEL_CONNECTING:
604 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000605 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000606 case SSH_CHANNEL_UNIX_LISTENER:
607 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000608 continue;
609 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000610 continue;
611 case SSH_CHANNEL_OPENING:
612 case SSH_CHANNEL_OPEN:
613 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100614 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000615 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000616 return 1;
617 case SSH_CHANNEL_INPUT_DRAINING:
618 case SSH_CHANNEL_OUTPUT_DRAINING:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000619 fatal("cannot happen: OUT_DRAIN");
Ben Lindstrome9c99912001-06-09 00:41:05 +0000620 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000621 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000622 /* NOTREACHED */
623 }
624 }
625 return 0;
626}
627
628/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000629int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000630channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000631{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000632 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633 Channel *c;
634
635 for (i = 0; i < channels_alloc; i++) {
636 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000637 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000638 continue;
639 switch (c->type) {
640 case SSH_CHANNEL_CLOSED:
641 case SSH_CHANNEL_DYNAMIC:
642 case SSH_CHANNEL_X11_LISTENER:
643 case SSH_CHANNEL_PORT_LISTENER:
644 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100645 case SSH_CHANNEL_MUX_LISTENER:
646 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000647 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000648 case SSH_CHANNEL_OPENING:
649 case SSH_CHANNEL_CONNECTING:
650 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000651 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000652 case SSH_CHANNEL_UNIX_LISTENER:
653 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000654 continue;
655 case SSH_CHANNEL_LARVAL:
656 case SSH_CHANNEL_AUTH_SOCKET:
657 case SSH_CHANNEL_OPEN:
658 case SSH_CHANNEL_X11_OPEN:
659 return i;
660 case SSH_CHANNEL_INPUT_DRAINING:
661 case SSH_CHANNEL_OUTPUT_DRAINING:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000662 fatal("cannot happen: OUT_DRAIN");
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000664 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000665 /* NOTREACHED */
666 }
667 }
668 return -1;
669}
670
Ben Lindstrome9c99912001-06-09 00:41:05 +0000671/*
672 * Returns a message describing the currently open forwarded connections,
673 * suitable for sending to the client. The message contains crlf pairs for
674 * newlines.
675 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000676char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000677channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000678{
679 Buffer buffer;
680 Channel *c;
681 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000682 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000683
684 buffer_init(&buffer);
685 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
686 buffer_append(&buffer, buf, strlen(buf));
687 for (i = 0; i < channels_alloc; i++) {
688 c = channels[i];
689 if (c == NULL)
690 continue;
691 switch (c->type) {
692 case SSH_CHANNEL_X11_LISTENER:
693 case SSH_CHANNEL_PORT_LISTENER:
694 case SSH_CHANNEL_RPORT_LISTENER:
695 case SSH_CHANNEL_CLOSED:
696 case SSH_CHANNEL_AUTH_SOCKET:
697 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000698 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100699 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000700 case SSH_CHANNEL_UNIX_LISTENER:
701 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000702 continue;
703 case SSH_CHANNEL_LARVAL:
704 case SSH_CHANNEL_OPENING:
705 case SSH_CHANNEL_CONNECTING:
706 case SSH_CHANNEL_DYNAMIC:
707 case SSH_CHANNEL_OPEN:
708 case SSH_CHANNEL_X11_OPEN:
709 case SSH_CHANNEL_INPUT_DRAINING:
710 case SSH_CHANNEL_OUTPUT_DRAINING:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000711 case SSH_CHANNEL_MUX_PROXY:
712 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000713 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000714 " #%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 +0000715 c->self, c->remote_name,
716 c->type, c->remote_id,
717 c->istate, buffer_len(&c->input),
718 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100719 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000720 buffer_append(&buffer, buf, strlen(buf));
721 continue;
722 default:
723 fatal("channel_open_message: bad channel type %d", c->type);
724 /* NOTREACHED */
725 }
726 }
727 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000728 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000729 buffer_free(&buffer);
730 return cp;
731}
732
733void
734channel_send_open(int id)
735{
736 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000737
Ben Lindstrome9c99912001-06-09 00:41:05 +0000738 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000739 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000740 return;
741 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000742 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743 packet_start(SSH2_MSG_CHANNEL_OPEN);
744 packet_put_cstring(c->ctype);
745 packet_put_int(c->self);
746 packet_put_int(c->local_window);
747 packet_put_int(c->local_maxpacket);
748 packet_send();
749}
750
751void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000752channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000754 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000755
Ben Lindstrome9c99912001-06-09 00:41:05 +0000756 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000757 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000758 return;
759 }
Damien Miller0e220db2004-06-15 10:34:08 +1000760 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000761 packet_start(SSH2_MSG_CHANNEL_REQUEST);
762 packet_put_int(c->remote_id);
763 packet_put_cstring(service);
764 packet_put_char(wantconfirm);
765}
Damien Miller4f7becb2006-03-26 14:10:14 +1100766
Ben Lindstrome9c99912001-06-09 00:41:05 +0000767void
Damien Millerb84886b2008-05-19 15:05:07 +1000768channel_register_status_confirm(int id, channel_confirm_cb *cb,
769 channel_confirm_abandon_cb *abandon_cb, void *ctx)
770{
771 struct channel_confirm *cc;
772 Channel *c;
773
774 if ((c = channel_lookup(id)) == NULL)
775 fatal("channel_register_expect: %d: bad id", id);
776
Damien Miller6c81fee2013-11-08 12:19:55 +1100777 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000778 cc->cb = cb;
779 cc->abandon_cb = abandon_cb;
780 cc->ctx = ctx;
781 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
782}
783
784void
Damien Millerd530f5f2010-05-21 14:57:10 +1000785channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000786{
787 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000788
Ben Lindstrome9c99912001-06-09 00:41:05 +0000789 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100790 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000791 return;
792 }
Damien Millerb84886b2008-05-19 15:05:07 +1000793 c->open_confirm = fn;
794 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000795}
Damien Miller4f7becb2006-03-26 14:10:14 +1100796
Ben Lindstrome9c99912001-06-09 00:41:05 +0000797void
Damien Miller39eda6e2005-11-05 14:52:50 +1100798channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000799{
Damien Millerd47c62a2005-12-13 19:33:57 +1100800 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000801
Ben Lindstrome9c99912001-06-09 00:41:05 +0000802 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000803 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000804 return;
805 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000806 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100807 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000808}
Damien Miller4f7becb2006-03-26 14:10:14 +1100809
Ben Lindstrome9c99912001-06-09 00:41:05 +0000810void
811channel_cancel_cleanup(int id)
812{
Damien Millerd47c62a2005-12-13 19:33:57 +1100813 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000814
Ben Lindstrome9c99912001-06-09 00:41:05 +0000815 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000816 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000817 return;
818 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000819 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100820 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000821}
Damien Miller4f7becb2006-03-26 14:10:14 +1100822
Ben Lindstrome9c99912001-06-09 00:41:05 +0000823void
Damien Miller077b2382005-12-31 16:22:32 +1100824channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000825 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000826{
827 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000828
Ben Lindstrome9c99912001-06-09 00:41:05 +0000829 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000830 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000831 return;
832 }
Damien Miller077b2382005-12-31 16:22:32 +1100833 c->input_filter = ifn;
834 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000835 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000836 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000837}
838
839void
840channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000841 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000842{
843 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000844
Ben Lindstrome9c99912001-06-09 00:41:05 +0000845 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
846 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000847 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000848 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100849 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000850 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
851 packet_put_int(c->remote_id);
852 packet_put_int(c->local_window);
853 packet_send();
854}
855
Damien Miller5428f641999-11-25 11:54:57 +1100856/*
Damien Millerb38eff82000-04-01 11:09:21 +1000857 * 'channel_pre*' are called just before select() to add any bits relevant to
858 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100859 */
Damien Millerb38eff82000-04-01 11:09:21 +1000860/*
861 * 'channel_post*': perform any appropriate operations for channels which
862 * have events pending.
863 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100864typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000865chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
866chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
867
Damien Miller8473dd82006-07-24 14:08:32 +1000868/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000869static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100870channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000871{
872 FD_SET(c->sock, readset);
873}
874
Damien Miller8473dd82006-07-24 14:08:32 +1000875/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000876static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100877channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000878{
879 debug3("channel %d: waiting for connection", c->self);
880 FD_SET(c->sock, writeset);
881}
882
Ben Lindstrombba81212001-06-25 05:01:22 +0000883static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100884channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000885{
Damien Miller33b13562000-04-04 14:38:59 +1000886 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000887 c->remote_window > 0 &&
888 buffer_len(&c->input) < c->remote_window &&
Damien Miller499a0d52006-04-23 12:06:03 +1000889 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000890 FD_SET(c->rfd, readset);
891 if (c->ostate == CHAN_OUTPUT_OPEN ||
892 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
893 if (buffer_len(&c->output) > 0) {
894 FD_SET(c->wfd, writeset);
895 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000896 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000897 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
898 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000899 else
900 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000901 }
902 }
903 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000904 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
905 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000906 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
907 buffer_len(&c->extended) > 0)
908 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000909 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
910 (c->extended_usage == CHAN_EXTENDED_READ ||
911 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000912 buffer_len(&c->extended) < c->remote_window)
913 FD_SET(c->efd, readset);
914 }
Damien Miller0e220db2004-06-15 10:34:08 +1000915 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000916}
917
Damien Millerb38eff82000-04-01 11:09:21 +1000918/*
919 * This is a special state for X11 authentication spoofing. An opened X11
920 * connection (when authentication spoofing is being done) remains in this
921 * state until the first packet has been completely read. The authentication
922 * data in that packet is then substituted by the real data if it matches the
923 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000924 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000925 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000926 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000927static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000928x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000929{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000930 u_char *ucp;
931 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000932
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000933 /* Is this being called after the refusal deadline? */
934 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
935 verbose("Rejected X11 connection after ForwardX11Timeout "
936 "expired");
937 return -1;
938 }
939
Damien Millerb38eff82000-04-01 11:09:21 +1000940 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000941 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000942 return 0;
943
944 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100945 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000946 if (ucp[0] == 0x42) { /* Byte order MSB first. */
947 proto_len = 256 * ucp[6] + ucp[7];
948 data_len = 256 * ucp[8] + ucp[9];
949 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
950 proto_len = ucp[6] + 256 * ucp[7];
951 data_len = ucp[8] + 256 * ucp[9];
952 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000953 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100954 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000955 return -1;
956 }
957
958 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000959 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000960 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
961 return 0;
962
963 /* Check if authentication protocol matches. */
964 if (proto_len != strlen(x11_saved_proto) ||
965 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000966 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000967 return -1;
968 }
969 /* Check if authentication data matches our fake data. */
970 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000971 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000972 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000973 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000974 return -1;
975 }
976 /* Check fake data length */
977 if (x11_fake_data_len != x11_saved_data_len) {
978 error("X11 fake_data_len %d != saved_data_len %d",
979 x11_fake_data_len, x11_saved_data_len);
980 return -1;
981 }
982 /*
983 * Received authentication protocol and data match
984 * our fake data. Substitute the fake data with real
985 * data.
986 */
987 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
988 x11_saved_data, x11_saved_data_len);
989 return 1;
990}
991
Ben Lindstrombba81212001-06-25 05:01:22 +0000992static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100993channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000994{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000995 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000996
997 /* c->force_drain = 1; */
998
Damien Millerb38eff82000-04-01 11:09:21 +1000999 if (ret == 1) {
1000 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +11001001 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001002 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001003 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +10001004 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +11001005 chan_read_failed(c);
1006 buffer_clear(&c->input);
1007 chan_ibuf_empty(c);
1008 buffer_clear(&c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001009 chan_write_failed(c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001010 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001011 }
1012}
1013
Damien Millere1537f92010-01-26 13:26:22 +11001014static void
1015channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1016{
Damien Millerd530f5f2010-05-21 14:57:10 +10001017 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001018 buffer_check_alloc(&c->input, CHAN_RBUF))
1019 FD_SET(c->rfd, readset);
1020 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1021 /* clear buffer immediately (discard any partial packet) */
1022 buffer_clear(&c->input);
1023 chan_ibuf_empty(c);
1024 /* Start output drain. XXX just kill chan? */
1025 chan_rcvd_oclose(c);
1026 }
1027 if (c->ostate == CHAN_OUTPUT_OPEN ||
1028 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1029 if (buffer_len(&c->output) > 0)
1030 FD_SET(c->wfd, writeset);
1031 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1032 chan_obuf_empty(c);
1033 }
1034}
1035
Ben Lindstromb3921512001-04-11 15:57:50 +00001036/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001037/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001038static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001039channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001040{
Damien Millera10f5612002-09-12 09:49:15 +10001041 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001042 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001043 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001044 struct {
1045 u_int8_t version;
1046 u_int8_t command;
1047 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001048 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001049 } s4_req, s4_rsp;
1050
Ben Lindstromb3921512001-04-11 15:57:50 +00001051 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001052
1053 have = buffer_len(&c->input);
1054 len = sizeof(s4_req);
1055 if (have < len)
1056 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001057 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001058
1059 need = 1;
1060 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1061 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1062 debug2("channel %d: socks4a request", c->self);
1063 /* ... and needs an extra string (the hostname) */
1064 need = 2;
1065 }
1066 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001067 for (found = 0, i = len; i < have; i++) {
1068 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001069 found++;
1070 if (found == need)
1071 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001072 }
1073 if (i > 1024) {
1074 /* the peer is probably sending garbage */
1075 debug("channel %d: decode socks4: too long",
1076 c->self);
1077 return -1;
1078 }
1079 }
Damien Miller1781f532009-01-28 16:24:41 +11001080 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001081 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001082 buffer_get(&c->input, (char *)&s4_req.version, 1);
1083 buffer_get(&c->input, (char *)&s4_req.command, 1);
1084 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001085 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001086 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001087 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001088 if (memchr(p, '\0', have) == NULL)
1089 fatal("channel %d: decode socks4: user not nul terminated",
1090 c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001091 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001092 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001093 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001094 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001095 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001096 c->self, len, have);
1097 strlcpy(username, p, sizeof(username));
1098 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001099
Darren Tuckera627d422013-06-02 07:31:17 +10001100 free(c->path);
1101 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001102 if (need == 1) { /* SOCKS4: one string */
1103 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001104 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001105 } else { /* SOCKS4A: two strings */
1106 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001107 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001108 len = strlen(p);
1109 debug2("channel %d: decode socks4a: host %s/%d",
1110 c->self, p, len);
1111 len++; /* trailing '\0' */
1112 if (len > have)
1113 fatal("channel %d: decode socks4a: len %d > have %d",
1114 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001115 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001116 error("channel %d: hostname \"%.100s\" too long",
1117 c->self, p);
1118 return -1;
1119 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001120 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001121 buffer_consume(&c->input, len);
1122 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001123 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001124
Damien Millerfbdeece2003-09-02 22:52:31 +10001125 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001126 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001127
Ben Lindstromb3921512001-04-11 15:57:50 +00001128 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001129 debug("channel %d: cannot handle: %s cn %d",
1130 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001131 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001132 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001133 s4_rsp.version = 0; /* vn: 0 for reply */
1134 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001135 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001136 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001137 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001138 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001139}
1140
Darren Tucker46471c92003-07-03 13:55:19 +10001141/* try to decode a socks5 header */
1142#define SSH_SOCKS5_AUTHDONE 0x1000
1143#define SSH_SOCKS5_NOAUTH 0x00
1144#define SSH_SOCKS5_IPV4 0x01
1145#define SSH_SOCKS5_DOMAIN 0x03
1146#define SSH_SOCKS5_IPV6 0x04
1147#define SSH_SOCKS5_CONNECT 0x01
1148#define SSH_SOCKS5_SUCCESS 0x00
1149
Damien Miller8473dd82006-07-24 14:08:32 +10001150/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001151static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001152channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001153{
1154 struct {
1155 u_int8_t version;
1156 u_int8_t command;
1157 u_int8_t reserved;
1158 u_int8_t atyp;
1159 } s5_req, s5_rsp;
1160 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001161 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1162 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001163 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001164
1165 debug2("channel %d: decode socks5", c->self);
1166 p = buffer_ptr(&c->input);
1167 if (p[0] != 0x05)
1168 return -1;
1169 have = buffer_len(&c->input);
1170 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1171 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001172 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001173 return 0;
1174 nmethods = p[1];
1175 if (have < nmethods + 2)
1176 return 0;
1177 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001178 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001179 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001180 found = 1;
1181 break;
1182 }
1183 }
1184 if (!found) {
1185 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1186 c->self);
1187 return -1;
1188 }
1189 buffer_consume(&c->input, nmethods + 2);
1190 buffer_put_char(&c->output, 0x05); /* version */
1191 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1192 FD_SET(c->sock, writeset);
1193 c->flags |= SSH_SOCKS5_AUTHDONE;
1194 debug2("channel %d: socks5 auth done", c->self);
1195 return 0; /* need more */
1196 }
1197 debug2("channel %d: socks5 post auth", c->self);
1198 if (have < sizeof(s5_req)+1)
1199 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001200 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001201 if (s5_req.version != 0x05 ||
1202 s5_req.command != SSH_SOCKS5_CONNECT ||
1203 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001204 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001205 return -1;
1206 }
Darren Tucker47eede72005-03-14 23:08:12 +11001207 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001208 case SSH_SOCKS5_IPV4:
1209 addrlen = 4;
1210 af = AF_INET;
1211 break;
1212 case SSH_SOCKS5_DOMAIN:
1213 addrlen = p[sizeof(s5_req)];
1214 af = -1;
1215 break;
1216 case SSH_SOCKS5_IPV6:
1217 addrlen = 16;
1218 af = AF_INET6;
1219 break;
1220 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001221 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001222 return -1;
1223 }
Damien Miller0f077072006-07-10 22:21:02 +10001224 need = sizeof(s5_req) + addrlen + 2;
1225 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1226 need++;
1227 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001228 return 0;
1229 buffer_consume(&c->input, sizeof(s5_req));
1230 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1231 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001232 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001233 buffer_get(&c->input, (char *)&dest_port, 2);
1234 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001235 free(c->path);
1236 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001237 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001238 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001239 error("channel %d: dynamic request: socks5 hostname "
1240 "\"%.100s\" too long", c->self, dest_addr);
1241 return -1;
1242 }
1243 c->path = xstrdup(dest_addr);
1244 } else {
1245 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1246 return -1;
1247 c->path = xstrdup(ntop);
1248 }
Darren Tucker46471c92003-07-03 13:55:19 +10001249 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001250
Damien Millerfbdeece2003-09-02 22:52:31 +10001251 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001252 c->self, c->path, c->host_port, s5_req.command);
1253
1254 s5_rsp.version = 0x05;
1255 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1256 s5_rsp.reserved = 0; /* ignored */
1257 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001258 dest_port = 0; /* ignored */
1259
Damien Millera0fdce92006-03-26 14:28:50 +11001260 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001261 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001262 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001263 return 1;
1264}
1265
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001266Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001267channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1268 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001269{
1270 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001271
1272 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1273 port_to_connect);
1274
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001275 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1276 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1277 0, "stdio-forward", /*nonblock*/0);
1278
1279 c->path = xstrdup(host_to_connect);
1280 c->host_port = port_to_connect;
1281 c->listening_port = 0;
1282 c->force_drain = 1;
1283
1284 channel_register_fds(c, in, out, -1, 0, 1, 0);
1285 port_open_helper(c, "direct-tcpip");
1286
1287 return c;
1288}
1289
Ben Lindstromb3921512001-04-11 15:57:50 +00001290/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001291static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001292channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001293{
1294 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001295 u_int have;
1296 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001297
1298 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001299 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001300 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001301 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001302 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001303 /* need more */
1304 FD_SET(c->sock, readset);
1305 return;
1306 }
1307 /* try to guess the protocol */
1308 p = buffer_ptr(&c->input);
1309 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001310 case 0x04:
1311 ret = channel_decode_socks4(c, readset, writeset);
1312 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001313 case 0x05:
1314 ret = channel_decode_socks5(c, readset, writeset);
1315 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001316 default:
1317 ret = -1;
1318 break;
1319 }
1320 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001321 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001322 } else if (ret == 0) {
1323 debug2("channel %d: pre_dynamic: need more", c->self);
1324 /* need more */
1325 FD_SET(c->sock, readset);
1326 } else {
1327 /* switch to the next state */
1328 c->type = SSH_CHANNEL_OPENING;
1329 port_open_helper(c, "direct-tcpip");
1330 }
1331}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001332
Damien Millerb38eff82000-04-01 11:09:21 +10001333/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001334/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001335static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001336channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001337{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001338 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001339 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001340 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001341 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001342 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001343 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001344
1345 if (FD_ISSET(c->sock, readset)) {
1346 debug("X11 connection requested.");
1347 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001348 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001349 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001350 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001351 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001352 channel_close_fd(&c->sock);
1353 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001354 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001355 }
Damien Millerb38eff82000-04-01 11:09:21 +10001356 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001357 if (errno != EINTR && errno != EWOULDBLOCK &&
1358 errno != ECONNABORTED)
1359 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001360 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001361 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001362 return;
1363 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001364 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001365 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001366 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001367 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001368 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001369
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001370 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001371 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001372 c->local_window_max, c->local_maxpacket, 0, buf, 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001373 packet_start(SSH2_MSG_CHANNEL_OPEN);
1374 packet_put_cstring("x11");
1375 packet_put_int(nc->self);
1376 packet_put_int(nc->local_window_max);
1377 packet_put_int(nc->local_maxpacket);
1378 /* originator ipaddr and port */
1379 packet_put_cstring(remote_ipaddr);
1380 if (datafellows & SSH_BUG_X11FWD) {
1381 debug2("ssh2 x11 bug compat mode");
Damien Millerbd483e72000-04-30 10:00:53 +10001382 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001383 packet_put_int(remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001384 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001385 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001386 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001387 }
1388}
1389
Ben Lindstrombba81212001-06-25 05:01:22 +00001390static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001391port_open_helper(Channel *c, char *rtype)
1392{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001393 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001394 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001395 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001396 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001397 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001398
Damien Millerd6369432010-02-02 17:02:07 +11001399 if (remote_port == -1) {
1400 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001401 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001402 remote_ipaddr = xstrdup("127.0.0.1");
1403 remote_port = 65535;
1404 }
1405
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001406 snprintf(buf, sizeof buf,
1407 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001408 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001409 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001410 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001411
Darren Tuckera627d422013-06-02 07:31:17 +10001412 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001413 c->remote_name = xstrdup(buf);
1414
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001415 packet_start(SSH2_MSG_CHANNEL_OPEN);
1416 packet_put_cstring(rtype);
1417 packet_put_int(c->self);
1418 packet_put_int(c->local_window_max);
1419 packet_put_int(c->local_maxpacket);
1420 if (strcmp(rtype, "direct-tcpip") == 0) {
1421 /* target host, port */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001422 packet_put_cstring(c->path);
1423 packet_put_int(c->host_port);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001424 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1425 /* target path */
1426 packet_put_cstring(c->path);
1427 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1428 /* listen path */
1429 packet_put_cstring(c->path);
1430 } else {
1431 /* listen address, port */
1432 packet_put_cstring(c->path);
1433 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001434 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001435 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1436 /* reserved for future owner/mode info */
1437 packet_put_cstring("");
1438 } else {
1439 /* originator host and port */
1440 packet_put_cstring(remote_ipaddr);
1441 packet_put_int((u_int)remote_port);
1442 }
1443 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001444 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001445 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001446}
1447
Damien Miller5e7fd072005-11-05 14:53:39 +11001448static void
1449channel_set_reuseaddr(int fd)
1450{
1451 int on = 1;
1452
1453 /*
1454 * Set socket options.
1455 * Allow local port reuse in TIME_WAIT.
1456 */
1457 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1458 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1459}
1460
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001461void
1462channel_set_x11_refuse_time(u_int refuse_time)
1463{
1464 x11_refuse_time = refuse_time;
1465}
1466
Damien Millerb38eff82000-04-01 11:09:21 +10001467/*
1468 * This socket is listening for connections to a forwarded TCP/IP port.
1469 */
Damien Miller8473dd82006-07-24 14:08:32 +10001470/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001471static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001472channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001473{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001474 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001475 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001476 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001477 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001478 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001479
Damien Millerb38eff82000-04-01 11:09:21 +10001480 if (FD_ISSET(c->sock, readset)) {
1481 debug("Connection to port %d forwarding "
1482 "to %.100s port %d requested.",
1483 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001484
Damien Miller4623a752001-10-10 15:03:58 +10001485 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1486 nextstate = SSH_CHANNEL_OPENING;
1487 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001488 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1489 nextstate = SSH_CHANNEL_OPENING;
1490 rtype = "forwarded-streamlocal@openssh.com";
1491 } else if (c->host_port == PORT_STREAMLOCAL) {
1492 nextstate = SSH_CHANNEL_OPENING;
1493 rtype = "direct-streamlocal@openssh.com";
1494 } else if (c->host_port == 0) {
1495 nextstate = SSH_CHANNEL_DYNAMIC;
1496 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001497 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001498 nextstate = SSH_CHANNEL_OPENING;
1499 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001500 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001501
Damien Millerb38eff82000-04-01 11:09:21 +10001502 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001503 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001504 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001505 if (errno != EINTR && errno != EWOULDBLOCK &&
1506 errno != ECONNABORTED)
1507 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001508 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001509 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001510 return;
1511 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001512 if (c->host_port != PORT_STREAMLOCAL)
1513 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001514 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1515 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001516 nc->listening_port = c->listening_port;
1517 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001518 if (c->path != NULL)
1519 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001520
Darren Tucker876045b2010-01-08 17:08:00 +11001521 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001522 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001523 }
1524}
1525
1526/*
1527 * This is the authentication agent socket listening for connections from
1528 * clients.
1529 */
Damien Miller8473dd82006-07-24 14:08:32 +10001530/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001531static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001532channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001533{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001534 Channel *nc;
1535 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001536 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001537 socklen_t addrlen;
1538
1539 if (FD_ISSET(c->sock, readset)) {
1540 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001541 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001542 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001543 error("accept from auth socket: %.100s",
1544 strerror(errno));
1545 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001546 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001547 return;
1548 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001549 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001550 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1551 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001552 0, "accepted auth socket", 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001553 packet_start(SSH2_MSG_CHANNEL_OPEN);
1554 packet_put_cstring("auth-agent@openssh.com");
1555 packet_put_int(nc->self);
1556 packet_put_int(c->local_window_max);
1557 packet_put_int(c->local_maxpacket);
Damien Millerb38eff82000-04-01 11:09:21 +10001558 packet_send();
1559 }
1560}
1561
Damien Miller8473dd82006-07-24 14:08:32 +10001562/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001563static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001564channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001565{
Damien Millerbd740252008-05-19 15:37:09 +10001566 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001567 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001568
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001569 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001570 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001571 err = errno;
1572 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001573 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001574 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001575 debug("channel %d: connected to %s port %d",
1576 c->self, c->connect_ctx.host, c->connect_ctx.port);
1577 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001578 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001579 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1580 packet_put_int(c->remote_id);
1581 packet_put_int(c->self);
1582 packet_put_int(c->local_window);
1583 packet_put_int(c->local_maxpacket);
Ben Lindstrom69128662001-05-08 20:07:39 +00001584 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001585 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001586 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001587 /* Try next address, if any */
1588 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1589 close(c->sock);
1590 c->sock = c->rfd = c->wfd = sock;
1591 channel_max_fd = channel_find_maxfd();
1592 return;
1593 }
1594 /* Exhausted all addresses */
1595 error("connect_to %.100s port %d: failed.",
1596 c->connect_ctx.host, c->connect_ctx.port);
1597 channel_connect_ctx_free(&c->connect_ctx);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001598 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1599 packet_put_int(c->remote_id);
1600 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1601 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1602 packet_put_cstring(strerror(err));
1603 packet_put_cstring("");
Ben Lindstrom69128662001-05-08 20:07:39 +00001604 }
1605 chan_mark_dead(c);
1606 }
1607 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001608 }
1609}
1610
Damien Miller8473dd82006-07-24 14:08:32 +10001611/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001612static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001613channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001614{
Darren Tucker11327cc2005-03-14 23:22:25 +11001615 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001616 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001617
Damien Miller835284b2007-06-11 13:03:16 +10001618 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1619 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001620 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001621 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001622 if (len < 0 && (errno == EINTR ||
1623 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001624 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001625#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001626 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001627#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001628 if ((!c->isatty && len <= 0) ||
1629 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001630#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001631 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001632 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001633 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001634 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001635 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001636 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001637 } else {
1638 chan_read_failed(c);
1639 }
1640 return -1;
1641 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001642 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001643 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001644 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001645 chan_read_failed(c);
1646 }
Damien Millerd27b9472005-12-13 19:29:02 +11001647 } else if (c->datagram) {
1648 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001649 } else {
1650 buffer_append(&c->input, buf, len);
1651 }
Damien Millerb38eff82000-04-01 11:09:21 +10001652 }
1653 return 1;
1654}
Damien Miller4f7becb2006-03-26 14:10:14 +11001655
Damien Miller8473dd82006-07-24 14:08:32 +10001656/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001657static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001658channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001659{
Ben Lindstrome229b252001-03-05 06:28:06 +00001660 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001661 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001662 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001663 int len;
1664
1665 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001666 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001667 FD_ISSET(c->wfd, writeset) &&
1668 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001669 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001670 if (c->output_filter != NULL) {
1671 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1672 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001673 if (c->type != SSH_CHANNEL_OPEN)
1674 chan_mark_dead(c);
1675 else
1676 chan_write_failed(c);
1677 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001678 }
1679 } else if (c->datagram) {
1680 buf = data = buffer_get_string(&c->output, &dlen);
1681 } else {
1682 buf = data = buffer_ptr(&c->output);
1683 dlen = buffer_len(&c->output);
1684 }
1685
Damien Millerd27b9472005-12-13 19:29:02 +11001686 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001687 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001688 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001689 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001690 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1691 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001692 return 1;
1693 if (len <= 0) {
1694 if (c->type != SSH_CHANNEL_OPEN)
1695 chan_mark_dead(c);
1696 else
1697 chan_write_failed(c);
1698 return -1;
1699 }
Damien Miller7d457182010-08-05 23:09:48 +10001700 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001701 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001702#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001703 /* XXX: Later AIX versions can't push as much data to tty */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001704 if (c->wfd_isatty)
Darren Tucker240fdfa2003-11-22 14:10:02 +11001705 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001706#endif
Damien Miller077b2382005-12-31 16:22:32 +11001707
1708 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001709 if (len < 0 &&
1710 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001711 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001712 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001713 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001714 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001715 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001716 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001717 } else {
1718 chan_write_failed(c);
1719 }
1720 return -1;
1721 }
Darren Tucker3980b632009-08-28 11:02:37 +10001722#ifndef BROKEN_TCGETATTR_ICANON
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001723 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001724 if (tcgetattr(c->wfd, &tio) == 0 &&
1725 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1726 /*
1727 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001728 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001729 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001730 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001731 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001732 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001733 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001734 }
1735 }
Darren Tucker3980b632009-08-28 11:02:37 +10001736#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001737 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001738 }
Damien Miller7d457182010-08-05 23:09:48 +10001739 out:
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001740 if (olen > 0)
Damien Miller7d457182010-08-05 23:09:48 +10001741 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001742 return 1;
1743}
Damien Miller4f7becb2006-03-26 14:10:14 +11001744
Ben Lindstrombba81212001-06-25 05:01:22 +00001745static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001746channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001747{
Darren Tucker11327cc2005-03-14 23:22:25 +11001748 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001749 int len;
1750
Damien Miller1383bd82000-04-06 12:32:37 +10001751/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001752 if (c->efd != -1) {
1753 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1754 FD_ISSET(c->efd, writeset) &&
1755 buffer_len(&c->extended) > 0) {
1756 len = write(c->efd, buffer_ptr(&c->extended),
1757 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001758 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001759 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001760 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1761 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001762 return 1;
1763 if (len <= 0) {
1764 debug2("channel %d: closing write-efd %d",
1765 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001766 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001767 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001768 buffer_consume(&c->extended, len);
1769 c->local_consumed += len;
1770 }
Damien Miller8853ca52010-06-26 10:00:14 +10001771 } else if (c->efd != -1 &&
1772 (c->extended_usage == CHAN_EXTENDED_READ ||
1773 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001774 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001775 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001776 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001777 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001778 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1779 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001780 return 1;
1781 if (len <= 0) {
1782 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001783 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001784 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001785 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001786 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1787 debug3("channel %d: discard efd",
1788 c->self);
1789 } else
1790 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001791 }
Damien Miller33b13562000-04-04 14:38:59 +10001792 }
1793 }
1794 return 1;
1795}
Damien Miller4f7becb2006-03-26 14:10:14 +11001796
Damien Miller0e220db2004-06-15 10:34:08 +10001797static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001798channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001799{
Ben Lindstromb3921512001-04-11 15:57:50 +00001800 if (c->type == SSH_CHANNEL_OPEN &&
1801 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001802 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001803 c->local_maxpacket*3) ||
1804 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001805 c->local_consumed > 0) {
1806 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1807 packet_put_int(c->remote_id);
1808 packet_put_int(c->local_consumed);
1809 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001810 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001811 c->self, c->local_window,
1812 c->local_consumed);
1813 c->local_window += c->local_consumed;
1814 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001815 }
1816 return 1;
1817}
1818
Ben Lindstrombba81212001-06-25 05:01:22 +00001819static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001820channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001821{
1822 channel_handle_rfd(c, readset, writeset);
1823 channel_handle_wfd(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +10001824 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001825 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001826}
1827
Damien Millere1537f92010-01-26 13:26:22 +11001828static u_int
1829read_mux(Channel *c, u_int need)
1830{
1831 char buf[CHAN_RBUF];
1832 int len;
1833 u_int rlen;
1834
1835 if (buffer_len(&c->input) < need) {
1836 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001837 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001838 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1839 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001840 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001841 debug2("channel %d: ctl read<=0 rfd %d len %d",
1842 c->self, c->rfd, len);
1843 chan_read_failed(c);
1844 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001845 } else
1846 buffer_append(&c->input, buf, len);
1847 }
1848 return buffer_len(&c->input);
1849}
1850
1851static void
1852channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1853{
1854 u_int need;
1855 ssize_t len;
1856
Damien Millerd530f5f2010-05-21 14:57:10 +10001857 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001858 (c->istate == CHAN_INPUT_OPEN ||
1859 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1860 /*
1861 * Don't not read past the precise end of packets to
1862 * avoid disrupting fd passing.
1863 */
1864 if (read_mux(c, 4) < 4) /* read header */
1865 return;
1866 need = get_u32(buffer_ptr(&c->input));
1867#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1868 if (need > CHANNEL_MUX_MAX_PACKET) {
1869 debug2("channel %d: packet too big %u > %u",
1870 c->self, CHANNEL_MUX_MAX_PACKET, need);
1871 chan_rcvd_oclose(c);
1872 return;
1873 }
1874 if (read_mux(c, need + 4) < need + 4) /* read body */
1875 return;
1876 if (c->mux_rcb(c) != 0) {
1877 debug("channel %d: mux_rcb failed", c->self);
1878 chan_mark_dead(c);
1879 return;
1880 }
1881 }
1882
1883 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1884 buffer_len(&c->output) > 0) {
1885 len = write(c->wfd, buffer_ptr(&c->output),
1886 buffer_len(&c->output));
1887 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1888 return;
1889 if (len <= 0) {
1890 chan_mark_dead(c);
1891 return;
1892 }
1893 buffer_consume(&c->output, len);
1894 }
1895}
1896
1897static void
1898channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1899{
1900 Channel *nc;
1901 struct sockaddr_storage addr;
1902 socklen_t addrlen;
1903 int newsock;
1904 uid_t euid;
1905 gid_t egid;
1906
1907 if (!FD_ISSET(c->sock, readset))
1908 return;
1909
1910 debug("multiplexing control connection");
1911
1912 /*
1913 * Accept connection on control socket
1914 */
1915 memset(&addr, 0, sizeof(addr));
1916 addrlen = sizeof(addr);
1917 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1918 &addrlen)) == -1) {
1919 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001920 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001921 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001922 return;
1923 }
1924
1925 if (getpeereid(newsock, &euid, &egid) < 0) {
1926 error("%s getpeereid failed: %s", __func__,
1927 strerror(errno));
1928 close(newsock);
1929 return;
1930 }
1931 if ((euid != 0) && (getuid() != euid)) {
1932 error("multiplex uid mismatch: peer euid %u != uid %u",
1933 (u_int)euid, (u_int)getuid());
1934 close(newsock);
1935 return;
1936 }
1937 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
1938 newsock, newsock, -1, c->local_window_max,
1939 c->local_maxpacket, 0, "mux-control", 1);
1940 nc->mux_rcb = c->mux_rcb;
1941 debug3("%s: new mux channel %d fd %d", __func__,
1942 nc->self, nc->sock);
1943 /* establish state */
1944 nc->mux_rcb(nc);
1945 /* mux state transitions must not elicit protocol messages */
1946 nc->flags |= CHAN_LOCAL;
1947}
1948
Ben Lindstrombba81212001-06-25 05:01:22 +00001949static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001950channel_handler_init(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001951{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001952 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001953
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001954 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1955 channel_pre[i] = NULL;
1956 channel_post[i] = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001957 }
Damien Millerde6987c2002-01-22 23:20:40 +11001958 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001959 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001960 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001961 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001962 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
1963 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001964 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001965 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001966 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001967 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11001968 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
1969 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001970
Damien Millerde6987c2002-01-22 23:20:40 +11001971 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001972 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001973 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001974 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
1975 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001976 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001977 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001978 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001979 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11001980 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
1981 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001982}
1983
Damien Miller3ec27592001-10-12 11:35:04 +10001984/* gc dead channels */
1985static void
1986channel_garbage_collect(Channel *c)
1987{
1988 if (c == NULL)
1989 return;
1990 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001991 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001992 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001993 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001994 c->detach_user(c->self, NULL);
1995 /* if we still have a callback */
1996 if (c->detach_user != NULL)
1997 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001998 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001999 }
2000 if (!chan_is_dead(c, 1))
2001 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002002 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002003 channel_free(c);
2004}
2005
Ben Lindstrombba81212001-06-25 05:01:22 +00002006static void
Damien Millera6508752012-04-22 11:21:10 +10002007channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2008 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002009{
2010 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002011 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002012 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002013 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002014
2015 if (!did_init) {
2016 channel_handler_init();
2017 did_init = 1;
2018 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002019 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002020 if (unpause_secs != NULL)
2021 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002022 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002023 c = channels[i];
2024 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002025 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002026 if (c->delayed) {
2027 if (ftab == channel_pre)
2028 c->delayed = 0;
2029 else
2030 continue;
2031 }
Damien Millera6508752012-04-22 11:21:10 +10002032 if (ftab[c->type] != NULL) {
2033 /*
2034 * Run handlers that are not paused.
2035 */
2036 if (c->notbefore <= now)
2037 (*ftab[c->type])(c, readset, writeset);
2038 else if (unpause_secs != NULL) {
2039 /*
2040 * Collect the time that the earliest
2041 * channel comes off pause.
2042 */
2043 debug3("%s: chan %d: skip for %d more seconds",
2044 __func__, c->self,
2045 (int)(c->notbefore - now));
2046 if (*unpause_secs == 0 ||
2047 (c->notbefore - now) < *unpause_secs)
2048 *unpause_secs = c->notbefore - now;
2049 }
2050 }
Damien Miller3ec27592001-10-12 11:35:04 +10002051 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002052 }
Damien Millera6508752012-04-22 11:21:10 +10002053 if (unpause_secs != NULL && *unpause_secs != 0)
2054 debug3("%s: first channel unpauses in %d seconds",
2055 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002056}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002057
Ben Lindstrome9c99912001-06-09 00:41:05 +00002058/*
2059 * Allocate/update select bitmasks and add any bits relevant to channels in
2060 * select bitmasks.
2061 */
Damien Miller4af51302000-04-16 11:18:38 +10002062void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002063channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002064 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002065{
Damien Miller36812092006-03-26 14:22:47 +11002066 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002067
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002068 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002069
Damien Miller36812092006-03-26 14:22:47 +11002070 nfdset = howmany(n+1, NFDBITS);
2071 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002072 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002073 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2074 sz = nfdset * sizeof(fd_mask);
2075
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002076 /* perhaps check sz < nalloc/2 and shrink? */
2077 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002078 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2079 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002080 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002081 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002082 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002083 memset(*readsetp, 0, sz);
2084 memset(*writesetp, 0, sz);
2085
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002086 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002087 channel_handler(channel_pre, *readsetp, *writesetp,
2088 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002089}
2090
Ben Lindstrome9c99912001-06-09 00:41:05 +00002091/*
2092 * After select, perform any appropriate operations for channels which have
2093 * events pending.
2094 */
Damien Miller4af51302000-04-16 11:18:38 +10002095void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002096channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002097{
Damien Millera6508752012-04-22 11:21:10 +10002098 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099}
2100
Ben Lindstrome9c99912001-06-09 00:41:05 +00002101
Damien Miller5e953212001-01-30 09:14:00 +11002102/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002103void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002104channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002105{
Damien Millerb38eff82000-04-01 11:09:21 +10002106 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002107 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002108
Damien Miller95def091999-11-25 00:26:21 +11002109 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002110 c = channels[i];
2111 if (c == NULL)
2112 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002113
Ben Lindstrome9c99912001-06-09 00:41:05 +00002114 /*
2115 * We are only interested in channels that can have buffered
2116 * incoming data.
2117 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002118 if (c->type != SSH_CHANNEL_OPEN)
2119 continue;
2120 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002121 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002122 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002123 continue;
2124 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125
Damien Miller95def091999-11-25 00:26:21 +11002126 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002127 if ((c->istate == CHAN_INPUT_OPEN ||
2128 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2129 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002130 if (c->datagram) {
2131 if (len > 0) {
2132 u_char *data;
2133 u_int dlen;
2134
2135 data = buffer_get_string(&c->input,
2136 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002137 if (dlen > c->remote_window ||
2138 dlen > c->remote_maxpacket) {
2139 debug("channel %d: datagram "
2140 "too big for channel",
2141 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002142 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002143 continue;
2144 }
Damien Millerd27b9472005-12-13 19:29:02 +11002145 packet_start(SSH2_MSG_CHANNEL_DATA);
2146 packet_put_int(c->remote_id);
2147 packet_put_string(data, dlen);
2148 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002149 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002150 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002151 }
2152 continue;
2153 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002154 /*
2155 * Send some data for the other side over the secure
2156 * connection.
2157 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002158 if (len > c->remote_window)
2159 len = c->remote_window;
2160 if (len > c->remote_maxpacket)
2161 len = c->remote_maxpacket;
Damien Millerb38eff82000-04-01 11:09:21 +10002162 if (len > 0) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002163 packet_start(SSH2_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002164 packet_put_int(c->remote_id);
2165 packet_put_string(buffer_ptr(&c->input), len);
2166 packet_send();
2167 buffer_consume(&c->input, len);
2168 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002169 }
2170 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller5428f641999-11-25 11:54:57 +11002171 /*
2172 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002173 * tell peer, that we will not send more data: send IEOF.
2174 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002175 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002176 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002177 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2178 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002179 else
2180 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002181 }
Damien Miller33b13562000-04-04 14:38:59 +10002182 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002183 if (!(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002184 c->remote_window > 0 &&
2185 (len = buffer_len(&c->extended)) > 0 &&
2186 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002187 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002188 c->self, c->remote_window, buffer_len(&c->extended),
2189 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002190 if (len > c->remote_window)
2191 len = c->remote_window;
2192 if (len > c->remote_maxpacket)
2193 len = c->remote_maxpacket;
2194 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2195 packet_put_int(c->remote_id);
2196 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2197 packet_put_string(buffer_ptr(&c->extended), len);
2198 packet_send();
2199 buffer_consume(&c->extended, len);
2200 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002201 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002204}
2205
markus@openbsd.org8d057842016-09-30 09:19:13 +00002206/* -- mux proxy support */
2207
2208/*
2209 * When multiplexing channel messages for mux clients we have to deal
2210 * with downstream messages from the mux client and upstream messages
2211 * from the ssh server:
2212 * 1) Handling downstream messages is straightforward and happens
2213 * in channel_proxy_downstream():
2214 * - We forward all messages (mostly) unmodified to the server.
2215 * - However, in order to route messages from upstream to the correct
2216 * downstream client, we have to replace the channel IDs used by the
2217 * mux clients with a unique channel ID because the mux clients might
2218 * use conflicting channel IDs.
2219 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2220 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2221 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2222 * with the newly allocated channel ID.
2223 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2224 * channels and procesed by channel_proxy_upstream(). The local channel ID
2225 * is then translated back to the original mux client ID.
2226 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2227 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2228 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2229 * downstream mux client are removed.
2230 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2231 * requires more work, because they are not addressed to a specific
2232 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2233 * out whether the request is addressed to the local client or a
2234 * specific downstream client based on the listen-address/port.
2235 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2236 * not supported as the matching session/channel cannot be identified
2237 * easily.
2238 */
2239
2240/*
2241 * receive packets from downstream mux clients:
2242 * channel callback fired on read from mux client, creates
2243 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2244 * on channel creation.
2245 */
2246int
2247channel_proxy_downstream(Channel *downstream)
2248{
2249 Channel *c = NULL;
2250 struct ssh *ssh = active_state;
2251 struct sshbuf *original = NULL, *modified = NULL;
2252 const u_char *cp;
2253 char *ctype = NULL, *listen_host = NULL;
2254 u_char type;
2255 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002256 int ret = -1, r, idx;
2257 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002258
2259 /* sshbuf_dump(&downstream->input, stderr); */
2260 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2261 != 0) {
2262 error("%s: malformed message: %s", __func__, ssh_err(r));
2263 return -1;
2264 }
2265 if (have < 2) {
2266 error("%s: short message", __func__);
2267 return -1;
2268 }
2269 type = cp[1];
2270 /* skip padlen + type */
2271 cp += 2;
2272 have -= 2;
2273 if (ssh_packet_log_type(type))
2274 debug3("%s: channel %u: down->up: type %u", __func__,
2275 downstream->self, type);
2276
2277 switch (type) {
2278 case SSH2_MSG_CHANNEL_OPEN:
2279 if ((original = sshbuf_from(cp, have)) == NULL ||
2280 (modified = sshbuf_new()) == NULL) {
2281 error("%s: alloc", __func__);
2282 goto out;
2283 }
2284 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2285 (r = sshbuf_get_u32(original, &id)) != 0) {
2286 error("%s: parse error %s", __func__, ssh_err(r));
2287 goto out;
2288 }
2289 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2290 -1, -1, -1, 0, 0, 0, ctype, 1);
2291 c->mux_ctx = downstream; /* point to mux client */
2292 c->mux_downstream_id = id; /* original downstream id */
2293 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2294 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2295 (r = sshbuf_putb(modified, original)) != 0) {
2296 error("%s: compose error %s", __func__, ssh_err(r));
2297 channel_free(c);
2298 goto out;
2299 }
2300 break;
2301 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2302 /*
2303 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2304 * need to parse 'remote_id' instead of 'ctype'.
2305 */
2306 if ((original = sshbuf_from(cp, have)) == NULL ||
2307 (modified = sshbuf_new()) == NULL) {
2308 error("%s: alloc", __func__);
2309 goto out;
2310 }
2311 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2312 (r = sshbuf_get_u32(original, &id)) != 0) {
2313 error("%s: parse error %s", __func__, ssh_err(r));
2314 goto out;
2315 }
2316 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2317 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2318 c->mux_ctx = downstream; /* point to mux client */
2319 c->mux_downstream_id = id;
2320 c->remote_id = remote_id;
2321 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2322 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2323 (r = sshbuf_putb(modified, original)) != 0) {
2324 error("%s: compose error %s", __func__, ssh_err(r));
2325 channel_free(c);
2326 goto out;
2327 }
2328 break;
2329 case SSH2_MSG_GLOBAL_REQUEST:
2330 if ((original = sshbuf_from(cp, have)) == NULL) {
2331 error("%s: alloc", __func__);
2332 goto out;
2333 }
2334 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2335 error("%s: parse error %s", __func__, ssh_err(r));
2336 goto out;
2337 }
2338 if (strcmp(ctype, "tcpip-forward") != 0) {
2339 error("%s: unsupported request %s", __func__, ctype);
2340 goto out;
2341 }
2342 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2343 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2344 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2345 error("%s: parse error %s", __func__, ssh_err(r));
2346 goto out;
2347 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002348 if (listen_port > 65535) {
2349 error("%s: tcpip-forward for %s: bad port %u",
2350 __func__, listen_host, listen_port);
2351 goto out;
2352 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002353 /* Record that connection to this host/port is permitted. */
2354 permitted_opens = xreallocarray(permitted_opens,
2355 num_permitted_opens + 1, sizeof(*permitted_opens));
2356 idx = num_permitted_opens++;
2357 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2358 permitted_opens[idx].port_to_connect = -1;
2359 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002360 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002361 permitted_opens[idx].downstream = downstream;
2362 listen_host = NULL;
2363 break;
2364 case SSH2_MSG_CHANNEL_CLOSE:
2365 if (have < 4)
2366 break;
2367 remote_id = PEEK_U32(cp);
2368 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2369 if (c->flags & CHAN_CLOSE_RCVD)
2370 channel_free(c);
2371 else
2372 c->flags |= CHAN_CLOSE_SENT;
2373 }
2374 break;
2375 }
2376 if (modified) {
2377 if ((r = sshpkt_start(ssh, type)) != 0 ||
2378 (r = sshpkt_putb(ssh, modified)) != 0 ||
2379 (r = sshpkt_send(ssh)) != 0) {
2380 error("%s: send %s", __func__, ssh_err(r));
2381 goto out;
2382 }
2383 } else {
2384 if ((r = sshpkt_start(ssh, type)) != 0 ||
2385 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2386 (r = sshpkt_send(ssh)) != 0) {
2387 error("%s: send %s", __func__, ssh_err(r));
2388 goto out;
2389 }
2390 }
2391 ret = 0;
2392 out:
2393 free(ctype);
2394 free(listen_host);
2395 sshbuf_free(original);
2396 sshbuf_free(modified);
2397 return ret;
2398}
2399
2400/*
2401 * receive packets from upstream server and de-multiplex packets
2402 * to correct downstream:
2403 * implemented as a helper for channel input handlers,
2404 * replaces local (proxy) channel ID with downstream channel ID.
2405 */
2406int
2407channel_proxy_upstream(Channel *c, int type, u_int32_t seq, void *ctxt)
2408{
2409 struct ssh *ssh = active_state;
2410 struct sshbuf *b = NULL;
2411 Channel *downstream;
2412 const u_char *cp = NULL;
2413 size_t len;
2414 int r;
2415
2416 /*
2417 * When receiving packets from the peer we need to check whether we
2418 * need to forward the packets to the mux client. In this case we
2419 * restore the orignal channel id and keep track of CLOSE messages,
2420 * so we can cleanup the channel.
2421 */
2422 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2423 return 0;
2424 if ((downstream = c->mux_ctx) == NULL)
2425 return 0;
2426 switch (type) {
2427 case SSH2_MSG_CHANNEL_CLOSE:
2428 case SSH2_MSG_CHANNEL_DATA:
2429 case SSH2_MSG_CHANNEL_EOF:
2430 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2431 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2432 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2433 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2434 case SSH2_MSG_CHANNEL_SUCCESS:
2435 case SSH2_MSG_CHANNEL_FAILURE:
2436 case SSH2_MSG_CHANNEL_REQUEST:
2437 break;
2438 default:
2439 debug2("%s: channel %u: unsupported type %u", __func__,
2440 c->self, type);
2441 return 0;
2442 }
2443 if ((b = sshbuf_new()) == NULL) {
2444 error("%s: alloc reply", __func__);
2445 goto out;
2446 }
2447 /* get remaining payload (after id) */
2448 cp = sshpkt_ptr(ssh, &len);
2449 if (cp == NULL) {
2450 error("%s: no packet", __func__);
2451 goto out;
2452 }
2453 /* translate id and send to muxclient */
2454 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2455 (r = sshbuf_put_u8(b, type)) != 0 ||
2456 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2457 (r = sshbuf_put(b, cp, len)) != 0 ||
2458 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2459 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2460 goto out;
2461 }
2462 /* sshbuf_dump(b, stderr); */
2463 if (ssh_packet_log_type(type))
2464 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2465 type);
2466 out:
2467 /* update state */
2468 switch (type) {
2469 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2470 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2471 if (cp && len > 4)
2472 c->remote_id = PEEK_U32(cp);
2473 break;
2474 case SSH2_MSG_CHANNEL_CLOSE:
2475 if (c->flags & CHAN_CLOSE_SENT)
2476 channel_free(c);
2477 else
2478 c->flags |= CHAN_CLOSE_RCVD;
2479 break;
2480 }
2481 sshbuf_free(b);
2482 return 1;
2483}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002484
2485/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002486
2487/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002488int
Damien Miller630d6f42002-01-22 23:17:30 +11002489channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002490{
Damien Miller34132e52000-01-14 15:45:46 +11002491 int id;
Damien Miller633de332014-05-15 13:48:26 +10002492 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002493 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002494 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002495
Damien Miller95def091999-11-25 00:26:21 +11002496 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002497 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002498 c = channel_lookup(id);
2499 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002500 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002501 if (channel_proxy_upstream(c, type, seq, ctxt))
2502 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002503
Damien Miller95def091999-11-25 00:26:21 +11002504 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002505 if (c->type != SSH_CHANNEL_OPEN &&
2506 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002507 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002508
Damien Miller95def091999-11-25 00:26:21 +11002509 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002510 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002511 win_len = data_len;
2512 if (c->datagram)
2513 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002514
Damien Millera04ad492004-01-21 11:02:09 +11002515 /*
2516 * Ignore data for protocol > 1.3 if output end is no longer open.
2517 * For protocol 2 the sending side is reducing its window as it sends
2518 * data, so we must 'fake' consumption of the data in order to ensure
2519 * that window updates are sent back. Otherwise the connection might
2520 * deadlock.
2521 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002522 if (c->ostate != CHAN_OUTPUT_OPEN) {
2523 c->local_window -= win_len;
2524 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002525 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002526 }
2527
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002528 if (win_len > c->local_maxpacket) {
2529 logit("channel %d: rcvd big packet %d, maxpack %d",
2530 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002531 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002532 if (win_len > c->local_window) {
2533 logit("channel %d: rcvd too much data %d, win %d",
2534 c->self, win_len, c->local_window);
2535 return 0;
2536 }
2537 c->local_window -= win_len;
2538
Damien Millerd27b9472005-12-13 19:29:02 +11002539 if (c->datagram)
2540 buffer_put_string(&c->output, data, data_len);
2541 else
2542 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002543 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002544 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002545}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002546
Damien Millerd79b4242006-03-31 23:11:44 +11002547/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002548int
Damien Miller630d6f42002-01-22 23:17:30 +11002549channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002550{
2551 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002552 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002553 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002554 Channel *c;
2555
2556 /* Get the channel number and verify it. */
2557 id = packet_get_int();
2558 c = channel_lookup(id);
2559
2560 if (c == NULL)
2561 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002562 if (channel_proxy_upstream(c, type, seq, ctxt))
2563 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002564 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002565 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002566 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002567 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002568 if (c->flags & CHAN_EOF_RCVD) {
2569 if (datafellows & SSH_BUG_EXTEOF)
2570 debug("channel %d: accepting ext data after eof", id);
2571 else
2572 packet_disconnect("Received extended_data after EOF "
2573 "on channel %d.", id);
2574 }
Damien Miller33b13562000-04-04 14:38:59 +10002575 tcode = packet_get_int();
2576 if (c->efd == -1 ||
2577 c->extended_usage != CHAN_EXTENDED_WRITE ||
2578 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002579 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002580 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002581 }
2582 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002583 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002584 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002585 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002586 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002587 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002588 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002589 }
Damien Millerd3444942000-09-30 14:20:03 +11002590 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002591 c->local_window -= data_len;
2592 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002593 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002594 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002595}
2596
Damien Millerd79b4242006-03-31 23:11:44 +11002597/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002598int
Damien Miller630d6f42002-01-22 23:17:30 +11002599channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002600{
2601 int id;
2602 Channel *c;
2603
Damien Millerb38eff82000-04-01 11:09:21 +10002604 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002605 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002606 c = channel_lookup(id);
2607 if (c == NULL)
2608 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002609 if (channel_proxy_upstream(c, type, seq, ctxt))
2610 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002611 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002612
2613 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002614 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002615 debug("channel %d: FORCE input drain", c->self);
2616 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002617 if (buffer_len(&c->input) == 0)
2618 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002619 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002620 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002621}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002622
Damien Millerb38eff82000-04-01 11:09:21 +10002623/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002624/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002625int
Damien Miller630d6f42002-01-22 23:17:30 +11002626channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002627{
Damien Millerb38eff82000-04-01 11:09:21 +10002628 int id = packet_get_int();
2629 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002630
Damien Millerb38eff82000-04-01 11:09:21 +10002631 if (c == NULL)
2632 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002633 if (channel_proxy_upstream(c, type, seq, ctxt))
2634 return 0;
2635 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002636 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002637 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002638}
2639
Damien Millerd79b4242006-03-31 23:11:44 +11002640/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002641int
Damien Miller630d6f42002-01-22 23:17:30 +11002642channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002643{
2644 int id = packet_get_int();
2645 Channel *c = channel_lookup(id);
2646
2647 if (c == NULL)
2648 packet_disconnect("Received close confirmation for "
2649 "out-of-range channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002650 if (channel_proxy_upstream(c, type, seq, ctxt))
2651 return 0;
2652 packet_check_eom();
Damien Miller36187092013-06-10 13:07:11 +10002653 if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
Damien Millerb38eff82000-04-01 11:09:21 +10002654 packet_disconnect("Received close confirmation for "
2655 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002656 channel_free(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002657 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002658}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659
Damien Millerd79b4242006-03-31 23:11:44 +11002660/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002661int
Damien Miller630d6f42002-01-22 23:17:30 +11002662channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002663{
Damien Millerb38eff82000-04-01 11:09:21 +10002664 int id, remote_id;
2665 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002666
Damien Millerb38eff82000-04-01 11:09:21 +10002667 id = packet_get_int();
2668 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002669
markus@openbsd.org8d057842016-09-30 09:19:13 +00002670 if (c==NULL)
2671 packet_disconnect("Received open confirmation for "
2672 "unknown channel %d.", id);
2673 if (channel_proxy_upstream(c, type, seq, ctxt))
2674 return 0;
2675 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002676 packet_disconnect("Received open confirmation for "
2677 "non-opening channel %d.", id);
2678 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002679 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002680 c->remote_id = remote_id;
2681 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002682
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002683 c->remote_window = packet_get_int();
2684 c->remote_maxpacket = packet_get_int();
2685 if (c->open_confirm) {
2686 debug2("callback start");
2687 c->open_confirm(c->self, 1, c->open_confirm_ctx);
2688 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002689 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002690 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2691 c->remote_window, c->remote_maxpacket);
Damien Miller48b03fc2002-01-22 23:11:40 +11002692 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002693 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002694}
2695
Ben Lindstrombba81212001-06-25 05:01:22 +00002696static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002697reason2txt(int reason)
2698{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002699 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002700 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2701 return "administratively prohibited";
2702 case SSH2_OPEN_CONNECT_FAILED:
2703 return "connect failed";
2704 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2705 return "unknown channel type";
2706 case SSH2_OPEN_RESOURCE_SHORTAGE:
2707 return "resource shortage";
2708 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002709 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002710}
2711
Damien Millerd79b4242006-03-31 23:11:44 +11002712/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002713int
Damien Miller630d6f42002-01-22 23:17:30 +11002714channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002715{
Kevin Steves12057502001-02-05 14:54:34 +00002716 int id, reason;
2717 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002718 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002719
Damien Millerb38eff82000-04-01 11:09:21 +10002720 id = packet_get_int();
2721 c = channel_lookup(id);
2722
markus@openbsd.org8d057842016-09-30 09:19:13 +00002723 if (c==NULL)
2724 packet_disconnect("Received open failure for "
2725 "unknown channel %d.", id);
2726 if (channel_proxy_upstream(c, type, seq, ctxt))
2727 return 0;
2728 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002729 packet_disconnect("Received open failure for "
2730 "non-opening channel %d.", id);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002731 reason = packet_get_int();
2732 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2733 msg = packet_get_string(NULL);
2734 lang = packet_get_string(NULL);
2735 }
2736 logit("channel %d: open failed: %s%s%s", id,
2737 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2738 free(msg);
2739 free(lang);
2740 if (c->open_confirm) {
2741 debug2("callback start");
2742 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2743 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002744 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002745 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002746 /* Schedule the channel for cleanup/deletion. */
2747 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002748 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002749}
2750
Damien Millerd79b4242006-03-31 23:11:44 +11002751/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002752int
Damien Miller630d6f42002-01-22 23:17:30 +11002753channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002754{
2755 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002756 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00002757 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10002758
Damien Miller33b13562000-04-04 14:38:59 +10002759 /* Get the channel number and verify it. */
2760 id = packet_get_int();
2761 c = channel_lookup(id);
2762
Damien Millerd47c62a2005-12-13 19:33:57 +11002763 if (c == NULL) {
2764 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002765 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002766 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002767 if (channel_proxy_upstream(c, type, seq, ctxt))
2768 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002769 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002770 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002771 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00002772 if ((tmp = c->remote_window + adjust) < c->remote_window)
2773 fatal("channel %d: adjust %u overflows remote window %u",
2774 id, adjust, c->remote_window);
2775 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002776 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002777}
2778
Damien Millerd79b4242006-03-31 23:11:44 +11002779/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002780int
Damien Millerb84886b2008-05-19 15:05:07 +10002781channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2782{
2783 Channel *c;
2784 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002785 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002786
2787 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002788 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002789
Damien Miller16a73072008-12-08 09:55:25 +11002790 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11002791 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002792
Damien Miller16a73072008-12-08 09:55:25 +11002793 if ((c = channel_lookup(id)) == NULL) {
2794 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002795 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002796 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002797 if (channel_proxy_upstream(c, type, seq, ctxt))
2798 return 0;
2799 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10002800 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002801 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002802 cc->cb(type, c, cc->ctx);
2803 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002804 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002805 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002806 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002807}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002808
Ben Lindstrome9c99912001-06-09 00:41:05 +00002809/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002810
Ben Lindstrom908afed2001-10-03 17:34:59 +00002811void
2812channel_set_af(int af)
2813{
2814 IPv4or6 = af;
2815}
2816
Damien Millerf6dff7c2011-09-22 21:38:52 +10002817
2818/*
2819 * Determine whether or not a port forward listens to loopback, the
2820 * specified address or wildcard. On the client, a specified bind
2821 * address will always override gateway_ports. On the server, a
2822 * gateway_ports of 1 (``yes'') will override the client's specification
2823 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2824 * will bind to whatever address the client asked for.
2825 *
2826 * Special-case listen_addrs are:
2827 *
2828 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2829 * "" (empty string), "*" -> wildcard v4/v6
2830 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002831 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002832 */
2833static const char *
2834channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002835 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002836{
2837 const char *addr = NULL;
2838 int wildcard = 0;
2839
2840 if (listen_addr == NULL) {
2841 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002842 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002843 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002844 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002845 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2846 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2847 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002848 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002849 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002850 /*
2851 * Notify client if they requested a specific listen
2852 * address and it was overridden.
2853 */
2854 if (*listen_addr != '\0' &&
2855 strcmp(listen_addr, "0.0.0.0") != 0 &&
2856 strcmp(listen_addr, "*") != 0) {
2857 packet_send_debug("Forwarding listen address "
2858 "\"%s\" overridden by server "
2859 "GatewayPorts", listen_addr);
2860 }
Damien Miller602943d2014-07-04 08:59:41 +10002861 } else if (strcmp(listen_addr, "localhost") != 0 ||
2862 strcmp(listen_addr, "127.0.0.1") == 0 ||
2863 strcmp(listen_addr, "::1") == 0) {
2864 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002865 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002866 }
2867 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2868 strcmp(listen_addr, "::1") == 0) {
2869 /*
2870 * If a specific IPv4/IPv6 localhost address has been
2871 * requested then accept it even if gateway_ports is in
2872 * effect. This allows the client to prefer IPv4 or IPv6.
2873 */
2874 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002875 }
2876 if (wildcardp != NULL)
2877 *wildcardp = wildcard;
2878 return addr;
2879}
2880
Damien Millerb16461c2002-01-22 23:29:22 +11002881static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002882channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2883 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002884{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002885 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002886 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002887 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002888 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002889 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002890 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002891
Damien Millerf91ee4c2005-03-01 21:24:33 +11002892 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002893
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00002894 if (is_client && fwd->connect_path != NULL) {
2895 host = fwd->connect_path;
2896 } else {
2897 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2898 fwd->listen_host : fwd->connect_host;
2899 if (host == NULL) {
2900 error("No forward host name.");
2901 return 0;
2902 }
2903 if (strlen(host) >= NI_MAXHOST) {
2904 error("Forward host name too long.");
2905 return 0;
2906 }
Kevin Steves12057502001-02-05 14:54:34 +00002907 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002908
Damien Millerf6dff7c2011-09-22 21:38:52 +10002909 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002910 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2911 is_client, fwd_opts);
2912 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002913 type, wildcard, (addr == NULL) ? "NULL" : addr);
2914
2915 /*
Damien Miller34132e52000-01-14 15:45:46 +11002916 * getaddrinfo returns a loopback address if the hostname is
2917 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002918 */
Damien Miller34132e52000-01-14 15:45:46 +11002919 memset(&hints, 0, sizeof(hints));
2920 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002921 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002922 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002923 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002924 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2925 if (addr == NULL) {
2926 /* This really shouldn't happen */
2927 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002928 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002929 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002930 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11002931 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002932 }
Damien Millera7270302005-07-06 09:36:05 +10002933 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002934 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002935 if (allocated_listen_port != NULL)
2936 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11002937 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11002938 switch (ai->ai_family) {
2939 case AF_INET:
2940 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2941 sin_port;
2942 break;
2943 case AF_INET6:
2944 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2945 sin6_port;
2946 break;
2947 default:
Damien Miller34132e52000-01-14 15:45:46 +11002948 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11002949 }
2950 /*
2951 * If allocating a port for -R forwards, then use the
2952 * same port for all address families.
2953 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002954 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002955 allocated_listen_port != NULL && *allocated_listen_port > 0)
2956 *lport_p = htons(*allocated_listen_port);
2957
Damien Miller34132e52000-01-14 15:45:46 +11002958 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2959 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10002960 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11002961 continue;
2962 }
2963 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11002964 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002965 if (sock < 0) {
2966 /* this is no error since kernel may not support ipv6 */
2967 verbose("socket: %.100s", strerror(errno));
2968 continue;
2969 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002970
2971 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11002972 if (ai->ai_family == AF_INET6)
2973 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002974
Damien Miller4bf648f2009-02-14 16:28:21 +11002975 debug("Local forwarding listening on %s port %s.",
2976 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002977
Damien Miller34132e52000-01-14 15:45:46 +11002978 /* Bind the socket to the address. */
2979 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2980 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002981 if (!ai->ai_next)
2982 error("bind: %.100s", strerror(errno));
2983 else
2984 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002985
Damien Miller34132e52000-01-14 15:45:46 +11002986 close(sock);
2987 continue;
2988 }
2989 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002990 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002991 error("listen: %.100s", strerror(errno));
2992 close(sock);
2993 continue;
2994 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002995
2996 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10002997 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11002998 * record what we got.
2999 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003000 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003001 allocated_listen_port != NULL &&
3002 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003003 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003004 debug("Allocated listen port %d",
3005 *allocated_listen_port);
3006 }
3007
Damien Miller34132e52000-01-14 15:45:46 +11003008 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00003009 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003010 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003011 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003012 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003013 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003014 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003015 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003016 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3017 c->listening_port = *allocated_listen_port;
3018 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003019 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003020 success = 1;
3021 }
3022 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003023 error("%s: cannot listen to port: %d", __func__,
3024 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003025 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003026 return success;
Damien Miller95def091999-11-25 00:26:21 +11003027}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003028
Damien Miller7acefbb2014-07-18 14:11:24 +10003029static int
3030channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3031 struct ForwardOptions *fwd_opts)
3032{
3033 struct sockaddr_un sunaddr;
3034 const char *path;
3035 Channel *c;
3036 int port, sock;
3037 mode_t omask;
3038
3039 switch (type) {
3040 case SSH_CHANNEL_UNIX_LISTENER:
3041 if (fwd->connect_path != NULL) {
3042 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3043 error("Local connecting path too long: %s",
3044 fwd->connect_path);
3045 return 0;
3046 }
3047 path = fwd->connect_path;
3048 port = PORT_STREAMLOCAL;
3049 } else {
3050 if (fwd->connect_host == NULL) {
3051 error("No forward host name.");
3052 return 0;
3053 }
3054 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3055 error("Forward host name too long.");
3056 return 0;
3057 }
3058 path = fwd->connect_host;
3059 port = fwd->connect_port;
3060 }
3061 break;
3062 case SSH_CHANNEL_RUNIX_LISTENER:
3063 path = fwd->listen_path;
3064 port = PORT_STREAMLOCAL;
3065 break;
3066 default:
3067 error("%s: unexpected channel type %d", __func__, type);
3068 return 0;
3069 }
3070
3071 if (fwd->listen_path == NULL) {
3072 error("No forward path name.");
3073 return 0;
3074 }
3075 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3076 error("Local listening path too long: %s", fwd->listen_path);
3077 return 0;
3078 }
3079
3080 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3081
3082 /* Start a Unix domain listener. */
3083 omask = umask(fwd_opts->streamlocal_bind_mask);
3084 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3085 fwd_opts->streamlocal_bind_unlink);
3086 umask(omask);
3087 if (sock < 0)
3088 return 0;
3089
3090 debug("Local forwarding listening on path %s.", fwd->listen_path);
3091
3092 /* Allocate a channel number for the socket. */
3093 c = channel_new("unix listener", type, sock, sock, -1,
3094 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3095 0, "unix listener", 1);
3096 c->path = xstrdup(path);
3097 c->host_port = port;
3098 c->listening_port = PORT_STREAMLOCAL;
3099 c->listening_addr = xstrdup(fwd->listen_path);
3100 return 1;
3101}
3102
3103static int
3104channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003105{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003106 u_int i;
3107 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003108
Darren Tucker47eede72005-03-14 23:08:12 +11003109 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003110 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003111 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3112 continue;
3113 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3114 debug2("%s: close channel %d", __func__, i);
3115 channel_free(c);
3116 found = 1;
3117 }
3118 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003119
Damien Millerf6dff7c2011-09-22 21:38:52 +10003120 return (found);
3121}
3122
Damien Miller7acefbb2014-07-18 14:11:24 +10003123static int
3124channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003125{
3126 u_int i;
3127 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003128
3129 for (i = 0; i < channels_alloc; i++) {
3130 Channel *c = channels[i];
3131 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3132 continue;
3133 if (c->path == NULL)
3134 continue;
3135 if (strcmp(c->path, path) == 0) {
3136 debug2("%s: close channel %d", __func__, i);
3137 channel_free(c);
3138 found = 1;
3139 }
3140 }
3141
3142 return (found);
3143}
3144
3145int
3146channel_cancel_rport_listener(struct Forward *fwd)
3147{
3148 if (fwd->listen_path != NULL)
3149 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3150 else
3151 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3152}
3153
3154static int
3155channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3156 int cport, struct ForwardOptions *fwd_opts)
3157{
3158 u_int i;
3159 int found = 0;
3160 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003161
3162 for (i = 0; i < channels_alloc; i++) {
3163 Channel *c = channels[i];
3164 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3165 continue;
Damien Millerff773642011-09-22 21:39:48 +10003166 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003167 continue;
Damien Millerff773642011-09-22 21:39:48 +10003168 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3169 /* skip dynamic forwardings */
3170 if (c->host_port == 0)
3171 continue;
3172 } else {
3173 if (c->host_port != cport)
3174 continue;
3175 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003176 if ((c->listening_addr == NULL && addr != NULL) ||
3177 (c->listening_addr != NULL && addr == NULL))
3178 continue;
3179 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003180 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003181 channel_free(c);
3182 found = 1;
3183 }
3184 }
3185
3186 return (found);
3187}
3188
Damien Miller7acefbb2014-07-18 14:11:24 +10003189static int
3190channel_cancel_lport_listener_streamlocal(const char *path)
3191{
3192 u_int i;
3193 int found = 0;
3194
3195 if (path == NULL) {
3196 error("%s: no path specified.", __func__);
3197 return 0;
3198 }
3199
3200 for (i = 0; i < channels_alloc; i++) {
3201 Channel *c = channels[i];
3202 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3203 continue;
3204 if (c->listening_addr == NULL)
3205 continue;
3206 if (strcmp(c->listening_addr, path) == 0) {
3207 debug2("%s: close channel %d", __func__, i);
3208 channel_free(c);
3209 found = 1;
3210 }
3211 }
3212
3213 return (found);
3214}
3215
3216int
3217channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3218{
3219 if (fwd->listen_path != NULL)
3220 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3221 else
3222 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3223}
3224
Damien Millerb16461c2002-01-22 23:29:22 +11003225/* protocol local port fwd, used by ssh (and sshd in v1) */
3226int
Damien Miller7acefbb2014-07-18 14:11:24 +10003227channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003228{
Damien Miller7acefbb2014-07-18 14:11:24 +10003229 if (fwd->listen_path != NULL) {
3230 return channel_setup_fwd_listener_streamlocal(
3231 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3232 } else {
3233 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3234 fwd, NULL, fwd_opts);
3235 }
Damien Millerb16461c2002-01-22 23:29:22 +11003236}
3237
3238/* protocol v2 remote port fwd, used by sshd */
3239int
Damien Miller7acefbb2014-07-18 14:11:24 +10003240channel_setup_remote_fwd_listener(struct Forward *fwd,
3241 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003242{
Damien Miller7acefbb2014-07-18 14:11:24 +10003243 if (fwd->listen_path != NULL) {
3244 return channel_setup_fwd_listener_streamlocal(
3245 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3246 } else {
3247 return channel_setup_fwd_listener_tcpip(
3248 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3249 fwd_opts);
3250 }
Damien Millerb16461c2002-01-22 23:29:22 +11003251}
3252
Damien Miller5428f641999-11-25 11:54:57 +11003253/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003254 * Translate the requested rfwd listen host to something usable for
3255 * this server.
3256 */
3257static const char *
3258channel_rfwd_bind_host(const char *listen_host)
3259{
3260 if (listen_host == NULL) {
3261 if (datafellows & SSH_BUG_RFWD_ADDR)
3262 return "127.0.0.1";
3263 else
3264 return "localhost";
3265 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3266 if (datafellows & SSH_BUG_RFWD_ADDR)
3267 return "0.0.0.0";
3268 else
3269 return "";
3270 } else
3271 return listen_host;
3272}
3273
3274/*
Damien Miller5428f641999-11-25 11:54:57 +11003275 * Initiate forwarding of connections to port "port" on remote host through
3276 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003277 * Returns handle (index) for updating the dynamic listen port with
3278 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003279 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003280int
Damien Miller7acefbb2014-07-18 14:11:24 +10003281channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003282{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003283 int success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003284
Damien Miller95def091999-11-25 00:26:21 +11003285 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003286 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3287 if (fwd->listen_path != NULL) {
3288 packet_put_cstring("streamlocal-forward@openssh.com");
3289 packet_put_char(1); /* boolean: want reply */
3290 packet_put_cstring(fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003291 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003292 packet_put_cstring("tcpip-forward");
3293 packet_put_char(1); /* boolean: want reply */
3294 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3295 packet_put_int(fwd->listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11003296 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003297 packet_send();
3298 packet_write_wait();
3299 /* Assume that server accepts the request */
3300 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003301 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003302 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003303 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003304 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003305 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003306 if (fwd->connect_path != NULL) {
3307 permitted_opens[idx].host_to_connect =
3308 xstrdup(fwd->connect_path);
3309 permitted_opens[idx].port_to_connect =
3310 PORT_STREAMLOCAL;
3311 } else {
3312 permitted_opens[idx].host_to_connect =
3313 xstrdup(fwd->connect_host);
3314 permitted_opens[idx].port_to_connect =
3315 fwd->connect_port;
3316 }
3317 if (fwd->listen_path != NULL) {
3318 permitted_opens[idx].listen_host = NULL;
3319 permitted_opens[idx].listen_path =
3320 xstrdup(fwd->listen_path);
3321 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3322 } else {
3323 permitted_opens[idx].listen_host =
3324 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3325 permitted_opens[idx].listen_path = NULL;
3326 permitted_opens[idx].listen_port = fwd->listen_port;
3327 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003328 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003329 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003330 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003331}
3332
Damien Miller4b3ed642014-07-02 15:29:40 +10003333static int
3334open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003335 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003336{
3337 if (allowed_open->host_to_connect == NULL)
3338 return 0;
3339 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3340 allowed_open->port_to_connect != requestedport)
3341 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003342 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3343 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003344 return 0;
3345 return 1;
3346}
3347
3348/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003349 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003350 * we don't support FWD_PERMIT_ANY_PORT and
3351 * need to translate between the configured-host (listen_host)
3352 * and what we've sent to the remote server (channel_rfwd_bind_host)
3353 */
3354static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003355open_listen_match_tcpip(ForwardPermission *allowed_open,
3356 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003357{
3358 const char *allowed_host;
3359
3360 if (allowed_open->host_to_connect == NULL)
3361 return 0;
3362 if (allowed_open->listen_port != requestedport)
3363 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003364 if (!translate && allowed_open->listen_host == NULL &&
3365 requestedhost == NULL)
3366 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003367 allowed_host = translate ?
3368 channel_rfwd_bind_host(allowed_open->listen_host) :
3369 allowed_open->listen_host;
3370 if (allowed_host == NULL ||
3371 strcmp(allowed_host, requestedhost) != 0)
3372 return 0;
3373 return 1;
3374}
3375
Damien Miller7acefbb2014-07-18 14:11:24 +10003376static int
3377open_listen_match_streamlocal(ForwardPermission *allowed_open,
3378 const char *requestedpath)
3379{
3380 if (allowed_open->host_to_connect == NULL)
3381 return 0;
3382 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3383 return 0;
3384 if (allowed_open->listen_path == NULL ||
3385 strcmp(allowed_open->listen_path, requestedpath) != 0)
3386 return 0;
3387 return 1;
3388}
3389
Damien Miller5428f641999-11-25 11:54:57 +11003390/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003391 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003392 * local side.
3393 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003394static int
3395channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003396{
3397 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003398
Darren Tuckere7066df2004-05-24 10:18:05 +10003399 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003400 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003401 break;
3402 }
3403 if (i >= num_permitted_opens) {
3404 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003405 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003406 }
3407 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3408 packet_put_cstring("cancel-tcpip-forward");
3409 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003410 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003411 packet_put_int(port);
3412 packet_send();
3413
Damien Miller4b3ed642014-07-02 15:29:40 +10003414 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003415 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003416 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003417 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003418 free(permitted_opens[i].listen_host);
3419 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003420 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003421 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003422
3423 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003424}
3425
3426/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003427 * Request cancellation of remote forwarding of Unix domain socket
3428 * path from local side.
3429 */
3430static int
3431channel_request_rforward_cancel_streamlocal(const char *path)
3432{
3433 int i;
3434
Damien Miller7acefbb2014-07-18 14:11:24 +10003435 for (i = 0; i < num_permitted_opens; i++) {
3436 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3437 break;
3438 }
3439 if (i >= num_permitted_opens) {
3440 debug("%s: requested forward not found", __func__);
3441 return -1;
3442 }
3443 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3444 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3445 packet_put_char(0);
3446 packet_put_cstring(path);
3447 packet_send();
3448
3449 permitted_opens[i].listen_port = 0;
3450 permitted_opens[i].port_to_connect = 0;
3451 free(permitted_opens[i].host_to_connect);
3452 permitted_opens[i].host_to_connect = NULL;
3453 permitted_opens[i].listen_host = NULL;
3454 free(permitted_opens[i].listen_path);
3455 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003456 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003457
3458 return 0;
3459}
3460
3461/*
3462 * Request cancellation of remote forwarding of a connection from local side.
3463 */
3464int
3465channel_request_rforward_cancel(struct Forward *fwd)
3466{
3467 if (fwd->listen_path != NULL) {
3468 return (channel_request_rforward_cancel_streamlocal(
3469 fwd->listen_path));
3470 } else {
3471 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3472 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3473 }
3474}
3475
3476/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003477 * Permits opening to any host/port if permitted_opens[] is empty. This is
3478 * usually called by the server, because the user could connect to any port
3479 * anyway, and the server has no way to know but to trust the client anyway.
3480 */
3481void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003482channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003483{
3484 if (num_permitted_opens == 0)
3485 all_opens_permitted = 1;
3486}
3487
Ben Lindstroma3700052001-04-05 23:26:32 +00003488void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003489channel_add_permitted_opens(char *host, int port)
3490{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003491 debug("allow port forwarding to host %s port %d", host, port);
3492
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003493 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003494 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003495 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3496 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003497 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003498 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003499 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003500 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003501 num_permitted_opens++;
3502
3503 all_opens_permitted = 0;
3504}
3505
Darren Tucker68afb8c2011-10-02 18:59:03 +11003506/*
3507 * Update the listen port for a dynamic remote forward, after
3508 * the actual 'newport' has been allocated. If 'newport' < 0 is
3509 * passed then they entry will be invalidated.
3510 */
3511void
3512channel_update_permitted_opens(int idx, int newport)
3513{
3514 if (idx < 0 || idx >= num_permitted_opens) {
3515 debug("channel_update_permitted_opens: index out of range:"
3516 " %d num_permitted_opens %d", idx, num_permitted_opens);
3517 return;
3518 }
3519 debug("%s allowed port %d for forwarding to host %s port %d",
3520 newport > 0 ? "Updating" : "Removing",
3521 newport,
3522 permitted_opens[idx].host_to_connect,
3523 permitted_opens[idx].port_to_connect);
3524 if (newport >= 0) {
3525 permitted_opens[idx].listen_port =
3526 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3527 } else {
3528 permitted_opens[idx].listen_port = 0;
3529 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003530 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003531 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003532 free(permitted_opens[idx].listen_host);
3533 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003534 free(permitted_opens[idx].listen_path);
3535 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003536 }
3537}
3538
Damien Millera765cf42006-07-24 14:08:13 +10003539int
Damien Miller9b439df2006-07-24 14:04:00 +10003540channel_add_adm_permitted_opens(char *host, int port)
3541{
Damien Millera765cf42006-07-24 14:08:13 +10003542 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003543
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003544 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003545 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003546 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3547 = xstrdup(host);
3548 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003549 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003550 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003551 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003552 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003553}
3554
3555void
Damien Millerc6081482012-04-22 11:18:53 +10003556channel_disable_adm_local_opens(void)
3557{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003558 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003559 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003560 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3561 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003562}
3563
3564void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003565channel_clear_permitted_opens(void)
3566{
3567 int i;
3568
Damien Miller4b3ed642014-07-02 15:29:40 +10003569 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003570 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003571 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003572 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003573 }
Darren Tuckera627d422013-06-02 07:31:17 +10003574 free(permitted_opens);
3575 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003576 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003577}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003578
Damien Miller9b439df2006-07-24 14:04:00 +10003579void
3580channel_clear_adm_permitted_opens(void)
3581{
3582 int i;
3583
Damien Miller4b3ed642014-07-02 15:29:40 +10003584 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003585 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003586 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003587 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003588 }
Darren Tuckera627d422013-06-02 07:31:17 +10003589 free(permitted_adm_opens);
3590 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003591 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003592}
3593
Darren Tuckere7140f22008-06-10 23:01:51 +10003594void
3595channel_print_adm_permitted_opens(void)
3596{
Damien Miller6ef17492008-07-16 22:42:06 +10003597 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003598
Damien Millerb53d8a12009-01-28 16:13:04 +11003599 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003600 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003601 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003602 return;
3603 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003604 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003605 if (permitted_adm_opens[i].host_to_connect == NULL)
3606 printf(" none");
3607 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003608 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3609 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003610 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003611}
3612
Darren Tucker1338b9e2011-10-02 18:57:35 +11003613/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3614int
3615permitopen_port(const char *p)
3616{
3617 int port;
3618
3619 if (strcmp(p, "*") == 0)
3620 return FWD_PERMIT_ANY_PORT;
3621 if ((port = a2port(p)) > 0)
3622 return port;
3623 return -1;
3624}
3625
Damien Millerbd740252008-05-19 15:37:09 +10003626/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003627static int
Damien Millerbd740252008-05-19 15:37:09 +10003628connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003629{
Damien Millerbd740252008-05-19 15:37:09 +10003630 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003631 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00003632 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003633
Damien Millerbd740252008-05-19 15:37:09 +10003634 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003635 switch (cctx->ai->ai_family) {
3636 case AF_UNIX:
3637 /* unix:pathname instead of host:port */
3638 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3639 strlcpy(ntop, "unix", sizeof(ntop));
3640 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3641 break;
3642 case AF_INET:
3643 case AF_INET6:
3644 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3645 ntop, sizeof(ntop), strport, sizeof(strport),
3646 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3647 error("connect_next: getnameinfo failed");
3648 continue;
3649 }
3650 break;
3651 default:
Damien Miller34132e52000-01-14 15:45:46 +11003652 continue;
3653 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003654 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3655 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003656 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003657 error("socket: %.100s", strerror(errno));
3658 else
3659 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003660 continue;
3661 }
Damien Miller232711f2004-06-15 10:35:30 +10003662 if (set_nonblock(sock) == -1)
3663 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003664 if (connect(sock, cctx->ai->ai_addr,
3665 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3666 debug("connect_next: host %.100s ([%.100s]:%s): "
3667 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003668 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003669 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003670 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003671 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003672 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003673 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003674 if (cctx->ai->ai_family != AF_UNIX)
3675 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003676 debug("connect_next: host %.100s ([%.100s]:%s) "
3677 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3678 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003679 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003680 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003681 return -1;
3682}
Damien Millerb38eff82000-04-01 11:09:21 +10003683
Damien Millerbd740252008-05-19 15:37:09 +10003684static void
3685channel_connect_ctx_free(struct channel_connect *cctx)
3686{
Darren Tuckera627d422013-06-02 07:31:17 +10003687 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003688 if (cctx->aitop) {
3689 if (cctx->aitop->ai_family == AF_UNIX)
3690 free(cctx->aitop);
3691 else
3692 freeaddrinfo(cctx->aitop);
3693 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003694 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003695}
3696
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003697/*
3698 * Return CONNECTING channel to remote host:port or local socket path,
3699 * passing back the failure reason if appropriate.
3700 */
Damien Millerbd740252008-05-19 15:37:09 +10003701static Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003702connect_to_reason(const char *name, int port, char *ctype, char *rname,
3703 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003704{
3705 struct addrinfo hints;
3706 int gaierr;
3707 int sock = -1;
3708 char strport[NI_MAXSERV];
3709 struct channel_connect cctx;
3710 Channel *c;
3711
Damien Miller2bcb8662008-07-12 17:12:29 +10003712 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003713
3714 if (port == PORT_STREAMLOCAL) {
3715 struct sockaddr_un *sunaddr;
3716 struct addrinfo *ai;
3717
3718 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3719 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3720 return (NULL);
3721 }
3722
3723 /*
3724 * Fake up a struct addrinfo for AF_UNIX connections.
3725 * channel_connect_ctx_free() must check ai_family
3726 * and use free() not freeaddirinfo() for AF_UNIX.
3727 */
3728 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3729 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3730 ai->ai_addr = (struct sockaddr *)(ai + 1);
3731 ai->ai_addrlen = sizeof(*sunaddr);
3732 ai->ai_family = AF_UNIX;
3733 ai->ai_socktype = SOCK_STREAM;
3734 ai->ai_protocol = PF_UNSPEC;
3735 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3736 sunaddr->sun_family = AF_UNIX;
3737 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3738 cctx.aitop = ai;
3739 } else {
3740 memset(&hints, 0, sizeof(hints));
3741 hints.ai_family = IPv4or6;
3742 hints.ai_socktype = SOCK_STREAM;
3743 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003744 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3745 != 0) {
3746 if (errmsg != NULL)
3747 *errmsg = ssh_gai_strerror(gaierr);
3748 if (reason != NULL)
3749 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003750 error("connect_to %.100s: unknown host (%s)", name,
3751 ssh_gai_strerror(gaierr));
3752 return NULL;
3753 }
Damien Millerbd740252008-05-19 15:37:09 +10003754 }
3755
Damien Miller7acefbb2014-07-18 14:11:24 +10003756 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003757 cctx.port = port;
3758 cctx.ai = cctx.aitop;
3759
3760 if ((sock = connect_next(&cctx)) == -1) {
3761 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003762 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003763 channel_connect_ctx_free(&cctx);
3764 return NULL;
3765 }
3766 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3767 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3768 c->connect_ctx = cctx;
3769 return c;
3770}
3771
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003772/* Return CONNECTING channel to remote host:port or local socket path */
3773static Channel *
3774connect_to(const char *name, int port, char *ctype, char *rname)
3775{
3776 return connect_to_reason(name, port, ctype, rname, NULL, NULL);
3777}
3778
markus@openbsd.org8d057842016-09-30 09:19:13 +00003779/*
3780 * returns either the newly connected channel or the downstream channel
3781 * that needs to deal with this connection.
3782 */
Damien Millerbd740252008-05-19 15:37:09 +10003783Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003784channel_connect_by_listen_address(const char *listen_host,
3785 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003786{
3787 int i;
3788
3789 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003790 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003791 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00003792 if (permitted_opens[i].downstream)
3793 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10003794 return connect_to(
3795 permitted_opens[i].host_to_connect,
3796 permitted_opens[i].port_to_connect, ctype, rname);
3797 }
3798 }
3799 error("WARNING: Server requests forwarding for unknown listen_port %d",
3800 listen_port);
3801 return NULL;
3802}
3803
Damien Miller7acefbb2014-07-18 14:11:24 +10003804Channel *
3805channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3806{
3807 int i;
3808
3809 for (i = 0; i < num_permitted_opens; i++) {
3810 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3811 return connect_to(
3812 permitted_opens[i].host_to_connect,
3813 permitted_opens[i].port_to_connect, ctype, rname);
3814 }
3815 }
3816 error("WARNING: Server requests forwarding for unknown path %.100s",
3817 path);
3818 return NULL;
3819}
3820
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003821/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003822Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003823channel_connect_to_port(const char *host, u_short port, char *ctype,
3824 char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003825{
Damien Miller9b439df2006-07-24 14:04:00 +10003826 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003827
3828 permit = all_opens_permitted;
3829 if (!permit) {
3830 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003831 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003832 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003833 break;
3834 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003835 }
Damien Miller9b439df2006-07-24 14:04:00 +10003836
3837 if (num_adm_permitted_opens > 0) {
3838 permit_adm = 0;
3839 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003840 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003841 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003842 break;
3843 }
Damien Miller9b439df2006-07-24 14:04:00 +10003844 }
3845
3846 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003847 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003848 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003849 if (reason != NULL)
3850 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10003851 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003852 }
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003853 return connect_to_reason(host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003854}
3855
Damien Miller7acefbb2014-07-18 14:11:24 +10003856/* Check if connecting to that path is permitted and connect. */
3857Channel *
3858channel_connect_to_path(const char *path, char *ctype, char *rname)
3859{
3860 int i, permit, permit_adm = 1;
3861
3862 permit = all_opens_permitted;
3863 if (!permit) {
3864 for (i = 0; i < num_permitted_opens; i++)
3865 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3866 permit = 1;
3867 break;
3868 }
3869 }
3870
3871 if (num_adm_permitted_opens > 0) {
3872 permit_adm = 0;
3873 for (i = 0; i < num_adm_permitted_opens; i++)
3874 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3875 permit_adm = 1;
3876 break;
3877 }
3878 }
3879
3880 if (!permit || !permit_adm) {
3881 logit("Received request to connect to path %.100s, "
3882 "but the request was denied.", path);
3883 return NULL;
3884 }
3885 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3886}
3887
Damien Miller0e220db2004-06-15 10:34:08 +10003888void
3889channel_send_window_changes(void)
3890{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003891 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003892 struct winsize ws;
3893
3894 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003895 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003896 channels[i]->type != SSH_CHANNEL_OPEN)
3897 continue;
3898 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3899 continue;
3900 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003901 packet_put_int((u_int)ws.ws_col);
3902 packet_put_int((u_int)ws.ws_row);
3903 packet_put_int((u_int)ws.ws_xpixel);
3904 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003905 packet_send();
3906 }
3907}
3908
Ben Lindstrome9c99912001-06-09 00:41:05 +00003909/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003910
Damien Miller5428f641999-11-25 11:54:57 +11003911/*
3912 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003913 * Returns 0 and a suitable display number for the DISPLAY variable
3914 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003915 */
Kevin Steves366298c2001-12-19 17:58:01 +00003916int
Damien Miller95c249f2002-02-05 12:11:34 +11003917x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003918 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003919{
Damien Millere7378562001-12-21 14:58:35 +11003920 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003921 int display_number, sock;
3922 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003923 struct addrinfo hints, *ai, *aitop;
3924 char strport[NI_MAXSERV];
3925 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003926
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003927 if (chanids == NULL)
3928 return -1;
3929
Damien Millera34a28b1999-12-14 10:47:15 +11003930 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11003931 display_number < MAX_DISPLAYS;
3932 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11003933 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11003934 memset(&hints, 0, sizeof(hints));
3935 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11003936 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11003937 hints.ai_socktype = SOCK_STREAM;
3938 snprintf(strport, sizeof strport, "%d", port);
3939 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003940 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00003941 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003942 }
Damien Miller34132e52000-01-14 15:45:46 +11003943 for (ai = aitop; ai; ai = ai->ai_next) {
3944 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3945 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11003946 sock = socket(ai->ai_family, ai->ai_socktype,
3947 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003948 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11003949 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
3950#ifdef EPFNOSUPPORT
3951 && (errno != EPFNOSUPPORT)
3952#endif
3953 ) {
Damien Millere2192732000-01-17 13:22:55 +11003954 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10003955 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00003956 return -1;
Damien Millere2192732000-01-17 13:22:55 +11003957 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11003958 debug("x11_create_display_inet: Socket family %d not supported",
3959 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11003960 continue;
3961 }
Damien Miller34132e52000-01-14 15:45:46 +11003962 }
Damien Miller04ee0f82009-11-18 17:48:30 +11003963 if (ai->ai_family == AF_INET6)
3964 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10003965 if (x11_use_localhost)
3966 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11003967 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003968 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003969 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11003970
Damien Miller34132e52000-01-14 15:45:46 +11003971 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11003972 close(socks[n]);
3973 }
3974 num_socks = 0;
3975 break;
3976 }
3977 socks[num_socks++] = sock;
3978 if (num_socks == NUM_SOCKS)
3979 break;
Damien Miller95def091999-11-25 00:26:21 +11003980 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00003981 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11003982 if (num_socks > 0)
3983 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003984 }
Damien Miller95def091999-11-25 00:26:21 +11003985 if (display_number >= MAX_DISPLAYS) {
3986 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00003987 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003988 }
Damien Miller95def091999-11-25 00:26:21 +11003989 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11003990 for (n = 0; n < num_socks; n++) {
3991 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11003992 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003993 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003994 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00003995 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11003996 }
Damien Miller95def091999-11-25 00:26:21 +11003997 }
Damien Miller34132e52000-01-14 15:45:46 +11003998
Damien Miller34132e52000-01-14 15:45:46 +11003999 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004000 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004001 for (n = 0; n < num_socks; n++) {
4002 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11004003 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004004 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4005 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004006 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004007 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004008 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004009 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004010 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004011
Kevin Steves366298c2001-12-19 17:58:01 +00004012 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004013 *display_numberp = display_number;
4014 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004015}
4016
Ben Lindstrombba81212001-06-25 05:01:22 +00004017static int
Damien Miller819dbb62009-01-21 16:46:26 +11004018connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004019{
Damien Miller95def091999-11-25 00:26:21 +11004020 int sock;
4021 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004022
Damien Miller3afe3752001-12-21 12:39:51 +11004023 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4024 if (sock < 0)
4025 error("socket: %.100s", strerror(errno));
4026 memset(&addr, 0, sizeof(addr));
4027 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004028 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004029 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004030 return sock;
4031 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004032 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4033 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004034}
4035
Damien Miller819dbb62009-01-21 16:46:26 +11004036static int
4037connect_local_xsocket(u_int dnr)
4038{
4039 char buf[1024];
4040 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4041 return connect_local_xsocket_path(buf);
4042}
4043
Darren Tuckerda39b092017-03-10 13:22:32 +11004044#ifdef __APPLE__
4045static int
4046is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4047{
4048 struct stat sbuf;
4049
4050 if (strlcpy(path, display, pathlen) >= pathlen) {
4051 error("%s: display path too long", __func__);
4052 return 0;
4053 }
4054 if (display[0] != '/')
4055 return 0;
4056 if (stat(path, &sbuf) == 0) {
4057 return 1;
4058 } else {
4059 char *dot = strrchr(path, '.');
4060 if (dot != NULL) {
4061 *dot = '\0';
4062 if (stat(path, &sbuf) == 0) {
4063 return 1;
4064 }
4065 }
4066 }
4067 return 0;
4068}
4069#endif
4070
Damien Millerbd483e72000-04-30 10:00:53 +10004071int
4072x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004073{
Damien Miller57c4e872006-03-31 23:11:07 +11004074 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004075 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004076 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004077 struct addrinfo hints, *ai, *aitop;
4078 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004079 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004080
Damien Miller95def091999-11-25 00:26:21 +11004081 /* Try to open a socket for the local X server. */
4082 display = getenv("DISPLAY");
4083 if (!display) {
4084 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004085 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004086 }
Damien Miller5428f641999-11-25 11:54:57 +11004087 /*
4088 * Now we decode the value of the DISPLAY variable and make a
4089 * connection to the real X server.
4090 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004091
Damien Miller819dbb62009-01-21 16:46:26 +11004092#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004093 /* Check if display is a path to a socket (as set by launchd). */
4094 {
4095 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004096
Darren Tuckerda39b092017-03-10 13:22:32 +11004097 if (is_path_to_xsocket(display, path, sizeof(path))) {
4098 debug("x11_connect_display: $DISPLAY is launchd");
4099
4100 /* Create a socket. */
4101 sock = connect_local_xsocket_path(path);
4102 if (sock < 0)
4103 return -1;
4104
4105 /* OK, we now have a connection to the display. */
4106 return sock;
4107 }
Damien Miller819dbb62009-01-21 16:46:26 +11004108 }
4109#endif
Damien Miller5428f641999-11-25 11:54:57 +11004110 /*
4111 * Check if it is a unix domain socket. Unix domain displays are in
4112 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4113 */
Damien Miller95def091999-11-25 00:26:21 +11004114 if (strncmp(display, "unix:", 5) == 0 ||
4115 display[0] == ':') {
4116 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004117 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004118 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004119 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004120 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004121 }
4122 /* Create a socket. */
4123 sock = connect_local_xsocket(display_number);
4124 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004125 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004126
4127 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004128 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004129 }
Damien Miller5428f641999-11-25 11:54:57 +11004130 /*
4131 * Connect to an inet socket. The DISPLAY value is supposedly
4132 * hostname:d[.s], where hostname may also be numeric IP address.
4133 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004134 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004135 cp = strchr(buf, ':');
4136 if (!cp) {
4137 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004138 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004139 }
Damien Miller95def091999-11-25 00:26:21 +11004140 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004141 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004142 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004143 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004144 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004145 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004146 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004147
Damien Miller34132e52000-01-14 15:45:46 +11004148 /* Look up the host address */
4149 memset(&hints, 0, sizeof(hints));
4150 hints.ai_family = IPv4or6;
4151 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004152 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004153 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004154 error("%.100s: unknown host. (%s)", buf,
4155 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004156 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004157 }
Damien Miller34132e52000-01-14 15:45:46 +11004158 for (ai = aitop; ai; ai = ai->ai_next) {
4159 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004160 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004161 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004162 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004163 continue;
4164 }
4165 /* Connect it to the display. */
4166 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004167 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004168 6000 + display_number, strerror(errno));
4169 close(sock);
4170 continue;
4171 }
4172 /* Success */
4173 break;
Damien Miller34132e52000-01-14 15:45:46 +11004174 }
Damien Miller34132e52000-01-14 15:45:46 +11004175 freeaddrinfo(aitop);
4176 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004177 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004178 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004179 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004180 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004181 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004182 return sock;
4183}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004184
Damien Millerbd483e72000-04-30 10:00:53 +10004185/*
Damien Miller5428f641999-11-25 11:54:57 +11004186 * Requests forwarding of X11 connections, generates fake authentication
4187 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004188 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004189 */
Damien Miller4af51302000-04-16 11:18:38 +10004190void
Damien Miller17e7ed02005-06-17 12:54:33 +10004191x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004192 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004193{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004194 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004195 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004196 char *new_data;
4197 int screen_number;
4198 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004199
Damien Millerf92c0792005-07-06 09:45:26 +10004200 if (x11_saved_display == NULL)
4201 x11_saved_display = xstrdup(disp);
4202 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004203 error("x11_request_forwarding_with_spoofing: different "
4204 "$DISPLAY already forwarded");
4205 return;
4206 }
4207
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004208 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004209 if (cp)
4210 cp = strchr(cp, '.');
4211 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004212 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004213 else
4214 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004215
Damien Miller13390022005-07-06 09:44:19 +10004216 if (x11_saved_proto == NULL) {
4217 /* Save protocol name. */
4218 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004219
4220 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004221 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004222 for (i = 0; i < data_len; i++) {
4223 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4224 fatal("x11_request_forwarding: bad "
4225 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004226 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004227 }
4228 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004229
4230 /* Generate fake data of the same length. */
4231 x11_fake_data = xmalloc(data_len);
4232 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004233 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004234 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004235
Damien Miller95def091999-11-25 00:26:21 +11004236 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004237 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004238
Damien Miller95def091999-11-25 00:26:21 +11004239 /* Send the request packet. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00004240 channel_request_start(client_session_id, "x11-req", want_reply);
4241 packet_put_char(0); /* XXX bool single connection */
Damien Millerbd483e72000-04-30 10:00:53 +10004242 packet_put_cstring(proto);
4243 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004244 packet_put_int(screen_number);
4245 packet_send();
4246 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004247 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004248}