blob: d1976f41b1027ab7cd9f3c58eef53a69e00527d6 [file] [log] [blame]
djm@openbsd.org9145a732017-09-12 07:32:04 +00001/* $OpenBSD: channels.c,v 1.369 2017/09/12 07:32:04 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000058#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <netdb.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000060#include <stdarg.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#ifdef HAVE_STDINT_H
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000062 #include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110063#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100064#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100065#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110067#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Millerb84886b2008-05-19 15:05:07 +100070#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000074#include "ssherr.h"
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000075#include "sshbuf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "misc.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
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000086/* -- agent forwarding */
87#define NUM_SOCKS 10
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Ben Lindstrome9c99912001-06-09 00:41:05 +000089/* -- tcp forwarding */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000090/* special-case port number meaning allow any port */
91#define FWD_PERMIT_ANY_PORT 0
92
93/* special-case wildcard meaning allow any host */
94#define FWD_PERMIT_ANY_HOST "*"
95
96/* -- X11 forwarding */
97/* Maximum number of fake X11 displays to try. */
98#define MAX_DISPLAYS 1000
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Data structure for storing which hosts are permitted for forward requests.
102 * The local sides of any remote forwards are stored in this array to prevent
103 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
104 * network (which might be behind a firewall).
105 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000106/* XXX: streamlocal wants a path instead of host:port */
107/* Overload host_to_connect; we could just make this match Forward */
108/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100109typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000110 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000111 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000112 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000113 char *listen_path; /* Remote side should listen path. */
114 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000115 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116} ForwardPermission;
117
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000118typedef void chan_fn(struct ssh *, Channel *c,
119 fd_set *readset, fd_set *writeset);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000120
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000121/* Master structure for channels state */
122struct ssh_channels {
123 /*
124 * Pointer to an array containing all allocated channels. The array
125 * is dynamically extended as needed.
126 */
127 Channel **channels;
Damien Miller9b439df2006-07-24 14:04:00 +1000128
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000129 /*
130 * Size of the channel array. All slots of the array must always be
131 * initialized (at least the type field); unused slots set to NULL
132 */
133 u_int channels_alloc;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000135 /*
136 * Maximum file descriptor value used in any of the channels. This is
137 * updated in channel_new.
138 */
139 int channel_max_fd;
Damien Miller9b439df2006-07-24 14:04:00 +1000140
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000141 /*
142 * 'channel_pre*' are called just before select() to add any bits
143 * relevant to channels in the select bitmasks.
144 *
145 * 'channel_post*': perform any appropriate operations for
146 * channels which have events pending.
147 */
148 chan_fn **channel_pre;
149 chan_fn **channel_post;
Darren Tucker1338b9e2011-10-02 18:57:35 +1100150
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000151 /* -- tcp forwarding */
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000152
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000153 /* List of all permitted host/port pairs to connect by the user. */
154 ForwardPermission *permitted_opens;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000156 /* List of all permitted host/port pairs to connect by the admin. */
157 ForwardPermission *permitted_adm_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000158
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000159 /*
160 * Number of permitted host/port pairs in the array permitted by
161 * the user.
162 */
163 u_int num_permitted_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000164
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000165 /*
166 * Number of permitted host/port pair in the array permitted by
167 * the admin.
168 */
169 u_int num_adm_permitted_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000171 /*
172 * If this is true, all opens are permitted. This is the case on
173 * the server on which we have to trust the client anyway, and the
174 * user could do anything after logging in anyway.
175 */
176 int all_opens_permitted;
Damien Miller13390022005-07-06 09:44:19 +1000177
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000178 /* -- X11 forwarding */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000179
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000180 /* Saved X11 local (client) display. */
181 char *x11_saved_display;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000182
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000183 /* Saved X11 authentication protocol name. */
184 char *x11_saved_proto;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000185
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000186 /* Saved X11 authentication data. This is the real data. */
187 char *x11_saved_data;
188 u_int x11_saved_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000189
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000190 /* Deadline after which all X11 connections are refused */
191 u_int x11_refuse_time;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000192
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000193 /*
194 * Fake X11 authentication data. This is what the server will be
195 * sending us; we should replace any occurrences of this by the
196 * real data.
197 */
198 u_char *x11_fake_data;
199 u_int x11_fake_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000200
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000201 /* AF_UNSPEC or AF_INET or AF_INET6 */
202 int IPv4or6;
203};
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000204
Ben Lindstrome9c99912001-06-09 00:41:05 +0000205/* helper */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000206static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000207static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000208
Damien Millerbd740252008-05-19 15:37:09 +1000209/* non-blocking connect helpers */
210static int connect_next(struct channel_connect *);
211static void channel_connect_ctx_free(struct channel_connect *);
212
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000213/* Setup helper */
214static void channel_handler_init(struct ssh_channels *sc);
215
Ben Lindstrome9c99912001-06-09 00:41:05 +0000216/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000217
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000218void
219channel_init_channels(struct ssh *ssh)
220{
221 struct ssh_channels *sc;
222
223 if ((sc = calloc(1, sizeof(*sc))) == NULL ||
224 (sc->channel_pre = calloc(SSH_CHANNEL_MAX_TYPE,
225 sizeof(*sc->channel_pre))) == NULL ||
226 (sc->channel_post = calloc(SSH_CHANNEL_MAX_TYPE,
227 sizeof(*sc->channel_post))) == NULL)
228 fatal("%s: allocation failed", __func__);
229 sc->channels_alloc = 10;
230 sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels));
231 sc->IPv4or6 = AF_UNSPEC;
232 channel_handler_init(sc);
233
234 ssh->chanctxt = sc;
235}
236
Damien Millerb38eff82000-04-01 11:09:21 +1000237Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000238channel_by_id(struct ssh *ssh, int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000239{
240 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000241
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000242 if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
243 logit("%s: %d: bad id", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000244 return NULL;
245 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000246 c = ssh->chanctxt->channels[id];
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000247 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000248 logit("%s: %d: bad id: channel free", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000249 return NULL;
250 }
251 return c;
252}
253
markus@openbsd.org8d057842016-09-30 09:19:13 +0000254Channel *
djm@openbsd.org9f532292017-09-12 06:35:31 +0000255channel_by_remote_id(struct ssh *ssh, u_int remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000256{
257 Channel *c;
258 u_int i;
259
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000260 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
261 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000262 if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000263 return c;
264 }
265 return NULL;
266}
267
Damien Miller5428f641999-11-25 11:54:57 +1100268/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100269 * Returns the channel if it is allowed to receive protocol messages.
270 * Private channels, like listening sockets, may not receive messages.
271 */
272Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000273channel_lookup(struct ssh *ssh, int id)
Damien Millerd47c62a2005-12-13 19:33:57 +1100274{
275 Channel *c;
276
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000277 if ((c = channel_by_id(ssh, id)) == NULL)
278 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100279
Damien Millerd62f2ca2006-03-26 13:57:41 +1100280 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100281 case SSH_CHANNEL_X11_OPEN:
282 case SSH_CHANNEL_LARVAL:
283 case SSH_CHANNEL_CONNECTING:
284 case SSH_CHANNEL_DYNAMIC:
285 case SSH_CHANNEL_OPENING:
286 case SSH_CHANNEL_OPEN:
Damien Miller36187092013-06-10 13:07:11 +1000287 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000288 case SSH_CHANNEL_MUX_PROXY:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000289 return c;
Damien Millerd47c62a2005-12-13 19:33:57 +1100290 }
291 logit("Non-public channel %d, type %d.", id, c->type);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000292 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100293}
294
295/*
Damien Millere247cc42000-05-07 12:03:14 +1000296 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000297 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100298 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000300channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000301 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000303 struct ssh_channels *sc = ssh->chanctxt;
304
Damien Miller95def091999-11-25 00:26:21 +1100305 /* Update the maximum file descriptor value. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000306 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd);
307 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd);
308 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100309
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100310 if (rfd != -1)
311 fcntl(rfd, F_SETFD, FD_CLOEXEC);
312 if (wfd != -1 && wfd != rfd)
313 fcntl(wfd, F_SETFD, FD_CLOEXEC);
314 if (efd != -1 && efd != rfd && efd != wfd)
315 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000316
Damien Miller6f83b8e2000-05-02 09:23:45 +1000317 c->rfd = rfd;
318 c->wfd = wfd;
319 c->sock = (rfd == wfd) ? rfd : -1;
320 c->efd = efd;
321 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100322
Darren Tuckered3cdc02008-06-16 23:29:18 +1000323 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000324 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000325#ifdef _AIX
326 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000327 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000328#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100329
Damien Miller69b69aa2000-10-28 14:19:58 +1100330 /* enable nonblocking mode */
331 if (nonblock) {
332 if (rfd != -1)
333 set_nonblock(rfd);
334 if (wfd != -1)
335 set_nonblock(wfd);
336 if (efd != -1)
337 set_nonblock(efd);
338 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000339}
340
341/*
342 * Allocate a new channel object and set its type and socket. This will cause
343 * remote_name to be freed.
344 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000345Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000346channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000347 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000348{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000349 struct ssh_channels *sc = ssh->chanctxt;
350 u_int i, found;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000351 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Miller95def091999-11-25 00:26:21 +1100353 /* Try to find a free slot where to put the new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000354 for (i = 0; i < sc->channels_alloc; i++) {
355 if (sc->channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100356 /* Found a free slot. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000357 found = i;
Damien Miller95def091999-11-25 00:26:21 +1100358 break;
359 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000360 }
361 if (i >= sc->channels_alloc) {
362 /*
363 * There are no free slots. Take last+1 slot and expand
364 * the array.
365 */
366 found = sc->channels_alloc;
367 if (sc->channels_alloc > CHANNELS_MAX_CHANNELS)
368 fatal("%s: internal error: channels_alloc %d too big",
369 __func__, sc->channels_alloc);
370 sc->channels = xrecallocarray(sc->channels, sc->channels_alloc,
371 sc->channels_alloc + 10, sizeof(*sc->channels));
372 sc->channels_alloc += 10;
373 debug2("channel: expanding %d", sc->channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100374 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000375 /* Initialize and return new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000376 c = sc->channels[found] = xcalloc(1, sizeof(Channel));
377 if ((c->input = sshbuf_new()) == NULL ||
378 (c->output = sshbuf_new()) == NULL ||
379 (c->extended = sshbuf_new()) == NULL)
380 fatal("%s: sshbuf_new failed", __func__);
Damien Miller5144df92002-01-22 23:28:45 +1100381 c->ostate = CHAN_OUTPUT_OPEN;
382 c->istate = CHAN_INPUT_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000383 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Miller95def091999-11-25 00:26:21 +1100384 c->self = found;
385 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000386 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000387 c->local_window = window;
388 c->local_window_max = window;
Damien Millerbd483e72000-04-30 10:00:53 +1000389 c->local_maxpacket = maxpack;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000390 c->remote_name = xstrdup(remote_name);
Damien Millere1537f92010-01-26 13:26:22 +1100391 c->ctl_chan = -1;
Darren Tucker876045b2010-01-08 17:08:00 +1100392 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000393 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100394 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000395 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000397
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000398static void
399channel_find_maxfd(struct ssh_channels *sc)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000400{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000401 u_int i;
402 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000403 Channel *c;
404
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000405 for (i = 0; i < sc->channels_alloc; i++) {
406 c = sc->channels[i];
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000407 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000408 max = MAXIMUM(max, c->rfd);
409 max = MAXIMUM(max, c->wfd);
410 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000411 }
412 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000413 sc->channel_max_fd = max;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000414}
415
416int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000417channel_close_fd(struct ssh *ssh, int *fdp)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000418{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000419 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000420 int ret = 0, fd = *fdp;
421
422 if (fd != -1) {
423 ret = close(fd);
424 *fdp = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000425 if (fd == sc->channel_max_fd)
426 channel_find_maxfd(sc);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000427 }
428 return ret;
429}
430
Damien Miller6f83b8e2000-05-02 09:23:45 +1000431/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000433channel_close_fds(struct ssh *ssh, Channel *c)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000434{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000435 channel_close_fd(ssh, &c->sock);
436 channel_close_fd(ssh, &c->rfd);
437 channel_close_fd(ssh, &c->wfd);
438 channel_close_fd(ssh, &c->efd);
439}
440
441static void
442fwd_perm_clear(ForwardPermission *fp)
443{
444 free(fp->host_to_connect);
445 free(fp->listen_host);
446 free(fp->listen_path);
447 bzero(fp, sizeof(*fp));
448}
449
450enum { FWDPERM_USER, FWDPERM_ADMIN };
451
452static int
453fwd_perm_list_add(struct ssh *ssh, int which,
454 const char *host_to_connect, int port_to_connect,
455 const char *listen_host, const char *listen_path, int listen_port,
456 Channel *downstream)
457{
458 ForwardPermission **fpl;
459 u_int n, *nfpl;
460
461 switch (which) {
462 case FWDPERM_USER:
463 fpl = &ssh->chanctxt->permitted_opens;
464 nfpl = &ssh->chanctxt->num_permitted_opens;
465 break;
466 case FWDPERM_ADMIN:
467 fpl = &ssh->chanctxt->permitted_adm_opens;
468 nfpl = &ssh->chanctxt->num_adm_permitted_opens;
469 break;
470 default:
471 fatal("%s: invalid list %d", __func__, which);
472 }
473
474 if (*nfpl >= INT_MAX)
475 fatal("%s: overflow", __func__);
476
477 *fpl = xrecallocarray(*fpl, *nfpl, *nfpl + 1, sizeof(**fpl));
478 n = (*nfpl)++;
479#define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
480 (*fpl)[n].host_to_connect = MAYBE_DUP(host_to_connect);
481 (*fpl)[n].port_to_connect = port_to_connect;
482 (*fpl)[n].listen_host = MAYBE_DUP(listen_host);
483 (*fpl)[n].listen_path = MAYBE_DUP(listen_path);
484 (*fpl)[n].listen_port = listen_port;
485 (*fpl)[n].downstream = downstream;
486#undef MAYBE_DUP
487 return (int)n;
488}
489
490static void
491mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
492{
493 struct ssh_channels *sc = ssh->chanctxt;
494 ForwardPermission *fp;
495 int r;
496 u_int i;
497
498 for (i = 0; i < sc->num_permitted_opens; i++) {
499 fp = &sc->permitted_opens[i];
500 if (fp->downstream != c)
501 continue;
502
503 /* cancel on the server, since mux client is gone */
504 debug("channel %d: cleanup remote forward for %s:%u",
505 c->self, fp->listen_host, fp->listen_port);
506 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
507 (r = sshpkt_put_cstring(ssh,
508 "cancel-tcpip-forward")) != 0 ||
509 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
510 (r = sshpkt_put_cstring(ssh,
511 channel_rfwd_bind_host(fp->listen_host))) != 0 ||
512 (r = sshpkt_put_u32(ssh, fp->listen_port)) != 0 ||
513 (r = sshpkt_send(ssh)) != 0) {
514 fatal("%s: channel %i: %s", __func__,
515 c->self, ssh_err(r));
516 }
517 fwd_perm_clear(fp); /* unregister */
518 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000519}
520
521/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000522void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000523channel_free(struct ssh *ssh, Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000524{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000525 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000526 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000527 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000528 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000529 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000530
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000531 for (n = 0, i = 0; i < sc->channels_alloc; i++) {
532 if ((other = sc->channels[i]) == NULL)
533 continue;
534 n++;
535 /* detach from mux client and prepare for closing */
536 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
537 other->type == SSH_CHANNEL_MUX_PROXY &&
538 other->mux_ctx == c) {
539 other->mux_ctx = NULL;
540 other->type = SSH_CHANNEL_OPEN;
541 other->istate = CHAN_INPUT_CLOSED;
542 other->ostate = CHAN_OUTPUT_CLOSED;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000543 }
544 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000545 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000546 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000547
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000548 if (c->type == SSH_CHANNEL_MUX_CLIENT)
549 mux_remove_remote_forwardings(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000550
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000551 s = channel_open_message(ssh);
Damien Millerfbdeece2003-09-02 22:52:31 +1000552 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000554
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000555 channel_close_fds(ssh, c);
556 sshbuf_free(c->input);
557 sshbuf_free(c->output);
558 sshbuf_free(c->extended);
559 c->input = c->output = c->extended = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000560 free(c->remote_name);
561 c->remote_name = NULL;
562 free(c->path);
563 c->path = NULL;
564 free(c->listening_addr);
565 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000566 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
567 if (cc->abandon_cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000568 cc->abandon_cb(ssh, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +1000569 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100570 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000571 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000572 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000573 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000574 c->filter_cleanup(ssh, c->self, c->filter_ctx);
575 sc->channels[c->self] = NULL;
576 bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +1000577 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000578}
579
Ben Lindstrome9c99912001-06-09 00:41:05 +0000580void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000581channel_free_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000582{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000583 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000584
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000585 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
586 if (ssh->chanctxt->channels[i] != NULL)
587 channel_free(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000588}
589
590/*
591 * Closes the sockets/fds of all channels. This is used to close extra file
592 * descriptors after a fork.
593 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000594void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000595channel_close_all(struct ssh *ssh)
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
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000599 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
600 if (ssh->chanctxt->channels[i] != NULL)
601 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000602}
603
604/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000605 * Stop listening to channels.
606 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000607void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000608channel_stop_listening(struct ssh *ssh)
Ben Lindstrom809744e2001-07-04 05:26:06 +0000609{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000610 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000611 Channel *c;
612
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000613 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
614 c = ssh->chanctxt->channels[i];
Ben Lindstrom809744e2001-07-04 05:26:06 +0000615 if (c != NULL) {
616 switch (c->type) {
617 case SSH_CHANNEL_AUTH_SOCKET:
618 case SSH_CHANNEL_PORT_LISTENER:
619 case SSH_CHANNEL_RPORT_LISTENER:
620 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000621 case SSH_CHANNEL_UNIX_LISTENER:
622 case SSH_CHANNEL_RUNIX_LISTENER:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000623 channel_close_fd(ssh, &c->sock);
624 channel_free(ssh, c);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000625 break;
626 }
627 }
628 }
629}
630
631/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000632 * Returns true if no channel has too much buffered data, and false if one or
633 * more channel is overfull.
634 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000635int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000636channel_not_very_much_buffered_data(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000637{
638 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000639 u_int maxsize = ssh_packet_get_maxsize(ssh);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000640 Channel *c;
641
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000642 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
643 c = ssh->chanctxt->channels[i];
644 if (c == NULL || c->type != SSH_CHANNEL_OPEN)
645 continue;
646 if (sshbuf_len(c->output) > maxsize) {
647 debug2("channel %d: big output buffer %zu > %u",
648 c->self, sshbuf_len(c->output), maxsize);
649 return 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650 }
651 }
652 return 1;
653}
654
655/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000656int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000657channel_still_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000658{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000659 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000660 Channel *c;
661
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000662 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
663 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000664 if (c == NULL)
665 continue;
666 switch (c->type) {
667 case SSH_CHANNEL_X11_LISTENER:
668 case SSH_CHANNEL_PORT_LISTENER:
669 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100670 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000671 case SSH_CHANNEL_CLOSED:
672 case SSH_CHANNEL_AUTH_SOCKET:
673 case SSH_CHANNEL_DYNAMIC:
674 case SSH_CHANNEL_CONNECTING:
675 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000676 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000677 case SSH_CHANNEL_UNIX_LISTENER:
678 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000679 continue;
680 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000681 continue;
682 case SSH_CHANNEL_OPENING:
683 case SSH_CHANNEL_OPEN:
684 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100685 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000686 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000687 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000688 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000689 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 /* NOTREACHED */
691 }
692 }
693 return 0;
694}
695
696/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000697int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000698channel_find_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000699{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000700 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000701 Channel *c;
702
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000703 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
704 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000705 if (c == NULL || !c->have_remote_id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000706 continue;
707 switch (c->type) {
708 case SSH_CHANNEL_CLOSED:
709 case SSH_CHANNEL_DYNAMIC:
710 case SSH_CHANNEL_X11_LISTENER:
711 case SSH_CHANNEL_PORT_LISTENER:
712 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100713 case SSH_CHANNEL_MUX_LISTENER:
714 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000715 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000716 case SSH_CHANNEL_OPENING:
717 case SSH_CHANNEL_CONNECTING:
718 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000719 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000720 case SSH_CHANNEL_UNIX_LISTENER:
721 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000722 continue;
723 case SSH_CHANNEL_LARVAL:
724 case SSH_CHANNEL_AUTH_SOCKET:
725 case SSH_CHANNEL_OPEN:
726 case SSH_CHANNEL_X11_OPEN:
727 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000729 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000730 /* NOTREACHED */
731 }
732 }
733 return -1;
734}
735
Ben Lindstrome9c99912001-06-09 00:41:05 +0000736/*
737 * Returns a message describing the currently open forwarded connections,
738 * suitable for sending to the client. The message contains crlf pairs for
739 * newlines.
740 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000741char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000742channel_open_message(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000744 struct sshbuf *buf;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000745 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000746 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000747 int r;
748 char *ret;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000749
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000750 if ((buf = sshbuf_new()) == NULL)
751 fatal("%s: sshbuf_new", __func__);
752 if ((r = sshbuf_putf(buf,
753 "The following connections are open:\r\n")) != 0)
754 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
755 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
756 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757 if (c == NULL)
758 continue;
759 switch (c->type) {
760 case SSH_CHANNEL_X11_LISTENER:
761 case SSH_CHANNEL_PORT_LISTENER:
762 case SSH_CHANNEL_RPORT_LISTENER:
763 case SSH_CHANNEL_CLOSED:
764 case SSH_CHANNEL_AUTH_SOCKET:
765 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000766 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100767 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000768 case SSH_CHANNEL_UNIX_LISTENER:
769 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000770 continue;
771 case SSH_CHANNEL_LARVAL:
772 case SSH_CHANNEL_OPENING:
773 case SSH_CHANNEL_CONNECTING:
774 case SSH_CHANNEL_DYNAMIC:
775 case SSH_CHANNEL_OPEN:
776 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000777 case SSH_CHANNEL_MUX_PROXY:
778 case SSH_CHANNEL_MUX_CLIENT:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000779 if ((r = sshbuf_putf(buf, " #%d %.300s "
djm@openbsd.org9f532292017-09-12 06:35:31 +0000780 "(t%d %s%u i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000781 c->self, c->remote_name,
djm@openbsd.org9f532292017-09-12 06:35:31 +0000782 c->type,
783 c->have_remote_id ? "r" : "nr", c->remote_id,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000784 c->istate, sshbuf_len(c->input),
785 c->ostate, sshbuf_len(c->output),
786 c->rfd, c->wfd, c->ctl_chan)) != 0)
787 fatal("%s: sshbuf_putf: %s",
788 __func__, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000789 continue;
790 default:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000791 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 /* NOTREACHED */
793 }
794 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000795 if ((ret = sshbuf_dup_string(buf)) == NULL)
796 fatal("%s: sshbuf_dup_string", __func__);
797 sshbuf_free(buf);
798 return ret;
799}
800
801static void
802open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
803{
804 int r;
805
806 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
807 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
808 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
809 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
810 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
811 fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r));
812 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000813}
814
815void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000816channel_send_open(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000817{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000818 Channel *c = channel_lookup(ssh, id);
819 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000820
Ben Lindstrome9c99912001-06-09 00:41:05 +0000821 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000822 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000823 return;
824 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000825 debug2("channel %d: send open", id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000826 open_preamble(ssh, __func__, c, c->ctype);
827 if ((r = sshpkt_send(ssh)) != 0)
828 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000829}
830
831void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000832channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000833{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000834 Channel *c = channel_lookup(ssh, id);
835 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000836
Ben Lindstrome9c99912001-06-09 00:41:05 +0000837 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000838 logit("%s: %d: unknown channel id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000839 return;
840 }
djm@openbsd.org9f532292017-09-12 06:35:31 +0000841 if (!c->have_remote_id)
842 fatal(":%s: channel %d: no remote id", __func__, c->self);
843
Damien Miller0e220db2004-06-15 10:34:08 +1000844 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000845 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
846 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
847 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
848 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
849 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
850 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000851}
Damien Miller4f7becb2006-03-26 14:10:14 +1100852
Ben Lindstrome9c99912001-06-09 00:41:05 +0000853void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000854channel_register_status_confirm(struct ssh *ssh, int id,
855 channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
Damien Millerb84886b2008-05-19 15:05:07 +1000856{
857 struct channel_confirm *cc;
858 Channel *c;
859
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000860 if ((c = channel_lookup(ssh, id)) == NULL)
861 fatal("%s: %d: bad id", __func__, id);
Damien Millerb84886b2008-05-19 15:05:07 +1000862
Damien Miller6c81fee2013-11-08 12:19:55 +1100863 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000864 cc->cb = cb;
865 cc->abandon_cb = abandon_cb;
866 cc->ctx = ctx;
867 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
868}
869
870void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000871channel_register_open_confirm(struct ssh *ssh, int id,
872 channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000873{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000874 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000875
Ben Lindstrome9c99912001-06-09 00:41:05 +0000876 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000877 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000878 return;
879 }
Damien Millerb84886b2008-05-19 15:05:07 +1000880 c->open_confirm = fn;
881 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000882}
Damien Miller4f7becb2006-03-26 14:10:14 +1100883
Ben Lindstrome9c99912001-06-09 00:41:05 +0000884void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000885channel_register_cleanup(struct ssh *ssh, int id,
886 channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000887{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000888 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000889
Ben Lindstrome9c99912001-06-09 00:41:05 +0000890 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000891 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000892 return;
893 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000894 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100895 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000896}
Damien Miller4f7becb2006-03-26 14:10:14 +1100897
Ben Lindstrome9c99912001-06-09 00:41:05 +0000898void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000899channel_cancel_cleanup(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000900{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000901 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000902
Ben Lindstrome9c99912001-06-09 00:41:05 +0000903 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000904 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000905 return;
906 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000907 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100908 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000909}
Damien Miller4f7becb2006-03-26 14:10:14 +1100910
Ben Lindstrome9c99912001-06-09 00:41:05 +0000911void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000912channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000913 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000914{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000915 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000916
Ben Lindstrome9c99912001-06-09 00:41:05 +0000917 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000918 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000919 return;
920 }
Damien Miller077b2382005-12-31 16:22:32 +1100921 c->input_filter = ifn;
922 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000923 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000924 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000925}
926
927void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000928channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000929 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000930{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000931 Channel *c = channel_lookup(ssh, id);
932 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000933
Ben Lindstrome9c99912001-06-09 00:41:05 +0000934 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
935 fatal("channel_activate for non-larval channel %d.", id);
djm@openbsd.org9f532292017-09-12 06:35:31 +0000936 if (!c->have_remote_id)
937 fatal(":%s: channel %d: no remote id", __func__, c->self);
938
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000939 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000940 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100941 c->local_window = c->local_window_max = window_max;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000942
943 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
944 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
945 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
946 (r = sshpkt_send(ssh)) != 0)
947 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000948}
949
Ben Lindstrombba81212001-06-25 05:01:22 +0000950static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000951channel_pre_listener(struct ssh *ssh, Channel *c,
952 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000953{
954 FD_SET(c->sock, readset);
955}
956
Ben Lindstrombba81212001-06-25 05:01:22 +0000957static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000958channel_pre_connecting(struct ssh *ssh, Channel *c,
959 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000960{
961 debug3("channel %d: waiting for connection", c->self);
962 FD_SET(c->sock, writeset);
963}
964
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000966channel_pre_open(struct ssh *ssh, Channel *c,
967 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000968{
Damien Miller33b13562000-04-04 14:38:59 +1000969 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000970 c->remote_window > 0 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000971 sshbuf_len(c->input) < c->remote_window &&
972 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Miller33b13562000-04-04 14:38:59 +1000973 FD_SET(c->rfd, readset);
974 if (c->ostate == CHAN_OUTPUT_OPEN ||
975 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000976 if (sshbuf_len(c->output) > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000977 FD_SET(c->wfd, writeset);
978 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000979 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000980 debug2("channel %d: "
981 "obuf_empty delayed efd %d/(%zu)", c->self,
982 c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000983 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000984 chan_obuf_empty(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +1000985 }
986 }
987 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000988 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
989 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000990 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000991 sshbuf_len(c->extended) > 0)
Damien Miller33b13562000-04-04 14:38:59 +1000992 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000993 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
994 (c->extended_usage == CHAN_EXTENDED_READ ||
995 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000996 sshbuf_len(c->extended) < c->remote_window)
Damien Miller33b13562000-04-04 14:38:59 +1000997 FD_SET(c->efd, readset);
998 }
Damien Miller0e220db2004-06-15 10:34:08 +1000999 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +10001000}
1001
Damien Millerb38eff82000-04-01 11:09:21 +10001002/*
1003 * This is a special state for X11 authentication spoofing. An opened X11
1004 * connection (when authentication spoofing is being done) remains in this
1005 * state until the first packet has been completely read. The authentication
1006 * data in that packet is then substituted by the real data if it matches the
1007 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +10001008 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +00001009 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +10001010 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001011static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001012x11_open_helper(struct ssh *ssh, struct sshbuf *b)
Damien Millerb38eff82000-04-01 11:09:21 +10001013{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001014 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001015 u_char *ucp;
1016 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001017
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001018 /* Is this being called after the refusal deadline? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001019 if (sc->x11_refuse_time != 0 &&
1020 (u_int)monotime() >= sc->x11_refuse_time) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001021 verbose("Rejected X11 connection after ForwardX11Timeout "
1022 "expired");
1023 return -1;
1024 }
1025
Damien Millerb38eff82000-04-01 11:09:21 +10001026 /* Check if the fixed size part of the packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001027 if (sshbuf_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +10001028 return 0;
1029
1030 /* Parse the lengths of variable-length fields. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001031 ucp = sshbuf_mutable_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +10001032 if (ucp[0] == 0x42) { /* Byte order MSB first. */
1033 proto_len = 256 * ucp[6] + ucp[7];
1034 data_len = 256 * ucp[8] + ucp[9];
1035 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1036 proto_len = ucp[6] + 256 * ucp[7];
1037 data_len = ucp[8] + 256 * ucp[9];
1038 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001039 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001040 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001041 return -1;
1042 }
1043
1044 /* Check if the whole packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001045 if (sshbuf_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001046 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1047 return 0;
1048
1049 /* Check if authentication protocol matches. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001050 if (proto_len != strlen(sc->x11_saved_proto) ||
1051 memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001052 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001053 return -1;
1054 }
1055 /* Check if authentication data matches our fake data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001056 if (data_len != sc->x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001057 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001058 sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001059 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001060 return -1;
1061 }
1062 /* Check fake data length */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001063 if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
Damien Millerb38eff82000-04-01 11:09:21 +10001064 error("X11 fake_data_len %d != saved_data_len %d",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001065 sc->x11_fake_data_len, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001066 return -1;
1067 }
1068 /*
1069 * Received authentication protocol and data match
1070 * our fake data. Substitute the fake data with real
1071 * data.
1072 */
1073 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001074 sc->x11_saved_data, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001075 return 1;
1076}
1077
Ben Lindstrombba81212001-06-25 05:01:22 +00001078static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001079channel_pre_x11_open(struct ssh *ssh, Channel *c,
1080 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001081{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001082 int ret = x11_open_helper(ssh, c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001083
1084 /* c->force_drain = 1; */
1085
Damien Millerb38eff82000-04-01 11:09:21 +10001086 if (ret == 1) {
1087 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001088 channel_pre_open(ssh, c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001089 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001090 logit("X11 connection rejected because of wrong authentication.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001091 debug2("X11 rejected %d i%d/o%d",
1092 c->self, c->istate, c->ostate);
1093 chan_read_failed(ssh, c);
1094 sshbuf_reset(c->input);
1095 chan_ibuf_empty(ssh, c);
1096 sshbuf_reset(c->output);
1097 chan_write_failed(ssh, c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001098 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001099 }
1100}
1101
Damien Millere1537f92010-01-26 13:26:22 +11001102static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001103channel_pre_mux_client(struct ssh *ssh,
1104 Channel *c, fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11001105{
Damien Millerd530f5f2010-05-21 14:57:10 +10001106 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001107 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Millere1537f92010-01-26 13:26:22 +11001108 FD_SET(c->rfd, readset);
1109 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1110 /* clear buffer immediately (discard any partial packet) */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001111 sshbuf_reset(c->input);
1112 chan_ibuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001113 /* Start output drain. XXX just kill chan? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001114 chan_rcvd_oclose(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001115 }
1116 if (c->ostate == CHAN_OUTPUT_OPEN ||
1117 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001118 if (sshbuf_len(c->output) > 0)
Damien Millere1537f92010-01-26 13:26:22 +11001119 FD_SET(c->wfd, writeset);
1120 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001121 chan_obuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001122 }
1123}
1124
Ben Lindstromb3921512001-04-11 15:57:50 +00001125/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +00001126static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001127channel_decode_socks4(struct ssh *ssh, Channel *c,
1128 fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001129{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001130 const u_char *p;
1131 char *host;
Damien Miller1781f532009-01-28 16:24:41 +11001132 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001133 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001134 struct {
1135 u_int8_t version;
1136 u_int8_t command;
1137 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001138 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001139 } s4_req, s4_rsp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001140 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001141
Ben Lindstromb3921512001-04-11 15:57:50 +00001142 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001143
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001144 have = sshbuf_len(c->input);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001145 len = sizeof(s4_req);
1146 if (have < len)
1147 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001148 p = sshbuf_ptr(c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001149
1150 need = 1;
1151 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1152 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1153 debug2("channel %d: socks4a request", c->self);
1154 /* ... and needs an extra string (the hostname) */
1155 need = 2;
1156 }
1157 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001158 for (found = 0, i = len; i < have; i++) {
1159 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001160 found++;
1161 if (found == need)
1162 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001163 }
1164 if (i > 1024) {
1165 /* the peer is probably sending garbage */
1166 debug("channel %d: decode socks4: too long",
1167 c->self);
1168 return -1;
1169 }
1170 }
Damien Miller1781f532009-01-28 16:24:41 +11001171 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001172 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001173 if ((r = sshbuf_get(c->input, &s4_req.version, 1)) != 0 ||
1174 (r = sshbuf_get(c->input, &s4_req.command, 1)) != 0 ||
1175 (r = sshbuf_get(c->input, &s4_req.dest_port, 2)) != 0 ||
1176 (r = sshbuf_get(c->input, &s4_req.dest_addr, 4)) != 0) {
1177 debug("channels %d: decode socks4: %s", c->self, ssh_err(r));
1178 return -1;
1179 }
1180 have = sshbuf_len(c->input);
1181 p = sshbuf_ptr(c->input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001182 if (memchr(p, '\0', have) == NULL) {
1183 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001184 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001185 return -1;
1186 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001187 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001188 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001189 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001190 strlcpy(username, p, sizeof(username));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001191 if ((r = sshbuf_consume(c->input, len)) != 0) {
1192 fatal("%s: channel %d: consume: %s", __func__,
1193 c->self, ssh_err(r));
1194 }
Darren Tuckera627d422013-06-02 07:31:17 +10001195 free(c->path);
1196 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001197 if (need == 1) { /* SOCKS4: one string */
1198 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001199 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001200 } else { /* SOCKS4A: two strings */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001201 have = sshbuf_len(c->input);
1202 p = sshbuf_ptr(c->input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001203 if (memchr(p, '\0', have) == NULL) {
1204 error("channel %d: decode socks4a: host not nul "
1205 "terminated", c->self);
1206 return -1;
1207 }
Damien Miller1781f532009-01-28 16:24:41 +11001208 len = strlen(p);
1209 debug2("channel %d: decode socks4a: host %s/%d",
1210 c->self, p, len);
1211 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001212 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001213 error("channel %d: hostname \"%.100s\" too long",
1214 c->self, p);
1215 return -1;
1216 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001217 c->path = xstrdup(p);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001218 if ((r = sshbuf_consume(c->input, len)) != 0) {
1219 fatal("%s: channel %d: consume: %s", __func__,
1220 c->self, ssh_err(r));
1221 }
Damien Miller1781f532009-01-28 16:24:41 +11001222 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001223 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001224
Damien Millerfbdeece2003-09-02 22:52:31 +10001225 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001226 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001227
Ben Lindstromb3921512001-04-11 15:57:50 +00001228 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001229 debug("channel %d: cannot handle: %s cn %d",
1230 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001231 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001232 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001233 s4_rsp.version = 0; /* vn: 0 for reply */
1234 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001235 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001236 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001237 if ((r = sshbuf_put(c->output, &s4_rsp, sizeof(s4_rsp))) != 0) {
1238 fatal("%s: channel %d: append reply: %s", __func__,
1239 c->self, ssh_err(r));
1240 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001241 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001242}
1243
Darren Tucker46471c92003-07-03 13:55:19 +10001244/* try to decode a socks5 header */
1245#define SSH_SOCKS5_AUTHDONE 0x1000
1246#define SSH_SOCKS5_NOAUTH 0x00
1247#define SSH_SOCKS5_IPV4 0x01
1248#define SSH_SOCKS5_DOMAIN 0x03
1249#define SSH_SOCKS5_IPV6 0x04
1250#define SSH_SOCKS5_CONNECT 0x01
1251#define SSH_SOCKS5_SUCCESS 0x00
1252
1253static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001254channel_decode_socks5(struct ssh *ssh, Channel *c,
1255 fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001256{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001257 /* XXX use get/put_u8 instead of trusting struct padding */
Darren Tucker46471c92003-07-03 13:55:19 +10001258 struct {
1259 u_int8_t version;
1260 u_int8_t command;
1261 u_int8_t reserved;
1262 u_int8_t atyp;
1263 } s5_req, s5_rsp;
1264 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001265 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001266 const u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001267 u_int have, need, i, found, nmethods, addrlen, af;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001268 int r;
Darren Tucker46471c92003-07-03 13:55:19 +10001269
1270 debug2("channel %d: decode socks5", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001271 p = sshbuf_ptr(c->input);
Darren Tucker46471c92003-07-03 13:55:19 +10001272 if (p[0] != 0x05)
1273 return -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001274 have = sshbuf_len(c->input);
Darren Tucker46471c92003-07-03 13:55:19 +10001275 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1276 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001277 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001278 return 0;
1279 nmethods = p[1];
1280 if (have < nmethods + 2)
1281 return 0;
1282 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001283 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001284 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001285 found = 1;
1286 break;
1287 }
1288 }
1289 if (!found) {
1290 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1291 c->self);
1292 return -1;
1293 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001294 if ((r = sshbuf_consume(c->input, nmethods + 2)) != 0) {
1295 fatal("%s: channel %d: consume: %s", __func__,
1296 c->self, ssh_err(r));
1297 }
1298 /* version, method */
1299 if ((r = sshbuf_put_u8(c->output, 0x05)) != 0 ||
1300 (r = sshbuf_put_u8(c->output, SSH_SOCKS5_NOAUTH)) != 0) {
1301 fatal("%s: channel %d: append reply: %s", __func__,
1302 c->self, ssh_err(r));
1303 }
Darren Tucker46471c92003-07-03 13:55:19 +10001304 FD_SET(c->sock, writeset);
1305 c->flags |= SSH_SOCKS5_AUTHDONE;
1306 debug2("channel %d: socks5 auth done", c->self);
1307 return 0; /* need more */
1308 }
1309 debug2("channel %d: socks5 post auth", c->self);
1310 if (have < sizeof(s5_req)+1)
1311 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001312 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001313 if (s5_req.version != 0x05 ||
1314 s5_req.command != SSH_SOCKS5_CONNECT ||
1315 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001316 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001317 return -1;
1318 }
Darren Tucker47eede72005-03-14 23:08:12 +11001319 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001320 case SSH_SOCKS5_IPV4:
1321 addrlen = 4;
1322 af = AF_INET;
1323 break;
1324 case SSH_SOCKS5_DOMAIN:
1325 addrlen = p[sizeof(s5_req)];
1326 af = -1;
1327 break;
1328 case SSH_SOCKS5_IPV6:
1329 addrlen = 16;
1330 af = AF_INET6;
1331 break;
1332 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001333 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001334 return -1;
1335 }
Damien Miller0f077072006-07-10 22:21:02 +10001336 need = sizeof(s5_req) + addrlen + 2;
1337 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1338 need++;
1339 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001340 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001341 if ((r = sshbuf_consume(c->input, sizeof(s5_req))) != 0) {
1342 fatal("%s: channel %d: consume: %s", __func__,
1343 c->self, ssh_err(r));
1344 }
1345 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1346 /* host string length */
1347 if ((r = sshbuf_consume(c->input, 1)) != 0) {
1348 fatal("%s: channel %d: consume: %s", __func__,
1349 c->self, ssh_err(r));
1350 }
1351 }
1352 if ((r = sshbuf_get(c->input, &dest_addr, addrlen)) != 0 ||
1353 (r = sshbuf_get(c->input, &dest_port, 2)) != 0) {
1354 debug("channel %d: parse addr/port: %s", c->self, ssh_err(r));
1355 return -1;
1356 }
Darren Tucker46471c92003-07-03 13:55:19 +10001357 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001358 free(c->path);
1359 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001360 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001361 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001362 error("channel %d: dynamic request: socks5 hostname "
1363 "\"%.100s\" too long", c->self, dest_addr);
1364 return -1;
1365 }
1366 c->path = xstrdup(dest_addr);
1367 } else {
1368 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1369 return -1;
1370 c->path = xstrdup(ntop);
1371 }
Darren Tucker46471c92003-07-03 13:55:19 +10001372 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001373
Damien Millerfbdeece2003-09-02 22:52:31 +10001374 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001375 c->self, c->path, c->host_port, s5_req.command);
1376
1377 s5_rsp.version = 0x05;
1378 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1379 s5_rsp.reserved = 0; /* ignored */
1380 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001381 dest_port = 0; /* ignored */
1382
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001383 if ((r = sshbuf_put(c->output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1384 (r = sshbuf_put_u32(c->output, ntohl(INADDR_ANY))) != 0 ||
1385 (r = sshbuf_put(c->output, &dest_port, sizeof(dest_port))) != 0)
1386 fatal("%s: channel %d: append reply: %s", __func__,
1387 c->self, ssh_err(r));
Darren Tucker46471c92003-07-03 13:55:19 +10001388 return 1;
1389}
1390
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001391Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001392channel_connect_stdio_fwd(struct ssh *ssh,
1393 const char *host_to_connect, u_short port_to_connect, int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001394{
1395 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001396
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001397 debug("%s %s:%d", __func__, host_to_connect, port_to_connect);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001398
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001399 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001400 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1401 0, "stdio-forward", /*nonblock*/0);
1402
1403 c->path = xstrdup(host_to_connect);
1404 c->host_port = port_to_connect;
1405 c->listening_port = 0;
1406 c->force_drain = 1;
1407
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001408 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1409 port_open_helper(ssh, c, "direct-tcpip");
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001410
1411 return c;
1412}
1413
Ben Lindstromb3921512001-04-11 15:57:50 +00001414/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001415static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001416channel_pre_dynamic(struct ssh *ssh, Channel *c,
1417 fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001418{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001419 const u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001420 u_int have;
1421 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001422
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001423 have = sshbuf_len(c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001424 debug2("channel %d: pre_dynamic: have %d", c->self, have);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001425 /* sshbuf_dump(c->input, stderr); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001426 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001427 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001428 /* need more */
1429 FD_SET(c->sock, readset);
1430 return;
1431 }
1432 /* try to guess the protocol */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001433 p = sshbuf_ptr(c->input);
1434 /* XXX sshbuf_peek_u8? */
Ben Lindstromb3921512001-04-11 15:57:50 +00001435 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001436 case 0x04:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001437 ret = channel_decode_socks4(ssh, c, readset, writeset);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001438 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001439 case 0x05:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001440 ret = channel_decode_socks5(ssh, c, readset, writeset);
Darren Tucker46471c92003-07-03 13:55:19 +10001441 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001442 default:
1443 ret = -1;
1444 break;
1445 }
1446 if (ret < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001447 chan_mark_dead(ssh, c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001448 } else if (ret == 0) {
1449 debug2("channel %d: pre_dynamic: need more", c->self);
1450 /* need more */
1451 FD_SET(c->sock, readset);
1452 } else {
1453 /* switch to the next state */
1454 c->type = SSH_CHANNEL_OPENING;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001455 port_open_helper(ssh, c, "direct-tcpip");
Ben Lindstromb3921512001-04-11 15:57:50 +00001456 }
1457}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001458
Damien Millerb38eff82000-04-01 11:09:21 +10001459/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001460static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001461channel_post_x11_listener(struct ssh *ssh, Channel *c,
1462 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001463{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001464 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001465 struct sockaddr_storage addr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001466 int r, newsock, oerrno, remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001467 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001468 char buf[16384], *remote_ipaddr;
Damien Millerb38eff82000-04-01 11:09:21 +10001469
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001470 if (!FD_ISSET(c->sock, readset))
1471 return;
Damien Millerbd483e72000-04-30 10:00:53 +10001472
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001473 debug("X11 connection requested.");
1474 addrlen = sizeof(addr);
1475 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1476 if (c->single_connection) {
1477 oerrno = errno;
1478 debug2("single_connection: closing X11 listener.");
1479 channel_close_fd(ssh, &c->sock);
1480 chan_mark_dead(ssh, c);
1481 errno = oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001482 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001483 if (newsock < 0) {
1484 if (errno != EINTR && errno != EWOULDBLOCK &&
1485 errno != ECONNABORTED)
1486 error("accept: %.100s", strerror(errno));
1487 if (errno == EMFILE || errno == ENFILE)
1488 c->notbefore = monotime() + 1;
1489 return;
1490 }
1491 set_nodelay(newsock);
1492 remote_ipaddr = get_peer_ipaddr(newsock);
1493 remote_port = get_peer_port(newsock);
1494 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1495 remote_ipaddr, remote_port);
1496
1497 nc = channel_new(ssh, "accepted x11 socket",
1498 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1499 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1500 open_preamble(ssh, __func__, nc, "x11");
1501 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0) {
1502 fatal("%s: channel %i: reply %s", __func__,
1503 c->self, ssh_err(r));
1504 }
1505 if ((datafellows & SSH_BUG_X11FWD) != 0)
1506 debug2("channel %d: ssh2 x11 bug compat mode", nc->self);
1507 else if ((r = sshpkt_put_u32(ssh, remote_port)) != 0) {
1508 fatal("%s: channel %i: reply %s", __func__,
1509 c->self, ssh_err(r));
1510 }
1511 if ((r = sshpkt_send(ssh)) != 0)
1512 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
1513 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001514}
1515
Ben Lindstrombba81212001-06-25 05:01:22 +00001516static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001517port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001518{
Damien Miller4def1842013-12-29 17:45:26 +11001519 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001520 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001521 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001522 int remote_port = get_peer_port(c->sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001523 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001524
Damien Millerd6369432010-02-02 17:02:07 +11001525 if (remote_port == -1) {
1526 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001527 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001528 remote_ipaddr = xstrdup("127.0.0.1");
1529 remote_port = 65535;
1530 }
1531
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001532 free(c->remote_name);
1533 xasprintf(&c->remote_name,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001534 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001535 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001536 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001537 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001538
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001539 open_preamble(ssh, __func__, c, rtype);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001540 if (strcmp(rtype, "direct-tcpip") == 0) {
1541 /* target host, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001542 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1543 (r = sshpkt_put_u32(ssh, c->host_port)) != 0) {
1544 fatal("%s: channel %i: reply %s", __func__,
1545 c->self, ssh_err(r));
1546 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001547 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1548 /* target path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001549 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1550 fatal("%s: channel %i: reply %s", __func__,
1551 c->self, ssh_err(r));
1552 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001553 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1554 /* listen path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001555 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1556 fatal("%s: channel %i: reply %s", __func__,
1557 c->self, ssh_err(r));
1558 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001559 } else {
1560 /* listen address, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001561 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1562 (r = sshpkt_put_u32(ssh, local_port)) != 0) {
1563 fatal("%s: channel %i: reply %s", __func__,
1564 c->self, ssh_err(r));
1565 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001566 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001567 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1568 /* reserved for future owner/mode info */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001569 if ((r = sshpkt_put_cstring(ssh, "")) != 0) {
1570 fatal("%s: channel %i: reply %s", __func__,
1571 c->self, ssh_err(r));
1572 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001573 } else {
1574 /* originator host and port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001575 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1576 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) {
1577 fatal("%s: channel %i: reply %s", __func__,
1578 c->self, ssh_err(r));
1579 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001580 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001581 if ((r = sshpkt_send(ssh)) != 0)
1582 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001583 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001584 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001585}
1586
Damien Miller5e7fd072005-11-05 14:53:39 +11001587static void
1588channel_set_reuseaddr(int fd)
1589{
1590 int on = 1;
1591
1592 /*
1593 * Set socket options.
1594 * Allow local port reuse in TIME_WAIT.
1595 */
1596 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1597 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1598}
1599
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001600void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001601channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001602{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001603 ssh->chanctxt->x11_refuse_time = refuse_time;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001604}
1605
Damien Millerb38eff82000-04-01 11:09:21 +10001606/*
1607 * This socket is listening for connections to a forwarded TCP/IP port.
1608 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001609static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001610channel_post_port_listener(struct ssh *ssh, Channel *c,
1611 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001612{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001613 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001614 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001615 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001616 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001617 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001618
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001619 if (!FD_ISSET(c->sock, readset))
1620 return;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001621
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001622 debug("Connection to port %d forwarding to %.100s port %d requested.",
1623 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001624
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001625 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1626 nextstate = SSH_CHANNEL_OPENING;
1627 rtype = "forwarded-tcpip";
1628 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1629 nextstate = SSH_CHANNEL_OPENING;
1630 rtype = "forwarded-streamlocal@openssh.com";
1631 } else if (c->host_port == PORT_STREAMLOCAL) {
1632 nextstate = SSH_CHANNEL_OPENING;
1633 rtype = "direct-streamlocal@openssh.com";
1634 } else if (c->host_port == 0) {
1635 nextstate = SSH_CHANNEL_DYNAMIC;
1636 rtype = "dynamic-tcpip";
1637 } else {
1638 nextstate = SSH_CHANNEL_OPENING;
1639 rtype = "direct-tcpip";
Damien Millerb38eff82000-04-01 11:09:21 +10001640 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001641
1642 addrlen = sizeof(addr);
1643 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1644 if (newsock < 0) {
1645 if (errno != EINTR && errno != EWOULDBLOCK &&
1646 errno != ECONNABORTED)
1647 error("accept: %.100s", strerror(errno));
1648 if (errno == EMFILE || errno == ENFILE)
1649 c->notbefore = monotime() + 1;
1650 return;
1651 }
1652 if (c->host_port != PORT_STREAMLOCAL)
1653 set_nodelay(newsock);
1654 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1655 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1656 nc->listening_port = c->listening_port;
1657 nc->host_port = c->host_port;
1658 if (c->path != NULL)
1659 nc->path = xstrdup(c->path);
1660
1661 if (nextstate != SSH_CHANNEL_DYNAMIC)
1662 port_open_helper(ssh, nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001663}
1664
1665/*
1666 * This is the authentication agent socket listening for connections from
1667 * clients.
1668 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001669static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001670channel_post_auth_listener(struct ssh *ssh, Channel *c,
1671 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001672{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001673 Channel *nc;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001674 int r, newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001675 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001676 socklen_t addrlen;
1677
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001678 if (!FD_ISSET(c->sock, readset))
1679 return;
1680
1681 addrlen = sizeof(addr);
1682 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1683 if (newsock < 0) {
1684 error("accept from auth socket: %.100s", strerror(errno));
1685 if (errno == EMFILE || errno == ENFILE)
1686 c->notbefore = monotime() + 1;
1687 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001688 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001689 nc = channel_new(ssh, "accepted auth socket",
1690 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1691 c->local_window_max, c->local_maxpacket,
1692 0, "accepted auth socket", 1);
1693 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1694 if ((r = sshpkt_send(ssh)) != 0)
1695 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001696}
1697
Ben Lindstrombba81212001-06-25 05:01:22 +00001698static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001699channel_post_connecting(struct ssh *ssh, Channel *c,
1700 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001701{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001702 int err = 0, sock, r;
Ben Lindstrom11180952001-07-04 05:13:35 +00001703 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001704
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001705 if (!FD_ISSET(c->sock, writeset))
1706 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001707 if (!c->have_remote_id)
1708 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001709
1710 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1711 err = errno;
1712 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001713 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001714 if (err == 0) {
1715 debug("channel %d: connected to %s port %d",
1716 c->self, c->connect_ctx.host, c->connect_ctx.port);
1717 channel_connect_ctx_free(&c->connect_ctx);
1718 c->type = SSH_CHANNEL_OPEN;
1719 if ((r = sshpkt_start(ssh,
1720 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1721 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1722 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1723 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1724 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
1725 fatal("%s: channel %i: confirm: %s", __func__,
1726 c->self, ssh_err(r));
1727 }
1728 } else {
1729 debug("channel %d: connection failed: %s",
1730 c->self, strerror(err));
1731 /* Try next address, if any */
1732 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1733 close(c->sock);
1734 c->sock = c->rfd = c->wfd = sock;
1735 channel_find_maxfd(ssh->chanctxt);
1736 return;
1737 }
1738 /* Exhausted all addresses */
1739 error("connect_to %.100s port %d: failed.",
1740 c->connect_ctx.host, c->connect_ctx.port);
1741 channel_connect_ctx_free(&c->connect_ctx);
1742 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1743 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1744 (r = sshpkt_put_u32(ssh, SSH2_OPEN_CONNECT_FAILED)) != 0) {
1745 fatal("%s: channel %i: failure: %s", __func__,
1746 c->self, ssh_err(r));
1747 }
1748 if ((datafellows & SSH_BUG_OPENFAILURE) == 0 &&
1749 ((r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1750 (r = sshpkt_put_cstring(ssh, "")) != 0)) {
1751 fatal("%s: channel %i: failure: %s", __func__,
1752 c->self, ssh_err(r));
1753 }
1754 chan_mark_dead(ssh, c);
1755 }
1756 if ((r = sshpkt_send(ssh)) != 0)
1757 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001758}
1759
Ben Lindstrombba81212001-06-25 05:01:22 +00001760static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001761channel_handle_rfd(struct ssh *ssh, Channel *c,
1762 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001763{
Darren Tucker11327cc2005-03-14 23:22:25 +11001764 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001765 ssize_t len;
1766 int r, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001767
Damien Miller835284b2007-06-11 13:03:16 +10001768 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001769
1770 if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset)))
1771 return 1;
1772
1773 errno = 0;
1774 len = read(c->rfd, buf, sizeof(buf));
1775 if (len < 0 && (errno == EINTR ||
1776 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1777 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001778#ifndef PTY_ZEROREAD
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001779 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001780#else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001781 if ((!c->isatty && len <= 0) ||
1782 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001783#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001784 debug2("channel %d: read<=0 rfd %d len %zd",
1785 c->self, c->rfd, len);
1786 if (c->type != SSH_CHANNEL_OPEN) {
1787 debug2("channel %d: not open", c->self);
1788 chan_mark_dead(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001789 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001790 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001791 chan_read_failed(ssh, c);
Damien Millerad833b32000-08-23 10:46:23 +10001792 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001793 return -1;
1794 }
1795 if (c->input_filter != NULL) {
1796 if (c->input_filter(ssh, c, buf, len) == -1) {
1797 debug2("channel %d: filter stops", c->self);
1798 chan_read_failed(ssh, c);
1799 }
1800 } else if (c->datagram) {
1801 if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1802 fatal("%s: channel %d: put datagram: %s", __func__,
1803 c->self, ssh_err(r));
1804 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
1805 fatal("%s: channel %d: put data: %s", __func__,
1806 c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001807 }
1808 return 1;
1809}
Damien Miller4f7becb2006-03-26 14:10:14 +11001810
Ben Lindstrombba81212001-06-25 05:01:22 +00001811static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001812channel_handle_wfd(struct ssh *ssh, Channel *c,
1813 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001814{
Ben Lindstrome229b252001-03-05 06:28:06 +00001815 struct termios tio;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001816 u_char *data = NULL, *buf; /* XXX const; need filter API change */
1817 size_t dlen, olen = 0;
1818 int r, len;
1819
1820 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
1821 sshbuf_len(c->output) == 0)
1822 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001823
1824 /* Send buffered output data to the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001825 olen = sshbuf_len(c->output);
1826 if (c->output_filter != NULL) {
1827 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
1828 debug2("channel %d: filter stops", c->self);
1829 if (c->type != SSH_CHANNEL_OPEN)
1830 chan_mark_dead(ssh, c);
1831 else
1832 chan_write_failed(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001833 return -1;
1834 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001835 } else if (c->datagram) {
1836 if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
1837 fatal("%s: channel %d: get datagram: %s", __func__,
1838 c->self, ssh_err(r));
djm@openbsd.org9145a732017-09-12 07:32:04 +00001839 buf = data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001840 } else {
1841 buf = data = sshbuf_mutable_ptr(c->output);
1842 dlen = sshbuf_len(c->output);
1843 }
1844
1845 if (c->datagram) {
1846 /* ignore truncated writes, datagrams might get lost */
1847 len = write(c->wfd, data, dlen);
1848 free(data);
1849 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1850 errno == EWOULDBLOCK))
1851 return 1;
1852 if (len <= 0)
1853 goto write_fail;
1854 goto out;
1855 }
1856
1857#ifdef _AIX
1858 /* XXX: Later AIX versions can't push as much data to tty */
1859 if (c->wfd_isatty)
1860 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10001861#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001862
1863 len = write(c->wfd, buf, dlen);
1864 if (len < 0 &&
1865 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
1866 return 1;
1867 if (len <= 0) {
1868 write_fail:
1869 if (c->type != SSH_CHANNEL_OPEN) {
1870 debug2("channel %d: not open", c->self);
1871 chan_mark_dead(ssh, c);
1872 return -1;
1873 } else {
1874 chan_write_failed(ssh, c);
1875 }
1876 return -1;
1877 }
1878#ifndef BROKEN_TCGETATTR_ICANON
1879 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
1880 if (tcgetattr(c->wfd, &tio) == 0 &&
1881 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1882 /*
1883 * Simulate echo to reduce the impact of
1884 * traffic analysis. We need to match the
1885 * size of a SSH2_MSG_CHANNEL_DATA message
1886 * (4 byte channel id + buf)
1887 */
1888 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
1889 (r = sshpkt_send(ssh)) != 0)
1890 fatal("%s: channel %d: ignore: %s",
1891 __func__, c->self, ssh_err(r));
1892 }
1893 }
1894#endif /* BROKEN_TCGETATTR_ICANON */
1895 if ((r = sshbuf_consume(c->output, len)) != 0) {
1896 fatal("%s: channel %d: consume: %s",
1897 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001898 }
Damien Miller7d457182010-08-05 23:09:48 +10001899 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001900 c->local_consumed += olen - sshbuf_len(c->output);
1901
Damien Miller33b13562000-04-04 14:38:59 +10001902 return 1;
1903}
Damien Miller4f7becb2006-03-26 14:10:14 +11001904
Ben Lindstrombba81212001-06-25 05:01:22 +00001905static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001906channel_handle_efd_write(struct ssh *ssh, Channel *c,
1907 fd_set *readset, fd_set *writeset)
1908{
1909 int r;
1910 ssize_t len;
1911
1912 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
1913 return 1;
1914
1915 len = write(c->efd, sshbuf_ptr(c->extended),
1916 sshbuf_len(c->extended));
1917 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
1918 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1919 errno == EWOULDBLOCK))
1920 return 1;
1921 if (len <= 0) {
1922 debug2("channel %d: closing write-efd %d", c->self, c->efd);
1923 channel_close_fd(ssh, &c->efd);
1924 } else {
1925 if ((r = sshbuf_consume(c->extended, len)) != 0) {
1926 fatal("%s: channel %d: consume: %s",
1927 __func__, c->self, ssh_err(r));
1928 }
1929 c->local_consumed += len;
1930 }
1931 return 1;
1932}
1933
1934static int
1935channel_handle_efd_read(struct ssh *ssh, Channel *c,
1936 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001937{
Darren Tucker11327cc2005-03-14 23:22:25 +11001938 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001939 int r;
1940 ssize_t len;
Damien Miller33b13562000-04-04 14:38:59 +10001941
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001942 if (!c->detach_close && !FD_ISSET(c->efd, readset))
1943 return 1;
1944
1945 len = read(c->efd, buf, sizeof(buf));
1946 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
1947 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1948 errno == EWOULDBLOCK) && !c->detach_close)))
1949 return 1;
1950 if (len <= 0) {
1951 debug2("channel %d: closing read-efd %d",
1952 c->self, c->efd);
1953 channel_close_fd(ssh, &c->efd);
1954 } else {
1955 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1956 debug3("channel %d: discard efd",
1957 c->self);
1958 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
1959 fatal("%s: channel %d: append: %s",
1960 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001961 }
1962 }
1963 return 1;
1964}
Damien Miller4f7becb2006-03-26 14:10:14 +11001965
Damien Miller0e220db2004-06-15 10:34:08 +10001966static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001967channel_handle_efd(struct ssh *ssh, Channel *c,
1968 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001969{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001970 if (c->efd == -1)
1971 return 1;
1972
1973 /** XXX handle drain efd, too */
1974
1975 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1976 return channel_handle_efd_write(ssh, c, readset, writeset);
1977 else if (c->extended_usage == CHAN_EXTENDED_READ ||
1978 c->extended_usage == CHAN_EXTENDED_IGNORE)
1979 return channel_handle_efd_read(ssh, c, readset, writeset);
1980
1981 return 1;
1982}
1983
1984static int
1985channel_check_window(struct ssh *ssh, Channel *c)
1986{
1987 int r;
1988
Ben Lindstromb3921512001-04-11 15:57:50 +00001989 if (c->type == SSH_CHANNEL_OPEN &&
1990 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001991 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001992 c->local_maxpacket*3) ||
1993 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001994 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001995 if (!c->have_remote_id)
1996 fatal(":%s: channel %d: no remote id",
1997 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001998 if ((r = sshpkt_start(ssh,
1999 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2000 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2001 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2002 (r = sshpkt_send(ssh)) != 0) {
2003 fatal("%s: channel %i: %s", __func__,
2004 c->self, ssh_err(r));
2005 }
Damien Millerd3444942000-09-30 14:20:03 +11002006 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002007 c->self, c->local_window,
2008 c->local_consumed);
2009 c->local_window += c->local_consumed;
2010 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002011 }
2012 return 1;
2013}
2014
Ben Lindstrombba81212001-06-25 05:01:22 +00002015static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002016channel_post_open(struct ssh *ssh, Channel *c,
2017 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002018{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002019 channel_handle_rfd(ssh, c, readset, writeset);
2020 channel_handle_wfd(ssh, c, readset, writeset);
2021 channel_handle_efd(ssh, c, readset, writeset);
2022 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002023}
2024
Damien Millere1537f92010-01-26 13:26:22 +11002025static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002026read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002027{
2028 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002029 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002030 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002031 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002032
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002033 if (sshbuf_len(c->input) < need) {
2034 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002035 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002036 if (len < 0 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002037 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002038 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002039 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002040 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002041 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002042 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002043 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2044 fatal("%s: channel %d: append: %s",
2045 __func__, c->self, ssh_err(r));
2046 }
Damien Millere1537f92010-01-26 13:26:22 +11002047 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002048 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002049}
2050
2051static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002052channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2053 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002054{
2055 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002056
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002057 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2058 return;
2059 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2060 return;
2061 if (c->mux_pause)
2062 return;
2063
2064 /*
2065 * Don't not read past the precise end of packets to
2066 * avoid disrupting fd passing.
2067 */
2068 if (read_mux(ssh, c, 4) < 4) /* read header */
2069 return;
2070 /* XXX sshbuf_peek_u32 */
2071 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002072#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002073 if (need > CHANNEL_MUX_MAX_PACKET) {
2074 debug2("channel %d: packet too big %u > %u",
2075 c->self, CHANNEL_MUX_MAX_PACKET, need);
2076 chan_rcvd_oclose(ssh, c);
2077 return;
Damien Millere1537f92010-01-26 13:26:22 +11002078 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002079 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2080 return;
2081 if (c->mux_rcb(ssh, c) != 0) {
2082 debug("channel %d: mux_rcb failed", c->self);
2083 chan_mark_dead(ssh, c);
2084 return;
Damien Millere1537f92010-01-26 13:26:22 +11002085 }
2086}
2087
2088static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002089channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2090 fd_set *readset, fd_set *writeset)
2091{
2092 ssize_t len;
2093 int r;
2094
2095 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2096 sshbuf_len(c->output) == 0)
2097 return;
2098
2099 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2100 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2101 return;
2102 if (len <= 0) {
2103 chan_mark_dead(ssh, c);
2104 return;
2105 }
2106 if ((r = sshbuf_consume(c->output, len)) != 0)
2107 fatal("%s: channel %d: consume: %s", __func__,
2108 c->self, ssh_err(r));
2109}
2110
2111static void
2112channel_post_mux_client(struct ssh *ssh, Channel *c,
2113 fd_set *readset, fd_set *writeset)
2114{
2115 channel_post_mux_client_read(ssh, c, readset, writeset);
2116 channel_post_mux_client_write(ssh, c, readset, writeset);
2117}
2118
2119static void
2120channel_post_mux_listener(struct ssh *ssh, Channel *c,
2121 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002122{
2123 Channel *nc;
2124 struct sockaddr_storage addr;
2125 socklen_t addrlen;
2126 int newsock;
2127 uid_t euid;
2128 gid_t egid;
2129
2130 if (!FD_ISSET(c->sock, readset))
2131 return;
2132
2133 debug("multiplexing control connection");
2134
2135 /*
2136 * Accept connection on control socket
2137 */
2138 memset(&addr, 0, sizeof(addr));
2139 addrlen = sizeof(addr);
2140 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2141 &addrlen)) == -1) {
2142 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002143 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002144 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002145 return;
2146 }
2147
2148 if (getpeereid(newsock, &euid, &egid) < 0) {
2149 error("%s getpeereid failed: %s", __func__,
2150 strerror(errno));
2151 close(newsock);
2152 return;
2153 }
2154 if ((euid != 0) && (getuid() != euid)) {
2155 error("multiplex uid mismatch: peer euid %u != uid %u",
2156 (u_int)euid, (u_int)getuid());
2157 close(newsock);
2158 return;
2159 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002160 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002161 newsock, newsock, -1, c->local_window_max,
2162 c->local_maxpacket, 0, "mux-control", 1);
2163 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002164 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002165 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002166 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002167 /* mux state transitions must not elicit protocol messages */
2168 nc->flags |= CHAN_LOCAL;
2169}
2170
Ben Lindstrombba81212001-06-25 05:01:22 +00002171static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002172channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002173{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002174 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002175
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002176 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2177 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2178 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002179
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002180 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2181 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2182 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2183 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2184 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2185 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2186 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2187 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2188 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2189 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
2190 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2191 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2192
2193 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2194 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2195 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2196 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2197 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2198 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2199 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2200 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2201 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
2202 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2203 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2204
2205 sc->channel_pre = pre;
2206 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002207}
2208
Damien Miller3ec27592001-10-12 11:35:04 +10002209/* gc dead channels */
2210static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002211channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002212{
2213 if (c == NULL)
2214 return;
2215 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002216 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002217 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002218 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002219 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002220 /* if we still have a callback */
2221 if (c->detach_user != NULL)
2222 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002223 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002224 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002225 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002226 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002227 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002228 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002229}
2230
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002231enum channel_table { CHAN_PRE, CHAN_POST };
2232
Ben Lindstrombba81212001-06-25 05:01:22 +00002233static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002234channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002235 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002236{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002237 struct ssh_channels *sc = ssh->chanctxt;
2238 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002239 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002240 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002241 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002242
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002243 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002244 if (unpause_secs != NULL)
2245 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002246 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2247 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002248 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002249 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002250 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002251 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002252 c->delayed = 0;
2253 else
2254 continue;
2255 }
Damien Millera6508752012-04-22 11:21:10 +10002256 if (ftab[c->type] != NULL) {
2257 /*
2258 * Run handlers that are not paused.
2259 */
2260 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002261 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002262 else if (unpause_secs != NULL) {
2263 /*
2264 * Collect the time that the earliest
2265 * channel comes off pause.
2266 */
2267 debug3("%s: chan %d: skip for %d more seconds",
2268 __func__, c->self,
2269 (int)(c->notbefore - now));
2270 if (*unpause_secs == 0 ||
2271 (c->notbefore - now) < *unpause_secs)
2272 *unpause_secs = c->notbefore - now;
2273 }
2274 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002275 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002276 }
Damien Millera6508752012-04-22 11:21:10 +10002277 if (unpause_secs != NULL && *unpause_secs != 0)
2278 debug3("%s: first channel unpauses in %d seconds",
2279 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002280}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002281
Ben Lindstrome9c99912001-06-09 00:41:05 +00002282/*
2283 * Allocate/update select bitmasks and add any bits relevant to channels in
2284 * select bitmasks.
2285 */
Damien Miller4af51302000-04-16 11:18:38 +10002286void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002287channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2288 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002289{
Damien Miller36812092006-03-26 14:22:47 +11002290 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002291
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002292 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002293
Damien Miller36812092006-03-26 14:22:47 +11002294 nfdset = howmany(n+1, NFDBITS);
2295 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002296 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002297 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2298 sz = nfdset * sizeof(fd_mask);
2299
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002300 /* perhaps check sz < nalloc/2 and shrink? */
2301 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002302 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2303 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002304 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002305 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002306 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002307 memset(*readsetp, 0, sz);
2308 memset(*writesetp, 0, sz);
2309
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002310 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002311 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002312 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002313}
2314
Ben Lindstrome9c99912001-06-09 00:41:05 +00002315/*
2316 * After select, perform any appropriate operations for channels which have
2317 * events pending.
2318 */
Damien Miller4af51302000-04-16 11:18:38 +10002319void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002320channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002321{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002322 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002323}
2324
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002325/*
2326 * Enqueue data for channels with open or draining c->input.
2327 */
2328static void
2329channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2330{
2331 size_t len, dlen;
2332 int r;
2333
2334 if ((len = sshbuf_len(c->input)) == 0) {
2335 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2336 /*
2337 * input-buffer is empty and read-socket shutdown:
2338 * tell peer, that we will not send more data:
2339 * send IEOF.
2340 * hack for extended data: delay EOF if EFD still
2341 * in use.
2342 */
2343 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2344 debug2("channel %d: "
2345 "ibuf_empty delayed efd %d/(%zu)",
2346 c->self, c->efd, sshbuf_len(c->extended));
2347 else
2348 chan_ibuf_empty(ssh, c);
2349 }
2350 return;
2351 }
2352
djm@openbsd.org9f532292017-09-12 06:35:31 +00002353 if (!c->have_remote_id)
2354 fatal(":%s: channel %d: no remote id", __func__, c->self);
2355
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002356 if (c->datagram) {
2357 /* Check datagram will fit; drop if not */
2358 if ((r = sshbuf_peek_string_direct(c->input, NULL, &dlen)) != 0)
2359 fatal("%s: channel %d: peek datagram: %s", __func__,
2360 c->self, ssh_err(r));
2361 /*
2362 * XXX this does tail-drop on the datagram queue which is
2363 * usually suboptimal compared to head-drop. Better to have
2364 * backpressure at read time? (i.e. read + discard)
2365 */
2366 if (dlen > c->remote_window || dlen > c->remote_maxpacket) {
2367 debug("channel %d: datagram too big", c->self);
2368 return;
2369 }
2370 /* Enqueue it */
2371 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2372 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2373 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
2374 (r = sshpkt_send(ssh)) != 0) {
2375 fatal("%s: channel %i: datagram: %s", __func__,
2376 c->self, ssh_err(r));
2377 }
2378 c->remote_window -= dlen;
2379 return;
2380 }
2381
2382 /* Enqueue packet for buffered data. */
2383 if (len > c->remote_window)
2384 len = c->remote_window;
2385 if (len > c->remote_maxpacket)
2386 len = c->remote_maxpacket;
2387 if (len == 0)
2388 return;
2389 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2390 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2391 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2392 (r = sshpkt_send(ssh)) != 0) {
2393 fatal("%s: channel %i: data: %s", __func__,
2394 c->self, ssh_err(r));
2395 }
2396 if ((r = sshbuf_consume(c->input, len)) != 0)
2397 fatal("%s: channel %i: consume: %s", __func__,
2398 c->self, ssh_err(r));
2399 c->remote_window -= len;
2400}
2401
2402/*
2403 * Enqueue data for channels with open c->extended in read mode.
2404 */
2405static void
2406channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2407{
2408 size_t len;
2409 int r;
2410
2411 if ((len = sshbuf_len(c->extended)) == 0)
2412 return;
2413
2414 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2415 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2416 if (len > c->remote_window)
2417 len = c->remote_window;
2418 if (len > c->remote_maxpacket)
2419 len = c->remote_maxpacket;
2420 if (len == 0)
2421 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002422 if (!c->have_remote_id)
2423 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002424 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2425 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2426 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2427 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2428 (r = sshpkt_send(ssh)) != 0) {
2429 fatal("%s: channel %i: data: %s", __func__,
2430 c->self, ssh_err(r));
2431 }
2432 if ((r = sshbuf_consume(c->extended, len)) != 0)
2433 fatal("%s: channel %i: consume: %s", __func__,
2434 c->self, ssh_err(r));
2435 c->remote_window -= len;
2436 debug2("channel %d: sent ext data %zu", c->self, len);
2437}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002438
Damien Miller5e953212001-01-30 09:14:00 +11002439/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002440void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002441channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002442{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002443 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002444 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002445 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002446
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002447 for (i = 0; i < sc->channels_alloc; i++) {
2448 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002449 if (c == NULL)
2450 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002451
Ben Lindstrome9c99912001-06-09 00:41:05 +00002452 /*
2453 * We are only interested in channels that can have buffered
2454 * incoming data.
2455 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002456 if (c->type != SSH_CHANNEL_OPEN)
2457 continue;
2458 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002459 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002460 debug3("channel %d: will not send data after close",
2461 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002462 continue;
2463 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002464
Damien Miller95def091999-11-25 00:26:21 +11002465 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002466 if (c->istate == CHAN_INPUT_OPEN ||
2467 c->istate == CHAN_INPUT_WAIT_DRAIN)
2468 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002469 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002470 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002471 c->extended_usage == CHAN_EXTENDED_READ)
2472 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002473 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002474}
2475
markus@openbsd.org8d057842016-09-30 09:19:13 +00002476/* -- mux proxy support */
2477
2478/*
2479 * When multiplexing channel messages for mux clients we have to deal
2480 * with downstream messages from the mux client and upstream messages
2481 * from the ssh server:
2482 * 1) Handling downstream messages is straightforward and happens
2483 * in channel_proxy_downstream():
2484 * - We forward all messages (mostly) unmodified to the server.
2485 * - However, in order to route messages from upstream to the correct
2486 * downstream client, we have to replace the channel IDs used by the
2487 * mux clients with a unique channel ID because the mux clients might
2488 * use conflicting channel IDs.
2489 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2490 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2491 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2492 * with the newly allocated channel ID.
2493 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2494 * channels and procesed by channel_proxy_upstream(). The local channel ID
2495 * is then translated back to the original mux client ID.
2496 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2497 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2498 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2499 * downstream mux client are removed.
2500 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2501 * requires more work, because they are not addressed to a specific
2502 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2503 * out whether the request is addressed to the local client or a
2504 * specific downstream client based on the listen-address/port.
2505 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2506 * not supported as the matching session/channel cannot be identified
2507 * easily.
2508 */
2509
2510/*
2511 * receive packets from downstream mux clients:
2512 * channel callback fired on read from mux client, creates
2513 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2514 * on channel creation.
2515 */
2516int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002517channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002518{
2519 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002520 struct sshbuf *original = NULL, *modified = NULL;
2521 const u_char *cp;
2522 char *ctype = NULL, *listen_host = NULL;
2523 u_char type;
2524 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002525 int ret = -1, r, idx;
2526 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002527
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002528 /* sshbuf_dump(downstream->input, stderr); */
2529 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002530 != 0) {
2531 error("%s: malformed message: %s", __func__, ssh_err(r));
2532 return -1;
2533 }
2534 if (have < 2) {
2535 error("%s: short message", __func__);
2536 return -1;
2537 }
2538 type = cp[1];
2539 /* skip padlen + type */
2540 cp += 2;
2541 have -= 2;
2542 if (ssh_packet_log_type(type))
2543 debug3("%s: channel %u: down->up: type %u", __func__,
2544 downstream->self, type);
2545
2546 switch (type) {
2547 case SSH2_MSG_CHANNEL_OPEN:
2548 if ((original = sshbuf_from(cp, have)) == NULL ||
2549 (modified = sshbuf_new()) == NULL) {
2550 error("%s: alloc", __func__);
2551 goto out;
2552 }
2553 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2554 (r = sshbuf_get_u32(original, &id)) != 0) {
2555 error("%s: parse error %s", __func__, ssh_err(r));
2556 goto out;
2557 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002558 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002559 -1, -1, -1, 0, 0, 0, ctype, 1);
2560 c->mux_ctx = downstream; /* point to mux client */
2561 c->mux_downstream_id = id; /* original downstream id */
2562 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2563 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2564 (r = sshbuf_putb(modified, original)) != 0) {
2565 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002566 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002567 goto out;
2568 }
2569 break;
2570 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2571 /*
2572 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2573 * need to parse 'remote_id' instead of 'ctype'.
2574 */
2575 if ((original = sshbuf_from(cp, have)) == NULL ||
2576 (modified = sshbuf_new()) == NULL) {
2577 error("%s: alloc", __func__);
2578 goto out;
2579 }
2580 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2581 (r = sshbuf_get_u32(original, &id)) != 0) {
2582 error("%s: parse error %s", __func__, ssh_err(r));
2583 goto out;
2584 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002585 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002586 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2587 c->mux_ctx = downstream; /* point to mux client */
2588 c->mux_downstream_id = id;
2589 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002590 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002591 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2592 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2593 (r = sshbuf_putb(modified, original)) != 0) {
2594 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002595 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002596 goto out;
2597 }
2598 break;
2599 case SSH2_MSG_GLOBAL_REQUEST:
2600 if ((original = sshbuf_from(cp, have)) == NULL) {
2601 error("%s: alloc", __func__);
2602 goto out;
2603 }
2604 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2605 error("%s: parse error %s", __func__, ssh_err(r));
2606 goto out;
2607 }
2608 if (strcmp(ctype, "tcpip-forward") != 0) {
2609 error("%s: unsupported request %s", __func__, ctype);
2610 goto out;
2611 }
2612 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2613 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2614 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2615 error("%s: parse error %s", __func__, ssh_err(r));
2616 goto out;
2617 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002618 if (listen_port > 65535) {
2619 error("%s: tcpip-forward for %s: bad port %u",
2620 __func__, listen_host, listen_port);
2621 goto out;
2622 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002623 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002624 idx = fwd_perm_list_add(ssh, FWDPERM_USER, "<mux>", -1,
2625 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002626 listen_host = NULL;
2627 break;
2628 case SSH2_MSG_CHANNEL_CLOSE:
2629 if (have < 4)
2630 break;
2631 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002632 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002633 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002634 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002635 else
2636 c->flags |= CHAN_CLOSE_SENT;
2637 }
2638 break;
2639 }
2640 if (modified) {
2641 if ((r = sshpkt_start(ssh, type)) != 0 ||
2642 (r = sshpkt_putb(ssh, modified)) != 0 ||
2643 (r = sshpkt_send(ssh)) != 0) {
2644 error("%s: send %s", __func__, ssh_err(r));
2645 goto out;
2646 }
2647 } else {
2648 if ((r = sshpkt_start(ssh, type)) != 0 ||
2649 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2650 (r = sshpkt_send(ssh)) != 0) {
2651 error("%s: send %s", __func__, ssh_err(r));
2652 goto out;
2653 }
2654 }
2655 ret = 0;
2656 out:
2657 free(ctype);
2658 free(listen_host);
2659 sshbuf_free(original);
2660 sshbuf_free(modified);
2661 return ret;
2662}
2663
2664/*
2665 * receive packets from upstream server and de-multiplex packets
2666 * to correct downstream:
2667 * implemented as a helper for channel input handlers,
2668 * replaces local (proxy) channel ID with downstream channel ID.
2669 */
2670int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002671channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002672{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002673 struct sshbuf *b = NULL;
2674 Channel *downstream;
2675 const u_char *cp = NULL;
2676 size_t len;
2677 int r;
2678
2679 /*
2680 * When receiving packets from the peer we need to check whether we
2681 * need to forward the packets to the mux client. In this case we
2682 * restore the orignal channel id and keep track of CLOSE messages,
2683 * so we can cleanup the channel.
2684 */
2685 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2686 return 0;
2687 if ((downstream = c->mux_ctx) == NULL)
2688 return 0;
2689 switch (type) {
2690 case SSH2_MSG_CHANNEL_CLOSE:
2691 case SSH2_MSG_CHANNEL_DATA:
2692 case SSH2_MSG_CHANNEL_EOF:
2693 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2694 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2695 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2696 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2697 case SSH2_MSG_CHANNEL_SUCCESS:
2698 case SSH2_MSG_CHANNEL_FAILURE:
2699 case SSH2_MSG_CHANNEL_REQUEST:
2700 break;
2701 default:
2702 debug2("%s: channel %u: unsupported type %u", __func__,
2703 c->self, type);
2704 return 0;
2705 }
2706 if ((b = sshbuf_new()) == NULL) {
2707 error("%s: alloc reply", __func__);
2708 goto out;
2709 }
2710 /* get remaining payload (after id) */
2711 cp = sshpkt_ptr(ssh, &len);
2712 if (cp == NULL) {
2713 error("%s: no packet", __func__);
2714 goto out;
2715 }
2716 /* translate id and send to muxclient */
2717 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2718 (r = sshbuf_put_u8(b, type)) != 0 ||
2719 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2720 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002721 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002722 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2723 goto out;
2724 }
2725 /* sshbuf_dump(b, stderr); */
2726 if (ssh_packet_log_type(type))
2727 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2728 type);
2729 out:
2730 /* update state */
2731 switch (type) {
2732 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2733 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002734 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002735 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002736 c->have_remote_id = 1;
2737 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002738 break;
2739 case SSH2_MSG_CHANNEL_CLOSE:
2740 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002741 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002742 else
2743 c->flags |= CHAN_CLOSE_RCVD;
2744 break;
2745 }
2746 sshbuf_free(b);
2747 return 1;
2748}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002749
2750/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002751
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002752/* Parse a channel ID from the current packet */
2753static int
2754channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2755{
2756 u_int32_t id;
2757 int r;
2758
2759 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2760 error("%s: parse id: %s", where, ssh_err(r));
2761 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2762 }
2763 if (id > INT_MAX) {
2764 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2765 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2766 }
2767 return (int)id;
2768}
2769
2770/* Lookup a channel from an ID in the current packet */
2771static Channel *
2772channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2773{
2774 int id = channel_parse_id(ssh, where, what);
2775 Channel *c;
2776
2777 if ((c = channel_lookup(ssh, id)) == NULL) {
2778 ssh_packet_disconnect(ssh,
2779 "%s packet referred to nonexistent channel %d", what, id);
2780 }
2781 return c;
2782}
2783
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002784int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002785channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002786{
Damien Miller633de332014-05-15 13:48:26 +10002787 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002788 size_t data_len, win_len;
2789 Channel *c = channel_from_packet_id(ssh, __func__, "data");
2790 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002791
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002792 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002793 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002794
Damien Miller95def091999-11-25 00:26:21 +11002795 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002796 if (c->type != SSH_CHANNEL_OPEN &&
2797 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002798 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002799
Damien Miller95def091999-11-25 00:26:21 +11002800 /* Get the data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002801 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0)
2802 fatal("%s: channel %d: get data: %s", __func__,
2803 c->self, ssh_err(r));
2804 ssh_packet_check_eom(ssh);
2805
Damien Miller7d457182010-08-05 23:09:48 +10002806 win_len = data_len;
2807 if (c->datagram)
2808 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002809
Damien Millera04ad492004-01-21 11:02:09 +11002810 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002811 * The sending side reduces its window as it sends data, so we
2812 * must 'fake' consumption of the data in order to ensure that window
2813 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11002814 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002815 if (c->ostate != CHAN_OUTPUT_OPEN) {
2816 c->local_window -= win_len;
2817 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002818 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002819 }
2820
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002821 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002822 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002823 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002824 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002825 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002826 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002827 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002828 c->self, win_len, c->local_window);
2829 return 0;
2830 }
2831 c->local_window -= win_len;
2832
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002833 if (c->datagram) {
2834 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
2835 fatal("%s: channel %d: append datagram: %s",
2836 __func__, c->self, ssh_err(r));
2837 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
2838 fatal("%s: channel %d: append data: %s",
2839 __func__, c->self, ssh_err(r));
2840
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002841 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002842}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002843
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002844int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002845channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002846{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002847 const u_char *data;
2848 size_t data_len;
2849 u_int32_t tcode;
2850 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
2851 int r;
Damien Miller33b13562000-04-04 14:38:59 +10002852
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002853 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002854 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002855 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002856 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002857 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002858 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002859 if (c->flags & CHAN_EOF_RCVD) {
2860 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002861 debug("channel %d: accepting ext data after eof",
2862 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002863 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002864 ssh_packet_disconnect(ssh, "Received extended_data "
2865 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002866 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002867
2868 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
2869 error("%s: parse tcode: %s", __func__, ssh_err(r));
2870 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2871 }
Damien Miller33b13562000-04-04 14:38:59 +10002872 if (c->efd == -1 ||
2873 c->extended_usage != CHAN_EXTENDED_WRITE ||
2874 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002875 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002876 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002877 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002878 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) {
2879 error("%s: parse data: %s", __func__, ssh_err(r));
2880 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2881 }
2882 ssh_packet_check_eom(ssh);
2883
Damien Miller33b13562000-04-04 14:38:59 +10002884 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002885 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10002886 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002887 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002888 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002889 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
2890 /* XXX sshpkt_getb? */
2891 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
2892 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002893 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002894 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002895}
2896
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002897int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002898channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10002899{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002900 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
Damien Millerb38eff82000-04-01 11:09:21 +10002901
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002902 ssh_packet_check_eom(ssh);
2903
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002904 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002905 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002906 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002907
2908 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002909 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002910 debug("channel %d: FORCE input drain", c->self);
2911 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002912 if (sshbuf_len(c->input) == 0)
2913 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002914 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002915 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002916}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002917
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002918int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002919channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002920{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002921 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
Damien Miller66823cd2002-01-22 23:11:38 +11002922
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002923 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002924 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002925 ssh_packet_check_eom(ssh);
2926 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002927 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002928}
2929
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002930int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002931channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002932{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002933 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
2934 u_int32_t remote_window, remote_maxpacket;
2935 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002936
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002937 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002938 return 0;
2939 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002940 packet_disconnect("Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002941 "non-opening channel %d.", c->self);
2942 /*
2943 * Record the remote channel number and mark that the channel
2944 * is now open.
2945 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002946 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
2947 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002948 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) {
2949 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
2950 packet_disconnect("Invalid open confirmation message");
2951 }
2952 ssh_packet_check_eom(ssh);
Damien Miller33b13562000-04-04 14:38:59 +10002953
djm@openbsd.org9f532292017-09-12 06:35:31 +00002954 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002955 c->remote_window = remote_window;
2956 c->remote_maxpacket = remote_maxpacket;
2957 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002958 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002959 debug2("%s: channel %d: callback start", __func__, c->self);
2960 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
2961 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002962 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002963 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2964 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002965 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002966}
2967
Ben Lindstrombba81212001-06-25 05:01:22 +00002968static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002969reason2txt(int reason)
2970{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002971 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002972 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2973 return "administratively prohibited";
2974 case SSH2_OPEN_CONNECT_FAILED:
2975 return "connect failed";
2976 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2977 return "unknown channel type";
2978 case SSH2_OPEN_RESOURCE_SHORTAGE:
2979 return "resource shortage";
2980 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002981 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002982}
2983
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002984int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002985channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002986{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002987 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
2988 u_int32_t reason;
2989 char *msg = NULL;
2990 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002991
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002992 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002993 return 0;
2994 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002995 packet_disconnect("Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002996 "non-opening channel %d.", c->self);
2997 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
2998 error("%s: reason: %s", __func__, ssh_err(r));
2999 packet_disconnect("Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003000 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003001 if ((datafellows & SSH_BUG_OPENFAILURE) == 0) {
3002 /* skip language */
3003 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
3004 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) == 0) {
3005 error("%s: message/lang: %s", __func__, ssh_err(r));
3006 packet_disconnect("Invalid open failure message");
3007 }
3008 }
3009 ssh_packet_check_eom(ssh);
3010 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003011 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3012 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003013 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003014 debug2("%s: channel %d: callback start", __func__, c->self);
3015 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3016 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003017 }
Damien Miller7a606212009-01-28 16:22:34 +11003018 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003019 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003020 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003021}
3022
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003023int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003024channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003025{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003026 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003027 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003028 u_int32_t adjust;
3029 u_int new_rwin;
3030 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003031
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003032 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003033 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003034 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003035 }
3036
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003037 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003038 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003039 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) {
3040 error("%s: adjust: %s", __func__, ssh_err(r));
3041 packet_disconnect("Invalid window adjust message");
3042 }
3043 ssh_packet_check_eom(ssh);
3044 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3045 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003046 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003047 c->self, adjust, c->remote_window);
3048 }
3049 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003050 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003051}
3052
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003053int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003054channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003055{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003056 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003057 Channel *c;
3058 struct channel_confirm *cc;
Damien Millerb84886b2008-05-19 15:05:07 +10003059
3060 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003061 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003062
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003063 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003064
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003065 if ((c = channel_lookup(ssh, id)) == NULL) {
3066 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003067 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003068 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003069 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003070 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003071 ssh_packet_check_eom(ssh);
Damien Millerb84886b2008-05-19 15:05:07 +10003072 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003073 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003074 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003075 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003076 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003077 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003078 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003079}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003080
Ben Lindstrome9c99912001-06-09 00:41:05 +00003081/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003082
Ben Lindstrom908afed2001-10-03 17:34:59 +00003083void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003084channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003085{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003086 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003087}
3088
Damien Millerf6dff7c2011-09-22 21:38:52 +10003089
3090/*
3091 * Determine whether or not a port forward listens to loopback, the
3092 * specified address or wildcard. On the client, a specified bind
3093 * address will always override gateway_ports. On the server, a
3094 * gateway_ports of 1 (``yes'') will override the client's specification
3095 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3096 * will bind to whatever address the client asked for.
3097 *
3098 * Special-case listen_addrs are:
3099 *
3100 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3101 * "" (empty string), "*" -> wildcard v4/v6
3102 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003103 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003104 */
3105static const char *
3106channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003107 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003108{
3109 const char *addr = NULL;
3110 int wildcard = 0;
3111
3112 if (listen_addr == NULL) {
3113 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003114 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003115 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003116 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003117 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3118 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3119 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003120 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003121 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003122 /*
3123 * Notify client if they requested a specific listen
3124 * address and it was overridden.
3125 */
3126 if (*listen_addr != '\0' &&
3127 strcmp(listen_addr, "0.0.0.0") != 0 &&
3128 strcmp(listen_addr, "*") != 0) {
3129 packet_send_debug("Forwarding listen address "
3130 "\"%s\" overridden by server "
3131 "GatewayPorts", listen_addr);
3132 }
Damien Miller602943d2014-07-04 08:59:41 +10003133 } else if (strcmp(listen_addr, "localhost") != 0 ||
3134 strcmp(listen_addr, "127.0.0.1") == 0 ||
3135 strcmp(listen_addr, "::1") == 0) {
3136 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003137 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003138 }
3139 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3140 strcmp(listen_addr, "::1") == 0) {
3141 /*
3142 * If a specific IPv4/IPv6 localhost address has been
3143 * requested then accept it even if gateway_ports is in
3144 * effect. This allows the client to prefer IPv4 or IPv6.
3145 */
3146 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003147 }
3148 if (wildcardp != NULL)
3149 *wildcardp = wildcard;
3150 return addr;
3151}
3152
Damien Millerb16461c2002-01-22 23:29:22 +11003153static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003154channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3155 struct Forward *fwd, int *allocated_listen_port,
3156 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003157{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003158 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003159 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003160 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003161 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003162 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003163 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003164
Damien Millerf91ee4c2005-03-01 21:24:33 +11003165 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003166
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003167 if (is_client && fwd->connect_path != NULL) {
3168 host = fwd->connect_path;
3169 } else {
3170 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3171 fwd->listen_host : fwd->connect_host;
3172 if (host == NULL) {
3173 error("No forward host name.");
3174 return 0;
3175 }
3176 if (strlen(host) >= NI_MAXHOST) {
3177 error("Forward host name too long.");
3178 return 0;
3179 }
Kevin Steves12057502001-02-05 14:54:34 +00003180 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003181
Damien Millerf6dff7c2011-09-22 21:38:52 +10003182 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003183 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3184 is_client, fwd_opts);
3185 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003186 type, wildcard, (addr == NULL) ? "NULL" : addr);
3187
3188 /*
Damien Miller34132e52000-01-14 15:45:46 +11003189 * getaddrinfo returns a loopback address if the hostname is
3190 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003191 */
Damien Miller34132e52000-01-14 15:45:46 +11003192 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003193 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003194 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003195 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003196 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003197 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3198 if (addr == NULL) {
3199 /* This really shouldn't happen */
3200 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003201 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003202 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003203 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003204 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003205 }
Damien Millera7270302005-07-06 09:36:05 +10003206 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003207 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003208 if (allocated_listen_port != NULL)
3209 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003210 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003211 switch (ai->ai_family) {
3212 case AF_INET:
3213 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3214 sin_port;
3215 break;
3216 case AF_INET6:
3217 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3218 sin6_port;
3219 break;
3220 default:
Damien Miller34132e52000-01-14 15:45:46 +11003221 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003222 }
3223 /*
3224 * If allocating a port for -R forwards, then use the
3225 * same port for all address families.
3226 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003227 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3228 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3229 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003230 *lport_p = htons(*allocated_listen_port);
3231
Damien Miller34132e52000-01-14 15:45:46 +11003232 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003233 strport, sizeof(strport),
3234 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003235 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003236 continue;
3237 }
3238 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003239 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003240 if (sock < 0) {
3241 /* this is no error since kernel may not support ipv6 */
3242 verbose("socket: %.100s", strerror(errno));
3243 continue;
3244 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003245
3246 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003247 if (ai->ai_family == AF_INET6)
3248 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003249
Damien Miller4bf648f2009-02-14 16:28:21 +11003250 debug("Local forwarding listening on %s port %s.",
3251 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003252
Damien Miller34132e52000-01-14 15:45:46 +11003253 /* Bind the socket to the address. */
3254 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003255 /*
3256 * address can be in if use ipv6 address is
3257 * already bound
3258 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003259 if (!ai->ai_next)
3260 error("bind: %.100s", strerror(errno));
3261 else
3262 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003263
Damien Miller34132e52000-01-14 15:45:46 +11003264 close(sock);
3265 continue;
3266 }
3267 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003268 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003269 error("listen: %.100s", strerror(errno));
3270 close(sock);
3271 continue;
3272 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003273
3274 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003275 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003276 * record what we got.
3277 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003278 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3279 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003280 allocated_listen_port != NULL &&
3281 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003282 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003283 debug("Allocated listen port %d",
3284 *allocated_listen_port);
3285 }
3286
Damien Miller34132e52000-01-14 15:45:46 +11003287 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003288 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003289 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003290 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003291 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003292 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003293 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003294 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003295 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3296 c->listening_port = *allocated_listen_port;
3297 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003298 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003299 success = 1;
3300 }
3301 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003302 error("%s: cannot listen to port: %d", __func__,
3303 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003304 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003305 return success;
Damien Miller95def091999-11-25 00:26:21 +11003306}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003307
Damien Miller7acefbb2014-07-18 14:11:24 +10003308static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003309channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3310 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003311{
3312 struct sockaddr_un sunaddr;
3313 const char *path;
3314 Channel *c;
3315 int port, sock;
3316 mode_t omask;
3317
3318 switch (type) {
3319 case SSH_CHANNEL_UNIX_LISTENER:
3320 if (fwd->connect_path != NULL) {
3321 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3322 error("Local connecting path too long: %s",
3323 fwd->connect_path);
3324 return 0;
3325 }
3326 path = fwd->connect_path;
3327 port = PORT_STREAMLOCAL;
3328 } else {
3329 if (fwd->connect_host == NULL) {
3330 error("No forward host name.");
3331 return 0;
3332 }
3333 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3334 error("Forward host name too long.");
3335 return 0;
3336 }
3337 path = fwd->connect_host;
3338 port = fwd->connect_port;
3339 }
3340 break;
3341 case SSH_CHANNEL_RUNIX_LISTENER:
3342 path = fwd->listen_path;
3343 port = PORT_STREAMLOCAL;
3344 break;
3345 default:
3346 error("%s: unexpected channel type %d", __func__, type);
3347 return 0;
3348 }
3349
3350 if (fwd->listen_path == NULL) {
3351 error("No forward path name.");
3352 return 0;
3353 }
3354 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3355 error("Local listening path too long: %s", fwd->listen_path);
3356 return 0;
3357 }
3358
3359 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3360
3361 /* Start a Unix domain listener. */
3362 omask = umask(fwd_opts->streamlocal_bind_mask);
3363 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3364 fwd_opts->streamlocal_bind_unlink);
3365 umask(omask);
3366 if (sock < 0)
3367 return 0;
3368
3369 debug("Local forwarding listening on path %s.", fwd->listen_path);
3370
3371 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003372 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003373 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3374 0, "unix listener", 1);
3375 c->path = xstrdup(path);
3376 c->host_port = port;
3377 c->listening_port = PORT_STREAMLOCAL;
3378 c->listening_addr = xstrdup(fwd->listen_path);
3379 return 1;
3380}
3381
3382static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003383channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3384 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003385{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003386 u_int i;
3387 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003388
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003389 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3390 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003391 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3392 continue;
3393 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3394 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003395 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003396 found = 1;
3397 }
3398 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003399
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003400 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003401}
3402
Damien Miller7acefbb2014-07-18 14:11:24 +10003403static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003404channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003405{
3406 u_int i;
3407 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003408
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003409 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3410 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003411 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3412 continue;
3413 if (c->path == NULL)
3414 continue;
3415 if (strcmp(c->path, path) == 0) {
3416 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003417 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003418 found = 1;
3419 }
3420 }
3421
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003422 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003423}
3424
3425int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003426channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003427{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003428 if (fwd->listen_path != NULL) {
3429 return channel_cancel_rport_listener_streamlocal(ssh,
3430 fwd->listen_path);
3431 } else {
3432 return channel_cancel_rport_listener_tcpip(ssh,
3433 fwd->listen_host, fwd->listen_port);
3434 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003435}
3436
3437static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003438channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3439 const char *lhost, u_short lport, int cport,
3440 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003441{
3442 u_int i;
3443 int found = 0;
3444 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003445
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003446 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3447 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003448 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3449 continue;
Damien Millerff773642011-09-22 21:39:48 +10003450 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003451 continue;
Damien Millerff773642011-09-22 21:39:48 +10003452 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3453 /* skip dynamic forwardings */
3454 if (c->host_port == 0)
3455 continue;
3456 } else {
3457 if (c->host_port != cport)
3458 continue;
3459 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003460 if ((c->listening_addr == NULL && addr != NULL) ||
3461 (c->listening_addr != NULL && addr == NULL))
3462 continue;
3463 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003464 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003465 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003466 found = 1;
3467 }
3468 }
3469
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003470 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003471}
3472
Damien Miller7acefbb2014-07-18 14:11:24 +10003473static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003474channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003475{
3476 u_int i;
3477 int found = 0;
3478
3479 if (path == NULL) {
3480 error("%s: no path specified.", __func__);
3481 return 0;
3482 }
3483
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003484 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3485 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003486 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3487 continue;
3488 if (c->listening_addr == NULL)
3489 continue;
3490 if (strcmp(c->listening_addr, path) == 0) {
3491 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003492 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003493 found = 1;
3494 }
3495 }
3496
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003497 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003498}
3499
3500int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003501channel_cancel_lport_listener(struct ssh *ssh,
3502 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003503{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003504 if (fwd->listen_path != NULL) {
3505 return channel_cancel_lport_listener_streamlocal(ssh,
3506 fwd->listen_path);
3507 } else {
3508 return channel_cancel_lport_listener_tcpip(ssh,
3509 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3510 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003511}
3512
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003513/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003514int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003515channel_setup_local_fwd_listener(struct ssh *ssh,
3516 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003517{
Damien Miller7acefbb2014-07-18 14:11:24 +10003518 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003519 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003520 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3521 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003522 return channel_setup_fwd_listener_tcpip(ssh,
3523 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003524 }
Damien Millerb16461c2002-01-22 23:29:22 +11003525}
3526
3527/* protocol v2 remote port fwd, used by sshd */
3528int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003529channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003530 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003531{
Damien Miller7acefbb2014-07-18 14:11:24 +10003532 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003533 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003534 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3535 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003536 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003537 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3538 fwd_opts);
3539 }
Damien Millerb16461c2002-01-22 23:29:22 +11003540}
3541
Damien Miller5428f641999-11-25 11:54:57 +11003542/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003543 * Translate the requested rfwd listen host to something usable for
3544 * this server.
3545 */
3546static const char *
3547channel_rfwd_bind_host(const char *listen_host)
3548{
3549 if (listen_host == NULL) {
3550 if (datafellows & SSH_BUG_RFWD_ADDR)
3551 return "127.0.0.1";
3552 else
3553 return "localhost";
3554 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3555 if (datafellows & SSH_BUG_RFWD_ADDR)
3556 return "0.0.0.0";
3557 else
3558 return "";
3559 } else
3560 return listen_host;
3561}
3562
3563/*
Damien Miller5428f641999-11-25 11:54:57 +11003564 * Initiate forwarding of connections to port "port" on remote host through
3565 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003566 * Returns handle (index) for updating the dynamic listen port with
3567 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003568 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003569int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003570channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003571{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003572 int r, success = 0, idx = -1;
3573 char *host_to_connect, *listen_host, *listen_path;
3574 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003575
Damien Miller95def091999-11-25 00:26:21 +11003576 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003577 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003578 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3579 (r = sshpkt_put_cstring(ssh,
3580 "streamlocal-forward@openssh.com")) != 0 ||
3581 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3582 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3583 (r = sshpkt_send(ssh)) != 0 ||
3584 (r = ssh_packet_write_wait(ssh)) != 0)
3585 fatal("%s: request streamlocal: %s",
3586 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003587 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003588 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3589 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3590 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3591 (r = sshpkt_put_cstring(ssh,
3592 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3593 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3594 (r = sshpkt_send(ssh)) != 0 ||
3595 (r = ssh_packet_write_wait(ssh)) != 0)
3596 fatal("%s: request tcpip-forward: %s",
3597 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003598 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003599 /* Assume that server accepts the request */
3600 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003601 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003602 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003603 host_to_connect = listen_host = listen_path = NULL;
3604 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003605 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003606 host_to_connect = xstrdup(fwd->connect_path);
3607 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003608 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003609 host_to_connect = xstrdup(fwd->connect_host);
3610 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003611 }
3612 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003613 listen_path = xstrdup(fwd->listen_path);
3614 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003615 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003616 if (fwd->listen_host != NULL)
3617 listen_host = xstrdup(fwd->listen_host);
3618 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003619 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003620 idx = fwd_perm_list_add(ssh, FWDPERM_USER,
3621 host_to_connect, port_to_connect,
3622 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003623 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003624 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003625}
3626
Damien Miller4b3ed642014-07-02 15:29:40 +10003627static int
3628open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003629 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003630{
3631 if (allowed_open->host_to_connect == NULL)
3632 return 0;
3633 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3634 allowed_open->port_to_connect != requestedport)
3635 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003636 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3637 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003638 return 0;
3639 return 1;
3640}
3641
3642/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003643 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003644 * we don't support FWD_PERMIT_ANY_PORT and
3645 * need to translate between the configured-host (listen_host)
3646 * and what we've sent to the remote server (channel_rfwd_bind_host)
3647 */
3648static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003649open_listen_match_tcpip(ForwardPermission *allowed_open,
3650 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003651{
3652 const char *allowed_host;
3653
3654 if (allowed_open->host_to_connect == NULL)
3655 return 0;
3656 if (allowed_open->listen_port != requestedport)
3657 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003658 if (!translate && allowed_open->listen_host == NULL &&
3659 requestedhost == NULL)
3660 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003661 allowed_host = translate ?
3662 channel_rfwd_bind_host(allowed_open->listen_host) :
3663 allowed_open->listen_host;
3664 if (allowed_host == NULL ||
3665 strcmp(allowed_host, requestedhost) != 0)
3666 return 0;
3667 return 1;
3668}
3669
Damien Miller7acefbb2014-07-18 14:11:24 +10003670static int
3671open_listen_match_streamlocal(ForwardPermission *allowed_open,
3672 const char *requestedpath)
3673{
3674 if (allowed_open->host_to_connect == NULL)
3675 return 0;
3676 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3677 return 0;
3678 if (allowed_open->listen_path == NULL ||
3679 strcmp(allowed_open->listen_path, requestedpath) != 0)
3680 return 0;
3681 return 1;
3682}
3683
Damien Miller5428f641999-11-25 11:54:57 +11003684/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003685 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003686 * local side.
3687 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003688static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003689channel_request_rforward_cancel_tcpip(struct ssh *ssh,
3690 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003691{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003692 struct ssh_channels *sc = ssh->chanctxt;
3693 int r;
3694 u_int i;
3695 ForwardPermission *fp;
Darren Tuckere7066df2004-05-24 10:18:05 +10003696
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003697 for (i = 0; i < sc->num_permitted_opens; i++) {
3698 fp = &sc->permitted_opens[i];
3699 if (open_listen_match_tcpip(fp, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003700 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003701 fp = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10003702 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003703 if (fp == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003704 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003705 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003706 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003707 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3708 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
3709 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3710 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
3711 (r = sshpkt_put_u32(ssh, port)) != 0 ||
3712 (r = sshpkt_send(ssh)) != 0)
3713 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10003714
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003715 fwd_perm_clear(fp); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10003716
3717 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003718}
3719
3720/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003721 * Request cancellation of remote forwarding of Unix domain socket
3722 * path from local side.
3723 */
3724static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003725channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003726{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003727 struct ssh_channels *sc = ssh->chanctxt;
3728 int r;
3729 u_int i;
3730 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10003731
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003732 for (i = 0; i < sc->num_permitted_opens; i++) {
3733 fp = &sc->permitted_opens[i];
3734 if (open_listen_match_streamlocal(fp, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10003735 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003736 fp = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003737 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003738 if (fp == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003739 debug("%s: requested forward not found", __func__);
3740 return -1;
3741 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003742 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3743 (r = sshpkt_put_cstring(ssh,
3744 "cancel-streamlocal-forward@openssh.com")) != 0 ||
3745 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3746 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
3747 (r = sshpkt_send(ssh)) != 0)
3748 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003749
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003750 fwd_perm_clear(fp); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10003751
3752 return 0;
3753}
3754
3755/*
3756 * Request cancellation of remote forwarding of a connection from local side.
3757 */
3758int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003759channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003760{
3761 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003762 return channel_request_rforward_cancel_streamlocal(ssh,
3763 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003764 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003765 return channel_request_rforward_cancel_tcpip(ssh,
3766 fwd->listen_host,
3767 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10003768 }
3769}
3770
3771/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003772 * Permits opening to any host/port if permitted_opens[] is empty. This is
3773 * usually called by the server, because the user could connect to any port
3774 * anyway, and the server has no way to know but to trust the client anyway.
3775 */
3776void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003777channel_permit_all_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003778{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003779 if (ssh->chanctxt->num_permitted_opens == 0)
3780 ssh->chanctxt->all_opens_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003781}
3782
Ben Lindstroma3700052001-04-05 23:26:32 +00003783void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003784channel_add_permitted_opens(struct ssh *ssh, char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003785{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003786 struct ssh_channels *sc = ssh->chanctxt;
3787
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003788 debug("allow port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003789 fwd_perm_list_add(ssh, FWDPERM_USER, host, port, NULL, NULL, 0, NULL);
3790 sc->all_opens_permitted = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003791}
3792
Darren Tucker68afb8c2011-10-02 18:59:03 +11003793/*
3794 * Update the listen port for a dynamic remote forward, after
3795 * the actual 'newport' has been allocated. If 'newport' < 0 is
3796 * passed then they entry will be invalidated.
3797 */
3798void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003799channel_update_permitted_opens(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11003800{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003801 struct ssh_channels *sc = ssh->chanctxt;
3802
3803 if (idx < 0 || (u_int)idx >= sc->num_permitted_opens) {
3804 debug("%s: index out of range: %d num_permitted_opens %d",
3805 __func__, idx, sc->num_permitted_opens);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003806 return;
3807 }
3808 debug("%s allowed port %d for forwarding to host %s port %d",
3809 newport > 0 ? "Updating" : "Removing",
3810 newport,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003811 sc->permitted_opens[idx].host_to_connect,
3812 sc->permitted_opens[idx].port_to_connect);
3813 if (newport <= 0)
3814 fwd_perm_clear(&sc->permitted_opens[idx]);
3815 else {
3816 sc->permitted_opens[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11003817 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003818 }
3819}
3820
Damien Millera765cf42006-07-24 14:08:13 +10003821int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003822channel_add_adm_permitted_opens(struct ssh *ssh, char *host, int port)
Damien Miller9b439df2006-07-24 14:04:00 +10003823{
Damien Millera765cf42006-07-24 14:08:13 +10003824 debug("config allows port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003825 return fwd_perm_list_add(ssh, FWDPERM_ADMIN, host, port,
3826 NULL, NULL, 0, NULL);
Damien Miller9b439df2006-07-24 14:04:00 +10003827}
3828
3829void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003830channel_disable_adm_local_opens(struct ssh *ssh)
Damien Millerc6081482012-04-22 11:18:53 +10003831{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003832 channel_clear_adm_permitted_opens(ssh);
3833 fwd_perm_list_add(ssh, FWDPERM_ADMIN, NULL, 0, NULL, NULL, 0, NULL);
Damien Millerc6081482012-04-22 11:18:53 +10003834}
3835
3836void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003837channel_clear_permitted_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003838{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003839 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003840
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003841 sc->permitted_opens = xrecallocarray(sc->permitted_opens,
3842 sc->num_permitted_opens, 0, sizeof(*sc->permitted_opens));
3843 sc->num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003844}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003845
Damien Miller9b439df2006-07-24 14:04:00 +10003846void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003847channel_clear_adm_permitted_opens(struct ssh *ssh)
Damien Miller9b439df2006-07-24 14:04:00 +10003848{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003849 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller9b439df2006-07-24 14:04:00 +10003850
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003851 sc->permitted_adm_opens = xrecallocarray(sc->permitted_adm_opens,
3852 sc->num_adm_permitted_opens, 0, sizeof(*sc->permitted_adm_opens));
3853 sc->num_adm_permitted_opens = 0;
Darren Tuckere7140f22008-06-10 23:01:51 +10003854}
3855
Darren Tucker1338b9e2011-10-02 18:57:35 +11003856/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3857int
3858permitopen_port(const char *p)
3859{
3860 int port;
3861
3862 if (strcmp(p, "*") == 0)
3863 return FWD_PERMIT_ANY_PORT;
3864 if ((port = a2port(p)) > 0)
3865 return port;
3866 return -1;
3867}
3868
Damien Millerbd740252008-05-19 15:37:09 +10003869/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003870static int
Damien Millerbd740252008-05-19 15:37:09 +10003871connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003872{
Damien Millerbd740252008-05-19 15:37:09 +10003873 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003874 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003875 char ntop[NI_MAXHOST];
3876 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003877
Damien Millerbd740252008-05-19 15:37:09 +10003878 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003879 switch (cctx->ai->ai_family) {
3880 case AF_UNIX:
3881 /* unix:pathname instead of host:port */
3882 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3883 strlcpy(ntop, "unix", sizeof(ntop));
3884 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3885 break;
3886 case AF_INET:
3887 case AF_INET6:
3888 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3889 ntop, sizeof(ntop), strport, sizeof(strport),
3890 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3891 error("connect_next: getnameinfo failed");
3892 continue;
3893 }
3894 break;
3895 default:
Damien Miller34132e52000-01-14 15:45:46 +11003896 continue;
3897 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003898 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3899 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003900 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003901 error("socket: %.100s", strerror(errno));
3902 else
3903 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003904 continue;
3905 }
Damien Miller232711f2004-06-15 10:35:30 +10003906 if (set_nonblock(sock) == -1)
3907 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003908 if (connect(sock, cctx->ai->ai_addr,
3909 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3910 debug("connect_next: host %.100s ([%.100s]:%s): "
3911 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003912 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003913 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003914 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003915 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003916 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003917 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003918 if (cctx->ai->ai_family != AF_UNIX)
3919 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003920 debug("connect_next: host %.100s ([%.100s]:%s) "
3921 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3922 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003923 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003924 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003925 return -1;
3926}
Damien Millerb38eff82000-04-01 11:09:21 +10003927
Damien Millerbd740252008-05-19 15:37:09 +10003928static void
3929channel_connect_ctx_free(struct channel_connect *cctx)
3930{
Darren Tuckera627d422013-06-02 07:31:17 +10003931 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003932 if (cctx->aitop) {
3933 if (cctx->aitop->ai_family == AF_UNIX)
3934 free(cctx->aitop);
3935 else
3936 freeaddrinfo(cctx->aitop);
3937 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003938 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003939}
3940
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003941/*
3942 * Return CONNECTING channel to remote host:port or local socket path,
3943 * passing back the failure reason if appropriate.
3944 */
Damien Millerbd740252008-05-19 15:37:09 +10003945static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003946connect_to_reason(struct ssh *ssh, const char *name, int port,
3947 char *ctype, char *rname, int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003948{
3949 struct addrinfo hints;
3950 int gaierr;
3951 int sock = -1;
3952 char strport[NI_MAXSERV];
3953 struct channel_connect cctx;
3954 Channel *c;
3955
Damien Miller2bcb8662008-07-12 17:12:29 +10003956 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003957
3958 if (port == PORT_STREAMLOCAL) {
3959 struct sockaddr_un *sunaddr;
3960 struct addrinfo *ai;
3961
3962 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3963 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3964 return (NULL);
3965 }
3966
3967 /*
3968 * Fake up a struct addrinfo for AF_UNIX connections.
3969 * channel_connect_ctx_free() must check ai_family
3970 * and use free() not freeaddirinfo() for AF_UNIX.
3971 */
3972 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3973 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3974 ai->ai_addr = (struct sockaddr *)(ai + 1);
3975 ai->ai_addrlen = sizeof(*sunaddr);
3976 ai->ai_family = AF_UNIX;
3977 ai->ai_socktype = SOCK_STREAM;
3978 ai->ai_protocol = PF_UNSPEC;
3979 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3980 sunaddr->sun_family = AF_UNIX;
3981 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3982 cctx.aitop = ai;
3983 } else {
3984 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003985 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller7acefbb2014-07-18 14:11:24 +10003986 hints.ai_socktype = SOCK_STREAM;
3987 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003988 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3989 != 0) {
3990 if (errmsg != NULL)
3991 *errmsg = ssh_gai_strerror(gaierr);
3992 if (reason != NULL)
3993 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003994 error("connect_to %.100s: unknown host (%s)", name,
3995 ssh_gai_strerror(gaierr));
3996 return NULL;
3997 }
Damien Millerbd740252008-05-19 15:37:09 +10003998 }
3999
Damien Miller7acefbb2014-07-18 14:11:24 +10004000 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10004001 cctx.port = port;
4002 cctx.ai = cctx.aitop;
4003
4004 if ((sock = connect_next(&cctx)) == -1) {
4005 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004006 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004007 channel_connect_ctx_free(&cctx);
4008 return NULL;
4009 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004010 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004011 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4012 c->connect_ctx = cctx;
4013 return c;
4014}
4015
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004016/* Return CONNECTING channel to remote host:port or local socket path */
4017static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004018connect_to(struct ssh *ssh, const char *name, int port,
4019 char *ctype, char *rname)
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004020{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004021 return connect_to_reason(ssh, name, port, ctype, rname, NULL, NULL);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004022}
4023
markus@openbsd.org8d057842016-09-30 09:19:13 +00004024/*
4025 * returns either the newly connected channel or the downstream channel
4026 * that needs to deal with this connection.
4027 */
Damien Millerbd740252008-05-19 15:37:09 +10004028Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004029channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004030 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004031{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004032 struct ssh_channels *sc = ssh->chanctxt;
4033 u_int i;
4034 ForwardPermission *fp;
Damien Millerbd740252008-05-19 15:37:09 +10004035
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004036 for (i = 0; i < sc->num_permitted_opens; i++) {
4037 fp = &sc->permitted_opens[i];
4038 if (open_listen_match_tcpip(fp, listen_host, listen_port, 1)) {
4039 if (fp->downstream)
4040 return fp->downstream;
4041 return connect_to(ssh,
4042 fp->host_to_connect, fp->port_to_connect,
4043 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004044 }
4045 }
4046 error("WARNING: Server requests forwarding for unknown listen_port %d",
4047 listen_port);
4048 return NULL;
4049}
4050
Damien Miller7acefbb2014-07-18 14:11:24 +10004051Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004052channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4053 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004054{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004055 struct ssh_channels *sc = ssh->chanctxt;
4056 u_int i;
4057 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004058
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004059 for (i = 0; i < sc->num_permitted_opens; i++) {
4060 fp = &sc->permitted_opens[i];
4061 if (open_listen_match_streamlocal(fp, path)) {
4062 return connect_to(ssh,
4063 fp->host_to_connect, fp->port_to_connect,
4064 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004065 }
4066 }
4067 error("WARNING: Server requests forwarding for unknown path %.100s",
4068 path);
4069 return NULL;
4070}
4071
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004072/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004073Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004074channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4075 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004076{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004077 struct ssh_channels *sc = ssh->chanctxt;
4078 u_int i, permit, permit_adm = 1;
4079 ForwardPermission *fp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004080
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004081 permit = sc->all_opens_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004082 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004083 for (i = 0; i < sc->num_permitted_opens; i++) {
4084 fp = &sc->permitted_opens[i];
4085 if (open_match(fp, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004086 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004087 break;
4088 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004089 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004090 }
Damien Miller9b439df2006-07-24 14:04:00 +10004091
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004092 if (sc->num_adm_permitted_opens > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004093 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004094 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4095 fp = &sc->permitted_adm_opens[i];
4096 if (open_match(fp, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004097 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004098 break;
4099 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004100 }
Damien Miller9b439df2006-07-24 14:04:00 +10004101 }
4102
4103 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004104 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004105 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004106 if (reason != NULL)
4107 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004108 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004109 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004110 return connect_to_reason(ssh, host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004111}
4112
Damien Miller7acefbb2014-07-18 14:11:24 +10004113/* Check if connecting to that path is permitted and connect. */
4114Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004115channel_connect_to_path(struct ssh *ssh, const char *path,
4116 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004117{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004118 struct ssh_channels *sc = ssh->chanctxt;
4119 u_int i, permit, permit_adm = 1;
4120 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004121
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004122 permit = sc->all_opens_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004123 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004124 for (i = 0; i < sc->num_permitted_opens; i++) {
4125 fp = &sc->permitted_opens[i];
4126 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004127 permit = 1;
4128 break;
4129 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004130 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004131 }
4132
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004133 if (sc->num_adm_permitted_opens > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004134 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004135 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4136 fp = &sc->permitted_adm_opens[i];
4137 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004138 permit_adm = 1;
4139 break;
4140 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004141 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004142 }
4143
4144 if (!permit || !permit_adm) {
4145 logit("Received request to connect to path %.100s, "
4146 "but the request was denied.", path);
4147 return NULL;
4148 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004149 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004150}
4151
Damien Miller0e220db2004-06-15 10:34:08 +10004152void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004153channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004154{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004155 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004156 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004157 int r;
4158 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004159
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004160 for (i = 0; i < sc->channels_alloc; i++) {
4161 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4162 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004163 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004164 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
Damien Miller0e220db2004-06-15 10:34:08 +10004165 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004166 channel_request_start(ssh, i, "window-change", 0);
4167 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4168 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4169 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4170 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4171 (r = sshpkt_send(ssh)) != 0)
4172 fatal("%s: channel %u: send window-change: %s",
4173 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004174 }
4175}
4176
Ben Lindstrome9c99912001-06-09 00:41:05 +00004177/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004178
Damien Miller5428f641999-11-25 11:54:57 +11004179/*
4180 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004181 * Returns 0 and a suitable display number for the DISPLAY variable
4182 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004183 */
Kevin Steves366298c2001-12-19 17:58:01 +00004184int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004185x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4186 int x11_use_localhost, int single_connection,
4187 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004188{
Damien Millere7378562001-12-21 14:58:35 +11004189 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004190 int display_number, sock;
4191 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004192 struct addrinfo hints, *ai, *aitop;
4193 char strport[NI_MAXSERV];
4194 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004195
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004196 if (chanids == NULL)
4197 return -1;
4198
Damien Millera34a28b1999-12-14 10:47:15 +11004199 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004200 display_number < MAX_DISPLAYS;
4201 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004202 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004203 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004204 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004205 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004206 hints.ai_socktype = SOCK_STREAM;
4207 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004208 if ((gaierr = getaddrinfo(NULL, strport,
4209 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004210 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004211 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004212 }
Damien Miller34132e52000-01-14 15:45:46 +11004213 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004214 if (ai->ai_family != AF_INET &&
4215 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004216 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004217 sock = socket(ai->ai_family, ai->ai_socktype,
4218 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004219 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004220 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4221#ifdef EPFNOSUPPORT
4222 && (errno != EPFNOSUPPORT)
4223#endif
4224 ) {
Damien Millere2192732000-01-17 13:22:55 +11004225 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004226 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004227 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004228 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004229 debug("x11_create_display_inet: Socket family %d not supported",
4230 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004231 continue;
4232 }
Damien Miller34132e52000-01-14 15:45:46 +11004233 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004234 if (ai->ai_family == AF_INET6)
4235 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004236 if (x11_use_localhost)
4237 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004238 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004239 debug2("%s: bind port %d: %.100s", __func__,
4240 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004241 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004242 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004243 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004244 num_socks = 0;
4245 break;
4246 }
4247 socks[num_socks++] = sock;
4248 if (num_socks == NUM_SOCKS)
4249 break;
Damien Miller95def091999-11-25 00:26:21 +11004250 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004251 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004252 if (num_socks > 0)
4253 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004254 }
Damien Miller95def091999-11-25 00:26:21 +11004255 if (display_number >= MAX_DISPLAYS) {
4256 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004257 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004258 }
Damien Miller95def091999-11-25 00:26:21 +11004259 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004260 for (n = 0; n < num_socks; n++) {
4261 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004262 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004263 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004264 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004265 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004266 }
Damien Miller95def091999-11-25 00:26:21 +11004267 }
Damien Miller34132e52000-01-14 15:45:46 +11004268
Damien Miller34132e52000-01-14 15:45:46 +11004269 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004270 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004271 for (n = 0; n < num_socks; n++) {
4272 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004273 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004274 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4275 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004276 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004277 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004278 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004279 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004280 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004281
Kevin Steves366298c2001-12-19 17:58:01 +00004282 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004283 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004284 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004285}
4286
Ben Lindstrombba81212001-06-25 05:01:22 +00004287static int
Damien Miller819dbb62009-01-21 16:46:26 +11004288connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004289{
Damien Miller95def091999-11-25 00:26:21 +11004290 int sock;
4291 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004292
Damien Miller3afe3752001-12-21 12:39:51 +11004293 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4294 if (sock < 0)
4295 error("socket: %.100s", strerror(errno));
4296 memset(&addr, 0, sizeof(addr));
4297 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004298 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004299 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004300 return sock;
4301 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004302 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4303 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004304}
4305
Damien Miller819dbb62009-01-21 16:46:26 +11004306static int
4307connect_local_xsocket(u_int dnr)
4308{
4309 char buf[1024];
4310 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4311 return connect_local_xsocket_path(buf);
4312}
4313
Darren Tuckerda39b092017-03-10 13:22:32 +11004314#ifdef __APPLE__
4315static int
4316is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4317{
4318 struct stat sbuf;
4319
4320 if (strlcpy(path, display, pathlen) >= pathlen) {
4321 error("%s: display path too long", __func__);
4322 return 0;
4323 }
4324 if (display[0] != '/')
4325 return 0;
4326 if (stat(path, &sbuf) == 0) {
4327 return 1;
4328 } else {
4329 char *dot = strrchr(path, '.');
4330 if (dot != NULL) {
4331 *dot = '\0';
4332 if (stat(path, &sbuf) == 0) {
4333 return 1;
4334 }
4335 }
4336 }
4337 return 0;
4338}
4339#endif
4340
Damien Millerbd483e72000-04-30 10:00:53 +10004341int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004342x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004343{
Damien Miller57c4e872006-03-31 23:11:07 +11004344 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004345 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004346 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004347 struct addrinfo hints, *ai, *aitop;
4348 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004349 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004350
Damien Miller95def091999-11-25 00:26:21 +11004351 /* Try to open a socket for the local X server. */
4352 display = getenv("DISPLAY");
4353 if (!display) {
4354 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004355 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004356 }
Damien Miller5428f641999-11-25 11:54:57 +11004357 /*
4358 * Now we decode the value of the DISPLAY variable and make a
4359 * connection to the real X server.
4360 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004361
Damien Miller819dbb62009-01-21 16:46:26 +11004362#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004363 /* Check if display is a path to a socket (as set by launchd). */
4364 {
4365 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004366
Darren Tuckerda39b092017-03-10 13:22:32 +11004367 if (is_path_to_xsocket(display, path, sizeof(path))) {
4368 debug("x11_connect_display: $DISPLAY is launchd");
4369
4370 /* Create a socket. */
4371 sock = connect_local_xsocket_path(path);
4372 if (sock < 0)
4373 return -1;
4374
4375 /* OK, we now have a connection to the display. */
4376 return sock;
4377 }
Damien Miller819dbb62009-01-21 16:46:26 +11004378 }
4379#endif
Damien Miller5428f641999-11-25 11:54:57 +11004380 /*
4381 * Check if it is a unix domain socket. Unix domain displays are in
4382 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4383 */
Damien Miller95def091999-11-25 00:26:21 +11004384 if (strncmp(display, "unix:", 5) == 0 ||
4385 display[0] == ':') {
4386 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004387 if (sscanf(strrchr(display, ':') + 1, "%u",
4388 &display_number) != 1) {
4389 error("Could not parse display number from DISPLAY: "
4390 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004391 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004392 }
4393 /* Create a socket. */
4394 sock = connect_local_xsocket(display_number);
4395 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004396 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004397
4398 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004399 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004400 }
Damien Miller5428f641999-11-25 11:54:57 +11004401 /*
4402 * Connect to an inet socket. The DISPLAY value is supposedly
4403 * hostname:d[.s], where hostname may also be numeric IP address.
4404 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004405 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004406 cp = strchr(buf, ':');
4407 if (!cp) {
4408 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004409 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004410 }
Damien Miller95def091999-11-25 00:26:21 +11004411 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004412 /*
4413 * buf now contains the host name. But first we parse the
4414 * display number.
4415 */
Damien Miller57c4e872006-03-31 23:11:07 +11004416 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004417 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004418 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004419 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004420 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004421
Damien Miller34132e52000-01-14 15:45:46 +11004422 /* Look up the host address */
4423 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004424 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004425 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004426 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004427 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004428 error("%.100s: unknown host. (%s)", buf,
4429 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004430 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004431 }
Damien Miller34132e52000-01-14 15:45:46 +11004432 for (ai = aitop; ai; ai = ai->ai_next) {
4433 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004434 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004435 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004436 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004437 continue;
4438 }
4439 /* Connect it to the display. */
4440 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004441 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004442 6000 + display_number, strerror(errno));
4443 close(sock);
4444 continue;
4445 }
4446 /* Success */
4447 break;
Damien Miller34132e52000-01-14 15:45:46 +11004448 }
Damien Miller34132e52000-01-14 15:45:46 +11004449 freeaddrinfo(aitop);
4450 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004451 error("connect %.100s port %u: %.100s", buf,
4452 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004453 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004454 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004455 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004456 return sock;
4457}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004458
Damien Millerbd483e72000-04-30 10:00:53 +10004459/*
Damien Miller5428f641999-11-25 11:54:57 +11004460 * Requests forwarding of X11 connections, generates fake authentication
4461 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004462 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004463 */
Damien Miller4af51302000-04-16 11:18:38 +10004464void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004465x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4466 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004467{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004468 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004469 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004470 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004471 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004472 char *new_data;
4473 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004474
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004475 if (sc->x11_saved_display == NULL)
4476 sc->x11_saved_display = xstrdup(disp);
4477 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004478 error("x11_request_forwarding_with_spoofing: different "
4479 "$DISPLAY already forwarded");
4480 return;
4481 }
4482
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004483 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004484 if (cp)
4485 cp = strchr(cp, '.');
4486 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004487 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004488 else
4489 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004490
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004491 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004492 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004493 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004494
4495 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004496 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004497 for (i = 0; i < data_len; i++) {
4498 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4499 fatal("x11_request_forwarding: bad "
4500 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004501 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004502 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004503 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004504
4505 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004506 sc->x11_fake_data = xmalloc(data_len);
4507 arc4random_buf(sc->x11_fake_data, data_len);
4508 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004509 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004510
Damien Miller95def091999-11-25 00:26:21 +11004511 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004512 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004513
Damien Miller95def091999-11-25 00:26:21 +11004514 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004515 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4516 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4517 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4518 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4519 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4520 (r = sshpkt_send(ssh)) != 0 ||
4521 (r = ssh_packet_write_wait(ssh)) != 0)
4522 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004523 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004524}