blob: 3ab4823a98ac3dbf5b3f498a5fd66b950b721f5f [file] [log] [blame]
djm@openbsd.org9f532292017-09-12 06:35:31 +00001/* $OpenBSD: channels.c,v 1.368 2017/09/12 06:35:31 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));
1839 } else {
1840 buf = data = sshbuf_mutable_ptr(c->output);
1841 dlen = sshbuf_len(c->output);
1842 }
1843
1844 if (c->datagram) {
1845 /* ignore truncated writes, datagrams might get lost */
1846 len = write(c->wfd, data, dlen);
1847 free(data);
1848 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1849 errno == EWOULDBLOCK))
1850 return 1;
1851 if (len <= 0)
1852 goto write_fail;
1853 goto out;
1854 }
1855
1856#ifdef _AIX
1857 /* XXX: Later AIX versions can't push as much data to tty */
1858 if (c->wfd_isatty)
1859 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10001860#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001861
1862 len = write(c->wfd, buf, dlen);
1863 if (len < 0 &&
1864 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
1865 return 1;
1866 if (len <= 0) {
1867 write_fail:
1868 if (c->type != SSH_CHANNEL_OPEN) {
1869 debug2("channel %d: not open", c->self);
1870 chan_mark_dead(ssh, c);
1871 return -1;
1872 } else {
1873 chan_write_failed(ssh, c);
1874 }
1875 return -1;
1876 }
1877#ifndef BROKEN_TCGETATTR_ICANON
1878 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
1879 if (tcgetattr(c->wfd, &tio) == 0 &&
1880 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1881 /*
1882 * Simulate echo to reduce the impact of
1883 * traffic analysis. We need to match the
1884 * size of a SSH2_MSG_CHANNEL_DATA message
1885 * (4 byte channel id + buf)
1886 */
1887 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
1888 (r = sshpkt_send(ssh)) != 0)
1889 fatal("%s: channel %d: ignore: %s",
1890 __func__, c->self, ssh_err(r));
1891 }
1892 }
1893#endif /* BROKEN_TCGETATTR_ICANON */
1894 if ((r = sshbuf_consume(c->output, len)) != 0) {
1895 fatal("%s: channel %d: consume: %s",
1896 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001897 }
Damien Miller7d457182010-08-05 23:09:48 +10001898 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001899 c->local_consumed += olen - sshbuf_len(c->output);
1900
Damien Miller33b13562000-04-04 14:38:59 +10001901 return 1;
1902}
Damien Miller4f7becb2006-03-26 14:10:14 +11001903
Ben Lindstrombba81212001-06-25 05:01:22 +00001904static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001905channel_handle_efd_write(struct ssh *ssh, Channel *c,
1906 fd_set *readset, fd_set *writeset)
1907{
1908 int r;
1909 ssize_t len;
1910
1911 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
1912 return 1;
1913
1914 len = write(c->efd, sshbuf_ptr(c->extended),
1915 sshbuf_len(c->extended));
1916 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
1917 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1918 errno == EWOULDBLOCK))
1919 return 1;
1920 if (len <= 0) {
1921 debug2("channel %d: closing write-efd %d", c->self, c->efd);
1922 channel_close_fd(ssh, &c->efd);
1923 } else {
1924 if ((r = sshbuf_consume(c->extended, len)) != 0) {
1925 fatal("%s: channel %d: consume: %s",
1926 __func__, c->self, ssh_err(r));
1927 }
1928 c->local_consumed += len;
1929 }
1930 return 1;
1931}
1932
1933static int
1934channel_handle_efd_read(struct ssh *ssh, Channel *c,
1935 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001936{
Darren Tucker11327cc2005-03-14 23:22:25 +11001937 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001938 int r;
1939 ssize_t len;
Damien Miller33b13562000-04-04 14:38:59 +10001940
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001941 if (!c->detach_close && !FD_ISSET(c->efd, readset))
1942 return 1;
1943
1944 len = read(c->efd, buf, sizeof(buf));
1945 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
1946 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1947 errno == EWOULDBLOCK) && !c->detach_close)))
1948 return 1;
1949 if (len <= 0) {
1950 debug2("channel %d: closing read-efd %d",
1951 c->self, c->efd);
1952 channel_close_fd(ssh, &c->efd);
1953 } else {
1954 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1955 debug3("channel %d: discard efd",
1956 c->self);
1957 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
1958 fatal("%s: channel %d: append: %s",
1959 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001960 }
1961 }
1962 return 1;
1963}
Damien Miller4f7becb2006-03-26 14:10:14 +11001964
Damien Miller0e220db2004-06-15 10:34:08 +10001965static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001966channel_handle_efd(struct ssh *ssh, Channel *c,
1967 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001968{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001969 if (c->efd == -1)
1970 return 1;
1971
1972 /** XXX handle drain efd, too */
1973
1974 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1975 return channel_handle_efd_write(ssh, c, readset, writeset);
1976 else if (c->extended_usage == CHAN_EXTENDED_READ ||
1977 c->extended_usage == CHAN_EXTENDED_IGNORE)
1978 return channel_handle_efd_read(ssh, c, readset, writeset);
1979
1980 return 1;
1981}
1982
1983static int
1984channel_check_window(struct ssh *ssh, Channel *c)
1985{
1986 int r;
1987
Ben Lindstromb3921512001-04-11 15:57:50 +00001988 if (c->type == SSH_CHANNEL_OPEN &&
1989 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001990 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001991 c->local_maxpacket*3) ||
1992 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001993 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00001994 if (!c->have_remote_id)
1995 fatal(":%s: channel %d: no remote id",
1996 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001997 if ((r = sshpkt_start(ssh,
1998 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1999 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2000 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2001 (r = sshpkt_send(ssh)) != 0) {
2002 fatal("%s: channel %i: %s", __func__,
2003 c->self, ssh_err(r));
2004 }
Damien Millerd3444942000-09-30 14:20:03 +11002005 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002006 c->self, c->local_window,
2007 c->local_consumed);
2008 c->local_window += c->local_consumed;
2009 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002010 }
2011 return 1;
2012}
2013
Ben Lindstrombba81212001-06-25 05:01:22 +00002014static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002015channel_post_open(struct ssh *ssh, Channel *c,
2016 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002017{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002018 channel_handle_rfd(ssh, c, readset, writeset);
2019 channel_handle_wfd(ssh, c, readset, writeset);
2020 channel_handle_efd(ssh, c, readset, writeset);
2021 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002022}
2023
Damien Millere1537f92010-01-26 13:26:22 +11002024static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002025read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002026{
2027 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002028 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002029 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002030 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002031
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002032 if (sshbuf_len(c->input) < need) {
2033 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002034 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002035 if (len < 0 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002036 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002037 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002038 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002039 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002040 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002041 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002042 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2043 fatal("%s: channel %d: append: %s",
2044 __func__, c->self, ssh_err(r));
2045 }
Damien Millere1537f92010-01-26 13:26:22 +11002046 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002047 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002048}
2049
2050static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002051channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2052 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002053{
2054 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002055
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002056 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2057 return;
2058 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2059 return;
2060 if (c->mux_pause)
2061 return;
2062
2063 /*
2064 * Don't not read past the precise end of packets to
2065 * avoid disrupting fd passing.
2066 */
2067 if (read_mux(ssh, c, 4) < 4) /* read header */
2068 return;
2069 /* XXX sshbuf_peek_u32 */
2070 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002071#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002072 if (need > CHANNEL_MUX_MAX_PACKET) {
2073 debug2("channel %d: packet too big %u > %u",
2074 c->self, CHANNEL_MUX_MAX_PACKET, need);
2075 chan_rcvd_oclose(ssh, c);
2076 return;
Damien Millere1537f92010-01-26 13:26:22 +11002077 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002078 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2079 return;
2080 if (c->mux_rcb(ssh, c) != 0) {
2081 debug("channel %d: mux_rcb failed", c->self);
2082 chan_mark_dead(ssh, c);
2083 return;
Damien Millere1537f92010-01-26 13:26:22 +11002084 }
2085}
2086
2087static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002088channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2089 fd_set *readset, fd_set *writeset)
2090{
2091 ssize_t len;
2092 int r;
2093
2094 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2095 sshbuf_len(c->output) == 0)
2096 return;
2097
2098 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2099 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2100 return;
2101 if (len <= 0) {
2102 chan_mark_dead(ssh, c);
2103 return;
2104 }
2105 if ((r = sshbuf_consume(c->output, len)) != 0)
2106 fatal("%s: channel %d: consume: %s", __func__,
2107 c->self, ssh_err(r));
2108}
2109
2110static void
2111channel_post_mux_client(struct ssh *ssh, Channel *c,
2112 fd_set *readset, fd_set *writeset)
2113{
2114 channel_post_mux_client_read(ssh, c, readset, writeset);
2115 channel_post_mux_client_write(ssh, c, readset, writeset);
2116}
2117
2118static void
2119channel_post_mux_listener(struct ssh *ssh, Channel *c,
2120 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002121{
2122 Channel *nc;
2123 struct sockaddr_storage addr;
2124 socklen_t addrlen;
2125 int newsock;
2126 uid_t euid;
2127 gid_t egid;
2128
2129 if (!FD_ISSET(c->sock, readset))
2130 return;
2131
2132 debug("multiplexing control connection");
2133
2134 /*
2135 * Accept connection on control socket
2136 */
2137 memset(&addr, 0, sizeof(addr));
2138 addrlen = sizeof(addr);
2139 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2140 &addrlen)) == -1) {
2141 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002142 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002143 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002144 return;
2145 }
2146
2147 if (getpeereid(newsock, &euid, &egid) < 0) {
2148 error("%s getpeereid failed: %s", __func__,
2149 strerror(errno));
2150 close(newsock);
2151 return;
2152 }
2153 if ((euid != 0) && (getuid() != euid)) {
2154 error("multiplex uid mismatch: peer euid %u != uid %u",
2155 (u_int)euid, (u_int)getuid());
2156 close(newsock);
2157 return;
2158 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002159 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002160 newsock, newsock, -1, c->local_window_max,
2161 c->local_maxpacket, 0, "mux-control", 1);
2162 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002163 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002164 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002165 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002166 /* mux state transitions must not elicit protocol messages */
2167 nc->flags |= CHAN_LOCAL;
2168}
2169
Ben Lindstrombba81212001-06-25 05:01:22 +00002170static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002171channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002172{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002173 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002174
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002175 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2176 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2177 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002178
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002179 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2180 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2181 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2182 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2183 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2184 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2185 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2186 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2187 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2188 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
2189 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2190 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2191
2192 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2193 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2194 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2195 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2196 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2197 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2198 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2199 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2200 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
2201 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2202 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2203
2204 sc->channel_pre = pre;
2205 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002206}
2207
Damien Miller3ec27592001-10-12 11:35:04 +10002208/* gc dead channels */
2209static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002210channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002211{
2212 if (c == NULL)
2213 return;
2214 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002215 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002216 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002217 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002218 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002219 /* if we still have a callback */
2220 if (c->detach_user != NULL)
2221 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002222 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002223 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002224 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002225 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002226 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002227 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002228}
2229
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002230enum channel_table { CHAN_PRE, CHAN_POST };
2231
Ben Lindstrombba81212001-06-25 05:01:22 +00002232static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002233channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002234 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002235{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002236 struct ssh_channels *sc = ssh->chanctxt;
2237 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002238 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002239 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002240 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002241
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002242 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002243 if (unpause_secs != NULL)
2244 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002245 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2246 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002247 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002248 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002249 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002250 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002251 c->delayed = 0;
2252 else
2253 continue;
2254 }
Damien Millera6508752012-04-22 11:21:10 +10002255 if (ftab[c->type] != NULL) {
2256 /*
2257 * Run handlers that are not paused.
2258 */
2259 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002260 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002261 else if (unpause_secs != NULL) {
2262 /*
2263 * Collect the time that the earliest
2264 * channel comes off pause.
2265 */
2266 debug3("%s: chan %d: skip for %d more seconds",
2267 __func__, c->self,
2268 (int)(c->notbefore - now));
2269 if (*unpause_secs == 0 ||
2270 (c->notbefore - now) < *unpause_secs)
2271 *unpause_secs = c->notbefore - now;
2272 }
2273 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002274 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002275 }
Damien Millera6508752012-04-22 11:21:10 +10002276 if (unpause_secs != NULL && *unpause_secs != 0)
2277 debug3("%s: first channel unpauses in %d seconds",
2278 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002279}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002280
Ben Lindstrome9c99912001-06-09 00:41:05 +00002281/*
2282 * Allocate/update select bitmasks and add any bits relevant to channels in
2283 * select bitmasks.
2284 */
Damien Miller4af51302000-04-16 11:18:38 +10002285void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002286channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2287 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002288{
Damien Miller36812092006-03-26 14:22:47 +11002289 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002290
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002291 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002292
Damien Miller36812092006-03-26 14:22:47 +11002293 nfdset = howmany(n+1, NFDBITS);
2294 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002295 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002296 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2297 sz = nfdset * sizeof(fd_mask);
2298
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002299 /* perhaps check sz < nalloc/2 and shrink? */
2300 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002301 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2302 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002303 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002304 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002305 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002306 memset(*readsetp, 0, sz);
2307 memset(*writesetp, 0, sz);
2308
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002309 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002310 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002311 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002312}
2313
Ben Lindstrome9c99912001-06-09 00:41:05 +00002314/*
2315 * After select, perform any appropriate operations for channels which have
2316 * events pending.
2317 */
Damien Miller4af51302000-04-16 11:18:38 +10002318void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002319channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002321 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002322}
2323
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002324/*
2325 * Enqueue data for channels with open or draining c->input.
2326 */
2327static void
2328channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2329{
2330 size_t len, dlen;
2331 int r;
2332
2333 if ((len = sshbuf_len(c->input)) == 0) {
2334 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2335 /*
2336 * input-buffer is empty and read-socket shutdown:
2337 * tell peer, that we will not send more data:
2338 * send IEOF.
2339 * hack for extended data: delay EOF if EFD still
2340 * in use.
2341 */
2342 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2343 debug2("channel %d: "
2344 "ibuf_empty delayed efd %d/(%zu)",
2345 c->self, c->efd, sshbuf_len(c->extended));
2346 else
2347 chan_ibuf_empty(ssh, c);
2348 }
2349 return;
2350 }
2351
djm@openbsd.org9f532292017-09-12 06:35:31 +00002352 if (!c->have_remote_id)
2353 fatal(":%s: channel %d: no remote id", __func__, c->self);
2354
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002355 if (c->datagram) {
2356 /* Check datagram will fit; drop if not */
2357 if ((r = sshbuf_peek_string_direct(c->input, NULL, &dlen)) != 0)
2358 fatal("%s: channel %d: peek datagram: %s", __func__,
2359 c->self, ssh_err(r));
2360 /*
2361 * XXX this does tail-drop on the datagram queue which is
2362 * usually suboptimal compared to head-drop. Better to have
2363 * backpressure at read time? (i.e. read + discard)
2364 */
2365 if (dlen > c->remote_window || dlen > c->remote_maxpacket) {
2366 debug("channel %d: datagram too big", c->self);
2367 return;
2368 }
2369 /* Enqueue it */
2370 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2371 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2372 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
2373 (r = sshpkt_send(ssh)) != 0) {
2374 fatal("%s: channel %i: datagram: %s", __func__,
2375 c->self, ssh_err(r));
2376 }
2377 c->remote_window -= dlen;
2378 return;
2379 }
2380
2381 /* Enqueue packet for buffered data. */
2382 if (len > c->remote_window)
2383 len = c->remote_window;
2384 if (len > c->remote_maxpacket)
2385 len = c->remote_maxpacket;
2386 if (len == 0)
2387 return;
2388 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2389 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2390 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2391 (r = sshpkt_send(ssh)) != 0) {
2392 fatal("%s: channel %i: data: %s", __func__,
2393 c->self, ssh_err(r));
2394 }
2395 if ((r = sshbuf_consume(c->input, len)) != 0)
2396 fatal("%s: channel %i: consume: %s", __func__,
2397 c->self, ssh_err(r));
2398 c->remote_window -= len;
2399}
2400
2401/*
2402 * Enqueue data for channels with open c->extended in read mode.
2403 */
2404static void
2405channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2406{
2407 size_t len;
2408 int r;
2409
2410 if ((len = sshbuf_len(c->extended)) == 0)
2411 return;
2412
2413 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2414 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2415 if (len > c->remote_window)
2416 len = c->remote_window;
2417 if (len > c->remote_maxpacket)
2418 len = c->remote_maxpacket;
2419 if (len == 0)
2420 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002421 if (!c->have_remote_id)
2422 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002423 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2424 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2425 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2426 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2427 (r = sshpkt_send(ssh)) != 0) {
2428 fatal("%s: channel %i: data: %s", __func__,
2429 c->self, ssh_err(r));
2430 }
2431 if ((r = sshbuf_consume(c->extended, len)) != 0)
2432 fatal("%s: channel %i: consume: %s", __func__,
2433 c->self, ssh_err(r));
2434 c->remote_window -= len;
2435 debug2("channel %d: sent ext data %zu", c->self, len);
2436}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002437
Damien Miller5e953212001-01-30 09:14:00 +11002438/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002439void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002440channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002441{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002442 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002443 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002444 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002445
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002446 for (i = 0; i < sc->channels_alloc; i++) {
2447 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002448 if (c == NULL)
2449 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002450
Ben Lindstrome9c99912001-06-09 00:41:05 +00002451 /*
2452 * We are only interested in channels that can have buffered
2453 * incoming data.
2454 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002455 if (c->type != SSH_CHANNEL_OPEN)
2456 continue;
2457 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002458 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002459 debug3("channel %d: will not send data after close",
2460 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002461 continue;
2462 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002463
Damien Miller95def091999-11-25 00:26:21 +11002464 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002465 if (c->istate == CHAN_INPUT_OPEN ||
2466 c->istate == CHAN_INPUT_WAIT_DRAIN)
2467 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002468 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002469 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002470 c->extended_usage == CHAN_EXTENDED_READ)
2471 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002472 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002473}
2474
markus@openbsd.org8d057842016-09-30 09:19:13 +00002475/* -- mux proxy support */
2476
2477/*
2478 * When multiplexing channel messages for mux clients we have to deal
2479 * with downstream messages from the mux client and upstream messages
2480 * from the ssh server:
2481 * 1) Handling downstream messages is straightforward and happens
2482 * in channel_proxy_downstream():
2483 * - We forward all messages (mostly) unmodified to the server.
2484 * - However, in order to route messages from upstream to the correct
2485 * downstream client, we have to replace the channel IDs used by the
2486 * mux clients with a unique channel ID because the mux clients might
2487 * use conflicting channel IDs.
2488 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2489 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2490 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2491 * with the newly allocated channel ID.
2492 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2493 * channels and procesed by channel_proxy_upstream(). The local channel ID
2494 * is then translated back to the original mux client ID.
2495 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2496 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2497 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2498 * downstream mux client are removed.
2499 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2500 * requires more work, because they are not addressed to a specific
2501 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2502 * out whether the request is addressed to the local client or a
2503 * specific downstream client based on the listen-address/port.
2504 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2505 * not supported as the matching session/channel cannot be identified
2506 * easily.
2507 */
2508
2509/*
2510 * receive packets from downstream mux clients:
2511 * channel callback fired on read from mux client, creates
2512 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2513 * on channel creation.
2514 */
2515int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002516channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002517{
2518 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002519 struct sshbuf *original = NULL, *modified = NULL;
2520 const u_char *cp;
2521 char *ctype = NULL, *listen_host = NULL;
2522 u_char type;
2523 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002524 int ret = -1, r, idx;
2525 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002526
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002527 /* sshbuf_dump(downstream->input, stderr); */
2528 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002529 != 0) {
2530 error("%s: malformed message: %s", __func__, ssh_err(r));
2531 return -1;
2532 }
2533 if (have < 2) {
2534 error("%s: short message", __func__);
2535 return -1;
2536 }
2537 type = cp[1];
2538 /* skip padlen + type */
2539 cp += 2;
2540 have -= 2;
2541 if (ssh_packet_log_type(type))
2542 debug3("%s: channel %u: down->up: type %u", __func__,
2543 downstream->self, type);
2544
2545 switch (type) {
2546 case SSH2_MSG_CHANNEL_OPEN:
2547 if ((original = sshbuf_from(cp, have)) == NULL ||
2548 (modified = sshbuf_new()) == NULL) {
2549 error("%s: alloc", __func__);
2550 goto out;
2551 }
2552 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2553 (r = sshbuf_get_u32(original, &id)) != 0) {
2554 error("%s: parse error %s", __func__, ssh_err(r));
2555 goto out;
2556 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002557 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002558 -1, -1, -1, 0, 0, 0, ctype, 1);
2559 c->mux_ctx = downstream; /* point to mux client */
2560 c->mux_downstream_id = id; /* original downstream id */
2561 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2562 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2563 (r = sshbuf_putb(modified, original)) != 0) {
2564 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002565 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002566 goto out;
2567 }
2568 break;
2569 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2570 /*
2571 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2572 * need to parse 'remote_id' instead of 'ctype'.
2573 */
2574 if ((original = sshbuf_from(cp, have)) == NULL ||
2575 (modified = sshbuf_new()) == NULL) {
2576 error("%s: alloc", __func__);
2577 goto out;
2578 }
2579 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2580 (r = sshbuf_get_u32(original, &id)) != 0) {
2581 error("%s: parse error %s", __func__, ssh_err(r));
2582 goto out;
2583 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002584 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002585 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2586 c->mux_ctx = downstream; /* point to mux client */
2587 c->mux_downstream_id = id;
2588 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002589 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002590 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2591 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2592 (r = sshbuf_putb(modified, original)) != 0) {
2593 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002594 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002595 goto out;
2596 }
2597 break;
2598 case SSH2_MSG_GLOBAL_REQUEST:
2599 if ((original = sshbuf_from(cp, have)) == NULL) {
2600 error("%s: alloc", __func__);
2601 goto out;
2602 }
2603 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2604 error("%s: parse error %s", __func__, ssh_err(r));
2605 goto out;
2606 }
2607 if (strcmp(ctype, "tcpip-forward") != 0) {
2608 error("%s: unsupported request %s", __func__, ctype);
2609 goto out;
2610 }
2611 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2612 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2613 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2614 error("%s: parse error %s", __func__, ssh_err(r));
2615 goto out;
2616 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002617 if (listen_port > 65535) {
2618 error("%s: tcpip-forward for %s: bad port %u",
2619 __func__, listen_host, listen_port);
2620 goto out;
2621 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002622 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002623 idx = fwd_perm_list_add(ssh, FWDPERM_USER, "<mux>", -1,
2624 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002625 listen_host = NULL;
2626 break;
2627 case SSH2_MSG_CHANNEL_CLOSE:
2628 if (have < 4)
2629 break;
2630 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002631 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002632 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002633 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002634 else
2635 c->flags |= CHAN_CLOSE_SENT;
2636 }
2637 break;
2638 }
2639 if (modified) {
2640 if ((r = sshpkt_start(ssh, type)) != 0 ||
2641 (r = sshpkt_putb(ssh, modified)) != 0 ||
2642 (r = sshpkt_send(ssh)) != 0) {
2643 error("%s: send %s", __func__, ssh_err(r));
2644 goto out;
2645 }
2646 } else {
2647 if ((r = sshpkt_start(ssh, type)) != 0 ||
2648 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2649 (r = sshpkt_send(ssh)) != 0) {
2650 error("%s: send %s", __func__, ssh_err(r));
2651 goto out;
2652 }
2653 }
2654 ret = 0;
2655 out:
2656 free(ctype);
2657 free(listen_host);
2658 sshbuf_free(original);
2659 sshbuf_free(modified);
2660 return ret;
2661}
2662
2663/*
2664 * receive packets from upstream server and de-multiplex packets
2665 * to correct downstream:
2666 * implemented as a helper for channel input handlers,
2667 * replaces local (proxy) channel ID with downstream channel ID.
2668 */
2669int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002670channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002671{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002672 struct sshbuf *b = NULL;
2673 Channel *downstream;
2674 const u_char *cp = NULL;
2675 size_t len;
2676 int r;
2677
2678 /*
2679 * When receiving packets from the peer we need to check whether we
2680 * need to forward the packets to the mux client. In this case we
2681 * restore the orignal channel id and keep track of CLOSE messages,
2682 * so we can cleanup the channel.
2683 */
2684 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2685 return 0;
2686 if ((downstream = c->mux_ctx) == NULL)
2687 return 0;
2688 switch (type) {
2689 case SSH2_MSG_CHANNEL_CLOSE:
2690 case SSH2_MSG_CHANNEL_DATA:
2691 case SSH2_MSG_CHANNEL_EOF:
2692 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2693 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2694 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2695 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2696 case SSH2_MSG_CHANNEL_SUCCESS:
2697 case SSH2_MSG_CHANNEL_FAILURE:
2698 case SSH2_MSG_CHANNEL_REQUEST:
2699 break;
2700 default:
2701 debug2("%s: channel %u: unsupported type %u", __func__,
2702 c->self, type);
2703 return 0;
2704 }
2705 if ((b = sshbuf_new()) == NULL) {
2706 error("%s: alloc reply", __func__);
2707 goto out;
2708 }
2709 /* get remaining payload (after id) */
2710 cp = sshpkt_ptr(ssh, &len);
2711 if (cp == NULL) {
2712 error("%s: no packet", __func__);
2713 goto out;
2714 }
2715 /* translate id and send to muxclient */
2716 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2717 (r = sshbuf_put_u8(b, type)) != 0 ||
2718 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2719 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002720 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002721 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2722 goto out;
2723 }
2724 /* sshbuf_dump(b, stderr); */
2725 if (ssh_packet_log_type(type))
2726 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2727 type);
2728 out:
2729 /* update state */
2730 switch (type) {
2731 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2732 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002733 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002734 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002735 c->have_remote_id = 1;
2736 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002737 break;
2738 case SSH2_MSG_CHANNEL_CLOSE:
2739 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002740 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002741 else
2742 c->flags |= CHAN_CLOSE_RCVD;
2743 break;
2744 }
2745 sshbuf_free(b);
2746 return 1;
2747}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002748
2749/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002750
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002751/* Parse a channel ID from the current packet */
2752static int
2753channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2754{
2755 u_int32_t id;
2756 int r;
2757
2758 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2759 error("%s: parse id: %s", where, ssh_err(r));
2760 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2761 }
2762 if (id > INT_MAX) {
2763 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2764 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2765 }
2766 return (int)id;
2767}
2768
2769/* Lookup a channel from an ID in the current packet */
2770static Channel *
2771channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2772{
2773 int id = channel_parse_id(ssh, where, what);
2774 Channel *c;
2775
2776 if ((c = channel_lookup(ssh, id)) == NULL) {
2777 ssh_packet_disconnect(ssh,
2778 "%s packet referred to nonexistent channel %d", what, id);
2779 }
2780 return c;
2781}
2782
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002783int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002784channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002785{
Damien Miller633de332014-05-15 13:48:26 +10002786 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002787 size_t data_len, win_len;
2788 Channel *c = channel_from_packet_id(ssh, __func__, "data");
2789 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002790
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002791 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002792 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002793
Damien Miller95def091999-11-25 00:26:21 +11002794 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002795 if (c->type != SSH_CHANNEL_OPEN &&
2796 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002797 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002798
Damien Miller95def091999-11-25 00:26:21 +11002799 /* Get the data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002800 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0)
2801 fatal("%s: channel %d: get data: %s", __func__,
2802 c->self, ssh_err(r));
2803 ssh_packet_check_eom(ssh);
2804
Damien Miller7d457182010-08-05 23:09:48 +10002805 win_len = data_len;
2806 if (c->datagram)
2807 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002808
Damien Millera04ad492004-01-21 11:02:09 +11002809 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002810 * The sending side reduces its window as it sends data, so we
2811 * must 'fake' consumption of the data in order to ensure that window
2812 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11002813 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002814 if (c->ostate != CHAN_OUTPUT_OPEN) {
2815 c->local_window -= win_len;
2816 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002817 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002818 }
2819
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002820 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002821 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002822 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002823 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002824 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002825 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002826 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002827 c->self, win_len, c->local_window);
2828 return 0;
2829 }
2830 c->local_window -= win_len;
2831
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002832 if (c->datagram) {
2833 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
2834 fatal("%s: channel %d: append datagram: %s",
2835 __func__, c->self, ssh_err(r));
2836 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
2837 fatal("%s: channel %d: append data: %s",
2838 __func__, c->self, ssh_err(r));
2839
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002840 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002841}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002842
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002843int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002844channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002845{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002846 const u_char *data;
2847 size_t data_len;
2848 u_int32_t tcode;
2849 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
2850 int r;
Damien Miller33b13562000-04-04 14:38:59 +10002851
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002852 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002853 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002854 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002855 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002856 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002857 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002858 if (c->flags & CHAN_EOF_RCVD) {
2859 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002860 debug("channel %d: accepting ext data after eof",
2861 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002862 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002863 ssh_packet_disconnect(ssh, "Received extended_data "
2864 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002865 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002866
2867 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
2868 error("%s: parse tcode: %s", __func__, ssh_err(r));
2869 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2870 }
Damien Miller33b13562000-04-04 14:38:59 +10002871 if (c->efd == -1 ||
2872 c->extended_usage != CHAN_EXTENDED_WRITE ||
2873 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002874 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002875 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002876 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002877 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) {
2878 error("%s: parse data: %s", __func__, ssh_err(r));
2879 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2880 }
2881 ssh_packet_check_eom(ssh);
2882
Damien Miller33b13562000-04-04 14:38:59 +10002883 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002884 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10002885 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002886 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002887 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002888 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
2889 /* XXX sshpkt_getb? */
2890 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
2891 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002892 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002893 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002894}
2895
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002896int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002897channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10002898{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002899 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
Damien Millerb38eff82000-04-01 11:09:21 +10002900
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002901 ssh_packet_check_eom(ssh);
2902
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002903 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002904 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002905 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002906
2907 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002908 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002909 debug("channel %d: FORCE input drain", c->self);
2910 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002911 if (sshbuf_len(c->input) == 0)
2912 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002913 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002914 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002915}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002916
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002917int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002918channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002919{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002920 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
Damien Miller66823cd2002-01-22 23:11:38 +11002921
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002922 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002923 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002924 ssh_packet_check_eom(ssh);
2925 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002926 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002927}
2928
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002929int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002930channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002932 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
2933 u_int32_t remote_window, remote_maxpacket;
2934 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002935
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002936 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002937 return 0;
2938 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002939 packet_disconnect("Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002940 "non-opening channel %d.", c->self);
2941 /*
2942 * Record the remote channel number and mark that the channel
2943 * is now open.
2944 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002945 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
2946 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002947 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) {
2948 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
2949 packet_disconnect("Invalid open confirmation message");
2950 }
2951 ssh_packet_check_eom(ssh);
Damien Miller33b13562000-04-04 14:38:59 +10002952
djm@openbsd.org9f532292017-09-12 06:35:31 +00002953 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002954 c->remote_window = remote_window;
2955 c->remote_maxpacket = remote_maxpacket;
2956 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002957 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002958 debug2("%s: channel %d: callback start", __func__, c->self);
2959 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
2960 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002961 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002962 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2963 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002964 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002965}
2966
Ben Lindstrombba81212001-06-25 05:01:22 +00002967static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002968reason2txt(int reason)
2969{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002970 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002971 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2972 return "administratively prohibited";
2973 case SSH2_OPEN_CONNECT_FAILED:
2974 return "connect failed";
2975 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2976 return "unknown channel type";
2977 case SSH2_OPEN_RESOURCE_SHORTAGE:
2978 return "resource shortage";
2979 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002980 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002981}
2982
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002983int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002984channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002985{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002986 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
2987 u_int32_t reason;
2988 char *msg = NULL;
2989 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002990
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002991 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002992 return 0;
2993 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002994 packet_disconnect("Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002995 "non-opening channel %d.", c->self);
2996 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
2997 error("%s: reason: %s", __func__, ssh_err(r));
2998 packet_disconnect("Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002999 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003000 if ((datafellows & SSH_BUG_OPENFAILURE) == 0) {
3001 /* skip language */
3002 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
3003 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) == 0) {
3004 error("%s: message/lang: %s", __func__, ssh_err(r));
3005 packet_disconnect("Invalid open failure message");
3006 }
3007 }
3008 ssh_packet_check_eom(ssh);
3009 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003010 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3011 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003012 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003013 debug2("%s: channel %d: callback start", __func__, c->self);
3014 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3015 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003016 }
Damien Miller7a606212009-01-28 16:22:34 +11003017 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003018 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003019 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003020}
3021
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003022int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003023channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003024{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003025 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003026 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003027 u_int32_t adjust;
3028 u_int new_rwin;
3029 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003030
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003031 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003032 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003033 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003034 }
3035
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003036 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003037 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003038 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) {
3039 error("%s: adjust: %s", __func__, ssh_err(r));
3040 packet_disconnect("Invalid window adjust message");
3041 }
3042 ssh_packet_check_eom(ssh);
3043 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3044 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003045 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003046 c->self, adjust, c->remote_window);
3047 }
3048 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003049 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003050}
3051
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003052int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003053channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003054{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003055 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003056 Channel *c;
3057 struct channel_confirm *cc;
Damien Millerb84886b2008-05-19 15:05:07 +10003058
3059 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003060 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003061
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003062 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003063
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003064 if ((c = channel_lookup(ssh, id)) == NULL) {
3065 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003066 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003067 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003068 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003069 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003070 ssh_packet_check_eom(ssh);
Damien Millerb84886b2008-05-19 15:05:07 +10003071 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003072 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003073 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003074 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003075 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003076 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003077 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003078}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003079
Ben Lindstrome9c99912001-06-09 00:41:05 +00003080/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003081
Ben Lindstrom908afed2001-10-03 17:34:59 +00003082void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003083channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003084{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003085 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003086}
3087
Damien Millerf6dff7c2011-09-22 21:38:52 +10003088
3089/*
3090 * Determine whether or not a port forward listens to loopback, the
3091 * specified address or wildcard. On the client, a specified bind
3092 * address will always override gateway_ports. On the server, a
3093 * gateway_ports of 1 (``yes'') will override the client's specification
3094 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3095 * will bind to whatever address the client asked for.
3096 *
3097 * Special-case listen_addrs are:
3098 *
3099 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3100 * "" (empty string), "*" -> wildcard v4/v6
3101 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003102 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003103 */
3104static const char *
3105channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003106 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003107{
3108 const char *addr = NULL;
3109 int wildcard = 0;
3110
3111 if (listen_addr == NULL) {
3112 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003113 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003114 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003115 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003116 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3117 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3118 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003119 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003120 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003121 /*
3122 * Notify client if they requested a specific listen
3123 * address and it was overridden.
3124 */
3125 if (*listen_addr != '\0' &&
3126 strcmp(listen_addr, "0.0.0.0") != 0 &&
3127 strcmp(listen_addr, "*") != 0) {
3128 packet_send_debug("Forwarding listen address "
3129 "\"%s\" overridden by server "
3130 "GatewayPorts", listen_addr);
3131 }
Damien Miller602943d2014-07-04 08:59:41 +10003132 } else if (strcmp(listen_addr, "localhost") != 0 ||
3133 strcmp(listen_addr, "127.0.0.1") == 0 ||
3134 strcmp(listen_addr, "::1") == 0) {
3135 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003136 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003137 }
3138 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3139 strcmp(listen_addr, "::1") == 0) {
3140 /*
3141 * If a specific IPv4/IPv6 localhost address has been
3142 * requested then accept it even if gateway_ports is in
3143 * effect. This allows the client to prefer IPv4 or IPv6.
3144 */
3145 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003146 }
3147 if (wildcardp != NULL)
3148 *wildcardp = wildcard;
3149 return addr;
3150}
3151
Damien Millerb16461c2002-01-22 23:29:22 +11003152static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003153channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3154 struct Forward *fwd, int *allocated_listen_port,
3155 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003156{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003157 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003158 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003159 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003160 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003161 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003162 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003163
Damien Millerf91ee4c2005-03-01 21:24:33 +11003164 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003165
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003166 if (is_client && fwd->connect_path != NULL) {
3167 host = fwd->connect_path;
3168 } else {
3169 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3170 fwd->listen_host : fwd->connect_host;
3171 if (host == NULL) {
3172 error("No forward host name.");
3173 return 0;
3174 }
3175 if (strlen(host) >= NI_MAXHOST) {
3176 error("Forward host name too long.");
3177 return 0;
3178 }
Kevin Steves12057502001-02-05 14:54:34 +00003179 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003180
Damien Millerf6dff7c2011-09-22 21:38:52 +10003181 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003182 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3183 is_client, fwd_opts);
3184 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003185 type, wildcard, (addr == NULL) ? "NULL" : addr);
3186
3187 /*
Damien Miller34132e52000-01-14 15:45:46 +11003188 * getaddrinfo returns a loopback address if the hostname is
3189 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003190 */
Damien Miller34132e52000-01-14 15:45:46 +11003191 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003192 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003193 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003194 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003195 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003196 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3197 if (addr == NULL) {
3198 /* This really shouldn't happen */
3199 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003200 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003201 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003202 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003203 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003204 }
Damien Millera7270302005-07-06 09:36:05 +10003205 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003206 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003207 if (allocated_listen_port != NULL)
3208 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003209 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003210 switch (ai->ai_family) {
3211 case AF_INET:
3212 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3213 sin_port;
3214 break;
3215 case AF_INET6:
3216 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3217 sin6_port;
3218 break;
3219 default:
Damien Miller34132e52000-01-14 15:45:46 +11003220 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003221 }
3222 /*
3223 * If allocating a port for -R forwards, then use the
3224 * same port for all address families.
3225 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003226 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3227 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3228 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003229 *lport_p = htons(*allocated_listen_port);
3230
Damien Miller34132e52000-01-14 15:45:46 +11003231 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003232 strport, sizeof(strport),
3233 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003234 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003235 continue;
3236 }
3237 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003238 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003239 if (sock < 0) {
3240 /* this is no error since kernel may not support ipv6 */
3241 verbose("socket: %.100s", strerror(errno));
3242 continue;
3243 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003244
3245 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003246 if (ai->ai_family == AF_INET6)
3247 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003248
Damien Miller4bf648f2009-02-14 16:28:21 +11003249 debug("Local forwarding listening on %s port %s.",
3250 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003251
Damien Miller34132e52000-01-14 15:45:46 +11003252 /* Bind the socket to the address. */
3253 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003254 /*
3255 * address can be in if use ipv6 address is
3256 * already bound
3257 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003258 if (!ai->ai_next)
3259 error("bind: %.100s", strerror(errno));
3260 else
3261 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003262
Damien Miller34132e52000-01-14 15:45:46 +11003263 close(sock);
3264 continue;
3265 }
3266 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003267 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003268 error("listen: %.100s", strerror(errno));
3269 close(sock);
3270 continue;
3271 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003272
3273 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003274 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003275 * record what we got.
3276 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003277 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3278 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003279 allocated_listen_port != NULL &&
3280 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003281 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003282 debug("Allocated listen port %d",
3283 *allocated_listen_port);
3284 }
3285
Damien Miller34132e52000-01-14 15:45:46 +11003286 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003287 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003288 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003289 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003290 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003291 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003292 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003293 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003294 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3295 c->listening_port = *allocated_listen_port;
3296 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003297 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003298 success = 1;
3299 }
3300 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003301 error("%s: cannot listen to port: %d", __func__,
3302 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003303 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003304 return success;
Damien Miller95def091999-11-25 00:26:21 +11003305}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003306
Damien Miller7acefbb2014-07-18 14:11:24 +10003307static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003308channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3309 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003310{
3311 struct sockaddr_un sunaddr;
3312 const char *path;
3313 Channel *c;
3314 int port, sock;
3315 mode_t omask;
3316
3317 switch (type) {
3318 case SSH_CHANNEL_UNIX_LISTENER:
3319 if (fwd->connect_path != NULL) {
3320 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3321 error("Local connecting path too long: %s",
3322 fwd->connect_path);
3323 return 0;
3324 }
3325 path = fwd->connect_path;
3326 port = PORT_STREAMLOCAL;
3327 } else {
3328 if (fwd->connect_host == NULL) {
3329 error("No forward host name.");
3330 return 0;
3331 }
3332 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3333 error("Forward host name too long.");
3334 return 0;
3335 }
3336 path = fwd->connect_host;
3337 port = fwd->connect_port;
3338 }
3339 break;
3340 case SSH_CHANNEL_RUNIX_LISTENER:
3341 path = fwd->listen_path;
3342 port = PORT_STREAMLOCAL;
3343 break;
3344 default:
3345 error("%s: unexpected channel type %d", __func__, type);
3346 return 0;
3347 }
3348
3349 if (fwd->listen_path == NULL) {
3350 error("No forward path name.");
3351 return 0;
3352 }
3353 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3354 error("Local listening path too long: %s", fwd->listen_path);
3355 return 0;
3356 }
3357
3358 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3359
3360 /* Start a Unix domain listener. */
3361 omask = umask(fwd_opts->streamlocal_bind_mask);
3362 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3363 fwd_opts->streamlocal_bind_unlink);
3364 umask(omask);
3365 if (sock < 0)
3366 return 0;
3367
3368 debug("Local forwarding listening on path %s.", fwd->listen_path);
3369
3370 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003371 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003372 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3373 0, "unix listener", 1);
3374 c->path = xstrdup(path);
3375 c->host_port = port;
3376 c->listening_port = PORT_STREAMLOCAL;
3377 c->listening_addr = xstrdup(fwd->listen_path);
3378 return 1;
3379}
3380
3381static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003382channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3383 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003384{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003385 u_int i;
3386 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003387
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003388 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3389 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003390 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3391 continue;
3392 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3393 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003394 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003395 found = 1;
3396 }
3397 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003398
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003399 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003400}
3401
Damien Miller7acefbb2014-07-18 14:11:24 +10003402static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003403channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003404{
3405 u_int i;
3406 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003407
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003408 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3409 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003410 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3411 continue;
3412 if (c->path == NULL)
3413 continue;
3414 if (strcmp(c->path, path) == 0) {
3415 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003416 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003417 found = 1;
3418 }
3419 }
3420
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003421 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003422}
3423
3424int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003425channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003426{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003427 if (fwd->listen_path != NULL) {
3428 return channel_cancel_rport_listener_streamlocal(ssh,
3429 fwd->listen_path);
3430 } else {
3431 return channel_cancel_rport_listener_tcpip(ssh,
3432 fwd->listen_host, fwd->listen_port);
3433 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003434}
3435
3436static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003437channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3438 const char *lhost, u_short lport, int cport,
3439 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003440{
3441 u_int i;
3442 int found = 0;
3443 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003444
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003445 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3446 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003447 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3448 continue;
Damien Millerff773642011-09-22 21:39:48 +10003449 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003450 continue;
Damien Millerff773642011-09-22 21:39:48 +10003451 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3452 /* skip dynamic forwardings */
3453 if (c->host_port == 0)
3454 continue;
3455 } else {
3456 if (c->host_port != cport)
3457 continue;
3458 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003459 if ((c->listening_addr == NULL && addr != NULL) ||
3460 (c->listening_addr != NULL && addr == NULL))
3461 continue;
3462 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003463 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003464 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003465 found = 1;
3466 }
3467 }
3468
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003469 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003470}
3471
Damien Miller7acefbb2014-07-18 14:11:24 +10003472static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003473channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003474{
3475 u_int i;
3476 int found = 0;
3477
3478 if (path == NULL) {
3479 error("%s: no path specified.", __func__);
3480 return 0;
3481 }
3482
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003483 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3484 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003485 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3486 continue;
3487 if (c->listening_addr == NULL)
3488 continue;
3489 if (strcmp(c->listening_addr, path) == 0) {
3490 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003491 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003492 found = 1;
3493 }
3494 }
3495
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003496 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003497}
3498
3499int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003500channel_cancel_lport_listener(struct ssh *ssh,
3501 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003502{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003503 if (fwd->listen_path != NULL) {
3504 return channel_cancel_lport_listener_streamlocal(ssh,
3505 fwd->listen_path);
3506 } else {
3507 return channel_cancel_lport_listener_tcpip(ssh,
3508 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3509 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003510}
3511
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003512/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003513int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003514channel_setup_local_fwd_listener(struct ssh *ssh,
3515 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003516{
Damien Miller7acefbb2014-07-18 14:11:24 +10003517 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003518 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003519 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3520 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003521 return channel_setup_fwd_listener_tcpip(ssh,
3522 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003523 }
Damien Millerb16461c2002-01-22 23:29:22 +11003524}
3525
3526/* protocol v2 remote port fwd, used by sshd */
3527int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003528channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003529 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003530{
Damien Miller7acefbb2014-07-18 14:11:24 +10003531 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003532 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003533 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3534 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003535 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003536 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3537 fwd_opts);
3538 }
Damien Millerb16461c2002-01-22 23:29:22 +11003539}
3540
Damien Miller5428f641999-11-25 11:54:57 +11003541/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003542 * Translate the requested rfwd listen host to something usable for
3543 * this server.
3544 */
3545static const char *
3546channel_rfwd_bind_host(const char *listen_host)
3547{
3548 if (listen_host == NULL) {
3549 if (datafellows & SSH_BUG_RFWD_ADDR)
3550 return "127.0.0.1";
3551 else
3552 return "localhost";
3553 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3554 if (datafellows & SSH_BUG_RFWD_ADDR)
3555 return "0.0.0.0";
3556 else
3557 return "";
3558 } else
3559 return listen_host;
3560}
3561
3562/*
Damien Miller5428f641999-11-25 11:54:57 +11003563 * Initiate forwarding of connections to port "port" on remote host through
3564 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003565 * Returns handle (index) for updating the dynamic listen port with
3566 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003567 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003568int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003569channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003570{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003571 int r, success = 0, idx = -1;
3572 char *host_to_connect, *listen_host, *listen_path;
3573 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003574
Damien Miller95def091999-11-25 00:26:21 +11003575 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003576 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003577 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3578 (r = sshpkt_put_cstring(ssh,
3579 "streamlocal-forward@openssh.com")) != 0 ||
3580 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3581 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3582 (r = sshpkt_send(ssh)) != 0 ||
3583 (r = ssh_packet_write_wait(ssh)) != 0)
3584 fatal("%s: request streamlocal: %s",
3585 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003586 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003587 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3588 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3589 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3590 (r = sshpkt_put_cstring(ssh,
3591 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3592 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3593 (r = sshpkt_send(ssh)) != 0 ||
3594 (r = ssh_packet_write_wait(ssh)) != 0)
3595 fatal("%s: request tcpip-forward: %s",
3596 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003597 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003598 /* Assume that server accepts the request */
3599 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003600 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003601 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003602 host_to_connect = listen_host = listen_path = NULL;
3603 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003604 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003605 host_to_connect = xstrdup(fwd->connect_path);
3606 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003607 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003608 host_to_connect = xstrdup(fwd->connect_host);
3609 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003610 }
3611 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003612 listen_path = xstrdup(fwd->listen_path);
3613 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003614 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003615 if (fwd->listen_host != NULL)
3616 listen_host = xstrdup(fwd->listen_host);
3617 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003618 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003619 idx = fwd_perm_list_add(ssh, FWDPERM_USER,
3620 host_to_connect, port_to_connect,
3621 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003622 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003623 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003624}
3625
Damien Miller4b3ed642014-07-02 15:29:40 +10003626static int
3627open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003628 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003629{
3630 if (allowed_open->host_to_connect == NULL)
3631 return 0;
3632 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3633 allowed_open->port_to_connect != requestedport)
3634 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003635 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3636 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003637 return 0;
3638 return 1;
3639}
3640
3641/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003642 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003643 * we don't support FWD_PERMIT_ANY_PORT and
3644 * need to translate between the configured-host (listen_host)
3645 * and what we've sent to the remote server (channel_rfwd_bind_host)
3646 */
3647static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003648open_listen_match_tcpip(ForwardPermission *allowed_open,
3649 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003650{
3651 const char *allowed_host;
3652
3653 if (allowed_open->host_to_connect == NULL)
3654 return 0;
3655 if (allowed_open->listen_port != requestedport)
3656 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003657 if (!translate && allowed_open->listen_host == NULL &&
3658 requestedhost == NULL)
3659 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003660 allowed_host = translate ?
3661 channel_rfwd_bind_host(allowed_open->listen_host) :
3662 allowed_open->listen_host;
3663 if (allowed_host == NULL ||
3664 strcmp(allowed_host, requestedhost) != 0)
3665 return 0;
3666 return 1;
3667}
3668
Damien Miller7acefbb2014-07-18 14:11:24 +10003669static int
3670open_listen_match_streamlocal(ForwardPermission *allowed_open,
3671 const char *requestedpath)
3672{
3673 if (allowed_open->host_to_connect == NULL)
3674 return 0;
3675 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3676 return 0;
3677 if (allowed_open->listen_path == NULL ||
3678 strcmp(allowed_open->listen_path, requestedpath) != 0)
3679 return 0;
3680 return 1;
3681}
3682
Damien Miller5428f641999-11-25 11:54:57 +11003683/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003684 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003685 * local side.
3686 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003687static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003688channel_request_rforward_cancel_tcpip(struct ssh *ssh,
3689 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003690{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003691 struct ssh_channels *sc = ssh->chanctxt;
3692 int r;
3693 u_int i;
3694 ForwardPermission *fp;
Darren Tuckere7066df2004-05-24 10:18:05 +10003695
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003696 for (i = 0; i < sc->num_permitted_opens; i++) {
3697 fp = &sc->permitted_opens[i];
3698 if (open_listen_match_tcpip(fp, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003699 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003700 fp = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10003701 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003702 if (fp == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003703 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003704 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003705 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003706 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3707 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
3708 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3709 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
3710 (r = sshpkt_put_u32(ssh, port)) != 0 ||
3711 (r = sshpkt_send(ssh)) != 0)
3712 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10003713
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003714 fwd_perm_clear(fp); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10003715
3716 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003717}
3718
3719/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003720 * Request cancellation of remote forwarding of Unix domain socket
3721 * path from local side.
3722 */
3723static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003724channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003725{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003726 struct ssh_channels *sc = ssh->chanctxt;
3727 int r;
3728 u_int i;
3729 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10003730
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003731 for (i = 0; i < sc->num_permitted_opens; i++) {
3732 fp = &sc->permitted_opens[i];
3733 if (open_listen_match_streamlocal(fp, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10003734 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003735 fp = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003736 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003737 if (fp == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003738 debug("%s: requested forward not found", __func__);
3739 return -1;
3740 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003741 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3742 (r = sshpkt_put_cstring(ssh,
3743 "cancel-streamlocal-forward@openssh.com")) != 0 ||
3744 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3745 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
3746 (r = sshpkt_send(ssh)) != 0)
3747 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003748
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003749 fwd_perm_clear(fp); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10003750
3751 return 0;
3752}
3753
3754/*
3755 * Request cancellation of remote forwarding of a connection from local side.
3756 */
3757int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003758channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003759{
3760 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003761 return channel_request_rforward_cancel_streamlocal(ssh,
3762 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003763 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003764 return channel_request_rforward_cancel_tcpip(ssh,
3765 fwd->listen_host,
3766 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10003767 }
3768}
3769
3770/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003771 * Permits opening to any host/port if permitted_opens[] is empty. This is
3772 * usually called by the server, because the user could connect to any port
3773 * anyway, and the server has no way to know but to trust the client anyway.
3774 */
3775void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003776channel_permit_all_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003777{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003778 if (ssh->chanctxt->num_permitted_opens == 0)
3779 ssh->chanctxt->all_opens_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003780}
3781
Ben Lindstroma3700052001-04-05 23:26:32 +00003782void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003783channel_add_permitted_opens(struct ssh *ssh, char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003784{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003785 struct ssh_channels *sc = ssh->chanctxt;
3786
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003787 debug("allow port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003788 fwd_perm_list_add(ssh, FWDPERM_USER, host, port, NULL, NULL, 0, NULL);
3789 sc->all_opens_permitted = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003790}
3791
Darren Tucker68afb8c2011-10-02 18:59:03 +11003792/*
3793 * Update the listen port for a dynamic remote forward, after
3794 * the actual 'newport' has been allocated. If 'newport' < 0 is
3795 * passed then they entry will be invalidated.
3796 */
3797void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003798channel_update_permitted_opens(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11003799{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003800 struct ssh_channels *sc = ssh->chanctxt;
3801
3802 if (idx < 0 || (u_int)idx >= sc->num_permitted_opens) {
3803 debug("%s: index out of range: %d num_permitted_opens %d",
3804 __func__, idx, sc->num_permitted_opens);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003805 return;
3806 }
3807 debug("%s allowed port %d for forwarding to host %s port %d",
3808 newport > 0 ? "Updating" : "Removing",
3809 newport,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003810 sc->permitted_opens[idx].host_to_connect,
3811 sc->permitted_opens[idx].port_to_connect);
3812 if (newport <= 0)
3813 fwd_perm_clear(&sc->permitted_opens[idx]);
3814 else {
3815 sc->permitted_opens[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11003816 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003817 }
3818}
3819
Damien Millera765cf42006-07-24 14:08:13 +10003820int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003821channel_add_adm_permitted_opens(struct ssh *ssh, char *host, int port)
Damien Miller9b439df2006-07-24 14:04:00 +10003822{
Damien Millera765cf42006-07-24 14:08:13 +10003823 debug("config allows port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003824 return fwd_perm_list_add(ssh, FWDPERM_ADMIN, host, port,
3825 NULL, NULL, 0, NULL);
Damien Miller9b439df2006-07-24 14:04:00 +10003826}
3827
3828void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003829channel_disable_adm_local_opens(struct ssh *ssh)
Damien Millerc6081482012-04-22 11:18:53 +10003830{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003831 channel_clear_adm_permitted_opens(ssh);
3832 fwd_perm_list_add(ssh, FWDPERM_ADMIN, NULL, 0, NULL, NULL, 0, NULL);
Damien Millerc6081482012-04-22 11:18:53 +10003833}
3834
3835void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003836channel_clear_permitted_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003837{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003838 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003839
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003840 sc->permitted_opens = xrecallocarray(sc->permitted_opens,
3841 sc->num_permitted_opens, 0, sizeof(*sc->permitted_opens));
3842 sc->num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003843}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003844
Damien Miller9b439df2006-07-24 14:04:00 +10003845void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003846channel_clear_adm_permitted_opens(struct ssh *ssh)
Damien Miller9b439df2006-07-24 14:04:00 +10003847{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003848 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller9b439df2006-07-24 14:04:00 +10003849
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003850 sc->permitted_adm_opens = xrecallocarray(sc->permitted_adm_opens,
3851 sc->num_adm_permitted_opens, 0, sizeof(*sc->permitted_adm_opens));
3852 sc->num_adm_permitted_opens = 0;
Darren Tuckere7140f22008-06-10 23:01:51 +10003853}
3854
Darren Tucker1338b9e2011-10-02 18:57:35 +11003855/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3856int
3857permitopen_port(const char *p)
3858{
3859 int port;
3860
3861 if (strcmp(p, "*") == 0)
3862 return FWD_PERMIT_ANY_PORT;
3863 if ((port = a2port(p)) > 0)
3864 return port;
3865 return -1;
3866}
3867
Damien Millerbd740252008-05-19 15:37:09 +10003868/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003869static int
Damien Millerbd740252008-05-19 15:37:09 +10003870connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003871{
Damien Millerbd740252008-05-19 15:37:09 +10003872 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003873 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003874 char ntop[NI_MAXHOST];
3875 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003876
Damien Millerbd740252008-05-19 15:37:09 +10003877 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003878 switch (cctx->ai->ai_family) {
3879 case AF_UNIX:
3880 /* unix:pathname instead of host:port */
3881 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3882 strlcpy(ntop, "unix", sizeof(ntop));
3883 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3884 break;
3885 case AF_INET:
3886 case AF_INET6:
3887 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3888 ntop, sizeof(ntop), strport, sizeof(strport),
3889 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3890 error("connect_next: getnameinfo failed");
3891 continue;
3892 }
3893 break;
3894 default:
Damien Miller34132e52000-01-14 15:45:46 +11003895 continue;
3896 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003897 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3898 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003899 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003900 error("socket: %.100s", strerror(errno));
3901 else
3902 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003903 continue;
3904 }
Damien Miller232711f2004-06-15 10:35:30 +10003905 if (set_nonblock(sock) == -1)
3906 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003907 if (connect(sock, cctx->ai->ai_addr,
3908 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3909 debug("connect_next: host %.100s ([%.100s]:%s): "
3910 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003911 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003912 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003913 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003914 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003915 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003916 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003917 if (cctx->ai->ai_family != AF_UNIX)
3918 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003919 debug("connect_next: host %.100s ([%.100s]:%s) "
3920 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3921 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003922 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003923 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003924 return -1;
3925}
Damien Millerb38eff82000-04-01 11:09:21 +10003926
Damien Millerbd740252008-05-19 15:37:09 +10003927static void
3928channel_connect_ctx_free(struct channel_connect *cctx)
3929{
Darren Tuckera627d422013-06-02 07:31:17 +10003930 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003931 if (cctx->aitop) {
3932 if (cctx->aitop->ai_family == AF_UNIX)
3933 free(cctx->aitop);
3934 else
3935 freeaddrinfo(cctx->aitop);
3936 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003937 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003938}
3939
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003940/*
3941 * Return CONNECTING channel to remote host:port or local socket path,
3942 * passing back the failure reason if appropriate.
3943 */
Damien Millerbd740252008-05-19 15:37:09 +10003944static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003945connect_to_reason(struct ssh *ssh, const char *name, int port,
3946 char *ctype, char *rname, int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003947{
3948 struct addrinfo hints;
3949 int gaierr;
3950 int sock = -1;
3951 char strport[NI_MAXSERV];
3952 struct channel_connect cctx;
3953 Channel *c;
3954
Damien Miller2bcb8662008-07-12 17:12:29 +10003955 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003956
3957 if (port == PORT_STREAMLOCAL) {
3958 struct sockaddr_un *sunaddr;
3959 struct addrinfo *ai;
3960
3961 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3962 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3963 return (NULL);
3964 }
3965
3966 /*
3967 * Fake up a struct addrinfo for AF_UNIX connections.
3968 * channel_connect_ctx_free() must check ai_family
3969 * and use free() not freeaddirinfo() for AF_UNIX.
3970 */
3971 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3972 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3973 ai->ai_addr = (struct sockaddr *)(ai + 1);
3974 ai->ai_addrlen = sizeof(*sunaddr);
3975 ai->ai_family = AF_UNIX;
3976 ai->ai_socktype = SOCK_STREAM;
3977 ai->ai_protocol = PF_UNSPEC;
3978 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3979 sunaddr->sun_family = AF_UNIX;
3980 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3981 cctx.aitop = ai;
3982 } else {
3983 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003984 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller7acefbb2014-07-18 14:11:24 +10003985 hints.ai_socktype = SOCK_STREAM;
3986 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003987 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3988 != 0) {
3989 if (errmsg != NULL)
3990 *errmsg = ssh_gai_strerror(gaierr);
3991 if (reason != NULL)
3992 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003993 error("connect_to %.100s: unknown host (%s)", name,
3994 ssh_gai_strerror(gaierr));
3995 return NULL;
3996 }
Damien Millerbd740252008-05-19 15:37:09 +10003997 }
3998
Damien Miller7acefbb2014-07-18 14:11:24 +10003999 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10004000 cctx.port = port;
4001 cctx.ai = cctx.aitop;
4002
4003 if ((sock = connect_next(&cctx)) == -1) {
4004 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004005 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004006 channel_connect_ctx_free(&cctx);
4007 return NULL;
4008 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004009 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004010 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4011 c->connect_ctx = cctx;
4012 return c;
4013}
4014
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004015/* Return CONNECTING channel to remote host:port or local socket path */
4016static Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004017connect_to(struct ssh *ssh, const char *name, int port,
4018 char *ctype, char *rname)
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004019{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004020 return connect_to_reason(ssh, name, port, ctype, rname, NULL, NULL);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004021}
4022
markus@openbsd.org8d057842016-09-30 09:19:13 +00004023/*
4024 * returns either the newly connected channel or the downstream channel
4025 * that needs to deal with this connection.
4026 */
Damien Millerbd740252008-05-19 15:37:09 +10004027Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004028channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004029 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004030{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004031 struct ssh_channels *sc = ssh->chanctxt;
4032 u_int i;
4033 ForwardPermission *fp;
Damien Millerbd740252008-05-19 15:37:09 +10004034
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004035 for (i = 0; i < sc->num_permitted_opens; i++) {
4036 fp = &sc->permitted_opens[i];
4037 if (open_listen_match_tcpip(fp, listen_host, listen_port, 1)) {
4038 if (fp->downstream)
4039 return fp->downstream;
4040 return connect_to(ssh,
4041 fp->host_to_connect, fp->port_to_connect,
4042 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004043 }
4044 }
4045 error("WARNING: Server requests forwarding for unknown listen_port %d",
4046 listen_port);
4047 return NULL;
4048}
4049
Damien Miller7acefbb2014-07-18 14:11:24 +10004050Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004051channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4052 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004053{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004054 struct ssh_channels *sc = ssh->chanctxt;
4055 u_int i;
4056 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004057
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004058 for (i = 0; i < sc->num_permitted_opens; i++) {
4059 fp = &sc->permitted_opens[i];
4060 if (open_listen_match_streamlocal(fp, path)) {
4061 return connect_to(ssh,
4062 fp->host_to_connect, fp->port_to_connect,
4063 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004064 }
4065 }
4066 error("WARNING: Server requests forwarding for unknown path %.100s",
4067 path);
4068 return NULL;
4069}
4070
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004071/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004072Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004073channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4074 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004075{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004076 struct ssh_channels *sc = ssh->chanctxt;
4077 u_int i, permit, permit_adm = 1;
4078 ForwardPermission *fp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004079
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004080 permit = sc->all_opens_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004081 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004082 for (i = 0; i < sc->num_permitted_opens; i++) {
4083 fp = &sc->permitted_opens[i];
4084 if (open_match(fp, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004085 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004086 break;
4087 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004088 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004089 }
Damien Miller9b439df2006-07-24 14:04:00 +10004090
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004091 if (sc->num_adm_permitted_opens > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004092 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004093 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4094 fp = &sc->permitted_adm_opens[i];
4095 if (open_match(fp, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004096 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004097 break;
4098 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004099 }
Damien Miller9b439df2006-07-24 14:04:00 +10004100 }
4101
4102 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004103 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004104 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004105 if (reason != NULL)
4106 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004107 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004108 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004109 return connect_to_reason(ssh, host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004110}
4111
Damien Miller7acefbb2014-07-18 14:11:24 +10004112/* Check if connecting to that path is permitted and connect. */
4113Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004114channel_connect_to_path(struct ssh *ssh, const char *path,
4115 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004116{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004117 struct ssh_channels *sc = ssh->chanctxt;
4118 u_int i, permit, permit_adm = 1;
4119 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004120
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004121 permit = sc->all_opens_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004122 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004123 for (i = 0; i < sc->num_permitted_opens; i++) {
4124 fp = &sc->permitted_opens[i];
4125 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004126 permit = 1;
4127 break;
4128 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004129 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004130 }
4131
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004132 if (sc->num_adm_permitted_opens > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004133 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004134 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4135 fp = &sc->permitted_adm_opens[i];
4136 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004137 permit_adm = 1;
4138 break;
4139 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004140 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004141 }
4142
4143 if (!permit || !permit_adm) {
4144 logit("Received request to connect to path %.100s, "
4145 "but the request was denied.", path);
4146 return NULL;
4147 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004148 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004149}
4150
Damien Miller0e220db2004-06-15 10:34:08 +10004151void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004152channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004153{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004154 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004155 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004156 int r;
4157 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004158
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004159 for (i = 0; i < sc->channels_alloc; i++) {
4160 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4161 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004162 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004163 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
Damien Miller0e220db2004-06-15 10:34:08 +10004164 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004165 channel_request_start(ssh, i, "window-change", 0);
4166 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4167 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4168 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4169 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4170 (r = sshpkt_send(ssh)) != 0)
4171 fatal("%s: channel %u: send window-change: %s",
4172 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004173 }
4174}
4175
Ben Lindstrome9c99912001-06-09 00:41:05 +00004176/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004177
Damien Miller5428f641999-11-25 11:54:57 +11004178/*
4179 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004180 * Returns 0 and a suitable display number for the DISPLAY variable
4181 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004182 */
Kevin Steves366298c2001-12-19 17:58:01 +00004183int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004184x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4185 int x11_use_localhost, int single_connection,
4186 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004187{
Damien Millere7378562001-12-21 14:58:35 +11004188 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004189 int display_number, sock;
4190 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004191 struct addrinfo hints, *ai, *aitop;
4192 char strport[NI_MAXSERV];
4193 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004194
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004195 if (chanids == NULL)
4196 return -1;
4197
Damien Millera34a28b1999-12-14 10:47:15 +11004198 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004199 display_number < MAX_DISPLAYS;
4200 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004201 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004202 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004203 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004204 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004205 hints.ai_socktype = SOCK_STREAM;
4206 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004207 if ((gaierr = getaddrinfo(NULL, strport,
4208 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004209 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004210 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004211 }
Damien Miller34132e52000-01-14 15:45:46 +11004212 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004213 if (ai->ai_family != AF_INET &&
4214 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004215 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004216 sock = socket(ai->ai_family, ai->ai_socktype,
4217 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004218 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004219 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4220#ifdef EPFNOSUPPORT
4221 && (errno != EPFNOSUPPORT)
4222#endif
4223 ) {
Damien Millere2192732000-01-17 13:22:55 +11004224 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004225 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004226 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004227 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004228 debug("x11_create_display_inet: Socket family %d not supported",
4229 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004230 continue;
4231 }
Damien Miller34132e52000-01-14 15:45:46 +11004232 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004233 if (ai->ai_family == AF_INET6)
4234 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004235 if (x11_use_localhost)
4236 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004237 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004238 debug2("%s: bind port %d: %.100s", __func__,
4239 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004240 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004241 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004242 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004243 num_socks = 0;
4244 break;
4245 }
4246 socks[num_socks++] = sock;
4247 if (num_socks == NUM_SOCKS)
4248 break;
Damien Miller95def091999-11-25 00:26:21 +11004249 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004250 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004251 if (num_socks > 0)
4252 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004253 }
Damien Miller95def091999-11-25 00:26:21 +11004254 if (display_number >= MAX_DISPLAYS) {
4255 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004256 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004257 }
Damien Miller95def091999-11-25 00:26:21 +11004258 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004259 for (n = 0; n < num_socks; n++) {
4260 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004261 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004262 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004263 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004264 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004265 }
Damien Miller95def091999-11-25 00:26:21 +11004266 }
Damien Miller34132e52000-01-14 15:45:46 +11004267
Damien Miller34132e52000-01-14 15:45:46 +11004268 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004269 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004270 for (n = 0; n < num_socks; n++) {
4271 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004272 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004273 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4274 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004275 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004276 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004277 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004278 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004279 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004280
Kevin Steves366298c2001-12-19 17:58:01 +00004281 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004282 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004283 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004284}
4285
Ben Lindstrombba81212001-06-25 05:01:22 +00004286static int
Damien Miller819dbb62009-01-21 16:46:26 +11004287connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004288{
Damien Miller95def091999-11-25 00:26:21 +11004289 int sock;
4290 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004291
Damien Miller3afe3752001-12-21 12:39:51 +11004292 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4293 if (sock < 0)
4294 error("socket: %.100s", strerror(errno));
4295 memset(&addr, 0, sizeof(addr));
4296 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004297 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004298 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004299 return sock;
4300 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004301 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4302 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004303}
4304
Damien Miller819dbb62009-01-21 16:46:26 +11004305static int
4306connect_local_xsocket(u_int dnr)
4307{
4308 char buf[1024];
4309 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4310 return connect_local_xsocket_path(buf);
4311}
4312
Darren Tuckerda39b092017-03-10 13:22:32 +11004313#ifdef __APPLE__
4314static int
4315is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4316{
4317 struct stat sbuf;
4318
4319 if (strlcpy(path, display, pathlen) >= pathlen) {
4320 error("%s: display path too long", __func__);
4321 return 0;
4322 }
4323 if (display[0] != '/')
4324 return 0;
4325 if (stat(path, &sbuf) == 0) {
4326 return 1;
4327 } else {
4328 char *dot = strrchr(path, '.');
4329 if (dot != NULL) {
4330 *dot = '\0';
4331 if (stat(path, &sbuf) == 0) {
4332 return 1;
4333 }
4334 }
4335 }
4336 return 0;
4337}
4338#endif
4339
Damien Millerbd483e72000-04-30 10:00:53 +10004340int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004341x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004342{
Damien Miller57c4e872006-03-31 23:11:07 +11004343 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004344 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004345 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004346 struct addrinfo hints, *ai, *aitop;
4347 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004348 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004349
Damien Miller95def091999-11-25 00:26:21 +11004350 /* Try to open a socket for the local X server. */
4351 display = getenv("DISPLAY");
4352 if (!display) {
4353 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004354 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004355 }
Damien Miller5428f641999-11-25 11:54:57 +11004356 /*
4357 * Now we decode the value of the DISPLAY variable and make a
4358 * connection to the real X server.
4359 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004360
Damien Miller819dbb62009-01-21 16:46:26 +11004361#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004362 /* Check if display is a path to a socket (as set by launchd). */
4363 {
4364 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004365
Darren Tuckerda39b092017-03-10 13:22:32 +11004366 if (is_path_to_xsocket(display, path, sizeof(path))) {
4367 debug("x11_connect_display: $DISPLAY is launchd");
4368
4369 /* Create a socket. */
4370 sock = connect_local_xsocket_path(path);
4371 if (sock < 0)
4372 return -1;
4373
4374 /* OK, we now have a connection to the display. */
4375 return sock;
4376 }
Damien Miller819dbb62009-01-21 16:46:26 +11004377 }
4378#endif
Damien Miller5428f641999-11-25 11:54:57 +11004379 /*
4380 * Check if it is a unix domain socket. Unix domain displays are in
4381 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4382 */
Damien Miller95def091999-11-25 00:26:21 +11004383 if (strncmp(display, "unix:", 5) == 0 ||
4384 display[0] == ':') {
4385 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004386 if (sscanf(strrchr(display, ':') + 1, "%u",
4387 &display_number) != 1) {
4388 error("Could not parse display number from DISPLAY: "
4389 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004390 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004391 }
4392 /* Create a socket. */
4393 sock = connect_local_xsocket(display_number);
4394 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004395 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004396
4397 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004398 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004399 }
Damien Miller5428f641999-11-25 11:54:57 +11004400 /*
4401 * Connect to an inet socket. The DISPLAY value is supposedly
4402 * hostname:d[.s], where hostname may also be numeric IP address.
4403 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004404 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004405 cp = strchr(buf, ':');
4406 if (!cp) {
4407 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004408 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004409 }
Damien Miller95def091999-11-25 00:26:21 +11004410 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004411 /*
4412 * buf now contains the host name. But first we parse the
4413 * display number.
4414 */
Damien Miller57c4e872006-03-31 23:11:07 +11004415 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004416 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004417 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004418 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004419 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004420
Damien Miller34132e52000-01-14 15:45:46 +11004421 /* Look up the host address */
4422 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004423 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004424 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004425 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004426 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004427 error("%.100s: unknown host. (%s)", buf,
4428 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004429 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004430 }
Damien Miller34132e52000-01-14 15:45:46 +11004431 for (ai = aitop; ai; ai = ai->ai_next) {
4432 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004433 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004434 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004435 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004436 continue;
4437 }
4438 /* Connect it to the display. */
4439 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004440 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004441 6000 + display_number, strerror(errno));
4442 close(sock);
4443 continue;
4444 }
4445 /* Success */
4446 break;
Damien Miller34132e52000-01-14 15:45:46 +11004447 }
Damien Miller34132e52000-01-14 15:45:46 +11004448 freeaddrinfo(aitop);
4449 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004450 error("connect %.100s port %u: %.100s", buf,
4451 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004452 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004453 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004454 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004455 return sock;
4456}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004457
Damien Millerbd483e72000-04-30 10:00:53 +10004458/*
Damien Miller5428f641999-11-25 11:54:57 +11004459 * Requests forwarding of X11 connections, generates fake authentication
4460 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004461 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004462 */
Damien Miller4af51302000-04-16 11:18:38 +10004463void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004464x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4465 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004466{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004467 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004468 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004469 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004470 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004471 char *new_data;
4472 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004473
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004474 if (sc->x11_saved_display == NULL)
4475 sc->x11_saved_display = xstrdup(disp);
4476 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004477 error("x11_request_forwarding_with_spoofing: different "
4478 "$DISPLAY already forwarded");
4479 return;
4480 }
4481
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004482 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004483 if (cp)
4484 cp = strchr(cp, '.');
4485 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004486 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004487 else
4488 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004489
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004490 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004491 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004492 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004493
4494 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004495 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004496 for (i = 0; i < data_len; i++) {
4497 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4498 fatal("x11_request_forwarding: bad "
4499 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004500 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004501 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004502 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004503
4504 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004505 sc->x11_fake_data = xmalloc(data_len);
4506 arc4random_buf(sc->x11_fake_data, data_len);
4507 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004508 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004509
Damien Miller95def091999-11-25 00:26:21 +11004510 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004511 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004512
Damien Miller95def091999-11-25 00:26:21 +11004513 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004514 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4515 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4516 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4517 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4518 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4519 (r = sshpkt_send(ssh)) != 0 ||
4520 (r = ssh_packet_write_wait(ssh)) != 0)
4521 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004522 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004523}