blob: d030fcdd9010fb3165d0f1de5396eb575230e415 [file] [log] [blame]
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00001/* $OpenBSD: channels.c,v 1.357 2017/02/01 02:59:09 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <netdb.h>
Darren Tucker37f92202015-02-23 03:07:24 +110059#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000060#include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100062#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100063#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100064#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110065#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100066#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100067#include <stdarg.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Millerb84886b2008-05-19 15:05:07 +100069#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "ssh1.h"
73#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000074#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100078#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100082#include "key.h"
83#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110084#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrome9c99912001-06-09 00:41:05 +000086/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * Pointer to an array containing all allocated channels. The array is
90 * dynamically extended as needed.
91 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000092static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000096 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110097 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100098static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000102 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100103 */
Damien Miller5e953212001-01-30 09:14:00 +1100104static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Ben Lindstrome9c99912001-06-09 00:41:05 +0000107/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * Data structure for storing which hosts are permitted for forward requests.
111 * The local sides of any remote forwards are stored in this array to prevent
112 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
113 * network (which might be behind a firewall).
114 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000115/* XXX: streamlocal wants a path instead of host:port */
116/* Overload host_to_connect; we could just make this match Forward */
117/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100118typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000119 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000120 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000121 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000122 char *listen_path; /* Remote side should listen path. */
123 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000124 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125} ForwardPermission;
126
Damien Miller9b439df2006-07-24 14:04:00 +1000127/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000128static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000129
Damien Miller9b439df2006-07-24 14:04:00 +1000130/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000131static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000132
133/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000135
136/* Number of permitted host/port pair in the array permitted by the admin. */
137static int num_adm_permitted_opens = 0;
138
Darren Tucker1338b9e2011-10-02 18:57:35 +1100139/* special-case port number meaning allow any port */
140#define FWD_PERMIT_ANY_PORT 0
141
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000142/* special-case wildcard meaning allow any host */
143#define FWD_PERMIT_ANY_HOST "*"
144
Damien Miller5428f641999-11-25 11:54:57 +1100145/*
146 * If this is true, all opens are permitted. This is the case on the server
147 * on which we have to trust the client anyway, and the user could do
148 * anything after logging in anyway.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150static int all_opens_permitted = 0;
151
Ben Lindstrome9c99912001-06-09 00:41:05 +0000152
153/* -- X11 forwarding */
154
155/* Maximum number of fake X11 displays to try. */
156#define MAX_DISPLAYS 1000
157
Damien Miller13390022005-07-06 09:44:19 +1000158/* Saved X11 local (client) display. */
159static char *x11_saved_display = NULL;
160
Ben Lindstrome9c99912001-06-09 00:41:05 +0000161/* Saved X11 authentication protocol name. */
162static char *x11_saved_proto = NULL;
163
164/* Saved X11 authentication data. This is the real data. */
165static char *x11_saved_data = NULL;
166static u_int x11_saved_data_len = 0;
167
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000168/* Deadline after which all X11 connections are refused */
169static u_int x11_refuse_time;
170
Ben Lindstrome9c99912001-06-09 00:41:05 +0000171/*
172 * Fake X11 authentication data. This is what the server will be sending us;
173 * we should replace any occurrences of this by the real data.
174 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100175static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000176static u_int x11_fake_data_len;
177
178
179/* -- agent forwarding */
180
181#define NUM_SOCKS 10
182
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000183/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000184static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000185
Ben Lindstrome9c99912001-06-09 00:41:05 +0000186/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static void port_open_helper(Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000188static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000189
Damien Millerbd740252008-05-19 15:37:09 +1000190/* non-blocking connect helpers */
191static int connect_next(struct channel_connect *);
192static void channel_connect_ctx_free(struct channel_connect *);
193
Ben Lindstrome9c99912001-06-09 00:41:05 +0000194/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000195
196Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100197channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000198{
199 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000200
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000201 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100202 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000203 return NULL;
204 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000205 c = channels[id];
206 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100207 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000208 return NULL;
209 }
210 return c;
211}
212
markus@openbsd.org8d057842016-09-30 09:19:13 +0000213Channel *
214channel_by_remote_id(int remote_id)
215{
216 Channel *c;
217 u_int i;
218
219 for (i = 0; i < channels_alloc; i++) {
220 c = channels[i];
221 if (c != NULL && c->remote_id == remote_id)
222 return c;
223 }
224 return NULL;
225}
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100228 * Returns the channel if it is allowed to receive protocol messages.
229 * Private channels, like listening sockets, may not receive messages.
230 */
231Channel *
232channel_lookup(int id)
233{
234 Channel *c;
235
236 if ((c = channel_by_id(id)) == NULL)
237 return (NULL);
238
Damien Millerd62f2ca2006-03-26 13:57:41 +1100239 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100240 case SSH_CHANNEL_X11_OPEN:
241 case SSH_CHANNEL_LARVAL:
242 case SSH_CHANNEL_CONNECTING:
243 case SSH_CHANNEL_DYNAMIC:
244 case SSH_CHANNEL_OPENING:
245 case SSH_CHANNEL_OPEN:
246 case SSH_CHANNEL_INPUT_DRAINING:
247 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller36187092013-06-10 13:07:11 +1000248 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000249 case SSH_CHANNEL_MUX_PROXY:
Damien Millerd47c62a2005-12-13 19:33:57 +1100250 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100251 }
252 logit("Non-public channel %d, type %d.", id, c->type);
253 return (NULL);
254}
255
256/*
Damien Millere247cc42000-05-07 12:03:14 +1000257 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000258 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100259 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000260static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100261channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000262 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263{
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Update the maximum file descriptor value. */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000265 channel_max_fd = MAXIMUM(channel_max_fd, rfd);
266 channel_max_fd = MAXIMUM(channel_max_fd, wfd);
267 channel_max_fd = MAXIMUM(channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100268
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100269 if (rfd != -1)
270 fcntl(rfd, F_SETFD, FD_CLOEXEC);
271 if (wfd != -1 && wfd != rfd)
272 fcntl(wfd, F_SETFD, FD_CLOEXEC);
273 if (efd != -1 && efd != rfd && efd != wfd)
274 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000275
Damien Miller6f83b8e2000-05-02 09:23:45 +1000276 c->rfd = rfd;
277 c->wfd = wfd;
278 c->sock = (rfd == wfd) ? rfd : -1;
279 c->efd = efd;
280 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100281
Darren Tuckered3cdc02008-06-16 23:29:18 +1000282 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000283 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000284#ifdef _AIX
285 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000286 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000287#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100288
Damien Miller69b69aa2000-10-28 14:19:58 +1100289 /* enable nonblocking mode */
290 if (nonblock) {
291 if (rfd != -1)
292 set_nonblock(rfd);
293 if (wfd != -1)
294 set_nonblock(wfd);
295 if (efd != -1)
296 set_nonblock(efd);
297 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000298}
299
300/*
301 * Allocate a new channel object and set its type and socket. This will cause
302 * remote_name to be freed.
303 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000305channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000306 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000307{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000308 int found;
309 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000310 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Miller95def091999-11-25 00:26:21 +1100312 /* Do initial allocation if this is the first call. */
313 if (channels_alloc == 0) {
314 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100315 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100316 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000317 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100318 }
319 /* Try to find a free slot where to put the new channel. */
320 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000321 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100322 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000323 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100324 break;
325 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000326 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100327 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100328 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000329 if (channels_alloc > 10000)
330 fatal("channel_new: internal error: channels_alloc %d "
331 "too big.", channels_alloc);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000332 channels = xreallocarray(channels, channels_alloc + 10,
Damien Miller36812092006-03-26 14:22:47 +1100333 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000334 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100335 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100336 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000337 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100338 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000339 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100340 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100341 buffer_init(&c->input);
342 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000343 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100344 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000345 c->listening_addr = NULL;
346 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100347 c->ostate = CHAN_OUTPUT_OPEN;
348 c->istate = CHAN_INPUT_OPEN;
349 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000350 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000351 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100352 c->self = found;
353 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000354 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000355 c->local_window = window;
356 c->local_window_max = window;
357 c->local_consumed = 0;
358 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100359 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000360 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000361 c->remote_window = 0;
362 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000363 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100364 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000365 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100366 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000367 c->open_confirm = NULL;
368 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000369 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100370 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000371 c->filter_ctx = NULL;
372 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100373 c->ctl_chan = -1;
374 c->mux_rcb = NULL;
375 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000376 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100377 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000378 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100379 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000380 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000382
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000383static int
384channel_find_maxfd(void)
385{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000386 u_int i;
387 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000388 Channel *c;
389
390 for (i = 0; i < channels_alloc; i++) {
391 c = channels[i];
392 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000393 max = MAXIMUM(max, c->rfd);
394 max = MAXIMUM(max, c->wfd);
395 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000396 }
397 }
398 return max;
399}
400
401int
402channel_close_fd(int *fdp)
403{
404 int ret = 0, fd = *fdp;
405
406 if (fd != -1) {
407 ret = close(fd);
408 *fdp = -1;
409 if (fd == channel_max_fd)
410 channel_max_fd = channel_find_maxfd();
411 }
412 return ret;
413}
414
Damien Miller6f83b8e2000-05-02 09:23:45 +1000415/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000417channel_close_fds(Channel *c)
418{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000419 channel_close_fd(&c->sock);
420 channel_close_fd(&c->rfd);
421 channel_close_fd(&c->wfd);
422 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000423}
424
425/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000426void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000427channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000429 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000430 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000431 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000432 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000433
markus@openbsd.org8d057842016-09-30 09:19:13 +0000434 for (n = 0, i = 0; i < channels_alloc; i++) {
435 if ((other = channels[i]) != NULL) {
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000436 n++;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000437
438 /* detach from mux client and prepare for closing */
439 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
440 other->type == SSH_CHANNEL_MUX_PROXY &&
441 other->mux_ctx == c) {
442 other->mux_ctx = NULL;
443 other->type = SSH_CHANNEL_OPEN;
444 other->istate = CHAN_INPUT_CLOSED;
445 other->ostate = CHAN_OUTPUT_CLOSED;
446 }
447 }
448 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000449 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000450 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000451
markus@openbsd.org8d057842016-09-30 09:19:13 +0000452 /* XXX more MUX cleanup: remove remote forwardings */
453 if (c->type == SSH_CHANNEL_MUX_CLIENT) {
454 for (i = 0; i < (u_int)num_permitted_opens; i++) {
455 if (permitted_opens[i].downstream != c)
456 continue;
457 /* cancel on the server, since mux client is gone */
458 debug("channel %d: cleanup remote forward for %s:%u",
459 c->self,
460 permitted_opens[i].listen_host,
461 permitted_opens[i].listen_port);
462 packet_start(SSH2_MSG_GLOBAL_REQUEST);
463 packet_put_cstring("cancel-tcpip-forward");
464 packet_put_char(0);
465 packet_put_cstring(channel_rfwd_bind_host(
466 permitted_opens[i].listen_host));
467 packet_put_int(permitted_opens[i].listen_port);
468 packet_send();
469 /* unregister */
470 permitted_opens[i].listen_port = 0;
471 permitted_opens[i].port_to_connect = 0;
472 free(permitted_opens[i].host_to_connect);
473 permitted_opens[i].host_to_connect = NULL;
474 free(permitted_opens[i].listen_host);
475 permitted_opens[i].listen_host = NULL;
476 permitted_opens[i].listen_path = NULL;
477 permitted_opens[i].downstream = NULL;
478 }
479 }
480
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000481 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000482 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000483 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000484
Damien Miller6f83b8e2000-05-02 09:23:45 +1000485 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000486 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000487 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000488 buffer_free(&c->input);
489 buffer_free(&c->output);
490 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000491 free(c->remote_name);
492 c->remote_name = NULL;
493 free(c->path);
494 c->path = NULL;
495 free(c->listening_addr);
496 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000497 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
498 if (cc->abandon_cb != NULL)
499 cc->abandon_cb(c, cc->ctx);
500 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100501 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000502 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000503 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000504 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
505 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000506 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000507 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508}
509
Ben Lindstrome9c99912001-06-09 00:41:05 +0000510void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000511channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000512{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000513 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000514
Ben Lindstrom601e4362001-06-21 03:19:23 +0000515 for (i = 0; i < channels_alloc; i++)
516 if (channels[i] != NULL)
517 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000518}
519
520/*
521 * Closes the sockets/fds of all channels. This is used to close extra file
522 * descriptors after a fork.
523 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000524void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000525channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000526{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000527 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000528
529 for (i = 0; i < channels_alloc; i++)
530 if (channels[i] != NULL)
531 channel_close_fds(channels[i]);
532}
533
534/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000535 * Stop listening to channels.
536 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000537void
538channel_stop_listening(void)
539{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000540 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000541 Channel *c;
542
543 for (i = 0; i < channels_alloc; i++) {
544 c = channels[i];
545 if (c != NULL) {
546 switch (c->type) {
547 case SSH_CHANNEL_AUTH_SOCKET:
548 case SSH_CHANNEL_PORT_LISTENER:
549 case SSH_CHANNEL_RPORT_LISTENER:
550 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000551 case SSH_CHANNEL_UNIX_LISTENER:
552 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000553 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000554 channel_free(c);
555 break;
556 }
557 }
558 }
559}
560
561/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000562 * Returns true if no channel has too much buffered data, and false if one or
563 * more channel is overfull.
564 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000565int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000566channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000567{
568 u_int i;
569 Channel *c;
570
571 for (i = 0; i < channels_alloc; i++) {
572 c = channels[i];
573 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000574#if 0
575 if (!compat20 &&
576 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100577 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578 c->self, buffer_len(&c->input));
579 return 0;
580 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000581#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000582 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000583 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000584 c->self, buffer_len(&c->output),
585 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000586 return 0;
587 }
588 }
589 }
590 return 1;
591}
592
593/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000594int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000595channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000596{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000597 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 Channel *c;
599
600 for (i = 0; i < channels_alloc; i++) {
601 c = channels[i];
602 if (c == NULL)
603 continue;
604 switch (c->type) {
605 case SSH_CHANNEL_X11_LISTENER:
606 case SSH_CHANNEL_PORT_LISTENER:
607 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100608 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000609 case SSH_CHANNEL_CLOSED:
610 case SSH_CHANNEL_AUTH_SOCKET:
611 case SSH_CHANNEL_DYNAMIC:
612 case SSH_CHANNEL_CONNECTING:
613 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000614 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000615 case SSH_CHANNEL_UNIX_LISTENER:
616 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000617 continue;
618 case SSH_CHANNEL_LARVAL:
619 if (!compat20)
620 fatal("cannot happen: SSH_CHANNEL_LARVAL");
621 continue;
622 case SSH_CHANNEL_OPENING:
623 case SSH_CHANNEL_OPEN:
624 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100625 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000626 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000627 return 1;
628 case SSH_CHANNEL_INPUT_DRAINING:
629 case SSH_CHANNEL_OUTPUT_DRAINING:
630 if (!compat13)
631 fatal("cannot happen: OUT_DRAIN");
632 return 1;
633 default:
634 fatal("channel_still_open: bad channel type %d", c->type);
635 /* NOTREACHED */
636 }
637 }
638 return 0;
639}
640
641/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000642int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000643channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000645 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646 Channel *c;
647
648 for (i = 0; i < channels_alloc; i++) {
649 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000650 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000651 continue;
652 switch (c->type) {
653 case SSH_CHANNEL_CLOSED:
654 case SSH_CHANNEL_DYNAMIC:
655 case SSH_CHANNEL_X11_LISTENER:
656 case SSH_CHANNEL_PORT_LISTENER:
657 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100658 case SSH_CHANNEL_MUX_LISTENER:
659 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000660 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661 case SSH_CHANNEL_OPENING:
662 case SSH_CHANNEL_CONNECTING:
663 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000664 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000665 case SSH_CHANNEL_UNIX_LISTENER:
666 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000667 continue;
668 case SSH_CHANNEL_LARVAL:
669 case SSH_CHANNEL_AUTH_SOCKET:
670 case SSH_CHANNEL_OPEN:
671 case SSH_CHANNEL_X11_OPEN:
672 return i;
673 case SSH_CHANNEL_INPUT_DRAINING:
674 case SSH_CHANNEL_OUTPUT_DRAINING:
675 if (!compat13)
676 fatal("cannot happen: OUT_DRAIN");
677 return i;
678 default:
679 fatal("channel_find_open: bad channel type %d", c->type);
680 /* NOTREACHED */
681 }
682 }
683 return -1;
684}
685
Ben Lindstrome9c99912001-06-09 00:41:05 +0000686/*
687 * Returns a message describing the currently open forwarded connections,
688 * suitable for sending to the client. The message contains crlf pairs for
689 * newlines.
690 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000691char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000692channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000693{
694 Buffer buffer;
695 Channel *c;
696 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000697 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698
699 buffer_init(&buffer);
700 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
701 buffer_append(&buffer, buf, strlen(buf));
702 for (i = 0; i < channels_alloc; i++) {
703 c = channels[i];
704 if (c == NULL)
705 continue;
706 switch (c->type) {
707 case SSH_CHANNEL_X11_LISTENER:
708 case SSH_CHANNEL_PORT_LISTENER:
709 case SSH_CHANNEL_RPORT_LISTENER:
710 case SSH_CHANNEL_CLOSED:
711 case SSH_CHANNEL_AUTH_SOCKET:
712 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000713 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100714 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000715 case SSH_CHANNEL_UNIX_LISTENER:
716 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000717 continue;
718 case SSH_CHANNEL_LARVAL:
719 case SSH_CHANNEL_OPENING:
720 case SSH_CHANNEL_CONNECTING:
721 case SSH_CHANNEL_DYNAMIC:
722 case SSH_CHANNEL_OPEN:
723 case SSH_CHANNEL_X11_OPEN:
724 case SSH_CHANNEL_INPUT_DRAINING:
725 case SSH_CHANNEL_OUTPUT_DRAINING:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000726 case SSH_CHANNEL_MUX_PROXY:
727 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000728 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000729 " #%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 +0000730 c->self, c->remote_name,
731 c->type, c->remote_id,
732 c->istate, buffer_len(&c->input),
733 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100734 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000735 buffer_append(&buffer, buf, strlen(buf));
736 continue;
737 default:
738 fatal("channel_open_message: bad channel type %d", c->type);
739 /* NOTREACHED */
740 }
741 }
742 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000743 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744 buffer_free(&buffer);
745 return cp;
746}
747
748void
749channel_send_open(int id)
750{
751 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000752
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000754 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000755 return;
756 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000757 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000758 packet_start(SSH2_MSG_CHANNEL_OPEN);
759 packet_put_cstring(c->ctype);
760 packet_put_int(c->self);
761 packet_put_int(c->local_window);
762 packet_put_int(c->local_maxpacket);
763 packet_send();
764}
765
766void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000767channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000768{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000769 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000770
Ben Lindstrome9c99912001-06-09 00:41:05 +0000771 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000772 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000773 return;
774 }
Damien Miller0e220db2004-06-15 10:34:08 +1000775 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000776 packet_start(SSH2_MSG_CHANNEL_REQUEST);
777 packet_put_int(c->remote_id);
778 packet_put_cstring(service);
779 packet_put_char(wantconfirm);
780}
Damien Miller4f7becb2006-03-26 14:10:14 +1100781
Ben Lindstrome9c99912001-06-09 00:41:05 +0000782void
Damien Millerb84886b2008-05-19 15:05:07 +1000783channel_register_status_confirm(int id, channel_confirm_cb *cb,
784 channel_confirm_abandon_cb *abandon_cb, void *ctx)
785{
786 struct channel_confirm *cc;
787 Channel *c;
788
789 if ((c = channel_lookup(id)) == NULL)
790 fatal("channel_register_expect: %d: bad id", id);
791
Damien Miller6c81fee2013-11-08 12:19:55 +1100792 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000793 cc->cb = cb;
794 cc->abandon_cb = abandon_cb;
795 cc->ctx = ctx;
796 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
797}
798
799void
Damien Millerd530f5f2010-05-21 14:57:10 +1000800channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000801{
802 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000803
Ben Lindstrome9c99912001-06-09 00:41:05 +0000804 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100805 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000806 return;
807 }
Damien Millerb84886b2008-05-19 15:05:07 +1000808 c->open_confirm = fn;
809 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000810}
Damien Miller4f7becb2006-03-26 14:10:14 +1100811
Ben Lindstrome9c99912001-06-09 00:41:05 +0000812void
Damien Miller39eda6e2005-11-05 14:52:50 +1100813channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000814{
Damien Millerd47c62a2005-12-13 19:33:57 +1100815 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000816
Ben Lindstrome9c99912001-06-09 00:41:05 +0000817 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000818 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000819 return;
820 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000821 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100822 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000823}
Damien Miller4f7becb2006-03-26 14:10:14 +1100824
Ben Lindstrome9c99912001-06-09 00:41:05 +0000825void
826channel_cancel_cleanup(int id)
827{
Damien Millerd47c62a2005-12-13 19:33:57 +1100828 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000829
Ben Lindstrome9c99912001-06-09 00:41:05 +0000830 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000831 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000832 return;
833 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000834 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100835 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000836}
Damien Miller4f7becb2006-03-26 14:10:14 +1100837
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838void
Damien Miller077b2382005-12-31 16:22:32 +1100839channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000840 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000841{
842 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000843
Ben Lindstrome9c99912001-06-09 00:41:05 +0000844 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000845 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000846 return;
847 }
Damien Miller077b2382005-12-31 16:22:32 +1100848 c->input_filter = ifn;
849 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000850 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000851 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000852}
853
854void
855channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000856 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000857{
858 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000859
Ben Lindstrome9c99912001-06-09 00:41:05 +0000860 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
861 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000862 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000863 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100864 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000865 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
866 packet_put_int(c->remote_id);
867 packet_put_int(c->local_window);
868 packet_send();
869}
870
Damien Miller5428f641999-11-25 11:54:57 +1100871/*
Damien Millerb38eff82000-04-01 11:09:21 +1000872 * 'channel_pre*' are called just before select() to add any bits relevant to
873 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100874 */
Damien Millerb38eff82000-04-01 11:09:21 +1000875/*
876 * 'channel_post*': perform any appropriate operations for channels which
877 * have events pending.
878 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100879typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000880chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
881chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
882
Damien Miller8473dd82006-07-24 14:08:32 +1000883/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000884static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100885channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000886{
887 FD_SET(c->sock, readset);
888}
889
Damien Miller8473dd82006-07-24 14:08:32 +1000890/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000891static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100892channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000893{
894 debug3("channel %d: waiting for connection", c->self);
895 FD_SET(c->sock, writeset);
896}
897
Ben Lindstrombba81212001-06-25 05:01:22 +0000898static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100899channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000900{
901 if (buffer_len(&c->input) < packet_get_maxsize())
902 FD_SET(c->sock, readset);
903 if (buffer_len(&c->output) > 0)
904 FD_SET(c->sock, writeset);
905}
906
Ben Lindstrombba81212001-06-25 05:01:22 +0000907static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100908channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000909{
Damien Millerde6987c2002-01-22 23:20:40 +1100910 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000911
Damien Miller33b13562000-04-04 14:38:59 +1000912 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100913 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000914 buffer_len(&c->input) < limit &&
915 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000916 FD_SET(c->rfd, readset);
917 if (c->ostate == CHAN_OUTPUT_OPEN ||
918 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
919 if (buffer_len(&c->output) > 0) {
920 FD_SET(c->wfd, writeset);
921 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000922 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000923 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
924 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000925 else
926 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000927 }
928 }
929 /** XXX check close conditions, too */
Damien Millerd654dd22008-05-19 16:05:41 +1000930 if (compat20 && c->efd != -1 &&
931 !(c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000932 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
933 buffer_len(&c->extended) > 0)
934 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000935 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
936 (c->extended_usage == CHAN_EXTENDED_READ ||
937 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000938 buffer_len(&c->extended) < c->remote_window)
939 FD_SET(c->efd, readset);
940 }
Damien Miller0e220db2004-06-15 10:34:08 +1000941 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000942}
943
Damien Miller8473dd82006-07-24 14:08:32 +1000944/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100946channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000947{
948 if (buffer_len(&c->input) == 0) {
949 packet_start(SSH_MSG_CHANNEL_CLOSE);
950 packet_put_int(c->remote_id);
951 packet_send();
952 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000953 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000954 }
955}
956
Damien Miller8473dd82006-07-24 14:08:32 +1000957/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000958static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100959channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000960{
961 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000962 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000963 else
Damien Millerb38eff82000-04-01 11:09:21 +1000964 FD_SET(c->sock, writeset);
965}
966
967/*
968 * This is a special state for X11 authentication spoofing. An opened X11
969 * connection (when authentication spoofing is being done) remains in this
970 * state until the first packet has been completely read. The authentication
971 * data in that packet is then substituted by the real data if it matches the
972 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000973 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000974 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000975 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000976static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000977x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000978{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000979 u_char *ucp;
980 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000981
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000982 /* Is this being called after the refusal deadline? */
983 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
984 verbose("Rejected X11 connection after ForwardX11Timeout "
985 "expired");
986 return -1;
987 }
988
Damien Millerb38eff82000-04-01 11:09:21 +1000989 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000990 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000991 return 0;
992
993 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100994 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000995 if (ucp[0] == 0x42) { /* Byte order MSB first. */
996 proto_len = 256 * ucp[6] + ucp[7];
997 data_len = 256 * ucp[8] + ucp[9];
998 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
999 proto_len = ucp[6] + 256 * ucp[7];
1000 data_len = ucp[8] + 256 * ucp[9];
1001 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001002 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001003 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001004 return -1;
1005 }
1006
1007 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00001008 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001009 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1010 return 0;
1011
1012 /* Check if authentication protocol matches. */
1013 if (proto_len != strlen(x11_saved_proto) ||
1014 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001015 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001016 return -1;
1017 }
1018 /* Check if authentication data matches our fake data. */
1019 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001020 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +10001021 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001022 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001023 return -1;
1024 }
1025 /* Check fake data length */
1026 if (x11_fake_data_len != x11_saved_data_len) {
1027 error("X11 fake_data_len %d != saved_data_len %d",
1028 x11_fake_data_len, x11_saved_data_len);
1029 return -1;
1030 }
1031 /*
1032 * Received authentication protocol and data match
1033 * our fake data. Substitute the fake data with real
1034 * data.
1035 */
1036 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
1037 x11_saved_data, x11_saved_data_len);
1038 return 1;
1039}
1040
Ben Lindstrombba81212001-06-25 05:01:22 +00001041static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001042channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001043{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001044 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001045
Damien Millerb38eff82000-04-01 11:09:21 +10001046 if (ret == 1) {
1047 /* Start normal processing for the channel. */
1048 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +10001049 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001050 } else if (ret == -1) {
1051 /*
1052 * We have received an X11 connection that has bad
1053 * authentication information.
1054 */
Damien Miller996acd22003-04-09 20:59:48 +10001055 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +10001056 buffer_clear(&c->input);
1057 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001058 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +10001059 c->sock = -1;
1060 c->type = SSH_CHANNEL_CLOSED;
1061 packet_start(SSH_MSG_CHANNEL_CLOSE);
1062 packet_put_int(c->remote_id);
1063 packet_send();
1064 }
1065}
1066
Ben Lindstrombba81212001-06-25 05:01:22 +00001067static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001068channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001069{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001070 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001071
1072 /* c->force_drain = 1; */
1073
Damien Millerb38eff82000-04-01 11:09:21 +10001074 if (ret == 1) {
1075 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +11001076 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001077 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001078 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +10001079 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +11001080 chan_read_failed(c);
1081 buffer_clear(&c->input);
1082 chan_ibuf_empty(c);
1083 buffer_clear(&c->output);
1084 /* for proto v1, the peer will send an IEOF */
1085 if (compat20)
1086 chan_write_failed(c);
1087 else
1088 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +10001089 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001090 }
1091}
1092
Damien Millere1537f92010-01-26 13:26:22 +11001093static void
1094channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1095{
Damien Millerd530f5f2010-05-21 14:57:10 +10001096 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001097 buffer_check_alloc(&c->input, CHAN_RBUF))
1098 FD_SET(c->rfd, readset);
1099 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1100 /* clear buffer immediately (discard any partial packet) */
1101 buffer_clear(&c->input);
1102 chan_ibuf_empty(c);
1103 /* Start output drain. XXX just kill chan? */
1104 chan_rcvd_oclose(c);
1105 }
1106 if (c->ostate == CHAN_OUTPUT_OPEN ||
1107 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1108 if (buffer_len(&c->output) > 0)
1109 FD_SET(c->wfd, writeset);
1110 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1111 chan_obuf_empty(c);
1112 }
1113}
1114
Ben Lindstromb3921512001-04-11 15:57:50 +00001115/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001116/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001117static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001118channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001119{
Damien Millera10f5612002-09-12 09:49:15 +10001120 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001121 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001122 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001123 struct {
1124 u_int8_t version;
1125 u_int8_t command;
1126 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001127 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001128 } s4_req, s4_rsp;
1129
Ben Lindstromb3921512001-04-11 15:57:50 +00001130 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001131
1132 have = buffer_len(&c->input);
1133 len = sizeof(s4_req);
1134 if (have < len)
1135 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001136 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001137
1138 need = 1;
1139 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1140 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1141 debug2("channel %d: socks4a request", c->self);
1142 /* ... and needs an extra string (the hostname) */
1143 need = 2;
1144 }
1145 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001146 for (found = 0, i = len; i < have; i++) {
1147 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001148 found++;
1149 if (found == need)
1150 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001151 }
1152 if (i > 1024) {
1153 /* the peer is probably sending garbage */
1154 debug("channel %d: decode socks4: too long",
1155 c->self);
1156 return -1;
1157 }
1158 }
Damien Miller1781f532009-01-28 16:24:41 +11001159 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001160 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001161 buffer_get(&c->input, (char *)&s4_req.version, 1);
1162 buffer_get(&c->input, (char *)&s4_req.command, 1);
1163 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001164 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001165 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001166 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001167 if (memchr(p, '\0', have) == NULL)
1168 fatal("channel %d: decode socks4: user not nul terminated",
1169 c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001170 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001171 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001172 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001173 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001174 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001175 c->self, len, have);
1176 strlcpy(username, p, sizeof(username));
1177 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001178
Darren Tuckera627d422013-06-02 07:31:17 +10001179 free(c->path);
1180 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001181 if (need == 1) { /* SOCKS4: one string */
1182 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001183 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001184 } else { /* SOCKS4A: two strings */
1185 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001186 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001187 len = strlen(p);
1188 debug2("channel %d: decode socks4a: host %s/%d",
1189 c->self, p, len);
1190 len++; /* trailing '\0' */
1191 if (len > have)
1192 fatal("channel %d: decode socks4a: len %d > have %d",
1193 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001194 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001195 error("channel %d: hostname \"%.100s\" too long",
1196 c->self, p);
1197 return -1;
1198 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001199 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001200 buffer_consume(&c->input, len);
1201 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001202 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001203
Damien Millerfbdeece2003-09-02 22:52:31 +10001204 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001205 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001206
Ben Lindstromb3921512001-04-11 15:57:50 +00001207 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001208 debug("channel %d: cannot handle: %s cn %d",
1209 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001210 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001211 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001212 s4_rsp.version = 0; /* vn: 0 for reply */
1213 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001214 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001215 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001216 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001217 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001218}
1219
Darren Tucker46471c92003-07-03 13:55:19 +10001220/* try to decode a socks5 header */
1221#define SSH_SOCKS5_AUTHDONE 0x1000
1222#define SSH_SOCKS5_NOAUTH 0x00
1223#define SSH_SOCKS5_IPV4 0x01
1224#define SSH_SOCKS5_DOMAIN 0x03
1225#define SSH_SOCKS5_IPV6 0x04
1226#define SSH_SOCKS5_CONNECT 0x01
1227#define SSH_SOCKS5_SUCCESS 0x00
1228
Damien Miller8473dd82006-07-24 14:08:32 +10001229/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001230static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001231channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001232{
1233 struct {
1234 u_int8_t version;
1235 u_int8_t command;
1236 u_int8_t reserved;
1237 u_int8_t atyp;
1238 } s5_req, s5_rsp;
1239 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001240 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1241 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001242 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001243
1244 debug2("channel %d: decode socks5", c->self);
1245 p = buffer_ptr(&c->input);
1246 if (p[0] != 0x05)
1247 return -1;
1248 have = buffer_len(&c->input);
1249 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1250 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001251 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001252 return 0;
1253 nmethods = p[1];
1254 if (have < nmethods + 2)
1255 return 0;
1256 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001257 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001258 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001259 found = 1;
1260 break;
1261 }
1262 }
1263 if (!found) {
1264 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1265 c->self);
1266 return -1;
1267 }
1268 buffer_consume(&c->input, nmethods + 2);
1269 buffer_put_char(&c->output, 0x05); /* version */
1270 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1271 FD_SET(c->sock, writeset);
1272 c->flags |= SSH_SOCKS5_AUTHDONE;
1273 debug2("channel %d: socks5 auth done", c->self);
1274 return 0; /* need more */
1275 }
1276 debug2("channel %d: socks5 post auth", c->self);
1277 if (have < sizeof(s5_req)+1)
1278 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001279 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001280 if (s5_req.version != 0x05 ||
1281 s5_req.command != SSH_SOCKS5_CONNECT ||
1282 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001283 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001284 return -1;
1285 }
Darren Tucker47eede72005-03-14 23:08:12 +11001286 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001287 case SSH_SOCKS5_IPV4:
1288 addrlen = 4;
1289 af = AF_INET;
1290 break;
1291 case SSH_SOCKS5_DOMAIN:
1292 addrlen = p[sizeof(s5_req)];
1293 af = -1;
1294 break;
1295 case SSH_SOCKS5_IPV6:
1296 addrlen = 16;
1297 af = AF_INET6;
1298 break;
1299 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001300 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001301 return -1;
1302 }
Damien Miller0f077072006-07-10 22:21:02 +10001303 need = sizeof(s5_req) + addrlen + 2;
1304 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1305 need++;
1306 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001307 return 0;
1308 buffer_consume(&c->input, sizeof(s5_req));
1309 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1310 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001311 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001312 buffer_get(&c->input, (char *)&dest_port, 2);
1313 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001314 free(c->path);
1315 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001316 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001317 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001318 error("channel %d: dynamic request: socks5 hostname "
1319 "\"%.100s\" too long", c->self, dest_addr);
1320 return -1;
1321 }
1322 c->path = xstrdup(dest_addr);
1323 } else {
1324 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1325 return -1;
1326 c->path = xstrdup(ntop);
1327 }
Darren Tucker46471c92003-07-03 13:55:19 +10001328 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001329
Damien Millerfbdeece2003-09-02 22:52:31 +10001330 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001331 c->self, c->path, c->host_port, s5_req.command);
1332
1333 s5_rsp.version = 0x05;
1334 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1335 s5_rsp.reserved = 0; /* ignored */
1336 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001337 dest_port = 0; /* ignored */
1338
Damien Millera0fdce92006-03-26 14:28:50 +11001339 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001340 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001341 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001342 return 1;
1343}
1344
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001345Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001346channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1347 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001348{
1349 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001350
1351 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1352 port_to_connect);
1353
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001354 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1355 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1356 0, "stdio-forward", /*nonblock*/0);
1357
1358 c->path = xstrdup(host_to_connect);
1359 c->host_port = port_to_connect;
1360 c->listening_port = 0;
1361 c->force_drain = 1;
1362
1363 channel_register_fds(c, in, out, -1, 0, 1, 0);
1364 port_open_helper(c, "direct-tcpip");
1365
1366 return c;
1367}
1368
Ben Lindstromb3921512001-04-11 15:57:50 +00001369/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001370static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001371channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001372{
1373 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001374 u_int have;
1375 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001376
1377 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001378 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001379 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001380 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001381 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001382 /* need more */
1383 FD_SET(c->sock, readset);
1384 return;
1385 }
1386 /* try to guess the protocol */
1387 p = buffer_ptr(&c->input);
1388 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001389 case 0x04:
1390 ret = channel_decode_socks4(c, readset, writeset);
1391 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001392 case 0x05:
1393 ret = channel_decode_socks5(c, readset, writeset);
1394 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001395 default:
1396 ret = -1;
1397 break;
1398 }
1399 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001400 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001401 } else if (ret == 0) {
1402 debug2("channel %d: pre_dynamic: need more", c->self);
1403 /* need more */
1404 FD_SET(c->sock, readset);
1405 } else {
1406 /* switch to the next state */
1407 c->type = SSH_CHANNEL_OPENING;
1408 port_open_helper(c, "direct-tcpip");
1409 }
1410}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001411
Damien Millerb38eff82000-04-01 11:09:21 +10001412/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001413/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001414static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001415channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001416{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001417 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001418 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001419 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001420 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001421 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001422 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001423
1424 if (FD_ISSET(c->sock, readset)) {
1425 debug("X11 connection requested.");
1426 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001427 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001428 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001429 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001430 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001431 channel_close_fd(&c->sock);
1432 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001433 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001434 }
Damien Millerb38eff82000-04-01 11:09:21 +10001435 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001436 if (errno != EINTR && errno != EWOULDBLOCK &&
1437 errno != ECONNABORTED)
1438 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001439 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001440 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001441 return;
1442 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001443 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001444 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001445 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001446 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001447 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001448
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001449 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001450 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001451 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001452 if (compat20) {
1453 packet_start(SSH2_MSG_CHANNEL_OPEN);
1454 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001455 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001456 packet_put_int(nc->local_window_max);
1457 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001458 /* originator ipaddr and port */
1459 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001460 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001461 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001462 } else {
1463 packet_put_int(remote_port);
1464 }
Damien Millerbd483e72000-04-30 10:00:53 +10001465 packet_send();
1466 } else {
1467 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001468 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001469 if (packet_get_protocol_flags() &
1470 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001471 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001472 packet_send();
1473 }
Darren Tuckera627d422013-06-02 07:31:17 +10001474 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001475 }
1476}
1477
Ben Lindstrombba81212001-06-25 05:01:22 +00001478static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001479port_open_helper(Channel *c, char *rtype)
1480{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001481 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001482 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001483 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001484 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001485 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001486
Damien Millerd6369432010-02-02 17:02:07 +11001487 if (remote_port == -1) {
1488 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001489 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001490 remote_ipaddr = xstrdup("127.0.0.1");
1491 remote_port = 65535;
1492 }
1493
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001494 snprintf(buf, sizeof buf,
1495 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001496 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001497 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001498 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001499
Darren Tuckera627d422013-06-02 07:31:17 +10001500 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001501 c->remote_name = xstrdup(buf);
1502
1503 if (compat20) {
1504 packet_start(SSH2_MSG_CHANNEL_OPEN);
1505 packet_put_cstring(rtype);
1506 packet_put_int(c->self);
1507 packet_put_int(c->local_window_max);
1508 packet_put_int(c->local_maxpacket);
Damien Miller7acefbb2014-07-18 14:11:24 +10001509 if (strcmp(rtype, "direct-tcpip") == 0) {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001510 /* target host, port */
1511 packet_put_cstring(c->path);
1512 packet_put_int(c->host_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001513 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1514 /* target path */
1515 packet_put_cstring(c->path);
1516 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1517 /* listen path */
1518 packet_put_cstring(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001519 } else {
1520 /* listen address, port */
1521 packet_put_cstring(c->path);
Damien Miller4def1842013-12-29 17:45:26 +11001522 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001523 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001524 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1525 /* reserved for future owner/mode info */
1526 packet_put_cstring("");
1527 } else {
1528 /* originator host and port */
1529 packet_put_cstring(remote_ipaddr);
1530 packet_put_int((u_int)remote_port);
1531 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001532 packet_send();
1533 } else {
1534 packet_start(SSH_MSG_PORT_OPEN);
1535 packet_put_int(c->self);
1536 packet_put_cstring(c->path);
1537 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001538 if (packet_get_protocol_flags() &
1539 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001540 packet_put_cstring(c->remote_name);
1541 packet_send();
1542 }
Darren Tuckera627d422013-06-02 07:31:17 +10001543 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001544 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001545}
1546
Damien Miller5e7fd072005-11-05 14:53:39 +11001547static void
1548channel_set_reuseaddr(int fd)
1549{
1550 int on = 1;
1551
1552 /*
1553 * Set socket options.
1554 * Allow local port reuse in TIME_WAIT.
1555 */
1556 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1557 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1558}
1559
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001560void
1561channel_set_x11_refuse_time(u_int refuse_time)
1562{
1563 x11_refuse_time = refuse_time;
1564}
1565
Damien Millerb38eff82000-04-01 11:09:21 +10001566/*
1567 * This socket is listening for connections to a forwarded TCP/IP port.
1568 */
Damien Miller8473dd82006-07-24 14:08:32 +10001569/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001570static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001571channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001572{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001573 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001574 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001575 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001576 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001577 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001578
Damien Millerb38eff82000-04-01 11:09:21 +10001579 if (FD_ISSET(c->sock, readset)) {
1580 debug("Connection to port %d forwarding "
1581 "to %.100s port %d requested.",
1582 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001583
Damien Miller4623a752001-10-10 15:03:58 +10001584 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1585 nextstate = SSH_CHANNEL_OPENING;
1586 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001587 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1588 nextstate = SSH_CHANNEL_OPENING;
1589 rtype = "forwarded-streamlocal@openssh.com";
1590 } else if (c->host_port == PORT_STREAMLOCAL) {
1591 nextstate = SSH_CHANNEL_OPENING;
1592 rtype = "direct-streamlocal@openssh.com";
1593 } else if (c->host_port == 0) {
1594 nextstate = SSH_CHANNEL_DYNAMIC;
1595 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001596 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001597 nextstate = SSH_CHANNEL_OPENING;
1598 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001599 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001600
Damien Millerb38eff82000-04-01 11:09:21 +10001601 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001602 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001603 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001604 if (errno != EINTR && errno != EWOULDBLOCK &&
1605 errno != ECONNABORTED)
1606 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001607 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001608 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001609 return;
1610 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001611 if (c->host_port != PORT_STREAMLOCAL)
1612 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001613 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1614 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001615 nc->listening_port = c->listening_port;
1616 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001617 if (c->path != NULL)
1618 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001619
Darren Tucker876045b2010-01-08 17:08:00 +11001620 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001621 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001622 }
1623}
1624
1625/*
1626 * This is the authentication agent socket listening for connections from
1627 * clients.
1628 */
Damien Miller8473dd82006-07-24 14:08:32 +10001629/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001630static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001631channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001632{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001633 Channel *nc;
1634 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001635 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001636 socklen_t addrlen;
1637
1638 if (FD_ISSET(c->sock, readset)) {
1639 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001640 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001641 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001642 error("accept from auth socket: %.100s",
1643 strerror(errno));
1644 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001645 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001646 return;
1647 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001648 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001649 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1650 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001651 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001652 if (compat20) {
1653 packet_start(SSH2_MSG_CHANNEL_OPEN);
1654 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001655 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001656 packet_put_int(c->local_window_max);
1657 packet_put_int(c->local_maxpacket);
1658 } else {
1659 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001660 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001661 }
Damien Millerb38eff82000-04-01 11:09:21 +10001662 packet_send();
1663 }
1664}
1665
Damien Miller8473dd82006-07-24 14:08:32 +10001666/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001667static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001668channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001669{
Damien Millerbd740252008-05-19 15:37:09 +10001670 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001671 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001672
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001673 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001674 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001675 err = errno;
1676 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001677 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001678 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001679 debug("channel %d: connected to %s port %d",
1680 c->self, c->connect_ctx.host, c->connect_ctx.port);
1681 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001682 c->type = SSH_CHANNEL_OPEN;
1683 if (compat20) {
1684 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1685 packet_put_int(c->remote_id);
1686 packet_put_int(c->self);
1687 packet_put_int(c->local_window);
1688 packet_put_int(c->local_maxpacket);
1689 } else {
1690 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1691 packet_put_int(c->remote_id);
1692 packet_put_int(c->self);
1693 }
1694 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001695 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001696 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001697 /* Try next address, if any */
1698 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1699 close(c->sock);
1700 c->sock = c->rfd = c->wfd = sock;
1701 channel_max_fd = channel_find_maxfd();
1702 return;
1703 }
1704 /* Exhausted all addresses */
1705 error("connect_to %.100s port %d: failed.",
1706 c->connect_ctx.host, c->connect_ctx.port);
1707 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001708 if (compat20) {
1709 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1710 packet_put_int(c->remote_id);
1711 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1712 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1713 packet_put_cstring(strerror(err));
1714 packet_put_cstring("");
1715 }
1716 } else {
1717 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1718 packet_put_int(c->remote_id);
1719 }
1720 chan_mark_dead(c);
1721 }
1722 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001723 }
1724}
1725
Damien Miller8473dd82006-07-24 14:08:32 +10001726/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001727static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001728channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001729{
Darren Tucker11327cc2005-03-14 23:22:25 +11001730 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001731 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001732
Damien Miller835284b2007-06-11 13:03:16 +10001733 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1734 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001735 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001736 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001737 if (len < 0 && (errno == EINTR ||
1738 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001739 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001740#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001741 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001742#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001743 if ((!c->isatty && len <= 0) ||
1744 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001745#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001746 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001747 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001748 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001749 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001750 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001751 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001752 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001753 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001754 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001755 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001756 } else {
1757 chan_read_failed(c);
1758 }
1759 return -1;
1760 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001761 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001762 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001763 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001764 chan_read_failed(c);
1765 }
Damien Millerd27b9472005-12-13 19:29:02 +11001766 } else if (c->datagram) {
1767 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001768 } else {
1769 buffer_append(&c->input, buf, len);
1770 }
Damien Millerb38eff82000-04-01 11:09:21 +10001771 }
1772 return 1;
1773}
Damien Miller4f7becb2006-03-26 14:10:14 +11001774
Damien Miller8473dd82006-07-24 14:08:32 +10001775/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001776static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001777channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001778{
Ben Lindstrome229b252001-03-05 06:28:06 +00001779 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001780 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001781 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001782 int len;
1783
1784 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001785 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001786 FD_ISSET(c->wfd, writeset) &&
1787 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001788 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001789 if (c->output_filter != NULL) {
1790 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1791 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001792 if (c->type != SSH_CHANNEL_OPEN)
1793 chan_mark_dead(c);
1794 else
1795 chan_write_failed(c);
1796 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001797 }
1798 } else if (c->datagram) {
1799 buf = data = buffer_get_string(&c->output, &dlen);
1800 } else {
1801 buf = data = buffer_ptr(&c->output);
1802 dlen = buffer_len(&c->output);
1803 }
1804
Damien Millerd27b9472005-12-13 19:29:02 +11001805 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001806 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001807 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001808 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001809 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1810 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001811 return 1;
1812 if (len <= 0) {
1813 if (c->type != SSH_CHANNEL_OPEN)
1814 chan_mark_dead(c);
1815 else
1816 chan_write_failed(c);
1817 return -1;
1818 }
Damien Miller7d457182010-08-05 23:09:48 +10001819 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001820 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001821#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001822 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001823 if (compat20 && c->wfd_isatty)
1824 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001825#endif
Damien Miller077b2382005-12-31 16:22:32 +11001826
1827 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001828 if (len < 0 &&
1829 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001830 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001831 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001832 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001833 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001834 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001835 return -1;
1836 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001837 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001838 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001839 c->type = SSH_CHANNEL_INPUT_DRAINING;
1840 } else {
1841 chan_write_failed(c);
1842 }
1843 return -1;
1844 }
Darren Tucker3980b632009-08-28 11:02:37 +10001845#ifndef BROKEN_TCGETATTR_ICANON
Damien Miller077b2382005-12-31 16:22:32 +11001846 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001847 if (tcgetattr(c->wfd, &tio) == 0 &&
1848 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1849 /*
1850 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001851 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001852 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001853 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001854 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001855 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001856 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001857 }
1858 }
Darren Tucker3980b632009-08-28 11:02:37 +10001859#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001860 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001861 }
Damien Miller7d457182010-08-05 23:09:48 +10001862 out:
1863 if (compat20 && olen > 0)
1864 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001865 return 1;
1866}
Damien Miller4f7becb2006-03-26 14:10:14 +11001867
Ben Lindstrombba81212001-06-25 05:01:22 +00001868static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001869channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001870{
Darren Tucker11327cc2005-03-14 23:22:25 +11001871 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001872 int len;
1873
Damien Miller1383bd82000-04-06 12:32:37 +10001874/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001875 if (c->efd != -1) {
1876 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1877 FD_ISSET(c->efd, writeset) &&
1878 buffer_len(&c->extended) > 0) {
1879 len = write(c->efd, buffer_ptr(&c->extended),
1880 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001881 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001882 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001883 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1884 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001885 return 1;
1886 if (len <= 0) {
1887 debug2("channel %d: closing write-efd %d",
1888 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001889 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001890 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001891 buffer_consume(&c->extended, len);
1892 c->local_consumed += len;
1893 }
Damien Miller8853ca52010-06-26 10:00:14 +10001894 } else if (c->efd != -1 &&
1895 (c->extended_usage == CHAN_EXTENDED_READ ||
1896 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001897 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001898 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001899 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001900 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001901 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1902 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001903 return 1;
1904 if (len <= 0) {
1905 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001906 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001907 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001908 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001909 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1910 debug3("channel %d: discard efd",
1911 c->self);
1912 } else
1913 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001914 }
Damien Miller33b13562000-04-04 14:38:59 +10001915 }
1916 }
1917 return 1;
1918}
Damien Miller4f7becb2006-03-26 14:10:14 +11001919
Damien Miller0e220db2004-06-15 10:34:08 +10001920static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001921channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001922{
Ben Lindstromb3921512001-04-11 15:57:50 +00001923 if (c->type == SSH_CHANNEL_OPEN &&
1924 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001925 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001926 c->local_maxpacket*3) ||
1927 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001928 c->local_consumed > 0) {
1929 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1930 packet_put_int(c->remote_id);
1931 packet_put_int(c->local_consumed);
1932 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001933 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001934 c->self, c->local_window,
1935 c->local_consumed);
1936 c->local_window += c->local_consumed;
1937 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001938 }
1939 return 1;
1940}
1941
Ben Lindstrombba81212001-06-25 05:01:22 +00001942static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001943channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001944{
1945 channel_handle_rfd(c, readset, writeset);
1946 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001947 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001948 return;
Damien Miller33b13562000-04-04 14:38:59 +10001949 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001950 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001951}
1952
Damien Millere1537f92010-01-26 13:26:22 +11001953static u_int
1954read_mux(Channel *c, u_int need)
1955{
1956 char buf[CHAN_RBUF];
1957 int len;
1958 u_int rlen;
1959
1960 if (buffer_len(&c->input) < need) {
1961 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001962 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001963 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1964 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001965 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001966 debug2("channel %d: ctl read<=0 rfd %d len %d",
1967 c->self, c->rfd, len);
1968 chan_read_failed(c);
1969 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001970 } else
1971 buffer_append(&c->input, buf, len);
1972 }
1973 return buffer_len(&c->input);
1974}
1975
1976static void
1977channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1978{
1979 u_int need;
1980 ssize_t len;
1981
1982 if (!compat20)
1983 fatal("%s: entered with !compat20", __func__);
1984
Damien Millerd530f5f2010-05-21 14:57:10 +10001985 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001986 (c->istate == CHAN_INPUT_OPEN ||
1987 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1988 /*
1989 * Don't not read past the precise end of packets to
1990 * avoid disrupting fd passing.
1991 */
1992 if (read_mux(c, 4) < 4) /* read header */
1993 return;
1994 need = get_u32(buffer_ptr(&c->input));
1995#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1996 if (need > CHANNEL_MUX_MAX_PACKET) {
1997 debug2("channel %d: packet too big %u > %u",
1998 c->self, CHANNEL_MUX_MAX_PACKET, need);
1999 chan_rcvd_oclose(c);
2000 return;
2001 }
2002 if (read_mux(c, need + 4) < need + 4) /* read body */
2003 return;
2004 if (c->mux_rcb(c) != 0) {
2005 debug("channel %d: mux_rcb failed", c->self);
2006 chan_mark_dead(c);
2007 return;
2008 }
2009 }
2010
2011 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
2012 buffer_len(&c->output) > 0) {
2013 len = write(c->wfd, buffer_ptr(&c->output),
2014 buffer_len(&c->output));
2015 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2016 return;
2017 if (len <= 0) {
2018 chan_mark_dead(c);
2019 return;
2020 }
2021 buffer_consume(&c->output, len);
2022 }
2023}
2024
2025static void
2026channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
2027{
2028 Channel *nc;
2029 struct sockaddr_storage addr;
2030 socklen_t addrlen;
2031 int newsock;
2032 uid_t euid;
2033 gid_t egid;
2034
2035 if (!FD_ISSET(c->sock, readset))
2036 return;
2037
2038 debug("multiplexing control connection");
2039
2040 /*
2041 * Accept connection on control socket
2042 */
2043 memset(&addr, 0, sizeof(addr));
2044 addrlen = sizeof(addr);
2045 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2046 &addrlen)) == -1) {
2047 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002048 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002049 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002050 return;
2051 }
2052
2053 if (getpeereid(newsock, &euid, &egid) < 0) {
2054 error("%s getpeereid failed: %s", __func__,
2055 strerror(errno));
2056 close(newsock);
2057 return;
2058 }
2059 if ((euid != 0) && (getuid() != euid)) {
2060 error("multiplex uid mismatch: peer euid %u != uid %u",
2061 (u_int)euid, (u_int)getuid());
2062 close(newsock);
2063 return;
2064 }
2065 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
2066 newsock, newsock, -1, c->local_window_max,
2067 c->local_maxpacket, 0, "mux-control", 1);
2068 nc->mux_rcb = c->mux_rcb;
2069 debug3("%s: new mux channel %d fd %d", __func__,
2070 nc->self, nc->sock);
2071 /* establish state */
2072 nc->mux_rcb(nc);
2073 /* mux state transitions must not elicit protocol messages */
2074 nc->flags |= CHAN_LOCAL;
2075}
2076
Damien Miller8473dd82006-07-24 14:08:32 +10002077/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00002078static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002079channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002080{
2081 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002082
Damien Millerb38eff82000-04-01 11:09:21 +10002083 /* Send buffered output data to the socket. */
2084 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
2085 len = write(c->sock, buffer_ptr(&c->output),
2086 buffer_len(&c->output));
2087 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11002088 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10002089 else
2090 buffer_consume(&c->output, len);
2091 }
2092}
2093
Ben Lindstrombba81212001-06-25 05:01:22 +00002094static void
Damien Miller33b13562000-04-04 14:38:59 +10002095channel_handler_init_20(void)
2096{
Damien Millerde6987c2002-01-22 23:20:40 +11002097 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002098 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10002099 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002100 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002101 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2102 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002103 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002104 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002105 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002106 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11002107 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2108 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002109
Damien Millerde6987c2002-01-22 23:20:40 +11002110 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10002111 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002112 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002113 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2114 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002115 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002116 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002117 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002118 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11002119 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2120 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002121}
2122
Ben Lindstrombba81212001-06-25 05:01:22 +00002123static void
Damien Millerb38eff82000-04-01 11:09:21 +10002124channel_handler_init_13(void)
2125{
2126 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
2127 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
2128 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2129 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2130 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2131 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
2132 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002133 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002134 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002135
Damien Millerde6987c2002-01-22 23:20:40 +11002136 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002137 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2138 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2139 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2140 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002141 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002142 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002143}
2144
Ben Lindstrombba81212001-06-25 05:01:22 +00002145static void
Damien Millerb38eff82000-04-01 11:09:21 +10002146channel_handler_init_15(void)
2147{
Damien Millerde6987c2002-01-22 23:20:40 +11002148 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002149 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002150 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2151 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2152 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002153 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002154 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002155
2156 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2157 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2158 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11002159 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002160 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002161 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002162}
2163
Ben Lindstrombba81212001-06-25 05:01:22 +00002164static void
Damien Millerb38eff82000-04-01 11:09:21 +10002165channel_handler_init(void)
2166{
2167 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002168
Damien Miller9f0f5c62001-12-21 14:45:46 +11002169 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10002170 channel_pre[i] = NULL;
2171 channel_post[i] = NULL;
2172 }
Damien Miller33b13562000-04-04 14:38:59 +10002173 if (compat20)
2174 channel_handler_init_20();
2175 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002176 channel_handler_init_13();
2177 else
2178 channel_handler_init_15();
2179}
2180
Damien Miller3ec27592001-10-12 11:35:04 +10002181/* gc dead channels */
2182static void
2183channel_garbage_collect(Channel *c)
2184{
2185 if (c == NULL)
2186 return;
2187 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11002188 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002189 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002190 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002191 c->detach_user(c->self, NULL);
2192 /* if we still have a callback */
2193 if (c->detach_user != NULL)
2194 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002195 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002196 }
2197 if (!chan_is_dead(c, 1))
2198 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002199 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002200 channel_free(c);
2201}
2202
Ben Lindstrombba81212001-06-25 05:01:22 +00002203static void
Damien Millera6508752012-04-22 11:21:10 +10002204channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2205 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002206{
2207 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002208 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002209 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002210 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002211
2212 if (!did_init) {
2213 channel_handler_init();
2214 did_init = 1;
2215 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002216 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002217 if (unpause_secs != NULL)
2218 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002219 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002220 c = channels[i];
2221 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002222 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002223 if (c->delayed) {
2224 if (ftab == channel_pre)
2225 c->delayed = 0;
2226 else
2227 continue;
2228 }
Damien Millera6508752012-04-22 11:21:10 +10002229 if (ftab[c->type] != NULL) {
2230 /*
2231 * Run handlers that are not paused.
2232 */
2233 if (c->notbefore <= now)
2234 (*ftab[c->type])(c, readset, writeset);
2235 else if (unpause_secs != NULL) {
2236 /*
2237 * Collect the time that the earliest
2238 * channel comes off pause.
2239 */
2240 debug3("%s: chan %d: skip for %d more seconds",
2241 __func__, c->self,
2242 (int)(c->notbefore - now));
2243 if (*unpause_secs == 0 ||
2244 (c->notbefore - now) < *unpause_secs)
2245 *unpause_secs = c->notbefore - now;
2246 }
2247 }
Damien Miller3ec27592001-10-12 11:35:04 +10002248 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002249 }
Damien Millera6508752012-04-22 11:21:10 +10002250 if (unpause_secs != NULL && *unpause_secs != 0)
2251 debug3("%s: first channel unpauses in %d seconds",
2252 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002253}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002254
Ben Lindstrome9c99912001-06-09 00:41:05 +00002255/*
2256 * Allocate/update select bitmasks and add any bits relevant to channels in
2257 * select bitmasks.
2258 */
Damien Miller4af51302000-04-16 11:18:38 +10002259void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002260channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002261 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002262{
Damien Miller36812092006-03-26 14:22:47 +11002263 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002264
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002265 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002266
Damien Miller36812092006-03-26 14:22:47 +11002267 nfdset = howmany(n+1, NFDBITS);
2268 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002269 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002270 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2271 sz = nfdset * sizeof(fd_mask);
2272
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002273 /* perhaps check sz < nalloc/2 and shrink? */
2274 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002275 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2276 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002277 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002278 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002279 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002280 memset(*readsetp, 0, sz);
2281 memset(*writesetp, 0, sz);
2282
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002283 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002284 channel_handler(channel_pre, *readsetp, *writesetp,
2285 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002286}
2287
Ben Lindstrome9c99912001-06-09 00:41:05 +00002288/*
2289 * After select, perform any appropriate operations for channels which have
2290 * events pending.
2291 */
Damien Miller4af51302000-04-16 11:18:38 +10002292void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002293channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294{
Damien Millera6508752012-04-22 11:21:10 +10002295 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296}
2297
Ben Lindstrome9c99912001-06-09 00:41:05 +00002298
Damien Miller5e953212001-01-30 09:14:00 +11002299/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002300void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002301channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002302{
Damien Millerb38eff82000-04-01 11:09:21 +10002303 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002304 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002305
Damien Miller95def091999-11-25 00:26:21 +11002306 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002307 c = channels[i];
2308 if (c == NULL)
2309 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002310
Ben Lindstrome9c99912001-06-09 00:41:05 +00002311 /*
2312 * We are only interested in channels that can have buffered
2313 * incoming data.
2314 */
Damien Miller34132e52000-01-14 15:45:46 +11002315 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10002316 if (c->type != SSH_CHANNEL_OPEN &&
2317 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11002318 continue;
2319 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10002320 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002321 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002322 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002323 if (compat20 &&
2324 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002325 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002326 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002327 continue;
2328 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002329
Damien Miller95def091999-11-25 00:26:21 +11002330 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002331 if ((c->istate == CHAN_INPUT_OPEN ||
2332 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2333 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002334 if (c->datagram) {
2335 if (len > 0) {
2336 u_char *data;
2337 u_int dlen;
2338
2339 data = buffer_get_string(&c->input,
2340 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002341 if (dlen > c->remote_window ||
2342 dlen > c->remote_maxpacket) {
2343 debug("channel %d: datagram "
2344 "too big for channel",
2345 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002346 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002347 continue;
2348 }
Damien Millerd27b9472005-12-13 19:29:02 +11002349 packet_start(SSH2_MSG_CHANNEL_DATA);
2350 packet_put_int(c->remote_id);
2351 packet_put_string(data, dlen);
2352 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002353 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002354 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002355 }
2356 continue;
2357 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002358 /*
2359 * Send some data for the other side over the secure
2360 * connection.
2361 */
Damien Miller33b13562000-04-04 14:38:59 +10002362 if (compat20) {
2363 if (len > c->remote_window)
2364 len = c->remote_window;
2365 if (len > c->remote_maxpacket)
2366 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11002367 } else {
Damien Miller33b13562000-04-04 14:38:59 +10002368 if (packet_is_interactive()) {
2369 if (len > 1024)
2370 len = 512;
2371 } else {
2372 /* Keep the packets at reasonable size. */
2373 if (len > packet_get_maxsize()/2)
2374 len = packet_get_maxsize()/2;
2375 }
Damien Miller95def091999-11-25 00:26:21 +11002376 }
Damien Millerb38eff82000-04-01 11:09:21 +10002377 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10002378 packet_start(compat20 ?
2379 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002380 packet_put_int(c->remote_id);
2381 packet_put_string(buffer_ptr(&c->input), len);
2382 packet_send();
2383 buffer_consume(&c->input, len);
2384 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002385 }
2386 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11002387 if (compat13)
2388 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11002389 /*
2390 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002391 * tell peer, that we will not send more data: send IEOF.
2392 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002393 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002394 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002395 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2396 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002397 else
2398 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002399 }
Damien Miller33b13562000-04-04 14:38:59 +10002400 /* Send extended data, i.e. stderr */
2401 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00002402 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002403 c->remote_window > 0 &&
2404 (len = buffer_len(&c->extended)) > 0 &&
2405 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002406 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002407 c->self, c->remote_window, buffer_len(&c->extended),
2408 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002409 if (len > c->remote_window)
2410 len = c->remote_window;
2411 if (len > c->remote_maxpacket)
2412 len = c->remote_maxpacket;
2413 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2414 packet_put_int(c->remote_id);
2415 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2416 packet_put_string(buffer_ptr(&c->extended), len);
2417 packet_send();
2418 buffer_consume(&c->extended, len);
2419 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002420 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002421 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002422 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002423}
2424
markus@openbsd.org8d057842016-09-30 09:19:13 +00002425/* -- mux proxy support */
2426
2427/*
2428 * When multiplexing channel messages for mux clients we have to deal
2429 * with downstream messages from the mux client and upstream messages
2430 * from the ssh server:
2431 * 1) Handling downstream messages is straightforward and happens
2432 * in channel_proxy_downstream():
2433 * - We forward all messages (mostly) unmodified to the server.
2434 * - However, in order to route messages from upstream to the correct
2435 * downstream client, we have to replace the channel IDs used by the
2436 * mux clients with a unique channel ID because the mux clients might
2437 * use conflicting channel IDs.
2438 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2439 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2440 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2441 * with the newly allocated channel ID.
2442 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2443 * channels and procesed by channel_proxy_upstream(). The local channel ID
2444 * is then translated back to the original mux client ID.
2445 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2446 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2447 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2448 * downstream mux client are removed.
2449 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2450 * requires more work, because they are not addressed to a specific
2451 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2452 * out whether the request is addressed to the local client or a
2453 * specific downstream client based on the listen-address/port.
2454 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2455 * not supported as the matching session/channel cannot be identified
2456 * easily.
2457 */
2458
2459/*
2460 * receive packets from downstream mux clients:
2461 * channel callback fired on read from mux client, creates
2462 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2463 * on channel creation.
2464 */
2465int
2466channel_proxy_downstream(Channel *downstream)
2467{
2468 Channel *c = NULL;
2469 struct ssh *ssh = active_state;
2470 struct sshbuf *original = NULL, *modified = NULL;
2471 const u_char *cp;
2472 char *ctype = NULL, *listen_host = NULL;
2473 u_char type;
2474 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002475 int ret = -1, r, idx;
2476 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002477
2478 /* sshbuf_dump(&downstream->input, stderr); */
2479 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2480 != 0) {
2481 error("%s: malformed message: %s", __func__, ssh_err(r));
2482 return -1;
2483 }
2484 if (have < 2) {
2485 error("%s: short message", __func__);
2486 return -1;
2487 }
2488 type = cp[1];
2489 /* skip padlen + type */
2490 cp += 2;
2491 have -= 2;
2492 if (ssh_packet_log_type(type))
2493 debug3("%s: channel %u: down->up: type %u", __func__,
2494 downstream->self, type);
2495
2496 switch (type) {
2497 case SSH2_MSG_CHANNEL_OPEN:
2498 if ((original = sshbuf_from(cp, have)) == NULL ||
2499 (modified = sshbuf_new()) == NULL) {
2500 error("%s: alloc", __func__);
2501 goto out;
2502 }
2503 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2504 (r = sshbuf_get_u32(original, &id)) != 0) {
2505 error("%s: parse error %s", __func__, ssh_err(r));
2506 goto out;
2507 }
2508 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2509 -1, -1, -1, 0, 0, 0, ctype, 1);
2510 c->mux_ctx = downstream; /* point to mux client */
2511 c->mux_downstream_id = id; /* original downstream id */
2512 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2513 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2514 (r = sshbuf_putb(modified, original)) != 0) {
2515 error("%s: compose error %s", __func__, ssh_err(r));
2516 channel_free(c);
2517 goto out;
2518 }
2519 break;
2520 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2521 /*
2522 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2523 * need to parse 'remote_id' instead of 'ctype'.
2524 */
2525 if ((original = sshbuf_from(cp, have)) == NULL ||
2526 (modified = sshbuf_new()) == NULL) {
2527 error("%s: alloc", __func__);
2528 goto out;
2529 }
2530 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2531 (r = sshbuf_get_u32(original, &id)) != 0) {
2532 error("%s: parse error %s", __func__, ssh_err(r));
2533 goto out;
2534 }
2535 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2536 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2537 c->mux_ctx = downstream; /* point to mux client */
2538 c->mux_downstream_id = id;
2539 c->remote_id = remote_id;
2540 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2541 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2542 (r = sshbuf_putb(modified, original)) != 0) {
2543 error("%s: compose error %s", __func__, ssh_err(r));
2544 channel_free(c);
2545 goto out;
2546 }
2547 break;
2548 case SSH2_MSG_GLOBAL_REQUEST:
2549 if ((original = sshbuf_from(cp, have)) == NULL) {
2550 error("%s: alloc", __func__);
2551 goto out;
2552 }
2553 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2554 error("%s: parse error %s", __func__, ssh_err(r));
2555 goto out;
2556 }
2557 if (strcmp(ctype, "tcpip-forward") != 0) {
2558 error("%s: unsupported request %s", __func__, ctype);
2559 goto out;
2560 }
2561 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2562 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2563 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2564 error("%s: parse error %s", __func__, ssh_err(r));
2565 goto out;
2566 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002567 if (listen_port > 65535) {
2568 error("%s: tcpip-forward for %s: bad port %u",
2569 __func__, listen_host, listen_port);
2570 goto out;
2571 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002572 /* Record that connection to this host/port is permitted. */
2573 permitted_opens = xreallocarray(permitted_opens,
2574 num_permitted_opens + 1, sizeof(*permitted_opens));
2575 idx = num_permitted_opens++;
2576 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2577 permitted_opens[idx].port_to_connect = -1;
2578 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002579 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002580 permitted_opens[idx].downstream = downstream;
2581 listen_host = NULL;
2582 break;
2583 case SSH2_MSG_CHANNEL_CLOSE:
2584 if (have < 4)
2585 break;
2586 remote_id = PEEK_U32(cp);
2587 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2588 if (c->flags & CHAN_CLOSE_RCVD)
2589 channel_free(c);
2590 else
2591 c->flags |= CHAN_CLOSE_SENT;
2592 }
2593 break;
2594 }
2595 if (modified) {
2596 if ((r = sshpkt_start(ssh, type)) != 0 ||
2597 (r = sshpkt_putb(ssh, modified)) != 0 ||
2598 (r = sshpkt_send(ssh)) != 0) {
2599 error("%s: send %s", __func__, ssh_err(r));
2600 goto out;
2601 }
2602 } else {
2603 if ((r = sshpkt_start(ssh, type)) != 0 ||
2604 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2605 (r = sshpkt_send(ssh)) != 0) {
2606 error("%s: send %s", __func__, ssh_err(r));
2607 goto out;
2608 }
2609 }
2610 ret = 0;
2611 out:
2612 free(ctype);
2613 free(listen_host);
2614 sshbuf_free(original);
2615 sshbuf_free(modified);
2616 return ret;
2617}
2618
2619/*
2620 * receive packets from upstream server and de-multiplex packets
2621 * to correct downstream:
2622 * implemented as a helper for channel input handlers,
2623 * replaces local (proxy) channel ID with downstream channel ID.
2624 */
2625int
2626channel_proxy_upstream(Channel *c, int type, u_int32_t seq, void *ctxt)
2627{
2628 struct ssh *ssh = active_state;
2629 struct sshbuf *b = NULL;
2630 Channel *downstream;
2631 const u_char *cp = NULL;
2632 size_t len;
2633 int r;
2634
2635 /*
2636 * When receiving packets from the peer we need to check whether we
2637 * need to forward the packets to the mux client. In this case we
2638 * restore the orignal channel id and keep track of CLOSE messages,
2639 * so we can cleanup the channel.
2640 */
2641 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2642 return 0;
2643 if ((downstream = c->mux_ctx) == NULL)
2644 return 0;
2645 switch (type) {
2646 case SSH2_MSG_CHANNEL_CLOSE:
2647 case SSH2_MSG_CHANNEL_DATA:
2648 case SSH2_MSG_CHANNEL_EOF:
2649 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2650 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2651 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2652 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2653 case SSH2_MSG_CHANNEL_SUCCESS:
2654 case SSH2_MSG_CHANNEL_FAILURE:
2655 case SSH2_MSG_CHANNEL_REQUEST:
2656 break;
2657 default:
2658 debug2("%s: channel %u: unsupported type %u", __func__,
2659 c->self, type);
2660 return 0;
2661 }
2662 if ((b = sshbuf_new()) == NULL) {
2663 error("%s: alloc reply", __func__);
2664 goto out;
2665 }
2666 /* get remaining payload (after id) */
2667 cp = sshpkt_ptr(ssh, &len);
2668 if (cp == NULL) {
2669 error("%s: no packet", __func__);
2670 goto out;
2671 }
2672 /* translate id and send to muxclient */
2673 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2674 (r = sshbuf_put_u8(b, type)) != 0 ||
2675 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2676 (r = sshbuf_put(b, cp, len)) != 0 ||
2677 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2678 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2679 goto out;
2680 }
2681 /* sshbuf_dump(b, stderr); */
2682 if (ssh_packet_log_type(type))
2683 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2684 type);
2685 out:
2686 /* update state */
2687 switch (type) {
2688 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2689 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2690 if (cp && len > 4)
2691 c->remote_id = PEEK_U32(cp);
2692 break;
2693 case SSH2_MSG_CHANNEL_CLOSE:
2694 if (c->flags & CHAN_CLOSE_SENT)
2695 channel_free(c);
2696 else
2697 c->flags |= CHAN_CLOSE_RCVD;
2698 break;
2699 }
2700 sshbuf_free(b);
2701 return 1;
2702}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002703
2704/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002705
2706/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002707int
Damien Miller630d6f42002-01-22 23:17:30 +11002708channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002709{
Damien Miller34132e52000-01-14 15:45:46 +11002710 int id;
Damien Miller633de332014-05-15 13:48:26 +10002711 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002712 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002713 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002714
Damien Miller95def091999-11-25 00:26:21 +11002715 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002716 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002717 c = channel_lookup(id);
2718 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002719 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002720 if (channel_proxy_upstream(c, type, seq, ctxt))
2721 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002722
Damien Miller95def091999-11-25 00:26:21 +11002723 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002724 if (c->type != SSH_CHANNEL_OPEN &&
2725 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002726 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002727
Damien Miller95def091999-11-25 00:26:21 +11002728 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002729 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002730 win_len = data_len;
2731 if (c->datagram)
2732 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002733
Damien Millera04ad492004-01-21 11:02:09 +11002734 /*
2735 * Ignore data for protocol > 1.3 if output end is no longer open.
2736 * For protocol 2 the sending side is reducing its window as it sends
2737 * data, so we must 'fake' consumption of the data in order to ensure
2738 * that window updates are sent back. Otherwise the connection might
2739 * deadlock.
2740 */
2741 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2742 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002743 c->local_window -= win_len;
2744 c->local_consumed += win_len;
Damien Millera04ad492004-01-21 11:02:09 +11002745 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002746 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002747 }
2748
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002749 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002750 if (win_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002751 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller7d457182010-08-05 23:09:48 +10002752 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002753 }
Damien Miller7d457182010-08-05 23:09:48 +10002754 if (win_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002755 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller7d457182010-08-05 23:09:48 +10002756 c->self, win_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002757 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002758 }
Damien Miller7d457182010-08-05 23:09:48 +10002759 c->local_window -= win_len;
Damien Miller33b13562000-04-04 14:38:59 +10002760 }
Damien Millerd27b9472005-12-13 19:29:02 +11002761 if (c->datagram)
2762 buffer_put_string(&c->output, data, data_len);
2763 else
2764 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002765 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002766 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002767}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002768
Damien Millerd79b4242006-03-31 23:11:44 +11002769/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002770int
Damien Miller630d6f42002-01-22 23:17:30 +11002771channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002772{
2773 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002774 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002775 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002776 Channel *c;
2777
2778 /* Get the channel number and verify it. */
2779 id = packet_get_int();
2780 c = channel_lookup(id);
2781
2782 if (c == NULL)
2783 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002784 if (channel_proxy_upstream(c, type, seq, ctxt))
2785 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002786 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002787 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002788 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002789 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002790 if (c->flags & CHAN_EOF_RCVD) {
2791 if (datafellows & SSH_BUG_EXTEOF)
2792 debug("channel %d: accepting ext data after eof", id);
2793 else
2794 packet_disconnect("Received extended_data after EOF "
2795 "on channel %d.", id);
2796 }
Damien Miller33b13562000-04-04 14:38:59 +10002797 tcode = packet_get_int();
2798 if (c->efd == -1 ||
2799 c->extended_usage != CHAN_EXTENDED_WRITE ||
2800 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002801 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002802 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002803 }
2804 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002805 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002806 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002807 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002808 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002809 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002810 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002811 }
Damien Millerd3444942000-09-30 14:20:03 +11002812 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002813 c->local_window -= data_len;
2814 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002815 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002816 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002817}
2818
Damien Millerd79b4242006-03-31 23:11:44 +11002819/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002820int
Damien Miller630d6f42002-01-22 23:17:30 +11002821channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002822{
2823 int id;
2824 Channel *c;
2825
Damien Millerb38eff82000-04-01 11:09:21 +10002826 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002827 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002828 c = channel_lookup(id);
2829 if (c == NULL)
2830 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002831 if (channel_proxy_upstream(c, type, seq, ctxt))
2832 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002833 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002834
2835 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002836 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002837 debug("channel %d: FORCE input drain", c->self);
2838 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002839 if (buffer_len(&c->input) == 0)
2840 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002841 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002842 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002843}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002844
Damien Millerd79b4242006-03-31 23:11:44 +11002845/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002846int
Damien Miller630d6f42002-01-22 23:17:30 +11002847channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002848{
Damien Millerb38eff82000-04-01 11:09:21 +10002849 int id;
2850 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002851
Damien Millerb38eff82000-04-01 11:09:21 +10002852 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002853 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002854 c = channel_lookup(id);
2855 if (c == NULL)
2856 packet_disconnect("Received close for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002857 if (channel_proxy_upstream(c, type, seq, ctxt))
2858 return 0;
Damien Miller5428f641999-11-25 11:54:57 +11002859 /*
2860 * Send a confirmation that we have closed the channel and no more
2861 * data is coming for it.
2862 */
Damien Miller95def091999-11-25 00:26:21 +11002863 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002864 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002865 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002866
Damien Miller5428f641999-11-25 11:54:57 +11002867 /*
2868 * If the channel is in closed state, we have sent a close request,
2869 * and the other side will eventually respond with a confirmation.
2870 * Thus, we cannot free the channel here, because then there would be
2871 * no-one to receive the confirmation. The channel gets freed when
2872 * the confirmation arrives.
2873 */
Damien Millerb38eff82000-04-01 11:09:21 +10002874 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002875 /*
2876 * Not a closed channel - mark it as draining, which will
2877 * cause it to be freed later.
2878 */
Damien Miller76765c02002-01-22 23:21:15 +11002879 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002880 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002881 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002882 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002883}
2884
Damien Millerb38eff82000-04-01 11:09:21 +10002885/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002886/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002887int
Damien Miller630d6f42002-01-22 23:17:30 +11002888channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002889{
Damien Millerb38eff82000-04-01 11:09:21 +10002890 int id = packet_get_int();
2891 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002892
Damien Millerb38eff82000-04-01 11:09:21 +10002893 if (c == NULL)
2894 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002895 if (channel_proxy_upstream(c, type, seq, ctxt))
2896 return 0;
2897 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002898 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002899 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002900}
2901
Damien Millerd79b4242006-03-31 23:11:44 +11002902/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002903int
Damien Miller630d6f42002-01-22 23:17:30 +11002904channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002905{
2906 int id = packet_get_int();
2907 Channel *c = channel_lookup(id);
2908
2909 if (c == NULL)
2910 packet_disconnect("Received close confirmation for "
2911 "out-of-range channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002912 if (channel_proxy_upstream(c, type, seq, ctxt))
2913 return 0;
2914 packet_check_eom();
Damien Miller36187092013-06-10 13:07:11 +10002915 if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
Damien Millerb38eff82000-04-01 11:09:21 +10002916 packet_disconnect("Received close confirmation for "
2917 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002918 channel_free(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002919 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002920}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002921
Damien Millerd79b4242006-03-31 23:11:44 +11002922/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002923int
Damien Miller630d6f42002-01-22 23:17:30 +11002924channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002925{
Damien Millerb38eff82000-04-01 11:09:21 +10002926 int id, remote_id;
2927 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002928
Damien Millerb38eff82000-04-01 11:09:21 +10002929 id = packet_get_int();
2930 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931
markus@openbsd.org8d057842016-09-30 09:19:13 +00002932 if (c==NULL)
2933 packet_disconnect("Received open confirmation for "
2934 "unknown channel %d.", id);
2935 if (channel_proxy_upstream(c, type, seq, ctxt))
2936 return 0;
2937 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002938 packet_disconnect("Received open confirmation for "
2939 "non-opening channel %d.", id);
2940 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002941 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002942 c->remote_id = remote_id;
2943 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002944
2945 if (compat20) {
2946 c->remote_window = packet_get_int();
2947 c->remote_maxpacket = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002948 if (c->open_confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002949 debug2("callback start");
Damien Millerd530f5f2010-05-21 14:57:10 +10002950 c->open_confirm(c->self, 1, c->open_confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002951 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002952 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002953 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002954 c->remote_window, c->remote_maxpacket);
2955 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002956 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002957 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002958}
2959
Ben Lindstrombba81212001-06-25 05:01:22 +00002960static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002961reason2txt(int reason)
2962{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002963 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002964 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2965 return "administratively prohibited";
2966 case SSH2_OPEN_CONNECT_FAILED:
2967 return "connect failed";
2968 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2969 return "unknown channel type";
2970 case SSH2_OPEN_RESOURCE_SHORTAGE:
2971 return "resource shortage";
2972 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002973 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002974}
2975
Damien Millerd79b4242006-03-31 23:11:44 +11002976/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002977int
Damien Miller630d6f42002-01-22 23:17:30 +11002978channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002979{
Kevin Steves12057502001-02-05 14:54:34 +00002980 int id, reason;
2981 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002982 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002983
Damien Millerb38eff82000-04-01 11:09:21 +10002984 id = packet_get_int();
2985 c = channel_lookup(id);
2986
markus@openbsd.org8d057842016-09-30 09:19:13 +00002987 if (c==NULL)
2988 packet_disconnect("Received open failure for "
2989 "unknown channel %d.", id);
2990 if (channel_proxy_upstream(c, type, seq, ctxt))
2991 return 0;
2992 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002993 packet_disconnect("Received open failure for "
2994 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002995 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002996 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002997 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002998 msg = packet_get_string(NULL);
2999 lang = packet_get_string(NULL);
3000 }
Damien Miller996acd22003-04-09 20:59:48 +10003001 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00003002 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Darren Tuckera627d422013-06-02 07:31:17 +10003003 free(msg);
3004 free(lang);
Damien Millerd530f5f2010-05-21 14:57:10 +10003005 if (c->open_confirm) {
3006 debug2("callback start");
3007 c->open_confirm(c->self, 0, c->open_confirm_ctx);
3008 debug2("callback done");
3009 }
Damien Miller33b13562000-04-04 14:38:59 +10003010 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003011 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11003012 /* Schedule the channel for cleanup/deletion. */
3013 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003014 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003015}
3016
Damien Millerd79b4242006-03-31 23:11:44 +11003017/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003018int
Damien Miller630d6f42002-01-22 23:17:30 +11003019channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10003020{
3021 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00003022 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00003023 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10003024
3025 if (!compat20)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003026 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003027
3028 /* Get the channel number and verify it. */
3029 id = packet_get_int();
3030 c = channel_lookup(id);
3031
Damien Millerd47c62a2005-12-13 19:33:57 +11003032 if (c == NULL) {
3033 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003034 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003035 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003036 if (channel_proxy_upstream(c, type, seq, ctxt))
3037 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003038 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11003039 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00003040 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00003041 if ((tmp = c->remote_window + adjust) < c->remote_window)
3042 fatal("channel %d: adjust %u overflows remote window %u",
3043 id, adjust, c->remote_window);
3044 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003045 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003046}
3047
Damien Millerd79b4242006-03-31 23:11:44 +11003048/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003049int
Damien Miller630d6f42002-01-22 23:17:30 +11003050channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003051{
Ben Lindstrome9c99912001-06-09 00:41:05 +00003052 Channel *c = NULL;
3053 u_short host_port;
3054 char *host, *originator_string;
Damien Millerbd740252008-05-19 15:37:09 +10003055 int remote_id;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003056
Ben Lindstrome9c99912001-06-09 00:41:05 +00003057 remote_id = packet_get_int();
3058 host = packet_get_string(NULL);
3059 host_port = packet_get_int();
3060
3061 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
3062 originator_string = packet_get_string(NULL);
3063 } else {
3064 originator_string = xstrdup("unknown (remote did not supply name)");
3065 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003066 packet_check_eom();
Damien Miller7acefbb2014-07-18 14:11:24 +10003067 c = channel_connect_to_port(host, host_port,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003068 "connected socket", originator_string, NULL, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10003069 free(originator_string);
3070 free(host);
Ben Lindstrome9c99912001-06-09 00:41:05 +00003071 if (c == NULL) {
3072 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3073 packet_put_int(remote_id);
3074 packet_send();
Damien Millerbd740252008-05-19 15:37:09 +10003075 } else
3076 c->remote_id = remote_id;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003077 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003078}
3079
Damien Millerb84886b2008-05-19 15:05:07 +10003080/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003081int
Damien Millerb84886b2008-05-19 15:05:07 +10003082channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
3083{
3084 Channel *c;
3085 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11003086 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10003087
3088 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003089 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003090
Damien Miller16a73072008-12-08 09:55:25 +11003091 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11003092 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003093
Damien Miller16a73072008-12-08 09:55:25 +11003094 if ((c = channel_lookup(id)) == NULL) {
3095 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003096 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003097 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003098 if (channel_proxy_upstream(c, type, seq, ctxt))
3099 return 0;
3100 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10003101 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003102 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003103 cc->cb(type, c, cc->ctx);
3104 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003105 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003106 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003107 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003108}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003109
Ben Lindstrome9c99912001-06-09 00:41:05 +00003110/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003111
Ben Lindstrom908afed2001-10-03 17:34:59 +00003112void
3113channel_set_af(int af)
3114{
3115 IPv4or6 = af;
3116}
3117
Damien Millerf6dff7c2011-09-22 21:38:52 +10003118
3119/*
3120 * Determine whether or not a port forward listens to loopback, the
3121 * specified address or wildcard. On the client, a specified bind
3122 * address will always override gateway_ports. On the server, a
3123 * gateway_ports of 1 (``yes'') will override the client's specification
3124 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3125 * will bind to whatever address the client asked for.
3126 *
3127 * Special-case listen_addrs are:
3128 *
3129 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3130 * "" (empty string), "*" -> wildcard v4/v6
3131 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003132 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003133 */
3134static const char *
3135channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003136 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003137{
3138 const char *addr = NULL;
3139 int wildcard = 0;
3140
3141 if (listen_addr == NULL) {
3142 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003143 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003144 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003145 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003146 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3147 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3148 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003149 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003150 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003151 /*
3152 * Notify client if they requested a specific listen
3153 * address and it was overridden.
3154 */
3155 if (*listen_addr != '\0' &&
3156 strcmp(listen_addr, "0.0.0.0") != 0 &&
3157 strcmp(listen_addr, "*") != 0) {
3158 packet_send_debug("Forwarding listen address "
3159 "\"%s\" overridden by server "
3160 "GatewayPorts", listen_addr);
3161 }
Damien Miller602943d2014-07-04 08:59:41 +10003162 } else if (strcmp(listen_addr, "localhost") != 0 ||
3163 strcmp(listen_addr, "127.0.0.1") == 0 ||
3164 strcmp(listen_addr, "::1") == 0) {
3165 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003166 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003167 }
3168 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3169 strcmp(listen_addr, "::1") == 0) {
3170 /*
3171 * If a specific IPv4/IPv6 localhost address has been
3172 * requested then accept it even if gateway_ports is in
3173 * effect. This allows the client to prefer IPv4 or IPv6.
3174 */
3175 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003176 }
3177 if (wildcardp != NULL)
3178 *wildcardp = wildcard;
3179 return addr;
3180}
3181
Damien Millerb16461c2002-01-22 23:29:22 +11003182static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003183channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
3184 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003185{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003186 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003187 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003188 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003189 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003190 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003191 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003192
Damien Millerf91ee4c2005-03-01 21:24:33 +11003193 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003194
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003195 if (is_client && fwd->connect_path != NULL) {
3196 host = fwd->connect_path;
3197 } else {
3198 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3199 fwd->listen_host : fwd->connect_host;
3200 if (host == NULL) {
3201 error("No forward host name.");
3202 return 0;
3203 }
3204 if (strlen(host) >= NI_MAXHOST) {
3205 error("Forward host name too long.");
3206 return 0;
3207 }
Kevin Steves12057502001-02-05 14:54:34 +00003208 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003209
Damien Millerf6dff7c2011-09-22 21:38:52 +10003210 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003211 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3212 is_client, fwd_opts);
3213 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003214 type, wildcard, (addr == NULL) ? "NULL" : addr);
3215
3216 /*
Damien Miller34132e52000-01-14 15:45:46 +11003217 * getaddrinfo returns a loopback address if the hostname is
3218 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003219 */
Damien Miller34132e52000-01-14 15:45:46 +11003220 memset(&hints, 0, sizeof(hints));
3221 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003222 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003223 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003224 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003225 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3226 if (addr == NULL) {
3227 /* This really shouldn't happen */
3228 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003229 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003230 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003231 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003232 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003233 }
Damien Millera7270302005-07-06 09:36:05 +10003234 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003235 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003236 if (allocated_listen_port != NULL)
3237 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003238 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003239 switch (ai->ai_family) {
3240 case AF_INET:
3241 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3242 sin_port;
3243 break;
3244 case AF_INET6:
3245 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3246 sin6_port;
3247 break;
3248 default:
Damien Miller34132e52000-01-14 15:45:46 +11003249 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003250 }
3251 /*
3252 * If allocating a port for -R forwards, then use the
3253 * same port for all address families.
3254 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003255 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003256 allocated_listen_port != NULL && *allocated_listen_port > 0)
3257 *lport_p = htons(*allocated_listen_port);
3258
Damien Miller34132e52000-01-14 15:45:46 +11003259 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
3260 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003261 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003262 continue;
3263 }
3264 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003265 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003266 if (sock < 0) {
3267 /* this is no error since kernel may not support ipv6 */
3268 verbose("socket: %.100s", strerror(errno));
3269 continue;
3270 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003271
3272 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003273 if (ai->ai_family == AF_INET6)
3274 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003275
Damien Miller4bf648f2009-02-14 16:28:21 +11003276 debug("Local forwarding listening on %s port %s.",
3277 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003278
Damien Miller34132e52000-01-14 15:45:46 +11003279 /* Bind the socket to the address. */
3280 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
3281 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003282 if (!ai->ai_next)
3283 error("bind: %.100s", strerror(errno));
3284 else
3285 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003286
Damien Miller34132e52000-01-14 15:45:46 +11003287 close(sock);
3288 continue;
3289 }
3290 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003291 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003292 error("listen: %.100s", strerror(errno));
3293 close(sock);
3294 continue;
3295 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003296
3297 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003298 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003299 * record what we got.
3300 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003301 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003302 allocated_listen_port != NULL &&
3303 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003304 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003305 debug("Allocated listen port %d",
3306 *allocated_listen_port);
3307 }
3308
Damien Miller34132e52000-01-14 15:45:46 +11003309 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00003310 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003311 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003312 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003313 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003314 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003315 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003316 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003317 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3318 c->listening_port = *allocated_listen_port;
3319 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003320 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003321 success = 1;
3322 }
3323 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003324 error("%s: cannot listen to port: %d", __func__,
3325 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003326 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003327 return success;
Damien Miller95def091999-11-25 00:26:21 +11003328}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003329
Damien Miller7acefbb2014-07-18 14:11:24 +10003330static int
3331channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3332 struct ForwardOptions *fwd_opts)
3333{
3334 struct sockaddr_un sunaddr;
3335 const char *path;
3336 Channel *c;
3337 int port, sock;
3338 mode_t omask;
3339
3340 switch (type) {
3341 case SSH_CHANNEL_UNIX_LISTENER:
3342 if (fwd->connect_path != NULL) {
3343 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3344 error("Local connecting path too long: %s",
3345 fwd->connect_path);
3346 return 0;
3347 }
3348 path = fwd->connect_path;
3349 port = PORT_STREAMLOCAL;
3350 } else {
3351 if (fwd->connect_host == NULL) {
3352 error("No forward host name.");
3353 return 0;
3354 }
3355 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3356 error("Forward host name too long.");
3357 return 0;
3358 }
3359 path = fwd->connect_host;
3360 port = fwd->connect_port;
3361 }
3362 break;
3363 case SSH_CHANNEL_RUNIX_LISTENER:
3364 path = fwd->listen_path;
3365 port = PORT_STREAMLOCAL;
3366 break;
3367 default:
3368 error("%s: unexpected channel type %d", __func__, type);
3369 return 0;
3370 }
3371
3372 if (fwd->listen_path == NULL) {
3373 error("No forward path name.");
3374 return 0;
3375 }
3376 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3377 error("Local listening path too long: %s", fwd->listen_path);
3378 return 0;
3379 }
3380
3381 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3382
3383 /* Start a Unix domain listener. */
3384 omask = umask(fwd_opts->streamlocal_bind_mask);
3385 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3386 fwd_opts->streamlocal_bind_unlink);
3387 umask(omask);
3388 if (sock < 0)
3389 return 0;
3390
3391 debug("Local forwarding listening on path %s.", fwd->listen_path);
3392
3393 /* Allocate a channel number for the socket. */
3394 c = channel_new("unix listener", type, sock, sock, -1,
3395 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3396 0, "unix listener", 1);
3397 c->path = xstrdup(path);
3398 c->host_port = port;
3399 c->listening_port = PORT_STREAMLOCAL;
3400 c->listening_addr = xstrdup(fwd->listen_path);
3401 return 1;
3402}
3403
3404static int
3405channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003406{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003407 u_int i;
3408 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003409
Darren Tucker47eede72005-03-14 23:08:12 +11003410 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003411 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003412 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3413 continue;
3414 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3415 debug2("%s: close channel %d", __func__, i);
3416 channel_free(c);
3417 found = 1;
3418 }
3419 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003420
Damien Millerf6dff7c2011-09-22 21:38:52 +10003421 return (found);
3422}
3423
Damien Miller7acefbb2014-07-18 14:11:24 +10003424static int
3425channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003426{
3427 u_int i;
3428 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003429
3430 for (i = 0; i < channels_alloc; i++) {
3431 Channel *c = channels[i];
3432 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3433 continue;
3434 if (c->path == NULL)
3435 continue;
3436 if (strcmp(c->path, path) == 0) {
3437 debug2("%s: close channel %d", __func__, i);
3438 channel_free(c);
3439 found = 1;
3440 }
3441 }
3442
3443 return (found);
3444}
3445
3446int
3447channel_cancel_rport_listener(struct Forward *fwd)
3448{
3449 if (fwd->listen_path != NULL)
3450 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3451 else
3452 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3453}
3454
3455static int
3456channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3457 int cport, struct ForwardOptions *fwd_opts)
3458{
3459 u_int i;
3460 int found = 0;
3461 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003462
3463 for (i = 0; i < channels_alloc; i++) {
3464 Channel *c = channels[i];
3465 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3466 continue;
Damien Millerff773642011-09-22 21:39:48 +10003467 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003468 continue;
Damien Millerff773642011-09-22 21:39:48 +10003469 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3470 /* skip dynamic forwardings */
3471 if (c->host_port == 0)
3472 continue;
3473 } else {
3474 if (c->host_port != cport)
3475 continue;
3476 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003477 if ((c->listening_addr == NULL && addr != NULL) ||
3478 (c->listening_addr != NULL && addr == NULL))
3479 continue;
3480 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003481 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003482 channel_free(c);
3483 found = 1;
3484 }
3485 }
3486
3487 return (found);
3488}
3489
Damien Miller7acefbb2014-07-18 14:11:24 +10003490static int
3491channel_cancel_lport_listener_streamlocal(const char *path)
3492{
3493 u_int i;
3494 int found = 0;
3495
3496 if (path == NULL) {
3497 error("%s: no path specified.", __func__);
3498 return 0;
3499 }
3500
3501 for (i = 0; i < channels_alloc; i++) {
3502 Channel *c = channels[i];
3503 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3504 continue;
3505 if (c->listening_addr == NULL)
3506 continue;
3507 if (strcmp(c->listening_addr, path) == 0) {
3508 debug2("%s: close channel %d", __func__, i);
3509 channel_free(c);
3510 found = 1;
3511 }
3512 }
3513
3514 return (found);
3515}
3516
3517int
3518channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3519{
3520 if (fwd->listen_path != NULL)
3521 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3522 else
3523 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3524}
3525
Damien Millerb16461c2002-01-22 23:29:22 +11003526/* protocol local port fwd, used by ssh (and sshd in v1) */
3527int
Damien Miller7acefbb2014-07-18 14:11:24 +10003528channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003529{
Damien Miller7acefbb2014-07-18 14:11:24 +10003530 if (fwd->listen_path != NULL) {
3531 return channel_setup_fwd_listener_streamlocal(
3532 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3533 } else {
3534 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3535 fwd, NULL, fwd_opts);
3536 }
Damien Millerb16461c2002-01-22 23:29:22 +11003537}
3538
3539/* protocol v2 remote port fwd, used by sshd */
3540int
Damien Miller7acefbb2014-07-18 14:11:24 +10003541channel_setup_remote_fwd_listener(struct Forward *fwd,
3542 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003543{
Damien Miller7acefbb2014-07-18 14:11:24 +10003544 if (fwd->listen_path != NULL) {
3545 return channel_setup_fwd_listener_streamlocal(
3546 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3547 } else {
3548 return channel_setup_fwd_listener_tcpip(
3549 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3550 fwd_opts);
3551 }
Damien Millerb16461c2002-01-22 23:29:22 +11003552}
3553
Damien Miller5428f641999-11-25 11:54:57 +11003554/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003555 * Translate the requested rfwd listen host to something usable for
3556 * this server.
3557 */
3558static const char *
3559channel_rfwd_bind_host(const char *listen_host)
3560{
3561 if (listen_host == NULL) {
3562 if (datafellows & SSH_BUG_RFWD_ADDR)
3563 return "127.0.0.1";
3564 else
3565 return "localhost";
3566 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3567 if (datafellows & SSH_BUG_RFWD_ADDR)
3568 return "0.0.0.0";
3569 else
3570 return "";
3571 } else
3572 return listen_host;
3573}
3574
3575/*
Damien Miller5428f641999-11-25 11:54:57 +11003576 * Initiate forwarding of connections to port "port" on remote host through
3577 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003578 * Returns handle (index) for updating the dynamic listen port with
3579 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003580 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003581int
Damien Miller7acefbb2014-07-18 14:11:24 +10003582channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003583{
Darren Tucker68afb8c2011-10-02 18:59:03 +11003584 int type, success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003585
Damien Miller95def091999-11-25 00:26:21 +11003586 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10003587 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10003588 packet_start(SSH2_MSG_GLOBAL_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003589 if (fwd->listen_path != NULL) {
3590 packet_put_cstring("streamlocal-forward@openssh.com");
3591 packet_put_char(1); /* boolean: want reply */
3592 packet_put_cstring(fwd->listen_path);
3593 } else {
3594 packet_put_cstring("tcpip-forward");
3595 packet_put_char(1); /* boolean: want reply */
3596 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3597 packet_put_int(fwd->listen_port);
3598 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003599 packet_send();
3600 packet_write_wait();
3601 /* Assume that server accepts the request */
3602 success = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003603 } else if (fwd->listen_path == NULL) {
Damien Miller33b13562000-04-04 14:38:59 +10003604 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003605 packet_put_int(fwd->listen_port);
3606 packet_put_cstring(fwd->connect_host);
3607 packet_put_int(fwd->connect_port);
Damien Miller33b13562000-04-04 14:38:59 +10003608 packet_send();
3609 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003610
3611 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11003612 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003613 switch (type) {
3614 case SSH_SMSG_SUCCESS:
3615 success = 1;
3616 break;
3617 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11003618 break;
3619 default:
3620 /* Unknown packet */
3621 packet_disconnect("Protocol error for port forward request:"
3622 "received packet type %d.", type);
3623 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003624 } else {
3625 logit("Warning: Server does not support remote stream local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11003626 }
3627 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003628 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003629 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003630 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003631 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003632 if (fwd->connect_path != NULL) {
3633 permitted_opens[idx].host_to_connect =
3634 xstrdup(fwd->connect_path);
3635 permitted_opens[idx].port_to_connect =
3636 PORT_STREAMLOCAL;
3637 } else {
3638 permitted_opens[idx].host_to_connect =
3639 xstrdup(fwd->connect_host);
3640 permitted_opens[idx].port_to_connect =
3641 fwd->connect_port;
3642 }
3643 if (fwd->listen_path != NULL) {
3644 permitted_opens[idx].listen_host = NULL;
3645 permitted_opens[idx].listen_path =
3646 xstrdup(fwd->listen_path);
3647 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3648 } else {
3649 permitted_opens[idx].listen_host =
3650 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3651 permitted_opens[idx].listen_path = NULL;
3652 permitted_opens[idx].listen_port = fwd->listen_port;
3653 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003654 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003655 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003656 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003657}
3658
Damien Miller4b3ed642014-07-02 15:29:40 +10003659static int
3660open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003661 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003662{
3663 if (allowed_open->host_to_connect == NULL)
3664 return 0;
3665 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3666 allowed_open->port_to_connect != requestedport)
3667 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003668 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3669 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003670 return 0;
3671 return 1;
3672}
3673
3674/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003675 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003676 * we don't support FWD_PERMIT_ANY_PORT and
3677 * need to translate between the configured-host (listen_host)
3678 * and what we've sent to the remote server (channel_rfwd_bind_host)
3679 */
3680static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003681open_listen_match_tcpip(ForwardPermission *allowed_open,
3682 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003683{
3684 const char *allowed_host;
3685
3686 if (allowed_open->host_to_connect == NULL)
3687 return 0;
3688 if (allowed_open->listen_port != requestedport)
3689 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003690 if (!translate && allowed_open->listen_host == NULL &&
3691 requestedhost == NULL)
3692 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003693 allowed_host = translate ?
3694 channel_rfwd_bind_host(allowed_open->listen_host) :
3695 allowed_open->listen_host;
3696 if (allowed_host == NULL ||
3697 strcmp(allowed_host, requestedhost) != 0)
3698 return 0;
3699 return 1;
3700}
3701
Damien Miller7acefbb2014-07-18 14:11:24 +10003702static int
3703open_listen_match_streamlocal(ForwardPermission *allowed_open,
3704 const char *requestedpath)
3705{
3706 if (allowed_open->host_to_connect == NULL)
3707 return 0;
3708 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3709 return 0;
3710 if (allowed_open->listen_path == NULL ||
3711 strcmp(allowed_open->listen_path, requestedpath) != 0)
3712 return 0;
3713 return 1;
3714}
3715
Damien Miller5428f641999-11-25 11:54:57 +11003716/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003717 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003718 * local side.
3719 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003720static int
3721channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003722{
3723 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003724
3725 if (!compat20)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003726 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003727
3728 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003729 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003730 break;
3731 }
3732 if (i >= num_permitted_opens) {
3733 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003734 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003735 }
3736 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3737 packet_put_cstring("cancel-tcpip-forward");
3738 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003739 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003740 packet_put_int(port);
3741 packet_send();
3742
Damien Miller4b3ed642014-07-02 15:29:40 +10003743 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003744 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003745 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003746 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003747 free(permitted_opens[i].listen_host);
3748 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003749 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003750 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003751
3752 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003753}
3754
3755/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003756 * Request cancellation of remote forwarding of Unix domain socket
3757 * path from local side.
3758 */
3759static int
3760channel_request_rforward_cancel_streamlocal(const char *path)
3761{
3762 int i;
3763
3764 if (!compat20)
3765 return -1;
3766
3767 for (i = 0; i < num_permitted_opens; i++) {
3768 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3769 break;
3770 }
3771 if (i >= num_permitted_opens) {
3772 debug("%s: requested forward not found", __func__);
3773 return -1;
3774 }
3775 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3776 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3777 packet_put_char(0);
3778 packet_put_cstring(path);
3779 packet_send();
3780
3781 permitted_opens[i].listen_port = 0;
3782 permitted_opens[i].port_to_connect = 0;
3783 free(permitted_opens[i].host_to_connect);
3784 permitted_opens[i].host_to_connect = NULL;
3785 permitted_opens[i].listen_host = NULL;
3786 free(permitted_opens[i].listen_path);
3787 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003788 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003789
3790 return 0;
3791}
3792
3793/*
3794 * Request cancellation of remote forwarding of a connection from local side.
3795 */
3796int
3797channel_request_rforward_cancel(struct Forward *fwd)
3798{
3799 if (fwd->listen_path != NULL) {
3800 return (channel_request_rforward_cancel_streamlocal(
3801 fwd->listen_path));
3802 } else {
3803 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3804 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3805 }
3806}
3807
3808/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003809 * Permits opening to any host/port if permitted_opens[] is empty. This is
3810 * usually called by the server, because the user could connect to any port
3811 * anyway, and the server has no way to know but to trust the client anyway.
3812 */
3813void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003814channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003815{
3816 if (num_permitted_opens == 0)
3817 all_opens_permitted = 1;
3818}
3819
Ben Lindstroma3700052001-04-05 23:26:32 +00003820void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003821channel_add_permitted_opens(char *host, int port)
3822{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003823 debug("allow port forwarding to host %s port %d", host, port);
3824
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003825 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003826 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003827 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3828 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003829 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003830 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003831 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003832 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003833 num_permitted_opens++;
3834
3835 all_opens_permitted = 0;
3836}
3837
Darren Tucker68afb8c2011-10-02 18:59:03 +11003838/*
3839 * Update the listen port for a dynamic remote forward, after
3840 * the actual 'newport' has been allocated. If 'newport' < 0 is
3841 * passed then they entry will be invalidated.
3842 */
3843void
3844channel_update_permitted_opens(int idx, int newport)
3845{
3846 if (idx < 0 || idx >= num_permitted_opens) {
3847 debug("channel_update_permitted_opens: index out of range:"
3848 " %d num_permitted_opens %d", idx, num_permitted_opens);
3849 return;
3850 }
3851 debug("%s allowed port %d for forwarding to host %s port %d",
3852 newport > 0 ? "Updating" : "Removing",
3853 newport,
3854 permitted_opens[idx].host_to_connect,
3855 permitted_opens[idx].port_to_connect);
3856 if (newport >= 0) {
3857 permitted_opens[idx].listen_port =
3858 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3859 } else {
3860 permitted_opens[idx].listen_port = 0;
3861 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003862 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003863 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003864 free(permitted_opens[idx].listen_host);
3865 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003866 free(permitted_opens[idx].listen_path);
3867 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003868 }
3869}
3870
Damien Millera765cf42006-07-24 14:08:13 +10003871int
Damien Miller9b439df2006-07-24 14:04:00 +10003872channel_add_adm_permitted_opens(char *host, int port)
3873{
Damien Millera765cf42006-07-24 14:08:13 +10003874 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003875
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003876 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003877 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003878 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3879 = xstrdup(host);
3880 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003881 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003882 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003883 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003884 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003885}
3886
3887void
Damien Millerc6081482012-04-22 11:18:53 +10003888channel_disable_adm_local_opens(void)
3889{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003890 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003891 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003892 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3893 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003894}
3895
3896void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003897channel_clear_permitted_opens(void)
3898{
3899 int i;
3900
Damien Miller4b3ed642014-07-02 15:29:40 +10003901 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003902 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003903 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003904 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003905 }
Darren Tuckera627d422013-06-02 07:31:17 +10003906 free(permitted_opens);
3907 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003908 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003909}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003910
Damien Miller9b439df2006-07-24 14:04:00 +10003911void
3912channel_clear_adm_permitted_opens(void)
3913{
3914 int i;
3915
Damien Miller4b3ed642014-07-02 15:29:40 +10003916 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003917 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003918 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003919 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003920 }
Darren Tuckera627d422013-06-02 07:31:17 +10003921 free(permitted_adm_opens);
3922 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003923 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003924}
3925
Darren Tuckere7140f22008-06-10 23:01:51 +10003926void
3927channel_print_adm_permitted_opens(void)
3928{
Damien Miller6ef17492008-07-16 22:42:06 +10003929 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003930
Damien Millerb53d8a12009-01-28 16:13:04 +11003931 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003932 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003933 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003934 return;
3935 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003936 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003937 if (permitted_adm_opens[i].host_to_connect == NULL)
3938 printf(" none");
3939 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003940 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3941 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003942 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003943}
3944
Darren Tucker1338b9e2011-10-02 18:57:35 +11003945/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3946int
3947permitopen_port(const char *p)
3948{
3949 int port;
3950
3951 if (strcmp(p, "*") == 0)
3952 return FWD_PERMIT_ANY_PORT;
3953 if ((port = a2port(p)) > 0)
3954 return port;
3955 return -1;
3956}
3957
Damien Millerbd740252008-05-19 15:37:09 +10003958/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003959static int
Damien Millerbd740252008-05-19 15:37:09 +10003960connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003961{
Damien Millerbd740252008-05-19 15:37:09 +10003962 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003963 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00003964 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003965
Damien Millerbd740252008-05-19 15:37:09 +10003966 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003967 switch (cctx->ai->ai_family) {
3968 case AF_UNIX:
3969 /* unix:pathname instead of host:port */
3970 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3971 strlcpy(ntop, "unix", sizeof(ntop));
3972 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3973 break;
3974 case AF_INET:
3975 case AF_INET6:
3976 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3977 ntop, sizeof(ntop), strport, sizeof(strport),
3978 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3979 error("connect_next: getnameinfo failed");
3980 continue;
3981 }
3982 break;
3983 default:
Damien Miller34132e52000-01-14 15:45:46 +11003984 continue;
3985 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003986 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3987 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003988 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003989 error("socket: %.100s", strerror(errno));
3990 else
3991 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003992 continue;
3993 }
Damien Miller232711f2004-06-15 10:35:30 +10003994 if (set_nonblock(sock) == -1)
3995 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003996 if (connect(sock, cctx->ai->ai_addr,
3997 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3998 debug("connect_next: host %.100s ([%.100s]:%s): "
3999 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004000 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004001 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004002 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004003 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004004 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004005 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004006 if (cctx->ai->ai_family != AF_UNIX)
4007 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004008 debug("connect_next: host %.100s ([%.100s]:%s) "
4009 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4010 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004011 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004012 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004013 return -1;
4014}
Damien Millerb38eff82000-04-01 11:09:21 +10004015
Damien Millerbd740252008-05-19 15:37:09 +10004016static void
4017channel_connect_ctx_free(struct channel_connect *cctx)
4018{
Darren Tuckera627d422013-06-02 07:31:17 +10004019 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004020 if (cctx->aitop) {
4021 if (cctx->aitop->ai_family == AF_UNIX)
4022 free(cctx->aitop);
4023 else
4024 freeaddrinfo(cctx->aitop);
4025 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004026 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004027}
4028
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004029/*
4030 * Return CONNECTING channel to remote host:port or local socket path,
4031 * passing back the failure reason if appropriate.
4032 */
Damien Millerbd740252008-05-19 15:37:09 +10004033static Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004034connect_to_reason(const char *name, int port, char *ctype, char *rname,
4035 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10004036{
4037 struct addrinfo hints;
4038 int gaierr;
4039 int sock = -1;
4040 char strport[NI_MAXSERV];
4041 struct channel_connect cctx;
4042 Channel *c;
4043
Damien Miller2bcb8662008-07-12 17:12:29 +10004044 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10004045
4046 if (port == PORT_STREAMLOCAL) {
4047 struct sockaddr_un *sunaddr;
4048 struct addrinfo *ai;
4049
4050 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4051 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
4052 return (NULL);
4053 }
4054
4055 /*
4056 * Fake up a struct addrinfo for AF_UNIX connections.
4057 * channel_connect_ctx_free() must check ai_family
4058 * and use free() not freeaddirinfo() for AF_UNIX.
4059 */
4060 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4061 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4062 ai->ai_addr = (struct sockaddr *)(ai + 1);
4063 ai->ai_addrlen = sizeof(*sunaddr);
4064 ai->ai_family = AF_UNIX;
4065 ai->ai_socktype = SOCK_STREAM;
4066 ai->ai_protocol = PF_UNSPEC;
4067 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4068 sunaddr->sun_family = AF_UNIX;
4069 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
4070 cctx.aitop = ai;
4071 } else {
4072 memset(&hints, 0, sizeof(hints));
4073 hints.ai_family = IPv4or6;
4074 hints.ai_socktype = SOCK_STREAM;
4075 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004076 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
4077 != 0) {
4078 if (errmsg != NULL)
4079 *errmsg = ssh_gai_strerror(gaierr);
4080 if (reason != NULL)
4081 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10004082 error("connect_to %.100s: unknown host (%s)", name,
4083 ssh_gai_strerror(gaierr));
4084 return NULL;
4085 }
Damien Millerbd740252008-05-19 15:37:09 +10004086 }
4087
Damien Miller7acefbb2014-07-18 14:11:24 +10004088 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10004089 cctx.port = port;
4090 cctx.ai = cctx.aitop;
4091
4092 if ((sock = connect_next(&cctx)) == -1) {
4093 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004094 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004095 channel_connect_ctx_free(&cctx);
4096 return NULL;
4097 }
4098 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4099 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4100 c->connect_ctx = cctx;
4101 return c;
4102}
4103
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004104/* Return CONNECTING channel to remote host:port or local socket path */
4105static Channel *
4106connect_to(const char *name, int port, char *ctype, char *rname)
4107{
4108 return connect_to_reason(name, port, ctype, rname, NULL, NULL);
4109}
4110
markus@openbsd.org8d057842016-09-30 09:19:13 +00004111/*
4112 * returns either the newly connected channel or the downstream channel
4113 * that needs to deal with this connection.
4114 */
Damien Millerbd740252008-05-19 15:37:09 +10004115Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10004116channel_connect_by_listen_address(const char *listen_host,
4117 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004118{
4119 int i;
4120
4121 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004122 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004123 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00004124 if (permitted_opens[i].downstream)
4125 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10004126 return connect_to(
4127 permitted_opens[i].host_to_connect,
4128 permitted_opens[i].port_to_connect, ctype, rname);
4129 }
4130 }
4131 error("WARNING: Server requests forwarding for unknown listen_port %d",
4132 listen_port);
4133 return NULL;
4134}
4135
Damien Miller7acefbb2014-07-18 14:11:24 +10004136Channel *
4137channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
4138{
4139 int i;
4140
4141 for (i = 0; i < num_permitted_opens; i++) {
4142 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
4143 return connect_to(
4144 permitted_opens[i].host_to_connect,
4145 permitted_opens[i].port_to_connect, ctype, rname);
4146 }
4147 }
4148 error("WARNING: Server requests forwarding for unknown path %.100s",
4149 path);
4150 return NULL;
4151}
4152
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004153/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004154Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004155channel_connect_to_port(const char *host, u_short port, char *ctype,
4156 char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004157{
Damien Miller9b439df2006-07-24 14:04:00 +10004158 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004159
4160 permit = all_opens_permitted;
4161 if (!permit) {
4162 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10004163 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004164 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004165 break;
4166 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004167 }
Damien Miller9b439df2006-07-24 14:04:00 +10004168
4169 if (num_adm_permitted_opens > 0) {
4170 permit_adm = 0;
4171 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10004172 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004173 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004174 break;
4175 }
Damien Miller9b439df2006-07-24 14:04:00 +10004176 }
4177
4178 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004179 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004180 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004181 if (reason != NULL)
4182 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004183 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004184 }
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004185 return connect_to_reason(host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004186}
4187
Damien Miller7acefbb2014-07-18 14:11:24 +10004188/* Check if connecting to that path is permitted and connect. */
4189Channel *
4190channel_connect_to_path(const char *path, char *ctype, char *rname)
4191{
4192 int i, permit, permit_adm = 1;
4193
4194 permit = all_opens_permitted;
4195 if (!permit) {
4196 for (i = 0; i < num_permitted_opens; i++)
4197 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
4198 permit = 1;
4199 break;
4200 }
4201 }
4202
4203 if (num_adm_permitted_opens > 0) {
4204 permit_adm = 0;
4205 for (i = 0; i < num_adm_permitted_opens; i++)
4206 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
4207 permit_adm = 1;
4208 break;
4209 }
4210 }
4211
4212 if (!permit || !permit_adm) {
4213 logit("Received request to connect to path %.100s, "
4214 "but the request was denied.", path);
4215 return NULL;
4216 }
4217 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
4218}
4219
Damien Miller0e220db2004-06-15 10:34:08 +10004220void
4221channel_send_window_changes(void)
4222{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10004223 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004224 struct winsize ws;
4225
4226 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11004227 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10004228 channels[i]->type != SSH_CHANNEL_OPEN)
4229 continue;
4230 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
4231 continue;
4232 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11004233 packet_put_int((u_int)ws.ws_col);
4234 packet_put_int((u_int)ws.ws_row);
4235 packet_put_int((u_int)ws.ws_xpixel);
4236 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10004237 packet_send();
4238 }
4239}
4240
Ben Lindstrome9c99912001-06-09 00:41:05 +00004241/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004242
Damien Miller5428f641999-11-25 11:54:57 +11004243/*
4244 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004245 * Returns 0 and a suitable display number for the DISPLAY variable
4246 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004247 */
Kevin Steves366298c2001-12-19 17:58:01 +00004248int
Damien Miller95c249f2002-02-05 12:11:34 +11004249x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10004250 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004251{
Damien Millere7378562001-12-21 14:58:35 +11004252 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004253 int display_number, sock;
4254 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004255 struct addrinfo hints, *ai, *aitop;
4256 char strport[NI_MAXSERV];
4257 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004258
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004259 if (chanids == NULL)
4260 return -1;
4261
Damien Millera34a28b1999-12-14 10:47:15 +11004262 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004263 display_number < MAX_DISPLAYS;
4264 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004265 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004266 memset(&hints, 0, sizeof(hints));
4267 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004268 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004269 hints.ai_socktype = SOCK_STREAM;
4270 snprintf(strport, sizeof strport, "%d", port);
4271 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004272 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004273 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004274 }
Damien Miller34132e52000-01-14 15:45:46 +11004275 for (ai = aitop; ai; ai = ai->ai_next) {
4276 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
4277 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004278 sock = socket(ai->ai_family, ai->ai_socktype,
4279 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004280 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004281 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4282#ifdef EPFNOSUPPORT
4283 && (errno != EPFNOSUPPORT)
4284#endif
4285 ) {
Damien Millere2192732000-01-17 13:22:55 +11004286 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004287 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004288 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004289 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004290 debug("x11_create_display_inet: Socket family %d not supported",
4291 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004292 continue;
4293 }
Damien Miller34132e52000-01-14 15:45:46 +11004294 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004295 if (ai->ai_family == AF_INET6)
4296 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004297 if (x11_use_localhost)
4298 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004299 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004300 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004301 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11004302
Damien Miller34132e52000-01-14 15:45:46 +11004303 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11004304 close(socks[n]);
4305 }
4306 num_socks = 0;
4307 break;
4308 }
4309 socks[num_socks++] = sock;
4310 if (num_socks == NUM_SOCKS)
4311 break;
Damien Miller95def091999-11-25 00:26:21 +11004312 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004313 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004314 if (num_socks > 0)
4315 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004316 }
Damien Miller95def091999-11-25 00:26:21 +11004317 if (display_number >= MAX_DISPLAYS) {
4318 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004319 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004320 }
Damien Miller95def091999-11-25 00:26:21 +11004321 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004322 for (n = 0; n < num_socks; n++) {
4323 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004324 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004325 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004326 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004327 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004328 }
Damien Miller95def091999-11-25 00:26:21 +11004329 }
Damien Miller34132e52000-01-14 15:45:46 +11004330
Damien Miller34132e52000-01-14 15:45:46 +11004331 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004332 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004333 for (n = 0; n < num_socks; n++) {
4334 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11004335 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004336 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4337 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004338 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004339 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004340 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004341 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004342 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004343
Kevin Steves366298c2001-12-19 17:58:01 +00004344 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004345 *display_numberp = display_number;
4346 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004347}
4348
Ben Lindstrombba81212001-06-25 05:01:22 +00004349static int
Damien Miller819dbb62009-01-21 16:46:26 +11004350connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004351{
Damien Miller95def091999-11-25 00:26:21 +11004352 int sock;
4353 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004354
Damien Miller3afe3752001-12-21 12:39:51 +11004355 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4356 if (sock < 0)
4357 error("socket: %.100s", strerror(errno));
4358 memset(&addr, 0, sizeof(addr));
4359 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004360 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004361 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004362 return sock;
4363 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004364 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4365 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004366}
4367
Damien Miller819dbb62009-01-21 16:46:26 +11004368static int
4369connect_local_xsocket(u_int dnr)
4370{
4371 char buf[1024];
4372 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4373 return connect_local_xsocket_path(buf);
4374}
4375
Darren Tuckerda39b092017-03-10 13:22:32 +11004376#ifdef __APPLE__
4377static int
4378is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4379{
4380 struct stat sbuf;
4381
4382 if (strlcpy(path, display, pathlen) >= pathlen) {
4383 error("%s: display path too long", __func__);
4384 return 0;
4385 }
4386 if (display[0] != '/')
4387 return 0;
4388 if (stat(path, &sbuf) == 0) {
4389 return 1;
4390 } else {
4391 char *dot = strrchr(path, '.');
4392 if (dot != NULL) {
4393 *dot = '\0';
4394 if (stat(path, &sbuf) == 0) {
4395 return 1;
4396 }
4397 }
4398 }
4399 return 0;
4400}
4401#endif
4402
Damien Millerbd483e72000-04-30 10:00:53 +10004403int
4404x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004405{
Damien Miller57c4e872006-03-31 23:11:07 +11004406 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004407 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004408 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004409 struct addrinfo hints, *ai, *aitop;
4410 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004411 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004412
Damien Miller95def091999-11-25 00:26:21 +11004413 /* Try to open a socket for the local X server. */
4414 display = getenv("DISPLAY");
4415 if (!display) {
4416 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004417 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004418 }
Damien Miller5428f641999-11-25 11:54:57 +11004419 /*
4420 * Now we decode the value of the DISPLAY variable and make a
4421 * connection to the real X server.
4422 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004423
Damien Miller819dbb62009-01-21 16:46:26 +11004424#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004425 /* Check if display is a path to a socket (as set by launchd). */
4426 {
4427 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004428
Darren Tuckerda39b092017-03-10 13:22:32 +11004429 if (is_path_to_xsocket(display, path, sizeof(path))) {
4430 debug("x11_connect_display: $DISPLAY is launchd");
4431
4432 /* Create a socket. */
4433 sock = connect_local_xsocket_path(path);
4434 if (sock < 0)
4435 return -1;
4436
4437 /* OK, we now have a connection to the display. */
4438 return sock;
4439 }
Damien Miller819dbb62009-01-21 16:46:26 +11004440 }
4441#endif
Damien Miller5428f641999-11-25 11:54:57 +11004442 /*
4443 * Check if it is a unix domain socket. Unix domain displays are in
4444 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4445 */
Damien Miller95def091999-11-25 00:26:21 +11004446 if (strncmp(display, "unix:", 5) == 0 ||
4447 display[0] == ':') {
4448 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004449 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004450 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004451 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004452 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004453 }
4454 /* Create a socket. */
4455 sock = connect_local_xsocket(display_number);
4456 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004457 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004458
4459 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004460 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004461 }
Damien Miller5428f641999-11-25 11:54:57 +11004462 /*
4463 * Connect to an inet socket. The DISPLAY value is supposedly
4464 * hostname:d[.s], where hostname may also be numeric IP address.
4465 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004466 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004467 cp = strchr(buf, ':');
4468 if (!cp) {
4469 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004470 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004471 }
Damien Miller95def091999-11-25 00:26:21 +11004472 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004473 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004474 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004475 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004476 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004477 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004478 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004479
Damien Miller34132e52000-01-14 15:45:46 +11004480 /* Look up the host address */
4481 memset(&hints, 0, sizeof(hints));
4482 hints.ai_family = IPv4or6;
4483 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004484 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004485 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004486 error("%.100s: unknown host. (%s)", buf,
4487 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004488 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004489 }
Damien Miller34132e52000-01-14 15:45:46 +11004490 for (ai = aitop; ai; ai = ai->ai_next) {
4491 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004492 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004493 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004494 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004495 continue;
4496 }
4497 /* Connect it to the display. */
4498 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004499 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004500 6000 + display_number, strerror(errno));
4501 close(sock);
4502 continue;
4503 }
4504 /* Success */
4505 break;
Damien Miller34132e52000-01-14 15:45:46 +11004506 }
Damien Miller34132e52000-01-14 15:45:46 +11004507 freeaddrinfo(aitop);
4508 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004509 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004510 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004511 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004512 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004513 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004514 return sock;
4515}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004516
Damien Millerbd483e72000-04-30 10:00:53 +10004517/*
4518 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
4519 * the remote channel number. We should do whatever we want, and respond
4520 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
4521 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004522
Damien Miller8473dd82006-07-24 14:08:32 +10004523/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004524int
Damien Miller630d6f42002-01-22 23:17:30 +11004525x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10004526{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004527 Channel *c = NULL;
4528 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10004529 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10004530
4531 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004532
4533 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00004534
4535 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004536 remote_host = packet_get_string(NULL);
4537 } else {
4538 remote_host = xstrdup("unknown (remote did not supply name)");
4539 }
Damien Miller48b03fc2002-01-22 23:11:40 +11004540 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10004541
4542 /* Obtain a connection to the real X display. */
4543 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004544 if (sock != -1) {
4545 /* Allocate a channel for this connection. */
4546 c = channel_new("connected x11 socket",
4547 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
4548 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11004549 c->remote_id = remote_id;
4550 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004551 }
Darren Tuckera627d422013-06-02 07:31:17 +10004552 free(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004553 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10004554 /* Send refusal to the remote host. */
4555 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004556 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10004557 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10004558 /* Send a confirmation to the remote host. */
4559 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004560 packet_put_int(remote_id);
4561 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10004562 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004563 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004564 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004565}
4566
Damien Miller69b69aa2000-10-28 14:19:58 +11004567/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10004568/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004569int
Damien Miller630d6f42002-01-22 23:17:30 +11004570deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11004571{
4572 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00004573
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00004574 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11004575 case SSH_SMSG_AGENT_OPEN:
4576 error("Warning: ssh server tried agent forwarding.");
4577 break;
4578 case SSH_SMSG_X11_OPEN:
4579 error("Warning: ssh server tried X11 forwarding.");
4580 break;
4581 default:
Damien Miller630d6f42002-01-22 23:17:30 +11004582 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11004583 break;
4584 }
Damien Miller5eb137c2005-12-31 16:19:53 +11004585 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11004586 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
4587 packet_put_int(rchan);
4588 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004589 return 0;
Damien Miller69b69aa2000-10-28 14:19:58 +11004590}
4591
Damien Miller5428f641999-11-25 11:54:57 +11004592/*
4593 * Requests forwarding of X11 connections, generates fake authentication
4594 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004595 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004596 */
Damien Miller4af51302000-04-16 11:18:38 +10004597void
Damien Miller17e7ed02005-06-17 12:54:33 +10004598x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004599 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004600{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004601 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004602 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004603 char *new_data;
4604 int screen_number;
4605 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004606
Damien Millerf92c0792005-07-06 09:45:26 +10004607 if (x11_saved_display == NULL)
4608 x11_saved_display = xstrdup(disp);
4609 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004610 error("x11_request_forwarding_with_spoofing: different "
4611 "$DISPLAY already forwarded");
4612 return;
4613 }
4614
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004615 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004616 if (cp)
4617 cp = strchr(cp, '.');
4618 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004619 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004620 else
4621 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004622
Damien Miller13390022005-07-06 09:44:19 +10004623 if (x11_saved_proto == NULL) {
4624 /* Save protocol name. */
4625 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004626
4627 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004628 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004629 for (i = 0; i < data_len; i++) {
4630 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4631 fatal("x11_request_forwarding: bad "
4632 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004633 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004634 }
4635 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004636
4637 /* Generate fake data of the same length. */
4638 x11_fake_data = xmalloc(data_len);
4639 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004640 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004641 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004642
Damien Miller95def091999-11-25 00:26:21 +11004643 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004644 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004645
Damien Miller95def091999-11-25 00:26:21 +11004646 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10004647 if (compat20) {
Damien Miller6d7b4372011-06-23 08:31:57 +10004648 channel_request_start(client_session_id, "x11-req", want_reply);
Damien Millerbd483e72000-04-30 10:00:53 +10004649 packet_put_char(0); /* XXX bool single connection */
4650 } else {
4651 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
4652 }
4653 packet_put_cstring(proto);
4654 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004655 packet_put_int(screen_number);
4656 packet_send();
4657 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004658 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004659}
4660
Ben Lindstrome9c99912001-06-09 00:41:05 +00004661
4662/* -- agent forwarding */
4663
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004664/* Sends a message to the server to request authentication fd forwarding. */
4665
Damien Miller4af51302000-04-16 11:18:38 +10004666void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00004667auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004668{
Damien Miller95def091999-11-25 00:26:21 +11004669 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
4670 packet_send();
4671 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004672}