blob: 226ba7a39123ba533c65eac309ea0bcac8e27f2c [file] [log] [blame]
djm@openbsd.org846446b2020-01-25 06:40:20 +00001/* $OpenBSD: channels.c,v 1.395 2020/01/25 06:40:20 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
Damien Millercfc18972019-10-09 09:06:35 +110062# 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"
markus@openbsd.org5467fbc2018-07-11 18:53:29 +000082#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100083#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110084#include "pathnames.h"
djm@openbsd.org115063a2018-06-06 18:22:41 +000085#include "match.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000087/* -- agent forwarding */
88#define NUM_SOCKS 10
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Ben Lindstrome9c99912001-06-09 00:41:05 +000090/* -- tcp forwarding */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000091/* special-case port number meaning allow any port */
92#define FWD_PERMIT_ANY_PORT 0
93
94/* special-case wildcard meaning allow any host */
95#define FWD_PERMIT_ANY_HOST "*"
96
97/* -- X11 forwarding */
98/* Maximum number of fake X11 displays to try. */
99#define MAX_DISPLAYS 1000
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
djm@openbsd.org115063a2018-06-06 18:22:41 +0000101/* Per-channel callback for pre/post select() actions */
102typedef void chan_fn(struct ssh *, Channel *c,
103 fd_set *readset, fd_set *writeset);
104
Damien Miller5428f641999-11-25 11:54:57 +1100105/*
106 * Data structure for storing which hosts are permitted for forward requests.
107 * The local sides of any remote forwards are stored in this array to prevent
108 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
109 * network (which might be behind a firewall).
110 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000111/* XXX: streamlocal wants a path instead of host:port */
112/* Overload host_to_connect; we could just make this match Forward */
113/* XXX - can we use listen_host instead of listen_path? */
djm@openbsd.org115063a2018-06-06 18:22:41 +0000114struct permission {
Damien Millerb38eff82000-04-01 11:09:21 +1000115 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000116 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000117 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000118 char *listen_path; /* Remote side should listen path. */
119 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000120 Channel *downstream; /* Downstream mux*/
djm@openbsd.org115063a2018-06-06 18:22:41 +0000121};
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
djm@openbsd.org115063a2018-06-06 18:22:41 +0000123/*
124 * Stores the forwarding permission state for a single direction (local or
125 * remote).
126 */
127struct permission_set {
128 /*
129 * List of all local permitted host/port pairs to allow for the
130 * user.
131 */
132 u_int num_permitted_user;
133 struct permission *permitted_user;
134
135 /*
136 * List of all permitted host/port pairs to allow for the admin.
137 */
138 u_int num_permitted_admin;
139 struct permission *permitted_admin;
140
141 /*
142 * If this is true, all opens/listens are permitted. This is the
143 * case on the server on which we have to trust the client anyway,
144 * and the user could do anything after logging in.
145 */
146 int all_permitted;
147};
Ben Lindstrome9c99912001-06-09 00:41:05 +0000148
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000149/* Master structure for channels state */
150struct ssh_channels {
151 /*
152 * Pointer to an array containing all allocated channels. The array
153 * is dynamically extended as needed.
154 */
155 Channel **channels;
Damien Miller9b439df2006-07-24 14:04:00 +1000156
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000157 /*
158 * Size of the channel array. All slots of the array must always be
159 * initialized (at least the type field); unused slots set to NULL
160 */
161 u_int channels_alloc;
Damien Miller9b439df2006-07-24 14:04:00 +1000162
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000163 /*
164 * Maximum file descriptor value used in any of the channels. This is
165 * updated in channel_new.
166 */
167 int channel_max_fd;
Damien Miller9b439df2006-07-24 14:04:00 +1000168
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000169 /*
170 * 'channel_pre*' are called just before select() to add any bits
171 * relevant to channels in the select bitmasks.
172 *
173 * 'channel_post*': perform any appropriate operations for
174 * channels which have events pending.
175 */
176 chan_fn **channel_pre;
177 chan_fn **channel_post;
Darren Tucker1338b9e2011-10-02 18:57:35 +1100178
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000179 /* -- tcp forwarding */
djm@openbsd.org115063a2018-06-06 18:22:41 +0000180 struct permission_set local_perms;
181 struct permission_set remote_perms;
Damien Miller13390022005-07-06 09:44:19 +1000182
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000183 /* -- X11 forwarding */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000184
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000185 /* Saved X11 local (client) display. */
186 char *x11_saved_display;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000187
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000188 /* Saved X11 authentication protocol name. */
189 char *x11_saved_proto;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000190
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000191 /* Saved X11 authentication data. This is the real data. */
192 char *x11_saved_data;
193 u_int x11_saved_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000194
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000195 /* Deadline after which all X11 connections are refused */
196 u_int x11_refuse_time;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000197
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000198 /*
199 * Fake X11 authentication data. This is what the server will be
200 * sending us; we should replace any occurrences of this by the
201 * real data.
202 */
203 u_char *x11_fake_data;
204 u_int x11_fake_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000205
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000206 /* AF_UNSPEC or AF_INET or AF_INET6 */
207 int IPv4or6;
208};
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000209
Ben Lindstrome9c99912001-06-09 00:41:05 +0000210/* helper */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000211static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000212static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000213
Damien Millerbd740252008-05-19 15:37:09 +1000214/* non-blocking connect helpers */
215static int connect_next(struct channel_connect *);
216static void channel_connect_ctx_free(struct channel_connect *);
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000217static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *);
218static int rdynamic_connect_finish(struct ssh *, Channel *);
Damien Millerbd740252008-05-19 15:37:09 +1000219
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000220/* Setup helper */
221static void channel_handler_init(struct ssh_channels *sc);
222
Ben Lindstrome9c99912001-06-09 00:41:05 +0000223/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000224
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000225void
226channel_init_channels(struct ssh *ssh)
227{
228 struct ssh_channels *sc;
229
djm@openbsd.org285310b2018-12-07 02:31:20 +0000230 if ((sc = calloc(1, sizeof(*sc))) == NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000231 fatal("%s: allocation failed", __func__);
232 sc->channels_alloc = 10;
233 sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels));
234 sc->IPv4or6 = AF_UNSPEC;
235 channel_handler_init(sc);
236
237 ssh->chanctxt = sc;
238}
239
Damien Millerb38eff82000-04-01 11:09:21 +1000240Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000241channel_by_id(struct ssh *ssh, int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000242{
243 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000244
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000245 if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
246 logit("%s: %d: bad id", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000247 return NULL;
248 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000249 c = ssh->chanctxt->channels[id];
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000250 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000251 logit("%s: %d: bad id: channel free", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000252 return NULL;
253 }
254 return c;
255}
256
markus@openbsd.org8d057842016-09-30 09:19:13 +0000257Channel *
djm@openbsd.org9f532292017-09-12 06:35:31 +0000258channel_by_remote_id(struct ssh *ssh, u_int remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000259{
260 Channel *c;
261 u_int i;
262
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000263 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
264 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000265 if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000266 return c;
267 }
268 return NULL;
269}
270
Damien Miller5428f641999-11-25 11:54:57 +1100271/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100272 * Returns the channel if it is allowed to receive protocol messages.
273 * Private channels, like listening sockets, may not receive messages.
274 */
275Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000276channel_lookup(struct ssh *ssh, int id)
Damien Millerd47c62a2005-12-13 19:33:57 +1100277{
278 Channel *c;
279
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000280 if ((c = channel_by_id(ssh, id)) == NULL)
281 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100282
Damien Millerd62f2ca2006-03-26 13:57:41 +1100283 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100284 case SSH_CHANNEL_X11_OPEN:
285 case SSH_CHANNEL_LARVAL:
286 case SSH_CHANNEL_CONNECTING:
287 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000288 case SSH_CHANNEL_RDYNAMIC_OPEN:
289 case SSH_CHANNEL_RDYNAMIC_FINISH:
Damien Millerd47c62a2005-12-13 19:33:57 +1100290 case SSH_CHANNEL_OPENING:
291 case SSH_CHANNEL_OPEN:
Damien Miller36187092013-06-10 13:07:11 +1000292 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000293 case SSH_CHANNEL_MUX_PROXY:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000294 return c;
Damien Millerd47c62a2005-12-13 19:33:57 +1100295 }
296 logit("Non-public channel %d, type %d.", id, c->type);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000297 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100298}
299
300/*
Damien Millere247cc42000-05-07 12:03:14 +1000301 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000302 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100303 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000304static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000305channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000306 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000308 struct ssh_channels *sc = ssh->chanctxt;
309
Damien Miller95def091999-11-25 00:26:21 +1100310 /* Update the maximum file descriptor value. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000311 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd);
312 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd);
313 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100314
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100315 if (rfd != -1)
316 fcntl(rfd, F_SETFD, FD_CLOEXEC);
317 if (wfd != -1 && wfd != rfd)
318 fcntl(wfd, F_SETFD, FD_CLOEXEC);
319 if (efd != -1 && efd != rfd && efd != wfd)
320 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000321
Damien Miller6f83b8e2000-05-02 09:23:45 +1000322 c->rfd = rfd;
323 c->wfd = wfd;
324 c->sock = (rfd == wfd) ? rfd : -1;
325 c->efd = efd;
326 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100327
Darren Tuckered3cdc02008-06-16 23:29:18 +1000328 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000329 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000330#ifdef _AIX
331 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000332 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000333#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100334
Damien Miller69b69aa2000-10-28 14:19:58 +1100335 /* enable nonblocking mode */
336 if (nonblock) {
337 if (rfd != -1)
338 set_nonblock(rfd);
339 if (wfd != -1)
340 set_nonblock(wfd);
341 if (efd != -1)
342 set_nonblock(efd);
343 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000344}
345
346/*
347 * Allocate a new channel object and set its type and socket. This will cause
348 * remote_name to be freed.
349 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000350Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000351channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000352 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000353{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000354 struct ssh_channels *sc = ssh->chanctxt;
355 u_int i, found;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000356 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Miller95def091999-11-25 00:26:21 +1100358 /* Try to find a free slot where to put the new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000359 for (i = 0; i < sc->channels_alloc; i++) {
360 if (sc->channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100361 /* Found a free slot. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000362 found = i;
Damien Miller95def091999-11-25 00:26:21 +1100363 break;
364 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000365 }
366 if (i >= sc->channels_alloc) {
367 /*
368 * There are no free slots. Take last+1 slot and expand
369 * the array.
370 */
371 found = sc->channels_alloc;
372 if (sc->channels_alloc > CHANNELS_MAX_CHANNELS)
373 fatal("%s: internal error: channels_alloc %d too big",
374 __func__, sc->channels_alloc);
375 sc->channels = xrecallocarray(sc->channels, sc->channels_alloc,
376 sc->channels_alloc + 10, sizeof(*sc->channels));
377 sc->channels_alloc += 10;
378 debug2("channel: expanding %d", sc->channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100379 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000380 /* Initialize and return new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000381 c = sc->channels[found] = xcalloc(1, sizeof(Channel));
382 if ((c->input = sshbuf_new()) == NULL ||
383 (c->output = sshbuf_new()) == NULL ||
384 (c->extended = sshbuf_new()) == NULL)
385 fatal("%s: sshbuf_new failed", __func__);
Damien Miller5144df92002-01-22 23:28:45 +1100386 c->ostate = CHAN_OUTPUT_OPEN;
387 c->istate = CHAN_INPUT_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000388 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Miller95def091999-11-25 00:26:21 +1100389 c->self = found;
390 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000391 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000392 c->local_window = window;
393 c->local_window_max = window;
Damien Millerbd483e72000-04-30 10:00:53 +1000394 c->local_maxpacket = maxpack;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000395 c->remote_name = xstrdup(remote_name);
Damien Millere1537f92010-01-26 13:26:22 +1100396 c->ctl_chan = -1;
Darren Tucker876045b2010-01-08 17:08:00 +1100397 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000398 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100399 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000400 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000402
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000403static void
404channel_find_maxfd(struct ssh_channels *sc)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000405{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000406 u_int i;
407 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000408 Channel *c;
409
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000410 for (i = 0; i < sc->channels_alloc; i++) {
411 c = sc->channels[i];
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000412 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000413 max = MAXIMUM(max, c->rfd);
414 max = MAXIMUM(max, c->wfd);
415 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000416 }
417 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000418 sc->channel_max_fd = max;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000419}
420
421int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000422channel_close_fd(struct ssh *ssh, int *fdp)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000423{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000424 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000425 int ret = 0, fd = *fdp;
426
427 if (fd != -1) {
428 ret = close(fd);
429 *fdp = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000430 if (fd == sc->channel_max_fd)
431 channel_find_maxfd(sc);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000432 }
433 return ret;
434}
435
Damien Miller6f83b8e2000-05-02 09:23:45 +1000436/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000437static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000438channel_close_fds(struct ssh *ssh, Channel *c)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000439{
tb@openbsd.org50693202018-02-05 05:36:49 +0000440 int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
441
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000442 channel_close_fd(ssh, &c->sock);
tb@openbsd.org50693202018-02-05 05:36:49 +0000443 if (rfd != sock)
444 channel_close_fd(ssh, &c->rfd);
445 if (wfd != sock && wfd != rfd)
446 channel_close_fd(ssh, &c->wfd);
447 if (efd != sock && efd != rfd && efd != wfd)
448 channel_close_fd(ssh, &c->efd);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000449}
450
451static void
djm@openbsd.org115063a2018-06-06 18:22:41 +0000452fwd_perm_clear(struct permission *perm)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000453{
djm@openbsd.org115063a2018-06-06 18:22:41 +0000454 free(perm->host_to_connect);
455 free(perm->listen_host);
456 free(perm->listen_path);
457 bzero(perm, sizeof(*perm));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000458}
459
djm@openbsd.org115063a2018-06-06 18:22:41 +0000460/* Returns an printable name for the specified forwarding permission list */
461static const char *
462fwd_ident(int who, int where)
463{
464 if (who == FORWARD_ADM) {
465 if (where == FORWARD_LOCAL)
466 return "admin local";
467 else if (where == FORWARD_REMOTE)
468 return "admin remote";
469 } else if (who == FORWARD_USER) {
470 if (where == FORWARD_LOCAL)
471 return "user local";
472 else if (where == FORWARD_REMOTE)
473 return "user remote";
474 }
475 fatal("Unknown forward permission list %d/%d", who, where);
476}
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000477
djm@openbsd.org115063a2018-06-06 18:22:41 +0000478/* Returns the forwarding permission list for the specified direction */
479static struct permission_set *
480permission_set_get(struct ssh *ssh, int where)
481{
482 struct ssh_channels *sc = ssh->chanctxt;
483
484 switch (where) {
485 case FORWARD_LOCAL:
486 return &sc->local_perms;
487 break;
488 case FORWARD_REMOTE:
489 return &sc->remote_perms;
490 break;
491 default:
492 fatal("%s: invalid forwarding direction %d", __func__, where);
493 }
494}
495
dtucker@openbsd.org4b7dd222019-06-07 14:18:48 +0000496/* Returns pointers to the specified forwarding list and its element count */
djm@openbsd.org115063a2018-06-06 18:22:41 +0000497static void
498permission_set_get_array(struct ssh *ssh, int who, int where,
499 struct permission ***permpp, u_int **npermpp)
500{
501 struct permission_set *pset = permission_set_get(ssh, where);
502
503 switch (who) {
504 case FORWARD_USER:
505 *permpp = &pset->permitted_user;
506 *npermpp = &pset->num_permitted_user;
507 break;
508 case FORWARD_ADM:
509 *permpp = &pset->permitted_admin;
510 *npermpp = &pset->num_permitted_admin;
511 break;
512 default:
513 fatal("%s: invalid forwarding client %d", __func__, who);
514 }
515}
516
517/* Adds an entry to the spcified forwarding list */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000518static int
djm@openbsd.org115063a2018-06-06 18:22:41 +0000519permission_set_add(struct ssh *ssh, int who, int where,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000520 const char *host_to_connect, int port_to_connect,
521 const char *listen_host, const char *listen_path, int listen_port,
522 Channel *downstream)
523{
djm@openbsd.org115063a2018-06-06 18:22:41 +0000524 struct permission **permp;
525 u_int n, *npermp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000526
djm@openbsd.org115063a2018-06-06 18:22:41 +0000527 permission_set_get_array(ssh, who, where, &permp, &npermp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000528
djm@openbsd.org115063a2018-06-06 18:22:41 +0000529 if (*npermp >= INT_MAX)
530 fatal("%s: %s overflow", __func__, fwd_ident(who, where));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000531
djm@openbsd.org115063a2018-06-06 18:22:41 +0000532 *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp));
533 n = (*npermp)++;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000534#define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
djm@openbsd.org115063a2018-06-06 18:22:41 +0000535 (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect);
536 (*permp)[n].port_to_connect = port_to_connect;
537 (*permp)[n].listen_host = MAYBE_DUP(listen_host);
538 (*permp)[n].listen_path = MAYBE_DUP(listen_path);
539 (*permp)[n].listen_port = listen_port;
540 (*permp)[n].downstream = downstream;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000541#undef MAYBE_DUP
542 return (int)n;
543}
544
545static void
546mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
547{
548 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +0000549 struct permission_set *pset = &sc->local_perms;
550 struct permission *perm;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000551 int r;
552 u_int i;
553
djm@openbsd.org115063a2018-06-06 18:22:41 +0000554 for (i = 0; i < pset->num_permitted_user; i++) {
555 perm = &pset->permitted_user[i];
556 if (perm->downstream != c)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000557 continue;
558
559 /* cancel on the server, since mux client is gone */
560 debug("channel %d: cleanup remote forward for %s:%u",
djm@openbsd.org115063a2018-06-06 18:22:41 +0000561 c->self, perm->listen_host, perm->listen_port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000562 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
563 (r = sshpkt_put_cstring(ssh,
564 "cancel-tcpip-forward")) != 0 ||
565 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
566 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +0000567 channel_rfwd_bind_host(perm->listen_host))) != 0 ||
568 (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000569 (r = sshpkt_send(ssh)) != 0) {
570 fatal("%s: channel %i: %s", __func__,
571 c->self, ssh_err(r));
572 }
djm@openbsd.org115063a2018-06-06 18:22:41 +0000573 fwd_perm_clear(perm); /* unregister */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000574 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000575}
576
577/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000578void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000579channel_free(struct ssh *ssh, Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000580{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000581 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000582 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000583 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000584 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000585 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000586
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000587 for (n = 0, i = 0; i < sc->channels_alloc; i++) {
588 if ((other = sc->channels[i]) == NULL)
589 continue;
590 n++;
591 /* detach from mux client and prepare for closing */
592 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
593 other->type == SSH_CHANNEL_MUX_PROXY &&
594 other->mux_ctx == c) {
595 other->mux_ctx = NULL;
596 other->type = SSH_CHANNEL_OPEN;
597 other->istate = CHAN_INPUT_CLOSED;
598 other->ostate = CHAN_OUTPUT_CLOSED;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000599 }
600 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000601 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000602 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000603
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000604 if (c->type == SSH_CHANNEL_MUX_CLIENT)
605 mux_remove_remote_forwardings(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000606
markus@openbsd.org5d140192018-07-27 12:03:17 +0000607 if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) {
608 s = channel_open_message(ssh);
609 debug3("channel %d: status: %s", c->self, s);
610 free(s);
611 }
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000612
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000613 channel_close_fds(ssh, c);
614 sshbuf_free(c->input);
615 sshbuf_free(c->output);
616 sshbuf_free(c->extended);
617 c->input = c->output = c->extended = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000618 free(c->remote_name);
619 c->remote_name = NULL;
620 free(c->path);
621 c->path = NULL;
622 free(c->listening_addr);
623 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000624 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
625 if (cc->abandon_cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000626 cc->abandon_cb(ssh, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +1000627 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100628 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000629 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000630 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000631 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000632 c->filter_cleanup(ssh, c->self, c->filter_ctx);
633 sc->channels[c->self] = NULL;
millert@openbsd.org3e8d1852017-09-19 12:10:30 +0000634 explicit_bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +1000635 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000636}
637
Ben Lindstrome9c99912001-06-09 00:41:05 +0000638void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000639channel_free_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000640{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000641 u_int i;
dtucker@openbsd.org62be1ff2019-05-03 04:11:00 +0000642 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000643
dtucker@openbsd.org62be1ff2019-05-03 04:11:00 +0000644 for (i = 0; i < sc->channels_alloc; i++)
645 if (sc->channels[i] != NULL)
646 channel_free(ssh, sc->channels[i]);
647
648 free(sc->channels);
649 sc->channels = NULL;
650 sc->channels_alloc = 0;
651 sc->channel_max_fd = 0;
652
653 free(sc->x11_saved_display);
654 sc->x11_saved_display = NULL;
655
656 free(sc->x11_saved_proto);
657 sc->x11_saved_proto = NULL;
658
659 free(sc->x11_saved_data);
660 sc->x11_saved_data = NULL;
661 sc->x11_saved_data_len = 0;
662
663 free(sc->x11_fake_data);
664 sc->x11_fake_data = NULL;
665 sc->x11_fake_data_len = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666}
667
668/*
669 * Closes the sockets/fds of all channels. This is used to close extra file
670 * descriptors after a fork.
671 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000672void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000673channel_close_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000674{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000675 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000676
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000677 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
678 if (ssh->chanctxt->channels[i] != NULL)
679 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000680}
681
682/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000683 * Stop listening to channels.
684 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000685void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000686channel_stop_listening(struct ssh *ssh)
Ben Lindstrom809744e2001-07-04 05:26:06 +0000687{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000688 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000689 Channel *c;
690
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000691 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
692 c = ssh->chanctxt->channels[i];
Ben Lindstrom809744e2001-07-04 05:26:06 +0000693 if (c != NULL) {
694 switch (c->type) {
695 case SSH_CHANNEL_AUTH_SOCKET:
696 case SSH_CHANNEL_PORT_LISTENER:
697 case SSH_CHANNEL_RPORT_LISTENER:
698 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000699 case SSH_CHANNEL_UNIX_LISTENER:
700 case SSH_CHANNEL_RUNIX_LISTENER:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000701 channel_close_fd(ssh, &c->sock);
702 channel_free(ssh, c);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000703 break;
704 }
705 }
706 }
707}
708
709/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000710 * Returns true if no channel has too much buffered data, and false if one or
711 * more channel is overfull.
712 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000713int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000714channel_not_very_much_buffered_data(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000715{
716 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000717 u_int maxsize = ssh_packet_get_maxsize(ssh);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000718 Channel *c;
719
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000720 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
721 c = ssh->chanctxt->channels[i];
722 if (c == NULL || c->type != SSH_CHANNEL_OPEN)
723 continue;
724 if (sshbuf_len(c->output) > maxsize) {
725 debug2("channel %d: big output buffer %zu > %u",
726 c->self, sshbuf_len(c->output), maxsize);
727 return 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728 }
729 }
730 return 1;
731}
732
733/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000734int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000735channel_still_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000736{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000737 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000738 Channel *c;
739
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000740 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
741 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000742 if (c == NULL)
743 continue;
744 switch (c->type) {
745 case SSH_CHANNEL_X11_LISTENER:
746 case SSH_CHANNEL_PORT_LISTENER:
747 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100748 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000749 case SSH_CHANNEL_CLOSED:
750 case SSH_CHANNEL_AUTH_SOCKET:
751 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000752 case SSH_CHANNEL_RDYNAMIC_OPEN:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753 case SSH_CHANNEL_CONNECTING:
754 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000755 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000756 case SSH_CHANNEL_UNIX_LISTENER:
757 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000758 continue;
759 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000760 continue;
761 case SSH_CHANNEL_OPENING:
762 case SSH_CHANNEL_OPEN:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000763 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000764 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100765 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000766 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000767 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000768 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000769 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000770 /* NOTREACHED */
771 }
772 }
773 return 0;
774}
775
776/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000777int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000778channel_find_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000779{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000780 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000781 Channel *c;
782
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000783 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
784 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000785 if (c == NULL || !c->have_remote_id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000786 continue;
787 switch (c->type) {
788 case SSH_CHANNEL_CLOSED:
789 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000790 case SSH_CHANNEL_RDYNAMIC_OPEN:
791 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 case SSH_CHANNEL_X11_LISTENER:
793 case SSH_CHANNEL_PORT_LISTENER:
794 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100795 case SSH_CHANNEL_MUX_LISTENER:
796 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000797 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000798 case SSH_CHANNEL_OPENING:
799 case SSH_CHANNEL_CONNECTING:
800 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000801 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000802 case SSH_CHANNEL_UNIX_LISTENER:
803 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000804 continue;
805 case SSH_CHANNEL_LARVAL:
806 case SSH_CHANNEL_AUTH_SOCKET:
807 case SSH_CHANNEL_OPEN:
808 case SSH_CHANNEL_X11_OPEN:
809 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000810 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000811 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000812 /* NOTREACHED */
813 }
814 }
815 return -1;
816}
817
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000818/* Returns the state of the channel's extended usage flag */
819const char *
820channel_format_extended_usage(const Channel *c)
821{
822 if (c->efd == -1)
823 return "closed";
824
825 switch (c->extended_usage) {
826 case CHAN_EXTENDED_WRITE:
827 return "write";
828 case CHAN_EXTENDED_READ:
829 return "read";
830 case CHAN_EXTENDED_IGNORE:
831 return "ignore";
832 default:
833 return "UNKNOWN";
834 }
835}
836
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000837static char *
838channel_format_status(const Channel *c)
839{
840 char *ret = NULL;
841
842 xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu "
843 "fd %d/%d/%d sock %d cc %d",
844 c->type,
845 c->have_remote_id ? "r" : "nr", c->remote_id,
846 c->istate, sshbuf_len(c->input),
847 c->ostate, sshbuf_len(c->output),
848 channel_format_extended_usage(c), sshbuf_len(c->extended),
849 c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan);
850 return ret;
851}
852
Ben Lindstrome9c99912001-06-09 00:41:05 +0000853/*
854 * Returns a message describing the currently open forwarded connections,
855 * suitable for sending to the client. The message contains crlf pairs for
856 * newlines.
857 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000858char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000859channel_open_message(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000860{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000861 struct sshbuf *buf;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000862 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000863 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000864 int r;
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000865 char *cp, *ret;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000866
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000867 if ((buf = sshbuf_new()) == NULL)
868 fatal("%s: sshbuf_new", __func__);
869 if ((r = sshbuf_putf(buf,
870 "The following connections are open:\r\n")) != 0)
871 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
872 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
873 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000874 if (c == NULL)
875 continue;
876 switch (c->type) {
877 case SSH_CHANNEL_X11_LISTENER:
878 case SSH_CHANNEL_PORT_LISTENER:
879 case SSH_CHANNEL_RPORT_LISTENER:
880 case SSH_CHANNEL_CLOSED:
881 case SSH_CHANNEL_AUTH_SOCKET:
882 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000883 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100884 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000885 case SSH_CHANNEL_UNIX_LISTENER:
886 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000887 continue;
888 case SSH_CHANNEL_LARVAL:
889 case SSH_CHANNEL_OPENING:
890 case SSH_CHANNEL_CONNECTING:
891 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000892 case SSH_CHANNEL_RDYNAMIC_OPEN:
893 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000894 case SSH_CHANNEL_OPEN:
895 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000896 case SSH_CHANNEL_MUX_PROXY:
897 case SSH_CHANNEL_MUX_CLIENT:
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000898 cp = channel_format_status(c);
899 if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n",
900 c->self, c->remote_name, cp)) != 0) {
901 free(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000902 fatal("%s: sshbuf_putf: %s",
903 __func__, ssh_err(r));
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000904 }
905 free(cp);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000906 continue;
907 default:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000908 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000909 /* NOTREACHED */
910 }
911 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000912 if ((ret = sshbuf_dup_string(buf)) == NULL)
913 fatal("%s: sshbuf_dup_string", __func__);
914 sshbuf_free(buf);
915 return ret;
916}
917
918static void
919open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
920{
921 int r;
922
923 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
924 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
925 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
926 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
927 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
928 fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r));
929 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000930}
931
932void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000933channel_send_open(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000934{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000935 Channel *c = channel_lookup(ssh, id);
936 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000937
Ben Lindstrome9c99912001-06-09 00:41:05 +0000938 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000939 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000940 return;
941 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000942 debug2("channel %d: send open", id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000943 open_preamble(ssh, __func__, c, c->ctype);
944 if ((r = sshpkt_send(ssh)) != 0)
945 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000946}
947
948void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000949channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000950{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000951 Channel *c = channel_lookup(ssh, id);
952 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000953
Ben Lindstrome9c99912001-06-09 00:41:05 +0000954 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000955 logit("%s: %d: unknown channel id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000956 return;
957 }
djm@openbsd.org9f532292017-09-12 06:35:31 +0000958 if (!c->have_remote_id)
959 fatal(":%s: channel %d: no remote id", __func__, c->self);
960
Damien Miller0e220db2004-06-15 10:34:08 +1000961 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000962 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
963 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
964 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
965 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
966 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
967 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000968}
Damien Miller4f7becb2006-03-26 14:10:14 +1100969
Ben Lindstrome9c99912001-06-09 00:41:05 +0000970void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000971channel_register_status_confirm(struct ssh *ssh, int id,
972 channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
Damien Millerb84886b2008-05-19 15:05:07 +1000973{
974 struct channel_confirm *cc;
975 Channel *c;
976
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000977 if ((c = channel_lookup(ssh, id)) == NULL)
978 fatal("%s: %d: bad id", __func__, id);
Damien Millerb84886b2008-05-19 15:05:07 +1000979
Damien Miller6c81fee2013-11-08 12:19:55 +1100980 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000981 cc->cb = cb;
982 cc->abandon_cb = abandon_cb;
983 cc->ctx = ctx;
984 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
985}
986
987void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000988channel_register_open_confirm(struct ssh *ssh, int id,
989 channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000990{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000991 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000992
Ben Lindstrome9c99912001-06-09 00:41:05 +0000993 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000994 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000995 return;
996 }
Damien Millerb84886b2008-05-19 15:05:07 +1000997 c->open_confirm = fn;
998 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000999}
Damien Miller4f7becb2006-03-26 14:10:14 +11001000
Ben Lindstrome9c99912001-06-09 00:41:05 +00001001void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001002channel_register_cleanup(struct ssh *ssh, int id,
1003 channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001004{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001005 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001006
Ben Lindstrome9c99912001-06-09 00:41:05 +00001007 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001008 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001009 return;
1010 }
Ben Lindstrom809744e2001-07-04 05:26:06 +00001011 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +11001012 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001013}
Damien Miller4f7becb2006-03-26 14:10:14 +11001014
Ben Lindstrome9c99912001-06-09 00:41:05 +00001015void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001016channel_cancel_cleanup(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001017{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001018 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001019
Ben Lindstrome9c99912001-06-09 00:41:05 +00001020 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001021 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001022 return;
1023 }
Ben Lindstrom809744e2001-07-04 05:26:06 +00001024 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +11001025 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001026}
Damien Miller4f7becb2006-03-26 14:10:14 +11001027
Ben Lindstrome9c99912001-06-09 00:41:05 +00001028void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001029channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +10001030 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001031{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001032 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001033
Ben Lindstrome9c99912001-06-09 00:41:05 +00001034 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001035 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001036 return;
1037 }
Damien Miller077b2382005-12-31 16:22:32 +11001038 c->input_filter = ifn;
1039 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001040 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +10001041 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001042}
1043
1044void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001045channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +10001046 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001047{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001048 Channel *c = channel_lookup(ssh, id);
1049 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001050
Ben Lindstrome9c99912001-06-09 00:41:05 +00001051 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
1052 fatal("channel_activate for non-larval channel %d.", id);
djm@openbsd.org9f532292017-09-12 06:35:31 +00001053 if (!c->have_remote_id)
1054 fatal(":%s: channel %d: no remote id", __func__, c->self);
1055
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001056 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001057 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +11001058 c->local_window = c->local_window_max = window_max;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001059
1060 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1061 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1062 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1063 (r = sshpkt_send(ssh)) != 0)
1064 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001065}
1066
Ben Lindstrombba81212001-06-25 05:01:22 +00001067static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001068channel_pre_listener(struct ssh *ssh, Channel *c,
1069 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001070{
1071 FD_SET(c->sock, readset);
1072}
1073
Ben Lindstrombba81212001-06-25 05:01:22 +00001074static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001075channel_pre_connecting(struct ssh *ssh, Channel *c,
1076 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001077{
1078 debug3("channel %d: waiting for connection", c->self);
1079 FD_SET(c->sock, writeset);
1080}
1081
Ben Lindstrombba81212001-06-25 05:01:22 +00001082static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001083channel_pre_open(struct ssh *ssh, Channel *c,
1084 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001085{
Damien Miller33b13562000-04-04 14:38:59 +10001086 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001087 c->remote_window > 0 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001088 sshbuf_len(c->input) < c->remote_window &&
1089 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Miller33b13562000-04-04 14:38:59 +10001090 FD_SET(c->rfd, readset);
1091 if (c->ostate == CHAN_OUTPUT_OPEN ||
1092 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001093 if (sshbuf_len(c->output) > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001094 FD_SET(c->wfd, writeset);
1095 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +00001096 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001097 debug2("channel %d: "
1098 "obuf_empty delayed efd %d/(%zu)", c->self,
1099 c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001100 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001101 chan_obuf_empty(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10001102 }
1103 }
1104 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001105 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
1106 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +10001107 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001108 sshbuf_len(c->extended) > 0)
Damien Miller33b13562000-04-04 14:38:59 +10001109 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +10001110 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1111 (c->extended_usage == CHAN_EXTENDED_READ ||
1112 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001113 sshbuf_len(c->extended) < c->remote_window)
Damien Miller33b13562000-04-04 14:38:59 +10001114 FD_SET(c->efd, readset);
1115 }
Damien Miller0e220db2004-06-15 10:34:08 +10001116 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +10001117}
1118
Damien Millerb38eff82000-04-01 11:09:21 +10001119/*
1120 * This is a special state for X11 authentication spoofing. An opened X11
1121 * connection (when authentication spoofing is being done) remains in this
1122 * state until the first packet has been completely read. The authentication
1123 * data in that packet is then substituted by the real data if it matches the
1124 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +10001125 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +00001126 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +10001127 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001128static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001129x11_open_helper(struct ssh *ssh, struct sshbuf *b)
Damien Millerb38eff82000-04-01 11:09:21 +10001130{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001131 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001132 u_char *ucp;
1133 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001134
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001135 /* Is this being called after the refusal deadline? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001136 if (sc->x11_refuse_time != 0 &&
1137 (u_int)monotime() >= sc->x11_refuse_time) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001138 verbose("Rejected X11 connection after ForwardX11Timeout "
1139 "expired");
1140 return -1;
1141 }
1142
Damien Millerb38eff82000-04-01 11:09:21 +10001143 /* Check if the fixed size part of the packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001144 if (sshbuf_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +10001145 return 0;
1146
1147 /* Parse the lengths of variable-length fields. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001148 ucp = sshbuf_mutable_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +10001149 if (ucp[0] == 0x42) { /* Byte order MSB first. */
1150 proto_len = 256 * ucp[6] + ucp[7];
1151 data_len = 256 * ucp[8] + ucp[9];
1152 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1153 proto_len = ucp[6] + 256 * ucp[7];
1154 data_len = ucp[8] + 256 * ucp[9];
1155 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001156 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001157 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001158 return -1;
1159 }
1160
1161 /* Check if the whole packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001162 if (sshbuf_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001163 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1164 return 0;
1165
1166 /* Check if authentication protocol matches. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001167 if (proto_len != strlen(sc->x11_saved_proto) ||
1168 memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001169 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001170 return -1;
1171 }
1172 /* Check if authentication data matches our fake data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001173 if (data_len != sc->x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001174 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001175 sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001176 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001177 return -1;
1178 }
1179 /* Check fake data length */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001180 if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
Damien Millerb38eff82000-04-01 11:09:21 +10001181 error("X11 fake_data_len %d != saved_data_len %d",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001182 sc->x11_fake_data_len, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001183 return -1;
1184 }
1185 /*
1186 * Received authentication protocol and data match
1187 * our fake data. Substitute the fake data with real
1188 * data.
1189 */
1190 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001191 sc->x11_saved_data, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001192 return 1;
1193}
1194
Ben Lindstrombba81212001-06-25 05:01:22 +00001195static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001196channel_pre_x11_open(struct ssh *ssh, Channel *c,
1197 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001198{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001199 int ret = x11_open_helper(ssh, c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001200
1201 /* c->force_drain = 1; */
1202
Damien Millerb38eff82000-04-01 11:09:21 +10001203 if (ret == 1) {
1204 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001205 channel_pre_open(ssh, c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001206 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001207 logit("X11 connection rejected because of wrong authentication.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001208 debug2("X11 rejected %d i%d/o%d",
1209 c->self, c->istate, c->ostate);
1210 chan_read_failed(ssh, c);
1211 sshbuf_reset(c->input);
1212 chan_ibuf_empty(ssh, c);
1213 sshbuf_reset(c->output);
1214 chan_write_failed(ssh, c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001215 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001216 }
1217}
1218
Damien Millere1537f92010-01-26 13:26:22 +11001219static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001220channel_pre_mux_client(struct ssh *ssh,
1221 Channel *c, fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11001222{
Damien Millerd530f5f2010-05-21 14:57:10 +10001223 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001224 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Millere1537f92010-01-26 13:26:22 +11001225 FD_SET(c->rfd, readset);
1226 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1227 /* clear buffer immediately (discard any partial packet) */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001228 sshbuf_reset(c->input);
1229 chan_ibuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001230 /* Start output drain. XXX just kill chan? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001231 chan_rcvd_oclose(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001232 }
1233 if (c->ostate == CHAN_OUTPUT_OPEN ||
1234 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001235 if (sshbuf_len(c->output) > 0)
Damien Millere1537f92010-01-26 13:26:22 +11001236 FD_SET(c->wfd, writeset);
1237 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001238 chan_obuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001239 }
1240}
1241
Ben Lindstromb3921512001-04-11 15:57:50 +00001242/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +00001243static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001244channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001245{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001246 const u_char *p;
1247 char *host;
Damien Miller1781f532009-01-28 16:24:41 +11001248 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001249 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001250 struct {
1251 u_int8_t version;
1252 u_int8_t command;
1253 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001254 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001255 } s4_req, s4_rsp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001256 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001257
Ben Lindstromb3921512001-04-11 15:57:50 +00001258 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001259
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001260 have = sshbuf_len(input);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001261 len = sizeof(s4_req);
1262 if (have < len)
1263 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001264 p = sshbuf_ptr(input);
Damien Miller1781f532009-01-28 16:24:41 +11001265
1266 need = 1;
1267 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1268 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1269 debug2("channel %d: socks4a request", c->self);
1270 /* ... and needs an extra string (the hostname) */
1271 need = 2;
1272 }
1273 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001274 for (found = 0, i = len; i < have; i++) {
1275 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001276 found++;
1277 if (found == need)
1278 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001279 }
1280 if (i > 1024) {
1281 /* the peer is probably sending garbage */
1282 debug("channel %d: decode socks4: too long",
1283 c->self);
1284 return -1;
1285 }
1286 }
Damien Miller1781f532009-01-28 16:24:41 +11001287 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001288 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001289 if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1290 (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1291 (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1292 (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001293 debug("channels %d: decode socks4: %s", c->self, ssh_err(r));
1294 return -1;
1295 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001296 have = sshbuf_len(input);
1297 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001298 if (memchr(p, '\0', have) == NULL) {
1299 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001300 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001301 return -1;
1302 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001303 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001304 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001305 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001306 strlcpy(username, p, sizeof(username));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001307 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001308 fatal("%s: channel %d: consume: %s", __func__,
1309 c->self, ssh_err(r));
1310 }
Darren Tuckera627d422013-06-02 07:31:17 +10001311 free(c->path);
1312 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001313 if (need == 1) { /* SOCKS4: one string */
1314 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001315 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001316 } else { /* SOCKS4A: two strings */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001317 have = sshbuf_len(input);
1318 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001319 if (memchr(p, '\0', have) == NULL) {
1320 error("channel %d: decode socks4a: host not nul "
1321 "terminated", c->self);
1322 return -1;
1323 }
Damien Miller1781f532009-01-28 16:24:41 +11001324 len = strlen(p);
1325 debug2("channel %d: decode socks4a: host %s/%d",
1326 c->self, p, len);
1327 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001328 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001329 error("channel %d: hostname \"%.100s\" too long",
1330 c->self, p);
1331 return -1;
1332 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001333 c->path = xstrdup(p);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001334 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001335 fatal("%s: channel %d: consume: %s", __func__,
1336 c->self, ssh_err(r));
1337 }
Damien Miller1781f532009-01-28 16:24:41 +11001338 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001339 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001340
Damien Millerfbdeece2003-09-02 22:52:31 +10001341 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001342 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001343
Ben Lindstromb3921512001-04-11 15:57:50 +00001344 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001345 debug("channel %d: cannot handle: %s cn %d",
1346 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001347 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001348 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001349 s4_rsp.version = 0; /* vn: 0 for reply */
1350 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001351 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001352 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001353 if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001354 fatal("%s: channel %d: append reply: %s", __func__,
1355 c->self, ssh_err(r));
1356 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001357 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001358}
1359
Darren Tucker46471c92003-07-03 13:55:19 +10001360/* try to decode a socks5 header */
1361#define SSH_SOCKS5_AUTHDONE 0x1000
1362#define SSH_SOCKS5_NOAUTH 0x00
1363#define SSH_SOCKS5_IPV4 0x01
1364#define SSH_SOCKS5_DOMAIN 0x03
1365#define SSH_SOCKS5_IPV6 0x04
1366#define SSH_SOCKS5_CONNECT 0x01
1367#define SSH_SOCKS5_SUCCESS 0x00
1368
1369static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001370channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
Darren Tucker46471c92003-07-03 13:55:19 +10001371{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001372 /* XXX use get/put_u8 instead of trusting struct padding */
Darren Tucker46471c92003-07-03 13:55:19 +10001373 struct {
1374 u_int8_t version;
1375 u_int8_t command;
1376 u_int8_t reserved;
1377 u_int8_t atyp;
1378 } s5_req, s5_rsp;
1379 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001380 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001381 const u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001382 u_int have, need, i, found, nmethods, addrlen, af;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001383 int r;
Darren Tucker46471c92003-07-03 13:55:19 +10001384
1385 debug2("channel %d: decode socks5", c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001386 p = sshbuf_ptr(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001387 if (p[0] != 0x05)
1388 return -1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001389 have = sshbuf_len(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001390 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1391 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001392 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001393 return 0;
1394 nmethods = p[1];
1395 if (have < nmethods + 2)
1396 return 0;
1397 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001398 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001399 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001400 found = 1;
1401 break;
1402 }
1403 }
1404 if (!found) {
1405 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1406 c->self);
1407 return -1;
1408 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001409 if ((r = sshbuf_consume(input, nmethods + 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001410 fatal("%s: channel %d: consume: %s", __func__,
1411 c->self, ssh_err(r));
1412 }
1413 /* version, method */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001414 if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1415 (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001416 fatal("%s: channel %d: append reply: %s", __func__,
1417 c->self, ssh_err(r));
1418 }
Darren Tucker46471c92003-07-03 13:55:19 +10001419 c->flags |= SSH_SOCKS5_AUTHDONE;
1420 debug2("channel %d: socks5 auth done", c->self);
1421 return 0; /* need more */
1422 }
1423 debug2("channel %d: socks5 post auth", c->self);
1424 if (have < sizeof(s5_req)+1)
1425 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001426 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001427 if (s5_req.version != 0x05 ||
1428 s5_req.command != SSH_SOCKS5_CONNECT ||
1429 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001430 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001431 return -1;
1432 }
Darren Tucker47eede72005-03-14 23:08:12 +11001433 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001434 case SSH_SOCKS5_IPV4:
1435 addrlen = 4;
1436 af = AF_INET;
1437 break;
1438 case SSH_SOCKS5_DOMAIN:
1439 addrlen = p[sizeof(s5_req)];
1440 af = -1;
1441 break;
1442 case SSH_SOCKS5_IPV6:
1443 addrlen = 16;
1444 af = AF_INET6;
1445 break;
1446 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001447 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001448 return -1;
1449 }
Damien Miller0f077072006-07-10 22:21:02 +10001450 need = sizeof(s5_req) + addrlen + 2;
1451 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1452 need++;
1453 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001454 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001455 if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001456 fatal("%s: channel %d: consume: %s", __func__,
1457 c->self, ssh_err(r));
1458 }
1459 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1460 /* host string length */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001461 if ((r = sshbuf_consume(input, 1)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001462 fatal("%s: channel %d: consume: %s", __func__,
1463 c->self, ssh_err(r));
1464 }
1465 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001466 if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1467 (r = sshbuf_get(input, &dest_port, 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001468 debug("channel %d: parse addr/port: %s", c->self, ssh_err(r));
1469 return -1;
1470 }
Darren Tucker46471c92003-07-03 13:55:19 +10001471 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001472 free(c->path);
1473 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001474 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001475 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001476 error("channel %d: dynamic request: socks5 hostname "
1477 "\"%.100s\" too long", c->self, dest_addr);
1478 return -1;
1479 }
1480 c->path = xstrdup(dest_addr);
1481 } else {
1482 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1483 return -1;
1484 c->path = xstrdup(ntop);
1485 }
Darren Tucker46471c92003-07-03 13:55:19 +10001486 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001487
Damien Millerfbdeece2003-09-02 22:52:31 +10001488 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001489 c->self, c->path, c->host_port, s5_req.command);
1490
1491 s5_rsp.version = 0x05;
1492 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1493 s5_rsp.reserved = 0; /* ignored */
1494 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001495 dest_port = 0; /* ignored */
1496
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001497 if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1498 (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1499 (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001500 fatal("%s: channel %d: append reply: %s", __func__,
1501 c->self, ssh_err(r));
Darren Tucker46471c92003-07-03 13:55:19 +10001502 return 1;
1503}
1504
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001505Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001506channel_connect_stdio_fwd(struct ssh *ssh,
1507 const char *host_to_connect, u_short port_to_connect, int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001508{
1509 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001510
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001511 debug("%s %s:%d", __func__, host_to_connect, port_to_connect);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001512
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001513 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001514 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1515 0, "stdio-forward", /*nonblock*/0);
1516
1517 c->path = xstrdup(host_to_connect);
1518 c->host_port = port_to_connect;
1519 c->listening_port = 0;
1520 c->force_drain = 1;
1521
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001522 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1523 port_open_helper(ssh, c, "direct-tcpip");
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001524
1525 return c;
1526}
1527
Ben Lindstromb3921512001-04-11 15:57:50 +00001528/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001529static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001530channel_pre_dynamic(struct ssh *ssh, Channel *c,
1531 fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001532{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001533 const u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001534 u_int have;
1535 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001536
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001537 have = sshbuf_len(c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001538 debug2("channel %d: pre_dynamic: have %d", c->self, have);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001539 /* sshbuf_dump(c->input, stderr); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001540 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001541 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001542 /* need more */
1543 FD_SET(c->sock, readset);
1544 return;
1545 }
1546 /* try to guess the protocol */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001547 p = sshbuf_ptr(c->input);
1548 /* XXX sshbuf_peek_u8? */
Ben Lindstromb3921512001-04-11 15:57:50 +00001549 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001550 case 0x04:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001551 ret = channel_decode_socks4(c, c->input, c->output);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001552 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001553 case 0x05:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001554 ret = channel_decode_socks5(c, c->input, c->output);
Darren Tucker46471c92003-07-03 13:55:19 +10001555 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001556 default:
1557 ret = -1;
1558 break;
1559 }
1560 if (ret < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001561 chan_mark_dead(ssh, c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001562 } else if (ret == 0) {
1563 debug2("channel %d: pre_dynamic: need more", c->self);
1564 /* need more */
1565 FD_SET(c->sock, readset);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001566 if (sshbuf_len(c->output))
1567 FD_SET(c->sock, writeset);
Ben Lindstromb3921512001-04-11 15:57:50 +00001568 } else {
1569 /* switch to the next state */
1570 c->type = SSH_CHANNEL_OPENING;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001571 port_open_helper(ssh, c, "direct-tcpip");
Ben Lindstromb3921512001-04-11 15:57:50 +00001572 }
1573}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001574
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001575/* simulate read-error */
1576static void
1577rdynamic_close(struct ssh *ssh, Channel *c)
1578{
1579 c->type = SSH_CHANNEL_OPEN;
1580 chan_read_failed(ssh, c);
1581 sshbuf_reset(c->input);
1582 chan_ibuf_empty(ssh, c);
1583 sshbuf_reset(c->output);
1584 chan_write_failed(ssh, c);
1585}
1586
1587/* reverse dynamic port forwarding */
1588static void
1589channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c)
1590{
1591 const u_char *p;
1592 u_int have, len;
1593 int r, ret;
1594
1595 have = sshbuf_len(c->output);
1596 debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1597 /* sshbuf_dump(c->output, stderr); */
1598 /* EOF received */
1599 if (c->flags & CHAN_EOF_RCVD) {
1600 if ((r = sshbuf_consume(c->output, have)) != 0) {
1601 fatal("%s: channel %d: consume: %s",
1602 __func__, c->self, ssh_err(r));
1603 }
1604 rdynamic_close(ssh, c);
1605 return;
1606 }
1607 /* check if the fixed size part of the packet is in buffer. */
1608 if (have < 3)
1609 return;
1610 /* try to guess the protocol */
1611 p = sshbuf_ptr(c->output);
1612 switch (p[0]) {
1613 case 0x04:
1614 /* switch input/output for reverse forwarding */
1615 ret = channel_decode_socks4(c, c->output, c->input);
1616 break;
1617 case 0x05:
1618 ret = channel_decode_socks5(c, c->output, c->input);
1619 break;
1620 default:
1621 ret = -1;
1622 break;
1623 }
1624 if (ret < 0) {
1625 rdynamic_close(ssh, c);
1626 } else if (ret == 0) {
1627 debug2("channel %d: pre_rdynamic: need more", c->self);
1628 /* send socks request to peer */
1629 len = sshbuf_len(c->input);
1630 if (len > 0 && len < c->remote_window) {
1631 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1632 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1633 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1634 (r = sshpkt_send(ssh)) != 0) {
1635 fatal("%s: channel %i: rdynamic: %s", __func__,
1636 c->self, ssh_err(r));
1637 }
1638 if ((r = sshbuf_consume(c->input, len)) != 0) {
1639 fatal("%s: channel %d: consume: %s",
1640 __func__, c->self, ssh_err(r));
1641 }
1642 c->remote_window -= len;
1643 }
1644 } else if (rdynamic_connect_finish(ssh, c) < 0) {
1645 /* the connect failed */
1646 rdynamic_close(ssh, c);
1647 }
1648}
1649
Damien Millerb38eff82000-04-01 11:09:21 +10001650/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001651static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001652channel_post_x11_listener(struct ssh *ssh, Channel *c,
1653 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001654{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001655 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001656 struct sockaddr_storage addr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001657 int r, newsock, oerrno, remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001658 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001659 char buf[16384], *remote_ipaddr;
Damien Millerb38eff82000-04-01 11:09:21 +10001660
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001661 if (!FD_ISSET(c->sock, readset))
1662 return;
Damien Millerbd483e72000-04-30 10:00:53 +10001663
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001664 debug("X11 connection requested.");
1665 addrlen = sizeof(addr);
1666 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1667 if (c->single_connection) {
1668 oerrno = errno;
1669 debug2("single_connection: closing X11 listener.");
1670 channel_close_fd(ssh, &c->sock);
1671 chan_mark_dead(ssh, c);
1672 errno = oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001673 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001674 if (newsock == -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001675 if (errno != EINTR && errno != EWOULDBLOCK &&
1676 errno != ECONNABORTED)
1677 error("accept: %.100s", strerror(errno));
1678 if (errno == EMFILE || errno == ENFILE)
1679 c->notbefore = monotime() + 1;
1680 return;
1681 }
1682 set_nodelay(newsock);
1683 remote_ipaddr = get_peer_ipaddr(newsock);
1684 remote_port = get_peer_port(newsock);
1685 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1686 remote_ipaddr, remote_port);
1687
1688 nc = channel_new(ssh, "accepted x11 socket",
1689 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1690 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1691 open_preamble(ssh, __func__, nc, "x11");
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001692 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1693 (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001694 fatal("%s: channel %i: reply %s", __func__,
1695 c->self, ssh_err(r));
1696 }
1697 if ((r = sshpkt_send(ssh)) != 0)
1698 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
1699 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001700}
1701
Ben Lindstrombba81212001-06-25 05:01:22 +00001702static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001703port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001704{
Damien Miller4def1842013-12-29 17:45:26 +11001705 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001706 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001707 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001708 int remote_port = get_peer_port(c->sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001709 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001710
Damien Millerd6369432010-02-02 17:02:07 +11001711 if (remote_port == -1) {
1712 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001713 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001714 remote_ipaddr = xstrdup("127.0.0.1");
1715 remote_port = 65535;
1716 }
1717
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001718 free(c->remote_name);
1719 xasprintf(&c->remote_name,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001720 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001721 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001722 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001723 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001724
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001725 open_preamble(ssh, __func__, c, rtype);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001726 if (strcmp(rtype, "direct-tcpip") == 0) {
1727 /* target host, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001728 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1729 (r = sshpkt_put_u32(ssh, c->host_port)) != 0) {
1730 fatal("%s: channel %i: reply %s", __func__,
1731 c->self, ssh_err(r));
1732 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001733 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1734 /* target path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001735 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1736 fatal("%s: channel %i: reply %s", __func__,
1737 c->self, ssh_err(r));
1738 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001739 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1740 /* listen path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001741 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1742 fatal("%s: channel %i: reply %s", __func__,
1743 c->self, ssh_err(r));
1744 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001745 } else {
1746 /* listen address, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001747 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1748 (r = sshpkt_put_u32(ssh, local_port)) != 0) {
1749 fatal("%s: channel %i: reply %s", __func__,
1750 c->self, ssh_err(r));
1751 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001752 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001753 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1754 /* reserved for future owner/mode info */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001755 if ((r = sshpkt_put_cstring(ssh, "")) != 0) {
1756 fatal("%s: channel %i: reply %s", __func__,
1757 c->self, ssh_err(r));
1758 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001759 } else {
1760 /* originator host and port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001761 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1762 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) {
1763 fatal("%s: channel %i: reply %s", __func__,
1764 c->self, ssh_err(r));
1765 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001766 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001767 if ((r = sshpkt_send(ssh)) != 0)
1768 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001769 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001770 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001771}
1772
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001773void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001774channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001775{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001776 ssh->chanctxt->x11_refuse_time = refuse_time;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001777}
1778
Damien Millerb38eff82000-04-01 11:09:21 +10001779/*
1780 * This socket is listening for connections to a forwarded TCP/IP port.
1781 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001782static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001783channel_post_port_listener(struct ssh *ssh, Channel *c,
1784 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001785{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001786 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001787 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001788 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001789 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001790 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001791
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001792 if (!FD_ISSET(c->sock, readset))
1793 return;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001794
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001795 debug("Connection to port %d forwarding to %.100s port %d requested.",
1796 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001797
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001798 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1799 nextstate = SSH_CHANNEL_OPENING;
1800 rtype = "forwarded-tcpip";
1801 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1802 nextstate = SSH_CHANNEL_OPENING;
1803 rtype = "forwarded-streamlocal@openssh.com";
1804 } else if (c->host_port == PORT_STREAMLOCAL) {
1805 nextstate = SSH_CHANNEL_OPENING;
1806 rtype = "direct-streamlocal@openssh.com";
1807 } else if (c->host_port == 0) {
1808 nextstate = SSH_CHANNEL_DYNAMIC;
1809 rtype = "dynamic-tcpip";
1810 } else {
1811 nextstate = SSH_CHANNEL_OPENING;
1812 rtype = "direct-tcpip";
Damien Millerb38eff82000-04-01 11:09:21 +10001813 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001814
1815 addrlen = sizeof(addr);
1816 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001817 if (newsock == -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001818 if (errno != EINTR && errno != EWOULDBLOCK &&
1819 errno != ECONNABORTED)
1820 error("accept: %.100s", strerror(errno));
1821 if (errno == EMFILE || errno == ENFILE)
1822 c->notbefore = monotime() + 1;
1823 return;
1824 }
1825 if (c->host_port != PORT_STREAMLOCAL)
1826 set_nodelay(newsock);
1827 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1828 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1829 nc->listening_port = c->listening_port;
1830 nc->host_port = c->host_port;
1831 if (c->path != NULL)
1832 nc->path = xstrdup(c->path);
1833
1834 if (nextstate != SSH_CHANNEL_DYNAMIC)
1835 port_open_helper(ssh, nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001836}
1837
1838/*
1839 * This is the authentication agent socket listening for connections from
1840 * clients.
1841 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001842static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001843channel_post_auth_listener(struct ssh *ssh, Channel *c,
1844 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001845{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001846 Channel *nc;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001847 int r, newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001848 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001849 socklen_t addrlen;
1850
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001851 if (!FD_ISSET(c->sock, readset))
1852 return;
1853
1854 addrlen = sizeof(addr);
1855 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001856 if (newsock == -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001857 error("accept from auth socket: %.100s", strerror(errno));
1858 if (errno == EMFILE || errno == ENFILE)
1859 c->notbefore = monotime() + 1;
1860 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001861 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001862 nc = channel_new(ssh, "accepted auth socket",
1863 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1864 c->local_window_max, c->local_maxpacket,
1865 0, "accepted auth socket", 1);
1866 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1867 if ((r = sshpkt_send(ssh)) != 0)
1868 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001869}
1870
Ben Lindstrombba81212001-06-25 05:01:22 +00001871static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001872channel_post_connecting(struct ssh *ssh, Channel *c,
1873 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001874{
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001875 int err = 0, sock, isopen, r;
Ben Lindstrom11180952001-07-04 05:13:35 +00001876 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001877
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001878 if (!FD_ISSET(c->sock, writeset))
1879 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001880 if (!c->have_remote_id)
1881 fatal(":%s: channel %d: no remote id", __func__, c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001882 /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1883 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001884 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) == -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001885 err = errno;
1886 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001887 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001888 if (err == 0) {
1889 debug("channel %d: connected to %s port %d",
1890 c->self, c->connect_ctx.host, c->connect_ctx.port);
1891 channel_connect_ctx_free(&c->connect_ctx);
1892 c->type = SSH_CHANNEL_OPEN;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001893 if (isopen) {
1894 /* no message necessary */
1895 } else {
1896 if ((r = sshpkt_start(ssh,
1897 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1898 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1899 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1900 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1901 (r = sshpkt_put_u32(ssh, c->local_maxpacket))
1902 != 0)
1903 fatal("%s: channel %i: confirm: %s", __func__,
1904 c->self, ssh_err(r));
1905 if ((r = sshpkt_send(ssh)) != 0)
1906 fatal("%s: channel %i: %s", __func__, c->self,
1907 ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001908 }
1909 } else {
1910 debug("channel %d: connection failed: %s",
1911 c->self, strerror(err));
1912 /* Try next address, if any */
1913 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1914 close(c->sock);
1915 c->sock = c->rfd = c->wfd = sock;
1916 channel_find_maxfd(ssh->chanctxt);
1917 return;
1918 }
1919 /* Exhausted all addresses */
1920 error("connect_to %.100s port %d: failed.",
1921 c->connect_ctx.host, c->connect_ctx.port);
1922 channel_connect_ctx_free(&c->connect_ctx);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001923 if (isopen) {
1924 rdynamic_close(ssh, c);
1925 } else {
1926 if ((r = sshpkt_start(ssh,
1927 SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1928 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001929 (r = sshpkt_put_u32(ssh,
1930 SSH2_OPEN_CONNECT_FAILED)) != 0 ||
1931 (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1932 (r = sshpkt_put_cstring(ssh, "")) != 0) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001933 fatal("%s: channel %i: failure: %s", __func__,
1934 c->self, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001935 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001936 if ((r = sshpkt_send(ssh)) != 0)
1937 fatal("%s: channel %i: %s", __func__, c->self,
1938 ssh_err(r));
1939 chan_mark_dead(ssh, c);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001940 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001941 }
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001942}
1943
Ben Lindstrombba81212001-06-25 05:01:22 +00001944static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001945channel_handle_rfd(struct ssh *ssh, Channel *c,
1946 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001947{
Darren Tucker11327cc2005-03-14 23:22:25 +11001948 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001949 ssize_t len;
1950 int r, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001951
Damien Miller835284b2007-06-11 13:03:16 +10001952 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001953
1954 if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset)))
1955 return 1;
1956
1957 errno = 0;
1958 len = read(c->rfd, buf, sizeof(buf));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001959 if (len == -1 && (errno == EINTR ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001960 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1961 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001962#ifndef PTY_ZEROREAD
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001963 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001964#else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001965 if ((!c->isatty && len <= 0) ||
1966 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001967#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001968 debug2("channel %d: read<=0 rfd %d len %zd",
1969 c->self, c->rfd, len);
1970 if (c->type != SSH_CHANNEL_OPEN) {
1971 debug2("channel %d: not open", c->self);
1972 chan_mark_dead(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001973 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001974 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001975 chan_read_failed(ssh, c);
Damien Millerad833b32000-08-23 10:46:23 +10001976 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001977 return -1;
1978 }
1979 if (c->input_filter != NULL) {
1980 if (c->input_filter(ssh, c, buf, len) == -1) {
1981 debug2("channel %d: filter stops", c->self);
1982 chan_read_failed(ssh, c);
1983 }
1984 } else if (c->datagram) {
1985 if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1986 fatal("%s: channel %d: put datagram: %s", __func__,
1987 c->self, ssh_err(r));
1988 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
1989 fatal("%s: channel %d: put data: %s", __func__,
1990 c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001991 }
1992 return 1;
1993}
Damien Miller4f7becb2006-03-26 14:10:14 +11001994
Ben Lindstrombba81212001-06-25 05:01:22 +00001995static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001996channel_handle_wfd(struct ssh *ssh, Channel *c,
1997 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001998{
Ben Lindstrome229b252001-03-05 06:28:06 +00001999 struct termios tio;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002000 u_char *data = NULL, *buf; /* XXX const; need filter API change */
2001 size_t dlen, olen = 0;
2002 int r, len;
2003
2004 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2005 sshbuf_len(c->output) == 0)
2006 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10002007
2008 /* Send buffered output data to the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002009 olen = sshbuf_len(c->output);
2010 if (c->output_filter != NULL) {
2011 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
2012 debug2("channel %d: filter stops", c->self);
2013 if (c->type != SSH_CHANNEL_OPEN)
2014 chan_mark_dead(ssh, c);
2015 else
2016 chan_write_failed(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002017 return -1;
2018 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002019 } else if (c->datagram) {
2020 if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
2021 fatal("%s: channel %d: get datagram: %s", __func__,
2022 c->self, ssh_err(r));
djm@openbsd.org9145a732017-09-12 07:32:04 +00002023 buf = data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002024 } else {
2025 buf = data = sshbuf_mutable_ptr(c->output);
2026 dlen = sshbuf_len(c->output);
2027 }
2028
2029 if (c->datagram) {
2030 /* ignore truncated writes, datagrams might get lost */
djm@openbsd.orgc704f642017-09-24 09:50:01 +00002031 len = write(c->wfd, buf, dlen);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002032 free(data);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002033 if (len == -1 && (errno == EINTR || errno == EAGAIN ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002034 errno == EWOULDBLOCK))
2035 return 1;
2036 if (len <= 0)
2037 goto write_fail;
2038 goto out;
2039 }
2040
2041#ifdef _AIX
2042 /* XXX: Later AIX versions can't push as much data to tty */
2043 if (c->wfd_isatty)
2044 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10002045#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002046
2047 len = write(c->wfd, buf, dlen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002048 if (len == -1 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002049 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
2050 return 1;
2051 if (len <= 0) {
2052 write_fail:
2053 if (c->type != SSH_CHANNEL_OPEN) {
2054 debug2("channel %d: not open", c->self);
2055 chan_mark_dead(ssh, c);
2056 return -1;
2057 } else {
2058 chan_write_failed(ssh, c);
2059 }
2060 return -1;
2061 }
2062#ifndef BROKEN_TCGETATTR_ICANON
2063 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
2064 if (tcgetattr(c->wfd, &tio) == 0 &&
2065 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
2066 /*
2067 * Simulate echo to reduce the impact of
2068 * traffic analysis. We need to match the
2069 * size of a SSH2_MSG_CHANNEL_DATA message
2070 * (4 byte channel id + buf)
2071 */
2072 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
2073 (r = sshpkt_send(ssh)) != 0)
2074 fatal("%s: channel %d: ignore: %s",
2075 __func__, c->self, ssh_err(r));
2076 }
2077 }
2078#endif /* BROKEN_TCGETATTR_ICANON */
2079 if ((r = sshbuf_consume(c->output, len)) != 0) {
2080 fatal("%s: channel %d: consume: %s",
2081 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002082 }
Damien Miller7d457182010-08-05 23:09:48 +10002083 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002084 c->local_consumed += olen - sshbuf_len(c->output);
2085
Damien Miller33b13562000-04-04 14:38:59 +10002086 return 1;
2087}
Damien Miller4f7becb2006-03-26 14:10:14 +11002088
Ben Lindstrombba81212001-06-25 05:01:22 +00002089static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002090channel_handle_efd_write(struct ssh *ssh, Channel *c,
2091 fd_set *readset, fd_set *writeset)
2092{
2093 int r;
2094 ssize_t len;
2095
2096 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
2097 return 1;
2098
2099 len = write(c->efd, sshbuf_ptr(c->extended),
2100 sshbuf_len(c->extended));
2101 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002102 if (len == -1 && (errno == EINTR || errno == EAGAIN ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002103 errno == EWOULDBLOCK))
2104 return 1;
2105 if (len <= 0) {
2106 debug2("channel %d: closing write-efd %d", c->self, c->efd);
2107 channel_close_fd(ssh, &c->efd);
2108 } else {
2109 if ((r = sshbuf_consume(c->extended, len)) != 0) {
2110 fatal("%s: channel %d: consume: %s",
2111 __func__, c->self, ssh_err(r));
2112 }
2113 c->local_consumed += len;
2114 }
2115 return 1;
2116}
2117
2118static int
2119channel_handle_efd_read(struct ssh *ssh, Channel *c,
2120 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002121{
Darren Tucker11327cc2005-03-14 23:22:25 +11002122 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002123 ssize_t len;
Damien Miller03e92dd2019-02-08 14:50:36 +11002124 int r, force;
Damien Miller33b13562000-04-04 14:38:59 +10002125
Damien Miller03e92dd2019-02-08 14:50:36 +11002126 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
2127
2128 if (c->efd == -1 || (!force && !FD_ISSET(c->efd, readset)))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002129 return 1;
2130
2131 len = read(c->efd, buf, sizeof(buf));
2132 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002133 if (len == -1 && (errno == EINTR || ((errno == EAGAIN ||
Damien Miller03e92dd2019-02-08 14:50:36 +11002134 errno == EWOULDBLOCK) && !force)))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002135 return 1;
2136 if (len <= 0) {
2137 debug2("channel %d: closing read-efd %d",
2138 c->self, c->efd);
2139 channel_close_fd(ssh, &c->efd);
2140 } else {
2141 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
2142 debug3("channel %d: discard efd",
2143 c->self);
2144 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
2145 fatal("%s: channel %d: append: %s",
2146 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002147 }
2148 }
2149 return 1;
2150}
Damien Miller4f7becb2006-03-26 14:10:14 +11002151
Damien Miller0e220db2004-06-15 10:34:08 +10002152static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002153channel_handle_efd(struct ssh *ssh, Channel *c,
2154 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002155{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002156 if (c->efd == -1)
2157 return 1;
2158
2159 /** XXX handle drain efd, too */
2160
2161 if (c->extended_usage == CHAN_EXTENDED_WRITE)
2162 return channel_handle_efd_write(ssh, c, readset, writeset);
2163 else if (c->extended_usage == CHAN_EXTENDED_READ ||
2164 c->extended_usage == CHAN_EXTENDED_IGNORE)
2165 return channel_handle_efd_read(ssh, c, readset, writeset);
2166
2167 return 1;
2168}
2169
2170static int
2171channel_check_window(struct ssh *ssh, Channel *c)
2172{
2173 int r;
2174
Ben Lindstromb3921512001-04-11 15:57:50 +00002175 if (c->type == SSH_CHANNEL_OPEN &&
2176 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10002177 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10002178 c->local_maxpacket*3) ||
2179 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10002180 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00002181 if (!c->have_remote_id)
2182 fatal(":%s: channel %d: no remote id",
2183 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002184 if ((r = sshpkt_start(ssh,
2185 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2186 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2187 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2188 (r = sshpkt_send(ssh)) != 0) {
2189 fatal("%s: channel %i: %s", __func__,
2190 c->self, ssh_err(r));
2191 }
Damien Millerd3444942000-09-30 14:20:03 +11002192 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002193 c->self, c->local_window,
2194 c->local_consumed);
2195 c->local_window += c->local_consumed;
2196 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002197 }
2198 return 1;
2199}
2200
Ben Lindstrombba81212001-06-25 05:01:22 +00002201static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002202channel_post_open(struct ssh *ssh, Channel *c,
2203 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002204{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002205 channel_handle_rfd(ssh, c, readset, writeset);
2206 channel_handle_wfd(ssh, c, readset, writeset);
2207 channel_handle_efd(ssh, c, readset, writeset);
2208 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002209}
2210
Damien Millere1537f92010-01-26 13:26:22 +11002211static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002212read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002213{
2214 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002215 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002216 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002217 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002218
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002219 if (sshbuf_len(c->input) < need) {
2220 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002221 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002222 if (len == -1 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002223 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002224 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002225 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002226 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002227 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002228 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002229 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2230 fatal("%s: channel %d: append: %s",
2231 __func__, c->self, ssh_err(r));
2232 }
Damien Millere1537f92010-01-26 13:26:22 +11002233 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002234 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002235}
2236
2237static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002238channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2239 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002240{
2241 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002242
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002243 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2244 return;
2245 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2246 return;
2247 if (c->mux_pause)
2248 return;
2249
2250 /*
2251 * Don't not read past the precise end of packets to
2252 * avoid disrupting fd passing.
2253 */
2254 if (read_mux(ssh, c, 4) < 4) /* read header */
2255 return;
2256 /* XXX sshbuf_peek_u32 */
2257 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002258#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002259 if (need > CHANNEL_MUX_MAX_PACKET) {
2260 debug2("channel %d: packet too big %u > %u",
2261 c->self, CHANNEL_MUX_MAX_PACKET, need);
2262 chan_rcvd_oclose(ssh, c);
2263 return;
Damien Millere1537f92010-01-26 13:26:22 +11002264 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002265 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2266 return;
2267 if (c->mux_rcb(ssh, c) != 0) {
2268 debug("channel %d: mux_rcb failed", c->self);
2269 chan_mark_dead(ssh, c);
2270 return;
Damien Millere1537f92010-01-26 13:26:22 +11002271 }
2272}
2273
2274static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002275channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2276 fd_set *readset, fd_set *writeset)
2277{
2278 ssize_t len;
2279 int r;
2280
2281 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2282 sshbuf_len(c->output) == 0)
2283 return;
2284
2285 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002286 if (len == -1 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002287 return;
2288 if (len <= 0) {
2289 chan_mark_dead(ssh, c);
2290 return;
2291 }
2292 if ((r = sshbuf_consume(c->output, len)) != 0)
2293 fatal("%s: channel %d: consume: %s", __func__,
2294 c->self, ssh_err(r));
2295}
2296
2297static void
2298channel_post_mux_client(struct ssh *ssh, Channel *c,
2299 fd_set *readset, fd_set *writeset)
2300{
2301 channel_post_mux_client_read(ssh, c, readset, writeset);
2302 channel_post_mux_client_write(ssh, c, readset, writeset);
2303}
2304
2305static void
2306channel_post_mux_listener(struct ssh *ssh, Channel *c,
2307 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002308{
2309 Channel *nc;
2310 struct sockaddr_storage addr;
2311 socklen_t addrlen;
2312 int newsock;
2313 uid_t euid;
2314 gid_t egid;
2315
2316 if (!FD_ISSET(c->sock, readset))
2317 return;
2318
2319 debug("multiplexing control connection");
2320
2321 /*
2322 * Accept connection on control socket
2323 */
2324 memset(&addr, 0, sizeof(addr));
2325 addrlen = sizeof(addr);
2326 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2327 &addrlen)) == -1) {
2328 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002329 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002330 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002331 return;
2332 }
2333
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00002334 if (getpeereid(newsock, &euid, &egid) == -1) {
Damien Millere1537f92010-01-26 13:26:22 +11002335 error("%s getpeereid failed: %s", __func__,
2336 strerror(errno));
2337 close(newsock);
2338 return;
2339 }
2340 if ((euid != 0) && (getuid() != euid)) {
2341 error("multiplex uid mismatch: peer euid %u != uid %u",
2342 (u_int)euid, (u_int)getuid());
2343 close(newsock);
2344 return;
2345 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002346 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002347 newsock, newsock, -1, c->local_window_max,
2348 c->local_maxpacket, 0, "mux-control", 1);
2349 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002350 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002351 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002352 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002353 /* mux state transitions must not elicit protocol messages */
2354 nc->flags |= CHAN_LOCAL;
2355}
2356
Ben Lindstrombba81212001-06-25 05:01:22 +00002357static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002358channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002359{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002360 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002361
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002362 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2363 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2364 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002365
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002366 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2367 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2368 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2369 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2370 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2371 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2372 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2373 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2374 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2375 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002376 pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002377 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2378 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2379
2380 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2381 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2382 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2383 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2384 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2385 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2386 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2387 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2388 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002389 post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002390 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2391 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2392
2393 sc->channel_pre = pre;
2394 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002395}
2396
Damien Miller3ec27592001-10-12 11:35:04 +10002397/* gc dead channels */
2398static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002399channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002400{
2401 if (c == NULL)
2402 return;
2403 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002404 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002405 return;
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002406
Damien Millerfbdeece2003-09-02 22:52:31 +10002407 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002408 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002409 /* if we still have a callback */
2410 if (c->detach_user != NULL)
2411 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002412 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002413 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002414 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002415 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002416 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002417 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002418}
2419
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002420enum channel_table { CHAN_PRE, CHAN_POST };
2421
Ben Lindstrombba81212001-06-25 05:01:22 +00002422static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002423channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002424 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002425{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002426 struct ssh_channels *sc = ssh->chanctxt;
2427 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002428 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002429 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002430 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002431
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002432 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002433 if (unpause_secs != NULL)
2434 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002435 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2436 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002437 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002438 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002439 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002440 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002441 c->delayed = 0;
2442 else
2443 continue;
2444 }
Damien Millera6508752012-04-22 11:21:10 +10002445 if (ftab[c->type] != NULL) {
2446 /*
2447 * Run handlers that are not paused.
2448 */
2449 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002450 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002451 else if (unpause_secs != NULL) {
2452 /*
2453 * Collect the time that the earliest
2454 * channel comes off pause.
2455 */
2456 debug3("%s: chan %d: skip for %d more seconds",
2457 __func__, c->self,
2458 (int)(c->notbefore - now));
2459 if (*unpause_secs == 0 ||
2460 (c->notbefore - now) < *unpause_secs)
2461 *unpause_secs = c->notbefore - now;
2462 }
2463 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002464 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002465 }
Damien Millera6508752012-04-22 11:21:10 +10002466 if (unpause_secs != NULL && *unpause_secs != 0)
2467 debug3("%s: first channel unpauses in %d seconds",
2468 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002469}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002470
Ben Lindstrome9c99912001-06-09 00:41:05 +00002471/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002472 * Create sockets before allocating the select bitmasks.
2473 * This is necessary for things that need to happen after reading
2474 * the network-input but before channel_prepare_select().
2475 */
2476static void
2477channel_before_prepare_select(struct ssh *ssh)
2478{
2479 struct ssh_channels *sc = ssh->chanctxt;
2480 Channel *c;
2481 u_int i, oalloc;
2482
2483 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2484 c = sc->channels[i];
2485 if (c == NULL)
2486 continue;
2487 if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
2488 channel_before_prepare_select_rdynamic(ssh, c);
2489 }
2490}
2491
2492/*
Ben Lindstrome9c99912001-06-09 00:41:05 +00002493 * Allocate/update select bitmasks and add any bits relevant to channels in
2494 * select bitmasks.
2495 */
Damien Miller4af51302000-04-16 11:18:38 +10002496void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002497channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2498 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002499{
Damien Miller36812092006-03-26 14:22:47 +11002500 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002501
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002502 channel_before_prepare_select(ssh); /* might update channel_max_fd */
2503
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002504 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002505
Damien Miller36812092006-03-26 14:22:47 +11002506 nfdset = howmany(n+1, NFDBITS);
2507 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002508 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002509 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2510 sz = nfdset * sizeof(fd_mask);
2511
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002512 /* perhaps check sz < nalloc/2 and shrink? */
2513 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002514 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2515 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002516 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002517 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002518 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002519 memset(*readsetp, 0, sz);
2520 memset(*writesetp, 0, sz);
2521
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002522 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002523 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002524 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002525}
2526
Ben Lindstrome9c99912001-06-09 00:41:05 +00002527/*
2528 * After select, perform any appropriate operations for channels which have
2529 * events pending.
2530 */
Damien Miller4af51302000-04-16 11:18:38 +10002531void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002532channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002533{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002534 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002535}
2536
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002537/*
2538 * Enqueue data for channels with open or draining c->input.
2539 */
2540static void
2541channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2542{
djm@openbsd.org55486f52017-09-23 22:04:07 +00002543 size_t len, plen;
2544 const u_char *pkt;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002545 int r;
2546
2547 if ((len = sshbuf_len(c->input)) == 0) {
2548 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2549 /*
2550 * input-buffer is empty and read-socket shutdown:
2551 * tell peer, that we will not send more data:
2552 * send IEOF.
2553 * hack for extended data: delay EOF if EFD still
2554 * in use.
2555 */
2556 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2557 debug2("channel %d: "
2558 "ibuf_empty delayed efd %d/(%zu)",
2559 c->self, c->efd, sshbuf_len(c->extended));
2560 else
2561 chan_ibuf_empty(ssh, c);
2562 }
2563 return;
2564 }
2565
djm@openbsd.org9f532292017-09-12 06:35:31 +00002566 if (!c->have_remote_id)
2567 fatal(":%s: channel %d: no remote id", __func__, c->self);
2568
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002569 if (c->datagram) {
2570 /* Check datagram will fit; drop if not */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002571 if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
2572 fatal("%s: channel %d: get datagram: %s", __func__,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002573 c->self, ssh_err(r));
2574 /*
2575 * XXX this does tail-drop on the datagram queue which is
2576 * usually suboptimal compared to head-drop. Better to have
2577 * backpressure at read time? (i.e. read + discard)
2578 */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002579 if (plen > c->remote_window || plen > c->remote_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002580 debug("channel %d: datagram too big", c->self);
2581 return;
2582 }
2583 /* Enqueue it */
2584 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2585 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org55486f52017-09-23 22:04:07 +00002586 (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002587 (r = sshpkt_send(ssh)) != 0) {
2588 fatal("%s: channel %i: datagram: %s", __func__,
2589 c->self, ssh_err(r));
2590 }
djm@openbsd.org55486f52017-09-23 22:04:07 +00002591 c->remote_window -= plen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002592 return;
2593 }
2594
2595 /* Enqueue packet for buffered data. */
2596 if (len > c->remote_window)
2597 len = c->remote_window;
2598 if (len > c->remote_maxpacket)
2599 len = c->remote_maxpacket;
2600 if (len == 0)
2601 return;
2602 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2603 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2604 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2605 (r = sshpkt_send(ssh)) != 0) {
2606 fatal("%s: channel %i: data: %s", __func__,
2607 c->self, ssh_err(r));
2608 }
2609 if ((r = sshbuf_consume(c->input, len)) != 0)
2610 fatal("%s: channel %i: consume: %s", __func__,
2611 c->self, ssh_err(r));
2612 c->remote_window -= len;
2613}
2614
2615/*
2616 * Enqueue data for channels with open c->extended in read mode.
2617 */
2618static void
2619channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2620{
2621 size_t len;
2622 int r;
2623
2624 if ((len = sshbuf_len(c->extended)) == 0)
2625 return;
2626
2627 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2628 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2629 if (len > c->remote_window)
2630 len = c->remote_window;
2631 if (len > c->remote_maxpacket)
2632 len = c->remote_maxpacket;
2633 if (len == 0)
2634 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002635 if (!c->have_remote_id)
2636 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002637 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2638 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2639 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2640 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2641 (r = sshpkt_send(ssh)) != 0) {
2642 fatal("%s: channel %i: data: %s", __func__,
2643 c->self, ssh_err(r));
2644 }
2645 if ((r = sshbuf_consume(c->extended, len)) != 0)
2646 fatal("%s: channel %i: consume: %s", __func__,
2647 c->self, ssh_err(r));
2648 c->remote_window -= len;
2649 debug2("channel %d: sent ext data %zu", c->self, len);
2650}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002651
Damien Miller5e953212001-01-30 09:14:00 +11002652/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002653void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002654channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002655{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002656 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002657 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002658 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002660 for (i = 0; i < sc->channels_alloc; i++) {
2661 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002662 if (c == NULL)
2663 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002664
Ben Lindstrome9c99912001-06-09 00:41:05 +00002665 /*
2666 * We are only interested in channels that can have buffered
2667 * incoming data.
2668 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002669 if (c->type != SSH_CHANNEL_OPEN)
2670 continue;
2671 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002672 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002673 debug3("channel %d: will not send data after close",
2674 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002675 continue;
2676 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002677
Damien Miller95def091999-11-25 00:26:21 +11002678 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002679 if (c->istate == CHAN_INPUT_OPEN ||
2680 c->istate == CHAN_INPUT_WAIT_DRAIN)
2681 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002682 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002683 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002684 c->extended_usage == CHAN_EXTENDED_READ)
2685 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002686 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002687}
2688
markus@openbsd.org8d057842016-09-30 09:19:13 +00002689/* -- mux proxy support */
2690
2691/*
2692 * When multiplexing channel messages for mux clients we have to deal
2693 * with downstream messages from the mux client and upstream messages
2694 * from the ssh server:
2695 * 1) Handling downstream messages is straightforward and happens
2696 * in channel_proxy_downstream():
2697 * - We forward all messages (mostly) unmodified to the server.
2698 * - However, in order to route messages from upstream to the correct
2699 * downstream client, we have to replace the channel IDs used by the
2700 * mux clients with a unique channel ID because the mux clients might
2701 * use conflicting channel IDs.
2702 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2703 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2704 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2705 * with the newly allocated channel ID.
2706 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
djm@openbsd.org001aa552018-04-10 00:10:49 +00002707 * channels and processed by channel_proxy_upstream(). The local channel ID
markus@openbsd.org8d057842016-09-30 09:19:13 +00002708 * is then translated back to the original mux client ID.
2709 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2710 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2711 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2712 * downstream mux client are removed.
2713 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2714 * requires more work, because they are not addressed to a specific
2715 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2716 * out whether the request is addressed to the local client or a
2717 * specific downstream client based on the listen-address/port.
djm@openbsd.org001aa552018-04-10 00:10:49 +00002718 * 6) Agent and X11-Forwarding have a similar problem and are currently
markus@openbsd.org8d057842016-09-30 09:19:13 +00002719 * not supported as the matching session/channel cannot be identified
2720 * easily.
2721 */
2722
2723/*
2724 * receive packets from downstream mux clients:
2725 * channel callback fired on read from mux client, creates
2726 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2727 * on channel creation.
2728 */
2729int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002730channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002731{
2732 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002733 struct sshbuf *original = NULL, *modified = NULL;
2734 const u_char *cp;
2735 char *ctype = NULL, *listen_host = NULL;
2736 u_char type;
2737 size_t have;
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002738 int ret = -1, r;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002739 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002740
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002741 /* sshbuf_dump(downstream->input, stderr); */
2742 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002743 != 0) {
2744 error("%s: malformed message: %s", __func__, ssh_err(r));
2745 return -1;
2746 }
2747 if (have < 2) {
2748 error("%s: short message", __func__);
2749 return -1;
2750 }
2751 type = cp[1];
2752 /* skip padlen + type */
2753 cp += 2;
2754 have -= 2;
2755 if (ssh_packet_log_type(type))
2756 debug3("%s: channel %u: down->up: type %u", __func__,
2757 downstream->self, type);
2758
2759 switch (type) {
2760 case SSH2_MSG_CHANNEL_OPEN:
2761 if ((original = sshbuf_from(cp, have)) == NULL ||
2762 (modified = sshbuf_new()) == NULL) {
2763 error("%s: alloc", __func__);
2764 goto out;
2765 }
2766 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2767 (r = sshbuf_get_u32(original, &id)) != 0) {
2768 error("%s: parse error %s", __func__, ssh_err(r));
2769 goto out;
2770 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002771 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002772 -1, -1, -1, 0, 0, 0, ctype, 1);
2773 c->mux_ctx = downstream; /* point to mux client */
2774 c->mux_downstream_id = id; /* original downstream id */
2775 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2776 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2777 (r = sshbuf_putb(modified, original)) != 0) {
2778 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002779 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002780 goto out;
2781 }
2782 break;
2783 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2784 /*
2785 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2786 * need to parse 'remote_id' instead of 'ctype'.
2787 */
2788 if ((original = sshbuf_from(cp, have)) == NULL ||
2789 (modified = sshbuf_new()) == NULL) {
2790 error("%s: alloc", __func__);
2791 goto out;
2792 }
2793 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2794 (r = sshbuf_get_u32(original, &id)) != 0) {
2795 error("%s: parse error %s", __func__, ssh_err(r));
2796 goto out;
2797 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002798 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002799 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2800 c->mux_ctx = downstream; /* point to mux client */
2801 c->mux_downstream_id = id;
2802 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002803 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002804 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2805 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2806 (r = sshbuf_putb(modified, original)) != 0) {
2807 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002808 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002809 goto out;
2810 }
2811 break;
2812 case SSH2_MSG_GLOBAL_REQUEST:
2813 if ((original = sshbuf_from(cp, have)) == NULL) {
2814 error("%s: alloc", __func__);
2815 goto out;
2816 }
2817 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2818 error("%s: parse error %s", __func__, ssh_err(r));
2819 goto out;
2820 }
2821 if (strcmp(ctype, "tcpip-forward") != 0) {
2822 error("%s: unsupported request %s", __func__, ctype);
2823 goto out;
2824 }
2825 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2826 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2827 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2828 error("%s: parse error %s", __func__, ssh_err(r));
2829 goto out;
2830 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002831 if (listen_port > 65535) {
2832 error("%s: tcpip-forward for %s: bad port %u",
2833 __func__, listen_host, listen_port);
2834 goto out;
2835 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002836 /* Record that connection to this host/port is permitted. */
djm@openbsd.org115063a2018-06-06 18:22:41 +00002837 permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002838 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002839 listen_host = NULL;
2840 break;
2841 case SSH2_MSG_CHANNEL_CLOSE:
2842 if (have < 4)
2843 break;
2844 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002845 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002846 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002847 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002848 else
2849 c->flags |= CHAN_CLOSE_SENT;
2850 }
2851 break;
2852 }
2853 if (modified) {
2854 if ((r = sshpkt_start(ssh, type)) != 0 ||
2855 (r = sshpkt_putb(ssh, modified)) != 0 ||
2856 (r = sshpkt_send(ssh)) != 0) {
2857 error("%s: send %s", __func__, ssh_err(r));
2858 goto out;
2859 }
2860 } else {
2861 if ((r = sshpkt_start(ssh, type)) != 0 ||
2862 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2863 (r = sshpkt_send(ssh)) != 0) {
2864 error("%s: send %s", __func__, ssh_err(r));
2865 goto out;
2866 }
2867 }
2868 ret = 0;
2869 out:
2870 free(ctype);
2871 free(listen_host);
2872 sshbuf_free(original);
2873 sshbuf_free(modified);
2874 return ret;
2875}
2876
2877/*
2878 * receive packets from upstream server and de-multiplex packets
2879 * to correct downstream:
2880 * implemented as a helper for channel input handlers,
2881 * replaces local (proxy) channel ID with downstream channel ID.
2882 */
2883int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002884channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002885{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002886 struct sshbuf *b = NULL;
2887 Channel *downstream;
2888 const u_char *cp = NULL;
2889 size_t len;
2890 int r;
2891
2892 /*
2893 * When receiving packets from the peer we need to check whether we
2894 * need to forward the packets to the mux client. In this case we
djm@openbsd.org001aa552018-04-10 00:10:49 +00002895 * restore the original channel id and keep track of CLOSE messages,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002896 * so we can cleanup the channel.
2897 */
2898 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2899 return 0;
2900 if ((downstream = c->mux_ctx) == NULL)
2901 return 0;
2902 switch (type) {
2903 case SSH2_MSG_CHANNEL_CLOSE:
2904 case SSH2_MSG_CHANNEL_DATA:
2905 case SSH2_MSG_CHANNEL_EOF:
2906 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2907 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2908 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2909 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2910 case SSH2_MSG_CHANNEL_SUCCESS:
2911 case SSH2_MSG_CHANNEL_FAILURE:
2912 case SSH2_MSG_CHANNEL_REQUEST:
2913 break;
2914 default:
2915 debug2("%s: channel %u: unsupported type %u", __func__,
2916 c->self, type);
2917 return 0;
2918 }
2919 if ((b = sshbuf_new()) == NULL) {
2920 error("%s: alloc reply", __func__);
2921 goto out;
2922 }
2923 /* get remaining payload (after id) */
2924 cp = sshpkt_ptr(ssh, &len);
2925 if (cp == NULL) {
2926 error("%s: no packet", __func__);
2927 goto out;
2928 }
2929 /* translate id and send to muxclient */
2930 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2931 (r = sshbuf_put_u8(b, type)) != 0 ||
2932 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2933 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002934 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002935 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2936 goto out;
2937 }
2938 /* sshbuf_dump(b, stderr); */
2939 if (ssh_packet_log_type(type))
2940 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2941 type);
2942 out:
2943 /* update state */
2944 switch (type) {
2945 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2946 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002947 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002948 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002949 c->have_remote_id = 1;
2950 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002951 break;
2952 case SSH2_MSG_CHANNEL_CLOSE:
2953 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002954 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002955 else
2956 c->flags |= CHAN_CLOSE_RCVD;
2957 break;
2958 }
2959 sshbuf_free(b);
2960 return 1;
2961}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002962
2963/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002964
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002965/* Parse a channel ID from the current packet */
2966static int
2967channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2968{
2969 u_int32_t id;
2970 int r;
2971
2972 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2973 error("%s: parse id: %s", where, ssh_err(r));
2974 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2975 }
2976 if (id > INT_MAX) {
2977 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2978 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2979 }
2980 return (int)id;
2981}
2982
2983/* Lookup a channel from an ID in the current packet */
2984static Channel *
2985channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2986{
2987 int id = channel_parse_id(ssh, where, what);
2988 Channel *c;
2989
2990 if ((c = channel_lookup(ssh, id)) == NULL) {
2991 ssh_packet_disconnect(ssh,
2992 "%s packet referred to nonexistent channel %d", what, id);
2993 }
2994 return c;
2995}
2996
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002997int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002998channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002999{
Damien Miller633de332014-05-15 13:48:26 +10003000 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003001 size_t data_len, win_len;
3002 Channel *c = channel_from_packet_id(ssh, __func__, "data");
3003 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003004
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003005 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003006 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003007
Damien Miller95def091999-11-25 00:26:21 +11003008 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10003009 if (c->type != SSH_CHANNEL_OPEN &&
markus@openbsd.org609d7a62017-09-21 19:16:53 +00003010 c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
3011 c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
Damien Millerb38eff82000-04-01 11:09:21 +10003012 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003013 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11003014
Damien Miller95def091999-11-25 00:26:21 +11003015 /* Get the data. */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003016 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3017 (r = sshpkt_get_end(ssh)) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003018 fatal("%s: channel %d: get data: %s", __func__,
3019 c->self, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003020
Damien Miller7d457182010-08-05 23:09:48 +10003021 win_len = data_len;
3022 if (c->datagram)
3023 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10003024
Damien Millera04ad492004-01-21 11:02:09 +11003025 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003026 * The sending side reduces its window as it sends data, so we
3027 * must 'fake' consumption of the data in order to ensure that window
3028 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11003029 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003030 if (c->ostate != CHAN_OUTPUT_OPEN) {
3031 c->local_window -= win_len;
3032 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003033 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11003034 }
3035
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003036 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003037 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003038 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003039 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003040 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003041 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003042 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003043 c->self, win_len, c->local_window);
3044 return 0;
3045 }
3046 c->local_window -= win_len;
3047
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003048 if (c->datagram) {
3049 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
3050 fatal("%s: channel %d: append datagram: %s",
3051 __func__, c->self, ssh_err(r));
3052 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
3053 fatal("%s: channel %d: append data: %s",
3054 __func__, c->self, ssh_err(r));
3055
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003056 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003057}
Ben Lindstrome9c99912001-06-09 00:41:05 +00003058
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003059int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003060channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003061{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003062 const u_char *data;
3063 size_t data_len;
3064 u_int32_t tcode;
3065 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
3066 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003067
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;
Damien Miller33b13562000-04-04 14:38:59 +10003070 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003071 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003072 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003073 }
Ben Lindstromcf159442002-03-26 03:26:24 +00003074 if (c->flags & CHAN_EOF_RCVD) {
3075 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003076 debug("channel %d: accepting ext data after eof",
3077 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00003078 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003079 ssh_packet_disconnect(ssh, "Received extended_data "
3080 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00003081 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003082
3083 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
3084 error("%s: parse tcode: %s", __func__, ssh_err(r));
3085 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3086 }
Damien Miller33b13562000-04-04 14:38:59 +10003087 if (c->efd == -1 ||
3088 c->extended_usage != CHAN_EXTENDED_WRITE ||
3089 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10003090 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003091 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003092 }
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003093 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3094 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003095 error("%s: parse data: %s", __func__, ssh_err(r));
3096 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3097 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003098
Damien Miller33b13562000-04-04 14:38:59 +10003099 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003100 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10003101 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003102 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003103 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003104 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
3105 /* XXX sshpkt_getb? */
3106 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
3107 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10003108 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003109 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003110}
3111
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003112int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003113channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10003114{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003115 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003116 int r;
Damien Millerb38eff82000-04-01 11:09:21 +10003117
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003118 if ((r = sshpkt_get_end(ssh)) != 0) {
3119 error("%s: parse data: %s", __func__, ssh_err(r));
3120 ssh_packet_disconnect(ssh, "Invalid ieof message");
3121 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003122
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003123 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003124 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003125 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003126
3127 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11003128 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003129 debug("channel %d: FORCE input drain", c->self);
3130 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003131 if (sshbuf_len(c->input) == 0)
3132 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003133 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003134 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10003135}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003136
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003137int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003138channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003139{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003140 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003141 int r;
Damien Miller66823cd2002-01-22 23:11:38 +11003142
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003143 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003144 return 0;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003145 if ((r = sshpkt_get_end(ssh)) != 0) {
3146 error("%s: parse data: %s", __func__, ssh_err(r));
3147 ssh_packet_disconnect(ssh, "Invalid oclose message");
3148 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003149 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003150 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003151}
3152
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003153int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003154channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003155{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003156 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3157 u_int32_t remote_window, remote_maxpacket;
3158 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003159
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003160 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003161 return 0;
3162 if (c->type != SSH_CHANNEL_OPENING)
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003163 ssh_packet_disconnect(ssh, "Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003164 "non-opening channel %d.", c->self);
3165 /*
3166 * Record the remote channel number and mark that the channel
3167 * is now open.
3168 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00003169 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3170 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003171 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 ||
3172 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003173 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003174 ssh_packet_disconnect(ssh, "Invalid open confirmation message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003175 }
Damien Miller33b13562000-04-04 14:38:59 +10003176
djm@openbsd.org9f532292017-09-12 06:35:31 +00003177 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003178 c->remote_window = remote_window;
3179 c->remote_maxpacket = remote_maxpacket;
3180 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003181 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003182 debug2("%s: channel %d: callback start", __func__, c->self);
3183 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
3184 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003185 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003186 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3187 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003188 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003189}
3190
Ben Lindstrombba81212001-06-25 05:01:22 +00003191static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00003192reason2txt(int reason)
3193{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003194 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00003195 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3196 return "administratively prohibited";
3197 case SSH2_OPEN_CONNECT_FAILED:
3198 return "connect failed";
3199 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3200 return "unknown channel type";
3201 case SSH2_OPEN_RESOURCE_SHORTAGE:
3202 return "resource shortage";
3203 }
Ben Lindstrome2595442001-06-05 20:01:39 +00003204 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00003205}
3206
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003207int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003208channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003209{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003210 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3211 u_int32_t reason;
3212 char *msg = NULL;
3213 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003214
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003215 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003216 return 0;
3217 if (c->type != SSH_CHANNEL_OPENING)
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003218 ssh_packet_disconnect(ssh, "Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003219 "non-opening channel %d.", c->self);
3220 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
3221 error("%s: reason: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003222 ssh_packet_disconnect(ssh, "Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003223 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003224 /* skip language */
3225 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003226 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 ||
3227 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003228 error("%s: message/lang: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003229 ssh_packet_disconnect(ssh, "Invalid open failure message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003230 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003231 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003232 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3233 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003234 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003235 debug2("%s: channel %d: callback start", __func__, c->self);
3236 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3237 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003238 }
Damien Miller7a606212009-01-28 16:22:34 +11003239 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003240 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003241 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003242}
3243
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003244int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003245channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003246{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003247 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003248 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003249 u_int32_t adjust;
3250 u_int new_rwin;
3251 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003252
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003253 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003254 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003255 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00003256 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003257
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003258 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003259 return 0;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003260 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 ||
3261 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003262 error("%s: adjust: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003263 ssh_packet_disconnect(ssh, "Invalid window adjust message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003264 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003265 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3266 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003267 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003268 c->self, adjust, c->remote_window);
3269 }
3270 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003271 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003272}
3273
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003274int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003275channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003276{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003277 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003278 Channel *c;
3279 struct channel_confirm *cc;
Damien Millerb84886b2008-05-19 15:05:07 +10003280
3281 /* Reset keepalive timeout */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003282 ssh_packet_set_alive_timeouts(ssh, 0);
Damien Millerb84886b2008-05-19 15:05:07 +10003283
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003284 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003285
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003286 if ((c = channel_lookup(ssh, id)) == NULL) {
3287 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003288 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003289 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003290 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003291 return 0;
dtucker@openbsd.org696fb422019-07-07 01:05:00 +00003292 if (sshpkt_get_end(ssh) != 0)
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003293 ssh_packet_disconnect(ssh, "Invalid status confirm message");
Damien Millerb84886b2008-05-19 15:05:07 +10003294 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003295 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003296 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003297 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003298 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003299 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003300 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003301}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003302
Ben Lindstrome9c99912001-06-09 00:41:05 +00003303/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003304
Ben Lindstrom908afed2001-10-03 17:34:59 +00003305void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003306channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003307{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003308 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003309}
3310
Damien Millerf6dff7c2011-09-22 21:38:52 +10003311
3312/*
3313 * Determine whether or not a port forward listens to loopback, the
3314 * specified address or wildcard. On the client, a specified bind
3315 * address will always override gateway_ports. On the server, a
3316 * gateway_ports of 1 (``yes'') will override the client's specification
3317 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3318 * will bind to whatever address the client asked for.
3319 *
3320 * Special-case listen_addrs are:
3321 *
3322 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3323 * "" (empty string), "*" -> wildcard v4/v6
3324 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003325 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003326 */
3327static const char *
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003328channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003329 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003330{
3331 const char *addr = NULL;
3332 int wildcard = 0;
3333
3334 if (listen_addr == NULL) {
3335 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003336 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003337 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003338 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003339 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3340 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3341 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003342 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003343 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003344 /*
3345 * Notify client if they requested a specific listen
3346 * address and it was overridden.
3347 */
3348 if (*listen_addr != '\0' &&
3349 strcmp(listen_addr, "0.0.0.0") != 0 &&
3350 strcmp(listen_addr, "*") != 0) {
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003351 ssh_packet_send_debug(ssh,
3352 "Forwarding listen address "
Darren Tucker71152bc2013-10-10 10:27:21 +11003353 "\"%s\" overridden by server "
3354 "GatewayPorts", listen_addr);
3355 }
Damien Miller602943d2014-07-04 08:59:41 +10003356 } else if (strcmp(listen_addr, "localhost") != 0 ||
3357 strcmp(listen_addr, "127.0.0.1") == 0 ||
3358 strcmp(listen_addr, "::1") == 0) {
djm@openbsd.org846446b2020-01-25 06:40:20 +00003359 /*
3360 * Accept explicit localhost address when
3361 * GatewayPorts=yes. The "localhost" hostname is
3362 * deliberately skipped here so it will listen on all
3363 * available local address families.
3364 */
Damien Millere84d1032014-05-21 17:13:36 +10003365 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003366 }
3367 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3368 strcmp(listen_addr, "::1") == 0) {
3369 /*
3370 * If a specific IPv4/IPv6 localhost address has been
3371 * requested then accept it even if gateway_ports is in
3372 * effect. This allows the client to prefer IPv4 or IPv6.
3373 */
3374 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003375 }
3376 if (wildcardp != NULL)
3377 *wildcardp = wildcard;
3378 return addr;
3379}
3380
Damien Millerb16461c2002-01-22 23:29:22 +11003381static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003382channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3383 struct Forward *fwd, int *allocated_listen_port,
3384 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003385{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003386 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003387 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003388 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003389 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003390 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003391 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003392
Damien Millerf91ee4c2005-03-01 21:24:33 +11003393 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003394
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003395 if (is_client && fwd->connect_path != NULL) {
3396 host = fwd->connect_path;
3397 } else {
3398 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3399 fwd->listen_host : fwd->connect_host;
3400 if (host == NULL) {
3401 error("No forward host name.");
3402 return 0;
3403 }
3404 if (strlen(host) >= NI_MAXHOST) {
3405 error("Forward host name too long.");
3406 return 0;
3407 }
Kevin Steves12057502001-02-05 14:54:34 +00003408 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003409
Damien Millerf6dff7c2011-09-22 21:38:52 +10003410 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003411 addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard,
Damien Miller7acefbb2014-07-18 14:11:24 +10003412 is_client, fwd_opts);
3413 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003414 type, wildcard, (addr == NULL) ? "NULL" : addr);
3415
3416 /*
Damien Miller34132e52000-01-14 15:45:46 +11003417 * getaddrinfo returns a loopback address if the hostname is
3418 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003419 */
Damien Miller34132e52000-01-14 15:45:46 +11003420 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003421 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003422 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003423 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003424 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003425 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3426 if (addr == NULL) {
3427 /* This really shouldn't happen */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003428 ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003429 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003430 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003431 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003432 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003433 }
Damien Millera7270302005-07-06 09:36:05 +10003434 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003435 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003436 if (allocated_listen_port != NULL)
3437 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003438 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003439 switch (ai->ai_family) {
3440 case AF_INET:
3441 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3442 sin_port;
3443 break;
3444 case AF_INET6:
3445 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3446 sin6_port;
3447 break;
3448 default:
Damien Miller34132e52000-01-14 15:45:46 +11003449 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003450 }
3451 /*
3452 * If allocating a port for -R forwards, then use the
3453 * same port for all address families.
3454 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003455 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3456 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3457 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003458 *lport_p = htons(*allocated_listen_port);
3459
Damien Miller34132e52000-01-14 15:45:46 +11003460 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003461 strport, sizeof(strport),
3462 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003463 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003464 continue;
3465 }
3466 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003467 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003468 if (sock == -1) {
Damien Miller34132e52000-01-14 15:45:46 +11003469 /* this is no error since kernel may not support ipv6 */
djm@openbsd.org927f8512017-12-05 01:30:19 +00003470 verbose("socket [%s]:%s: %.100s", ntop, strport,
3471 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003472 continue;
3473 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003474
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00003475 set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003476 if (ai->ai_family == AF_INET6)
3477 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003478
Damien Miller4bf648f2009-02-14 16:28:21 +11003479 debug("Local forwarding listening on %s port %s.",
3480 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003481
Damien Miller34132e52000-01-14 15:45:46 +11003482 /* Bind the socket to the address. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003483 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003484 /*
3485 * address can be in if use ipv6 address is
3486 * already bound
3487 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003488 if (!ai->ai_next)
djm@openbsd.org927f8512017-12-05 01:30:19 +00003489 error("bind [%s]:%s: %.100s",
3490 ntop, strport, strerror(errno));
Damien Miller3c7eeb22000-03-03 22:35:33 +11003491 else
djm@openbsd.org927f8512017-12-05 01:30:19 +00003492 verbose("bind [%s]:%s: %.100s",
3493 ntop, strport, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003494
Damien Miller34132e52000-01-14 15:45:46 +11003495 close(sock);
3496 continue;
3497 }
3498 /* Start listening for connections on the socket. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00003499 if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
Damien Miller34132e52000-01-14 15:45:46 +11003500 error("listen: %.100s", strerror(errno));
djm@openbsd.org927f8512017-12-05 01:30:19 +00003501 error("listen [%s]:%s: %.100s", ntop, strport,
3502 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003503 close(sock);
3504 continue;
3505 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003506
3507 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003508 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003509 * record what we got.
3510 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003511 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3512 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003513 allocated_listen_port != NULL &&
3514 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003515 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003516 debug("Allocated listen port %d",
3517 *allocated_listen_port);
3518 }
3519
Damien Miller34132e52000-01-14 15:45:46 +11003520 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003521 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003522 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003523 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003524 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003525 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003526 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003527 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003528 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3529 c->listening_port = *allocated_listen_port;
3530 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003531 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003532 success = 1;
3533 }
3534 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003535 error("%s: cannot listen to port: %d", __func__,
3536 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003537 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003538 return success;
Damien Miller95def091999-11-25 00:26:21 +11003539}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003540
Damien Miller7acefbb2014-07-18 14:11:24 +10003541static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003542channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3543 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003544{
3545 struct sockaddr_un sunaddr;
3546 const char *path;
3547 Channel *c;
3548 int port, sock;
3549 mode_t omask;
3550
3551 switch (type) {
3552 case SSH_CHANNEL_UNIX_LISTENER:
3553 if (fwd->connect_path != NULL) {
3554 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3555 error("Local connecting path too long: %s",
3556 fwd->connect_path);
3557 return 0;
3558 }
3559 path = fwd->connect_path;
3560 port = PORT_STREAMLOCAL;
3561 } else {
3562 if (fwd->connect_host == NULL) {
3563 error("No forward host name.");
3564 return 0;
3565 }
3566 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3567 error("Forward host name too long.");
3568 return 0;
3569 }
3570 path = fwd->connect_host;
3571 port = fwd->connect_port;
3572 }
3573 break;
3574 case SSH_CHANNEL_RUNIX_LISTENER:
3575 path = fwd->listen_path;
3576 port = PORT_STREAMLOCAL;
3577 break;
3578 default:
3579 error("%s: unexpected channel type %d", __func__, type);
3580 return 0;
3581 }
3582
3583 if (fwd->listen_path == NULL) {
3584 error("No forward path name.");
3585 return 0;
3586 }
3587 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3588 error("Local listening path too long: %s", fwd->listen_path);
3589 return 0;
3590 }
3591
3592 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3593
3594 /* Start a Unix domain listener. */
3595 omask = umask(fwd_opts->streamlocal_bind_mask);
3596 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3597 fwd_opts->streamlocal_bind_unlink);
3598 umask(omask);
3599 if (sock < 0)
3600 return 0;
3601
3602 debug("Local forwarding listening on path %s.", fwd->listen_path);
3603
3604 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003605 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003606 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3607 0, "unix listener", 1);
3608 c->path = xstrdup(path);
3609 c->host_port = port;
3610 c->listening_port = PORT_STREAMLOCAL;
3611 c->listening_addr = xstrdup(fwd->listen_path);
3612 return 1;
3613}
3614
3615static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003616channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3617 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003618{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003619 u_int i;
3620 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003621
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003622 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3623 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003624 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3625 continue;
3626 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3627 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003628 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003629 found = 1;
3630 }
3631 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003632
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003633 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003634}
3635
Damien Miller7acefbb2014-07-18 14:11:24 +10003636static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003637channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003638{
3639 u_int i;
3640 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003641
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003642 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3643 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003644 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3645 continue;
3646 if (c->path == NULL)
3647 continue;
3648 if (strcmp(c->path, path) == 0) {
3649 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003650 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003651 found = 1;
3652 }
3653 }
3654
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003655 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003656}
3657
3658int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003659channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003660{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003661 if (fwd->listen_path != NULL) {
3662 return channel_cancel_rport_listener_streamlocal(ssh,
3663 fwd->listen_path);
3664 } else {
3665 return channel_cancel_rport_listener_tcpip(ssh,
3666 fwd->listen_host, fwd->listen_port);
3667 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003668}
3669
3670static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003671channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3672 const char *lhost, u_short lport, int cport,
3673 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003674{
3675 u_int i;
3676 int found = 0;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003677 const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003678
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003679 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3680 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003681 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3682 continue;
Damien Millerff773642011-09-22 21:39:48 +10003683 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003684 continue;
Damien Millerff773642011-09-22 21:39:48 +10003685 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3686 /* skip dynamic forwardings */
3687 if (c->host_port == 0)
3688 continue;
3689 } else {
3690 if (c->host_port != cport)
3691 continue;
3692 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003693 if ((c->listening_addr == NULL && addr != NULL) ||
3694 (c->listening_addr != NULL && addr == NULL))
3695 continue;
3696 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003697 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003698 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003699 found = 1;
3700 }
3701 }
3702
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003703 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003704}
3705
Damien Miller7acefbb2014-07-18 14:11:24 +10003706static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003707channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003708{
3709 u_int i;
3710 int found = 0;
3711
3712 if (path == NULL) {
3713 error("%s: no path specified.", __func__);
3714 return 0;
3715 }
3716
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003717 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3718 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003719 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3720 continue;
3721 if (c->listening_addr == NULL)
3722 continue;
3723 if (strcmp(c->listening_addr, path) == 0) {
3724 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003725 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003726 found = 1;
3727 }
3728 }
3729
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003730 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003731}
3732
3733int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003734channel_cancel_lport_listener(struct ssh *ssh,
3735 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003736{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003737 if (fwd->listen_path != NULL) {
3738 return channel_cancel_lport_listener_streamlocal(ssh,
3739 fwd->listen_path);
3740 } else {
3741 return channel_cancel_lport_listener_tcpip(ssh,
3742 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3743 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003744}
3745
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003746/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003747int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003748channel_setup_local_fwd_listener(struct ssh *ssh,
3749 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003750{
Damien Miller7acefbb2014-07-18 14:11:24 +10003751 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003752 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003753 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3754 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003755 return channel_setup_fwd_listener_tcpip(ssh,
3756 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003757 }
Damien Millerb16461c2002-01-22 23:29:22 +11003758}
3759
djm@openbsd.org115063a2018-06-06 18:22:41 +00003760/* Matches a remote forwarding permission against a requested forwarding */
3761static int
3762remote_open_match(struct permission *allowed_open, struct Forward *fwd)
3763{
3764 int ret;
3765 char *lhost;
3766
3767 /* XXX add ACLs for streamlocal */
3768 if (fwd->listen_path != NULL)
3769 return 1;
3770
3771 if (fwd->listen_host == NULL || allowed_open->listen_host == NULL)
3772 return 0;
3773
3774 if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT &&
3775 allowed_open->listen_port != fwd->listen_port)
3776 return 0;
3777
3778 /* Match hostnames case-insensitively */
3779 lhost = xstrdup(fwd->listen_host);
3780 lowercase(lhost);
3781 ret = match_pattern(lhost, allowed_open->listen_host);
3782 free(lhost);
3783
3784 return ret;
3785}
3786
3787/* Checks whether a requested remote forwarding is permitted */
3788static int
3789check_rfwd_permission(struct ssh *ssh, struct Forward *fwd)
3790{
3791 struct ssh_channels *sc = ssh->chanctxt;
3792 struct permission_set *pset = &sc->remote_perms;
3793 u_int i, permit, permit_adm = 1;
3794 struct permission *perm;
3795
3796 /* XXX apply GatewayPorts override before checking? */
3797
3798 permit = pset->all_permitted;
3799 if (!permit) {
3800 for (i = 0; i < pset->num_permitted_user; i++) {
3801 perm = &pset->permitted_user[i];
3802 if (remote_open_match(perm, fwd)) {
3803 permit = 1;
3804 break;
3805 }
3806 }
3807 }
3808
3809 if (pset->num_permitted_admin > 0) {
3810 permit_adm = 0;
3811 for (i = 0; i < pset->num_permitted_admin; i++) {
3812 perm = &pset->permitted_admin[i];
3813 if (remote_open_match(perm, fwd)) {
3814 permit_adm = 1;
3815 break;
3816 }
3817 }
3818 }
3819
3820 return permit && permit_adm;
3821}
3822
Damien Millerb16461c2002-01-22 23:29:22 +11003823/* protocol v2 remote port fwd, used by sshd */
3824int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003825channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003826 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003827{
djm@openbsd.org115063a2018-06-06 18:22:41 +00003828 if (!check_rfwd_permission(ssh, fwd)) {
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003829 ssh_packet_send_debug(ssh, "port forwarding refused");
florian@openbsd.orgcb4accb2019-05-10 18:55:17 +00003830 if (fwd->listen_path != NULL)
3831 /* XXX always allowed, see remote_open_match() */
3832 logit("Received request from %.100s port %d to "
3833 "remote forward to path \"%.100s\", "
3834 "but the request was denied.",
3835 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
3836 fwd->listen_path);
3837 else if(fwd->listen_host != NULL)
3838 logit("Received request from %.100s port %d to "
3839 "remote forward to host %.100s port %d, "
3840 "but the request was denied.",
3841 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
3842 fwd->listen_host, fwd->listen_port );
3843 else
3844 logit("Received request from %.100s port %d to remote "
3845 "forward, but the request was denied.",
3846 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
djm@openbsd.org115063a2018-06-06 18:22:41 +00003847 return 0;
3848 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003849 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003850 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003851 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3852 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003853 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003854 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3855 fwd_opts);
3856 }
Damien Millerb16461c2002-01-22 23:29:22 +11003857}
3858
Damien Miller5428f641999-11-25 11:54:57 +11003859/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003860 * Translate the requested rfwd listen host to something usable for
3861 * this server.
3862 */
3863static const char *
3864channel_rfwd_bind_host(const char *listen_host)
3865{
3866 if (listen_host == NULL) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003867 return "localhost";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003868 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003869 return "";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003870 } else
3871 return listen_host;
3872}
3873
3874/*
Damien Miller5428f641999-11-25 11:54:57 +11003875 * Initiate forwarding of connections to port "port" on remote host through
3876 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003877 * Returns handle (index) for updating the dynamic listen port with
djm@openbsd.org115063a2018-06-06 18:22:41 +00003878 * channel_update_permission().
Damien Miller5428f641999-11-25 11:54:57 +11003879 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003880int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003881channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003882{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003883 int r, success = 0, idx = -1;
3884 char *host_to_connect, *listen_host, *listen_path;
3885 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003886
Damien Miller95def091999-11-25 00:26:21 +11003887 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003888 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003889 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3890 (r = sshpkt_put_cstring(ssh,
3891 "streamlocal-forward@openssh.com")) != 0 ||
3892 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3893 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3894 (r = sshpkt_send(ssh)) != 0 ||
3895 (r = ssh_packet_write_wait(ssh)) != 0)
3896 fatal("%s: request streamlocal: %s",
3897 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003898 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003899 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3900 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3901 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3902 (r = sshpkt_put_cstring(ssh,
3903 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3904 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3905 (r = sshpkt_send(ssh)) != 0 ||
3906 (r = ssh_packet_write_wait(ssh)) != 0)
3907 fatal("%s: request tcpip-forward: %s",
3908 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003909 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003910 /* Assume that server accepts the request */
3911 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003912 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003913 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003914 host_to_connect = listen_host = listen_path = NULL;
3915 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003916 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003917 host_to_connect = xstrdup(fwd->connect_path);
3918 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003919 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003920 host_to_connect = xstrdup(fwd->connect_host);
3921 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003922 }
3923 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003924 listen_path = xstrdup(fwd->listen_path);
3925 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003926 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003927 if (fwd->listen_host != NULL)
3928 listen_host = xstrdup(fwd->listen_host);
3929 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003930 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00003931 idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003932 host_to_connect, port_to_connect,
3933 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003934 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003935 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003936}
3937
Damien Miller4b3ed642014-07-02 15:29:40 +10003938static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003939open_match(struct permission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003940 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003941{
3942 if (allowed_open->host_to_connect == NULL)
3943 return 0;
3944 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3945 allowed_open->port_to_connect != requestedport)
3946 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003947 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3948 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003949 return 0;
3950 return 1;
3951}
3952
3953/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003954 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003955 * we don't support FWD_PERMIT_ANY_PORT and
3956 * need to translate between the configured-host (listen_host)
3957 * and what we've sent to the remote server (channel_rfwd_bind_host)
3958 */
3959static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003960open_listen_match_tcpip(struct permission *allowed_open,
Damien Miller7acefbb2014-07-18 14:11:24 +10003961 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003962{
3963 const char *allowed_host;
3964
3965 if (allowed_open->host_to_connect == NULL)
3966 return 0;
3967 if (allowed_open->listen_port != requestedport)
3968 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003969 if (!translate && allowed_open->listen_host == NULL &&
3970 requestedhost == NULL)
3971 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003972 allowed_host = translate ?
3973 channel_rfwd_bind_host(allowed_open->listen_host) :
3974 allowed_open->listen_host;
djm@openbsd.org90e51d62018-06-25 22:28:33 +00003975 if (allowed_host == NULL || requestedhost == NULL ||
Damien Miller4b3ed642014-07-02 15:29:40 +10003976 strcmp(allowed_host, requestedhost) != 0)
3977 return 0;
3978 return 1;
3979}
3980
Damien Miller7acefbb2014-07-18 14:11:24 +10003981static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003982open_listen_match_streamlocal(struct permission *allowed_open,
Damien Miller7acefbb2014-07-18 14:11:24 +10003983 const char *requestedpath)
3984{
3985 if (allowed_open->host_to_connect == NULL)
3986 return 0;
3987 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3988 return 0;
3989 if (allowed_open->listen_path == NULL ||
3990 strcmp(allowed_open->listen_path, requestedpath) != 0)
3991 return 0;
3992 return 1;
3993}
3994
Damien Miller5428f641999-11-25 11:54:57 +11003995/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003996 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003997 * local side.
3998 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003999static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004000channel_request_rforward_cancel_tcpip(struct ssh *ssh,
4001 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10004002{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004003 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004004 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004005 int r;
4006 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004007 struct permission *perm;
Darren Tuckere7066df2004-05-24 10:18:05 +10004008
djm@openbsd.org115063a2018-06-06 18:22:41 +00004009 for (i = 0; i < pset->num_permitted_user; i++) {
4010 perm = &pset->permitted_user[i];
4011 if (open_listen_match_tcpip(perm, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10004012 break;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004013 perm = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10004014 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00004015 if (perm == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10004016 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10004017 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10004018 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004019 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4020 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
4021 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4022 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
4023 (r = sshpkt_put_u32(ssh, port)) != 0 ||
4024 (r = sshpkt_send(ssh)) != 0)
4025 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10004026
djm@openbsd.org115063a2018-06-06 18:22:41 +00004027 fwd_perm_clear(perm); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10004028
4029 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10004030}
4031
4032/*
Damien Miller7acefbb2014-07-18 14:11:24 +10004033 * Request cancellation of remote forwarding of Unix domain socket
4034 * path from local side.
4035 */
4036static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004037channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10004038{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004039 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004040 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004041 int r;
4042 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004043 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004044
djm@openbsd.org115063a2018-06-06 18:22:41 +00004045 for (i = 0; i < pset->num_permitted_user; i++) {
4046 perm = &pset->permitted_user[i];
4047 if (open_listen_match_streamlocal(perm, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10004048 break;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004049 perm = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10004050 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00004051 if (perm == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004052 debug("%s: requested forward not found", __func__);
4053 return -1;
4054 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004055 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4056 (r = sshpkt_put_cstring(ssh,
4057 "cancel-streamlocal-forward@openssh.com")) != 0 ||
4058 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4059 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
4060 (r = sshpkt_send(ssh)) != 0)
4061 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10004062
djm@openbsd.org115063a2018-06-06 18:22:41 +00004063 fwd_perm_clear(perm); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10004064
4065 return 0;
4066}
4067
4068/*
4069 * Request cancellation of remote forwarding of a connection from local side.
4070 */
4071int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004072channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10004073{
4074 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004075 return channel_request_rforward_cancel_streamlocal(ssh,
4076 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10004077 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004078 return channel_request_rforward_cancel_tcpip(ssh,
4079 fwd->listen_host,
4080 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10004081 }
4082}
4083
4084/*
djm@openbsd.org115063a2018-06-06 18:22:41 +00004085 * Permits opening to any host/port if permitted_user[] is empty. This is
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004086 * usually called by the server, because the user could connect to any port
4087 * anyway, and the server has no way to know but to trust the client anyway.
4088 */
4089void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004090channel_permit_all(struct ssh *ssh, int where)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004091{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004092 struct permission_set *pset = permission_set_get(ssh, where);
4093
4094 if (pset->num_permitted_user == 0)
4095 pset->all_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004096}
4097
djm@openbsd.org115063a2018-06-06 18:22:41 +00004098/*
4099 * Permit the specified host/port for forwarding.
4100 */
Ben Lindstroma3700052001-04-05 23:26:32 +00004101void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004102channel_add_permission(struct ssh *ssh, int who, int where,
4103 char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004104{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004105 int local = where == FORWARD_LOCAL;
4106 struct permission_set *pset = permission_set_get(ssh, where);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004107
djm@openbsd.org115063a2018-06-06 18:22:41 +00004108 debug("allow %s forwarding to host %s port %d",
4109 fwd_ident(who, where), host, port);
4110 /*
4111 * Remote forwards set listen_host/port, local forwards set
4112 * host/port_to_connect.
4113 */
4114 permission_set_add(ssh, who, where,
4115 local ? host : 0, local ? port : 0,
4116 local ? NULL : host, NULL, local ? 0 : port, NULL);
4117 pset->all_permitted = 0;
4118}
4119
4120/*
4121 * Administratively disable forwarding.
4122 */
4123void
4124channel_disable_admin(struct ssh *ssh, int where)
4125{
4126 channel_clear_permission(ssh, FORWARD_ADM, where);
4127 permission_set_add(ssh, FORWARD_ADM, where,
4128 NULL, 0, NULL, NULL, 0, NULL);
4129}
4130
4131/*
4132 * Clear a list of permitted opens.
4133 */
4134void
4135channel_clear_permission(struct ssh *ssh, int who, int where)
4136{
4137 struct permission **permp;
4138 u_int *npermp;
4139
4140 permission_set_get_array(ssh, who, where, &permp, &npermp);
4141 *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp));
4142 *npermp = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004143}
4144
Darren Tucker68afb8c2011-10-02 18:59:03 +11004145/*
4146 * Update the listen port for a dynamic remote forward, after
4147 * the actual 'newport' has been allocated. If 'newport' < 0 is
4148 * passed then they entry will be invalidated.
4149 */
4150void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004151channel_update_permission(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11004152{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004153 struct permission_set *pset = &ssh->chanctxt->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004154
djm@openbsd.org115063a2018-06-06 18:22:41 +00004155 if (idx < 0 || (u_int)idx >= pset->num_permitted_user) {
4156 debug("%s: index out of range: %d num_permitted_user %d",
4157 __func__, idx, pset->num_permitted_user);
Darren Tucker68afb8c2011-10-02 18:59:03 +11004158 return;
4159 }
4160 debug("%s allowed port %d for forwarding to host %s port %d",
4161 newport > 0 ? "Updating" : "Removing",
4162 newport,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004163 pset->permitted_user[idx].host_to_connect,
4164 pset->permitted_user[idx].port_to_connect);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004165 if (newport <= 0)
djm@openbsd.org115063a2018-06-06 18:22:41 +00004166 fwd_perm_clear(&pset->permitted_user[idx]);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004167 else {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004168 pset->permitted_user[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11004169 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11004170 }
4171}
4172
Darren Tucker1338b9e2011-10-02 18:57:35 +11004173/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
4174int
4175permitopen_port(const char *p)
4176{
4177 int port;
4178
4179 if (strcmp(p, "*") == 0)
4180 return FWD_PERMIT_ANY_PORT;
4181 if ((port = a2port(p)) > 0)
4182 return port;
4183 return -1;
4184}
4185
Damien Millerbd740252008-05-19 15:37:09 +10004186/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00004187static int
Damien Millerbd740252008-05-19 15:37:09 +10004188connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004189{
Damien Millerbd740252008-05-19 15:37:09 +10004190 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10004191 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004192 char ntop[NI_MAXHOST];
4193 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11004194
Damien Millerbd740252008-05-19 15:37:09 +10004195 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004196 switch (cctx->ai->ai_family) {
4197 case AF_UNIX:
4198 /* unix:pathname instead of host:port */
4199 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
4200 strlcpy(ntop, "unix", sizeof(ntop));
4201 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
4202 break;
4203 case AF_INET:
4204 case AF_INET6:
4205 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
4206 ntop, sizeof(ntop), strport, sizeof(strport),
4207 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
4208 error("connect_next: getnameinfo failed");
4209 continue;
4210 }
4211 break;
4212 default:
Damien Miller34132e52000-01-14 15:45:46 +11004213 continue;
4214 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11004215 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4216 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004217 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11004218 error("socket: %.100s", strerror(errno));
4219 else
4220 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004221 continue;
4222 }
Damien Miller232711f2004-06-15 10:35:30 +10004223 if (set_nonblock(sock) == -1)
4224 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10004225 if (connect(sock, cctx->ai->ai_addr,
4226 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
4227 debug("connect_next: host %.100s ([%.100s]:%s): "
4228 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004229 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004230 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004231 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004232 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004233 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004234 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004235 if (cctx->ai->ai_family != AF_UNIX)
4236 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004237 debug("connect_next: host %.100s ([%.100s]:%s) "
4238 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4239 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004240 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004241 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004242 return -1;
4243}
Damien Millerb38eff82000-04-01 11:09:21 +10004244
Damien Millerbd740252008-05-19 15:37:09 +10004245static void
4246channel_connect_ctx_free(struct channel_connect *cctx)
4247{
Darren Tuckera627d422013-06-02 07:31:17 +10004248 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004249 if (cctx->aitop) {
4250 if (cctx->aitop->ai_family == AF_UNIX)
4251 free(cctx->aitop);
4252 else
4253 freeaddrinfo(cctx->aitop);
4254 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004255 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004256}
4257
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004258/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004259 * Return connecting socket to remote host:port or local socket path,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004260 * passing back the failure reason if appropriate.
4261 */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004262static int
4263connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4264 char *ctype, char *rname, struct channel_connect *cctx,
4265 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10004266{
4267 struct addrinfo hints;
4268 int gaierr;
4269 int sock = -1;
4270 char strport[NI_MAXSERV];
Damien Miller7acefbb2014-07-18 14:11:24 +10004271
4272 if (port == PORT_STREAMLOCAL) {
4273 struct sockaddr_un *sunaddr;
4274 struct addrinfo *ai;
4275
4276 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4277 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004278 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004279 }
4280
4281 /*
4282 * Fake up a struct addrinfo for AF_UNIX connections.
4283 * channel_connect_ctx_free() must check ai_family
4284 * and use free() not freeaddirinfo() for AF_UNIX.
4285 */
4286 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4287 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4288 ai->ai_addr = (struct sockaddr *)(ai + 1);
4289 ai->ai_addrlen = sizeof(*sunaddr);
4290 ai->ai_family = AF_UNIX;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004291 ai->ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004292 ai->ai_protocol = PF_UNSPEC;
4293 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4294 sunaddr->sun_family = AF_UNIX;
4295 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004296 cctx->aitop = ai;
Damien Miller7acefbb2014-07-18 14:11:24 +10004297 } else {
4298 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004299 hints.ai_family = ssh->chanctxt->IPv4or6;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004300 hints.ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004301 snprintf(strport, sizeof strport, "%d", port);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004302 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004303 != 0) {
4304 if (errmsg != NULL)
4305 *errmsg = ssh_gai_strerror(gaierr);
4306 if (reason != NULL)
4307 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10004308 error("connect_to %.100s: unknown host (%s)", name,
4309 ssh_gai_strerror(gaierr));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004310 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004311 }
Damien Millerbd740252008-05-19 15:37:09 +10004312 }
4313
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004314 cctx->host = xstrdup(name);
4315 cctx->port = port;
4316 cctx->ai = cctx->aitop;
Damien Millerbd740252008-05-19 15:37:09 +10004317
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004318 if ((sock = connect_next(cctx)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004319 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004320 name, port, strerror(errno));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004321 return -1;
4322 }
4323
4324 return sock;
4325}
4326
4327/* Return CONNECTING channel to remote host:port or local socket path */
4328static Channel *
4329connect_to(struct ssh *ssh, const char *host, int port,
4330 char *ctype, char *rname)
4331{
4332 struct channel_connect cctx;
4333 Channel *c;
4334 int sock;
4335
4336 memset(&cctx, 0, sizeof(cctx));
4337 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4338 &cctx, NULL, NULL);
4339 if (sock == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004340 channel_connect_ctx_free(&cctx);
4341 return NULL;
4342 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004343 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004344 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004345 c->host_port = port;
4346 c->path = xstrdup(host);
Damien Millerbd740252008-05-19 15:37:09 +10004347 c->connect_ctx = cctx;
Damien Millerbd740252008-05-19 15:37:09 +10004348
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004349 return c;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004350}
4351
markus@openbsd.org8d057842016-09-30 09:19:13 +00004352/*
4353 * returns either the newly connected channel or the downstream channel
4354 * that needs to deal with this connection.
4355 */
Damien Millerbd740252008-05-19 15:37:09 +10004356Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004357channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004358 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004359{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004360 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004361 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004362 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004363 struct permission *perm;
Damien Millerbd740252008-05-19 15:37:09 +10004364
djm@openbsd.org115063a2018-06-06 18:22:41 +00004365 for (i = 0; i < pset->num_permitted_user; i++) {
4366 perm = &pset->permitted_user[i];
4367 if (open_listen_match_tcpip(perm,
4368 listen_host, listen_port, 1)) {
4369 if (perm->downstream)
4370 return perm->downstream;
4371 if (perm->port_to_connect == 0)
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004372 return rdynamic_connect_prepare(ssh,
4373 ctype, rname);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004374 return connect_to(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004375 perm->host_to_connect, perm->port_to_connect,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004376 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004377 }
4378 }
4379 error("WARNING: Server requests forwarding for unknown listen_port %d",
4380 listen_port);
4381 return NULL;
4382}
4383
Damien Miller7acefbb2014-07-18 14:11:24 +10004384Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004385channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4386 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004387{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004388 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004389 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004390 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004391 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004392
djm@openbsd.org115063a2018-06-06 18:22:41 +00004393 for (i = 0; i < pset->num_permitted_user; i++) {
4394 perm = &pset->permitted_user[i];
4395 if (open_listen_match_streamlocal(perm, path)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004396 return connect_to(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004397 perm->host_to_connect, perm->port_to_connect,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004398 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004399 }
4400 }
4401 error("WARNING: Server requests forwarding for unknown path %.100s",
4402 path);
4403 return NULL;
4404}
4405
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004406/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004407Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004408channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4409 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004410{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004411 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004412 struct permission_set *pset = &sc->local_perms;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004413 struct channel_connect cctx;
4414 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004415 u_int i, permit, permit_adm = 1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004416 int sock;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004417 struct permission *perm;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004418
djm@openbsd.org115063a2018-06-06 18:22:41 +00004419 permit = pset->all_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004420 if (!permit) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004421 for (i = 0; i < pset->num_permitted_user; i++) {
4422 perm = &pset->permitted_user[i];
4423 if (open_match(perm, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004424 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004425 break;
4426 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004427 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004428 }
Damien Miller9b439df2006-07-24 14:04:00 +10004429
djm@openbsd.org115063a2018-06-06 18:22:41 +00004430 if (pset->num_permitted_admin > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004431 permit_adm = 0;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004432 for (i = 0; i < pset->num_permitted_admin; i++) {
4433 perm = &pset->permitted_admin[i];
4434 if (open_match(perm, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004435 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004436 break;
4437 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004438 }
Damien Miller9b439df2006-07-24 14:04:00 +10004439 }
4440
4441 if (!permit || !permit_adm) {
florian@openbsd.orgcb4accb2019-05-10 18:55:17 +00004442 logit("Received request from %.100s port %d to connect to "
4443 "host %.100s port %d, but the request was denied.",
4444 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004445 if (reason != NULL)
4446 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004447 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004448 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004449
4450 memset(&cctx, 0, sizeof(cctx));
4451 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4452 &cctx, reason, errmsg);
4453 if (sock == -1) {
4454 channel_connect_ctx_free(&cctx);
4455 return NULL;
4456 }
4457
4458 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4459 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4460 c->host_port = port;
4461 c->path = xstrdup(host);
4462 c->connect_ctx = cctx;
4463
4464 return c;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004465}
4466
Damien Miller7acefbb2014-07-18 14:11:24 +10004467/* Check if connecting to that path is permitted and connect. */
4468Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004469channel_connect_to_path(struct ssh *ssh, const char *path,
4470 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004471{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004472 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004473 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004474 u_int i, permit, permit_adm = 1;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004475 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004476
djm@openbsd.org115063a2018-06-06 18:22:41 +00004477 permit = pset->all_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004478 if (!permit) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004479 for (i = 0; i < pset->num_permitted_user; i++) {
4480 perm = &pset->permitted_user[i];
4481 if (open_match(perm, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004482 permit = 1;
4483 break;
4484 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004485 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004486 }
4487
djm@openbsd.org115063a2018-06-06 18:22:41 +00004488 if (pset->num_permitted_admin > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004489 permit_adm = 0;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004490 for (i = 0; i < pset->num_permitted_admin; i++) {
4491 perm = &pset->permitted_admin[i];
4492 if (open_match(perm, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004493 permit_adm = 1;
4494 break;
4495 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004496 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004497 }
4498
4499 if (!permit || !permit_adm) {
4500 logit("Received request to connect to path %.100s, "
4501 "but the request was denied.", path);
4502 return NULL;
4503 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004504 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004505}
4506
Damien Miller0e220db2004-06-15 10:34:08 +10004507void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004508channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004509{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004510 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004511 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004512 int r;
4513 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004514
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004515 for (i = 0; i < sc->channels_alloc; i++) {
4516 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4517 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004518 continue;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004519 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) == -1)
Damien Miller0e220db2004-06-15 10:34:08 +10004520 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004521 channel_request_start(ssh, i, "window-change", 0);
4522 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4523 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4524 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4525 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4526 (r = sshpkt_send(ssh)) != 0)
4527 fatal("%s: channel %u: send window-change: %s",
4528 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004529 }
4530}
4531
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004532/* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4533static Channel *
4534rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4535{
4536 Channel *c;
4537 int r;
4538
4539 c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4540 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4541 c->host_port = 0;
4542 c->path = NULL;
4543
4544 /*
4545 * We need to open the channel before we have a FD,
4546 * so that we can get SOCKS header from peer.
4547 */
4548 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4549 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4550 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4551 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
4552 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
4553 fatal("%s: channel %i: confirm: %s", __func__,
4554 c->self, ssh_err(r));
4555 }
4556 return c;
4557}
4558
4559/* Return CONNECTING socket to remote host:port or local socket path */
4560static int
4561rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4562{
4563 struct channel_connect cctx;
4564 int sock;
4565
4566 memset(&cctx, 0, sizeof(cctx));
4567 sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4568 NULL, &cctx, NULL, NULL);
4569 if (sock == -1)
4570 channel_connect_ctx_free(&cctx);
4571 else {
4572 /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4573 c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4574 c->connect_ctx = cctx;
4575 channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4576 }
4577 return sock;
4578}
4579
Ben Lindstrome9c99912001-06-09 00:41:05 +00004580/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004581
Damien Miller5428f641999-11-25 11:54:57 +11004582/*
4583 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004584 * Returns 0 and a suitable display number for the DISPLAY variable
4585 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004586 */
Kevin Steves366298c2001-12-19 17:58:01 +00004587int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004588x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4589 int x11_use_localhost, int single_connection,
4590 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004591{
Damien Millere7378562001-12-21 14:58:35 +11004592 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004593 int display_number, sock;
4594 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004595 struct addrinfo hints, *ai, *aitop;
4596 char strport[NI_MAXSERV];
4597 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004598
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004599 if (chanids == NULL)
4600 return -1;
4601
Damien Millera34a28b1999-12-14 10:47:15 +11004602 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004603 display_number < MAX_DISPLAYS;
4604 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004605 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004606 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004607 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004608 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004609 hints.ai_socktype = SOCK_STREAM;
4610 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004611 if ((gaierr = getaddrinfo(NULL, strport,
4612 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004613 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004614 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004615 }
Damien Miller34132e52000-01-14 15:45:46 +11004616 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004617 if (ai->ai_family != AF_INET &&
4618 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004619 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004620 sock = socket(ai->ai_family, ai->ai_socktype,
4621 ai->ai_protocol);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004622 if (sock == -1) {
Damien Miller6480c632010-03-26 11:09:44 +11004623 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4624#ifdef EPFNOSUPPORT
4625 && (errno != EPFNOSUPPORT)
4626#endif
4627 ) {
Damien Millere2192732000-01-17 13:22:55 +11004628 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004629 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004630 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004631 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004632 debug("x11_create_display_inet: Socket family %d not supported",
4633 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004634 continue;
4635 }
Damien Miller34132e52000-01-14 15:45:46 +11004636 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004637 if (ai->ai_family == AF_INET6)
4638 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004639 if (x11_use_localhost)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00004640 set_reuseaddr(sock);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004641 if (bind(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004642 debug2("%s: bind port %d: %.100s", __func__,
4643 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004644 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004645 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004646 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004647 num_socks = 0;
4648 break;
4649 }
4650 socks[num_socks++] = sock;
4651 if (num_socks == NUM_SOCKS)
4652 break;
Damien Miller95def091999-11-25 00:26:21 +11004653 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004654 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004655 if (num_socks > 0)
4656 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004657 }
Damien Miller95def091999-11-25 00:26:21 +11004658 if (display_number >= MAX_DISPLAYS) {
4659 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004660 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004661 }
Damien Miller95def091999-11-25 00:26:21 +11004662 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004663 for (n = 0; n < num_socks; n++) {
4664 sock = socks[n];
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004665 if (listen(sock, SSH_LISTEN_BACKLOG) == -1) {
Damien Miller34132e52000-01-14 15:45:46 +11004666 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004667 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004668 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004669 }
Damien Miller95def091999-11-25 00:26:21 +11004670 }
Damien Miller34132e52000-01-14 15:45:46 +11004671
Damien Miller34132e52000-01-14 15:45:46 +11004672 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004673 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004674 for (n = 0; n < num_socks; n++) {
4675 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004676 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004677 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4678 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004679 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004680 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004681 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004682 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004683 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004684
Kevin Steves366298c2001-12-19 17:58:01 +00004685 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004686 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004687 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004688}
4689
Ben Lindstrombba81212001-06-25 05:01:22 +00004690static int
Damien Miller819dbb62009-01-21 16:46:26 +11004691connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004692{
Damien Miller95def091999-11-25 00:26:21 +11004693 int sock;
4694 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004695
Damien Miller3afe3752001-12-21 12:39:51 +11004696 sock = socket(AF_UNIX, SOCK_STREAM, 0);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004697 if (sock == -1)
Damien Miller3afe3752001-12-21 12:39:51 +11004698 error("socket: %.100s", strerror(errno));
4699 memset(&addr, 0, sizeof(addr));
4700 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004701 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004702 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004703 return sock;
4704 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004705 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4706 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004707}
4708
Damien Miller819dbb62009-01-21 16:46:26 +11004709static int
4710connect_local_xsocket(u_int dnr)
4711{
4712 char buf[1024];
4713 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4714 return connect_local_xsocket_path(buf);
4715}
4716
Darren Tuckerda39b092017-03-10 13:22:32 +11004717#ifdef __APPLE__
4718static int
4719is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4720{
4721 struct stat sbuf;
4722
4723 if (strlcpy(path, display, pathlen) >= pathlen) {
4724 error("%s: display path too long", __func__);
4725 return 0;
4726 }
4727 if (display[0] != '/')
4728 return 0;
4729 if (stat(path, &sbuf) == 0) {
4730 return 1;
4731 } else {
4732 char *dot = strrchr(path, '.');
4733 if (dot != NULL) {
4734 *dot = '\0';
4735 if (stat(path, &sbuf) == 0) {
4736 return 1;
4737 }
4738 }
4739 }
4740 return 0;
4741}
4742#endif
4743
Damien Millerbd483e72000-04-30 10:00:53 +10004744int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004745x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004746{
Damien Miller57c4e872006-03-31 23:11:07 +11004747 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004748 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004749 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004750 struct addrinfo hints, *ai, *aitop;
4751 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004752 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004753
Damien Miller95def091999-11-25 00:26:21 +11004754 /* Try to open a socket for the local X server. */
4755 display = getenv("DISPLAY");
4756 if (!display) {
4757 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004758 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004759 }
Damien Miller5428f641999-11-25 11:54:57 +11004760 /*
4761 * Now we decode the value of the DISPLAY variable and make a
4762 * connection to the real X server.
4763 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004764
Damien Miller819dbb62009-01-21 16:46:26 +11004765#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004766 /* Check if display is a path to a socket (as set by launchd). */
4767 {
4768 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004769
Darren Tuckerda39b092017-03-10 13:22:32 +11004770 if (is_path_to_xsocket(display, path, sizeof(path))) {
4771 debug("x11_connect_display: $DISPLAY is launchd");
4772
4773 /* Create a socket. */
4774 sock = connect_local_xsocket_path(path);
4775 if (sock < 0)
4776 return -1;
4777
4778 /* OK, we now have a connection to the display. */
4779 return sock;
4780 }
Damien Miller819dbb62009-01-21 16:46:26 +11004781 }
4782#endif
Damien Miller5428f641999-11-25 11:54:57 +11004783 /*
4784 * Check if it is a unix domain socket. Unix domain displays are in
4785 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4786 */
Damien Miller95def091999-11-25 00:26:21 +11004787 if (strncmp(display, "unix:", 5) == 0 ||
4788 display[0] == ':') {
4789 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004790 if (sscanf(strrchr(display, ':') + 1, "%u",
4791 &display_number) != 1) {
4792 error("Could not parse display number from DISPLAY: "
4793 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004794 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004795 }
4796 /* Create a socket. */
4797 sock = connect_local_xsocket(display_number);
4798 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004799 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004800
4801 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004802 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004803 }
Damien Miller5428f641999-11-25 11:54:57 +11004804 /*
4805 * Connect to an inet socket. The DISPLAY value is supposedly
4806 * hostname:d[.s], where hostname may also be numeric IP address.
4807 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004808 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004809 cp = strchr(buf, ':');
4810 if (!cp) {
4811 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004812 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004813 }
Damien Miller95def091999-11-25 00:26:21 +11004814 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004815 /*
4816 * buf now contains the host name. But first we parse the
4817 * display number.
4818 */
Damien Miller57c4e872006-03-31 23:11:07 +11004819 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004820 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004821 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004822 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004823 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004824
Damien Miller34132e52000-01-14 15:45:46 +11004825 /* Look up the host address */
4826 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004827 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004828 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004829 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004830 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004831 error("%.100s: unknown host. (%s)", buf,
4832 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004833 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004834 }
Damien Miller34132e52000-01-14 15:45:46 +11004835 for (ai = aitop; ai; ai = ai->ai_next) {
4836 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004837 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004838 if (sock == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004839 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004840 continue;
4841 }
4842 /* Connect it to the display. */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00004843 if (connect(sock, ai->ai_addr, ai->ai_addrlen) == -1) {
Damien Miller57c4e872006-03-31 23:11:07 +11004844 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004845 6000 + display_number, strerror(errno));
4846 close(sock);
4847 continue;
4848 }
4849 /* Success */
4850 break;
Damien Miller34132e52000-01-14 15:45:46 +11004851 }
Damien Miller34132e52000-01-14 15:45:46 +11004852 freeaddrinfo(aitop);
4853 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004854 error("connect %.100s port %u: %.100s", buf,
4855 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004856 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004857 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004858 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004859 return sock;
4860}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004861
Damien Millerbd483e72000-04-30 10:00:53 +10004862/*
Damien Miller5428f641999-11-25 11:54:57 +11004863 * Requests forwarding of X11 connections, generates fake authentication
4864 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004865 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004866 */
Damien Miller4af51302000-04-16 11:18:38 +10004867void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004868x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4869 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004870{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004871 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004872 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004873 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004874 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004875 char *new_data;
4876 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004877
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004878 if (sc->x11_saved_display == NULL)
4879 sc->x11_saved_display = xstrdup(disp);
4880 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004881 error("x11_request_forwarding_with_spoofing: different "
4882 "$DISPLAY already forwarded");
4883 return;
4884 }
4885
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004886 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004887 if (cp)
4888 cp = strchr(cp, '.');
4889 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004890 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004891 else
4892 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004893
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004894 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004895 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004896 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004897
4898 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004899 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004900 for (i = 0; i < data_len; i++) {
4901 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4902 fatal("x11_request_forwarding: bad "
4903 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004904 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004905 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004906 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004907
4908 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004909 sc->x11_fake_data = xmalloc(data_len);
4910 arc4random_buf(sc->x11_fake_data, data_len);
4911 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004912 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004913
Damien Miller95def091999-11-25 00:26:21 +11004914 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004915 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004916
Damien Miller95def091999-11-25 00:26:21 +11004917 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004918 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4919 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4920 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4921 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4922 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4923 (r = sshpkt_send(ssh)) != 0 ||
4924 (r = ssh_packet_write_wait(ssh)) != 0)
4925 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004926 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004927}