blob: 657381b8037d67aab96aab83521d11567fd898c7 [file] [log] [blame]
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00001/* $OpenBSD: channels.c,v 1.389 2019/01/19 21:37:13 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000058#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <netdb.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000060#include <stdarg.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#ifdef HAVE_STDINT_H
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000062 #include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110063#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100064#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100065#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110067#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Millerb84886b2008-05-19 15:05:07 +100070#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000074#include "ssherr.h"
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000075#include "sshbuf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "canohost.h"
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
496/* Reutrns pointers to the specified forwarding list and its element count */
497static 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;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000642
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000643 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
644 if (ssh->chanctxt->channels[i] != NULL)
645 channel_free(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646}
647
648/*
649 * Closes the sockets/fds of all channels. This is used to close extra file
650 * descriptors after a fork.
651 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000652void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000653channel_close_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000654{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000655 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000656
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000657 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
658 if (ssh->chanctxt->channels[i] != NULL)
659 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000660}
661
662/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000663 * Stop listening to channels.
664 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000665void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000666channel_stop_listening(struct ssh *ssh)
Ben Lindstrom809744e2001-07-04 05:26:06 +0000667{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000668 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000669 Channel *c;
670
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000671 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
672 c = ssh->chanctxt->channels[i];
Ben Lindstrom809744e2001-07-04 05:26:06 +0000673 if (c != NULL) {
674 switch (c->type) {
675 case SSH_CHANNEL_AUTH_SOCKET:
676 case SSH_CHANNEL_PORT_LISTENER:
677 case SSH_CHANNEL_RPORT_LISTENER:
678 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000679 case SSH_CHANNEL_UNIX_LISTENER:
680 case SSH_CHANNEL_RUNIX_LISTENER:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000681 channel_close_fd(ssh, &c->sock);
682 channel_free(ssh, c);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000683 break;
684 }
685 }
686 }
687}
688
689/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 * Returns true if no channel has too much buffered data, and false if one or
691 * more channel is overfull.
692 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000693int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000694channel_not_very_much_buffered_data(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000695{
696 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000697 u_int maxsize = ssh_packet_get_maxsize(ssh);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698 Channel *c;
699
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000700 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
701 c = ssh->chanctxt->channels[i];
702 if (c == NULL || c->type != SSH_CHANNEL_OPEN)
703 continue;
704 if (sshbuf_len(c->output) > maxsize) {
705 debug2("channel %d: big output buffer %zu > %u",
706 c->self, sshbuf_len(c->output), maxsize);
707 return 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000708 }
709 }
710 return 1;
711}
712
713/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000714int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000715channel_still_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000716{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000717 u_int i;
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];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000722 if (c == NULL)
723 continue;
724 switch (c->type) {
725 case SSH_CHANNEL_X11_LISTENER:
726 case SSH_CHANNEL_PORT_LISTENER:
727 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100728 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000729 case SSH_CHANNEL_CLOSED:
730 case SSH_CHANNEL_AUTH_SOCKET:
731 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000732 case SSH_CHANNEL_RDYNAMIC_OPEN:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000733 case SSH_CHANNEL_CONNECTING:
734 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000735 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000736 case SSH_CHANNEL_UNIX_LISTENER:
737 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000738 continue;
739 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000740 continue;
741 case SSH_CHANNEL_OPENING:
742 case SSH_CHANNEL_OPEN:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000743 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100745 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000746 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000747 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000748 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000749 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000750 /* NOTREACHED */
751 }
752 }
753 return 0;
754}
755
756/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000758channel_find_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000759{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000760 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000761 Channel *c;
762
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000763 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
764 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000765 if (c == NULL || !c->have_remote_id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000766 continue;
767 switch (c->type) {
768 case SSH_CHANNEL_CLOSED:
769 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000770 case SSH_CHANNEL_RDYNAMIC_OPEN:
771 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000772 case SSH_CHANNEL_X11_LISTENER:
773 case SSH_CHANNEL_PORT_LISTENER:
774 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100775 case SSH_CHANNEL_MUX_LISTENER:
776 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000777 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000778 case SSH_CHANNEL_OPENING:
779 case SSH_CHANNEL_CONNECTING:
780 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000781 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000782 case SSH_CHANNEL_UNIX_LISTENER:
783 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000784 continue;
785 case SSH_CHANNEL_LARVAL:
786 case SSH_CHANNEL_AUTH_SOCKET:
787 case SSH_CHANNEL_OPEN:
788 case SSH_CHANNEL_X11_OPEN:
789 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000790 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000791 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 /* NOTREACHED */
793 }
794 }
795 return -1;
796}
797
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000798/* Returns the state of the channel's extended usage flag */
799const char *
800channel_format_extended_usage(const Channel *c)
801{
802 if (c->efd == -1)
803 return "closed";
804
805 switch (c->extended_usage) {
806 case CHAN_EXTENDED_WRITE:
807 return "write";
808 case CHAN_EXTENDED_READ:
809 return "read";
810 case CHAN_EXTENDED_IGNORE:
811 return "ignore";
812 default:
813 return "UNKNOWN";
814 }
815}
816
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000817static char *
818channel_format_status(const Channel *c)
819{
820 char *ret = NULL;
821
822 xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu "
823 "fd %d/%d/%d sock %d cc %d",
824 c->type,
825 c->have_remote_id ? "r" : "nr", c->remote_id,
826 c->istate, sshbuf_len(c->input),
827 c->ostate, sshbuf_len(c->output),
828 channel_format_extended_usage(c), sshbuf_len(c->extended),
829 c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan);
830 return ret;
831}
832
Ben Lindstrome9c99912001-06-09 00:41:05 +0000833/*
834 * Returns a message describing the currently open forwarded connections,
835 * suitable for sending to the client. The message contains crlf pairs for
836 * newlines.
837 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000839channel_open_message(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000840{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000841 struct sshbuf *buf;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000842 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000843 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000844 int r;
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000845 char *cp, *ret;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000846
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000847 if ((buf = sshbuf_new()) == NULL)
848 fatal("%s: sshbuf_new", __func__);
849 if ((r = sshbuf_putf(buf,
850 "The following connections are open:\r\n")) != 0)
851 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
852 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
853 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000854 if (c == NULL)
855 continue;
856 switch (c->type) {
857 case SSH_CHANNEL_X11_LISTENER:
858 case SSH_CHANNEL_PORT_LISTENER:
859 case SSH_CHANNEL_RPORT_LISTENER:
860 case SSH_CHANNEL_CLOSED:
861 case SSH_CHANNEL_AUTH_SOCKET:
862 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000863 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100864 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000865 case SSH_CHANNEL_UNIX_LISTENER:
866 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000867 continue;
868 case SSH_CHANNEL_LARVAL:
869 case SSH_CHANNEL_OPENING:
870 case SSH_CHANNEL_CONNECTING:
871 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000872 case SSH_CHANNEL_RDYNAMIC_OPEN:
873 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000874 case SSH_CHANNEL_OPEN:
875 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000876 case SSH_CHANNEL_MUX_PROXY:
877 case SSH_CHANNEL_MUX_CLIENT:
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000878 cp = channel_format_status(c);
879 if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n",
880 c->self, c->remote_name, cp)) != 0) {
881 free(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000882 fatal("%s: sshbuf_putf: %s",
883 __func__, ssh_err(r));
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000884 }
885 free(cp);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000886 continue;
887 default:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000888 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000889 /* NOTREACHED */
890 }
891 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000892 if ((ret = sshbuf_dup_string(buf)) == NULL)
893 fatal("%s: sshbuf_dup_string", __func__);
894 sshbuf_free(buf);
895 return ret;
896}
897
898static void
899open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
900{
901 int r;
902
903 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
904 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
905 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
906 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
907 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
908 fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r));
909 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000910}
911
912void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000913channel_send_open(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000914{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000915 Channel *c = channel_lookup(ssh, id);
916 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000917
Ben Lindstrome9c99912001-06-09 00:41:05 +0000918 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000919 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000920 return;
921 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000922 debug2("channel %d: send open", id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000923 open_preamble(ssh, __func__, c, c->ctype);
924 if ((r = sshpkt_send(ssh)) != 0)
925 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000926}
927
928void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000929channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000930{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000931 Channel *c = channel_lookup(ssh, id);
932 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000933
Ben Lindstrome9c99912001-06-09 00:41:05 +0000934 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000935 logit("%s: %d: unknown channel id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000936 return;
937 }
djm@openbsd.org9f532292017-09-12 06:35:31 +0000938 if (!c->have_remote_id)
939 fatal(":%s: channel %d: no remote id", __func__, c->self);
940
Damien Miller0e220db2004-06-15 10:34:08 +1000941 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000942 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
943 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
944 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
945 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
946 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
947 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000948}
Damien Miller4f7becb2006-03-26 14:10:14 +1100949
Ben Lindstrome9c99912001-06-09 00:41:05 +0000950void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000951channel_register_status_confirm(struct ssh *ssh, int id,
952 channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
Damien Millerb84886b2008-05-19 15:05:07 +1000953{
954 struct channel_confirm *cc;
955 Channel *c;
956
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000957 if ((c = channel_lookup(ssh, id)) == NULL)
958 fatal("%s: %d: bad id", __func__, id);
Damien Millerb84886b2008-05-19 15:05:07 +1000959
Damien Miller6c81fee2013-11-08 12:19:55 +1100960 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000961 cc->cb = cb;
962 cc->abandon_cb = abandon_cb;
963 cc->ctx = ctx;
964 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
965}
966
967void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000968channel_register_open_confirm(struct ssh *ssh, int id,
969 channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000970{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000971 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000972
Ben Lindstrome9c99912001-06-09 00:41:05 +0000973 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000974 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000975 return;
976 }
Damien Millerb84886b2008-05-19 15:05:07 +1000977 c->open_confirm = fn;
978 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000979}
Damien Miller4f7becb2006-03-26 14:10:14 +1100980
Ben Lindstrome9c99912001-06-09 00:41:05 +0000981void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000982channel_register_cleanup(struct ssh *ssh, int id,
983 channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000984{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000985 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000986
Ben Lindstrome9c99912001-06-09 00:41:05 +0000987 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000988 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000989 return;
990 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000991 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100992 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000993}
Damien Miller4f7becb2006-03-26 14:10:14 +1100994
Ben Lindstrome9c99912001-06-09 00:41:05 +0000995void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000996channel_cancel_cleanup(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000997{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000998 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000999
Ben Lindstrome9c99912001-06-09 00:41:05 +00001000 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001001 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001002 return;
1003 }
Ben Lindstrom809744e2001-07-04 05:26:06 +00001004 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +11001005 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001006}
Damien Miller4f7becb2006-03-26 14:10:14 +11001007
Ben Lindstrome9c99912001-06-09 00:41:05 +00001008void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001009channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +10001010 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001011{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001012 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001013
Ben Lindstrome9c99912001-06-09 00:41:05 +00001014 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001015 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001016 return;
1017 }
Damien Miller077b2382005-12-31 16:22:32 +11001018 c->input_filter = ifn;
1019 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001020 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +10001021 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001022}
1023
1024void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001025channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +10001026 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001027{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001028 Channel *c = channel_lookup(ssh, id);
1029 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001030
Ben Lindstrome9c99912001-06-09 00:41:05 +00001031 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
1032 fatal("channel_activate for non-larval channel %d.", id);
djm@openbsd.org9f532292017-09-12 06:35:31 +00001033 if (!c->have_remote_id)
1034 fatal(":%s: channel %d: no remote id", __func__, c->self);
1035
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001036 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001037 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +11001038 c->local_window = c->local_window_max = window_max;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001039
1040 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1041 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1042 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1043 (r = sshpkt_send(ssh)) != 0)
1044 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001045}
1046
Ben Lindstrombba81212001-06-25 05:01:22 +00001047static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001048channel_pre_listener(struct ssh *ssh, Channel *c,
1049 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001050{
1051 FD_SET(c->sock, readset);
1052}
1053
Ben Lindstrombba81212001-06-25 05:01:22 +00001054static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001055channel_pre_connecting(struct ssh *ssh, Channel *c,
1056 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001057{
1058 debug3("channel %d: waiting for connection", c->self);
1059 FD_SET(c->sock, writeset);
1060}
1061
Ben Lindstrombba81212001-06-25 05:01:22 +00001062static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001063channel_pre_open(struct ssh *ssh, Channel *c,
1064 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001065{
Damien Miller33b13562000-04-04 14:38:59 +10001066 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001067 c->remote_window > 0 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001068 sshbuf_len(c->input) < c->remote_window &&
1069 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Miller33b13562000-04-04 14:38:59 +10001070 FD_SET(c->rfd, readset);
1071 if (c->ostate == CHAN_OUTPUT_OPEN ||
1072 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001073 if (sshbuf_len(c->output) > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001074 FD_SET(c->wfd, writeset);
1075 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +00001076 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001077 debug2("channel %d: "
1078 "obuf_empty delayed efd %d/(%zu)", c->self,
1079 c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001080 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001081 chan_obuf_empty(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10001082 }
1083 }
1084 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001085 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
1086 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +10001087 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001088 sshbuf_len(c->extended) > 0)
Damien Miller33b13562000-04-04 14:38:59 +10001089 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +10001090 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1091 (c->extended_usage == CHAN_EXTENDED_READ ||
1092 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001093 sshbuf_len(c->extended) < c->remote_window)
Damien Miller33b13562000-04-04 14:38:59 +10001094 FD_SET(c->efd, readset);
1095 }
Damien Miller0e220db2004-06-15 10:34:08 +10001096 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +10001097}
1098
Damien Millerb38eff82000-04-01 11:09:21 +10001099/*
1100 * This is a special state for X11 authentication spoofing. An opened X11
1101 * connection (when authentication spoofing is being done) remains in this
1102 * state until the first packet has been completely read. The authentication
1103 * data in that packet is then substituted by the real data if it matches the
1104 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +10001105 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +00001106 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +10001107 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001108static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001109x11_open_helper(struct ssh *ssh, struct sshbuf *b)
Damien Millerb38eff82000-04-01 11:09:21 +10001110{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001111 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001112 u_char *ucp;
1113 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001114
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001115 /* Is this being called after the refusal deadline? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001116 if (sc->x11_refuse_time != 0 &&
1117 (u_int)monotime() >= sc->x11_refuse_time) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001118 verbose("Rejected X11 connection after ForwardX11Timeout "
1119 "expired");
1120 return -1;
1121 }
1122
Damien Millerb38eff82000-04-01 11:09:21 +10001123 /* Check if the fixed size part of the packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001124 if (sshbuf_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +10001125 return 0;
1126
1127 /* Parse the lengths of variable-length fields. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001128 ucp = sshbuf_mutable_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +10001129 if (ucp[0] == 0x42) { /* Byte order MSB first. */
1130 proto_len = 256 * ucp[6] + ucp[7];
1131 data_len = 256 * ucp[8] + ucp[9];
1132 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1133 proto_len = ucp[6] + 256 * ucp[7];
1134 data_len = ucp[8] + 256 * ucp[9];
1135 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001136 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001137 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001138 return -1;
1139 }
1140
1141 /* Check if the whole packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001142 if (sshbuf_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001143 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1144 return 0;
1145
1146 /* Check if authentication protocol matches. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001147 if (proto_len != strlen(sc->x11_saved_proto) ||
1148 memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001149 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001150 return -1;
1151 }
1152 /* Check if authentication data matches our fake data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001153 if (data_len != sc->x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001154 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001155 sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001156 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001157 return -1;
1158 }
1159 /* Check fake data length */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001160 if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
Damien Millerb38eff82000-04-01 11:09:21 +10001161 error("X11 fake_data_len %d != saved_data_len %d",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001162 sc->x11_fake_data_len, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001163 return -1;
1164 }
1165 /*
1166 * Received authentication protocol and data match
1167 * our fake data. Substitute the fake data with real
1168 * data.
1169 */
1170 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001171 sc->x11_saved_data, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001172 return 1;
1173}
1174
Ben Lindstrombba81212001-06-25 05:01:22 +00001175static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001176channel_pre_x11_open(struct ssh *ssh, Channel *c,
1177 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001178{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001179 int ret = x11_open_helper(ssh, c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001180
1181 /* c->force_drain = 1; */
1182
Damien Millerb38eff82000-04-01 11:09:21 +10001183 if (ret == 1) {
1184 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001185 channel_pre_open(ssh, c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001186 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001187 logit("X11 connection rejected because of wrong authentication.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001188 debug2("X11 rejected %d i%d/o%d",
1189 c->self, c->istate, c->ostate);
1190 chan_read_failed(ssh, c);
1191 sshbuf_reset(c->input);
1192 chan_ibuf_empty(ssh, c);
1193 sshbuf_reset(c->output);
1194 chan_write_failed(ssh, c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001195 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001196 }
1197}
1198
Damien Millere1537f92010-01-26 13:26:22 +11001199static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001200channel_pre_mux_client(struct ssh *ssh,
1201 Channel *c, fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11001202{
Damien Millerd530f5f2010-05-21 14:57:10 +10001203 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001204 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Millere1537f92010-01-26 13:26:22 +11001205 FD_SET(c->rfd, readset);
1206 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1207 /* clear buffer immediately (discard any partial packet) */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001208 sshbuf_reset(c->input);
1209 chan_ibuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001210 /* Start output drain. XXX just kill chan? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001211 chan_rcvd_oclose(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001212 }
1213 if (c->ostate == CHAN_OUTPUT_OPEN ||
1214 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001215 if (sshbuf_len(c->output) > 0)
Damien Millere1537f92010-01-26 13:26:22 +11001216 FD_SET(c->wfd, writeset);
1217 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001218 chan_obuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001219 }
1220}
1221
Ben Lindstromb3921512001-04-11 15:57:50 +00001222/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +00001223static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001224channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001225{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001226 const u_char *p;
1227 char *host;
Damien Miller1781f532009-01-28 16:24:41 +11001228 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001229 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001230 struct {
1231 u_int8_t version;
1232 u_int8_t command;
1233 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001234 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001235 } s4_req, s4_rsp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001236 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001237
Ben Lindstromb3921512001-04-11 15:57:50 +00001238 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001239
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001240 have = sshbuf_len(input);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001241 len = sizeof(s4_req);
1242 if (have < len)
1243 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001244 p = sshbuf_ptr(input);
Damien Miller1781f532009-01-28 16:24:41 +11001245
1246 need = 1;
1247 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1248 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1249 debug2("channel %d: socks4a request", c->self);
1250 /* ... and needs an extra string (the hostname) */
1251 need = 2;
1252 }
1253 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001254 for (found = 0, i = len; i < have; i++) {
1255 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001256 found++;
1257 if (found == need)
1258 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001259 }
1260 if (i > 1024) {
1261 /* the peer is probably sending garbage */
1262 debug("channel %d: decode socks4: too long",
1263 c->self);
1264 return -1;
1265 }
1266 }
Damien Miller1781f532009-01-28 16:24:41 +11001267 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001268 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001269 if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1270 (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1271 (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1272 (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001273 debug("channels %d: decode socks4: %s", c->self, ssh_err(r));
1274 return -1;
1275 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001276 have = sshbuf_len(input);
1277 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001278 if (memchr(p, '\0', have) == NULL) {
1279 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001280 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001281 return -1;
1282 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001283 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001284 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001285 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001286 strlcpy(username, p, sizeof(username));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001287 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001288 fatal("%s: channel %d: consume: %s", __func__,
1289 c->self, ssh_err(r));
1290 }
Darren Tuckera627d422013-06-02 07:31:17 +10001291 free(c->path);
1292 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001293 if (need == 1) { /* SOCKS4: one string */
1294 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001295 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001296 } else { /* SOCKS4A: two strings */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001297 have = sshbuf_len(input);
1298 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001299 if (memchr(p, '\0', have) == NULL) {
1300 error("channel %d: decode socks4a: host not nul "
1301 "terminated", c->self);
1302 return -1;
1303 }
Damien Miller1781f532009-01-28 16:24:41 +11001304 len = strlen(p);
1305 debug2("channel %d: decode socks4a: host %s/%d",
1306 c->self, p, len);
1307 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001308 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001309 error("channel %d: hostname \"%.100s\" too long",
1310 c->self, p);
1311 return -1;
1312 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001313 c->path = xstrdup(p);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001314 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001315 fatal("%s: channel %d: consume: %s", __func__,
1316 c->self, ssh_err(r));
1317 }
Damien Miller1781f532009-01-28 16:24:41 +11001318 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001319 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001320
Damien Millerfbdeece2003-09-02 22:52:31 +10001321 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001322 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001323
Ben Lindstromb3921512001-04-11 15:57:50 +00001324 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001325 debug("channel %d: cannot handle: %s cn %d",
1326 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001327 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001328 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001329 s4_rsp.version = 0; /* vn: 0 for reply */
1330 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001331 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001332 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001333 if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001334 fatal("%s: channel %d: append reply: %s", __func__,
1335 c->self, ssh_err(r));
1336 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001337 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001338}
1339
Darren Tucker46471c92003-07-03 13:55:19 +10001340/* try to decode a socks5 header */
1341#define SSH_SOCKS5_AUTHDONE 0x1000
1342#define SSH_SOCKS5_NOAUTH 0x00
1343#define SSH_SOCKS5_IPV4 0x01
1344#define SSH_SOCKS5_DOMAIN 0x03
1345#define SSH_SOCKS5_IPV6 0x04
1346#define SSH_SOCKS5_CONNECT 0x01
1347#define SSH_SOCKS5_SUCCESS 0x00
1348
1349static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001350channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
Darren Tucker46471c92003-07-03 13:55:19 +10001351{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001352 /* XXX use get/put_u8 instead of trusting struct padding */
Darren Tucker46471c92003-07-03 13:55:19 +10001353 struct {
1354 u_int8_t version;
1355 u_int8_t command;
1356 u_int8_t reserved;
1357 u_int8_t atyp;
1358 } s5_req, s5_rsp;
1359 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001360 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001361 const u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001362 u_int have, need, i, found, nmethods, addrlen, af;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001363 int r;
Darren Tucker46471c92003-07-03 13:55:19 +10001364
1365 debug2("channel %d: decode socks5", c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001366 p = sshbuf_ptr(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001367 if (p[0] != 0x05)
1368 return -1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001369 have = sshbuf_len(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001370 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1371 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001372 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001373 return 0;
1374 nmethods = p[1];
1375 if (have < nmethods + 2)
1376 return 0;
1377 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001378 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001379 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001380 found = 1;
1381 break;
1382 }
1383 }
1384 if (!found) {
1385 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1386 c->self);
1387 return -1;
1388 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001389 if ((r = sshbuf_consume(input, nmethods + 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001390 fatal("%s: channel %d: consume: %s", __func__,
1391 c->self, ssh_err(r));
1392 }
1393 /* version, method */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001394 if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1395 (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001396 fatal("%s: channel %d: append reply: %s", __func__,
1397 c->self, ssh_err(r));
1398 }
Darren Tucker46471c92003-07-03 13:55:19 +10001399 c->flags |= SSH_SOCKS5_AUTHDONE;
1400 debug2("channel %d: socks5 auth done", c->self);
1401 return 0; /* need more */
1402 }
1403 debug2("channel %d: socks5 post auth", c->self);
1404 if (have < sizeof(s5_req)+1)
1405 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001406 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001407 if (s5_req.version != 0x05 ||
1408 s5_req.command != SSH_SOCKS5_CONNECT ||
1409 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001410 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001411 return -1;
1412 }
Darren Tucker47eede72005-03-14 23:08:12 +11001413 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001414 case SSH_SOCKS5_IPV4:
1415 addrlen = 4;
1416 af = AF_INET;
1417 break;
1418 case SSH_SOCKS5_DOMAIN:
1419 addrlen = p[sizeof(s5_req)];
1420 af = -1;
1421 break;
1422 case SSH_SOCKS5_IPV6:
1423 addrlen = 16;
1424 af = AF_INET6;
1425 break;
1426 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001427 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001428 return -1;
1429 }
Damien Miller0f077072006-07-10 22:21:02 +10001430 need = sizeof(s5_req) + addrlen + 2;
1431 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1432 need++;
1433 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001434 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001435 if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001436 fatal("%s: channel %d: consume: %s", __func__,
1437 c->self, ssh_err(r));
1438 }
1439 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1440 /* host string length */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001441 if ((r = sshbuf_consume(input, 1)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001442 fatal("%s: channel %d: consume: %s", __func__,
1443 c->self, ssh_err(r));
1444 }
1445 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001446 if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1447 (r = sshbuf_get(input, &dest_port, 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001448 debug("channel %d: parse addr/port: %s", c->self, ssh_err(r));
1449 return -1;
1450 }
Darren Tucker46471c92003-07-03 13:55:19 +10001451 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001452 free(c->path);
1453 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001454 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001455 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001456 error("channel %d: dynamic request: socks5 hostname "
1457 "\"%.100s\" too long", c->self, dest_addr);
1458 return -1;
1459 }
1460 c->path = xstrdup(dest_addr);
1461 } else {
1462 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1463 return -1;
1464 c->path = xstrdup(ntop);
1465 }
Darren Tucker46471c92003-07-03 13:55:19 +10001466 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001467
Damien Millerfbdeece2003-09-02 22:52:31 +10001468 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001469 c->self, c->path, c->host_port, s5_req.command);
1470
1471 s5_rsp.version = 0x05;
1472 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1473 s5_rsp.reserved = 0; /* ignored */
1474 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001475 dest_port = 0; /* ignored */
1476
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001477 if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1478 (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1479 (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001480 fatal("%s: channel %d: append reply: %s", __func__,
1481 c->self, ssh_err(r));
Darren Tucker46471c92003-07-03 13:55:19 +10001482 return 1;
1483}
1484
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001485Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001486channel_connect_stdio_fwd(struct ssh *ssh,
1487 const char *host_to_connect, u_short port_to_connect, int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001488{
1489 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001490
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001491 debug("%s %s:%d", __func__, host_to_connect, port_to_connect);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001492
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001493 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001494 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1495 0, "stdio-forward", /*nonblock*/0);
1496
1497 c->path = xstrdup(host_to_connect);
1498 c->host_port = port_to_connect;
1499 c->listening_port = 0;
1500 c->force_drain = 1;
1501
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001502 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1503 port_open_helper(ssh, c, "direct-tcpip");
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001504
1505 return c;
1506}
1507
Ben Lindstromb3921512001-04-11 15:57:50 +00001508/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001509static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001510channel_pre_dynamic(struct ssh *ssh, Channel *c,
1511 fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001512{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001513 const u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001514 u_int have;
1515 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001516
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001517 have = sshbuf_len(c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001518 debug2("channel %d: pre_dynamic: have %d", c->self, have);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001519 /* sshbuf_dump(c->input, stderr); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001520 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001521 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001522 /* need more */
1523 FD_SET(c->sock, readset);
1524 return;
1525 }
1526 /* try to guess the protocol */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001527 p = sshbuf_ptr(c->input);
1528 /* XXX sshbuf_peek_u8? */
Ben Lindstromb3921512001-04-11 15:57:50 +00001529 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001530 case 0x04:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001531 ret = channel_decode_socks4(c, c->input, c->output);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001532 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001533 case 0x05:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001534 ret = channel_decode_socks5(c, c->input, c->output);
Darren Tucker46471c92003-07-03 13:55:19 +10001535 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001536 default:
1537 ret = -1;
1538 break;
1539 }
1540 if (ret < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001541 chan_mark_dead(ssh, c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001542 } else if (ret == 0) {
1543 debug2("channel %d: pre_dynamic: need more", c->self);
1544 /* need more */
1545 FD_SET(c->sock, readset);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001546 if (sshbuf_len(c->output))
1547 FD_SET(c->sock, writeset);
Ben Lindstromb3921512001-04-11 15:57:50 +00001548 } else {
1549 /* switch to the next state */
1550 c->type = SSH_CHANNEL_OPENING;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001551 port_open_helper(ssh, c, "direct-tcpip");
Ben Lindstromb3921512001-04-11 15:57:50 +00001552 }
1553}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001554
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001555/* simulate read-error */
1556static void
1557rdynamic_close(struct ssh *ssh, Channel *c)
1558{
1559 c->type = SSH_CHANNEL_OPEN;
1560 chan_read_failed(ssh, c);
1561 sshbuf_reset(c->input);
1562 chan_ibuf_empty(ssh, c);
1563 sshbuf_reset(c->output);
1564 chan_write_failed(ssh, c);
1565}
1566
1567/* reverse dynamic port forwarding */
1568static void
1569channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c)
1570{
1571 const u_char *p;
1572 u_int have, len;
1573 int r, ret;
1574
1575 have = sshbuf_len(c->output);
1576 debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1577 /* sshbuf_dump(c->output, stderr); */
1578 /* EOF received */
1579 if (c->flags & CHAN_EOF_RCVD) {
1580 if ((r = sshbuf_consume(c->output, have)) != 0) {
1581 fatal("%s: channel %d: consume: %s",
1582 __func__, c->self, ssh_err(r));
1583 }
1584 rdynamic_close(ssh, c);
1585 return;
1586 }
1587 /* check if the fixed size part of the packet is in buffer. */
1588 if (have < 3)
1589 return;
1590 /* try to guess the protocol */
1591 p = sshbuf_ptr(c->output);
1592 switch (p[0]) {
1593 case 0x04:
1594 /* switch input/output for reverse forwarding */
1595 ret = channel_decode_socks4(c, c->output, c->input);
1596 break;
1597 case 0x05:
1598 ret = channel_decode_socks5(c, c->output, c->input);
1599 break;
1600 default:
1601 ret = -1;
1602 break;
1603 }
1604 if (ret < 0) {
1605 rdynamic_close(ssh, c);
1606 } else if (ret == 0) {
1607 debug2("channel %d: pre_rdynamic: need more", c->self);
1608 /* send socks request to peer */
1609 len = sshbuf_len(c->input);
1610 if (len > 0 && len < c->remote_window) {
1611 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1612 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1613 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1614 (r = sshpkt_send(ssh)) != 0) {
1615 fatal("%s: channel %i: rdynamic: %s", __func__,
1616 c->self, ssh_err(r));
1617 }
1618 if ((r = sshbuf_consume(c->input, len)) != 0) {
1619 fatal("%s: channel %d: consume: %s",
1620 __func__, c->self, ssh_err(r));
1621 }
1622 c->remote_window -= len;
1623 }
1624 } else if (rdynamic_connect_finish(ssh, c) < 0) {
1625 /* the connect failed */
1626 rdynamic_close(ssh, c);
1627 }
1628}
1629
Damien Millerb38eff82000-04-01 11:09:21 +10001630/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001631static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001632channel_post_x11_listener(struct ssh *ssh, Channel *c,
1633 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001634{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001635 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001636 struct sockaddr_storage addr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001637 int r, newsock, oerrno, remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001638 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001639 char buf[16384], *remote_ipaddr;
Damien Millerb38eff82000-04-01 11:09:21 +10001640
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001641 if (!FD_ISSET(c->sock, readset))
1642 return;
Damien Millerbd483e72000-04-30 10:00:53 +10001643
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001644 debug("X11 connection requested.");
1645 addrlen = sizeof(addr);
1646 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1647 if (c->single_connection) {
1648 oerrno = errno;
1649 debug2("single_connection: closing X11 listener.");
1650 channel_close_fd(ssh, &c->sock);
1651 chan_mark_dead(ssh, c);
1652 errno = oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001653 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001654 if (newsock < 0) {
1655 if (errno != EINTR && errno != EWOULDBLOCK &&
1656 errno != ECONNABORTED)
1657 error("accept: %.100s", strerror(errno));
1658 if (errno == EMFILE || errno == ENFILE)
1659 c->notbefore = monotime() + 1;
1660 return;
1661 }
1662 set_nodelay(newsock);
1663 remote_ipaddr = get_peer_ipaddr(newsock);
1664 remote_port = get_peer_port(newsock);
1665 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1666 remote_ipaddr, remote_port);
1667
1668 nc = channel_new(ssh, "accepted x11 socket",
1669 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1670 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1671 open_preamble(ssh, __func__, nc, "x11");
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001672 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1673 (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001674 fatal("%s: channel %i: reply %s", __func__,
1675 c->self, ssh_err(r));
1676 }
1677 if ((r = sshpkt_send(ssh)) != 0)
1678 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
1679 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001680}
1681
Ben Lindstrombba81212001-06-25 05:01:22 +00001682static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001683port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001684{
Damien Miller4def1842013-12-29 17:45:26 +11001685 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001686 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001687 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001688 int remote_port = get_peer_port(c->sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001689 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001690
Damien Millerd6369432010-02-02 17:02:07 +11001691 if (remote_port == -1) {
1692 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001693 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001694 remote_ipaddr = xstrdup("127.0.0.1");
1695 remote_port = 65535;
1696 }
1697
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001698 free(c->remote_name);
1699 xasprintf(&c->remote_name,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001700 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001701 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001702 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001703 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001704
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001705 open_preamble(ssh, __func__, c, rtype);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001706 if (strcmp(rtype, "direct-tcpip") == 0) {
1707 /* target host, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001708 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1709 (r = sshpkt_put_u32(ssh, c->host_port)) != 0) {
1710 fatal("%s: channel %i: reply %s", __func__,
1711 c->self, ssh_err(r));
1712 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001713 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1714 /* target path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001715 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1716 fatal("%s: channel %i: reply %s", __func__,
1717 c->self, ssh_err(r));
1718 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001719 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1720 /* listen path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001721 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1722 fatal("%s: channel %i: reply %s", __func__,
1723 c->self, ssh_err(r));
1724 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001725 } else {
1726 /* listen address, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001727 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1728 (r = sshpkt_put_u32(ssh, local_port)) != 0) {
1729 fatal("%s: channel %i: reply %s", __func__,
1730 c->self, ssh_err(r));
1731 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001732 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001733 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1734 /* reserved for future owner/mode info */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001735 if ((r = sshpkt_put_cstring(ssh, "")) != 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 {
1740 /* originator host and port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001741 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1742 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) {
1743 fatal("%s: channel %i: reply %s", __func__,
1744 c->self, ssh_err(r));
1745 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001746 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001747 if ((r = sshpkt_send(ssh)) != 0)
1748 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001749 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001750 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001751}
1752
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001753void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001754channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001755{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001756 ssh->chanctxt->x11_refuse_time = refuse_time;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001757}
1758
Damien Millerb38eff82000-04-01 11:09:21 +10001759/*
1760 * This socket is listening for connections to a forwarded TCP/IP port.
1761 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001762static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001763channel_post_port_listener(struct ssh *ssh, Channel *c,
1764 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001765{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001766 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001767 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001768 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001769 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001770 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001771
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001772 if (!FD_ISSET(c->sock, readset))
1773 return;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001774
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001775 debug("Connection to port %d forwarding to %.100s port %d requested.",
1776 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001777
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001778 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1779 nextstate = SSH_CHANNEL_OPENING;
1780 rtype = "forwarded-tcpip";
1781 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1782 nextstate = SSH_CHANNEL_OPENING;
1783 rtype = "forwarded-streamlocal@openssh.com";
1784 } else if (c->host_port == PORT_STREAMLOCAL) {
1785 nextstate = SSH_CHANNEL_OPENING;
1786 rtype = "direct-streamlocal@openssh.com";
1787 } else if (c->host_port == 0) {
1788 nextstate = SSH_CHANNEL_DYNAMIC;
1789 rtype = "dynamic-tcpip";
1790 } else {
1791 nextstate = SSH_CHANNEL_OPENING;
1792 rtype = "direct-tcpip";
Damien Millerb38eff82000-04-01 11:09:21 +10001793 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001794
1795 addrlen = sizeof(addr);
1796 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1797 if (newsock < 0) {
1798 if (errno != EINTR && errno != EWOULDBLOCK &&
1799 errno != ECONNABORTED)
1800 error("accept: %.100s", strerror(errno));
1801 if (errno == EMFILE || errno == ENFILE)
1802 c->notbefore = monotime() + 1;
1803 return;
1804 }
1805 if (c->host_port != PORT_STREAMLOCAL)
1806 set_nodelay(newsock);
1807 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1808 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1809 nc->listening_port = c->listening_port;
1810 nc->host_port = c->host_port;
1811 if (c->path != NULL)
1812 nc->path = xstrdup(c->path);
1813
1814 if (nextstate != SSH_CHANNEL_DYNAMIC)
1815 port_open_helper(ssh, nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001816}
1817
1818/*
1819 * This is the authentication agent socket listening for connections from
1820 * clients.
1821 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001822static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001823channel_post_auth_listener(struct ssh *ssh, Channel *c,
1824 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001825{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001826 Channel *nc;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001827 int r, newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001828 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001829 socklen_t addrlen;
1830
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001831 if (!FD_ISSET(c->sock, readset))
1832 return;
1833
1834 addrlen = sizeof(addr);
1835 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1836 if (newsock < 0) {
1837 error("accept from auth socket: %.100s", strerror(errno));
1838 if (errno == EMFILE || errno == ENFILE)
1839 c->notbefore = monotime() + 1;
1840 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001841 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001842 nc = channel_new(ssh, "accepted auth socket",
1843 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1844 c->local_window_max, c->local_maxpacket,
1845 0, "accepted auth socket", 1);
1846 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1847 if ((r = sshpkt_send(ssh)) != 0)
1848 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001849}
1850
Ben Lindstrombba81212001-06-25 05:01:22 +00001851static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001852channel_post_connecting(struct ssh *ssh, Channel *c,
1853 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001854{
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001855 int err = 0, sock, isopen, r;
Ben Lindstrom11180952001-07-04 05:13:35 +00001856 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001857
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001858 if (!FD_ISSET(c->sock, writeset))
1859 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001860 if (!c->have_remote_id)
1861 fatal(":%s: channel %d: no remote id", __func__, c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001862 /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1863 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001864 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1865 err = errno;
1866 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001867 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001868 if (err == 0) {
1869 debug("channel %d: connected to %s port %d",
1870 c->self, c->connect_ctx.host, c->connect_ctx.port);
1871 channel_connect_ctx_free(&c->connect_ctx);
1872 c->type = SSH_CHANNEL_OPEN;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001873 if (isopen) {
1874 /* no message necessary */
1875 } else {
1876 if ((r = sshpkt_start(ssh,
1877 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1878 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1879 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1880 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1881 (r = sshpkt_put_u32(ssh, c->local_maxpacket))
1882 != 0)
1883 fatal("%s: channel %i: confirm: %s", __func__,
1884 c->self, ssh_err(r));
1885 if ((r = sshpkt_send(ssh)) != 0)
1886 fatal("%s: channel %i: %s", __func__, c->self,
1887 ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001888 }
1889 } else {
1890 debug("channel %d: connection failed: %s",
1891 c->self, strerror(err));
1892 /* Try next address, if any */
1893 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1894 close(c->sock);
1895 c->sock = c->rfd = c->wfd = sock;
1896 channel_find_maxfd(ssh->chanctxt);
1897 return;
1898 }
1899 /* Exhausted all addresses */
1900 error("connect_to %.100s port %d: failed.",
1901 c->connect_ctx.host, c->connect_ctx.port);
1902 channel_connect_ctx_free(&c->connect_ctx);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001903 if (isopen) {
1904 rdynamic_close(ssh, c);
1905 } else {
1906 if ((r = sshpkt_start(ssh,
1907 SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1908 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001909 (r = sshpkt_put_u32(ssh,
1910 SSH2_OPEN_CONNECT_FAILED)) != 0 ||
1911 (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1912 (r = sshpkt_put_cstring(ssh, "")) != 0) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001913 fatal("%s: channel %i: failure: %s", __func__,
1914 c->self, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001915 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001916 if ((r = sshpkt_send(ssh)) != 0)
1917 fatal("%s: channel %i: %s", __func__, c->self,
1918 ssh_err(r));
1919 chan_mark_dead(ssh, c);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001920 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001921 }
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001922}
1923
Ben Lindstrombba81212001-06-25 05:01:22 +00001924static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001925channel_handle_rfd(struct ssh *ssh, Channel *c,
1926 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001927{
Darren Tucker11327cc2005-03-14 23:22:25 +11001928 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001929 ssize_t len;
1930 int r, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001931
Damien Miller835284b2007-06-11 13:03:16 +10001932 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001933
1934 if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset)))
1935 return 1;
1936
1937 errno = 0;
1938 len = read(c->rfd, buf, sizeof(buf));
1939 if (len < 0 && (errno == EINTR ||
1940 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1941 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001942#ifndef PTY_ZEROREAD
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001943 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001944#else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001945 if ((!c->isatty && len <= 0) ||
1946 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001947#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001948 debug2("channel %d: read<=0 rfd %d len %zd",
1949 c->self, c->rfd, len);
1950 if (c->type != SSH_CHANNEL_OPEN) {
1951 debug2("channel %d: not open", c->self);
1952 chan_mark_dead(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001953 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001954 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001955 chan_read_failed(ssh, c);
Damien Millerad833b32000-08-23 10:46:23 +10001956 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001957 return -1;
1958 }
1959 if (c->input_filter != NULL) {
1960 if (c->input_filter(ssh, c, buf, len) == -1) {
1961 debug2("channel %d: filter stops", c->self);
1962 chan_read_failed(ssh, c);
1963 }
1964 } else if (c->datagram) {
1965 if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1966 fatal("%s: channel %d: put datagram: %s", __func__,
1967 c->self, ssh_err(r));
1968 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
1969 fatal("%s: channel %d: put data: %s", __func__,
1970 c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001971 }
1972 return 1;
1973}
Damien Miller4f7becb2006-03-26 14:10:14 +11001974
Ben Lindstrombba81212001-06-25 05:01:22 +00001975static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001976channel_handle_wfd(struct ssh *ssh, Channel *c,
1977 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001978{
Ben Lindstrome229b252001-03-05 06:28:06 +00001979 struct termios tio;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001980 u_char *data = NULL, *buf; /* XXX const; need filter API change */
1981 size_t dlen, olen = 0;
1982 int r, len;
1983
1984 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
1985 sshbuf_len(c->output) == 0)
1986 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001987
1988 /* Send buffered output data to the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001989 olen = sshbuf_len(c->output);
1990 if (c->output_filter != NULL) {
1991 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
1992 debug2("channel %d: filter stops", c->self);
1993 if (c->type != SSH_CHANNEL_OPEN)
1994 chan_mark_dead(ssh, c);
1995 else
1996 chan_write_failed(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001997 return -1;
1998 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001999 } else if (c->datagram) {
2000 if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
2001 fatal("%s: channel %d: get datagram: %s", __func__,
2002 c->self, ssh_err(r));
djm@openbsd.org9145a732017-09-12 07:32:04 +00002003 buf = data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002004 } else {
2005 buf = data = sshbuf_mutable_ptr(c->output);
2006 dlen = sshbuf_len(c->output);
2007 }
2008
2009 if (c->datagram) {
2010 /* ignore truncated writes, datagrams might get lost */
djm@openbsd.orgc704f642017-09-24 09:50:01 +00002011 len = write(c->wfd, buf, dlen);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002012 free(data);
2013 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
2014 errno == EWOULDBLOCK))
2015 return 1;
2016 if (len <= 0)
2017 goto write_fail;
2018 goto out;
2019 }
2020
2021#ifdef _AIX
2022 /* XXX: Later AIX versions can't push as much data to tty */
2023 if (c->wfd_isatty)
2024 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10002025#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002026
2027 len = write(c->wfd, buf, dlen);
2028 if (len < 0 &&
2029 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
2030 return 1;
2031 if (len <= 0) {
2032 write_fail:
2033 if (c->type != SSH_CHANNEL_OPEN) {
2034 debug2("channel %d: not open", c->self);
2035 chan_mark_dead(ssh, c);
2036 return -1;
2037 } else {
2038 chan_write_failed(ssh, c);
2039 }
2040 return -1;
2041 }
2042#ifndef BROKEN_TCGETATTR_ICANON
2043 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
2044 if (tcgetattr(c->wfd, &tio) == 0 &&
2045 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
2046 /*
2047 * Simulate echo to reduce the impact of
2048 * traffic analysis. We need to match the
2049 * size of a SSH2_MSG_CHANNEL_DATA message
2050 * (4 byte channel id + buf)
2051 */
2052 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
2053 (r = sshpkt_send(ssh)) != 0)
2054 fatal("%s: channel %d: ignore: %s",
2055 __func__, c->self, ssh_err(r));
2056 }
2057 }
2058#endif /* BROKEN_TCGETATTR_ICANON */
2059 if ((r = sshbuf_consume(c->output, len)) != 0) {
2060 fatal("%s: channel %d: consume: %s",
2061 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002062 }
Damien Miller7d457182010-08-05 23:09:48 +10002063 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002064 c->local_consumed += olen - sshbuf_len(c->output);
2065
Damien Miller33b13562000-04-04 14:38:59 +10002066 return 1;
2067}
Damien Miller4f7becb2006-03-26 14:10:14 +11002068
Ben Lindstrombba81212001-06-25 05:01:22 +00002069static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002070channel_handle_efd_write(struct ssh *ssh, Channel *c,
2071 fd_set *readset, fd_set *writeset)
2072{
2073 int r;
2074 ssize_t len;
2075
2076 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
2077 return 1;
2078
2079 len = write(c->efd, sshbuf_ptr(c->extended),
2080 sshbuf_len(c->extended));
2081 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
2082 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
2083 errno == EWOULDBLOCK))
2084 return 1;
2085 if (len <= 0) {
2086 debug2("channel %d: closing write-efd %d", c->self, c->efd);
2087 channel_close_fd(ssh, &c->efd);
2088 } else {
2089 if ((r = sshbuf_consume(c->extended, len)) != 0) {
2090 fatal("%s: channel %d: consume: %s",
2091 __func__, c->self, ssh_err(r));
2092 }
2093 c->local_consumed += len;
2094 }
2095 return 1;
2096}
2097
2098static int
2099channel_handle_efd_read(struct ssh *ssh, Channel *c,
2100 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002101{
Darren Tucker11327cc2005-03-14 23:22:25 +11002102 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002103 ssize_t len;
Damien Miller03e92dd2019-02-08 14:50:36 +11002104 int r, force;
Damien Miller33b13562000-04-04 14:38:59 +10002105
Damien Miller03e92dd2019-02-08 14:50:36 +11002106 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
2107
2108 if (c->efd == -1 || (!force && !FD_ISSET(c->efd, readset)))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002109 return 1;
2110
2111 len = read(c->efd, buf, sizeof(buf));
2112 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
2113 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
Damien Miller03e92dd2019-02-08 14:50:36 +11002114 errno == EWOULDBLOCK) && !force)))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002115 return 1;
2116 if (len <= 0) {
2117 debug2("channel %d: closing read-efd %d",
2118 c->self, c->efd);
2119 channel_close_fd(ssh, &c->efd);
2120 } else {
2121 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
2122 debug3("channel %d: discard efd",
2123 c->self);
2124 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
2125 fatal("%s: channel %d: append: %s",
2126 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002127 }
2128 }
2129 return 1;
2130}
Damien Miller4f7becb2006-03-26 14:10:14 +11002131
Damien Miller0e220db2004-06-15 10:34:08 +10002132static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002133channel_handle_efd(struct ssh *ssh, Channel *c,
2134 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002135{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002136 if (c->efd == -1)
2137 return 1;
2138
2139 /** XXX handle drain efd, too */
2140
2141 if (c->extended_usage == CHAN_EXTENDED_WRITE)
2142 return channel_handle_efd_write(ssh, c, readset, writeset);
2143 else if (c->extended_usage == CHAN_EXTENDED_READ ||
2144 c->extended_usage == CHAN_EXTENDED_IGNORE)
2145 return channel_handle_efd_read(ssh, c, readset, writeset);
2146
2147 return 1;
2148}
2149
2150static int
2151channel_check_window(struct ssh *ssh, Channel *c)
2152{
2153 int r;
2154
Ben Lindstromb3921512001-04-11 15:57:50 +00002155 if (c->type == SSH_CHANNEL_OPEN &&
2156 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10002157 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10002158 c->local_maxpacket*3) ||
2159 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10002160 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00002161 if (!c->have_remote_id)
2162 fatal(":%s: channel %d: no remote id",
2163 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002164 if ((r = sshpkt_start(ssh,
2165 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2166 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2167 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2168 (r = sshpkt_send(ssh)) != 0) {
2169 fatal("%s: channel %i: %s", __func__,
2170 c->self, ssh_err(r));
2171 }
Damien Millerd3444942000-09-30 14:20:03 +11002172 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002173 c->self, c->local_window,
2174 c->local_consumed);
2175 c->local_window += c->local_consumed;
2176 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002177 }
2178 return 1;
2179}
2180
Ben Lindstrombba81212001-06-25 05:01:22 +00002181static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002182channel_post_open(struct ssh *ssh, Channel *c,
2183 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002184{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002185 channel_handle_rfd(ssh, c, readset, writeset);
2186 channel_handle_wfd(ssh, c, readset, writeset);
2187 channel_handle_efd(ssh, c, readset, writeset);
2188 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002189}
2190
Damien Millere1537f92010-01-26 13:26:22 +11002191static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002192read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002193{
2194 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002195 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002196 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002197 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002198
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002199 if (sshbuf_len(c->input) < need) {
2200 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002201 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002202 if (len < 0 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002203 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002204 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002205 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002206 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002207 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002208 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002209 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2210 fatal("%s: channel %d: append: %s",
2211 __func__, c->self, ssh_err(r));
2212 }
Damien Millere1537f92010-01-26 13:26:22 +11002213 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002214 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002215}
2216
2217static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002218channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2219 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002220{
2221 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002222
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002223 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2224 return;
2225 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2226 return;
2227 if (c->mux_pause)
2228 return;
2229
2230 /*
2231 * Don't not read past the precise end of packets to
2232 * avoid disrupting fd passing.
2233 */
2234 if (read_mux(ssh, c, 4) < 4) /* read header */
2235 return;
2236 /* XXX sshbuf_peek_u32 */
2237 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002238#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002239 if (need > CHANNEL_MUX_MAX_PACKET) {
2240 debug2("channel %d: packet too big %u > %u",
2241 c->self, CHANNEL_MUX_MAX_PACKET, need);
2242 chan_rcvd_oclose(ssh, c);
2243 return;
Damien Millere1537f92010-01-26 13:26:22 +11002244 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002245 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2246 return;
2247 if (c->mux_rcb(ssh, c) != 0) {
2248 debug("channel %d: mux_rcb failed", c->self);
2249 chan_mark_dead(ssh, c);
2250 return;
Damien Millere1537f92010-01-26 13:26:22 +11002251 }
2252}
2253
2254static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002255channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2256 fd_set *readset, fd_set *writeset)
2257{
2258 ssize_t len;
2259 int r;
2260
2261 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2262 sshbuf_len(c->output) == 0)
2263 return;
2264
2265 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2266 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2267 return;
2268 if (len <= 0) {
2269 chan_mark_dead(ssh, c);
2270 return;
2271 }
2272 if ((r = sshbuf_consume(c->output, len)) != 0)
2273 fatal("%s: channel %d: consume: %s", __func__,
2274 c->self, ssh_err(r));
2275}
2276
2277static void
2278channel_post_mux_client(struct ssh *ssh, Channel *c,
2279 fd_set *readset, fd_set *writeset)
2280{
2281 channel_post_mux_client_read(ssh, c, readset, writeset);
2282 channel_post_mux_client_write(ssh, c, readset, writeset);
2283}
2284
2285static void
2286channel_post_mux_listener(struct ssh *ssh, Channel *c,
2287 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002288{
2289 Channel *nc;
2290 struct sockaddr_storage addr;
2291 socklen_t addrlen;
2292 int newsock;
2293 uid_t euid;
2294 gid_t egid;
2295
2296 if (!FD_ISSET(c->sock, readset))
2297 return;
2298
2299 debug("multiplexing control connection");
2300
2301 /*
2302 * Accept connection on control socket
2303 */
2304 memset(&addr, 0, sizeof(addr));
2305 addrlen = sizeof(addr);
2306 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2307 &addrlen)) == -1) {
2308 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002309 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002310 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002311 return;
2312 }
2313
2314 if (getpeereid(newsock, &euid, &egid) < 0) {
2315 error("%s getpeereid failed: %s", __func__,
2316 strerror(errno));
2317 close(newsock);
2318 return;
2319 }
2320 if ((euid != 0) && (getuid() != euid)) {
2321 error("multiplex uid mismatch: peer euid %u != uid %u",
2322 (u_int)euid, (u_int)getuid());
2323 close(newsock);
2324 return;
2325 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002326 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002327 newsock, newsock, -1, c->local_window_max,
2328 c->local_maxpacket, 0, "mux-control", 1);
2329 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002330 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002331 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002332 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002333 /* mux state transitions must not elicit protocol messages */
2334 nc->flags |= CHAN_LOCAL;
2335}
2336
Ben Lindstrombba81212001-06-25 05:01:22 +00002337static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002338channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002339{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002340 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002341
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002342 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2343 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2344 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002345
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002346 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2347 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2348 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2349 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2350 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2351 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2352 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2353 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2354 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2355 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002356 pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002357 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2358 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2359
2360 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2361 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2362 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2363 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2364 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2365 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2366 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2367 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2368 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002369 post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002370 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2371 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2372
2373 sc->channel_pre = pre;
2374 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002375}
2376
Damien Miller3ec27592001-10-12 11:35:04 +10002377/* gc dead channels */
2378static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002379channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002380{
2381 if (c == NULL)
2382 return;
2383 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002384 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002385 return;
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002386
Damien Millerfbdeece2003-09-02 22:52:31 +10002387 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002388 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002389 /* if we still have a callback */
2390 if (c->detach_user != NULL)
2391 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002392 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002393 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002394 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002395 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002396 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002397 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002398}
2399
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002400enum channel_table { CHAN_PRE, CHAN_POST };
2401
Ben Lindstrombba81212001-06-25 05:01:22 +00002402static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002403channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002404 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002405{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002406 struct ssh_channels *sc = ssh->chanctxt;
2407 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002408 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002409 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002410 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002411
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002412 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002413 if (unpause_secs != NULL)
2414 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002415 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2416 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002417 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002418 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002419 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002420 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002421 c->delayed = 0;
2422 else
2423 continue;
2424 }
Damien Millera6508752012-04-22 11:21:10 +10002425 if (ftab[c->type] != NULL) {
2426 /*
2427 * Run handlers that are not paused.
2428 */
2429 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002430 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002431 else if (unpause_secs != NULL) {
2432 /*
2433 * Collect the time that the earliest
2434 * channel comes off pause.
2435 */
2436 debug3("%s: chan %d: skip for %d more seconds",
2437 __func__, c->self,
2438 (int)(c->notbefore - now));
2439 if (*unpause_secs == 0 ||
2440 (c->notbefore - now) < *unpause_secs)
2441 *unpause_secs = c->notbefore - now;
2442 }
2443 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002444 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002445 }
Damien Millera6508752012-04-22 11:21:10 +10002446 if (unpause_secs != NULL && *unpause_secs != 0)
2447 debug3("%s: first channel unpauses in %d seconds",
2448 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002449}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002450
Ben Lindstrome9c99912001-06-09 00:41:05 +00002451/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002452 * Create sockets before allocating the select bitmasks.
2453 * This is necessary for things that need to happen after reading
2454 * the network-input but before channel_prepare_select().
2455 */
2456static void
2457channel_before_prepare_select(struct ssh *ssh)
2458{
2459 struct ssh_channels *sc = ssh->chanctxt;
2460 Channel *c;
2461 u_int i, oalloc;
2462
2463 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2464 c = sc->channels[i];
2465 if (c == NULL)
2466 continue;
2467 if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
2468 channel_before_prepare_select_rdynamic(ssh, c);
2469 }
2470}
2471
2472/*
Ben Lindstrome9c99912001-06-09 00:41:05 +00002473 * Allocate/update select bitmasks and add any bits relevant to channels in
2474 * select bitmasks.
2475 */
Damien Miller4af51302000-04-16 11:18:38 +10002476void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002477channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2478 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002479{
Damien Miller36812092006-03-26 14:22:47 +11002480 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002481
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002482 channel_before_prepare_select(ssh); /* might update channel_max_fd */
2483
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002484 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002485
Damien Miller36812092006-03-26 14:22:47 +11002486 nfdset = howmany(n+1, NFDBITS);
2487 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002488 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002489 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2490 sz = nfdset * sizeof(fd_mask);
2491
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002492 /* perhaps check sz < nalloc/2 and shrink? */
2493 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002494 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2495 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002496 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002497 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002498 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002499 memset(*readsetp, 0, sz);
2500 memset(*writesetp, 0, sz);
2501
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002502 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002503 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002504 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002505}
2506
Ben Lindstrome9c99912001-06-09 00:41:05 +00002507/*
2508 * After select, perform any appropriate operations for channels which have
2509 * events pending.
2510 */
Damien Miller4af51302000-04-16 11:18:38 +10002511void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002512channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002513{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002514 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002515}
2516
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002517/*
2518 * Enqueue data for channels with open or draining c->input.
2519 */
2520static void
2521channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2522{
djm@openbsd.org55486f52017-09-23 22:04:07 +00002523 size_t len, plen;
2524 const u_char *pkt;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002525 int r;
2526
2527 if ((len = sshbuf_len(c->input)) == 0) {
2528 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2529 /*
2530 * input-buffer is empty and read-socket shutdown:
2531 * tell peer, that we will not send more data:
2532 * send IEOF.
2533 * hack for extended data: delay EOF if EFD still
2534 * in use.
2535 */
2536 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2537 debug2("channel %d: "
2538 "ibuf_empty delayed efd %d/(%zu)",
2539 c->self, c->efd, sshbuf_len(c->extended));
2540 else
2541 chan_ibuf_empty(ssh, c);
2542 }
2543 return;
2544 }
2545
djm@openbsd.org9f532292017-09-12 06:35:31 +00002546 if (!c->have_remote_id)
2547 fatal(":%s: channel %d: no remote id", __func__, c->self);
2548
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002549 if (c->datagram) {
2550 /* Check datagram will fit; drop if not */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002551 if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
2552 fatal("%s: channel %d: get datagram: %s", __func__,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002553 c->self, ssh_err(r));
2554 /*
2555 * XXX this does tail-drop on the datagram queue which is
2556 * usually suboptimal compared to head-drop. Better to have
2557 * backpressure at read time? (i.e. read + discard)
2558 */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002559 if (plen > c->remote_window || plen > c->remote_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002560 debug("channel %d: datagram too big", c->self);
2561 return;
2562 }
2563 /* Enqueue it */
2564 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2565 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org55486f52017-09-23 22:04:07 +00002566 (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002567 (r = sshpkt_send(ssh)) != 0) {
2568 fatal("%s: channel %i: datagram: %s", __func__,
2569 c->self, ssh_err(r));
2570 }
djm@openbsd.org55486f52017-09-23 22:04:07 +00002571 c->remote_window -= plen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002572 return;
2573 }
2574
2575 /* Enqueue packet for buffered data. */
2576 if (len > c->remote_window)
2577 len = c->remote_window;
2578 if (len > c->remote_maxpacket)
2579 len = c->remote_maxpacket;
2580 if (len == 0)
2581 return;
2582 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2583 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2584 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2585 (r = sshpkt_send(ssh)) != 0) {
2586 fatal("%s: channel %i: data: %s", __func__,
2587 c->self, ssh_err(r));
2588 }
2589 if ((r = sshbuf_consume(c->input, len)) != 0)
2590 fatal("%s: channel %i: consume: %s", __func__,
2591 c->self, ssh_err(r));
2592 c->remote_window -= len;
2593}
2594
2595/*
2596 * Enqueue data for channels with open c->extended in read mode.
2597 */
2598static void
2599channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2600{
2601 size_t len;
2602 int r;
2603
2604 if ((len = sshbuf_len(c->extended)) == 0)
2605 return;
2606
2607 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2608 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2609 if (len > c->remote_window)
2610 len = c->remote_window;
2611 if (len > c->remote_maxpacket)
2612 len = c->remote_maxpacket;
2613 if (len == 0)
2614 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002615 if (!c->have_remote_id)
2616 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002617 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2618 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2619 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2620 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2621 (r = sshpkt_send(ssh)) != 0) {
2622 fatal("%s: channel %i: data: %s", __func__,
2623 c->self, ssh_err(r));
2624 }
2625 if ((r = sshbuf_consume(c->extended, len)) != 0)
2626 fatal("%s: channel %i: consume: %s", __func__,
2627 c->self, ssh_err(r));
2628 c->remote_window -= len;
2629 debug2("channel %d: sent ext data %zu", c->self, len);
2630}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002631
Damien Miller5e953212001-01-30 09:14:00 +11002632/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002633void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002634channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002635{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002636 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002637 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002638 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002639
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002640 for (i = 0; i < sc->channels_alloc; i++) {
2641 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002642 if (c == NULL)
2643 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002644
Ben Lindstrome9c99912001-06-09 00:41:05 +00002645 /*
2646 * We are only interested in channels that can have buffered
2647 * incoming data.
2648 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002649 if (c->type != SSH_CHANNEL_OPEN)
2650 continue;
2651 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002652 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002653 debug3("channel %d: will not send data after close",
2654 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002655 continue;
2656 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002657
Damien Miller95def091999-11-25 00:26:21 +11002658 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002659 if (c->istate == CHAN_INPUT_OPEN ||
2660 c->istate == CHAN_INPUT_WAIT_DRAIN)
2661 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002662 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002663 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002664 c->extended_usage == CHAN_EXTENDED_READ)
2665 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002666 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002667}
2668
markus@openbsd.org8d057842016-09-30 09:19:13 +00002669/* -- mux proxy support */
2670
2671/*
2672 * When multiplexing channel messages for mux clients we have to deal
2673 * with downstream messages from the mux client and upstream messages
2674 * from the ssh server:
2675 * 1) Handling downstream messages is straightforward and happens
2676 * in channel_proxy_downstream():
2677 * - We forward all messages (mostly) unmodified to the server.
2678 * - However, in order to route messages from upstream to the correct
2679 * downstream client, we have to replace the channel IDs used by the
2680 * mux clients with a unique channel ID because the mux clients might
2681 * use conflicting channel IDs.
2682 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2683 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2684 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2685 * with the newly allocated channel ID.
2686 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
djm@openbsd.org001aa552018-04-10 00:10:49 +00002687 * channels and processed by channel_proxy_upstream(). The local channel ID
markus@openbsd.org8d057842016-09-30 09:19:13 +00002688 * is then translated back to the original mux client ID.
2689 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2690 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2691 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2692 * downstream mux client are removed.
2693 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2694 * requires more work, because they are not addressed to a specific
2695 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2696 * out whether the request is addressed to the local client or a
2697 * specific downstream client based on the listen-address/port.
djm@openbsd.org001aa552018-04-10 00:10:49 +00002698 * 6) Agent and X11-Forwarding have a similar problem and are currently
markus@openbsd.org8d057842016-09-30 09:19:13 +00002699 * not supported as the matching session/channel cannot be identified
2700 * easily.
2701 */
2702
2703/*
2704 * receive packets from downstream mux clients:
2705 * channel callback fired on read from mux client, creates
2706 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2707 * on channel creation.
2708 */
2709int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002710channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002711{
2712 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002713 struct sshbuf *original = NULL, *modified = NULL;
2714 const u_char *cp;
2715 char *ctype = NULL, *listen_host = NULL;
2716 u_char type;
2717 size_t have;
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002718 int ret = -1, r;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002719 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002720
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002721 /* sshbuf_dump(downstream->input, stderr); */
2722 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002723 != 0) {
2724 error("%s: malformed message: %s", __func__, ssh_err(r));
2725 return -1;
2726 }
2727 if (have < 2) {
2728 error("%s: short message", __func__);
2729 return -1;
2730 }
2731 type = cp[1];
2732 /* skip padlen + type */
2733 cp += 2;
2734 have -= 2;
2735 if (ssh_packet_log_type(type))
2736 debug3("%s: channel %u: down->up: type %u", __func__,
2737 downstream->self, type);
2738
2739 switch (type) {
2740 case SSH2_MSG_CHANNEL_OPEN:
2741 if ((original = sshbuf_from(cp, have)) == NULL ||
2742 (modified = sshbuf_new()) == NULL) {
2743 error("%s: alloc", __func__);
2744 goto out;
2745 }
2746 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2747 (r = sshbuf_get_u32(original, &id)) != 0) {
2748 error("%s: parse error %s", __func__, ssh_err(r));
2749 goto out;
2750 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002751 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002752 -1, -1, -1, 0, 0, 0, ctype, 1);
2753 c->mux_ctx = downstream; /* point to mux client */
2754 c->mux_downstream_id = id; /* original downstream id */
2755 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2756 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2757 (r = sshbuf_putb(modified, original)) != 0) {
2758 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002759 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002760 goto out;
2761 }
2762 break;
2763 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2764 /*
2765 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2766 * need to parse 'remote_id' instead of 'ctype'.
2767 */
2768 if ((original = sshbuf_from(cp, have)) == NULL ||
2769 (modified = sshbuf_new()) == NULL) {
2770 error("%s: alloc", __func__);
2771 goto out;
2772 }
2773 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2774 (r = sshbuf_get_u32(original, &id)) != 0) {
2775 error("%s: parse error %s", __func__, ssh_err(r));
2776 goto out;
2777 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002778 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002779 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2780 c->mux_ctx = downstream; /* point to mux client */
2781 c->mux_downstream_id = id;
2782 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002783 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002784 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2785 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2786 (r = sshbuf_putb(modified, original)) != 0) {
2787 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002788 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002789 goto out;
2790 }
2791 break;
2792 case SSH2_MSG_GLOBAL_REQUEST:
2793 if ((original = sshbuf_from(cp, have)) == NULL) {
2794 error("%s: alloc", __func__);
2795 goto out;
2796 }
2797 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2798 error("%s: parse error %s", __func__, ssh_err(r));
2799 goto out;
2800 }
2801 if (strcmp(ctype, "tcpip-forward") != 0) {
2802 error("%s: unsupported request %s", __func__, ctype);
2803 goto out;
2804 }
2805 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2806 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2807 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2808 error("%s: parse error %s", __func__, ssh_err(r));
2809 goto out;
2810 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002811 if (listen_port > 65535) {
2812 error("%s: tcpip-forward for %s: bad port %u",
2813 __func__, listen_host, listen_port);
2814 goto out;
2815 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002816 /* Record that connection to this host/port is permitted. */
djm@openbsd.org115063a2018-06-06 18:22:41 +00002817 permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002818 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002819 listen_host = NULL;
2820 break;
2821 case SSH2_MSG_CHANNEL_CLOSE:
2822 if (have < 4)
2823 break;
2824 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002825 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002826 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002827 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002828 else
2829 c->flags |= CHAN_CLOSE_SENT;
2830 }
2831 break;
2832 }
2833 if (modified) {
2834 if ((r = sshpkt_start(ssh, type)) != 0 ||
2835 (r = sshpkt_putb(ssh, modified)) != 0 ||
2836 (r = sshpkt_send(ssh)) != 0) {
2837 error("%s: send %s", __func__, ssh_err(r));
2838 goto out;
2839 }
2840 } else {
2841 if ((r = sshpkt_start(ssh, type)) != 0 ||
2842 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2843 (r = sshpkt_send(ssh)) != 0) {
2844 error("%s: send %s", __func__, ssh_err(r));
2845 goto out;
2846 }
2847 }
2848 ret = 0;
2849 out:
2850 free(ctype);
2851 free(listen_host);
2852 sshbuf_free(original);
2853 sshbuf_free(modified);
2854 return ret;
2855}
2856
2857/*
2858 * receive packets from upstream server and de-multiplex packets
2859 * to correct downstream:
2860 * implemented as a helper for channel input handlers,
2861 * replaces local (proxy) channel ID with downstream channel ID.
2862 */
2863int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002864channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002865{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002866 struct sshbuf *b = NULL;
2867 Channel *downstream;
2868 const u_char *cp = NULL;
2869 size_t len;
2870 int r;
2871
2872 /*
2873 * When receiving packets from the peer we need to check whether we
2874 * need to forward the packets to the mux client. In this case we
djm@openbsd.org001aa552018-04-10 00:10:49 +00002875 * restore the original channel id and keep track of CLOSE messages,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002876 * so we can cleanup the channel.
2877 */
2878 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2879 return 0;
2880 if ((downstream = c->mux_ctx) == NULL)
2881 return 0;
2882 switch (type) {
2883 case SSH2_MSG_CHANNEL_CLOSE:
2884 case SSH2_MSG_CHANNEL_DATA:
2885 case SSH2_MSG_CHANNEL_EOF:
2886 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2887 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2888 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2889 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2890 case SSH2_MSG_CHANNEL_SUCCESS:
2891 case SSH2_MSG_CHANNEL_FAILURE:
2892 case SSH2_MSG_CHANNEL_REQUEST:
2893 break;
2894 default:
2895 debug2("%s: channel %u: unsupported type %u", __func__,
2896 c->self, type);
2897 return 0;
2898 }
2899 if ((b = sshbuf_new()) == NULL) {
2900 error("%s: alloc reply", __func__);
2901 goto out;
2902 }
2903 /* get remaining payload (after id) */
2904 cp = sshpkt_ptr(ssh, &len);
2905 if (cp == NULL) {
2906 error("%s: no packet", __func__);
2907 goto out;
2908 }
2909 /* translate id and send to muxclient */
2910 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2911 (r = sshbuf_put_u8(b, type)) != 0 ||
2912 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2913 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002914 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002915 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2916 goto out;
2917 }
2918 /* sshbuf_dump(b, stderr); */
2919 if (ssh_packet_log_type(type))
2920 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2921 type);
2922 out:
2923 /* update state */
2924 switch (type) {
2925 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2926 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002927 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002928 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002929 c->have_remote_id = 1;
2930 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002931 break;
2932 case SSH2_MSG_CHANNEL_CLOSE:
2933 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002934 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002935 else
2936 c->flags |= CHAN_CLOSE_RCVD;
2937 break;
2938 }
2939 sshbuf_free(b);
2940 return 1;
2941}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002942
2943/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002944
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002945/* Parse a channel ID from the current packet */
2946static int
2947channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2948{
2949 u_int32_t id;
2950 int r;
2951
2952 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2953 error("%s: parse id: %s", where, ssh_err(r));
2954 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2955 }
2956 if (id > INT_MAX) {
2957 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2958 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2959 }
2960 return (int)id;
2961}
2962
2963/* Lookup a channel from an ID in the current packet */
2964static Channel *
2965channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2966{
2967 int id = channel_parse_id(ssh, where, what);
2968 Channel *c;
2969
2970 if ((c = channel_lookup(ssh, id)) == NULL) {
2971 ssh_packet_disconnect(ssh,
2972 "%s packet referred to nonexistent channel %d", what, id);
2973 }
2974 return c;
2975}
2976
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002977int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002978channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002979{
Damien Miller633de332014-05-15 13:48:26 +10002980 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002981 size_t data_len, win_len;
2982 Channel *c = channel_from_packet_id(ssh, __func__, "data");
2983 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002984
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002985 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002986 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002987
Damien Miller95def091999-11-25 00:26:21 +11002988 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002989 if (c->type != SSH_CHANNEL_OPEN &&
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002990 c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
2991 c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
Damien Millerb38eff82000-04-01 11:09:21 +10002992 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002993 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002994
Damien Miller95def091999-11-25 00:26:21 +11002995 /* Get the data. */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00002996 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
2997 (r = sshpkt_get_end(ssh)) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002998 fatal("%s: channel %d: get data: %s", __func__,
2999 c->self, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003000
Damien Miller7d457182010-08-05 23:09:48 +10003001 win_len = data_len;
3002 if (c->datagram)
3003 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10003004
Damien Millera04ad492004-01-21 11:02:09 +11003005 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003006 * The sending side reduces its window as it sends data, so we
3007 * must 'fake' consumption of the data in order to ensure that window
3008 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11003009 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003010 if (c->ostate != CHAN_OUTPUT_OPEN) {
3011 c->local_window -= win_len;
3012 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003013 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11003014 }
3015
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003016 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003017 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003018 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003019 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003020 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003021 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003022 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003023 c->self, win_len, c->local_window);
3024 return 0;
3025 }
3026 c->local_window -= win_len;
3027
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003028 if (c->datagram) {
3029 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
3030 fatal("%s: channel %d: append datagram: %s",
3031 __func__, c->self, ssh_err(r));
3032 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
3033 fatal("%s: channel %d: append data: %s",
3034 __func__, c->self, ssh_err(r));
3035
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003036 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003037}
Ben Lindstrome9c99912001-06-09 00:41:05 +00003038
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003039int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003040channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003041{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003042 const u_char *data;
3043 size_t data_len;
3044 u_int32_t tcode;
3045 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
3046 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003047
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003048 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003049 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003050 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003051 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003052 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003053 }
Ben Lindstromcf159442002-03-26 03:26:24 +00003054 if (c->flags & CHAN_EOF_RCVD) {
3055 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003056 debug("channel %d: accepting ext data after eof",
3057 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00003058 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003059 ssh_packet_disconnect(ssh, "Received extended_data "
3060 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00003061 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003062
3063 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
3064 error("%s: parse tcode: %s", __func__, ssh_err(r));
3065 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3066 }
Damien Miller33b13562000-04-04 14:38:59 +10003067 if (c->efd == -1 ||
3068 c->extended_usage != CHAN_EXTENDED_WRITE ||
3069 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10003070 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003071 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003072 }
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003073 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3074 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003075 error("%s: parse data: %s", __func__, ssh_err(r));
3076 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3077 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003078
Damien Miller33b13562000-04-04 14:38:59 +10003079 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003080 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10003081 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003082 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003083 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003084 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
3085 /* XXX sshpkt_getb? */
3086 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
3087 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10003088 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003089 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003090}
3091
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003092int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003093channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10003094{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003095 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003096 int r;
Damien Millerb38eff82000-04-01 11:09:21 +10003097
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003098 if ((r = sshpkt_get_end(ssh)) != 0) {
3099 error("%s: parse data: %s", __func__, ssh_err(r));
3100 ssh_packet_disconnect(ssh, "Invalid ieof message");
3101 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003102
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003103 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003104 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003105 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003106
3107 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11003108 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003109 debug("channel %d: FORCE input drain", c->self);
3110 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003111 if (sshbuf_len(c->input) == 0)
3112 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003113 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003114 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10003115}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003116
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003117int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003118channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003119{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003120 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003121 int r;
Damien Miller66823cd2002-01-22 23:11:38 +11003122
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.org8cc7a672019-01-19 21:37:13 +00003125 if ((r = sshpkt_get_end(ssh)) != 0) {
3126 error("%s: parse data: %s", __func__, ssh_err(r));
3127 ssh_packet_disconnect(ssh, "Invalid oclose message");
3128 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003129 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003130 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003131}
3132
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003133int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003134channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003135{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003136 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3137 u_int32_t remote_window, remote_maxpacket;
3138 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003139
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003140 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003141 return 0;
3142 if (c->type != SSH_CHANNEL_OPENING)
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003143 ssh_packet_disconnect(ssh, "Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003144 "non-opening channel %d.", c->self);
3145 /*
3146 * Record the remote channel number and mark that the channel
3147 * is now open.
3148 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00003149 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3150 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003151 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 ||
3152 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003153 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003154 ssh_packet_disconnect(ssh, "Invalid open confirmation message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003155 }
Damien Miller33b13562000-04-04 14:38:59 +10003156
djm@openbsd.org9f532292017-09-12 06:35:31 +00003157 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003158 c->remote_window = remote_window;
3159 c->remote_maxpacket = remote_maxpacket;
3160 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003161 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003162 debug2("%s: channel %d: callback start", __func__, c->self);
3163 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
3164 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003165 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003166 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3167 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003168 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003169}
3170
Ben Lindstrombba81212001-06-25 05:01:22 +00003171static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00003172reason2txt(int reason)
3173{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003174 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00003175 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3176 return "administratively prohibited";
3177 case SSH2_OPEN_CONNECT_FAILED:
3178 return "connect failed";
3179 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3180 return "unknown channel type";
3181 case SSH2_OPEN_RESOURCE_SHORTAGE:
3182 return "resource shortage";
3183 }
Ben Lindstrome2595442001-06-05 20:01:39 +00003184 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00003185}
3186
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003187int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003188channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003189{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003190 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3191 u_int32_t reason;
3192 char *msg = NULL;
3193 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003194
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003195 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003196 return 0;
3197 if (c->type != SSH_CHANNEL_OPENING)
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003198 ssh_packet_disconnect(ssh, "Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003199 "non-opening channel %d.", c->self);
3200 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
3201 error("%s: reason: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003202 ssh_packet_disconnect(ssh, "Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003203 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003204 /* skip language */
3205 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003206 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 ||
3207 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003208 error("%s: message/lang: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003209 ssh_packet_disconnect(ssh, "Invalid open failure message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003210 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003211 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003212 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3213 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003214 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003215 debug2("%s: channel %d: callback start", __func__, c->self);
3216 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3217 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003218 }
Damien Miller7a606212009-01-28 16:22:34 +11003219 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003220 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003221 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003222}
3223
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003224int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003225channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003226{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003227 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003228 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003229 u_int32_t adjust;
3230 u_int new_rwin;
3231 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003232
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003233 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003234 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003235 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00003236 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003237
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003238 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003239 return 0;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003240 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 ||
3241 (r = sshpkt_get_end(ssh)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003242 error("%s: adjust: %s", __func__, ssh_err(r));
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003243 ssh_packet_disconnect(ssh, "Invalid window adjust message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003244 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003245 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3246 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003247 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003248 c->self, adjust, c->remote_window);
3249 }
3250 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003251 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003252}
3253
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003254int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003255channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003256{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003257 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003258 Channel *c;
3259 struct channel_confirm *cc;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003260 int r;
Damien Millerb84886b2008-05-19 15:05:07 +10003261
3262 /* Reset keepalive timeout */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003263 ssh_packet_set_alive_timeouts(ssh, 0);
Damien Millerb84886b2008-05-19 15:05:07 +10003264
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003265 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003266
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003267 if ((c = channel_lookup(ssh, id)) == NULL) {
3268 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003269 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003270 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003271 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003272 return 0;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003273 if ((r = sshpkt_get_end(ssh)) != 0)
3274 ssh_packet_disconnect(ssh, "Invalid status confirm message");
Damien Millerb84886b2008-05-19 15:05:07 +10003275 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003276 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003277 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003278 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003279 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003280 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003281 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003282}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003283
Ben Lindstrome9c99912001-06-09 00:41:05 +00003284/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003285
Ben Lindstrom908afed2001-10-03 17:34:59 +00003286void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003287channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003288{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003289 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003290}
3291
Damien Millerf6dff7c2011-09-22 21:38:52 +10003292
3293/*
3294 * Determine whether or not a port forward listens to loopback, the
3295 * specified address or wildcard. On the client, a specified bind
3296 * address will always override gateway_ports. On the server, a
3297 * gateway_ports of 1 (``yes'') will override the client's specification
3298 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3299 * will bind to whatever address the client asked for.
3300 *
3301 * Special-case listen_addrs are:
3302 *
3303 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3304 * "" (empty string), "*" -> wildcard v4/v6
3305 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003306 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003307 */
3308static const char *
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003309channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003310 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003311{
3312 const char *addr = NULL;
3313 int wildcard = 0;
3314
3315 if (listen_addr == NULL) {
3316 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003317 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003318 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003319 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003320 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3321 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3322 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003323 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003324 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003325 /*
3326 * Notify client if they requested a specific listen
3327 * address and it was overridden.
3328 */
3329 if (*listen_addr != '\0' &&
3330 strcmp(listen_addr, "0.0.0.0") != 0 &&
3331 strcmp(listen_addr, "*") != 0) {
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003332 ssh_packet_send_debug(ssh,
3333 "Forwarding listen address "
Darren Tucker71152bc2013-10-10 10:27:21 +11003334 "\"%s\" overridden by server "
3335 "GatewayPorts", listen_addr);
3336 }
Damien Miller602943d2014-07-04 08:59:41 +10003337 } else if (strcmp(listen_addr, "localhost") != 0 ||
3338 strcmp(listen_addr, "127.0.0.1") == 0 ||
3339 strcmp(listen_addr, "::1") == 0) {
3340 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003341 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003342 }
3343 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3344 strcmp(listen_addr, "::1") == 0) {
3345 /*
3346 * If a specific IPv4/IPv6 localhost address has been
3347 * requested then accept it even if gateway_ports is in
3348 * effect. This allows the client to prefer IPv4 or IPv6.
3349 */
3350 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003351 }
3352 if (wildcardp != NULL)
3353 *wildcardp = wildcard;
3354 return addr;
3355}
3356
Damien Millerb16461c2002-01-22 23:29:22 +11003357static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003358channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3359 struct Forward *fwd, int *allocated_listen_port,
3360 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003361{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003362 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003363 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003364 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003365 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003366 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003367 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003368
Damien Millerf91ee4c2005-03-01 21:24:33 +11003369 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003370
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003371 if (is_client && fwd->connect_path != NULL) {
3372 host = fwd->connect_path;
3373 } else {
3374 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3375 fwd->listen_host : fwd->connect_host;
3376 if (host == NULL) {
3377 error("No forward host name.");
3378 return 0;
3379 }
3380 if (strlen(host) >= NI_MAXHOST) {
3381 error("Forward host name too long.");
3382 return 0;
3383 }
Kevin Steves12057502001-02-05 14:54:34 +00003384 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003385
Damien Millerf6dff7c2011-09-22 21:38:52 +10003386 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003387 addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard,
Damien Miller7acefbb2014-07-18 14:11:24 +10003388 is_client, fwd_opts);
3389 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003390 type, wildcard, (addr == NULL) ? "NULL" : addr);
3391
3392 /*
Damien Miller34132e52000-01-14 15:45:46 +11003393 * getaddrinfo returns a loopback address if the hostname is
3394 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003395 */
Damien Miller34132e52000-01-14 15:45:46 +11003396 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003397 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003398 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003399 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003400 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003401 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3402 if (addr == NULL) {
3403 /* This really shouldn't happen */
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003404 ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003405 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003406 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003407 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003408 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003409 }
Damien Millera7270302005-07-06 09:36:05 +10003410 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003411 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003412 if (allocated_listen_port != NULL)
3413 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003414 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003415 switch (ai->ai_family) {
3416 case AF_INET:
3417 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3418 sin_port;
3419 break;
3420 case AF_INET6:
3421 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3422 sin6_port;
3423 break;
3424 default:
Damien Miller34132e52000-01-14 15:45:46 +11003425 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003426 }
3427 /*
3428 * If allocating a port for -R forwards, then use the
3429 * same port for all address families.
3430 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003431 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3432 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3433 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003434 *lport_p = htons(*allocated_listen_port);
3435
Damien Miller34132e52000-01-14 15:45:46 +11003436 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003437 strport, sizeof(strport),
3438 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003439 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003440 continue;
3441 }
3442 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003443 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003444 if (sock < 0) {
3445 /* this is no error since kernel may not support ipv6 */
djm@openbsd.org927f8512017-12-05 01:30:19 +00003446 verbose("socket [%s]:%s: %.100s", ntop, strport,
3447 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003448 continue;
3449 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003450
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00003451 set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003452 if (ai->ai_family == AF_INET6)
3453 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003454
Damien Miller4bf648f2009-02-14 16:28:21 +11003455 debug("Local forwarding listening on %s port %s.",
3456 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003457
Damien Miller34132e52000-01-14 15:45:46 +11003458 /* Bind the socket to the address. */
3459 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003460 /*
3461 * address can be in if use ipv6 address is
3462 * already bound
3463 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003464 if (!ai->ai_next)
djm@openbsd.org927f8512017-12-05 01:30:19 +00003465 error("bind [%s]:%s: %.100s",
3466 ntop, strport, strerror(errno));
Damien Miller3c7eeb22000-03-03 22:35:33 +11003467 else
djm@openbsd.org927f8512017-12-05 01:30:19 +00003468 verbose("bind [%s]:%s: %.100s",
3469 ntop, strport, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003470
Damien Miller34132e52000-01-14 15:45:46 +11003471 close(sock);
3472 continue;
3473 }
3474 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003475 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003476 error("listen: %.100s", strerror(errno));
djm@openbsd.org927f8512017-12-05 01:30:19 +00003477 error("listen [%s]:%s: %.100s", ntop, strport,
3478 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003479 close(sock);
3480 continue;
3481 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003482
3483 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003484 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003485 * record what we got.
3486 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003487 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3488 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003489 allocated_listen_port != NULL &&
3490 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003491 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003492 debug("Allocated listen port %d",
3493 *allocated_listen_port);
3494 }
3495
Damien Miller34132e52000-01-14 15:45:46 +11003496 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003497 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003498 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003499 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003500 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003501 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003502 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003503 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003504 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3505 c->listening_port = *allocated_listen_port;
3506 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003507 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003508 success = 1;
3509 }
3510 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003511 error("%s: cannot listen to port: %d", __func__,
3512 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003513 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003514 return success;
Damien Miller95def091999-11-25 00:26:21 +11003515}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003516
Damien Miller7acefbb2014-07-18 14:11:24 +10003517static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003518channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3519 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003520{
3521 struct sockaddr_un sunaddr;
3522 const char *path;
3523 Channel *c;
3524 int port, sock;
3525 mode_t omask;
3526
3527 switch (type) {
3528 case SSH_CHANNEL_UNIX_LISTENER:
3529 if (fwd->connect_path != NULL) {
3530 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3531 error("Local connecting path too long: %s",
3532 fwd->connect_path);
3533 return 0;
3534 }
3535 path = fwd->connect_path;
3536 port = PORT_STREAMLOCAL;
3537 } else {
3538 if (fwd->connect_host == NULL) {
3539 error("No forward host name.");
3540 return 0;
3541 }
3542 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3543 error("Forward host name too long.");
3544 return 0;
3545 }
3546 path = fwd->connect_host;
3547 port = fwd->connect_port;
3548 }
3549 break;
3550 case SSH_CHANNEL_RUNIX_LISTENER:
3551 path = fwd->listen_path;
3552 port = PORT_STREAMLOCAL;
3553 break;
3554 default:
3555 error("%s: unexpected channel type %d", __func__, type);
3556 return 0;
3557 }
3558
3559 if (fwd->listen_path == NULL) {
3560 error("No forward path name.");
3561 return 0;
3562 }
3563 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3564 error("Local listening path too long: %s", fwd->listen_path);
3565 return 0;
3566 }
3567
3568 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3569
3570 /* Start a Unix domain listener. */
3571 omask = umask(fwd_opts->streamlocal_bind_mask);
3572 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3573 fwd_opts->streamlocal_bind_unlink);
3574 umask(omask);
3575 if (sock < 0)
3576 return 0;
3577
3578 debug("Local forwarding listening on path %s.", fwd->listen_path);
3579
3580 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003581 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003582 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3583 0, "unix listener", 1);
3584 c->path = xstrdup(path);
3585 c->host_port = port;
3586 c->listening_port = PORT_STREAMLOCAL;
3587 c->listening_addr = xstrdup(fwd->listen_path);
3588 return 1;
3589}
3590
3591static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003592channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3593 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003594{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003595 u_int i;
3596 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003597
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003598 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3599 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003600 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3601 continue;
3602 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3603 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003604 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003605 found = 1;
3606 }
3607 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003608
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003609 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003610}
3611
Damien Miller7acefbb2014-07-18 14:11:24 +10003612static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003613channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003614{
3615 u_int i;
3616 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003617
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003618 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3619 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003620 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3621 continue;
3622 if (c->path == NULL)
3623 continue;
3624 if (strcmp(c->path, path) == 0) {
3625 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003626 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003627 found = 1;
3628 }
3629 }
3630
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003631 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003632}
3633
3634int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003635channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003636{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003637 if (fwd->listen_path != NULL) {
3638 return channel_cancel_rport_listener_streamlocal(ssh,
3639 fwd->listen_path);
3640 } else {
3641 return channel_cancel_rport_listener_tcpip(ssh,
3642 fwd->listen_host, fwd->listen_port);
3643 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003644}
3645
3646static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003647channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3648 const char *lhost, u_short lport, int cport,
3649 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003650{
3651 u_int i;
3652 int found = 0;
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003653 const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003654
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003655 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3656 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003657 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3658 continue;
Damien Millerff773642011-09-22 21:39:48 +10003659 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003660 continue;
Damien Millerff773642011-09-22 21:39:48 +10003661 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3662 /* skip dynamic forwardings */
3663 if (c->host_port == 0)
3664 continue;
3665 } else {
3666 if (c->host_port != cport)
3667 continue;
3668 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003669 if ((c->listening_addr == NULL && addr != NULL) ||
3670 (c->listening_addr != NULL && addr == NULL))
3671 continue;
3672 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003673 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003674 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003675 found = 1;
3676 }
3677 }
3678
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003679 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003680}
3681
Damien Miller7acefbb2014-07-18 14:11:24 +10003682static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003683channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003684{
3685 u_int i;
3686 int found = 0;
3687
3688 if (path == NULL) {
3689 error("%s: no path specified.", __func__);
3690 return 0;
3691 }
3692
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003693 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3694 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003695 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3696 continue;
3697 if (c->listening_addr == NULL)
3698 continue;
3699 if (strcmp(c->listening_addr, path) == 0) {
3700 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003701 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003702 found = 1;
3703 }
3704 }
3705
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003706 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003707}
3708
3709int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003710channel_cancel_lport_listener(struct ssh *ssh,
3711 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003712{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003713 if (fwd->listen_path != NULL) {
3714 return channel_cancel_lport_listener_streamlocal(ssh,
3715 fwd->listen_path);
3716 } else {
3717 return channel_cancel_lport_listener_tcpip(ssh,
3718 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3719 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003720}
3721
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003722/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003723int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003724channel_setup_local_fwd_listener(struct ssh *ssh,
3725 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003726{
Damien Miller7acefbb2014-07-18 14:11:24 +10003727 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003728 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003729 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3730 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003731 return channel_setup_fwd_listener_tcpip(ssh,
3732 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003733 }
Damien Millerb16461c2002-01-22 23:29:22 +11003734}
3735
djm@openbsd.org115063a2018-06-06 18:22:41 +00003736/* Matches a remote forwarding permission against a requested forwarding */
3737static int
3738remote_open_match(struct permission *allowed_open, struct Forward *fwd)
3739{
3740 int ret;
3741 char *lhost;
3742
3743 /* XXX add ACLs for streamlocal */
3744 if (fwd->listen_path != NULL)
3745 return 1;
3746
3747 if (fwd->listen_host == NULL || allowed_open->listen_host == NULL)
3748 return 0;
3749
3750 if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT &&
3751 allowed_open->listen_port != fwd->listen_port)
3752 return 0;
3753
3754 /* Match hostnames case-insensitively */
3755 lhost = xstrdup(fwd->listen_host);
3756 lowercase(lhost);
3757 ret = match_pattern(lhost, allowed_open->listen_host);
3758 free(lhost);
3759
3760 return ret;
3761}
3762
3763/* Checks whether a requested remote forwarding is permitted */
3764static int
3765check_rfwd_permission(struct ssh *ssh, struct Forward *fwd)
3766{
3767 struct ssh_channels *sc = ssh->chanctxt;
3768 struct permission_set *pset = &sc->remote_perms;
3769 u_int i, permit, permit_adm = 1;
3770 struct permission *perm;
3771
3772 /* XXX apply GatewayPorts override before checking? */
3773
3774 permit = pset->all_permitted;
3775 if (!permit) {
3776 for (i = 0; i < pset->num_permitted_user; i++) {
3777 perm = &pset->permitted_user[i];
3778 if (remote_open_match(perm, fwd)) {
3779 permit = 1;
3780 break;
3781 }
3782 }
3783 }
3784
3785 if (pset->num_permitted_admin > 0) {
3786 permit_adm = 0;
3787 for (i = 0; i < pset->num_permitted_admin; i++) {
3788 perm = &pset->permitted_admin[i];
3789 if (remote_open_match(perm, fwd)) {
3790 permit_adm = 1;
3791 break;
3792 }
3793 }
3794 }
3795
3796 return permit && permit_adm;
3797}
3798
Damien Millerb16461c2002-01-22 23:29:22 +11003799/* protocol v2 remote port fwd, used by sshd */
3800int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003801channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003802 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003803{
djm@openbsd.org115063a2018-06-06 18:22:41 +00003804 if (!check_rfwd_permission(ssh, fwd)) {
djm@openbsd.org8cc7a672019-01-19 21:37:13 +00003805 ssh_packet_send_debug(ssh, "port forwarding refused");
djm@openbsd.org115063a2018-06-06 18:22:41 +00003806 return 0;
3807 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003808 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003809 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003810 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3811 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003812 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003813 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3814 fwd_opts);
3815 }
Damien Millerb16461c2002-01-22 23:29:22 +11003816}
3817
Damien Miller5428f641999-11-25 11:54:57 +11003818/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003819 * Translate the requested rfwd listen host to something usable for
3820 * this server.
3821 */
3822static const char *
3823channel_rfwd_bind_host(const char *listen_host)
3824{
3825 if (listen_host == NULL) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003826 return "localhost";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003827 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003828 return "";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003829 } else
3830 return listen_host;
3831}
3832
3833/*
Damien Miller5428f641999-11-25 11:54:57 +11003834 * Initiate forwarding of connections to port "port" on remote host through
3835 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003836 * Returns handle (index) for updating the dynamic listen port with
djm@openbsd.org115063a2018-06-06 18:22:41 +00003837 * channel_update_permission().
Damien Miller5428f641999-11-25 11:54:57 +11003838 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003839int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003840channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003841{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003842 int r, success = 0, idx = -1;
3843 char *host_to_connect, *listen_host, *listen_path;
3844 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003845
Damien Miller95def091999-11-25 00:26:21 +11003846 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003847 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003848 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3849 (r = sshpkt_put_cstring(ssh,
3850 "streamlocal-forward@openssh.com")) != 0 ||
3851 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3852 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3853 (r = sshpkt_send(ssh)) != 0 ||
3854 (r = ssh_packet_write_wait(ssh)) != 0)
3855 fatal("%s: request streamlocal: %s",
3856 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003857 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003858 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3859 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3860 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3861 (r = sshpkt_put_cstring(ssh,
3862 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3863 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3864 (r = sshpkt_send(ssh)) != 0 ||
3865 (r = ssh_packet_write_wait(ssh)) != 0)
3866 fatal("%s: request tcpip-forward: %s",
3867 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003868 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003869 /* Assume that server accepts the request */
3870 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003871 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003872 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003873 host_to_connect = listen_host = listen_path = NULL;
3874 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003875 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003876 host_to_connect = xstrdup(fwd->connect_path);
3877 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003878 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003879 host_to_connect = xstrdup(fwd->connect_host);
3880 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003881 }
3882 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003883 listen_path = xstrdup(fwd->listen_path);
3884 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003885 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003886 if (fwd->listen_host != NULL)
3887 listen_host = xstrdup(fwd->listen_host);
3888 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003889 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00003890 idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003891 host_to_connect, port_to_connect,
3892 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003893 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003894 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003895}
3896
Damien Miller4b3ed642014-07-02 15:29:40 +10003897static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003898open_match(struct permission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003899 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003900{
3901 if (allowed_open->host_to_connect == NULL)
3902 return 0;
3903 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3904 allowed_open->port_to_connect != requestedport)
3905 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003906 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3907 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003908 return 0;
3909 return 1;
3910}
3911
3912/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003913 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003914 * we don't support FWD_PERMIT_ANY_PORT and
3915 * need to translate between the configured-host (listen_host)
3916 * and what we've sent to the remote server (channel_rfwd_bind_host)
3917 */
3918static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003919open_listen_match_tcpip(struct permission *allowed_open,
Damien Miller7acefbb2014-07-18 14:11:24 +10003920 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003921{
3922 const char *allowed_host;
3923
3924 if (allowed_open->host_to_connect == NULL)
3925 return 0;
3926 if (allowed_open->listen_port != requestedport)
3927 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003928 if (!translate && allowed_open->listen_host == NULL &&
3929 requestedhost == NULL)
3930 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003931 allowed_host = translate ?
3932 channel_rfwd_bind_host(allowed_open->listen_host) :
3933 allowed_open->listen_host;
djm@openbsd.org90e51d62018-06-25 22:28:33 +00003934 if (allowed_host == NULL || requestedhost == NULL ||
Damien Miller4b3ed642014-07-02 15:29:40 +10003935 strcmp(allowed_host, requestedhost) != 0)
3936 return 0;
3937 return 1;
3938}
3939
Damien Miller7acefbb2014-07-18 14:11:24 +10003940static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003941open_listen_match_streamlocal(struct permission *allowed_open,
Damien Miller7acefbb2014-07-18 14:11:24 +10003942 const char *requestedpath)
3943{
3944 if (allowed_open->host_to_connect == NULL)
3945 return 0;
3946 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3947 return 0;
3948 if (allowed_open->listen_path == NULL ||
3949 strcmp(allowed_open->listen_path, requestedpath) != 0)
3950 return 0;
3951 return 1;
3952}
3953
Damien Miller5428f641999-11-25 11:54:57 +11003954/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003955 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003956 * local side.
3957 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003958static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003959channel_request_rforward_cancel_tcpip(struct ssh *ssh,
3960 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003961{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003962 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003963 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003964 int r;
3965 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003966 struct permission *perm;
Darren Tuckere7066df2004-05-24 10:18:05 +10003967
djm@openbsd.org115063a2018-06-06 18:22:41 +00003968 for (i = 0; i < pset->num_permitted_user; i++) {
3969 perm = &pset->permitted_user[i];
3970 if (open_listen_match_tcpip(perm, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003971 break;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003972 perm = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10003973 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00003974 if (perm == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003975 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003976 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003977 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003978 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3979 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
3980 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3981 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
3982 (r = sshpkt_put_u32(ssh, port)) != 0 ||
3983 (r = sshpkt_send(ssh)) != 0)
3984 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10003985
djm@openbsd.org115063a2018-06-06 18:22:41 +00003986 fwd_perm_clear(perm); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10003987
3988 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003989}
3990
3991/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003992 * Request cancellation of remote forwarding of Unix domain socket
3993 * path from local side.
3994 */
3995static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003996channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003997{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003998 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003999 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004000 int r;
4001 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004002 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004003
djm@openbsd.org115063a2018-06-06 18:22:41 +00004004 for (i = 0; i < pset->num_permitted_user; i++) {
4005 perm = &pset->permitted_user[i];
4006 if (open_listen_match_streamlocal(perm, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10004007 break;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004008 perm = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10004009 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00004010 if (perm == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004011 debug("%s: requested forward not found", __func__);
4012 return -1;
4013 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004014 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4015 (r = sshpkt_put_cstring(ssh,
4016 "cancel-streamlocal-forward@openssh.com")) != 0 ||
4017 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4018 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
4019 (r = sshpkt_send(ssh)) != 0)
4020 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10004021
djm@openbsd.org115063a2018-06-06 18:22:41 +00004022 fwd_perm_clear(perm); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10004023
4024 return 0;
4025}
4026
4027/*
4028 * Request cancellation of remote forwarding of a connection from local side.
4029 */
4030int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004031channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10004032{
4033 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004034 return channel_request_rforward_cancel_streamlocal(ssh,
4035 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10004036 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004037 return channel_request_rforward_cancel_tcpip(ssh,
4038 fwd->listen_host,
4039 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10004040 }
4041}
4042
4043/*
djm@openbsd.org115063a2018-06-06 18:22:41 +00004044 * Permits opening to any host/port if permitted_user[] is empty. This is
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004045 * usually called by the server, because the user could connect to any port
4046 * anyway, and the server has no way to know but to trust the client anyway.
4047 */
4048void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004049channel_permit_all(struct ssh *ssh, int where)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004050{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004051 struct permission_set *pset = permission_set_get(ssh, where);
4052
4053 if (pset->num_permitted_user == 0)
4054 pset->all_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004055}
4056
djm@openbsd.org115063a2018-06-06 18:22:41 +00004057/*
4058 * Permit the specified host/port for forwarding.
4059 */
Ben Lindstroma3700052001-04-05 23:26:32 +00004060void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004061channel_add_permission(struct ssh *ssh, int who, int where,
4062 char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004063{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004064 int local = where == FORWARD_LOCAL;
4065 struct permission_set *pset = permission_set_get(ssh, where);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004066
djm@openbsd.org115063a2018-06-06 18:22:41 +00004067 debug("allow %s forwarding to host %s port %d",
4068 fwd_ident(who, where), host, port);
4069 /*
4070 * Remote forwards set listen_host/port, local forwards set
4071 * host/port_to_connect.
4072 */
4073 permission_set_add(ssh, who, where,
4074 local ? host : 0, local ? port : 0,
4075 local ? NULL : host, NULL, local ? 0 : port, NULL);
4076 pset->all_permitted = 0;
4077}
4078
4079/*
4080 * Administratively disable forwarding.
4081 */
4082void
4083channel_disable_admin(struct ssh *ssh, int where)
4084{
4085 channel_clear_permission(ssh, FORWARD_ADM, where);
4086 permission_set_add(ssh, FORWARD_ADM, where,
4087 NULL, 0, NULL, NULL, 0, NULL);
4088}
4089
4090/*
4091 * Clear a list of permitted opens.
4092 */
4093void
4094channel_clear_permission(struct ssh *ssh, int who, int where)
4095{
4096 struct permission **permp;
4097 u_int *npermp;
4098
4099 permission_set_get_array(ssh, who, where, &permp, &npermp);
4100 *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp));
4101 *npermp = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004102}
4103
Darren Tucker68afb8c2011-10-02 18:59:03 +11004104/*
4105 * Update the listen port for a dynamic remote forward, after
4106 * the actual 'newport' has been allocated. If 'newport' < 0 is
4107 * passed then they entry will be invalidated.
4108 */
4109void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004110channel_update_permission(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11004111{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004112 struct permission_set *pset = &ssh->chanctxt->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004113
djm@openbsd.org115063a2018-06-06 18:22:41 +00004114 if (idx < 0 || (u_int)idx >= pset->num_permitted_user) {
4115 debug("%s: index out of range: %d num_permitted_user %d",
4116 __func__, idx, pset->num_permitted_user);
Darren Tucker68afb8c2011-10-02 18:59:03 +11004117 return;
4118 }
4119 debug("%s allowed port %d for forwarding to host %s port %d",
4120 newport > 0 ? "Updating" : "Removing",
4121 newport,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004122 pset->permitted_user[idx].host_to_connect,
4123 pset->permitted_user[idx].port_to_connect);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004124 if (newport <= 0)
djm@openbsd.org115063a2018-06-06 18:22:41 +00004125 fwd_perm_clear(&pset->permitted_user[idx]);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004126 else {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004127 pset->permitted_user[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11004128 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11004129 }
4130}
4131
Darren Tucker1338b9e2011-10-02 18:57:35 +11004132/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
4133int
4134permitopen_port(const char *p)
4135{
4136 int port;
4137
4138 if (strcmp(p, "*") == 0)
4139 return FWD_PERMIT_ANY_PORT;
4140 if ((port = a2port(p)) > 0)
4141 return port;
4142 return -1;
4143}
4144
Damien Millerbd740252008-05-19 15:37:09 +10004145/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00004146static int
Damien Millerbd740252008-05-19 15:37:09 +10004147connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004148{
Damien Millerbd740252008-05-19 15:37:09 +10004149 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10004150 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004151 char ntop[NI_MAXHOST];
4152 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11004153
Damien Millerbd740252008-05-19 15:37:09 +10004154 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004155 switch (cctx->ai->ai_family) {
4156 case AF_UNIX:
4157 /* unix:pathname instead of host:port */
4158 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
4159 strlcpy(ntop, "unix", sizeof(ntop));
4160 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
4161 break;
4162 case AF_INET:
4163 case AF_INET6:
4164 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
4165 ntop, sizeof(ntop), strport, sizeof(strport),
4166 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
4167 error("connect_next: getnameinfo failed");
4168 continue;
4169 }
4170 break;
4171 default:
Damien Miller34132e52000-01-14 15:45:46 +11004172 continue;
4173 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11004174 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4175 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004176 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11004177 error("socket: %.100s", strerror(errno));
4178 else
4179 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004180 continue;
4181 }
Damien Miller232711f2004-06-15 10:35:30 +10004182 if (set_nonblock(sock) == -1)
4183 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10004184 if (connect(sock, cctx->ai->ai_addr,
4185 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
4186 debug("connect_next: host %.100s ([%.100s]:%s): "
4187 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004188 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004189 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004190 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004191 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004192 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004193 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004194 if (cctx->ai->ai_family != AF_UNIX)
4195 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004196 debug("connect_next: host %.100s ([%.100s]:%s) "
4197 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4198 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004199 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004200 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004201 return -1;
4202}
Damien Millerb38eff82000-04-01 11:09:21 +10004203
Damien Millerbd740252008-05-19 15:37:09 +10004204static void
4205channel_connect_ctx_free(struct channel_connect *cctx)
4206{
Darren Tuckera627d422013-06-02 07:31:17 +10004207 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004208 if (cctx->aitop) {
4209 if (cctx->aitop->ai_family == AF_UNIX)
4210 free(cctx->aitop);
4211 else
4212 freeaddrinfo(cctx->aitop);
4213 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004214 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004215}
4216
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004217/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004218 * Return connecting socket to remote host:port or local socket path,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004219 * passing back the failure reason if appropriate.
4220 */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004221static int
4222connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4223 char *ctype, char *rname, struct channel_connect *cctx,
4224 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10004225{
4226 struct addrinfo hints;
4227 int gaierr;
4228 int sock = -1;
4229 char strport[NI_MAXSERV];
Damien Miller7acefbb2014-07-18 14:11:24 +10004230
4231 if (port == PORT_STREAMLOCAL) {
4232 struct sockaddr_un *sunaddr;
4233 struct addrinfo *ai;
4234
4235 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4236 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004237 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004238 }
4239
4240 /*
4241 * Fake up a struct addrinfo for AF_UNIX connections.
4242 * channel_connect_ctx_free() must check ai_family
4243 * and use free() not freeaddirinfo() for AF_UNIX.
4244 */
4245 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4246 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4247 ai->ai_addr = (struct sockaddr *)(ai + 1);
4248 ai->ai_addrlen = sizeof(*sunaddr);
4249 ai->ai_family = AF_UNIX;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004250 ai->ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004251 ai->ai_protocol = PF_UNSPEC;
4252 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4253 sunaddr->sun_family = AF_UNIX;
4254 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004255 cctx->aitop = ai;
Damien Miller7acefbb2014-07-18 14:11:24 +10004256 } else {
4257 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004258 hints.ai_family = ssh->chanctxt->IPv4or6;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004259 hints.ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004260 snprintf(strport, sizeof strport, "%d", port);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004261 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004262 != 0) {
4263 if (errmsg != NULL)
4264 *errmsg = ssh_gai_strerror(gaierr);
4265 if (reason != NULL)
4266 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10004267 error("connect_to %.100s: unknown host (%s)", name,
4268 ssh_gai_strerror(gaierr));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004269 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004270 }
Damien Millerbd740252008-05-19 15:37:09 +10004271 }
4272
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004273 cctx->host = xstrdup(name);
4274 cctx->port = port;
4275 cctx->ai = cctx->aitop;
Damien Millerbd740252008-05-19 15:37:09 +10004276
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004277 if ((sock = connect_next(cctx)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004278 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004279 name, port, strerror(errno));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004280 return -1;
4281 }
4282
4283 return sock;
4284}
4285
4286/* Return CONNECTING channel to remote host:port or local socket path */
4287static Channel *
4288connect_to(struct ssh *ssh, const char *host, int port,
4289 char *ctype, char *rname)
4290{
4291 struct channel_connect cctx;
4292 Channel *c;
4293 int sock;
4294
4295 memset(&cctx, 0, sizeof(cctx));
4296 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4297 &cctx, NULL, NULL);
4298 if (sock == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004299 channel_connect_ctx_free(&cctx);
4300 return NULL;
4301 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004302 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004303 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004304 c->host_port = port;
4305 c->path = xstrdup(host);
Damien Millerbd740252008-05-19 15:37:09 +10004306 c->connect_ctx = cctx;
Damien Millerbd740252008-05-19 15:37:09 +10004307
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004308 return c;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004309}
4310
markus@openbsd.org8d057842016-09-30 09:19:13 +00004311/*
4312 * returns either the newly connected channel or the downstream channel
4313 * that needs to deal with this connection.
4314 */
Damien Millerbd740252008-05-19 15:37:09 +10004315Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004316channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004317 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004318{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004319 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004320 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004321 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004322 struct permission *perm;
Damien Millerbd740252008-05-19 15:37:09 +10004323
djm@openbsd.org115063a2018-06-06 18:22:41 +00004324 for (i = 0; i < pset->num_permitted_user; i++) {
4325 perm = &pset->permitted_user[i];
4326 if (open_listen_match_tcpip(perm,
4327 listen_host, listen_port, 1)) {
4328 if (perm->downstream)
4329 return perm->downstream;
4330 if (perm->port_to_connect == 0)
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004331 return rdynamic_connect_prepare(ssh,
4332 ctype, rname);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004333 return connect_to(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004334 perm->host_to_connect, perm->port_to_connect,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004335 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004336 }
4337 }
4338 error("WARNING: Server requests forwarding for unknown listen_port %d",
4339 listen_port);
4340 return NULL;
4341}
4342
Damien Miller7acefbb2014-07-18 14:11:24 +10004343Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004344channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4345 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004346{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004347 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004348 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004349 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004350 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004351
djm@openbsd.org115063a2018-06-06 18:22:41 +00004352 for (i = 0; i < pset->num_permitted_user; i++) {
4353 perm = &pset->permitted_user[i];
4354 if (open_listen_match_streamlocal(perm, path)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004355 return connect_to(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004356 perm->host_to_connect, perm->port_to_connect,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004357 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004358 }
4359 }
4360 error("WARNING: Server requests forwarding for unknown path %.100s",
4361 path);
4362 return NULL;
4363}
4364
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004365/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004366Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004367channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4368 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004369{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004370 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004371 struct permission_set *pset = &sc->local_perms;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004372 struct channel_connect cctx;
4373 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004374 u_int i, permit, permit_adm = 1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004375 int sock;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004376 struct permission *perm;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004377
djm@openbsd.org115063a2018-06-06 18:22:41 +00004378 permit = pset->all_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004379 if (!permit) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004380 for (i = 0; i < pset->num_permitted_user; i++) {
4381 perm = &pset->permitted_user[i];
4382 if (open_match(perm, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004383 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004384 break;
4385 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004386 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004387 }
Damien Miller9b439df2006-07-24 14:04:00 +10004388
djm@openbsd.org115063a2018-06-06 18:22:41 +00004389 if (pset->num_permitted_admin > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004390 permit_adm = 0;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004391 for (i = 0; i < pset->num_permitted_admin; i++) {
4392 perm = &pset->permitted_admin[i];
4393 if (open_match(perm, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004394 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004395 break;
4396 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004397 }
Damien Miller9b439df2006-07-24 14:04:00 +10004398 }
4399
4400 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004401 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004402 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004403 if (reason != NULL)
4404 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004405 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004406 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004407
4408 memset(&cctx, 0, sizeof(cctx));
4409 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4410 &cctx, reason, errmsg);
4411 if (sock == -1) {
4412 channel_connect_ctx_free(&cctx);
4413 return NULL;
4414 }
4415
4416 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4417 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4418 c->host_port = port;
4419 c->path = xstrdup(host);
4420 c->connect_ctx = cctx;
4421
4422 return c;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004423}
4424
Damien Miller7acefbb2014-07-18 14:11:24 +10004425/* Check if connecting to that path is permitted and connect. */
4426Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004427channel_connect_to_path(struct ssh *ssh, const char *path,
4428 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004429{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004430 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004431 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004432 u_int i, permit, permit_adm = 1;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004433 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004434
djm@openbsd.org115063a2018-06-06 18:22:41 +00004435 permit = pset->all_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004436 if (!permit) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004437 for (i = 0; i < pset->num_permitted_user; i++) {
4438 perm = &pset->permitted_user[i];
4439 if (open_match(perm, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004440 permit = 1;
4441 break;
4442 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004443 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004444 }
4445
djm@openbsd.org115063a2018-06-06 18:22:41 +00004446 if (pset->num_permitted_admin > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004447 permit_adm = 0;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004448 for (i = 0; i < pset->num_permitted_admin; i++) {
4449 perm = &pset->permitted_admin[i];
4450 if (open_match(perm, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004451 permit_adm = 1;
4452 break;
4453 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004454 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004455 }
4456
4457 if (!permit || !permit_adm) {
4458 logit("Received request to connect to path %.100s, "
4459 "but the request was denied.", path);
4460 return NULL;
4461 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004462 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004463}
4464
Damien Miller0e220db2004-06-15 10:34:08 +10004465void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004466channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004467{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004468 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004469 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004470 int r;
4471 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004472
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004473 for (i = 0; i < sc->channels_alloc; i++) {
4474 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4475 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004476 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004477 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
Damien Miller0e220db2004-06-15 10:34:08 +10004478 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004479 channel_request_start(ssh, i, "window-change", 0);
4480 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4481 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4482 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4483 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4484 (r = sshpkt_send(ssh)) != 0)
4485 fatal("%s: channel %u: send window-change: %s",
4486 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004487 }
4488}
4489
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004490/* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4491static Channel *
4492rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4493{
4494 Channel *c;
4495 int r;
4496
4497 c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4498 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4499 c->host_port = 0;
4500 c->path = NULL;
4501
4502 /*
4503 * We need to open the channel before we have a FD,
4504 * so that we can get SOCKS header from peer.
4505 */
4506 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4507 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4508 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4509 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
4510 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
4511 fatal("%s: channel %i: confirm: %s", __func__,
4512 c->self, ssh_err(r));
4513 }
4514 return c;
4515}
4516
4517/* Return CONNECTING socket to remote host:port or local socket path */
4518static int
4519rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4520{
4521 struct channel_connect cctx;
4522 int sock;
4523
4524 memset(&cctx, 0, sizeof(cctx));
4525 sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4526 NULL, &cctx, NULL, NULL);
4527 if (sock == -1)
4528 channel_connect_ctx_free(&cctx);
4529 else {
4530 /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4531 c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4532 c->connect_ctx = cctx;
4533 channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4534 }
4535 return sock;
4536}
4537
Ben Lindstrome9c99912001-06-09 00:41:05 +00004538/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004539
Damien Miller5428f641999-11-25 11:54:57 +11004540/*
4541 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004542 * Returns 0 and a suitable display number for the DISPLAY variable
4543 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004544 */
Kevin Steves366298c2001-12-19 17:58:01 +00004545int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004546x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4547 int x11_use_localhost, int single_connection,
4548 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004549{
Damien Millere7378562001-12-21 14:58:35 +11004550 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004551 int display_number, sock;
4552 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004553 struct addrinfo hints, *ai, *aitop;
4554 char strport[NI_MAXSERV];
4555 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004556
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004557 if (chanids == NULL)
4558 return -1;
4559
Damien Millera34a28b1999-12-14 10:47:15 +11004560 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004561 display_number < MAX_DISPLAYS;
4562 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004563 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004564 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004565 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004566 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004567 hints.ai_socktype = SOCK_STREAM;
4568 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004569 if ((gaierr = getaddrinfo(NULL, strport,
4570 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004571 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004572 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004573 }
Damien Miller34132e52000-01-14 15:45:46 +11004574 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004575 if (ai->ai_family != AF_INET &&
4576 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004577 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004578 sock = socket(ai->ai_family, ai->ai_socktype,
4579 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004580 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004581 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4582#ifdef EPFNOSUPPORT
4583 && (errno != EPFNOSUPPORT)
4584#endif
4585 ) {
Damien Millere2192732000-01-17 13:22:55 +11004586 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004587 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004588 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004589 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004590 debug("x11_create_display_inet: Socket family %d not supported",
4591 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004592 continue;
4593 }
Damien Miller34132e52000-01-14 15:45:46 +11004594 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004595 if (ai->ai_family == AF_INET6)
4596 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004597 if (x11_use_localhost)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00004598 set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004599 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004600 debug2("%s: bind port %d: %.100s", __func__,
4601 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004602 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004603 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004604 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004605 num_socks = 0;
4606 break;
4607 }
4608 socks[num_socks++] = sock;
4609 if (num_socks == NUM_SOCKS)
4610 break;
Damien Miller95def091999-11-25 00:26:21 +11004611 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004612 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004613 if (num_socks > 0)
4614 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004615 }
Damien Miller95def091999-11-25 00:26:21 +11004616 if (display_number >= MAX_DISPLAYS) {
4617 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004618 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004619 }
Damien Miller95def091999-11-25 00:26:21 +11004620 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004621 for (n = 0; n < num_socks; n++) {
4622 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004623 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004624 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004625 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004626 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004627 }
Damien Miller95def091999-11-25 00:26:21 +11004628 }
Damien Miller34132e52000-01-14 15:45:46 +11004629
Damien Miller34132e52000-01-14 15:45:46 +11004630 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004631 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004632 for (n = 0; n < num_socks; n++) {
4633 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004634 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004635 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4636 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004637 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004638 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004639 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004640 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004641 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004642
Kevin Steves366298c2001-12-19 17:58:01 +00004643 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004644 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004645 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004646}
4647
Ben Lindstrombba81212001-06-25 05:01:22 +00004648static int
Damien Miller819dbb62009-01-21 16:46:26 +11004649connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004650{
Damien Miller95def091999-11-25 00:26:21 +11004651 int sock;
4652 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004653
Damien Miller3afe3752001-12-21 12:39:51 +11004654 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4655 if (sock < 0)
4656 error("socket: %.100s", strerror(errno));
4657 memset(&addr, 0, sizeof(addr));
4658 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004659 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004660 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004661 return sock;
4662 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004663 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4664 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004665}
4666
Damien Miller819dbb62009-01-21 16:46:26 +11004667static int
4668connect_local_xsocket(u_int dnr)
4669{
4670 char buf[1024];
4671 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4672 return connect_local_xsocket_path(buf);
4673}
4674
Darren Tuckerda39b092017-03-10 13:22:32 +11004675#ifdef __APPLE__
4676static int
4677is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4678{
4679 struct stat sbuf;
4680
4681 if (strlcpy(path, display, pathlen) >= pathlen) {
4682 error("%s: display path too long", __func__);
4683 return 0;
4684 }
4685 if (display[0] != '/')
4686 return 0;
4687 if (stat(path, &sbuf) == 0) {
4688 return 1;
4689 } else {
4690 char *dot = strrchr(path, '.');
4691 if (dot != NULL) {
4692 *dot = '\0';
4693 if (stat(path, &sbuf) == 0) {
4694 return 1;
4695 }
4696 }
4697 }
4698 return 0;
4699}
4700#endif
4701
Damien Millerbd483e72000-04-30 10:00:53 +10004702int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004703x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004704{
Damien Miller57c4e872006-03-31 23:11:07 +11004705 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004706 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004707 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004708 struct addrinfo hints, *ai, *aitop;
4709 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004710 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004711
Damien Miller95def091999-11-25 00:26:21 +11004712 /* Try to open a socket for the local X server. */
4713 display = getenv("DISPLAY");
4714 if (!display) {
4715 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004716 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004717 }
Damien Miller5428f641999-11-25 11:54:57 +11004718 /*
4719 * Now we decode the value of the DISPLAY variable and make a
4720 * connection to the real X server.
4721 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004722
Damien Miller819dbb62009-01-21 16:46:26 +11004723#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004724 /* Check if display is a path to a socket (as set by launchd). */
4725 {
4726 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004727
Darren Tuckerda39b092017-03-10 13:22:32 +11004728 if (is_path_to_xsocket(display, path, sizeof(path))) {
4729 debug("x11_connect_display: $DISPLAY is launchd");
4730
4731 /* Create a socket. */
4732 sock = connect_local_xsocket_path(path);
4733 if (sock < 0)
4734 return -1;
4735
4736 /* OK, we now have a connection to the display. */
4737 return sock;
4738 }
Damien Miller819dbb62009-01-21 16:46:26 +11004739 }
4740#endif
Damien Miller5428f641999-11-25 11:54:57 +11004741 /*
4742 * Check if it is a unix domain socket. Unix domain displays are in
4743 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4744 */
Damien Miller95def091999-11-25 00:26:21 +11004745 if (strncmp(display, "unix:", 5) == 0 ||
4746 display[0] == ':') {
4747 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004748 if (sscanf(strrchr(display, ':') + 1, "%u",
4749 &display_number) != 1) {
4750 error("Could not parse display number from DISPLAY: "
4751 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004752 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004753 }
4754 /* Create a socket. */
4755 sock = connect_local_xsocket(display_number);
4756 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004757 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004758
4759 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004760 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004761 }
Damien Miller5428f641999-11-25 11:54:57 +11004762 /*
4763 * Connect to an inet socket. The DISPLAY value is supposedly
4764 * hostname:d[.s], where hostname may also be numeric IP address.
4765 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004766 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004767 cp = strchr(buf, ':');
4768 if (!cp) {
4769 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004770 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004771 }
Damien Miller95def091999-11-25 00:26:21 +11004772 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004773 /*
4774 * buf now contains the host name. But first we parse the
4775 * display number.
4776 */
Damien Miller57c4e872006-03-31 23:11:07 +11004777 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004778 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004779 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004780 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004781 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004782
Damien Miller34132e52000-01-14 15:45:46 +11004783 /* Look up the host address */
4784 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004785 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004786 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004787 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004788 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004789 error("%.100s: unknown host. (%s)", buf,
4790 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004791 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004792 }
Damien Miller34132e52000-01-14 15:45:46 +11004793 for (ai = aitop; ai; ai = ai->ai_next) {
4794 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004795 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004796 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004797 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004798 continue;
4799 }
4800 /* Connect it to the display. */
4801 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004802 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004803 6000 + display_number, strerror(errno));
4804 close(sock);
4805 continue;
4806 }
4807 /* Success */
4808 break;
Damien Miller34132e52000-01-14 15:45:46 +11004809 }
Damien Miller34132e52000-01-14 15:45:46 +11004810 freeaddrinfo(aitop);
4811 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004812 error("connect %.100s port %u: %.100s", buf,
4813 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004814 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004815 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004816 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004817 return sock;
4818}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004819
Damien Millerbd483e72000-04-30 10:00:53 +10004820/*
Damien Miller5428f641999-11-25 11:54:57 +11004821 * Requests forwarding of X11 connections, generates fake authentication
4822 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004823 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004824 */
Damien Miller4af51302000-04-16 11:18:38 +10004825void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004826x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4827 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004828{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004829 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004830 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004831 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004832 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004833 char *new_data;
4834 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004835
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004836 if (sc->x11_saved_display == NULL)
4837 sc->x11_saved_display = xstrdup(disp);
4838 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004839 error("x11_request_forwarding_with_spoofing: different "
4840 "$DISPLAY already forwarded");
4841 return;
4842 }
4843
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004844 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004845 if (cp)
4846 cp = strchr(cp, '.');
4847 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004848 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004849 else
4850 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004851
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004852 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004853 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004854 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004855
4856 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004857 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004858 for (i = 0; i < data_len; i++) {
4859 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4860 fatal("x11_request_forwarding: bad "
4861 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004862 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004863 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004864 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004865
4866 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004867 sc->x11_fake_data = xmalloc(data_len);
4868 arc4random_buf(sc->x11_fake_data, data_len);
4869 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004870 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004871
Damien Miller95def091999-11-25 00:26:21 +11004872 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004873 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004874
Damien Miller95def091999-11-25 00:26:21 +11004875 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004876 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4877 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4878 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4879 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4880 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4881 (r = sshpkt_send(ssh)) != 0 ||
4882 (r = ssh_packet_write_wait(ssh)) != 0)
4883 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004884 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004885}