blob: c85d46abd7629840cb22e62e87d3a486b2821a2a [file] [log] [blame]
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +00001/* $OpenBSD: channels.c,v 1.386 2018/10/04 01:04:52 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
230 if ((sc = calloc(1, sizeof(*sc))) == NULL ||
231 (sc->channel_pre = calloc(SSH_CHANNEL_MAX_TYPE,
232 sizeof(*sc->channel_pre))) == NULL ||
233 (sc->channel_post = calloc(SSH_CHANNEL_MAX_TYPE,
234 sizeof(*sc->channel_post))) == NULL)
235 fatal("%s: allocation failed", __func__);
236 sc->channels_alloc = 10;
237 sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels));
238 sc->IPv4or6 = AF_UNSPEC;
239 channel_handler_init(sc);
240
241 ssh->chanctxt = sc;
242}
243
Damien Millerb38eff82000-04-01 11:09:21 +1000244Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000245channel_by_id(struct ssh *ssh, int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000246{
247 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000248
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000249 if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
250 logit("%s: %d: bad id", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000251 return NULL;
252 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000253 c = ssh->chanctxt->channels[id];
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000254 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000255 logit("%s: %d: bad id: channel free", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000256 return NULL;
257 }
258 return c;
259}
260
markus@openbsd.org8d057842016-09-30 09:19:13 +0000261Channel *
djm@openbsd.org9f532292017-09-12 06:35:31 +0000262channel_by_remote_id(struct ssh *ssh, u_int remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000263{
264 Channel *c;
265 u_int i;
266
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000267 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
268 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000269 if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000270 return c;
271 }
272 return NULL;
273}
274
Damien Miller5428f641999-11-25 11:54:57 +1100275/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100276 * Returns the channel if it is allowed to receive protocol messages.
277 * Private channels, like listening sockets, may not receive messages.
278 */
279Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000280channel_lookup(struct ssh *ssh, int id)
Damien Millerd47c62a2005-12-13 19:33:57 +1100281{
282 Channel *c;
283
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000284 if ((c = channel_by_id(ssh, id)) == NULL)
285 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100286
Damien Millerd62f2ca2006-03-26 13:57:41 +1100287 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100288 case SSH_CHANNEL_X11_OPEN:
289 case SSH_CHANNEL_LARVAL:
290 case SSH_CHANNEL_CONNECTING:
291 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000292 case SSH_CHANNEL_RDYNAMIC_OPEN:
293 case SSH_CHANNEL_RDYNAMIC_FINISH:
Damien Millerd47c62a2005-12-13 19:33:57 +1100294 case SSH_CHANNEL_OPENING:
295 case SSH_CHANNEL_OPEN:
Damien Miller36187092013-06-10 13:07:11 +1000296 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000297 case SSH_CHANNEL_MUX_PROXY:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000298 return c;
Damien Millerd47c62a2005-12-13 19:33:57 +1100299 }
300 logit("Non-public channel %d, type %d.", id, c->type);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000301 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100302}
303
304/*
Damien Millere247cc42000-05-07 12:03:14 +1000305 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000306 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100307 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000308static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000309channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000310 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000312 struct ssh_channels *sc = ssh->chanctxt;
313
Damien Miller95def091999-11-25 00:26:21 +1100314 /* Update the maximum file descriptor value. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000315 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd);
316 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd);
317 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100318
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100319 if (rfd != -1)
320 fcntl(rfd, F_SETFD, FD_CLOEXEC);
321 if (wfd != -1 && wfd != rfd)
322 fcntl(wfd, F_SETFD, FD_CLOEXEC);
323 if (efd != -1 && efd != rfd && efd != wfd)
324 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000325
Damien Miller6f83b8e2000-05-02 09:23:45 +1000326 c->rfd = rfd;
327 c->wfd = wfd;
328 c->sock = (rfd == wfd) ? rfd : -1;
329 c->efd = efd;
330 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100331
Darren Tuckered3cdc02008-06-16 23:29:18 +1000332 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000333 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000334#ifdef _AIX
335 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000336 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000337#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100338
Damien Miller69b69aa2000-10-28 14:19:58 +1100339 /* enable nonblocking mode */
340 if (nonblock) {
341 if (rfd != -1)
342 set_nonblock(rfd);
343 if (wfd != -1)
344 set_nonblock(wfd);
345 if (efd != -1)
346 set_nonblock(efd);
347 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000348}
349
350/*
351 * Allocate a new channel object and set its type and socket. This will cause
352 * remote_name to be freed.
353 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000354Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000355channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000356 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000357{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000358 struct ssh_channels *sc = ssh->chanctxt;
359 u_int i, found;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000360 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361
Damien Miller95def091999-11-25 00:26:21 +1100362 /* Try to find a free slot where to put the new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000363 for (i = 0; i < sc->channels_alloc; i++) {
364 if (sc->channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100365 /* Found a free slot. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000366 found = i;
Damien Miller95def091999-11-25 00:26:21 +1100367 break;
368 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000369 }
370 if (i >= sc->channels_alloc) {
371 /*
372 * There are no free slots. Take last+1 slot and expand
373 * the array.
374 */
375 found = sc->channels_alloc;
376 if (sc->channels_alloc > CHANNELS_MAX_CHANNELS)
377 fatal("%s: internal error: channels_alloc %d too big",
378 __func__, sc->channels_alloc);
379 sc->channels = xrecallocarray(sc->channels, sc->channels_alloc,
380 sc->channels_alloc + 10, sizeof(*sc->channels));
381 sc->channels_alloc += 10;
382 debug2("channel: expanding %d", sc->channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100383 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000384 /* Initialize and return new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000385 c = sc->channels[found] = xcalloc(1, sizeof(Channel));
386 if ((c->input = sshbuf_new()) == NULL ||
387 (c->output = sshbuf_new()) == NULL ||
388 (c->extended = sshbuf_new()) == NULL)
389 fatal("%s: sshbuf_new failed", __func__);
Damien Miller5144df92002-01-22 23:28:45 +1100390 c->ostate = CHAN_OUTPUT_OPEN;
391 c->istate = CHAN_INPUT_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000392 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Miller95def091999-11-25 00:26:21 +1100393 c->self = found;
394 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000395 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000396 c->local_window = window;
397 c->local_window_max = window;
Damien Millerbd483e72000-04-30 10:00:53 +1000398 c->local_maxpacket = maxpack;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000399 c->remote_name = xstrdup(remote_name);
Damien Millere1537f92010-01-26 13:26:22 +1100400 c->ctl_chan = -1;
Darren Tucker876045b2010-01-08 17:08:00 +1100401 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000402 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100403 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000404 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000406
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000407static void
408channel_find_maxfd(struct ssh_channels *sc)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000409{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000410 u_int i;
411 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000412 Channel *c;
413
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000414 for (i = 0; i < sc->channels_alloc; i++) {
415 c = sc->channels[i];
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000416 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000417 max = MAXIMUM(max, c->rfd);
418 max = MAXIMUM(max, c->wfd);
419 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000420 }
421 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000422 sc->channel_max_fd = max;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000423}
424
425int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000426channel_close_fd(struct ssh *ssh, int *fdp)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000427{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000428 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000429 int ret = 0, fd = *fdp;
430
431 if (fd != -1) {
432 ret = close(fd);
433 *fdp = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000434 if (fd == sc->channel_max_fd)
435 channel_find_maxfd(sc);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000436 }
437 return ret;
438}
439
Damien Miller6f83b8e2000-05-02 09:23:45 +1000440/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000441static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000442channel_close_fds(struct ssh *ssh, Channel *c)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000443{
tb@openbsd.org50693202018-02-05 05:36:49 +0000444 int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
445
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000446 channel_close_fd(ssh, &c->sock);
tb@openbsd.org50693202018-02-05 05:36:49 +0000447 if (rfd != sock)
448 channel_close_fd(ssh, &c->rfd);
449 if (wfd != sock && wfd != rfd)
450 channel_close_fd(ssh, &c->wfd);
451 if (efd != sock && efd != rfd && efd != wfd)
452 channel_close_fd(ssh, &c->efd);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000453}
454
455static void
djm@openbsd.org115063a2018-06-06 18:22:41 +0000456fwd_perm_clear(struct permission *perm)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000457{
djm@openbsd.org115063a2018-06-06 18:22:41 +0000458 free(perm->host_to_connect);
459 free(perm->listen_host);
460 free(perm->listen_path);
461 bzero(perm, sizeof(*perm));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000462}
463
djm@openbsd.org115063a2018-06-06 18:22:41 +0000464/* Returns an printable name for the specified forwarding permission list */
465static const char *
466fwd_ident(int who, int where)
467{
468 if (who == FORWARD_ADM) {
469 if (where == FORWARD_LOCAL)
470 return "admin local";
471 else if (where == FORWARD_REMOTE)
472 return "admin remote";
473 } else if (who == FORWARD_USER) {
474 if (where == FORWARD_LOCAL)
475 return "user local";
476 else if (where == FORWARD_REMOTE)
477 return "user remote";
478 }
479 fatal("Unknown forward permission list %d/%d", who, where);
480}
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000481
djm@openbsd.org115063a2018-06-06 18:22:41 +0000482/* Returns the forwarding permission list for the specified direction */
483static struct permission_set *
484permission_set_get(struct ssh *ssh, int where)
485{
486 struct ssh_channels *sc = ssh->chanctxt;
487
488 switch (where) {
489 case FORWARD_LOCAL:
490 return &sc->local_perms;
491 break;
492 case FORWARD_REMOTE:
493 return &sc->remote_perms;
494 break;
495 default:
496 fatal("%s: invalid forwarding direction %d", __func__, where);
497 }
498}
499
500/* Reutrns pointers to the specified forwarding list and its element count */
501static void
502permission_set_get_array(struct ssh *ssh, int who, int where,
503 struct permission ***permpp, u_int **npermpp)
504{
505 struct permission_set *pset = permission_set_get(ssh, where);
506
507 switch (who) {
508 case FORWARD_USER:
509 *permpp = &pset->permitted_user;
510 *npermpp = &pset->num_permitted_user;
511 break;
512 case FORWARD_ADM:
513 *permpp = &pset->permitted_admin;
514 *npermpp = &pset->num_permitted_admin;
515 break;
516 default:
517 fatal("%s: invalid forwarding client %d", __func__, who);
518 }
519}
520
521/* Adds an entry to the spcified forwarding list */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000522static int
djm@openbsd.org115063a2018-06-06 18:22:41 +0000523permission_set_add(struct ssh *ssh, int who, int where,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000524 const char *host_to_connect, int port_to_connect,
525 const char *listen_host, const char *listen_path, int listen_port,
526 Channel *downstream)
527{
djm@openbsd.org115063a2018-06-06 18:22:41 +0000528 struct permission **permp;
529 u_int n, *npermp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000530
djm@openbsd.org115063a2018-06-06 18:22:41 +0000531 permission_set_get_array(ssh, who, where, &permp, &npermp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000532
djm@openbsd.org115063a2018-06-06 18:22:41 +0000533 if (*npermp >= INT_MAX)
534 fatal("%s: %s overflow", __func__, fwd_ident(who, where));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000535
djm@openbsd.org115063a2018-06-06 18:22:41 +0000536 *permp = xrecallocarray(*permp, *npermp, *npermp + 1, sizeof(**permp));
537 n = (*npermp)++;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000538#define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
djm@openbsd.org115063a2018-06-06 18:22:41 +0000539 (*permp)[n].host_to_connect = MAYBE_DUP(host_to_connect);
540 (*permp)[n].port_to_connect = port_to_connect;
541 (*permp)[n].listen_host = MAYBE_DUP(listen_host);
542 (*permp)[n].listen_path = MAYBE_DUP(listen_path);
543 (*permp)[n].listen_port = listen_port;
544 (*permp)[n].downstream = downstream;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000545#undef MAYBE_DUP
546 return (int)n;
547}
548
549static void
550mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
551{
552 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +0000553 struct permission_set *pset = &sc->local_perms;
554 struct permission *perm;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000555 int r;
556 u_int i;
557
djm@openbsd.org115063a2018-06-06 18:22:41 +0000558 for (i = 0; i < pset->num_permitted_user; i++) {
559 perm = &pset->permitted_user[i];
560 if (perm->downstream != c)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000561 continue;
562
563 /* cancel on the server, since mux client is gone */
564 debug("channel %d: cleanup remote forward for %s:%u",
djm@openbsd.org115063a2018-06-06 18:22:41 +0000565 c->self, perm->listen_host, perm->listen_port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000566 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
567 (r = sshpkt_put_cstring(ssh,
568 "cancel-tcpip-forward")) != 0 ||
569 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
570 (r = sshpkt_put_cstring(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +0000571 channel_rfwd_bind_host(perm->listen_host))) != 0 ||
572 (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000573 (r = sshpkt_send(ssh)) != 0) {
574 fatal("%s: channel %i: %s", __func__,
575 c->self, ssh_err(r));
576 }
djm@openbsd.org115063a2018-06-06 18:22:41 +0000577 fwd_perm_clear(perm); /* unregister */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000578 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000579}
580
581/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000582void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000583channel_free(struct ssh *ssh, Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000584{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000585 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000586 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000587 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000588 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000589 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000590
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000591 for (n = 0, i = 0; i < sc->channels_alloc; i++) {
592 if ((other = sc->channels[i]) == NULL)
593 continue;
594 n++;
595 /* detach from mux client and prepare for closing */
596 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
597 other->type == SSH_CHANNEL_MUX_PROXY &&
598 other->mux_ctx == c) {
599 other->mux_ctx = NULL;
600 other->type = SSH_CHANNEL_OPEN;
601 other->istate = CHAN_INPUT_CLOSED;
602 other->ostate = CHAN_OUTPUT_CLOSED;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000603 }
604 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000605 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000606 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000607
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000608 if (c->type == SSH_CHANNEL_MUX_CLIENT)
609 mux_remove_remote_forwardings(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000610
markus@openbsd.org5d140192018-07-27 12:03:17 +0000611 if (log_level_get() >= SYSLOG_LEVEL_DEBUG3) {
612 s = channel_open_message(ssh);
613 debug3("channel %d: status: %s", c->self, s);
614 free(s);
615 }
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000616
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000617 channel_close_fds(ssh, c);
618 sshbuf_free(c->input);
619 sshbuf_free(c->output);
620 sshbuf_free(c->extended);
621 c->input = c->output = c->extended = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000622 free(c->remote_name);
623 c->remote_name = NULL;
624 free(c->path);
625 c->path = NULL;
626 free(c->listening_addr);
627 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000628 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
629 if (cc->abandon_cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000630 cc->abandon_cb(ssh, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +1000631 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100632 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000633 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000634 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000635 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000636 c->filter_cleanup(ssh, c->self, c->filter_ctx);
637 sc->channels[c->self] = NULL;
millert@openbsd.org3e8d1852017-09-19 12:10:30 +0000638 explicit_bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +1000639 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000640}
641
Ben Lindstrome9c99912001-06-09 00:41:05 +0000642void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000643channel_free_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000645 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000647 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
648 if (ssh->chanctxt->channels[i] != NULL)
649 channel_free(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650}
651
652/*
653 * Closes the sockets/fds of all channels. This is used to close extra file
654 * descriptors after a fork.
655 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000656void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000657channel_close_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000658{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000659 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000660
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000661 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
662 if (ssh->chanctxt->channels[i] != NULL)
663 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000664}
665
666/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000667 * Stop listening to channels.
668 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000669void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000670channel_stop_listening(struct ssh *ssh)
Ben Lindstrom809744e2001-07-04 05:26:06 +0000671{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000672 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000673 Channel *c;
674
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000675 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
676 c = ssh->chanctxt->channels[i];
Ben Lindstrom809744e2001-07-04 05:26:06 +0000677 if (c != NULL) {
678 switch (c->type) {
679 case SSH_CHANNEL_AUTH_SOCKET:
680 case SSH_CHANNEL_PORT_LISTENER:
681 case SSH_CHANNEL_RPORT_LISTENER:
682 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000683 case SSH_CHANNEL_UNIX_LISTENER:
684 case SSH_CHANNEL_RUNIX_LISTENER:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000685 channel_close_fd(ssh, &c->sock);
686 channel_free(ssh, c);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000687 break;
688 }
689 }
690 }
691}
692
693/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 * Returns true if no channel has too much buffered data, and false if one or
695 * more channel is overfull.
696 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000697int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000698channel_not_very_much_buffered_data(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000699{
700 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000701 u_int maxsize = ssh_packet_get_maxsize(ssh);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000702 Channel *c;
703
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000704 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
705 c = ssh->chanctxt->channels[i];
706 if (c == NULL || c->type != SSH_CHANNEL_OPEN)
707 continue;
708 if (sshbuf_len(c->output) > maxsize) {
709 debug2("channel %d: big output buffer %zu > %u",
710 c->self, sshbuf_len(c->output), maxsize);
711 return 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000712 }
713 }
714 return 1;
715}
716
717/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000718int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000719channel_still_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000720{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000721 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000722 Channel *c;
723
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000724 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
725 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000726 if (c == NULL)
727 continue;
728 switch (c->type) {
729 case SSH_CHANNEL_X11_LISTENER:
730 case SSH_CHANNEL_PORT_LISTENER:
731 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100732 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000733 case SSH_CHANNEL_CLOSED:
734 case SSH_CHANNEL_AUTH_SOCKET:
735 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000736 case SSH_CHANNEL_RDYNAMIC_OPEN:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000737 case SSH_CHANNEL_CONNECTING:
738 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000739 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000740 case SSH_CHANNEL_UNIX_LISTENER:
741 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000742 continue;
743 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744 continue;
745 case SSH_CHANNEL_OPENING:
746 case SSH_CHANNEL_OPEN:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000747 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000748 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100749 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000750 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000751 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000752 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000753 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000754 /* NOTREACHED */
755 }
756 }
757 return 0;
758}
759
760/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000761int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000762channel_find_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000763{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000764 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000765 Channel *c;
766
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000767 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
768 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000769 if (c == NULL || !c->have_remote_id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000770 continue;
771 switch (c->type) {
772 case SSH_CHANNEL_CLOSED:
773 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000774 case SSH_CHANNEL_RDYNAMIC_OPEN:
775 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000776 case SSH_CHANNEL_X11_LISTENER:
777 case SSH_CHANNEL_PORT_LISTENER:
778 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100779 case SSH_CHANNEL_MUX_LISTENER:
780 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000781 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000782 case SSH_CHANNEL_OPENING:
783 case SSH_CHANNEL_CONNECTING:
784 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000785 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000786 case SSH_CHANNEL_UNIX_LISTENER:
787 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000788 continue;
789 case SSH_CHANNEL_LARVAL:
790 case SSH_CHANNEL_AUTH_SOCKET:
791 case SSH_CHANNEL_OPEN:
792 case SSH_CHANNEL_X11_OPEN:
793 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000794 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000795 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796 /* NOTREACHED */
797 }
798 }
799 return -1;
800}
801
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +0000802/* Returns the state of the channel's extended usage flag */
803const char *
804channel_format_extended_usage(const Channel *c)
805{
806 if (c->efd == -1)
807 return "closed";
808
809 switch (c->extended_usage) {
810 case CHAN_EXTENDED_WRITE:
811 return "write";
812 case CHAN_EXTENDED_READ:
813 return "read";
814 case CHAN_EXTENDED_IGNORE:
815 return "ignore";
816 default:
817 return "UNKNOWN";
818 }
819}
820
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000821static char *
822channel_format_status(const Channel *c)
823{
824 char *ret = NULL;
825
826 xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu "
827 "fd %d/%d/%d sock %d cc %d",
828 c->type,
829 c->have_remote_id ? "r" : "nr", c->remote_id,
830 c->istate, sshbuf_len(c->input),
831 c->ostate, sshbuf_len(c->output),
832 channel_format_extended_usage(c), sshbuf_len(c->extended),
833 c->rfd, c->wfd, c->efd, c->sock, c->ctl_chan);
834 return ret;
835}
836
Ben Lindstrome9c99912001-06-09 00:41:05 +0000837/*
838 * Returns a message describing the currently open forwarded connections,
839 * suitable for sending to the client. The message contains crlf pairs for
840 * newlines.
841 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000842char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000843channel_open_message(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000844{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000845 struct sshbuf *buf;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000846 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000847 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000848 int r;
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000849 char *cp, *ret;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000850
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000851 if ((buf = sshbuf_new()) == NULL)
852 fatal("%s: sshbuf_new", __func__);
853 if ((r = sshbuf_putf(buf,
854 "The following connections are open:\r\n")) != 0)
855 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
856 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
857 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000858 if (c == NULL)
859 continue;
860 switch (c->type) {
861 case SSH_CHANNEL_X11_LISTENER:
862 case SSH_CHANNEL_PORT_LISTENER:
863 case SSH_CHANNEL_RPORT_LISTENER:
864 case SSH_CHANNEL_CLOSED:
865 case SSH_CHANNEL_AUTH_SOCKET:
866 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000867 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100868 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000869 case SSH_CHANNEL_UNIX_LISTENER:
870 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000871 continue;
872 case SSH_CHANNEL_LARVAL:
873 case SSH_CHANNEL_OPENING:
874 case SSH_CHANNEL_CONNECTING:
875 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000876 case SSH_CHANNEL_RDYNAMIC_OPEN:
877 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000878 case SSH_CHANNEL_OPEN:
879 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000880 case SSH_CHANNEL_MUX_PROXY:
881 case SSH_CHANNEL_MUX_CLIENT:
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000882 cp = channel_format_status(c);
883 if ((r = sshbuf_putf(buf, " #%d %.300s (%s)\r\n",
884 c->self, c->remote_name, cp)) != 0) {
885 free(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000886 fatal("%s: sshbuf_putf: %s",
887 __func__, ssh_err(r));
djm@openbsd.org6f1aabb2018-10-04 01:04:52 +0000888 }
889 free(cp);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000890 continue;
891 default:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000892 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000893 /* NOTREACHED */
894 }
895 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000896 if ((ret = sshbuf_dup_string(buf)) == NULL)
897 fatal("%s: sshbuf_dup_string", __func__);
898 sshbuf_free(buf);
899 return ret;
900}
901
902static void
903open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
904{
905 int r;
906
907 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
908 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
909 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
910 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
911 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
912 fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r));
913 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000914}
915
916void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000917channel_send_open(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000918{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000919 Channel *c = channel_lookup(ssh, id);
920 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000921
Ben Lindstrome9c99912001-06-09 00:41:05 +0000922 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000923 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000924 return;
925 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000926 debug2("channel %d: send open", id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000927 open_preamble(ssh, __func__, c, c->ctype);
928 if ((r = sshpkt_send(ssh)) != 0)
929 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000930}
931
932void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000933channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000934{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000935 Channel *c = channel_lookup(ssh, id);
936 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000937
Ben Lindstrome9c99912001-06-09 00:41:05 +0000938 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000939 logit("%s: %d: unknown channel id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000940 return;
941 }
djm@openbsd.org9f532292017-09-12 06:35:31 +0000942 if (!c->have_remote_id)
943 fatal(":%s: channel %d: no remote id", __func__, c->self);
944
Damien Miller0e220db2004-06-15 10:34:08 +1000945 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000946 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
947 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
948 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
949 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
950 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
951 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000952}
Damien Miller4f7becb2006-03-26 14:10:14 +1100953
Ben Lindstrome9c99912001-06-09 00:41:05 +0000954void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000955channel_register_status_confirm(struct ssh *ssh, int id,
956 channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
Damien Millerb84886b2008-05-19 15:05:07 +1000957{
958 struct channel_confirm *cc;
959 Channel *c;
960
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000961 if ((c = channel_lookup(ssh, id)) == NULL)
962 fatal("%s: %d: bad id", __func__, id);
Damien Millerb84886b2008-05-19 15:05:07 +1000963
Damien Miller6c81fee2013-11-08 12:19:55 +1100964 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000965 cc->cb = cb;
966 cc->abandon_cb = abandon_cb;
967 cc->ctx = ctx;
968 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
969}
970
971void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000972channel_register_open_confirm(struct ssh *ssh, int id,
973 channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000974{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000975 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000976
Ben Lindstrome9c99912001-06-09 00:41:05 +0000977 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000978 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000979 return;
980 }
Damien Millerb84886b2008-05-19 15:05:07 +1000981 c->open_confirm = fn;
982 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000983}
Damien Miller4f7becb2006-03-26 14:10:14 +1100984
Ben Lindstrome9c99912001-06-09 00:41:05 +0000985void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000986channel_register_cleanup(struct ssh *ssh, int id,
987 channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000988{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000989 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000990
Ben Lindstrome9c99912001-06-09 00:41:05 +0000991 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000992 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000993 return;
994 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000995 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100996 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000997}
Damien Miller4f7becb2006-03-26 14:10:14 +1100998
Ben Lindstrome9c99912001-06-09 00:41:05 +0000999void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001000channel_cancel_cleanup(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001001{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001002 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001003
Ben Lindstrome9c99912001-06-09 00:41:05 +00001004 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001005 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001006 return;
1007 }
Ben Lindstrom809744e2001-07-04 05:26:06 +00001008 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +11001009 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001010}
Damien Miller4f7becb2006-03-26 14:10:14 +11001011
Ben Lindstrome9c99912001-06-09 00:41:05 +00001012void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001013channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +10001014 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001015{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001016 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001017
Ben Lindstrome9c99912001-06-09 00:41:05 +00001018 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001019 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001020 return;
1021 }
Damien Miller077b2382005-12-31 16:22:32 +11001022 c->input_filter = ifn;
1023 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +10001024 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +10001025 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001026}
1027
1028void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001029channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +10001030 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +00001031{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001032 Channel *c = channel_lookup(ssh, id);
1033 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001034
Ben Lindstrome9c99912001-06-09 00:41:05 +00001035 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
1036 fatal("channel_activate for non-larval channel %d.", id);
djm@openbsd.org9f532292017-09-12 06:35:31 +00001037 if (!c->have_remote_id)
1038 fatal(":%s: channel %d: no remote id", __func__, c->self);
1039
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001040 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001041 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +11001042 c->local_window = c->local_window_max = window_max;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001043
1044 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1045 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1046 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1047 (r = sshpkt_send(ssh)) != 0)
1048 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001049}
1050
Ben Lindstrombba81212001-06-25 05:01:22 +00001051static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001052channel_pre_listener(struct ssh *ssh, Channel *c,
1053 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001054{
1055 FD_SET(c->sock, readset);
1056}
1057
Ben Lindstrombba81212001-06-25 05:01:22 +00001058static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001059channel_pre_connecting(struct ssh *ssh, Channel *c,
1060 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001061{
1062 debug3("channel %d: waiting for connection", c->self);
1063 FD_SET(c->sock, writeset);
1064}
1065
Ben Lindstrombba81212001-06-25 05:01:22 +00001066static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001067channel_pre_open(struct ssh *ssh, Channel *c,
1068 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001069{
Damien Miller33b13562000-04-04 14:38:59 +10001070 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001071 c->remote_window > 0 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001072 sshbuf_len(c->input) < c->remote_window &&
1073 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Miller33b13562000-04-04 14:38:59 +10001074 FD_SET(c->rfd, readset);
1075 if (c->ostate == CHAN_OUTPUT_OPEN ||
1076 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001077 if (sshbuf_len(c->output) > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001078 FD_SET(c->wfd, writeset);
1079 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +00001080 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001081 debug2("channel %d: "
1082 "obuf_empty delayed efd %d/(%zu)", c->self,
1083 c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001084 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001085 chan_obuf_empty(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10001086 }
1087 }
1088 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001089 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
1090 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +10001091 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001092 sshbuf_len(c->extended) > 0)
Damien Miller33b13562000-04-04 14:38:59 +10001093 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +10001094 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1095 (c->extended_usage == CHAN_EXTENDED_READ ||
1096 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001097 sshbuf_len(c->extended) < c->remote_window)
Damien Miller33b13562000-04-04 14:38:59 +10001098 FD_SET(c->efd, readset);
1099 }
Damien Miller0e220db2004-06-15 10:34:08 +10001100 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +10001101}
1102
Damien Millerb38eff82000-04-01 11:09:21 +10001103/*
1104 * This is a special state for X11 authentication spoofing. An opened X11
1105 * connection (when authentication spoofing is being done) remains in this
1106 * state until the first packet has been completely read. The authentication
1107 * data in that packet is then substituted by the real data if it matches the
1108 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +10001109 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +00001110 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +10001111 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001113x11_open_helper(struct ssh *ssh, struct sshbuf *b)
Damien Millerb38eff82000-04-01 11:09:21 +10001114{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001115 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001116 u_char *ucp;
1117 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001118
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001119 /* Is this being called after the refusal deadline? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001120 if (sc->x11_refuse_time != 0 &&
1121 (u_int)monotime() >= sc->x11_refuse_time) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001122 verbose("Rejected X11 connection after ForwardX11Timeout "
1123 "expired");
1124 return -1;
1125 }
1126
Damien Millerb38eff82000-04-01 11:09:21 +10001127 /* Check if the fixed size part of the packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001128 if (sshbuf_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +10001129 return 0;
1130
1131 /* Parse the lengths of variable-length fields. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001132 ucp = sshbuf_mutable_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +10001133 if (ucp[0] == 0x42) { /* Byte order MSB first. */
1134 proto_len = 256 * ucp[6] + ucp[7];
1135 data_len = 256 * ucp[8] + ucp[9];
1136 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1137 proto_len = ucp[6] + 256 * ucp[7];
1138 data_len = ucp[8] + 256 * ucp[9];
1139 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001140 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001141 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001142 return -1;
1143 }
1144
1145 /* Check if the whole packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001146 if (sshbuf_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001147 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1148 return 0;
1149
1150 /* Check if authentication protocol matches. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001151 if (proto_len != strlen(sc->x11_saved_proto) ||
1152 memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001153 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001154 return -1;
1155 }
1156 /* Check if authentication data matches our fake data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001157 if (data_len != sc->x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001158 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001159 sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001160 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001161 return -1;
1162 }
1163 /* Check fake data length */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001164 if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
Damien Millerb38eff82000-04-01 11:09:21 +10001165 error("X11 fake_data_len %d != saved_data_len %d",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001166 sc->x11_fake_data_len, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001167 return -1;
1168 }
1169 /*
1170 * Received authentication protocol and data match
1171 * our fake data. Substitute the fake data with real
1172 * data.
1173 */
1174 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001175 sc->x11_saved_data, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001176 return 1;
1177}
1178
Ben Lindstrombba81212001-06-25 05:01:22 +00001179static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001180channel_pre_x11_open(struct ssh *ssh, Channel *c,
1181 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001182{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001183 int ret = x11_open_helper(ssh, c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001184
1185 /* c->force_drain = 1; */
1186
Damien Millerb38eff82000-04-01 11:09:21 +10001187 if (ret == 1) {
1188 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001189 channel_pre_open(ssh, c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001190 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001191 logit("X11 connection rejected because of wrong authentication.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001192 debug2("X11 rejected %d i%d/o%d",
1193 c->self, c->istate, c->ostate);
1194 chan_read_failed(ssh, c);
1195 sshbuf_reset(c->input);
1196 chan_ibuf_empty(ssh, c);
1197 sshbuf_reset(c->output);
1198 chan_write_failed(ssh, c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001199 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001200 }
1201}
1202
Damien Millere1537f92010-01-26 13:26:22 +11001203static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001204channel_pre_mux_client(struct ssh *ssh,
1205 Channel *c, fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11001206{
Damien Millerd530f5f2010-05-21 14:57:10 +10001207 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001208 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Millere1537f92010-01-26 13:26:22 +11001209 FD_SET(c->rfd, readset);
1210 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1211 /* clear buffer immediately (discard any partial packet) */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001212 sshbuf_reset(c->input);
1213 chan_ibuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001214 /* Start output drain. XXX just kill chan? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001215 chan_rcvd_oclose(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001216 }
1217 if (c->ostate == CHAN_OUTPUT_OPEN ||
1218 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001219 if (sshbuf_len(c->output) > 0)
Damien Millere1537f92010-01-26 13:26:22 +11001220 FD_SET(c->wfd, writeset);
1221 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001222 chan_obuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001223 }
1224}
1225
Ben Lindstromb3921512001-04-11 15:57:50 +00001226/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +00001227static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001228channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001229{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001230 const u_char *p;
1231 char *host;
Damien Miller1781f532009-01-28 16:24:41 +11001232 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001233 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001234 struct {
1235 u_int8_t version;
1236 u_int8_t command;
1237 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001238 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001239 } s4_req, s4_rsp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001240 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001241
Ben Lindstromb3921512001-04-11 15:57:50 +00001242 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001243
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001244 have = sshbuf_len(input);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001245 len = sizeof(s4_req);
1246 if (have < len)
1247 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001248 p = sshbuf_ptr(input);
Damien Miller1781f532009-01-28 16:24:41 +11001249
1250 need = 1;
1251 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1252 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1253 debug2("channel %d: socks4a request", c->self);
1254 /* ... and needs an extra string (the hostname) */
1255 need = 2;
1256 }
1257 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001258 for (found = 0, i = len; i < have; i++) {
1259 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001260 found++;
1261 if (found == need)
1262 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001263 }
1264 if (i > 1024) {
1265 /* the peer is probably sending garbage */
1266 debug("channel %d: decode socks4: too long",
1267 c->self);
1268 return -1;
1269 }
1270 }
Damien Miller1781f532009-01-28 16:24:41 +11001271 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001272 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001273 if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1274 (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1275 (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1276 (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001277 debug("channels %d: decode socks4: %s", c->self, ssh_err(r));
1278 return -1;
1279 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001280 have = sshbuf_len(input);
1281 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001282 if (memchr(p, '\0', have) == NULL) {
1283 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001284 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001285 return -1;
1286 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001287 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001288 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001289 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001290 strlcpy(username, p, sizeof(username));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001291 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001292 fatal("%s: channel %d: consume: %s", __func__,
1293 c->self, ssh_err(r));
1294 }
Darren Tuckera627d422013-06-02 07:31:17 +10001295 free(c->path);
1296 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001297 if (need == 1) { /* SOCKS4: one string */
1298 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001299 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001300 } else { /* SOCKS4A: two strings */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001301 have = sshbuf_len(input);
1302 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001303 if (memchr(p, '\0', have) == NULL) {
1304 error("channel %d: decode socks4a: host not nul "
1305 "terminated", c->self);
1306 return -1;
1307 }
Damien Miller1781f532009-01-28 16:24:41 +11001308 len = strlen(p);
1309 debug2("channel %d: decode socks4a: host %s/%d",
1310 c->self, p, len);
1311 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001312 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001313 error("channel %d: hostname \"%.100s\" too long",
1314 c->self, p);
1315 return -1;
1316 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001317 c->path = xstrdup(p);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001318 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001319 fatal("%s: channel %d: consume: %s", __func__,
1320 c->self, ssh_err(r));
1321 }
Damien Miller1781f532009-01-28 16:24:41 +11001322 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001323 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001324
Damien Millerfbdeece2003-09-02 22:52:31 +10001325 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001326 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001327
Ben Lindstromb3921512001-04-11 15:57:50 +00001328 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001329 debug("channel %d: cannot handle: %s cn %d",
1330 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001331 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001332 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001333 s4_rsp.version = 0; /* vn: 0 for reply */
1334 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001335 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001336 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001337 if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001338 fatal("%s: channel %d: append reply: %s", __func__,
1339 c->self, ssh_err(r));
1340 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001341 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001342}
1343
Darren Tucker46471c92003-07-03 13:55:19 +10001344/* try to decode a socks5 header */
1345#define SSH_SOCKS5_AUTHDONE 0x1000
1346#define SSH_SOCKS5_NOAUTH 0x00
1347#define SSH_SOCKS5_IPV4 0x01
1348#define SSH_SOCKS5_DOMAIN 0x03
1349#define SSH_SOCKS5_IPV6 0x04
1350#define SSH_SOCKS5_CONNECT 0x01
1351#define SSH_SOCKS5_SUCCESS 0x00
1352
1353static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001354channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
Darren Tucker46471c92003-07-03 13:55:19 +10001355{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001356 /* XXX use get/put_u8 instead of trusting struct padding */
Darren Tucker46471c92003-07-03 13:55:19 +10001357 struct {
1358 u_int8_t version;
1359 u_int8_t command;
1360 u_int8_t reserved;
1361 u_int8_t atyp;
1362 } s5_req, s5_rsp;
1363 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001364 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001365 const u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001366 u_int have, need, i, found, nmethods, addrlen, af;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001367 int r;
Darren Tucker46471c92003-07-03 13:55:19 +10001368
1369 debug2("channel %d: decode socks5", c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001370 p = sshbuf_ptr(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001371 if (p[0] != 0x05)
1372 return -1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001373 have = sshbuf_len(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001374 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1375 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001376 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001377 return 0;
1378 nmethods = p[1];
1379 if (have < nmethods + 2)
1380 return 0;
1381 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001382 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001383 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001384 found = 1;
1385 break;
1386 }
1387 }
1388 if (!found) {
1389 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1390 c->self);
1391 return -1;
1392 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001393 if ((r = sshbuf_consume(input, nmethods + 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001394 fatal("%s: channel %d: consume: %s", __func__,
1395 c->self, ssh_err(r));
1396 }
1397 /* version, method */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001398 if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1399 (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001400 fatal("%s: channel %d: append reply: %s", __func__,
1401 c->self, ssh_err(r));
1402 }
Darren Tucker46471c92003-07-03 13:55:19 +10001403 c->flags |= SSH_SOCKS5_AUTHDONE;
1404 debug2("channel %d: socks5 auth done", c->self);
1405 return 0; /* need more */
1406 }
1407 debug2("channel %d: socks5 post auth", c->self);
1408 if (have < sizeof(s5_req)+1)
1409 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001410 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001411 if (s5_req.version != 0x05 ||
1412 s5_req.command != SSH_SOCKS5_CONNECT ||
1413 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001414 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001415 return -1;
1416 }
Darren Tucker47eede72005-03-14 23:08:12 +11001417 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001418 case SSH_SOCKS5_IPV4:
1419 addrlen = 4;
1420 af = AF_INET;
1421 break;
1422 case SSH_SOCKS5_DOMAIN:
1423 addrlen = p[sizeof(s5_req)];
1424 af = -1;
1425 break;
1426 case SSH_SOCKS5_IPV6:
1427 addrlen = 16;
1428 af = AF_INET6;
1429 break;
1430 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001431 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001432 return -1;
1433 }
Damien Miller0f077072006-07-10 22:21:02 +10001434 need = sizeof(s5_req) + addrlen + 2;
1435 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1436 need++;
1437 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001438 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001439 if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001440 fatal("%s: channel %d: consume: %s", __func__,
1441 c->self, ssh_err(r));
1442 }
1443 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1444 /* host string length */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001445 if ((r = sshbuf_consume(input, 1)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001446 fatal("%s: channel %d: consume: %s", __func__,
1447 c->self, ssh_err(r));
1448 }
1449 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001450 if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1451 (r = sshbuf_get(input, &dest_port, 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001452 debug("channel %d: parse addr/port: %s", c->self, ssh_err(r));
1453 return -1;
1454 }
Darren Tucker46471c92003-07-03 13:55:19 +10001455 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001456 free(c->path);
1457 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001458 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001459 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001460 error("channel %d: dynamic request: socks5 hostname "
1461 "\"%.100s\" too long", c->self, dest_addr);
1462 return -1;
1463 }
1464 c->path = xstrdup(dest_addr);
1465 } else {
1466 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1467 return -1;
1468 c->path = xstrdup(ntop);
1469 }
Darren Tucker46471c92003-07-03 13:55:19 +10001470 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001471
Damien Millerfbdeece2003-09-02 22:52:31 +10001472 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001473 c->self, c->path, c->host_port, s5_req.command);
1474
1475 s5_rsp.version = 0x05;
1476 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1477 s5_rsp.reserved = 0; /* ignored */
1478 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001479 dest_port = 0; /* ignored */
1480
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001481 if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1482 (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1483 (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001484 fatal("%s: channel %d: append reply: %s", __func__,
1485 c->self, ssh_err(r));
Darren Tucker46471c92003-07-03 13:55:19 +10001486 return 1;
1487}
1488
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001489Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001490channel_connect_stdio_fwd(struct ssh *ssh,
1491 const char *host_to_connect, u_short port_to_connect, int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001492{
1493 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001494
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001495 debug("%s %s:%d", __func__, host_to_connect, port_to_connect);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001496
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001497 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001498 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1499 0, "stdio-forward", /*nonblock*/0);
1500
1501 c->path = xstrdup(host_to_connect);
1502 c->host_port = port_to_connect;
1503 c->listening_port = 0;
1504 c->force_drain = 1;
1505
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001506 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1507 port_open_helper(ssh, c, "direct-tcpip");
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001508
1509 return c;
1510}
1511
Ben Lindstromb3921512001-04-11 15:57:50 +00001512/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001513static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001514channel_pre_dynamic(struct ssh *ssh, Channel *c,
1515 fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001516{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001517 const u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001518 u_int have;
1519 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001520
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001521 have = sshbuf_len(c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001522 debug2("channel %d: pre_dynamic: have %d", c->self, have);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001523 /* sshbuf_dump(c->input, stderr); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001524 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001525 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001526 /* need more */
1527 FD_SET(c->sock, readset);
1528 return;
1529 }
1530 /* try to guess the protocol */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001531 p = sshbuf_ptr(c->input);
1532 /* XXX sshbuf_peek_u8? */
Ben Lindstromb3921512001-04-11 15:57:50 +00001533 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001534 case 0x04:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001535 ret = channel_decode_socks4(c, c->input, c->output);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001536 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001537 case 0x05:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001538 ret = channel_decode_socks5(c, c->input, c->output);
Darren Tucker46471c92003-07-03 13:55:19 +10001539 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001540 default:
1541 ret = -1;
1542 break;
1543 }
1544 if (ret < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001545 chan_mark_dead(ssh, c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001546 } else if (ret == 0) {
1547 debug2("channel %d: pre_dynamic: need more", c->self);
1548 /* need more */
1549 FD_SET(c->sock, readset);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001550 if (sshbuf_len(c->output))
1551 FD_SET(c->sock, writeset);
Ben Lindstromb3921512001-04-11 15:57:50 +00001552 } else {
1553 /* switch to the next state */
1554 c->type = SSH_CHANNEL_OPENING;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001555 port_open_helper(ssh, c, "direct-tcpip");
Ben Lindstromb3921512001-04-11 15:57:50 +00001556 }
1557}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001558
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001559/* simulate read-error */
1560static void
1561rdynamic_close(struct ssh *ssh, Channel *c)
1562{
1563 c->type = SSH_CHANNEL_OPEN;
1564 chan_read_failed(ssh, c);
1565 sshbuf_reset(c->input);
1566 chan_ibuf_empty(ssh, c);
1567 sshbuf_reset(c->output);
1568 chan_write_failed(ssh, c);
1569}
1570
1571/* reverse dynamic port forwarding */
1572static void
1573channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c)
1574{
1575 const u_char *p;
1576 u_int have, len;
1577 int r, ret;
1578
1579 have = sshbuf_len(c->output);
1580 debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1581 /* sshbuf_dump(c->output, stderr); */
1582 /* EOF received */
1583 if (c->flags & CHAN_EOF_RCVD) {
1584 if ((r = sshbuf_consume(c->output, have)) != 0) {
1585 fatal("%s: channel %d: consume: %s",
1586 __func__, c->self, ssh_err(r));
1587 }
1588 rdynamic_close(ssh, c);
1589 return;
1590 }
1591 /* check if the fixed size part of the packet is in buffer. */
1592 if (have < 3)
1593 return;
1594 /* try to guess the protocol */
1595 p = sshbuf_ptr(c->output);
1596 switch (p[0]) {
1597 case 0x04:
1598 /* switch input/output for reverse forwarding */
1599 ret = channel_decode_socks4(c, c->output, c->input);
1600 break;
1601 case 0x05:
1602 ret = channel_decode_socks5(c, c->output, c->input);
1603 break;
1604 default:
1605 ret = -1;
1606 break;
1607 }
1608 if (ret < 0) {
1609 rdynamic_close(ssh, c);
1610 } else if (ret == 0) {
1611 debug2("channel %d: pre_rdynamic: need more", c->self);
1612 /* send socks request to peer */
1613 len = sshbuf_len(c->input);
1614 if (len > 0 && len < c->remote_window) {
1615 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1616 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1617 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1618 (r = sshpkt_send(ssh)) != 0) {
1619 fatal("%s: channel %i: rdynamic: %s", __func__,
1620 c->self, ssh_err(r));
1621 }
1622 if ((r = sshbuf_consume(c->input, len)) != 0) {
1623 fatal("%s: channel %d: consume: %s",
1624 __func__, c->self, ssh_err(r));
1625 }
1626 c->remote_window -= len;
1627 }
1628 } else if (rdynamic_connect_finish(ssh, c) < 0) {
1629 /* the connect failed */
1630 rdynamic_close(ssh, c);
1631 }
1632}
1633
Damien Millerb38eff82000-04-01 11:09:21 +10001634/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001635static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001636channel_post_x11_listener(struct ssh *ssh, Channel *c,
1637 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001638{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001639 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001640 struct sockaddr_storage addr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001641 int r, newsock, oerrno, remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001642 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001643 char buf[16384], *remote_ipaddr;
Damien Millerb38eff82000-04-01 11:09:21 +10001644
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001645 if (!FD_ISSET(c->sock, readset))
1646 return;
Damien Millerbd483e72000-04-30 10:00:53 +10001647
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001648 debug("X11 connection requested.");
1649 addrlen = sizeof(addr);
1650 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1651 if (c->single_connection) {
1652 oerrno = errno;
1653 debug2("single_connection: closing X11 listener.");
1654 channel_close_fd(ssh, &c->sock);
1655 chan_mark_dead(ssh, c);
1656 errno = oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001657 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001658 if (newsock < 0) {
1659 if (errno != EINTR && errno != EWOULDBLOCK &&
1660 errno != ECONNABORTED)
1661 error("accept: %.100s", strerror(errno));
1662 if (errno == EMFILE || errno == ENFILE)
1663 c->notbefore = monotime() + 1;
1664 return;
1665 }
1666 set_nodelay(newsock);
1667 remote_ipaddr = get_peer_ipaddr(newsock);
1668 remote_port = get_peer_port(newsock);
1669 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1670 remote_ipaddr, remote_port);
1671
1672 nc = channel_new(ssh, "accepted x11 socket",
1673 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1674 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1675 open_preamble(ssh, __func__, nc, "x11");
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001676 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1677 (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001678 fatal("%s: channel %i: reply %s", __func__,
1679 c->self, ssh_err(r));
1680 }
1681 if ((r = sshpkt_send(ssh)) != 0)
1682 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
1683 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001684}
1685
Ben Lindstrombba81212001-06-25 05:01:22 +00001686static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001687port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001688{
Damien Miller4def1842013-12-29 17:45:26 +11001689 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001690 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001691 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001692 int remote_port = get_peer_port(c->sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001693 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001694
Damien Millerd6369432010-02-02 17:02:07 +11001695 if (remote_port == -1) {
1696 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001697 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001698 remote_ipaddr = xstrdup("127.0.0.1");
1699 remote_port = 65535;
1700 }
1701
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001702 free(c->remote_name);
1703 xasprintf(&c->remote_name,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001704 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001705 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001706 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001707 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001708
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001709 open_preamble(ssh, __func__, c, rtype);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001710 if (strcmp(rtype, "direct-tcpip") == 0) {
1711 /* target host, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001712 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1713 (r = sshpkt_put_u32(ssh, c->host_port)) != 0) {
1714 fatal("%s: channel %i: reply %s", __func__,
1715 c->self, ssh_err(r));
1716 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001717 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1718 /* target path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001719 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1720 fatal("%s: channel %i: reply %s", __func__,
1721 c->self, ssh_err(r));
1722 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001723 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1724 /* listen path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001725 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1726 fatal("%s: channel %i: reply %s", __func__,
1727 c->self, ssh_err(r));
1728 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001729 } else {
1730 /* listen address, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001731 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1732 (r = sshpkt_put_u32(ssh, local_port)) != 0) {
1733 fatal("%s: channel %i: reply %s", __func__,
1734 c->self, ssh_err(r));
1735 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001736 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001737 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1738 /* reserved for future owner/mode info */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001739 if ((r = sshpkt_put_cstring(ssh, "")) != 0) {
1740 fatal("%s: channel %i: reply %s", __func__,
1741 c->self, ssh_err(r));
1742 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001743 } else {
1744 /* originator host and port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001745 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1746 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) {
1747 fatal("%s: channel %i: reply %s", __func__,
1748 c->self, ssh_err(r));
1749 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001750 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001751 if ((r = sshpkt_send(ssh)) != 0)
1752 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001753 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001754 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001755}
1756
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001757void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001758channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001759{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001760 ssh->chanctxt->x11_refuse_time = refuse_time;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001761}
1762
Damien Millerb38eff82000-04-01 11:09:21 +10001763/*
1764 * This socket is listening for connections to a forwarded TCP/IP port.
1765 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001766static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001767channel_post_port_listener(struct ssh *ssh, Channel *c,
1768 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001769{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001770 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001771 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001772 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001773 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001774 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001775
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001776 if (!FD_ISSET(c->sock, readset))
1777 return;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001778
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001779 debug("Connection to port %d forwarding to %.100s port %d requested.",
1780 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001781
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001782 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1783 nextstate = SSH_CHANNEL_OPENING;
1784 rtype = "forwarded-tcpip";
1785 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1786 nextstate = SSH_CHANNEL_OPENING;
1787 rtype = "forwarded-streamlocal@openssh.com";
1788 } else if (c->host_port == PORT_STREAMLOCAL) {
1789 nextstate = SSH_CHANNEL_OPENING;
1790 rtype = "direct-streamlocal@openssh.com";
1791 } else if (c->host_port == 0) {
1792 nextstate = SSH_CHANNEL_DYNAMIC;
1793 rtype = "dynamic-tcpip";
1794 } else {
1795 nextstate = SSH_CHANNEL_OPENING;
1796 rtype = "direct-tcpip";
Damien Millerb38eff82000-04-01 11:09:21 +10001797 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001798
1799 addrlen = sizeof(addr);
1800 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1801 if (newsock < 0) {
1802 if (errno != EINTR && errno != EWOULDBLOCK &&
1803 errno != ECONNABORTED)
1804 error("accept: %.100s", strerror(errno));
1805 if (errno == EMFILE || errno == ENFILE)
1806 c->notbefore = monotime() + 1;
1807 return;
1808 }
1809 if (c->host_port != PORT_STREAMLOCAL)
1810 set_nodelay(newsock);
1811 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1812 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1813 nc->listening_port = c->listening_port;
1814 nc->host_port = c->host_port;
1815 if (c->path != NULL)
1816 nc->path = xstrdup(c->path);
1817
1818 if (nextstate != SSH_CHANNEL_DYNAMIC)
1819 port_open_helper(ssh, nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001820}
1821
1822/*
1823 * This is the authentication agent socket listening for connections from
1824 * clients.
1825 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001826static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001827channel_post_auth_listener(struct ssh *ssh, Channel *c,
1828 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001829{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001830 Channel *nc;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001831 int r, newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001832 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001833 socklen_t addrlen;
1834
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001835 if (!FD_ISSET(c->sock, readset))
1836 return;
1837
1838 addrlen = sizeof(addr);
1839 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1840 if (newsock < 0) {
1841 error("accept from auth socket: %.100s", strerror(errno));
1842 if (errno == EMFILE || errno == ENFILE)
1843 c->notbefore = monotime() + 1;
1844 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001845 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001846 nc = channel_new(ssh, "accepted auth socket",
1847 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1848 c->local_window_max, c->local_maxpacket,
1849 0, "accepted auth socket", 1);
1850 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1851 if ((r = sshpkt_send(ssh)) != 0)
1852 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001853}
1854
Ben Lindstrombba81212001-06-25 05:01:22 +00001855static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001856channel_post_connecting(struct ssh *ssh, Channel *c,
1857 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001858{
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001859 int err = 0, sock, isopen, r;
Ben Lindstrom11180952001-07-04 05:13:35 +00001860 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001861
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001862 if (!FD_ISSET(c->sock, writeset))
1863 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001864 if (!c->have_remote_id)
1865 fatal(":%s: channel %d: no remote id", __func__, c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001866 /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1867 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001868 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1869 err = errno;
1870 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001871 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001872 if (err == 0) {
1873 debug("channel %d: connected to %s port %d",
1874 c->self, c->connect_ctx.host, c->connect_ctx.port);
1875 channel_connect_ctx_free(&c->connect_ctx);
1876 c->type = SSH_CHANNEL_OPEN;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001877 if (isopen) {
1878 /* no message necessary */
1879 } else {
1880 if ((r = sshpkt_start(ssh,
1881 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1882 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1883 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1884 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1885 (r = sshpkt_put_u32(ssh, c->local_maxpacket))
1886 != 0)
1887 fatal("%s: channel %i: confirm: %s", __func__,
1888 c->self, ssh_err(r));
1889 if ((r = sshpkt_send(ssh)) != 0)
1890 fatal("%s: channel %i: %s", __func__, c->self,
1891 ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001892 }
1893 } else {
1894 debug("channel %d: connection failed: %s",
1895 c->self, strerror(err));
1896 /* Try next address, if any */
1897 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1898 close(c->sock);
1899 c->sock = c->rfd = c->wfd = sock;
1900 channel_find_maxfd(ssh->chanctxt);
1901 return;
1902 }
1903 /* Exhausted all addresses */
1904 error("connect_to %.100s port %d: failed.",
1905 c->connect_ctx.host, c->connect_ctx.port);
1906 channel_connect_ctx_free(&c->connect_ctx);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001907 if (isopen) {
1908 rdynamic_close(ssh, c);
1909 } else {
1910 if ((r = sshpkt_start(ssh,
1911 SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1912 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001913 (r = sshpkt_put_u32(ssh,
1914 SSH2_OPEN_CONNECT_FAILED)) != 0 ||
1915 (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1916 (r = sshpkt_put_cstring(ssh, "")) != 0) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001917 fatal("%s: channel %i: failure: %s", __func__,
1918 c->self, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001919 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001920 if ((r = sshpkt_send(ssh)) != 0)
1921 fatal("%s: channel %i: %s", __func__, c->self,
1922 ssh_err(r));
1923 chan_mark_dead(ssh, c);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001924 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001925 }
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001926}
1927
Ben Lindstrombba81212001-06-25 05:01:22 +00001928static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001929channel_handle_rfd(struct ssh *ssh, Channel *c,
1930 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001931{
Darren Tucker11327cc2005-03-14 23:22:25 +11001932 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001933 ssize_t len;
1934 int r, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001935
Damien Miller835284b2007-06-11 13:03:16 +10001936 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001937
1938 if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset)))
1939 return 1;
1940
1941 errno = 0;
1942 len = read(c->rfd, buf, sizeof(buf));
1943 if (len < 0 && (errno == EINTR ||
1944 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1945 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001946#ifndef PTY_ZEROREAD
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001947 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001948#else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001949 if ((!c->isatty && len <= 0) ||
1950 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001951#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001952 debug2("channel %d: read<=0 rfd %d len %zd",
1953 c->self, c->rfd, len);
1954 if (c->type != SSH_CHANNEL_OPEN) {
1955 debug2("channel %d: not open", c->self);
1956 chan_mark_dead(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001957 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001958 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001959 chan_read_failed(ssh, c);
Damien Millerad833b32000-08-23 10:46:23 +10001960 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001961 return -1;
1962 }
1963 if (c->input_filter != NULL) {
1964 if (c->input_filter(ssh, c, buf, len) == -1) {
1965 debug2("channel %d: filter stops", c->self);
1966 chan_read_failed(ssh, c);
1967 }
1968 } else if (c->datagram) {
1969 if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1970 fatal("%s: channel %d: put datagram: %s", __func__,
1971 c->self, ssh_err(r));
1972 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
1973 fatal("%s: channel %d: put data: %s", __func__,
1974 c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001975 }
1976 return 1;
1977}
Damien Miller4f7becb2006-03-26 14:10:14 +11001978
Ben Lindstrombba81212001-06-25 05:01:22 +00001979static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001980channel_handle_wfd(struct ssh *ssh, Channel *c,
1981 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001982{
Ben Lindstrome229b252001-03-05 06:28:06 +00001983 struct termios tio;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001984 u_char *data = NULL, *buf; /* XXX const; need filter API change */
1985 size_t dlen, olen = 0;
1986 int r, len;
1987
1988 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
1989 sshbuf_len(c->output) == 0)
1990 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001991
1992 /* Send buffered output data to the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001993 olen = sshbuf_len(c->output);
1994 if (c->output_filter != NULL) {
1995 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
1996 debug2("channel %d: filter stops", c->self);
1997 if (c->type != SSH_CHANNEL_OPEN)
1998 chan_mark_dead(ssh, c);
1999 else
2000 chan_write_failed(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002001 return -1;
2002 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002003 } else if (c->datagram) {
2004 if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
2005 fatal("%s: channel %d: get datagram: %s", __func__,
2006 c->self, ssh_err(r));
djm@openbsd.org9145a732017-09-12 07:32:04 +00002007 buf = data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002008 } else {
2009 buf = data = sshbuf_mutable_ptr(c->output);
2010 dlen = sshbuf_len(c->output);
2011 }
2012
2013 if (c->datagram) {
2014 /* ignore truncated writes, datagrams might get lost */
djm@openbsd.orgc704f642017-09-24 09:50:01 +00002015 len = write(c->wfd, buf, dlen);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002016 free(data);
2017 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
2018 errno == EWOULDBLOCK))
2019 return 1;
2020 if (len <= 0)
2021 goto write_fail;
2022 goto out;
2023 }
2024
2025#ifdef _AIX
2026 /* XXX: Later AIX versions can't push as much data to tty */
2027 if (c->wfd_isatty)
2028 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10002029#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002030
2031 len = write(c->wfd, buf, dlen);
2032 if (len < 0 &&
2033 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
2034 return 1;
2035 if (len <= 0) {
2036 write_fail:
2037 if (c->type != SSH_CHANNEL_OPEN) {
2038 debug2("channel %d: not open", c->self);
2039 chan_mark_dead(ssh, c);
2040 return -1;
2041 } else {
2042 chan_write_failed(ssh, c);
2043 }
2044 return -1;
2045 }
2046#ifndef BROKEN_TCGETATTR_ICANON
2047 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
2048 if (tcgetattr(c->wfd, &tio) == 0 &&
2049 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
2050 /*
2051 * Simulate echo to reduce the impact of
2052 * traffic analysis. We need to match the
2053 * size of a SSH2_MSG_CHANNEL_DATA message
2054 * (4 byte channel id + buf)
2055 */
2056 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
2057 (r = sshpkt_send(ssh)) != 0)
2058 fatal("%s: channel %d: ignore: %s",
2059 __func__, c->self, ssh_err(r));
2060 }
2061 }
2062#endif /* BROKEN_TCGETATTR_ICANON */
2063 if ((r = sshbuf_consume(c->output, len)) != 0) {
2064 fatal("%s: channel %d: consume: %s",
2065 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002066 }
Damien Miller7d457182010-08-05 23:09:48 +10002067 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002068 c->local_consumed += olen - sshbuf_len(c->output);
2069
Damien Miller33b13562000-04-04 14:38:59 +10002070 return 1;
2071}
Damien Miller4f7becb2006-03-26 14:10:14 +11002072
Ben Lindstrombba81212001-06-25 05:01:22 +00002073static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002074channel_handle_efd_write(struct ssh *ssh, Channel *c,
2075 fd_set *readset, fd_set *writeset)
2076{
2077 int r;
2078 ssize_t len;
2079
2080 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
2081 return 1;
2082
2083 len = write(c->efd, sshbuf_ptr(c->extended),
2084 sshbuf_len(c->extended));
2085 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
2086 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
2087 errno == EWOULDBLOCK))
2088 return 1;
2089 if (len <= 0) {
2090 debug2("channel %d: closing write-efd %d", c->self, c->efd);
2091 channel_close_fd(ssh, &c->efd);
2092 } else {
2093 if ((r = sshbuf_consume(c->extended, len)) != 0) {
2094 fatal("%s: channel %d: consume: %s",
2095 __func__, c->self, ssh_err(r));
2096 }
2097 c->local_consumed += len;
2098 }
2099 return 1;
2100}
2101
2102static int
2103channel_handle_efd_read(struct ssh *ssh, Channel *c,
2104 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002105{
Darren Tucker11327cc2005-03-14 23:22:25 +11002106 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002107 int r;
2108 ssize_t len;
Damien Miller33b13562000-04-04 14:38:59 +10002109
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002110 if (!c->detach_close && !FD_ISSET(c->efd, readset))
2111 return 1;
2112
2113 len = read(c->efd, buf, sizeof(buf));
2114 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
2115 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
2116 errno == EWOULDBLOCK) && !c->detach_close)))
2117 return 1;
2118 if (len <= 0) {
2119 debug2("channel %d: closing read-efd %d",
2120 c->self, c->efd);
2121 channel_close_fd(ssh, &c->efd);
2122 } else {
2123 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
2124 debug3("channel %d: discard efd",
2125 c->self);
2126 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
2127 fatal("%s: channel %d: append: %s",
2128 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002129 }
2130 }
2131 return 1;
2132}
Damien Miller4f7becb2006-03-26 14:10:14 +11002133
Damien Miller0e220db2004-06-15 10:34:08 +10002134static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002135channel_handle_efd(struct ssh *ssh, Channel *c,
2136 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002137{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002138 if (c->efd == -1)
2139 return 1;
2140
2141 /** XXX handle drain efd, too */
2142
2143 if (c->extended_usage == CHAN_EXTENDED_WRITE)
2144 return channel_handle_efd_write(ssh, c, readset, writeset);
2145 else if (c->extended_usage == CHAN_EXTENDED_READ ||
2146 c->extended_usage == CHAN_EXTENDED_IGNORE)
2147 return channel_handle_efd_read(ssh, c, readset, writeset);
2148
2149 return 1;
2150}
2151
2152static int
2153channel_check_window(struct ssh *ssh, Channel *c)
2154{
2155 int r;
2156
Ben Lindstromb3921512001-04-11 15:57:50 +00002157 if (c->type == SSH_CHANNEL_OPEN &&
2158 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10002159 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10002160 c->local_maxpacket*3) ||
2161 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10002162 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00002163 if (!c->have_remote_id)
2164 fatal(":%s: channel %d: no remote id",
2165 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002166 if ((r = sshpkt_start(ssh,
2167 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2168 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2169 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2170 (r = sshpkt_send(ssh)) != 0) {
2171 fatal("%s: channel %i: %s", __func__,
2172 c->self, ssh_err(r));
2173 }
Damien Millerd3444942000-09-30 14:20:03 +11002174 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002175 c->self, c->local_window,
2176 c->local_consumed);
2177 c->local_window += c->local_consumed;
2178 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002179 }
2180 return 1;
2181}
2182
Ben Lindstrombba81212001-06-25 05:01:22 +00002183static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002184channel_post_open(struct ssh *ssh, Channel *c,
2185 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002186{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002187 channel_handle_rfd(ssh, c, readset, writeset);
2188 channel_handle_wfd(ssh, c, readset, writeset);
2189 channel_handle_efd(ssh, c, readset, writeset);
2190 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002191}
2192
Damien Millere1537f92010-01-26 13:26:22 +11002193static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002194read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002195{
2196 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002197 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002198 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002199 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002200
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002201 if (sshbuf_len(c->input) < need) {
2202 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002203 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002204 if (len < 0 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002205 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002206 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002207 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002208 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002209 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002210 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002211 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2212 fatal("%s: channel %d: append: %s",
2213 __func__, c->self, ssh_err(r));
2214 }
Damien Millere1537f92010-01-26 13:26:22 +11002215 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002216 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002217}
2218
2219static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002220channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2221 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002222{
2223 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002224
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002225 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2226 return;
2227 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2228 return;
2229 if (c->mux_pause)
2230 return;
2231
2232 /*
2233 * Don't not read past the precise end of packets to
2234 * avoid disrupting fd passing.
2235 */
2236 if (read_mux(ssh, c, 4) < 4) /* read header */
2237 return;
2238 /* XXX sshbuf_peek_u32 */
2239 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002240#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002241 if (need > CHANNEL_MUX_MAX_PACKET) {
2242 debug2("channel %d: packet too big %u > %u",
2243 c->self, CHANNEL_MUX_MAX_PACKET, need);
2244 chan_rcvd_oclose(ssh, c);
2245 return;
Damien Millere1537f92010-01-26 13:26:22 +11002246 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002247 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2248 return;
2249 if (c->mux_rcb(ssh, c) != 0) {
2250 debug("channel %d: mux_rcb failed", c->self);
2251 chan_mark_dead(ssh, c);
2252 return;
Damien Millere1537f92010-01-26 13:26:22 +11002253 }
2254}
2255
2256static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002257channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2258 fd_set *readset, fd_set *writeset)
2259{
2260 ssize_t len;
2261 int r;
2262
2263 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2264 sshbuf_len(c->output) == 0)
2265 return;
2266
2267 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2268 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2269 return;
2270 if (len <= 0) {
2271 chan_mark_dead(ssh, c);
2272 return;
2273 }
2274 if ((r = sshbuf_consume(c->output, len)) != 0)
2275 fatal("%s: channel %d: consume: %s", __func__,
2276 c->self, ssh_err(r));
2277}
2278
2279static void
2280channel_post_mux_client(struct ssh *ssh, Channel *c,
2281 fd_set *readset, fd_set *writeset)
2282{
2283 channel_post_mux_client_read(ssh, c, readset, writeset);
2284 channel_post_mux_client_write(ssh, c, readset, writeset);
2285}
2286
2287static void
2288channel_post_mux_listener(struct ssh *ssh, Channel *c,
2289 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002290{
2291 Channel *nc;
2292 struct sockaddr_storage addr;
2293 socklen_t addrlen;
2294 int newsock;
2295 uid_t euid;
2296 gid_t egid;
2297
2298 if (!FD_ISSET(c->sock, readset))
2299 return;
2300
2301 debug("multiplexing control connection");
2302
2303 /*
2304 * Accept connection on control socket
2305 */
2306 memset(&addr, 0, sizeof(addr));
2307 addrlen = sizeof(addr);
2308 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2309 &addrlen)) == -1) {
2310 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002311 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002312 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002313 return;
2314 }
2315
2316 if (getpeereid(newsock, &euid, &egid) < 0) {
2317 error("%s getpeereid failed: %s", __func__,
2318 strerror(errno));
2319 close(newsock);
2320 return;
2321 }
2322 if ((euid != 0) && (getuid() != euid)) {
2323 error("multiplex uid mismatch: peer euid %u != uid %u",
2324 (u_int)euid, (u_int)getuid());
2325 close(newsock);
2326 return;
2327 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002328 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002329 newsock, newsock, -1, c->local_window_max,
2330 c->local_maxpacket, 0, "mux-control", 1);
2331 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002332 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002333 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002334 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002335 /* mux state transitions must not elicit protocol messages */
2336 nc->flags |= CHAN_LOCAL;
2337}
2338
Ben Lindstrombba81212001-06-25 05:01:22 +00002339static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002340channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002341{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002342 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002343
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002344 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2345 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2346 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002347
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002348 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2349 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2350 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2351 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2352 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2353 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2354 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2355 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2356 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2357 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002358 pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002359 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2360 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2361
2362 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2363 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2364 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2365 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2366 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2367 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2368 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2369 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2370 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002371 post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002372 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2373 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2374
2375 sc->channel_pre = pre;
2376 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002377}
2378
Damien Miller3ec27592001-10-12 11:35:04 +10002379/* gc dead channels */
2380static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002381channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002382{
2383 if (c == NULL)
2384 return;
2385 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002386 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002387 return;
djm@openbsd.orgf1dd1792018-10-04 00:10:11 +00002388
Damien Millerfbdeece2003-09-02 22:52:31 +10002389 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002390 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002391 /* if we still have a callback */
2392 if (c->detach_user != NULL)
2393 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002394 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002395 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002396 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002397 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002398 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002399 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002400}
2401
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002402enum channel_table { CHAN_PRE, CHAN_POST };
2403
Ben Lindstrombba81212001-06-25 05:01:22 +00002404static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002405channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002406 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002407{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002408 struct ssh_channels *sc = ssh->chanctxt;
2409 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002410 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002411 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002412 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002413
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002414 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002415 if (unpause_secs != NULL)
2416 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002417 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2418 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002419 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002420 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002421 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002422 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002423 c->delayed = 0;
2424 else
2425 continue;
2426 }
Damien Millera6508752012-04-22 11:21:10 +10002427 if (ftab[c->type] != NULL) {
2428 /*
2429 * Run handlers that are not paused.
2430 */
2431 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002432 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002433 else if (unpause_secs != NULL) {
2434 /*
2435 * Collect the time that the earliest
2436 * channel comes off pause.
2437 */
2438 debug3("%s: chan %d: skip for %d more seconds",
2439 __func__, c->self,
2440 (int)(c->notbefore - now));
2441 if (*unpause_secs == 0 ||
2442 (c->notbefore - now) < *unpause_secs)
2443 *unpause_secs = c->notbefore - now;
2444 }
2445 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002446 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002447 }
Damien Millera6508752012-04-22 11:21:10 +10002448 if (unpause_secs != NULL && *unpause_secs != 0)
2449 debug3("%s: first channel unpauses in %d seconds",
2450 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002451}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002452
Ben Lindstrome9c99912001-06-09 00:41:05 +00002453/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002454 * Create sockets before allocating the select bitmasks.
2455 * This is necessary for things that need to happen after reading
2456 * the network-input but before channel_prepare_select().
2457 */
2458static void
2459channel_before_prepare_select(struct ssh *ssh)
2460{
2461 struct ssh_channels *sc = ssh->chanctxt;
2462 Channel *c;
2463 u_int i, oalloc;
2464
2465 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2466 c = sc->channels[i];
2467 if (c == NULL)
2468 continue;
2469 if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
2470 channel_before_prepare_select_rdynamic(ssh, c);
2471 }
2472}
2473
2474/*
Ben Lindstrome9c99912001-06-09 00:41:05 +00002475 * Allocate/update select bitmasks and add any bits relevant to channels in
2476 * select bitmasks.
2477 */
Damien Miller4af51302000-04-16 11:18:38 +10002478void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002479channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2480 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002481{
Damien Miller36812092006-03-26 14:22:47 +11002482 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002483
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002484 channel_before_prepare_select(ssh); /* might update channel_max_fd */
2485
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002486 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002487
Damien Miller36812092006-03-26 14:22:47 +11002488 nfdset = howmany(n+1, NFDBITS);
2489 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002490 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002491 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2492 sz = nfdset * sizeof(fd_mask);
2493
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002494 /* perhaps check sz < nalloc/2 and shrink? */
2495 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002496 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2497 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002498 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002499 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002500 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002501 memset(*readsetp, 0, sz);
2502 memset(*writesetp, 0, sz);
2503
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002504 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002505 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002506 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002507}
2508
Ben Lindstrome9c99912001-06-09 00:41:05 +00002509/*
2510 * After select, perform any appropriate operations for channels which have
2511 * events pending.
2512 */
Damien Miller4af51302000-04-16 11:18:38 +10002513void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002514channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002515{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002516 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002517}
2518
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002519/*
2520 * Enqueue data for channels with open or draining c->input.
2521 */
2522static void
2523channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2524{
djm@openbsd.org55486f52017-09-23 22:04:07 +00002525 size_t len, plen;
2526 const u_char *pkt;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002527 int r;
2528
2529 if ((len = sshbuf_len(c->input)) == 0) {
2530 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2531 /*
2532 * input-buffer is empty and read-socket shutdown:
2533 * tell peer, that we will not send more data:
2534 * send IEOF.
2535 * hack for extended data: delay EOF if EFD still
2536 * in use.
2537 */
2538 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2539 debug2("channel %d: "
2540 "ibuf_empty delayed efd %d/(%zu)",
2541 c->self, c->efd, sshbuf_len(c->extended));
2542 else
2543 chan_ibuf_empty(ssh, c);
2544 }
2545 return;
2546 }
2547
djm@openbsd.org9f532292017-09-12 06:35:31 +00002548 if (!c->have_remote_id)
2549 fatal(":%s: channel %d: no remote id", __func__, c->self);
2550
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002551 if (c->datagram) {
2552 /* Check datagram will fit; drop if not */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002553 if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
2554 fatal("%s: channel %d: get datagram: %s", __func__,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002555 c->self, ssh_err(r));
2556 /*
2557 * XXX this does tail-drop on the datagram queue which is
2558 * usually suboptimal compared to head-drop. Better to have
2559 * backpressure at read time? (i.e. read + discard)
2560 */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002561 if (plen > c->remote_window || plen > c->remote_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002562 debug("channel %d: datagram too big", c->self);
2563 return;
2564 }
2565 /* Enqueue it */
2566 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2567 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org55486f52017-09-23 22:04:07 +00002568 (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002569 (r = sshpkt_send(ssh)) != 0) {
2570 fatal("%s: channel %i: datagram: %s", __func__,
2571 c->self, ssh_err(r));
2572 }
djm@openbsd.org55486f52017-09-23 22:04:07 +00002573 c->remote_window -= plen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002574 return;
2575 }
2576
2577 /* Enqueue packet for buffered data. */
2578 if (len > c->remote_window)
2579 len = c->remote_window;
2580 if (len > c->remote_maxpacket)
2581 len = c->remote_maxpacket;
2582 if (len == 0)
2583 return;
2584 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2585 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2586 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2587 (r = sshpkt_send(ssh)) != 0) {
2588 fatal("%s: channel %i: data: %s", __func__,
2589 c->self, ssh_err(r));
2590 }
2591 if ((r = sshbuf_consume(c->input, len)) != 0)
2592 fatal("%s: channel %i: consume: %s", __func__,
2593 c->self, ssh_err(r));
2594 c->remote_window -= len;
2595}
2596
2597/*
2598 * Enqueue data for channels with open c->extended in read mode.
2599 */
2600static void
2601channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2602{
2603 size_t len;
2604 int r;
2605
2606 if ((len = sshbuf_len(c->extended)) == 0)
2607 return;
2608
2609 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2610 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2611 if (len > c->remote_window)
2612 len = c->remote_window;
2613 if (len > c->remote_maxpacket)
2614 len = c->remote_maxpacket;
2615 if (len == 0)
2616 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002617 if (!c->have_remote_id)
2618 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002619 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2620 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2621 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2622 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2623 (r = sshpkt_send(ssh)) != 0) {
2624 fatal("%s: channel %i: data: %s", __func__,
2625 c->self, ssh_err(r));
2626 }
2627 if ((r = sshbuf_consume(c->extended, len)) != 0)
2628 fatal("%s: channel %i: consume: %s", __func__,
2629 c->self, ssh_err(r));
2630 c->remote_window -= len;
2631 debug2("channel %d: sent ext data %zu", c->self, len);
2632}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002633
Damien Miller5e953212001-01-30 09:14:00 +11002634/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002635void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002636channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002637{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002638 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002639 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002640 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002641
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002642 for (i = 0; i < sc->channels_alloc; i++) {
2643 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002644 if (c == NULL)
2645 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002646
Ben Lindstrome9c99912001-06-09 00:41:05 +00002647 /*
2648 * We are only interested in channels that can have buffered
2649 * incoming data.
2650 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002651 if (c->type != SSH_CHANNEL_OPEN)
2652 continue;
2653 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002654 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002655 debug3("channel %d: will not send data after close",
2656 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002657 continue;
2658 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659
Damien Miller95def091999-11-25 00:26:21 +11002660 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002661 if (c->istate == CHAN_INPUT_OPEN ||
2662 c->istate == CHAN_INPUT_WAIT_DRAIN)
2663 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002664 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002665 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002666 c->extended_usage == CHAN_EXTENDED_READ)
2667 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002668 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002669}
2670
markus@openbsd.org8d057842016-09-30 09:19:13 +00002671/* -- mux proxy support */
2672
2673/*
2674 * When multiplexing channel messages for mux clients we have to deal
2675 * with downstream messages from the mux client and upstream messages
2676 * from the ssh server:
2677 * 1) Handling downstream messages is straightforward and happens
2678 * in channel_proxy_downstream():
2679 * - We forward all messages (mostly) unmodified to the server.
2680 * - However, in order to route messages from upstream to the correct
2681 * downstream client, we have to replace the channel IDs used by the
2682 * mux clients with a unique channel ID because the mux clients might
2683 * use conflicting channel IDs.
2684 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2685 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2686 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2687 * with the newly allocated channel ID.
2688 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
djm@openbsd.org001aa552018-04-10 00:10:49 +00002689 * channels and processed by channel_proxy_upstream(). The local channel ID
markus@openbsd.org8d057842016-09-30 09:19:13 +00002690 * is then translated back to the original mux client ID.
2691 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2692 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2693 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2694 * downstream mux client are removed.
2695 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2696 * requires more work, because they are not addressed to a specific
2697 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2698 * out whether the request is addressed to the local client or a
2699 * specific downstream client based on the listen-address/port.
djm@openbsd.org001aa552018-04-10 00:10:49 +00002700 * 6) Agent and X11-Forwarding have a similar problem and are currently
markus@openbsd.org8d057842016-09-30 09:19:13 +00002701 * not supported as the matching session/channel cannot be identified
2702 * easily.
2703 */
2704
2705/*
2706 * receive packets from downstream mux clients:
2707 * channel callback fired on read from mux client, creates
2708 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2709 * on channel creation.
2710 */
2711int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002712channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002713{
2714 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002715 struct sshbuf *original = NULL, *modified = NULL;
2716 const u_char *cp;
2717 char *ctype = NULL, *listen_host = NULL;
2718 u_char type;
2719 size_t have;
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002720 int ret = -1, r;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002721 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002722
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002723 /* sshbuf_dump(downstream->input, stderr); */
2724 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002725 != 0) {
2726 error("%s: malformed message: %s", __func__, ssh_err(r));
2727 return -1;
2728 }
2729 if (have < 2) {
2730 error("%s: short message", __func__);
2731 return -1;
2732 }
2733 type = cp[1];
2734 /* skip padlen + type */
2735 cp += 2;
2736 have -= 2;
2737 if (ssh_packet_log_type(type))
2738 debug3("%s: channel %u: down->up: type %u", __func__,
2739 downstream->self, type);
2740
2741 switch (type) {
2742 case SSH2_MSG_CHANNEL_OPEN:
2743 if ((original = sshbuf_from(cp, have)) == NULL ||
2744 (modified = sshbuf_new()) == NULL) {
2745 error("%s: alloc", __func__);
2746 goto out;
2747 }
2748 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2749 (r = sshbuf_get_u32(original, &id)) != 0) {
2750 error("%s: parse error %s", __func__, ssh_err(r));
2751 goto out;
2752 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002753 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002754 -1, -1, -1, 0, 0, 0, ctype, 1);
2755 c->mux_ctx = downstream; /* point to mux client */
2756 c->mux_downstream_id = id; /* original downstream id */
2757 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2758 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2759 (r = sshbuf_putb(modified, original)) != 0) {
2760 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002761 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002762 goto out;
2763 }
2764 break;
2765 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2766 /*
2767 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2768 * need to parse 'remote_id' instead of 'ctype'.
2769 */
2770 if ((original = sshbuf_from(cp, have)) == NULL ||
2771 (modified = sshbuf_new()) == NULL) {
2772 error("%s: alloc", __func__);
2773 goto out;
2774 }
2775 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2776 (r = sshbuf_get_u32(original, &id)) != 0) {
2777 error("%s: parse error %s", __func__, ssh_err(r));
2778 goto out;
2779 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002780 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002781 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2782 c->mux_ctx = downstream; /* point to mux client */
2783 c->mux_downstream_id = id;
2784 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002785 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002786 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2787 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2788 (r = sshbuf_putb(modified, original)) != 0) {
2789 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002790 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002791 goto out;
2792 }
2793 break;
2794 case SSH2_MSG_GLOBAL_REQUEST:
2795 if ((original = sshbuf_from(cp, have)) == NULL) {
2796 error("%s: alloc", __func__);
2797 goto out;
2798 }
2799 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2800 error("%s: parse error %s", __func__, ssh_err(r));
2801 goto out;
2802 }
2803 if (strcmp(ctype, "tcpip-forward") != 0) {
2804 error("%s: unsupported request %s", __func__, ctype);
2805 goto out;
2806 }
2807 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2808 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2809 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2810 error("%s: parse error %s", __func__, ssh_err(r));
2811 goto out;
2812 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002813 if (listen_port > 65535) {
2814 error("%s: tcpip-forward for %s: bad port %u",
2815 __func__, listen_host, listen_port);
2816 goto out;
2817 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002818 /* Record that connection to this host/port is permitted. */
djm@openbsd.org115063a2018-06-06 18:22:41 +00002819 permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>", -1,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002820 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002821 listen_host = NULL;
2822 break;
2823 case SSH2_MSG_CHANNEL_CLOSE:
2824 if (have < 4)
2825 break;
2826 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002827 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002828 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002829 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002830 else
2831 c->flags |= CHAN_CLOSE_SENT;
2832 }
2833 break;
2834 }
2835 if (modified) {
2836 if ((r = sshpkt_start(ssh, type)) != 0 ||
2837 (r = sshpkt_putb(ssh, modified)) != 0 ||
2838 (r = sshpkt_send(ssh)) != 0) {
2839 error("%s: send %s", __func__, ssh_err(r));
2840 goto out;
2841 }
2842 } else {
2843 if ((r = sshpkt_start(ssh, type)) != 0 ||
2844 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2845 (r = sshpkt_send(ssh)) != 0) {
2846 error("%s: send %s", __func__, ssh_err(r));
2847 goto out;
2848 }
2849 }
2850 ret = 0;
2851 out:
2852 free(ctype);
2853 free(listen_host);
2854 sshbuf_free(original);
2855 sshbuf_free(modified);
2856 return ret;
2857}
2858
2859/*
2860 * receive packets from upstream server and de-multiplex packets
2861 * to correct downstream:
2862 * implemented as a helper for channel input handlers,
2863 * replaces local (proxy) channel ID with downstream channel ID.
2864 */
2865int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002866channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002867{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002868 struct sshbuf *b = NULL;
2869 Channel *downstream;
2870 const u_char *cp = NULL;
2871 size_t len;
2872 int r;
2873
2874 /*
2875 * When receiving packets from the peer we need to check whether we
2876 * need to forward the packets to the mux client. In this case we
djm@openbsd.org001aa552018-04-10 00:10:49 +00002877 * restore the original channel id and keep track of CLOSE messages,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002878 * so we can cleanup the channel.
2879 */
2880 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2881 return 0;
2882 if ((downstream = c->mux_ctx) == NULL)
2883 return 0;
2884 switch (type) {
2885 case SSH2_MSG_CHANNEL_CLOSE:
2886 case SSH2_MSG_CHANNEL_DATA:
2887 case SSH2_MSG_CHANNEL_EOF:
2888 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2889 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2890 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2891 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2892 case SSH2_MSG_CHANNEL_SUCCESS:
2893 case SSH2_MSG_CHANNEL_FAILURE:
2894 case SSH2_MSG_CHANNEL_REQUEST:
2895 break;
2896 default:
2897 debug2("%s: channel %u: unsupported type %u", __func__,
2898 c->self, type);
2899 return 0;
2900 }
2901 if ((b = sshbuf_new()) == NULL) {
2902 error("%s: alloc reply", __func__);
2903 goto out;
2904 }
2905 /* get remaining payload (after id) */
2906 cp = sshpkt_ptr(ssh, &len);
2907 if (cp == NULL) {
2908 error("%s: no packet", __func__);
2909 goto out;
2910 }
2911 /* translate id and send to muxclient */
2912 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2913 (r = sshbuf_put_u8(b, type)) != 0 ||
2914 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2915 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002916 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002917 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2918 goto out;
2919 }
2920 /* sshbuf_dump(b, stderr); */
2921 if (ssh_packet_log_type(type))
2922 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2923 type);
2924 out:
2925 /* update state */
2926 switch (type) {
2927 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2928 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002929 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002930 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002931 c->have_remote_id = 1;
2932 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002933 break;
2934 case SSH2_MSG_CHANNEL_CLOSE:
2935 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002936 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002937 else
2938 c->flags |= CHAN_CLOSE_RCVD;
2939 break;
2940 }
2941 sshbuf_free(b);
2942 return 1;
2943}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002944
2945/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002946
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002947/* Parse a channel ID from the current packet */
2948static int
2949channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2950{
2951 u_int32_t id;
2952 int r;
2953
2954 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2955 error("%s: parse id: %s", where, ssh_err(r));
2956 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2957 }
2958 if (id > INT_MAX) {
2959 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2960 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2961 }
2962 return (int)id;
2963}
2964
2965/* Lookup a channel from an ID in the current packet */
2966static Channel *
2967channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2968{
2969 int id = channel_parse_id(ssh, where, what);
2970 Channel *c;
2971
2972 if ((c = channel_lookup(ssh, id)) == NULL) {
2973 ssh_packet_disconnect(ssh,
2974 "%s packet referred to nonexistent channel %d", what, id);
2975 }
2976 return c;
2977}
2978
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002979int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002980channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002981{
Damien Miller633de332014-05-15 13:48:26 +10002982 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002983 size_t data_len, win_len;
2984 Channel *c = channel_from_packet_id(ssh, __func__, "data");
2985 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002986
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002987 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002988 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002989
Damien Miller95def091999-11-25 00:26:21 +11002990 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002991 if (c->type != SSH_CHANNEL_OPEN &&
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002992 c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
2993 c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
Damien Millerb38eff82000-04-01 11:09:21 +10002994 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002995 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002996
Damien Miller95def091999-11-25 00:26:21 +11002997 /* Get the data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002998 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0)
2999 fatal("%s: channel %d: get data: %s", __func__,
3000 c->self, ssh_err(r));
3001 ssh_packet_check_eom(ssh);
3002
Damien Miller7d457182010-08-05 23:09:48 +10003003 win_len = data_len;
3004 if (c->datagram)
3005 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10003006
Damien Millera04ad492004-01-21 11:02:09 +11003007 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003008 * The sending side reduces its window as it sends data, so we
3009 * must 'fake' consumption of the data in order to ensure that window
3010 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11003011 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003012 if (c->ostate != CHAN_OUTPUT_OPEN) {
3013 c->local_window -= win_len;
3014 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003015 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11003016 }
3017
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003018 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003019 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003020 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003021 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003022 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003023 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003024 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003025 c->self, win_len, c->local_window);
3026 return 0;
3027 }
3028 c->local_window -= win_len;
3029
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003030 if (c->datagram) {
3031 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
3032 fatal("%s: channel %d: append datagram: %s",
3033 __func__, c->self, ssh_err(r));
3034 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
3035 fatal("%s: channel %d: append data: %s",
3036 __func__, c->self, ssh_err(r));
3037
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003038 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003039}
Ben Lindstrome9c99912001-06-09 00:41:05 +00003040
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003041int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003042channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003043{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003044 const u_char *data;
3045 size_t data_len;
3046 u_int32_t tcode;
3047 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
3048 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003049
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003050 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003051 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003052 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003053 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003054 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003055 }
Ben Lindstromcf159442002-03-26 03:26:24 +00003056 if (c->flags & CHAN_EOF_RCVD) {
3057 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003058 debug("channel %d: accepting ext data after eof",
3059 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00003060 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003061 ssh_packet_disconnect(ssh, "Received extended_data "
3062 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00003063 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003064
3065 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
3066 error("%s: parse tcode: %s", __func__, ssh_err(r));
3067 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3068 }
Damien Miller33b13562000-04-04 14:38:59 +10003069 if (c->efd == -1 ||
3070 c->extended_usage != CHAN_EXTENDED_WRITE ||
3071 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10003072 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003073 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003074 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003075 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) {
3076 error("%s: parse data: %s", __func__, ssh_err(r));
3077 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3078 }
3079 ssh_packet_check_eom(ssh);
3080
Damien Miller33b13562000-04-04 14:38:59 +10003081 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003082 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10003083 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003084 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003085 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003086 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
3087 /* XXX sshpkt_getb? */
3088 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
3089 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10003090 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003091 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003092}
3093
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003094int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003095channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10003096{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003097 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
Damien Millerb38eff82000-04-01 11:09:21 +10003098
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003099 ssh_packet_check_eom(ssh);
3100
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003101 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003102 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003103 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003104
3105 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11003106 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003107 debug("channel %d: FORCE input drain", c->self);
3108 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003109 if (sshbuf_len(c->input) == 0)
3110 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003111 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003112 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10003113}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003114
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003115int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003116channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003117{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003118 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
Damien Miller66823cd2002-01-22 23:11:38 +11003119
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003120 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003121 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003122 ssh_packet_check_eom(ssh);
3123 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003124 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003125}
3126
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003127int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003128channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003129{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003130 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3131 u_int32_t remote_window, remote_maxpacket;
3132 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003133
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003134 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003135 return 0;
3136 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10003137 packet_disconnect("Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003138 "non-opening channel %d.", c->self);
3139 /*
3140 * Record the remote channel number and mark that the channel
3141 * is now open.
3142 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00003143 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3144 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003145 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) {
3146 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
3147 packet_disconnect("Invalid open confirmation message");
3148 }
3149 ssh_packet_check_eom(ssh);
Damien Miller33b13562000-04-04 14:38:59 +10003150
djm@openbsd.org9f532292017-09-12 06:35:31 +00003151 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003152 c->remote_window = remote_window;
3153 c->remote_maxpacket = remote_maxpacket;
3154 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003155 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003156 debug2("%s: channel %d: callback start", __func__, c->self);
3157 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
3158 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003159 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003160 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3161 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003162 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003163}
3164
Ben Lindstrombba81212001-06-25 05:01:22 +00003165static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00003166reason2txt(int reason)
3167{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003168 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00003169 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3170 return "administratively prohibited";
3171 case SSH2_OPEN_CONNECT_FAILED:
3172 return "connect failed";
3173 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3174 return "unknown channel type";
3175 case SSH2_OPEN_RESOURCE_SHORTAGE:
3176 return "resource shortage";
3177 }
Ben Lindstrome2595442001-06-05 20:01:39 +00003178 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00003179}
3180
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003181int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003182channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003183{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003184 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3185 u_int32_t reason;
3186 char *msg = NULL;
3187 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003188
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003189 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003190 return 0;
3191 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10003192 packet_disconnect("Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003193 "non-opening channel %d.", c->self);
3194 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
3195 error("%s: reason: %s", __func__, ssh_err(r));
3196 packet_disconnect("Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003197 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003198 /* skip language */
3199 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
3200 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) {
3201 error("%s: message/lang: %s", __func__, ssh_err(r));
3202 packet_disconnect("Invalid open failure message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003203 }
3204 ssh_packet_check_eom(ssh);
3205 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003206 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3207 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003208 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003209 debug2("%s: channel %d: callback start", __func__, c->self);
3210 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3211 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003212 }
Damien Miller7a606212009-01-28 16:22:34 +11003213 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003214 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003215 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003216}
3217
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003218int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003219channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003220{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003221 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003222 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003223 u_int32_t adjust;
3224 u_int new_rwin;
3225 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003226
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003227 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003228 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003229 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00003230 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003231
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003232 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003233 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003234 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) {
3235 error("%s: adjust: %s", __func__, ssh_err(r));
3236 packet_disconnect("Invalid window adjust message");
3237 }
3238 ssh_packet_check_eom(ssh);
3239 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3240 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003241 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003242 c->self, adjust, c->remote_window);
3243 }
3244 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003245 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003246}
3247
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003248int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003249channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003250{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003251 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003252 Channel *c;
3253 struct channel_confirm *cc;
Damien Millerb84886b2008-05-19 15:05:07 +10003254
3255 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003256 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003257
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003258 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003259
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003260 if ((c = channel_lookup(ssh, id)) == NULL) {
3261 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003262 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003263 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003264 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003265 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003266 ssh_packet_check_eom(ssh);
Damien Millerb84886b2008-05-19 15:05:07 +10003267 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003268 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003269 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003270 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003271 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003272 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003273 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003274}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003275
Ben Lindstrome9c99912001-06-09 00:41:05 +00003276/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003277
Ben Lindstrom908afed2001-10-03 17:34:59 +00003278void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003279channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003280{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003281 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003282}
3283
Damien Millerf6dff7c2011-09-22 21:38:52 +10003284
3285/*
3286 * Determine whether or not a port forward listens to loopback, the
3287 * specified address or wildcard. On the client, a specified bind
3288 * address will always override gateway_ports. On the server, a
3289 * gateway_ports of 1 (``yes'') will override the client's specification
3290 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3291 * will bind to whatever address the client asked for.
3292 *
3293 * Special-case listen_addrs are:
3294 *
3295 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3296 * "" (empty string), "*" -> wildcard v4/v6
3297 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003298 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003299 */
3300static const char *
3301channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003302 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003303{
3304 const char *addr = NULL;
3305 int wildcard = 0;
3306
3307 if (listen_addr == NULL) {
3308 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003309 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003310 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003311 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003312 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3313 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3314 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003315 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003316 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003317 /*
3318 * Notify client if they requested a specific listen
3319 * address and it was overridden.
3320 */
3321 if (*listen_addr != '\0' &&
3322 strcmp(listen_addr, "0.0.0.0") != 0 &&
3323 strcmp(listen_addr, "*") != 0) {
3324 packet_send_debug("Forwarding listen address "
3325 "\"%s\" overridden by server "
3326 "GatewayPorts", listen_addr);
3327 }
Damien Miller602943d2014-07-04 08:59:41 +10003328 } else if (strcmp(listen_addr, "localhost") != 0 ||
3329 strcmp(listen_addr, "127.0.0.1") == 0 ||
3330 strcmp(listen_addr, "::1") == 0) {
3331 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003332 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003333 }
3334 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3335 strcmp(listen_addr, "::1") == 0) {
3336 /*
3337 * If a specific IPv4/IPv6 localhost address has been
3338 * requested then accept it even if gateway_ports is in
3339 * effect. This allows the client to prefer IPv4 or IPv6.
3340 */
3341 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003342 }
3343 if (wildcardp != NULL)
3344 *wildcardp = wildcard;
3345 return addr;
3346}
3347
Damien Millerb16461c2002-01-22 23:29:22 +11003348static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003349channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3350 struct Forward *fwd, int *allocated_listen_port,
3351 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003352{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003353 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003354 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003355 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003356 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003357 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003358 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003359
Damien Millerf91ee4c2005-03-01 21:24:33 +11003360 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003361
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003362 if (is_client && fwd->connect_path != NULL) {
3363 host = fwd->connect_path;
3364 } else {
3365 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3366 fwd->listen_host : fwd->connect_host;
3367 if (host == NULL) {
3368 error("No forward host name.");
3369 return 0;
3370 }
3371 if (strlen(host) >= NI_MAXHOST) {
3372 error("Forward host name too long.");
3373 return 0;
3374 }
Kevin Steves12057502001-02-05 14:54:34 +00003375 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003376
Damien Millerf6dff7c2011-09-22 21:38:52 +10003377 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003378 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3379 is_client, fwd_opts);
3380 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003381 type, wildcard, (addr == NULL) ? "NULL" : addr);
3382
3383 /*
Damien Miller34132e52000-01-14 15:45:46 +11003384 * getaddrinfo returns a loopback address if the hostname is
3385 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003386 */
Damien Miller34132e52000-01-14 15:45:46 +11003387 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003388 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003389 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003390 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003391 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003392 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3393 if (addr == NULL) {
3394 /* This really shouldn't happen */
3395 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003396 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003397 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003398 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003399 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003400 }
Damien Millera7270302005-07-06 09:36:05 +10003401 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003402 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003403 if (allocated_listen_port != NULL)
3404 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003405 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003406 switch (ai->ai_family) {
3407 case AF_INET:
3408 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3409 sin_port;
3410 break;
3411 case AF_INET6:
3412 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3413 sin6_port;
3414 break;
3415 default:
Damien Miller34132e52000-01-14 15:45:46 +11003416 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003417 }
3418 /*
3419 * If allocating a port for -R forwards, then use the
3420 * same port for all address families.
3421 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003422 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3423 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3424 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003425 *lport_p = htons(*allocated_listen_port);
3426
Damien Miller34132e52000-01-14 15:45:46 +11003427 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003428 strport, sizeof(strport),
3429 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003430 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003431 continue;
3432 }
3433 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003434 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003435 if (sock < 0) {
3436 /* this is no error since kernel may not support ipv6 */
djm@openbsd.org927f8512017-12-05 01:30:19 +00003437 verbose("socket [%s]:%s: %.100s", ntop, strport,
3438 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003439 continue;
3440 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003441
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00003442 set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003443 if (ai->ai_family == AF_INET6)
3444 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003445
Damien Miller4bf648f2009-02-14 16:28:21 +11003446 debug("Local forwarding listening on %s port %s.",
3447 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003448
Damien Miller34132e52000-01-14 15:45:46 +11003449 /* Bind the socket to the address. */
3450 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003451 /*
3452 * address can be in if use ipv6 address is
3453 * already bound
3454 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003455 if (!ai->ai_next)
djm@openbsd.org927f8512017-12-05 01:30:19 +00003456 error("bind [%s]:%s: %.100s",
3457 ntop, strport, strerror(errno));
Damien Miller3c7eeb22000-03-03 22:35:33 +11003458 else
djm@openbsd.org927f8512017-12-05 01:30:19 +00003459 verbose("bind [%s]:%s: %.100s",
3460 ntop, strport, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003461
Damien Miller34132e52000-01-14 15:45:46 +11003462 close(sock);
3463 continue;
3464 }
3465 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003466 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003467 error("listen: %.100s", strerror(errno));
djm@openbsd.org927f8512017-12-05 01:30:19 +00003468 error("listen [%s]:%s: %.100s", ntop, strport,
3469 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003470 close(sock);
3471 continue;
3472 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003473
3474 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003475 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003476 * record what we got.
3477 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003478 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3479 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003480 allocated_listen_port != NULL &&
3481 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003482 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003483 debug("Allocated listen port %d",
3484 *allocated_listen_port);
3485 }
3486
Damien Miller34132e52000-01-14 15:45:46 +11003487 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003488 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003489 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003490 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003491 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003492 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003493 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003494 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003495 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3496 c->listening_port = *allocated_listen_port;
3497 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003498 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003499 success = 1;
3500 }
3501 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003502 error("%s: cannot listen to port: %d", __func__,
3503 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003504 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003505 return success;
Damien Miller95def091999-11-25 00:26:21 +11003506}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003507
Damien Miller7acefbb2014-07-18 14:11:24 +10003508static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003509channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3510 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003511{
3512 struct sockaddr_un sunaddr;
3513 const char *path;
3514 Channel *c;
3515 int port, sock;
3516 mode_t omask;
3517
3518 switch (type) {
3519 case SSH_CHANNEL_UNIX_LISTENER:
3520 if (fwd->connect_path != NULL) {
3521 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3522 error("Local connecting path too long: %s",
3523 fwd->connect_path);
3524 return 0;
3525 }
3526 path = fwd->connect_path;
3527 port = PORT_STREAMLOCAL;
3528 } else {
3529 if (fwd->connect_host == NULL) {
3530 error("No forward host name.");
3531 return 0;
3532 }
3533 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3534 error("Forward host name too long.");
3535 return 0;
3536 }
3537 path = fwd->connect_host;
3538 port = fwd->connect_port;
3539 }
3540 break;
3541 case SSH_CHANNEL_RUNIX_LISTENER:
3542 path = fwd->listen_path;
3543 port = PORT_STREAMLOCAL;
3544 break;
3545 default:
3546 error("%s: unexpected channel type %d", __func__, type);
3547 return 0;
3548 }
3549
3550 if (fwd->listen_path == NULL) {
3551 error("No forward path name.");
3552 return 0;
3553 }
3554 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3555 error("Local listening path too long: %s", fwd->listen_path);
3556 return 0;
3557 }
3558
3559 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3560
3561 /* Start a Unix domain listener. */
3562 omask = umask(fwd_opts->streamlocal_bind_mask);
3563 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3564 fwd_opts->streamlocal_bind_unlink);
3565 umask(omask);
3566 if (sock < 0)
3567 return 0;
3568
3569 debug("Local forwarding listening on path %s.", fwd->listen_path);
3570
3571 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003572 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003573 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3574 0, "unix listener", 1);
3575 c->path = xstrdup(path);
3576 c->host_port = port;
3577 c->listening_port = PORT_STREAMLOCAL;
3578 c->listening_addr = xstrdup(fwd->listen_path);
3579 return 1;
3580}
3581
3582static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003583channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3584 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003585{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003586 u_int i;
3587 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003588
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003589 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3590 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003591 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3592 continue;
3593 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3594 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003595 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003596 found = 1;
3597 }
3598 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003599
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003600 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003601}
3602
Damien Miller7acefbb2014-07-18 14:11:24 +10003603static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003604channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003605{
3606 u_int i;
3607 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003608
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003609 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3610 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003611 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3612 continue;
3613 if (c->path == NULL)
3614 continue;
3615 if (strcmp(c->path, path) == 0) {
3616 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003617 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003618 found = 1;
3619 }
3620 }
3621
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003622 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003623}
3624
3625int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003626channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003627{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003628 if (fwd->listen_path != NULL) {
3629 return channel_cancel_rport_listener_streamlocal(ssh,
3630 fwd->listen_path);
3631 } else {
3632 return channel_cancel_rport_listener_tcpip(ssh,
3633 fwd->listen_host, fwd->listen_port);
3634 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003635}
3636
3637static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003638channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3639 const char *lhost, u_short lport, int cport,
3640 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003641{
3642 u_int i;
3643 int found = 0;
3644 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003645
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003646 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3647 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003648 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3649 continue;
Damien Millerff773642011-09-22 21:39:48 +10003650 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003651 continue;
Damien Millerff773642011-09-22 21:39:48 +10003652 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3653 /* skip dynamic forwardings */
3654 if (c->host_port == 0)
3655 continue;
3656 } else {
3657 if (c->host_port != cport)
3658 continue;
3659 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003660 if ((c->listening_addr == NULL && addr != NULL) ||
3661 (c->listening_addr != NULL && addr == NULL))
3662 continue;
3663 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003664 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003665 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003666 found = 1;
3667 }
3668 }
3669
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003670 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003671}
3672
Damien Miller7acefbb2014-07-18 14:11:24 +10003673static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003674channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003675{
3676 u_int i;
3677 int found = 0;
3678
3679 if (path == NULL) {
3680 error("%s: no path specified.", __func__);
3681 return 0;
3682 }
3683
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003684 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3685 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003686 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3687 continue;
3688 if (c->listening_addr == NULL)
3689 continue;
3690 if (strcmp(c->listening_addr, path) == 0) {
3691 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003692 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003693 found = 1;
3694 }
3695 }
3696
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003697 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003698}
3699
3700int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003701channel_cancel_lport_listener(struct ssh *ssh,
3702 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003703{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003704 if (fwd->listen_path != NULL) {
3705 return channel_cancel_lport_listener_streamlocal(ssh,
3706 fwd->listen_path);
3707 } else {
3708 return channel_cancel_lport_listener_tcpip(ssh,
3709 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3710 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003711}
3712
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003713/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003714int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003715channel_setup_local_fwd_listener(struct ssh *ssh,
3716 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003717{
Damien Miller7acefbb2014-07-18 14:11:24 +10003718 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003719 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003720 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3721 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003722 return channel_setup_fwd_listener_tcpip(ssh,
3723 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003724 }
Damien Millerb16461c2002-01-22 23:29:22 +11003725}
3726
djm@openbsd.org115063a2018-06-06 18:22:41 +00003727/* Matches a remote forwarding permission against a requested forwarding */
3728static int
3729remote_open_match(struct permission *allowed_open, struct Forward *fwd)
3730{
3731 int ret;
3732 char *lhost;
3733
3734 /* XXX add ACLs for streamlocal */
3735 if (fwd->listen_path != NULL)
3736 return 1;
3737
3738 if (fwd->listen_host == NULL || allowed_open->listen_host == NULL)
3739 return 0;
3740
3741 if (allowed_open->listen_port != FWD_PERMIT_ANY_PORT &&
3742 allowed_open->listen_port != fwd->listen_port)
3743 return 0;
3744
3745 /* Match hostnames case-insensitively */
3746 lhost = xstrdup(fwd->listen_host);
3747 lowercase(lhost);
3748 ret = match_pattern(lhost, allowed_open->listen_host);
3749 free(lhost);
3750
3751 return ret;
3752}
3753
3754/* Checks whether a requested remote forwarding is permitted */
3755static int
3756check_rfwd_permission(struct ssh *ssh, struct Forward *fwd)
3757{
3758 struct ssh_channels *sc = ssh->chanctxt;
3759 struct permission_set *pset = &sc->remote_perms;
3760 u_int i, permit, permit_adm = 1;
3761 struct permission *perm;
3762
3763 /* XXX apply GatewayPorts override before checking? */
3764
3765 permit = pset->all_permitted;
3766 if (!permit) {
3767 for (i = 0; i < pset->num_permitted_user; i++) {
3768 perm = &pset->permitted_user[i];
3769 if (remote_open_match(perm, fwd)) {
3770 permit = 1;
3771 break;
3772 }
3773 }
3774 }
3775
3776 if (pset->num_permitted_admin > 0) {
3777 permit_adm = 0;
3778 for (i = 0; i < pset->num_permitted_admin; i++) {
3779 perm = &pset->permitted_admin[i];
3780 if (remote_open_match(perm, fwd)) {
3781 permit_adm = 1;
3782 break;
3783 }
3784 }
3785 }
3786
3787 return permit && permit_adm;
3788}
3789
Damien Millerb16461c2002-01-22 23:29:22 +11003790/* protocol v2 remote port fwd, used by sshd */
3791int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003792channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003793 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003794{
djm@openbsd.org115063a2018-06-06 18:22:41 +00003795 if (!check_rfwd_permission(ssh, fwd)) {
3796 packet_send_debug("port forwarding refused");
3797 return 0;
3798 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003799 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003800 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003801 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3802 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003803 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003804 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3805 fwd_opts);
3806 }
Damien Millerb16461c2002-01-22 23:29:22 +11003807}
3808
Damien Miller5428f641999-11-25 11:54:57 +11003809/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003810 * Translate the requested rfwd listen host to something usable for
3811 * this server.
3812 */
3813static const char *
3814channel_rfwd_bind_host(const char *listen_host)
3815{
3816 if (listen_host == NULL) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003817 return "localhost";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003818 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003819 return "";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003820 } else
3821 return listen_host;
3822}
3823
3824/*
Damien Miller5428f641999-11-25 11:54:57 +11003825 * Initiate forwarding of connections to port "port" on remote host through
3826 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003827 * Returns handle (index) for updating the dynamic listen port with
djm@openbsd.org115063a2018-06-06 18:22:41 +00003828 * channel_update_permission().
Damien Miller5428f641999-11-25 11:54:57 +11003829 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003830int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003831channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003832{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003833 int r, success = 0, idx = -1;
3834 char *host_to_connect, *listen_host, *listen_path;
3835 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003836
Damien Miller95def091999-11-25 00:26:21 +11003837 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003838 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003839 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3840 (r = sshpkt_put_cstring(ssh,
3841 "streamlocal-forward@openssh.com")) != 0 ||
3842 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3843 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3844 (r = sshpkt_send(ssh)) != 0 ||
3845 (r = ssh_packet_write_wait(ssh)) != 0)
3846 fatal("%s: request streamlocal: %s",
3847 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003848 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003849 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3850 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3851 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3852 (r = sshpkt_put_cstring(ssh,
3853 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3854 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3855 (r = sshpkt_send(ssh)) != 0 ||
3856 (r = ssh_packet_write_wait(ssh)) != 0)
3857 fatal("%s: request tcpip-forward: %s",
3858 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003859 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003860 /* Assume that server accepts the request */
3861 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003862 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003863 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003864 host_to_connect = listen_host = listen_path = NULL;
3865 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003866 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003867 host_to_connect = xstrdup(fwd->connect_path);
3868 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003869 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003870 host_to_connect = xstrdup(fwd->connect_host);
3871 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003872 }
3873 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003874 listen_path = xstrdup(fwd->listen_path);
3875 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003876 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003877 if (fwd->listen_host != NULL)
3878 listen_host = xstrdup(fwd->listen_host);
3879 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003880 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00003881 idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003882 host_to_connect, port_to_connect,
3883 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003884 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003885 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003886}
3887
Damien Miller4b3ed642014-07-02 15:29:40 +10003888static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003889open_match(struct permission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003890 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003891{
3892 if (allowed_open->host_to_connect == NULL)
3893 return 0;
3894 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3895 allowed_open->port_to_connect != requestedport)
3896 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003897 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3898 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003899 return 0;
3900 return 1;
3901}
3902
3903/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003904 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003905 * we don't support FWD_PERMIT_ANY_PORT and
3906 * need to translate between the configured-host (listen_host)
3907 * and what we've sent to the remote server (channel_rfwd_bind_host)
3908 */
3909static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003910open_listen_match_tcpip(struct permission *allowed_open,
Damien Miller7acefbb2014-07-18 14:11:24 +10003911 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003912{
3913 const char *allowed_host;
3914
3915 if (allowed_open->host_to_connect == NULL)
3916 return 0;
3917 if (allowed_open->listen_port != requestedport)
3918 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003919 if (!translate && allowed_open->listen_host == NULL &&
3920 requestedhost == NULL)
3921 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003922 allowed_host = translate ?
3923 channel_rfwd_bind_host(allowed_open->listen_host) :
3924 allowed_open->listen_host;
djm@openbsd.org90e51d62018-06-25 22:28:33 +00003925 if (allowed_host == NULL || requestedhost == NULL ||
Damien Miller4b3ed642014-07-02 15:29:40 +10003926 strcmp(allowed_host, requestedhost) != 0)
3927 return 0;
3928 return 1;
3929}
3930
Damien Miller7acefbb2014-07-18 14:11:24 +10003931static int
djm@openbsd.org115063a2018-06-06 18:22:41 +00003932open_listen_match_streamlocal(struct permission *allowed_open,
Damien Miller7acefbb2014-07-18 14:11:24 +10003933 const char *requestedpath)
3934{
3935 if (allowed_open->host_to_connect == NULL)
3936 return 0;
3937 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3938 return 0;
3939 if (allowed_open->listen_path == NULL ||
3940 strcmp(allowed_open->listen_path, requestedpath) != 0)
3941 return 0;
3942 return 1;
3943}
3944
Damien Miller5428f641999-11-25 11:54:57 +11003945/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003946 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003947 * local side.
3948 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003949static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003950channel_request_rforward_cancel_tcpip(struct ssh *ssh,
3951 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003952{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003953 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003954 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003955 int r;
3956 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003957 struct permission *perm;
Darren Tuckere7066df2004-05-24 10:18:05 +10003958
djm@openbsd.org115063a2018-06-06 18:22:41 +00003959 for (i = 0; i < pset->num_permitted_user; i++) {
3960 perm = &pset->permitted_user[i];
3961 if (open_listen_match_tcpip(perm, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003962 break;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003963 perm = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10003964 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00003965 if (perm == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003966 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003967 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003968 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003969 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3970 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
3971 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3972 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
3973 (r = sshpkt_put_u32(ssh, port)) != 0 ||
3974 (r = sshpkt_send(ssh)) != 0)
3975 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10003976
djm@openbsd.org115063a2018-06-06 18:22:41 +00003977 fwd_perm_clear(perm); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10003978
3979 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003980}
3981
3982/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003983 * Request cancellation of remote forwarding of Unix domain socket
3984 * path from local side.
3985 */
3986static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003987channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003988{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003989 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003990 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003991 int r;
3992 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003993 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10003994
djm@openbsd.org115063a2018-06-06 18:22:41 +00003995 for (i = 0; i < pset->num_permitted_user; i++) {
3996 perm = &pset->permitted_user[i];
3997 if (open_listen_match_streamlocal(perm, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10003998 break;
djm@openbsd.org115063a2018-06-06 18:22:41 +00003999 perm = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10004000 }
djm@openbsd.org115063a2018-06-06 18:22:41 +00004001 if (perm == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004002 debug("%s: requested forward not found", __func__);
4003 return -1;
4004 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004005 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4006 (r = sshpkt_put_cstring(ssh,
4007 "cancel-streamlocal-forward@openssh.com")) != 0 ||
4008 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4009 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
4010 (r = sshpkt_send(ssh)) != 0)
4011 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10004012
djm@openbsd.org115063a2018-06-06 18:22:41 +00004013 fwd_perm_clear(perm); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10004014
4015 return 0;
4016}
4017
4018/*
4019 * Request cancellation of remote forwarding of a connection from local side.
4020 */
4021int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004022channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10004023{
4024 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004025 return channel_request_rforward_cancel_streamlocal(ssh,
4026 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10004027 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004028 return channel_request_rforward_cancel_tcpip(ssh,
4029 fwd->listen_host,
4030 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10004031 }
4032}
4033
4034/*
djm@openbsd.org115063a2018-06-06 18:22:41 +00004035 * Permits opening to any host/port if permitted_user[] is empty. This is
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004036 * usually called by the server, because the user could connect to any port
4037 * anyway, and the server has no way to know but to trust the client anyway.
4038 */
4039void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004040channel_permit_all(struct ssh *ssh, int where)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004041{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004042 struct permission_set *pset = permission_set_get(ssh, where);
4043
4044 if (pset->num_permitted_user == 0)
4045 pset->all_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004046}
4047
djm@openbsd.org115063a2018-06-06 18:22:41 +00004048/*
4049 * Permit the specified host/port for forwarding.
4050 */
Ben Lindstroma3700052001-04-05 23:26:32 +00004051void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004052channel_add_permission(struct ssh *ssh, int who, int where,
4053 char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004054{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004055 int local = where == FORWARD_LOCAL;
4056 struct permission_set *pset = permission_set_get(ssh, where);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004057
djm@openbsd.org115063a2018-06-06 18:22:41 +00004058 debug("allow %s forwarding to host %s port %d",
4059 fwd_ident(who, where), host, port);
4060 /*
4061 * Remote forwards set listen_host/port, local forwards set
4062 * host/port_to_connect.
4063 */
4064 permission_set_add(ssh, who, where,
4065 local ? host : 0, local ? port : 0,
4066 local ? NULL : host, NULL, local ? 0 : port, NULL);
4067 pset->all_permitted = 0;
4068}
4069
4070/*
4071 * Administratively disable forwarding.
4072 */
4073void
4074channel_disable_admin(struct ssh *ssh, int where)
4075{
4076 channel_clear_permission(ssh, FORWARD_ADM, where);
4077 permission_set_add(ssh, FORWARD_ADM, where,
4078 NULL, 0, NULL, NULL, 0, NULL);
4079}
4080
4081/*
4082 * Clear a list of permitted opens.
4083 */
4084void
4085channel_clear_permission(struct ssh *ssh, int who, int where)
4086{
4087 struct permission **permp;
4088 u_int *npermp;
4089
4090 permission_set_get_array(ssh, who, where, &permp, &npermp);
4091 *permp = xrecallocarray(*permp, *npermp, 0, sizeof(**permp));
4092 *npermp = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004093}
4094
Darren Tucker68afb8c2011-10-02 18:59:03 +11004095/*
4096 * Update the listen port for a dynamic remote forward, after
4097 * the actual 'newport' has been allocated. If 'newport' < 0 is
4098 * passed then they entry will be invalidated.
4099 */
4100void
djm@openbsd.org115063a2018-06-06 18:22:41 +00004101channel_update_permission(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11004102{
djm@openbsd.org115063a2018-06-06 18:22:41 +00004103 struct permission_set *pset = &ssh->chanctxt->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004104
djm@openbsd.org115063a2018-06-06 18:22:41 +00004105 if (idx < 0 || (u_int)idx >= pset->num_permitted_user) {
4106 debug("%s: index out of range: %d num_permitted_user %d",
4107 __func__, idx, pset->num_permitted_user);
Darren Tucker68afb8c2011-10-02 18:59:03 +11004108 return;
4109 }
4110 debug("%s allowed port %d for forwarding to host %s port %d",
4111 newport > 0 ? "Updating" : "Removing",
4112 newport,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004113 pset->permitted_user[idx].host_to_connect,
4114 pset->permitted_user[idx].port_to_connect);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004115 if (newport <= 0)
djm@openbsd.org115063a2018-06-06 18:22:41 +00004116 fwd_perm_clear(&pset->permitted_user[idx]);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004117 else {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004118 pset->permitted_user[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11004119 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11004120 }
4121}
4122
Darren Tucker1338b9e2011-10-02 18:57:35 +11004123/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
4124int
4125permitopen_port(const char *p)
4126{
4127 int port;
4128
4129 if (strcmp(p, "*") == 0)
4130 return FWD_PERMIT_ANY_PORT;
4131 if ((port = a2port(p)) > 0)
4132 return port;
4133 return -1;
4134}
4135
Damien Millerbd740252008-05-19 15:37:09 +10004136/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00004137static int
Damien Millerbd740252008-05-19 15:37:09 +10004138connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004139{
Damien Millerbd740252008-05-19 15:37:09 +10004140 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10004141 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004142 char ntop[NI_MAXHOST];
4143 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11004144
Damien Millerbd740252008-05-19 15:37:09 +10004145 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004146 switch (cctx->ai->ai_family) {
4147 case AF_UNIX:
4148 /* unix:pathname instead of host:port */
4149 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
4150 strlcpy(ntop, "unix", sizeof(ntop));
4151 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
4152 break;
4153 case AF_INET:
4154 case AF_INET6:
4155 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
4156 ntop, sizeof(ntop), strport, sizeof(strport),
4157 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
4158 error("connect_next: getnameinfo failed");
4159 continue;
4160 }
4161 break;
4162 default:
Damien Miller34132e52000-01-14 15:45:46 +11004163 continue;
4164 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11004165 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4166 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004167 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11004168 error("socket: %.100s", strerror(errno));
4169 else
4170 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004171 continue;
4172 }
Damien Miller232711f2004-06-15 10:35:30 +10004173 if (set_nonblock(sock) == -1)
4174 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10004175 if (connect(sock, cctx->ai->ai_addr,
4176 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
4177 debug("connect_next: host %.100s ([%.100s]:%s): "
4178 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004179 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004180 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004181 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004182 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004183 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004184 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004185 if (cctx->ai->ai_family != AF_UNIX)
4186 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004187 debug("connect_next: host %.100s ([%.100s]:%s) "
4188 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4189 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004190 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004191 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004192 return -1;
4193}
Damien Millerb38eff82000-04-01 11:09:21 +10004194
Damien Millerbd740252008-05-19 15:37:09 +10004195static void
4196channel_connect_ctx_free(struct channel_connect *cctx)
4197{
Darren Tuckera627d422013-06-02 07:31:17 +10004198 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004199 if (cctx->aitop) {
4200 if (cctx->aitop->ai_family == AF_UNIX)
4201 free(cctx->aitop);
4202 else
4203 freeaddrinfo(cctx->aitop);
4204 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004205 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004206}
4207
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004208/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004209 * Return connecting socket to remote host:port or local socket path,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004210 * passing back the failure reason if appropriate.
4211 */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004212static int
4213connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4214 char *ctype, char *rname, struct channel_connect *cctx,
4215 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10004216{
4217 struct addrinfo hints;
4218 int gaierr;
4219 int sock = -1;
4220 char strport[NI_MAXSERV];
Damien Miller7acefbb2014-07-18 14:11:24 +10004221
4222 if (port == PORT_STREAMLOCAL) {
4223 struct sockaddr_un *sunaddr;
4224 struct addrinfo *ai;
4225
4226 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4227 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004228 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004229 }
4230
4231 /*
4232 * Fake up a struct addrinfo for AF_UNIX connections.
4233 * channel_connect_ctx_free() must check ai_family
4234 * and use free() not freeaddirinfo() for AF_UNIX.
4235 */
4236 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4237 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4238 ai->ai_addr = (struct sockaddr *)(ai + 1);
4239 ai->ai_addrlen = sizeof(*sunaddr);
4240 ai->ai_family = AF_UNIX;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004241 ai->ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004242 ai->ai_protocol = PF_UNSPEC;
4243 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4244 sunaddr->sun_family = AF_UNIX;
4245 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004246 cctx->aitop = ai;
Damien Miller7acefbb2014-07-18 14:11:24 +10004247 } else {
4248 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004249 hints.ai_family = ssh->chanctxt->IPv4or6;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004250 hints.ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004251 snprintf(strport, sizeof strport, "%d", port);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004252 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004253 != 0) {
4254 if (errmsg != NULL)
4255 *errmsg = ssh_gai_strerror(gaierr);
4256 if (reason != NULL)
4257 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10004258 error("connect_to %.100s: unknown host (%s)", name,
4259 ssh_gai_strerror(gaierr));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004260 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004261 }
Damien Millerbd740252008-05-19 15:37:09 +10004262 }
4263
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004264 cctx->host = xstrdup(name);
4265 cctx->port = port;
4266 cctx->ai = cctx->aitop;
Damien Millerbd740252008-05-19 15:37:09 +10004267
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004268 if ((sock = connect_next(cctx)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004269 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004270 name, port, strerror(errno));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004271 return -1;
4272 }
4273
4274 return sock;
4275}
4276
4277/* Return CONNECTING channel to remote host:port or local socket path */
4278static Channel *
4279connect_to(struct ssh *ssh, const char *host, int port,
4280 char *ctype, char *rname)
4281{
4282 struct channel_connect cctx;
4283 Channel *c;
4284 int sock;
4285
4286 memset(&cctx, 0, sizeof(cctx));
4287 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4288 &cctx, NULL, NULL);
4289 if (sock == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004290 channel_connect_ctx_free(&cctx);
4291 return NULL;
4292 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004293 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004294 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004295 c->host_port = port;
4296 c->path = xstrdup(host);
Damien Millerbd740252008-05-19 15:37:09 +10004297 c->connect_ctx = cctx;
Damien Millerbd740252008-05-19 15:37:09 +10004298
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004299 return c;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004300}
4301
markus@openbsd.org8d057842016-09-30 09:19:13 +00004302/*
4303 * returns either the newly connected channel or the downstream channel
4304 * that needs to deal with this connection.
4305 */
Damien Millerbd740252008-05-19 15:37:09 +10004306Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004307channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004308 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004309{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004310 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004311 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004312 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004313 struct permission *perm;
Damien Millerbd740252008-05-19 15:37:09 +10004314
djm@openbsd.org115063a2018-06-06 18:22:41 +00004315 for (i = 0; i < pset->num_permitted_user; i++) {
4316 perm = &pset->permitted_user[i];
4317 if (open_listen_match_tcpip(perm,
4318 listen_host, listen_port, 1)) {
4319 if (perm->downstream)
4320 return perm->downstream;
4321 if (perm->port_to_connect == 0)
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004322 return rdynamic_connect_prepare(ssh,
4323 ctype, rname);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004324 return connect_to(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004325 perm->host_to_connect, perm->port_to_connect,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004326 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004327 }
4328 }
4329 error("WARNING: Server requests forwarding for unknown listen_port %d",
4330 listen_port);
4331 return NULL;
4332}
4333
Damien Miller7acefbb2014-07-18 14:11:24 +10004334Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004335channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4336 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004337{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004338 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004339 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004340 u_int i;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004341 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004342
djm@openbsd.org115063a2018-06-06 18:22:41 +00004343 for (i = 0; i < pset->num_permitted_user; i++) {
4344 perm = &pset->permitted_user[i];
4345 if (open_listen_match_streamlocal(perm, path)) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004346 return connect_to(ssh,
djm@openbsd.org115063a2018-06-06 18:22:41 +00004347 perm->host_to_connect, perm->port_to_connect,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004348 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004349 }
4350 }
4351 error("WARNING: Server requests forwarding for unknown path %.100s",
4352 path);
4353 return NULL;
4354}
4355
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004356/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004357Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004358channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4359 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004360{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004361 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004362 struct permission_set *pset = &sc->local_perms;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004363 struct channel_connect cctx;
4364 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004365 u_int i, permit, permit_adm = 1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004366 int sock;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004367 struct permission *perm;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004368
djm@openbsd.org115063a2018-06-06 18:22:41 +00004369 permit = pset->all_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004370 if (!permit) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004371 for (i = 0; i < pset->num_permitted_user; i++) {
4372 perm = &pset->permitted_user[i];
4373 if (open_match(perm, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004374 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004375 break;
4376 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004377 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004378 }
Damien Miller9b439df2006-07-24 14:04:00 +10004379
djm@openbsd.org115063a2018-06-06 18:22:41 +00004380 if (pset->num_permitted_admin > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004381 permit_adm = 0;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004382 for (i = 0; i < pset->num_permitted_admin; i++) {
4383 perm = &pset->permitted_admin[i];
4384 if (open_match(perm, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004385 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004386 break;
4387 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004388 }
Damien Miller9b439df2006-07-24 14:04:00 +10004389 }
4390
4391 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004392 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004393 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004394 if (reason != NULL)
4395 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004396 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004397 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004398
4399 memset(&cctx, 0, sizeof(cctx));
4400 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4401 &cctx, reason, errmsg);
4402 if (sock == -1) {
4403 channel_connect_ctx_free(&cctx);
4404 return NULL;
4405 }
4406
4407 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4408 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4409 c->host_port = port;
4410 c->path = xstrdup(host);
4411 c->connect_ctx = cctx;
4412
4413 return c;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004414}
4415
Damien Miller7acefbb2014-07-18 14:11:24 +10004416/* Check if connecting to that path is permitted and connect. */
4417Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004418channel_connect_to_path(struct ssh *ssh, const char *path,
4419 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004420{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004421 struct ssh_channels *sc = ssh->chanctxt;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004422 struct permission_set *pset = &sc->local_perms;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004423 u_int i, permit, permit_adm = 1;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004424 struct permission *perm;
Damien Miller7acefbb2014-07-18 14:11:24 +10004425
djm@openbsd.org115063a2018-06-06 18:22:41 +00004426 permit = pset->all_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004427 if (!permit) {
djm@openbsd.org115063a2018-06-06 18:22:41 +00004428 for (i = 0; i < pset->num_permitted_user; i++) {
4429 perm = &pset->permitted_user[i];
4430 if (open_match(perm, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004431 permit = 1;
4432 break;
4433 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004434 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004435 }
4436
djm@openbsd.org115063a2018-06-06 18:22:41 +00004437 if (pset->num_permitted_admin > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004438 permit_adm = 0;
djm@openbsd.org115063a2018-06-06 18:22:41 +00004439 for (i = 0; i < pset->num_permitted_admin; i++) {
4440 perm = &pset->permitted_admin[i];
4441 if (open_match(perm, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004442 permit_adm = 1;
4443 break;
4444 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004445 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004446 }
4447
4448 if (!permit || !permit_adm) {
4449 logit("Received request to connect to path %.100s, "
4450 "but the request was denied.", path);
4451 return NULL;
4452 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004453 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004454}
4455
Damien Miller0e220db2004-06-15 10:34:08 +10004456void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004457channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004458{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004459 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004460 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004461 int r;
4462 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004463
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004464 for (i = 0; i < sc->channels_alloc; i++) {
4465 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4466 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004467 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004468 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
Damien Miller0e220db2004-06-15 10:34:08 +10004469 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004470 channel_request_start(ssh, i, "window-change", 0);
4471 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4472 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4473 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4474 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4475 (r = sshpkt_send(ssh)) != 0)
4476 fatal("%s: channel %u: send window-change: %s",
4477 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004478 }
4479}
4480
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004481/* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4482static Channel *
4483rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4484{
4485 Channel *c;
4486 int r;
4487
4488 c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4489 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4490 c->host_port = 0;
4491 c->path = NULL;
4492
4493 /*
4494 * We need to open the channel before we have a FD,
4495 * so that we can get SOCKS header from peer.
4496 */
4497 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4498 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4499 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4500 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
4501 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
4502 fatal("%s: channel %i: confirm: %s", __func__,
4503 c->self, ssh_err(r));
4504 }
4505 return c;
4506}
4507
4508/* Return CONNECTING socket to remote host:port or local socket path */
4509static int
4510rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4511{
4512 struct channel_connect cctx;
4513 int sock;
4514
4515 memset(&cctx, 0, sizeof(cctx));
4516 sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4517 NULL, &cctx, NULL, NULL);
4518 if (sock == -1)
4519 channel_connect_ctx_free(&cctx);
4520 else {
4521 /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4522 c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4523 c->connect_ctx = cctx;
4524 channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4525 }
4526 return sock;
4527}
4528
Ben Lindstrome9c99912001-06-09 00:41:05 +00004529/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004530
Damien Miller5428f641999-11-25 11:54:57 +11004531/*
4532 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004533 * Returns 0 and a suitable display number for the DISPLAY variable
4534 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004535 */
Kevin Steves366298c2001-12-19 17:58:01 +00004536int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004537x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4538 int x11_use_localhost, int single_connection,
4539 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004540{
Damien Millere7378562001-12-21 14:58:35 +11004541 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004542 int display_number, sock;
4543 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004544 struct addrinfo hints, *ai, *aitop;
4545 char strport[NI_MAXSERV];
4546 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004547
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004548 if (chanids == NULL)
4549 return -1;
4550
Damien Millera34a28b1999-12-14 10:47:15 +11004551 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004552 display_number < MAX_DISPLAYS;
4553 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004554 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004555 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004556 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004557 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004558 hints.ai_socktype = SOCK_STREAM;
4559 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004560 if ((gaierr = getaddrinfo(NULL, strport,
4561 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004562 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004563 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004564 }
Damien Miller34132e52000-01-14 15:45:46 +11004565 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004566 if (ai->ai_family != AF_INET &&
4567 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004568 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004569 sock = socket(ai->ai_family, ai->ai_socktype,
4570 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004571 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004572 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4573#ifdef EPFNOSUPPORT
4574 && (errno != EPFNOSUPPORT)
4575#endif
4576 ) {
Damien Millere2192732000-01-17 13:22:55 +11004577 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004578 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004579 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004580 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004581 debug("x11_create_display_inet: Socket family %d not supported",
4582 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004583 continue;
4584 }
Damien Miller34132e52000-01-14 15:45:46 +11004585 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004586 if (ai->ai_family == AF_INET6)
4587 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004588 if (x11_use_localhost)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00004589 set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004590 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004591 debug2("%s: bind port %d: %.100s", __func__,
4592 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004593 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004594 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004595 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004596 num_socks = 0;
4597 break;
4598 }
4599 socks[num_socks++] = sock;
4600 if (num_socks == NUM_SOCKS)
4601 break;
Damien Miller95def091999-11-25 00:26:21 +11004602 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004603 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004604 if (num_socks > 0)
4605 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004606 }
Damien Miller95def091999-11-25 00:26:21 +11004607 if (display_number >= MAX_DISPLAYS) {
4608 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004609 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004610 }
Damien Miller95def091999-11-25 00:26:21 +11004611 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004612 for (n = 0; n < num_socks; n++) {
4613 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004614 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004615 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004616 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004617 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004618 }
Damien Miller95def091999-11-25 00:26:21 +11004619 }
Damien Miller34132e52000-01-14 15:45:46 +11004620
Damien Miller34132e52000-01-14 15:45:46 +11004621 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004622 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004623 for (n = 0; n < num_socks; n++) {
4624 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004625 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004626 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4627 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004628 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004629 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004630 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004631 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004632 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004633
Kevin Steves366298c2001-12-19 17:58:01 +00004634 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004635 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004636 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004637}
4638
Ben Lindstrombba81212001-06-25 05:01:22 +00004639static int
Damien Miller819dbb62009-01-21 16:46:26 +11004640connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004641{
Damien Miller95def091999-11-25 00:26:21 +11004642 int sock;
4643 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004644
Damien Miller3afe3752001-12-21 12:39:51 +11004645 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4646 if (sock < 0)
4647 error("socket: %.100s", strerror(errno));
4648 memset(&addr, 0, sizeof(addr));
4649 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004650 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004651 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004652 return sock;
4653 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004654 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4655 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004656}
4657
Damien Miller819dbb62009-01-21 16:46:26 +11004658static int
4659connect_local_xsocket(u_int dnr)
4660{
4661 char buf[1024];
4662 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4663 return connect_local_xsocket_path(buf);
4664}
4665
Darren Tuckerda39b092017-03-10 13:22:32 +11004666#ifdef __APPLE__
4667static int
4668is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4669{
4670 struct stat sbuf;
4671
4672 if (strlcpy(path, display, pathlen) >= pathlen) {
4673 error("%s: display path too long", __func__);
4674 return 0;
4675 }
4676 if (display[0] != '/')
4677 return 0;
4678 if (stat(path, &sbuf) == 0) {
4679 return 1;
4680 } else {
4681 char *dot = strrchr(path, '.');
4682 if (dot != NULL) {
4683 *dot = '\0';
4684 if (stat(path, &sbuf) == 0) {
4685 return 1;
4686 }
4687 }
4688 }
4689 return 0;
4690}
4691#endif
4692
Damien Millerbd483e72000-04-30 10:00:53 +10004693int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004694x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004695{
Damien Miller57c4e872006-03-31 23:11:07 +11004696 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004697 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004698 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004699 struct addrinfo hints, *ai, *aitop;
4700 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004701 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004702
Damien Miller95def091999-11-25 00:26:21 +11004703 /* Try to open a socket for the local X server. */
4704 display = getenv("DISPLAY");
4705 if (!display) {
4706 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004707 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004708 }
Damien Miller5428f641999-11-25 11:54:57 +11004709 /*
4710 * Now we decode the value of the DISPLAY variable and make a
4711 * connection to the real X server.
4712 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004713
Damien Miller819dbb62009-01-21 16:46:26 +11004714#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004715 /* Check if display is a path to a socket (as set by launchd). */
4716 {
4717 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004718
Darren Tuckerda39b092017-03-10 13:22:32 +11004719 if (is_path_to_xsocket(display, path, sizeof(path))) {
4720 debug("x11_connect_display: $DISPLAY is launchd");
4721
4722 /* Create a socket. */
4723 sock = connect_local_xsocket_path(path);
4724 if (sock < 0)
4725 return -1;
4726
4727 /* OK, we now have a connection to the display. */
4728 return sock;
4729 }
Damien Miller819dbb62009-01-21 16:46:26 +11004730 }
4731#endif
Damien Miller5428f641999-11-25 11:54:57 +11004732 /*
4733 * Check if it is a unix domain socket. Unix domain displays are in
4734 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4735 */
Damien Miller95def091999-11-25 00:26:21 +11004736 if (strncmp(display, "unix:", 5) == 0 ||
4737 display[0] == ':') {
4738 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004739 if (sscanf(strrchr(display, ':') + 1, "%u",
4740 &display_number) != 1) {
4741 error("Could not parse display number from DISPLAY: "
4742 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004743 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004744 }
4745 /* Create a socket. */
4746 sock = connect_local_xsocket(display_number);
4747 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004748 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004749
4750 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004751 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004752 }
Damien Miller5428f641999-11-25 11:54:57 +11004753 /*
4754 * Connect to an inet socket. The DISPLAY value is supposedly
4755 * hostname:d[.s], where hostname may also be numeric IP address.
4756 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004757 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004758 cp = strchr(buf, ':');
4759 if (!cp) {
4760 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004761 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004762 }
Damien Miller95def091999-11-25 00:26:21 +11004763 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004764 /*
4765 * buf now contains the host name. But first we parse the
4766 * display number.
4767 */
Damien Miller57c4e872006-03-31 23:11:07 +11004768 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004769 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004770 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004771 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004772 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004773
Damien Miller34132e52000-01-14 15:45:46 +11004774 /* Look up the host address */
4775 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004776 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004777 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004778 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004779 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004780 error("%.100s: unknown host. (%s)", buf,
4781 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004782 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004783 }
Damien Miller34132e52000-01-14 15:45:46 +11004784 for (ai = aitop; ai; ai = ai->ai_next) {
4785 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004786 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004787 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004788 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004789 continue;
4790 }
4791 /* Connect it to the display. */
4792 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004793 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004794 6000 + display_number, strerror(errno));
4795 close(sock);
4796 continue;
4797 }
4798 /* Success */
4799 break;
Damien Miller34132e52000-01-14 15:45:46 +11004800 }
Damien Miller34132e52000-01-14 15:45:46 +11004801 freeaddrinfo(aitop);
4802 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004803 error("connect %.100s port %u: %.100s", buf,
4804 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004805 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004806 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004807 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004808 return sock;
4809}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004810
Damien Millerbd483e72000-04-30 10:00:53 +10004811/*
Damien Miller5428f641999-11-25 11:54:57 +11004812 * Requests forwarding of X11 connections, generates fake authentication
4813 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004814 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004815 */
Damien Miller4af51302000-04-16 11:18:38 +10004816void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004817x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4818 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004819{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004820 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004821 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004822 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004823 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004824 char *new_data;
4825 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004826
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004827 if (sc->x11_saved_display == NULL)
4828 sc->x11_saved_display = xstrdup(disp);
4829 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004830 error("x11_request_forwarding_with_spoofing: different "
4831 "$DISPLAY already forwarded");
4832 return;
4833 }
4834
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004835 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004836 if (cp)
4837 cp = strchr(cp, '.');
4838 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004839 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004840 else
4841 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004842
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004843 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004844 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004845 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004846
4847 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004848 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004849 for (i = 0; i < data_len; i++) {
4850 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4851 fatal("x11_request_forwarding: bad "
4852 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004853 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004854 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004855 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004856
4857 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004858 sc->x11_fake_data = xmalloc(data_len);
4859 arc4random_buf(sc->x11_fake_data, data_len);
4860 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004861 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004862
Damien Miller95def091999-11-25 00:26:21 +11004863 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004864 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004865
Damien Miller95def091999-11-25 00:26:21 +11004866 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004867 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4868 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4869 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4870 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4871 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4872 (r = sshpkt_send(ssh)) != 0 ||
4873 (r = ssh_packet_write_wait(ssh)) != 0)
4874 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004875 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004876}