blob: bdee1f3860a070510694e7c3b012afcb0face850 [file] [log] [blame]
tb@openbsd.org50693202018-02-05 05:36:49 +00001/* $OpenBSD: channels.c,v 1.379 2018/02/05 05:36:49 tb Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000058#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <netdb.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000060#include <stdarg.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#ifdef HAVE_STDINT_H
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000062 #include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110063#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100064#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100065#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110067#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Millerb84886b2008-05-19 15:05:07 +100070#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000074#include "ssherr.h"
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000075#include "sshbuf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100082#include "key.h"
83#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110084#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000086/* -- agent forwarding */
87#define NUM_SOCKS 10
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Ben Lindstrome9c99912001-06-09 00:41:05 +000089/* -- tcp forwarding */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000090/* special-case port number meaning allow any port */
91#define FWD_PERMIT_ANY_PORT 0
92
93/* special-case wildcard meaning allow any host */
94#define FWD_PERMIT_ANY_HOST "*"
95
96/* -- X11 forwarding */
97/* Maximum number of fake X11 displays to try. */
98#define MAX_DISPLAYS 1000
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Data structure for storing which hosts are permitted for forward requests.
102 * The local sides of any remote forwards are stored in this array to prevent
103 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
104 * network (which might be behind a firewall).
105 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000106/* XXX: streamlocal wants a path instead of host:port */
107/* Overload host_to_connect; we could just make this match Forward */
108/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100109typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000110 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000111 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000112 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000113 char *listen_path; /* Remote side should listen path. */
114 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000115 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116} ForwardPermission;
117
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000118typedef void chan_fn(struct ssh *, Channel *c,
119 fd_set *readset, fd_set *writeset);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000120
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000121/* Master structure for channels state */
122struct ssh_channels {
123 /*
124 * Pointer to an array containing all allocated channels. The array
125 * is dynamically extended as needed.
126 */
127 Channel **channels;
Damien Miller9b439df2006-07-24 14:04:00 +1000128
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000129 /*
130 * Size of the channel array. All slots of the array must always be
131 * initialized (at least the type field); unused slots set to NULL
132 */
133 u_int channels_alloc;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000135 /*
136 * Maximum file descriptor value used in any of the channels. This is
137 * updated in channel_new.
138 */
139 int channel_max_fd;
Damien Miller9b439df2006-07-24 14:04:00 +1000140
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000141 /*
142 * 'channel_pre*' are called just before select() to add any bits
143 * relevant to channels in the select bitmasks.
144 *
145 * 'channel_post*': perform any appropriate operations for
146 * channels which have events pending.
147 */
148 chan_fn **channel_pre;
149 chan_fn **channel_post;
Darren Tucker1338b9e2011-10-02 18:57:35 +1100150
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000151 /* -- tcp forwarding */
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000152
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000153 /* List of all permitted host/port pairs to connect by the user. */
154 ForwardPermission *permitted_opens;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000156 /* List of all permitted host/port pairs to connect by the admin. */
157 ForwardPermission *permitted_adm_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000158
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000159 /*
160 * Number of permitted host/port pairs in the array permitted by
161 * the user.
162 */
163 u_int num_permitted_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000164
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000165 /*
166 * Number of permitted host/port pair in the array permitted by
167 * the admin.
168 */
169 u_int num_adm_permitted_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000171 /*
172 * If this is true, all opens are permitted. This is the case on
173 * the server on which we have to trust the client anyway, and the
174 * user could do anything after logging in anyway.
175 */
176 int all_opens_permitted;
Damien Miller13390022005-07-06 09:44:19 +1000177
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000178 /* -- X11 forwarding */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000179
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000180 /* Saved X11 local (client) display. */
181 char *x11_saved_display;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000182
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000183 /* Saved X11 authentication protocol name. */
184 char *x11_saved_proto;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000185
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000186 /* Saved X11 authentication data. This is the real data. */
187 char *x11_saved_data;
188 u_int x11_saved_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000189
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000190 /* Deadline after which all X11 connections are refused */
191 u_int x11_refuse_time;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000192
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000193 /*
194 * Fake X11 authentication data. This is what the server will be
195 * sending us; we should replace any occurrences of this by the
196 * real data.
197 */
198 u_char *x11_fake_data;
199 u_int x11_fake_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000200
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000201 /* AF_UNSPEC or AF_INET or AF_INET6 */
202 int IPv4or6;
203};
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000204
Ben Lindstrome9c99912001-06-09 00:41:05 +0000205/* helper */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000206static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000207static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000208
Damien Millerbd740252008-05-19 15:37:09 +1000209/* non-blocking connect helpers */
210static int connect_next(struct channel_connect *);
211static void channel_connect_ctx_free(struct channel_connect *);
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000212static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *);
213static int rdynamic_connect_finish(struct ssh *, Channel *);
Damien Millerbd740252008-05-19 15:37:09 +1000214
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000215/* Setup helper */
216static void channel_handler_init(struct ssh_channels *sc);
217
Ben Lindstrome9c99912001-06-09 00:41:05 +0000218/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000219
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000220void
221channel_init_channels(struct ssh *ssh)
222{
223 struct ssh_channels *sc;
224
225 if ((sc = calloc(1, sizeof(*sc))) == NULL ||
226 (sc->channel_pre = calloc(SSH_CHANNEL_MAX_TYPE,
227 sizeof(*sc->channel_pre))) == NULL ||
228 (sc->channel_post = calloc(SSH_CHANNEL_MAX_TYPE,
229 sizeof(*sc->channel_post))) == NULL)
230 fatal("%s: allocation failed", __func__);
231 sc->channels_alloc = 10;
232 sc->channels = xcalloc(sc->channels_alloc, sizeof(*sc->channels));
233 sc->IPv4or6 = AF_UNSPEC;
234 channel_handler_init(sc);
235
236 ssh->chanctxt = sc;
237}
238
Damien Millerb38eff82000-04-01 11:09:21 +1000239Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000240channel_by_id(struct ssh *ssh, int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000241{
242 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000243
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000244 if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
245 logit("%s: %d: bad id", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000246 return NULL;
247 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000248 c = ssh->chanctxt->channels[id];
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000249 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000250 logit("%s: %d: bad id: channel free", __func__, id);
Damien Millerb38eff82000-04-01 11:09:21 +1000251 return NULL;
252 }
253 return c;
254}
255
markus@openbsd.org8d057842016-09-30 09:19:13 +0000256Channel *
djm@openbsd.org9f532292017-09-12 06:35:31 +0000257channel_by_remote_id(struct ssh *ssh, u_int remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000258{
259 Channel *c;
260 u_int i;
261
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000262 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
263 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000264 if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
markus@openbsd.org8d057842016-09-30 09:19:13 +0000265 return c;
266 }
267 return NULL;
268}
269
Damien Miller5428f641999-11-25 11:54:57 +1100270/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100271 * Returns the channel if it is allowed to receive protocol messages.
272 * Private channels, like listening sockets, may not receive messages.
273 */
274Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000275channel_lookup(struct ssh *ssh, int id)
Damien Millerd47c62a2005-12-13 19:33:57 +1100276{
277 Channel *c;
278
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000279 if ((c = channel_by_id(ssh, id)) == NULL)
280 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100281
Damien Millerd62f2ca2006-03-26 13:57:41 +1100282 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100283 case SSH_CHANNEL_X11_OPEN:
284 case SSH_CHANNEL_LARVAL:
285 case SSH_CHANNEL_CONNECTING:
286 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000287 case SSH_CHANNEL_RDYNAMIC_OPEN:
288 case SSH_CHANNEL_RDYNAMIC_FINISH:
Damien Millerd47c62a2005-12-13 19:33:57 +1100289 case SSH_CHANNEL_OPENING:
290 case SSH_CHANNEL_OPEN:
Damien Miller36187092013-06-10 13:07:11 +1000291 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000292 case SSH_CHANNEL_MUX_PROXY:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000293 return c;
Damien Millerd47c62a2005-12-13 19:33:57 +1100294 }
295 logit("Non-public channel %d, type %d.", id, c->type);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000296 return NULL;
Damien Millerd47c62a2005-12-13 19:33:57 +1100297}
298
299/*
Damien Millere247cc42000-05-07 12:03:14 +1000300 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000301 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100302 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000304channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000305 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000307 struct ssh_channels *sc = ssh->chanctxt;
308
Damien Miller95def091999-11-25 00:26:21 +1100309 /* Update the maximum file descriptor value. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000310 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, rfd);
311 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, wfd);
312 sc->channel_max_fd = MAXIMUM(sc->channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100313
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100314 if (rfd != -1)
315 fcntl(rfd, F_SETFD, FD_CLOEXEC);
316 if (wfd != -1 && wfd != rfd)
317 fcntl(wfd, F_SETFD, FD_CLOEXEC);
318 if (efd != -1 && efd != rfd && efd != wfd)
319 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000320
Damien Miller6f83b8e2000-05-02 09:23:45 +1000321 c->rfd = rfd;
322 c->wfd = wfd;
323 c->sock = (rfd == wfd) ? rfd : -1;
324 c->efd = efd;
325 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100326
Darren Tuckered3cdc02008-06-16 23:29:18 +1000327 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000328 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000329#ifdef _AIX
330 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000331 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000332#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100333
Damien Miller69b69aa2000-10-28 14:19:58 +1100334 /* enable nonblocking mode */
335 if (nonblock) {
336 if (rfd != -1)
337 set_nonblock(rfd);
338 if (wfd != -1)
339 set_nonblock(wfd);
340 if (efd != -1)
341 set_nonblock(efd);
342 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000343}
344
345/*
346 * Allocate a new channel object and set its type and socket. This will cause
347 * remote_name to be freed.
348 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000349Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000350channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000351 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000352{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000353 struct ssh_channels *sc = ssh->chanctxt;
354 u_int i, found;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000355 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
Damien Miller95def091999-11-25 00:26:21 +1100357 /* Try to find a free slot where to put the new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000358 for (i = 0; i < sc->channels_alloc; i++) {
359 if (sc->channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100360 /* Found a free slot. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000361 found = i;
Damien Miller95def091999-11-25 00:26:21 +1100362 break;
363 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000364 }
365 if (i >= sc->channels_alloc) {
366 /*
367 * There are no free slots. Take last+1 slot and expand
368 * the array.
369 */
370 found = sc->channels_alloc;
371 if (sc->channels_alloc > CHANNELS_MAX_CHANNELS)
372 fatal("%s: internal error: channels_alloc %d too big",
373 __func__, sc->channels_alloc);
374 sc->channels = xrecallocarray(sc->channels, sc->channels_alloc,
375 sc->channels_alloc + 10, sizeof(*sc->channels));
376 sc->channels_alloc += 10;
377 debug2("channel: expanding %d", sc->channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100378 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000379 /* Initialize and return new channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000380 c = sc->channels[found] = xcalloc(1, sizeof(Channel));
381 if ((c->input = sshbuf_new()) == NULL ||
382 (c->output = sshbuf_new()) == NULL ||
383 (c->extended = sshbuf_new()) == NULL)
384 fatal("%s: sshbuf_new failed", __func__);
Damien Miller5144df92002-01-22 23:28:45 +1100385 c->ostate = CHAN_OUTPUT_OPEN;
386 c->istate = CHAN_INPUT_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000387 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Miller95def091999-11-25 00:26:21 +1100388 c->self = found;
389 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000390 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000391 c->local_window = window;
392 c->local_window_max = window;
Damien Millerbd483e72000-04-30 10:00:53 +1000393 c->local_maxpacket = maxpack;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000394 c->remote_name = xstrdup(remote_name);
Damien Millere1537f92010-01-26 13:26:22 +1100395 c->ctl_chan = -1;
Darren Tucker876045b2010-01-08 17:08:00 +1100396 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000397 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100398 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000399 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000401
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000402static void
403channel_find_maxfd(struct ssh_channels *sc)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000404{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000405 u_int i;
406 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000407 Channel *c;
408
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000409 for (i = 0; i < sc->channels_alloc; i++) {
410 c = sc->channels[i];
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000411 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000412 max = MAXIMUM(max, c->rfd);
413 max = MAXIMUM(max, c->wfd);
414 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000415 }
416 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000417 sc->channel_max_fd = max;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000418}
419
420int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000421channel_close_fd(struct ssh *ssh, int *fdp)
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000422{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000423 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000424 int ret = 0, fd = *fdp;
425
426 if (fd != -1) {
427 ret = close(fd);
428 *fdp = -1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000429 if (fd == sc->channel_max_fd)
430 channel_find_maxfd(sc);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000431 }
432 return ret;
433}
434
Damien Miller6f83b8e2000-05-02 09:23:45 +1000435/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000436static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000437channel_close_fds(struct ssh *ssh, Channel *c)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000438{
tb@openbsd.org50693202018-02-05 05:36:49 +0000439 int sock = c->sock, rfd = c->rfd, wfd = c->wfd, efd = c->efd;
440
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000441 channel_close_fd(ssh, &c->sock);
tb@openbsd.org50693202018-02-05 05:36:49 +0000442 if (rfd != sock)
443 channel_close_fd(ssh, &c->rfd);
444 if (wfd != sock && wfd != rfd)
445 channel_close_fd(ssh, &c->wfd);
446 if (efd != sock && efd != rfd && efd != wfd)
447 channel_close_fd(ssh, &c->efd);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000448}
449
450static void
451fwd_perm_clear(ForwardPermission *fp)
452{
453 free(fp->host_to_connect);
454 free(fp->listen_host);
455 free(fp->listen_path);
456 bzero(fp, sizeof(*fp));
457}
458
459enum { FWDPERM_USER, FWDPERM_ADMIN };
460
461static int
462fwd_perm_list_add(struct ssh *ssh, int which,
463 const char *host_to_connect, int port_to_connect,
464 const char *listen_host, const char *listen_path, int listen_port,
465 Channel *downstream)
466{
467 ForwardPermission **fpl;
468 u_int n, *nfpl;
469
470 switch (which) {
471 case FWDPERM_USER:
472 fpl = &ssh->chanctxt->permitted_opens;
473 nfpl = &ssh->chanctxt->num_permitted_opens;
474 break;
475 case FWDPERM_ADMIN:
476 fpl = &ssh->chanctxt->permitted_adm_opens;
477 nfpl = &ssh->chanctxt->num_adm_permitted_opens;
478 break;
479 default:
480 fatal("%s: invalid list %d", __func__, which);
481 }
482
483 if (*nfpl >= INT_MAX)
484 fatal("%s: overflow", __func__);
485
486 *fpl = xrecallocarray(*fpl, *nfpl, *nfpl + 1, sizeof(**fpl));
487 n = (*nfpl)++;
488#define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
489 (*fpl)[n].host_to_connect = MAYBE_DUP(host_to_connect);
490 (*fpl)[n].port_to_connect = port_to_connect;
491 (*fpl)[n].listen_host = MAYBE_DUP(listen_host);
492 (*fpl)[n].listen_path = MAYBE_DUP(listen_path);
493 (*fpl)[n].listen_port = listen_port;
494 (*fpl)[n].downstream = downstream;
495#undef MAYBE_DUP
496 return (int)n;
497}
498
499static void
500mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
501{
502 struct ssh_channels *sc = ssh->chanctxt;
503 ForwardPermission *fp;
504 int r;
505 u_int i;
506
507 for (i = 0; i < sc->num_permitted_opens; i++) {
508 fp = &sc->permitted_opens[i];
509 if (fp->downstream != c)
510 continue;
511
512 /* cancel on the server, since mux client is gone */
513 debug("channel %d: cleanup remote forward for %s:%u",
514 c->self, fp->listen_host, fp->listen_port);
515 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
516 (r = sshpkt_put_cstring(ssh,
517 "cancel-tcpip-forward")) != 0 ||
518 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
519 (r = sshpkt_put_cstring(ssh,
520 channel_rfwd_bind_host(fp->listen_host))) != 0 ||
521 (r = sshpkt_put_u32(ssh, fp->listen_port)) != 0 ||
522 (r = sshpkt_send(ssh)) != 0) {
523 fatal("%s: channel %i: %s", __func__,
524 c->self, ssh_err(r));
525 }
526 fwd_perm_clear(fp); /* unregister */
527 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000528}
529
530/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000531void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000532channel_free(struct ssh *ssh, Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000533{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000534 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000535 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000536 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000537 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000538 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000539
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000540 for (n = 0, i = 0; i < sc->channels_alloc; i++) {
541 if ((other = sc->channels[i]) == NULL)
542 continue;
543 n++;
544 /* detach from mux client and prepare for closing */
545 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
546 other->type == SSH_CHANNEL_MUX_PROXY &&
547 other->mux_ctx == c) {
548 other->mux_ctx = NULL;
549 other->type = SSH_CHANNEL_OPEN;
550 other->istate = CHAN_INPUT_CLOSED;
551 other->ostate = CHAN_OUTPUT_CLOSED;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000552 }
553 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000554 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000555 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000556
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000557 if (c->type == SSH_CHANNEL_MUX_CLIENT)
558 mux_remove_remote_forwardings(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000559
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000560 s = channel_open_message(ssh);
Damien Millerfbdeece2003-09-02 22:52:31 +1000561 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000562 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000563
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000564 channel_close_fds(ssh, c);
565 sshbuf_free(c->input);
566 sshbuf_free(c->output);
567 sshbuf_free(c->extended);
568 c->input = c->output = c->extended = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000569 free(c->remote_name);
570 c->remote_name = NULL;
571 free(c->path);
572 c->path = NULL;
573 free(c->listening_addr);
574 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000575 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
576 if (cc->abandon_cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000577 cc->abandon_cb(ssh, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +1000578 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100579 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000580 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000581 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000582 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000583 c->filter_cleanup(ssh, c->self, c->filter_ctx);
584 sc->channels[c->self] = NULL;
millert@openbsd.org3e8d1852017-09-19 12:10:30 +0000585 explicit_bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +1000586 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587}
588
Ben Lindstrome9c99912001-06-09 00:41:05 +0000589void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000590channel_free_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000591{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000592 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000593
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000594 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
595 if (ssh->chanctxt->channels[i] != NULL)
596 channel_free(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000597}
598
599/*
600 * Closes the sockets/fds of all channels. This is used to close extra file
601 * descriptors after a fork.
602 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000603void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000604channel_close_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000605{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000606 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000607
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000608 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
609 if (ssh->chanctxt->channels[i] != NULL)
610 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000611}
612
613/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000614 * Stop listening to channels.
615 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000616void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000617channel_stop_listening(struct ssh *ssh)
Ben Lindstrom809744e2001-07-04 05:26:06 +0000618{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000619 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000620 Channel *c;
621
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000622 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
623 c = ssh->chanctxt->channels[i];
Ben Lindstrom809744e2001-07-04 05:26:06 +0000624 if (c != NULL) {
625 switch (c->type) {
626 case SSH_CHANNEL_AUTH_SOCKET:
627 case SSH_CHANNEL_PORT_LISTENER:
628 case SSH_CHANNEL_RPORT_LISTENER:
629 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000630 case SSH_CHANNEL_UNIX_LISTENER:
631 case SSH_CHANNEL_RUNIX_LISTENER:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000632 channel_close_fd(ssh, &c->sock);
633 channel_free(ssh, c);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000634 break;
635 }
636 }
637 }
638}
639
640/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000641 * Returns true if no channel has too much buffered data, and false if one or
642 * more channel is overfull.
643 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000645channel_not_very_much_buffered_data(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646{
647 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000648 u_int maxsize = ssh_packet_get_maxsize(ssh);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000649 Channel *c;
650
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000651 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
652 c = ssh->chanctxt->channels[i];
653 if (c == NULL || c->type != SSH_CHANNEL_OPEN)
654 continue;
655 if (sshbuf_len(c->output) > maxsize) {
656 debug2("channel %d: big output buffer %zu > %u",
657 c->self, sshbuf_len(c->output), maxsize);
658 return 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000659 }
660 }
661 return 1;
662}
663
664/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000665int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000666channel_still_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000667{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000668 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000669 Channel *c;
670
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000671 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
672 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000673 if (c == NULL)
674 continue;
675 switch (c->type) {
676 case SSH_CHANNEL_X11_LISTENER:
677 case SSH_CHANNEL_PORT_LISTENER:
678 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100679 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000680 case SSH_CHANNEL_CLOSED:
681 case SSH_CHANNEL_AUTH_SOCKET:
682 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000683 case SSH_CHANNEL_RDYNAMIC_OPEN:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000684 case SSH_CHANNEL_CONNECTING:
685 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000686 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000687 case SSH_CHANNEL_UNIX_LISTENER:
688 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000689 continue;
690 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000691 continue;
692 case SSH_CHANNEL_OPENING:
693 case SSH_CHANNEL_OPEN:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000694 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000695 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100696 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000697 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000699 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000700 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000701 /* NOTREACHED */
702 }
703 }
704 return 0;
705}
706
707/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000708int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000709channel_find_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000710{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000711 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000712 Channel *c;
713
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000714 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
715 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000716 if (c == NULL || !c->have_remote_id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000717 continue;
718 switch (c->type) {
719 case SSH_CHANNEL_CLOSED:
720 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000721 case SSH_CHANNEL_RDYNAMIC_OPEN:
722 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000723 case SSH_CHANNEL_X11_LISTENER:
724 case SSH_CHANNEL_PORT_LISTENER:
725 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100726 case SSH_CHANNEL_MUX_LISTENER:
727 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000728 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000729 case SSH_CHANNEL_OPENING:
730 case SSH_CHANNEL_CONNECTING:
731 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000732 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000733 case SSH_CHANNEL_UNIX_LISTENER:
734 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000735 continue;
736 case SSH_CHANNEL_LARVAL:
737 case SSH_CHANNEL_AUTH_SOCKET:
738 case SSH_CHANNEL_OPEN:
739 case SSH_CHANNEL_X11_OPEN:
740 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000741 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000742 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743 /* NOTREACHED */
744 }
745 }
746 return -1;
747}
748
Ben Lindstrome9c99912001-06-09 00:41:05 +0000749/*
750 * Returns a message describing the currently open forwarded connections,
751 * suitable for sending to the client. The message contains crlf pairs for
752 * newlines.
753 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000754char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000755channel_open_message(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000756{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000757 struct sshbuf *buf;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000758 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000759 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000760 int r;
761 char *ret;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000762
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000763 if ((buf = sshbuf_new()) == NULL)
764 fatal("%s: sshbuf_new", __func__);
765 if ((r = sshbuf_putf(buf,
766 "The following connections are open:\r\n")) != 0)
767 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
768 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
769 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000770 if (c == NULL)
771 continue;
772 switch (c->type) {
773 case SSH_CHANNEL_X11_LISTENER:
774 case SSH_CHANNEL_PORT_LISTENER:
775 case SSH_CHANNEL_RPORT_LISTENER:
776 case SSH_CHANNEL_CLOSED:
777 case SSH_CHANNEL_AUTH_SOCKET:
778 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000779 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100780 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000781 case SSH_CHANNEL_UNIX_LISTENER:
782 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000783 continue;
784 case SSH_CHANNEL_LARVAL:
785 case SSH_CHANNEL_OPENING:
786 case SSH_CHANNEL_CONNECTING:
787 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000788 case SSH_CHANNEL_RDYNAMIC_OPEN:
789 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000790 case SSH_CHANNEL_OPEN:
791 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000792 case SSH_CHANNEL_MUX_PROXY:
793 case SSH_CHANNEL_MUX_CLIENT:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000794 if ((r = sshbuf_putf(buf, " #%d %.300s "
djm@openbsd.org9f532292017-09-12 06:35:31 +0000795 "(t%d %s%u i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796 c->self, c->remote_name,
djm@openbsd.org9f532292017-09-12 06:35:31 +0000797 c->type,
798 c->have_remote_id ? "r" : "nr", c->remote_id,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000799 c->istate, sshbuf_len(c->input),
800 c->ostate, sshbuf_len(c->output),
801 c->rfd, c->wfd, c->ctl_chan)) != 0)
802 fatal("%s: sshbuf_putf: %s",
803 __func__, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000804 continue;
805 default:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000806 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000807 /* NOTREACHED */
808 }
809 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000810 if ((ret = sshbuf_dup_string(buf)) == NULL)
811 fatal("%s: sshbuf_dup_string", __func__);
812 sshbuf_free(buf);
813 return ret;
814}
815
816static void
817open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
818{
819 int r;
820
821 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
822 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
823 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
824 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
825 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
826 fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r));
827 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000828}
829
830void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000831channel_send_open(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000832{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000833 Channel *c = channel_lookup(ssh, id);
834 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000835
Ben Lindstrome9c99912001-06-09 00:41:05 +0000836 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000837 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838 return;
839 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000840 debug2("channel %d: send open", id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000841 open_preamble(ssh, __func__, c, c->ctype);
842 if ((r = sshpkt_send(ssh)) != 0)
843 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000844}
845
846void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000847channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000848{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000849 Channel *c = channel_lookup(ssh, id);
850 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000851
Ben Lindstrome9c99912001-06-09 00:41:05 +0000852 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000853 logit("%s: %d: unknown channel id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000854 return;
855 }
djm@openbsd.org9f532292017-09-12 06:35:31 +0000856 if (!c->have_remote_id)
857 fatal(":%s: channel %d: no remote id", __func__, c->self);
858
Damien Miller0e220db2004-06-15 10:34:08 +1000859 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000860 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
861 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
862 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
863 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
864 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
865 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000866}
Damien Miller4f7becb2006-03-26 14:10:14 +1100867
Ben Lindstrome9c99912001-06-09 00:41:05 +0000868void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000869channel_register_status_confirm(struct ssh *ssh, int id,
870 channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
Damien Millerb84886b2008-05-19 15:05:07 +1000871{
872 struct channel_confirm *cc;
873 Channel *c;
874
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000875 if ((c = channel_lookup(ssh, id)) == NULL)
876 fatal("%s: %d: bad id", __func__, id);
Damien Millerb84886b2008-05-19 15:05:07 +1000877
Damien Miller6c81fee2013-11-08 12:19:55 +1100878 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000879 cc->cb = cb;
880 cc->abandon_cb = abandon_cb;
881 cc->ctx = ctx;
882 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
883}
884
885void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000886channel_register_open_confirm(struct ssh *ssh, int id,
887 channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000888{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000889 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000890
Ben Lindstrome9c99912001-06-09 00:41:05 +0000891 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000892 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000893 return;
894 }
Damien Millerb84886b2008-05-19 15:05:07 +1000895 c->open_confirm = fn;
896 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000897}
Damien Miller4f7becb2006-03-26 14:10:14 +1100898
Ben Lindstrome9c99912001-06-09 00:41:05 +0000899void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000900channel_register_cleanup(struct ssh *ssh, int id,
901 channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000902{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000903 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000904
Ben Lindstrome9c99912001-06-09 00:41:05 +0000905 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000906 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000907 return;
908 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000909 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100910 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000911}
Damien Miller4f7becb2006-03-26 14:10:14 +1100912
Ben Lindstrome9c99912001-06-09 00:41:05 +0000913void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000914channel_cancel_cleanup(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000915{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000916 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000917
Ben Lindstrome9c99912001-06-09 00:41:05 +0000918 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000919 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000920 return;
921 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000922 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100923 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000924}
Damien Miller4f7becb2006-03-26 14:10:14 +1100925
Ben Lindstrome9c99912001-06-09 00:41:05 +0000926void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000927channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000928 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000929{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000930 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000931
Ben Lindstrome9c99912001-06-09 00:41:05 +0000932 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000933 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000934 return;
935 }
Damien Miller077b2382005-12-31 16:22:32 +1100936 c->input_filter = ifn;
937 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000938 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000939 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000940}
941
942void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000943channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000944 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000945{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000946 Channel *c = channel_lookup(ssh, id);
947 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000948
Ben Lindstrome9c99912001-06-09 00:41:05 +0000949 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
950 fatal("channel_activate for non-larval channel %d.", id);
djm@openbsd.org9f532292017-09-12 06:35:31 +0000951 if (!c->have_remote_id)
952 fatal(":%s: channel %d: no remote id", __func__, c->self);
953
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000954 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000955 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100956 c->local_window = c->local_window_max = window_max;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000957
958 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
959 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
960 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
961 (r = sshpkt_send(ssh)) != 0)
962 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000963}
964
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000966channel_pre_listener(struct ssh *ssh, Channel *c,
967 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000968{
969 FD_SET(c->sock, readset);
970}
971
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000973channel_pre_connecting(struct ssh *ssh, Channel *c,
974 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000975{
976 debug3("channel %d: waiting for connection", c->self);
977 FD_SET(c->sock, writeset);
978}
979
Ben Lindstrombba81212001-06-25 05:01:22 +0000980static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000981channel_pre_open(struct ssh *ssh, Channel *c,
982 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000983{
Damien Miller33b13562000-04-04 14:38:59 +1000984 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000985 c->remote_window > 0 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000986 sshbuf_len(c->input) < c->remote_window &&
987 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Miller33b13562000-04-04 14:38:59 +1000988 FD_SET(c->rfd, readset);
989 if (c->ostate == CHAN_OUTPUT_OPEN ||
990 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000991 if (sshbuf_len(c->output) > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000992 FD_SET(c->wfd, writeset);
993 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000994 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000995 debug2("channel %d: "
996 "obuf_empty delayed efd %d/(%zu)", c->self,
997 c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000998 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000999 chan_obuf_empty(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10001000 }
1001 }
1002 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001003 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
1004 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +10001005 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001006 sshbuf_len(c->extended) > 0)
Damien Miller33b13562000-04-04 14:38:59 +10001007 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +10001008 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1009 (c->extended_usage == CHAN_EXTENDED_READ ||
1010 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001011 sshbuf_len(c->extended) < c->remote_window)
Damien Miller33b13562000-04-04 14:38:59 +10001012 FD_SET(c->efd, readset);
1013 }
Damien Miller0e220db2004-06-15 10:34:08 +10001014 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +10001015}
1016
Damien Millerb38eff82000-04-01 11:09:21 +10001017/*
1018 * This is a special state for X11 authentication spoofing. An opened X11
1019 * connection (when authentication spoofing is being done) remains in this
1020 * state until the first packet has been completely read. The authentication
1021 * data in that packet is then substituted by the real data if it matches the
1022 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +10001023 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +00001024 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +10001025 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001026static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001027x11_open_helper(struct ssh *ssh, struct sshbuf *b)
Damien Millerb38eff82000-04-01 11:09:21 +10001028{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001029 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001030 u_char *ucp;
1031 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001032
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001033 /* Is this being called after the refusal deadline? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001034 if (sc->x11_refuse_time != 0 &&
1035 (u_int)monotime() >= sc->x11_refuse_time) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001036 verbose("Rejected X11 connection after ForwardX11Timeout "
1037 "expired");
1038 return -1;
1039 }
1040
Damien Millerb38eff82000-04-01 11:09:21 +10001041 /* Check if the fixed size part of the packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001042 if (sshbuf_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +10001043 return 0;
1044
1045 /* Parse the lengths of variable-length fields. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001046 ucp = sshbuf_mutable_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +10001047 if (ucp[0] == 0x42) { /* Byte order MSB first. */
1048 proto_len = 256 * ucp[6] + ucp[7];
1049 data_len = 256 * ucp[8] + ucp[9];
1050 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1051 proto_len = ucp[6] + 256 * ucp[7];
1052 data_len = ucp[8] + 256 * ucp[9];
1053 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001054 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001055 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001056 return -1;
1057 }
1058
1059 /* Check if the whole packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001060 if (sshbuf_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001061 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1062 return 0;
1063
1064 /* Check if authentication protocol matches. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001065 if (proto_len != strlen(sc->x11_saved_proto) ||
1066 memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001067 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001068 return -1;
1069 }
1070 /* Check if authentication data matches our fake data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001071 if (data_len != sc->x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001072 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001073 sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001074 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001075 return -1;
1076 }
1077 /* Check fake data length */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001078 if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
Damien Millerb38eff82000-04-01 11:09:21 +10001079 error("X11 fake_data_len %d != saved_data_len %d",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001080 sc->x11_fake_data_len, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001081 return -1;
1082 }
1083 /*
1084 * Received authentication protocol and data match
1085 * our fake data. Substitute the fake data with real
1086 * data.
1087 */
1088 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001089 sc->x11_saved_data, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001090 return 1;
1091}
1092
Ben Lindstrombba81212001-06-25 05:01:22 +00001093static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001094channel_pre_x11_open(struct ssh *ssh, Channel *c,
1095 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001096{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001097 int ret = x11_open_helper(ssh, c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001098
1099 /* c->force_drain = 1; */
1100
Damien Millerb38eff82000-04-01 11:09:21 +10001101 if (ret == 1) {
1102 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001103 channel_pre_open(ssh, c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001104 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001105 logit("X11 connection rejected because of wrong authentication.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001106 debug2("X11 rejected %d i%d/o%d",
1107 c->self, c->istate, c->ostate);
1108 chan_read_failed(ssh, c);
1109 sshbuf_reset(c->input);
1110 chan_ibuf_empty(ssh, c);
1111 sshbuf_reset(c->output);
1112 chan_write_failed(ssh, c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001113 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001114 }
1115}
1116
Damien Millere1537f92010-01-26 13:26:22 +11001117static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001118channel_pre_mux_client(struct ssh *ssh,
1119 Channel *c, fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11001120{
Damien Millerd530f5f2010-05-21 14:57:10 +10001121 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001122 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Millere1537f92010-01-26 13:26:22 +11001123 FD_SET(c->rfd, readset);
1124 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1125 /* clear buffer immediately (discard any partial packet) */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001126 sshbuf_reset(c->input);
1127 chan_ibuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001128 /* Start output drain. XXX just kill chan? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001129 chan_rcvd_oclose(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001130 }
1131 if (c->ostate == CHAN_OUTPUT_OPEN ||
1132 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001133 if (sshbuf_len(c->output) > 0)
Damien Millere1537f92010-01-26 13:26:22 +11001134 FD_SET(c->wfd, writeset);
1135 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001136 chan_obuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001137 }
1138}
1139
Ben Lindstromb3921512001-04-11 15:57:50 +00001140/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +00001141static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001142channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001143{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001144 const u_char *p;
1145 char *host;
Damien Miller1781f532009-01-28 16:24:41 +11001146 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001147 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001148 struct {
1149 u_int8_t version;
1150 u_int8_t command;
1151 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001152 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001153 } s4_req, s4_rsp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001154 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001155
Ben Lindstromb3921512001-04-11 15:57:50 +00001156 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001157
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001158 have = sshbuf_len(input);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001159 len = sizeof(s4_req);
1160 if (have < len)
1161 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001162 p = sshbuf_ptr(input);
Damien Miller1781f532009-01-28 16:24:41 +11001163
1164 need = 1;
1165 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1166 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1167 debug2("channel %d: socks4a request", c->self);
1168 /* ... and needs an extra string (the hostname) */
1169 need = 2;
1170 }
1171 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001172 for (found = 0, i = len; i < have; i++) {
1173 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001174 found++;
1175 if (found == need)
1176 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001177 }
1178 if (i > 1024) {
1179 /* the peer is probably sending garbage */
1180 debug("channel %d: decode socks4: too long",
1181 c->self);
1182 return -1;
1183 }
1184 }
Damien Miller1781f532009-01-28 16:24:41 +11001185 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001186 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001187 if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1188 (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1189 (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1190 (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001191 debug("channels %d: decode socks4: %s", c->self, ssh_err(r));
1192 return -1;
1193 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001194 have = sshbuf_len(input);
1195 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001196 if (memchr(p, '\0', have) == NULL) {
1197 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001198 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001199 return -1;
1200 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001201 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001202 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001203 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001204 strlcpy(username, p, sizeof(username));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001205 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001206 fatal("%s: channel %d: consume: %s", __func__,
1207 c->self, ssh_err(r));
1208 }
Darren Tuckera627d422013-06-02 07:31:17 +10001209 free(c->path);
1210 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001211 if (need == 1) { /* SOCKS4: one string */
1212 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001213 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001214 } else { /* SOCKS4A: two strings */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001215 have = sshbuf_len(input);
1216 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001217 if (memchr(p, '\0', have) == NULL) {
1218 error("channel %d: decode socks4a: host not nul "
1219 "terminated", c->self);
1220 return -1;
1221 }
Damien Miller1781f532009-01-28 16:24:41 +11001222 len = strlen(p);
1223 debug2("channel %d: decode socks4a: host %s/%d",
1224 c->self, p, len);
1225 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001226 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001227 error("channel %d: hostname \"%.100s\" too long",
1228 c->self, p);
1229 return -1;
1230 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001231 c->path = xstrdup(p);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001232 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001233 fatal("%s: channel %d: consume: %s", __func__,
1234 c->self, ssh_err(r));
1235 }
Damien Miller1781f532009-01-28 16:24:41 +11001236 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001237 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001238
Damien Millerfbdeece2003-09-02 22:52:31 +10001239 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001240 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001241
Ben Lindstromb3921512001-04-11 15:57:50 +00001242 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001243 debug("channel %d: cannot handle: %s cn %d",
1244 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001245 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001246 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001247 s4_rsp.version = 0; /* vn: 0 for reply */
1248 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001249 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001250 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001251 if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001252 fatal("%s: channel %d: append reply: %s", __func__,
1253 c->self, ssh_err(r));
1254 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001255 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001256}
1257
Darren Tucker46471c92003-07-03 13:55:19 +10001258/* try to decode a socks5 header */
1259#define SSH_SOCKS5_AUTHDONE 0x1000
1260#define SSH_SOCKS5_NOAUTH 0x00
1261#define SSH_SOCKS5_IPV4 0x01
1262#define SSH_SOCKS5_DOMAIN 0x03
1263#define SSH_SOCKS5_IPV6 0x04
1264#define SSH_SOCKS5_CONNECT 0x01
1265#define SSH_SOCKS5_SUCCESS 0x00
1266
1267static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001268channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
Darren Tucker46471c92003-07-03 13:55:19 +10001269{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001270 /* XXX use get/put_u8 instead of trusting struct padding */
Darren Tucker46471c92003-07-03 13:55:19 +10001271 struct {
1272 u_int8_t version;
1273 u_int8_t command;
1274 u_int8_t reserved;
1275 u_int8_t atyp;
1276 } s5_req, s5_rsp;
1277 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001278 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001279 const u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001280 u_int have, need, i, found, nmethods, addrlen, af;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001281 int r;
Darren Tucker46471c92003-07-03 13:55:19 +10001282
1283 debug2("channel %d: decode socks5", c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001284 p = sshbuf_ptr(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001285 if (p[0] != 0x05)
1286 return -1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001287 have = sshbuf_len(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001288 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1289 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001290 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001291 return 0;
1292 nmethods = p[1];
1293 if (have < nmethods + 2)
1294 return 0;
1295 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001296 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001297 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001298 found = 1;
1299 break;
1300 }
1301 }
1302 if (!found) {
1303 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1304 c->self);
1305 return -1;
1306 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001307 if ((r = sshbuf_consume(input, nmethods + 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001308 fatal("%s: channel %d: consume: %s", __func__,
1309 c->self, ssh_err(r));
1310 }
1311 /* version, method */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001312 if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1313 (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001314 fatal("%s: channel %d: append reply: %s", __func__,
1315 c->self, ssh_err(r));
1316 }
Darren Tucker46471c92003-07-03 13:55:19 +10001317 c->flags |= SSH_SOCKS5_AUTHDONE;
1318 debug2("channel %d: socks5 auth done", c->self);
1319 return 0; /* need more */
1320 }
1321 debug2("channel %d: socks5 post auth", c->self);
1322 if (have < sizeof(s5_req)+1)
1323 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001324 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001325 if (s5_req.version != 0x05 ||
1326 s5_req.command != SSH_SOCKS5_CONNECT ||
1327 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001328 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001329 return -1;
1330 }
Darren Tucker47eede72005-03-14 23:08:12 +11001331 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001332 case SSH_SOCKS5_IPV4:
1333 addrlen = 4;
1334 af = AF_INET;
1335 break;
1336 case SSH_SOCKS5_DOMAIN:
1337 addrlen = p[sizeof(s5_req)];
1338 af = -1;
1339 break;
1340 case SSH_SOCKS5_IPV6:
1341 addrlen = 16;
1342 af = AF_INET6;
1343 break;
1344 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001345 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001346 return -1;
1347 }
Damien Miller0f077072006-07-10 22:21:02 +10001348 need = sizeof(s5_req) + addrlen + 2;
1349 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1350 need++;
1351 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001352 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001353 if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001354 fatal("%s: channel %d: consume: %s", __func__,
1355 c->self, ssh_err(r));
1356 }
1357 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1358 /* host string length */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001359 if ((r = sshbuf_consume(input, 1)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001360 fatal("%s: channel %d: consume: %s", __func__,
1361 c->self, ssh_err(r));
1362 }
1363 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001364 if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1365 (r = sshbuf_get(input, &dest_port, 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001366 debug("channel %d: parse addr/port: %s", c->self, ssh_err(r));
1367 return -1;
1368 }
Darren Tucker46471c92003-07-03 13:55:19 +10001369 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001370 free(c->path);
1371 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001372 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001373 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001374 error("channel %d: dynamic request: socks5 hostname "
1375 "\"%.100s\" too long", c->self, dest_addr);
1376 return -1;
1377 }
1378 c->path = xstrdup(dest_addr);
1379 } else {
1380 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1381 return -1;
1382 c->path = xstrdup(ntop);
1383 }
Darren Tucker46471c92003-07-03 13:55:19 +10001384 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001385
Damien Millerfbdeece2003-09-02 22:52:31 +10001386 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001387 c->self, c->path, c->host_port, s5_req.command);
1388
1389 s5_rsp.version = 0x05;
1390 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1391 s5_rsp.reserved = 0; /* ignored */
1392 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001393 dest_port = 0; /* ignored */
1394
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001395 if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1396 (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1397 (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001398 fatal("%s: channel %d: append reply: %s", __func__,
1399 c->self, ssh_err(r));
Darren Tucker46471c92003-07-03 13:55:19 +10001400 return 1;
1401}
1402
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001403Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001404channel_connect_stdio_fwd(struct ssh *ssh,
1405 const char *host_to_connect, u_short port_to_connect, int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001406{
1407 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001408
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001409 debug("%s %s:%d", __func__, host_to_connect, port_to_connect);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001410
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001411 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001412 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1413 0, "stdio-forward", /*nonblock*/0);
1414
1415 c->path = xstrdup(host_to_connect);
1416 c->host_port = port_to_connect;
1417 c->listening_port = 0;
1418 c->force_drain = 1;
1419
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001420 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1421 port_open_helper(ssh, c, "direct-tcpip");
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001422
1423 return c;
1424}
1425
Ben Lindstromb3921512001-04-11 15:57:50 +00001426/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001427static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001428channel_pre_dynamic(struct ssh *ssh, Channel *c,
1429 fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001430{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001431 const u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001432 u_int have;
1433 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001434
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001435 have = sshbuf_len(c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001436 debug2("channel %d: pre_dynamic: have %d", c->self, have);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001437 /* sshbuf_dump(c->input, stderr); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001438 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001439 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001440 /* need more */
1441 FD_SET(c->sock, readset);
1442 return;
1443 }
1444 /* try to guess the protocol */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001445 p = sshbuf_ptr(c->input);
1446 /* XXX sshbuf_peek_u8? */
Ben Lindstromb3921512001-04-11 15:57:50 +00001447 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001448 case 0x04:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001449 ret = channel_decode_socks4(c, c->input, c->output);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001450 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001451 case 0x05:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001452 ret = channel_decode_socks5(c, c->input, c->output);
Darren Tucker46471c92003-07-03 13:55:19 +10001453 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001454 default:
1455 ret = -1;
1456 break;
1457 }
1458 if (ret < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001459 chan_mark_dead(ssh, c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001460 } else if (ret == 0) {
1461 debug2("channel %d: pre_dynamic: need more", c->self);
1462 /* need more */
1463 FD_SET(c->sock, readset);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001464 if (sshbuf_len(c->output))
1465 FD_SET(c->sock, writeset);
Ben Lindstromb3921512001-04-11 15:57:50 +00001466 } else {
1467 /* switch to the next state */
1468 c->type = SSH_CHANNEL_OPENING;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001469 port_open_helper(ssh, c, "direct-tcpip");
Ben Lindstromb3921512001-04-11 15:57:50 +00001470 }
1471}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001472
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001473/* simulate read-error */
1474static void
1475rdynamic_close(struct ssh *ssh, Channel *c)
1476{
1477 c->type = SSH_CHANNEL_OPEN;
1478 chan_read_failed(ssh, c);
1479 sshbuf_reset(c->input);
1480 chan_ibuf_empty(ssh, c);
1481 sshbuf_reset(c->output);
1482 chan_write_failed(ssh, c);
1483}
1484
1485/* reverse dynamic port forwarding */
1486static void
1487channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c)
1488{
1489 const u_char *p;
1490 u_int have, len;
1491 int r, ret;
1492
1493 have = sshbuf_len(c->output);
1494 debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1495 /* sshbuf_dump(c->output, stderr); */
1496 /* EOF received */
1497 if (c->flags & CHAN_EOF_RCVD) {
1498 if ((r = sshbuf_consume(c->output, have)) != 0) {
1499 fatal("%s: channel %d: consume: %s",
1500 __func__, c->self, ssh_err(r));
1501 }
1502 rdynamic_close(ssh, c);
1503 return;
1504 }
1505 /* check if the fixed size part of the packet is in buffer. */
1506 if (have < 3)
1507 return;
1508 /* try to guess the protocol */
1509 p = sshbuf_ptr(c->output);
1510 switch (p[0]) {
1511 case 0x04:
1512 /* switch input/output for reverse forwarding */
1513 ret = channel_decode_socks4(c, c->output, c->input);
1514 break;
1515 case 0x05:
1516 ret = channel_decode_socks5(c, c->output, c->input);
1517 break;
1518 default:
1519 ret = -1;
1520 break;
1521 }
1522 if (ret < 0) {
1523 rdynamic_close(ssh, c);
1524 } else if (ret == 0) {
1525 debug2("channel %d: pre_rdynamic: need more", c->self);
1526 /* send socks request to peer */
1527 len = sshbuf_len(c->input);
1528 if (len > 0 && len < c->remote_window) {
1529 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1530 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1531 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1532 (r = sshpkt_send(ssh)) != 0) {
1533 fatal("%s: channel %i: rdynamic: %s", __func__,
1534 c->self, ssh_err(r));
1535 }
1536 if ((r = sshbuf_consume(c->input, len)) != 0) {
1537 fatal("%s: channel %d: consume: %s",
1538 __func__, c->self, ssh_err(r));
1539 }
1540 c->remote_window -= len;
1541 }
1542 } else if (rdynamic_connect_finish(ssh, c) < 0) {
1543 /* the connect failed */
1544 rdynamic_close(ssh, c);
1545 }
1546}
1547
Damien Millerb38eff82000-04-01 11:09:21 +10001548/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001549static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001550channel_post_x11_listener(struct ssh *ssh, Channel *c,
1551 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001552{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001553 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001554 struct sockaddr_storage addr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001555 int r, newsock, oerrno, remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001556 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001557 char buf[16384], *remote_ipaddr;
Damien Millerb38eff82000-04-01 11:09:21 +10001558
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001559 if (!FD_ISSET(c->sock, readset))
1560 return;
Damien Millerbd483e72000-04-30 10:00:53 +10001561
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001562 debug("X11 connection requested.");
1563 addrlen = sizeof(addr);
1564 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1565 if (c->single_connection) {
1566 oerrno = errno;
1567 debug2("single_connection: closing X11 listener.");
1568 channel_close_fd(ssh, &c->sock);
1569 chan_mark_dead(ssh, c);
1570 errno = oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001571 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001572 if (newsock < 0) {
1573 if (errno != EINTR && errno != EWOULDBLOCK &&
1574 errno != ECONNABORTED)
1575 error("accept: %.100s", strerror(errno));
1576 if (errno == EMFILE || errno == ENFILE)
1577 c->notbefore = monotime() + 1;
1578 return;
1579 }
1580 set_nodelay(newsock);
1581 remote_ipaddr = get_peer_ipaddr(newsock);
1582 remote_port = get_peer_port(newsock);
1583 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1584 remote_ipaddr, remote_port);
1585
1586 nc = channel_new(ssh, "accepted x11 socket",
1587 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1588 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1589 open_preamble(ssh, __func__, nc, "x11");
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001590 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1591 (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001592 fatal("%s: channel %i: reply %s", __func__,
1593 c->self, ssh_err(r));
1594 }
1595 if ((r = sshpkt_send(ssh)) != 0)
1596 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
1597 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001598}
1599
Ben Lindstrombba81212001-06-25 05:01:22 +00001600static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001601port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001602{
Damien Miller4def1842013-12-29 17:45:26 +11001603 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001604 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001605 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001606 int remote_port = get_peer_port(c->sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001607 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001608
Damien Millerd6369432010-02-02 17:02:07 +11001609 if (remote_port == -1) {
1610 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001611 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001612 remote_ipaddr = xstrdup("127.0.0.1");
1613 remote_port = 65535;
1614 }
1615
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001616 free(c->remote_name);
1617 xasprintf(&c->remote_name,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001618 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001619 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001620 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001621 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001622
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001623 open_preamble(ssh, __func__, c, rtype);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001624 if (strcmp(rtype, "direct-tcpip") == 0) {
1625 /* target host, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001626 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1627 (r = sshpkt_put_u32(ssh, c->host_port)) != 0) {
1628 fatal("%s: channel %i: reply %s", __func__,
1629 c->self, ssh_err(r));
1630 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001631 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1632 /* target path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001633 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1634 fatal("%s: channel %i: reply %s", __func__,
1635 c->self, ssh_err(r));
1636 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001637 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1638 /* listen path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001639 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1640 fatal("%s: channel %i: reply %s", __func__,
1641 c->self, ssh_err(r));
1642 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001643 } else {
1644 /* listen address, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001645 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1646 (r = sshpkt_put_u32(ssh, local_port)) != 0) {
1647 fatal("%s: channel %i: reply %s", __func__,
1648 c->self, ssh_err(r));
1649 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001650 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001651 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1652 /* reserved for future owner/mode info */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001653 if ((r = sshpkt_put_cstring(ssh, "")) != 0) {
1654 fatal("%s: channel %i: reply %s", __func__,
1655 c->self, ssh_err(r));
1656 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001657 } else {
1658 /* originator host and port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001659 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1660 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) {
1661 fatal("%s: channel %i: reply %s", __func__,
1662 c->self, ssh_err(r));
1663 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001664 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001665 if ((r = sshpkt_send(ssh)) != 0)
1666 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001667 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001668 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001669}
1670
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001671void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001672channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001673{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001674 ssh->chanctxt->x11_refuse_time = refuse_time;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001675}
1676
Damien Millerb38eff82000-04-01 11:09:21 +10001677/*
1678 * This socket is listening for connections to a forwarded TCP/IP port.
1679 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001680static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001681channel_post_port_listener(struct ssh *ssh, Channel *c,
1682 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001683{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001684 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001685 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001686 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001687 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001688 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001689
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001690 if (!FD_ISSET(c->sock, readset))
1691 return;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001692
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001693 debug("Connection to port %d forwarding to %.100s port %d requested.",
1694 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001695
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001696 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1697 nextstate = SSH_CHANNEL_OPENING;
1698 rtype = "forwarded-tcpip";
1699 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1700 nextstate = SSH_CHANNEL_OPENING;
1701 rtype = "forwarded-streamlocal@openssh.com";
1702 } else if (c->host_port == PORT_STREAMLOCAL) {
1703 nextstate = SSH_CHANNEL_OPENING;
1704 rtype = "direct-streamlocal@openssh.com";
1705 } else if (c->host_port == 0) {
1706 nextstate = SSH_CHANNEL_DYNAMIC;
1707 rtype = "dynamic-tcpip";
1708 } else {
1709 nextstate = SSH_CHANNEL_OPENING;
1710 rtype = "direct-tcpip";
Damien Millerb38eff82000-04-01 11:09:21 +10001711 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001712
1713 addrlen = sizeof(addr);
1714 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1715 if (newsock < 0) {
1716 if (errno != EINTR && errno != EWOULDBLOCK &&
1717 errno != ECONNABORTED)
1718 error("accept: %.100s", strerror(errno));
1719 if (errno == EMFILE || errno == ENFILE)
1720 c->notbefore = monotime() + 1;
1721 return;
1722 }
1723 if (c->host_port != PORT_STREAMLOCAL)
1724 set_nodelay(newsock);
1725 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1726 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1727 nc->listening_port = c->listening_port;
1728 nc->host_port = c->host_port;
1729 if (c->path != NULL)
1730 nc->path = xstrdup(c->path);
1731
1732 if (nextstate != SSH_CHANNEL_DYNAMIC)
1733 port_open_helper(ssh, nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001734}
1735
1736/*
1737 * This is the authentication agent socket listening for connections from
1738 * clients.
1739 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001740static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001741channel_post_auth_listener(struct ssh *ssh, Channel *c,
1742 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001743{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001744 Channel *nc;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001745 int r, newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001746 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001747 socklen_t addrlen;
1748
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001749 if (!FD_ISSET(c->sock, readset))
1750 return;
1751
1752 addrlen = sizeof(addr);
1753 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1754 if (newsock < 0) {
1755 error("accept from auth socket: %.100s", strerror(errno));
1756 if (errno == EMFILE || errno == ENFILE)
1757 c->notbefore = monotime() + 1;
1758 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001759 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001760 nc = channel_new(ssh, "accepted auth socket",
1761 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1762 c->local_window_max, c->local_maxpacket,
1763 0, "accepted auth socket", 1);
1764 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1765 if ((r = sshpkt_send(ssh)) != 0)
1766 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001767}
1768
Ben Lindstrombba81212001-06-25 05:01:22 +00001769static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001770channel_post_connecting(struct ssh *ssh, Channel *c,
1771 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001772{
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001773 int err = 0, sock, isopen, r;
Ben Lindstrom11180952001-07-04 05:13:35 +00001774 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001775
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001776 if (!FD_ISSET(c->sock, writeset))
1777 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001778 if (!c->have_remote_id)
1779 fatal(":%s: channel %d: no remote id", __func__, c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001780 /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1781 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001782 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1783 err = errno;
1784 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001785 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001786 if (err == 0) {
1787 debug("channel %d: connected to %s port %d",
1788 c->self, c->connect_ctx.host, c->connect_ctx.port);
1789 channel_connect_ctx_free(&c->connect_ctx);
1790 c->type = SSH_CHANNEL_OPEN;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001791 if (isopen) {
1792 /* no message necessary */
1793 } else {
1794 if ((r = sshpkt_start(ssh,
1795 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1796 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1797 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1798 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1799 (r = sshpkt_put_u32(ssh, c->local_maxpacket))
1800 != 0)
1801 fatal("%s: channel %i: confirm: %s", __func__,
1802 c->self, ssh_err(r));
1803 if ((r = sshpkt_send(ssh)) != 0)
1804 fatal("%s: channel %i: %s", __func__, c->self,
1805 ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001806 }
1807 } else {
1808 debug("channel %d: connection failed: %s",
1809 c->self, strerror(err));
1810 /* Try next address, if any */
1811 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1812 close(c->sock);
1813 c->sock = c->rfd = c->wfd = sock;
1814 channel_find_maxfd(ssh->chanctxt);
1815 return;
1816 }
1817 /* Exhausted all addresses */
1818 error("connect_to %.100s port %d: failed.",
1819 c->connect_ctx.host, c->connect_ctx.port);
1820 channel_connect_ctx_free(&c->connect_ctx);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001821 if (isopen) {
1822 rdynamic_close(ssh, c);
1823 } else {
1824 if ((r = sshpkt_start(ssh,
1825 SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1826 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001827 (r = sshpkt_put_u32(ssh,
1828 SSH2_OPEN_CONNECT_FAILED)) != 0 ||
1829 (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1830 (r = sshpkt_put_cstring(ssh, "")) != 0) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001831 fatal("%s: channel %i: failure: %s", __func__,
1832 c->self, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001833 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001834 if ((r = sshpkt_send(ssh)) != 0)
1835 fatal("%s: channel %i: %s", __func__, c->self,
1836 ssh_err(r));
1837 chan_mark_dead(ssh, c);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001838 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001839 }
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001840}
1841
Ben Lindstrombba81212001-06-25 05:01:22 +00001842static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001843channel_handle_rfd(struct ssh *ssh, Channel *c,
1844 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001845{
Darren Tucker11327cc2005-03-14 23:22:25 +11001846 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001847 ssize_t len;
1848 int r, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001849
Damien Miller835284b2007-06-11 13:03:16 +10001850 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001851
1852 if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset)))
1853 return 1;
1854
1855 errno = 0;
1856 len = read(c->rfd, buf, sizeof(buf));
1857 if (len < 0 && (errno == EINTR ||
1858 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1859 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001860#ifndef PTY_ZEROREAD
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001861 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001862#else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001863 if ((!c->isatty && len <= 0) ||
1864 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001865#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001866 debug2("channel %d: read<=0 rfd %d len %zd",
1867 c->self, c->rfd, len);
1868 if (c->type != SSH_CHANNEL_OPEN) {
1869 debug2("channel %d: not open", c->self);
1870 chan_mark_dead(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001871 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001872 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001873 chan_read_failed(ssh, c);
Damien Millerad833b32000-08-23 10:46:23 +10001874 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001875 return -1;
1876 }
1877 if (c->input_filter != NULL) {
1878 if (c->input_filter(ssh, c, buf, len) == -1) {
1879 debug2("channel %d: filter stops", c->self);
1880 chan_read_failed(ssh, c);
1881 }
1882 } else if (c->datagram) {
1883 if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1884 fatal("%s: channel %d: put datagram: %s", __func__,
1885 c->self, ssh_err(r));
1886 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
1887 fatal("%s: channel %d: put data: %s", __func__,
1888 c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001889 }
1890 return 1;
1891}
Damien Miller4f7becb2006-03-26 14:10:14 +11001892
Ben Lindstrombba81212001-06-25 05:01:22 +00001893static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001894channel_handle_wfd(struct ssh *ssh, Channel *c,
1895 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001896{
Ben Lindstrome229b252001-03-05 06:28:06 +00001897 struct termios tio;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001898 u_char *data = NULL, *buf; /* XXX const; need filter API change */
1899 size_t dlen, olen = 0;
1900 int r, len;
1901
1902 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
1903 sshbuf_len(c->output) == 0)
1904 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001905
1906 /* Send buffered output data to the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001907 olen = sshbuf_len(c->output);
1908 if (c->output_filter != NULL) {
1909 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
1910 debug2("channel %d: filter stops", c->self);
1911 if (c->type != SSH_CHANNEL_OPEN)
1912 chan_mark_dead(ssh, c);
1913 else
1914 chan_write_failed(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001915 return -1;
1916 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001917 } else if (c->datagram) {
1918 if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
1919 fatal("%s: channel %d: get datagram: %s", __func__,
1920 c->self, ssh_err(r));
djm@openbsd.org9145a732017-09-12 07:32:04 +00001921 buf = data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001922 } else {
1923 buf = data = sshbuf_mutable_ptr(c->output);
1924 dlen = sshbuf_len(c->output);
1925 }
1926
1927 if (c->datagram) {
1928 /* ignore truncated writes, datagrams might get lost */
djm@openbsd.orgc704f642017-09-24 09:50:01 +00001929 len = write(c->wfd, buf, dlen);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001930 free(data);
1931 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1932 errno == EWOULDBLOCK))
1933 return 1;
1934 if (len <= 0)
1935 goto write_fail;
1936 goto out;
1937 }
1938
1939#ifdef _AIX
1940 /* XXX: Later AIX versions can't push as much data to tty */
1941 if (c->wfd_isatty)
1942 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10001943#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001944
1945 len = write(c->wfd, buf, dlen);
1946 if (len < 0 &&
1947 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
1948 return 1;
1949 if (len <= 0) {
1950 write_fail:
1951 if (c->type != SSH_CHANNEL_OPEN) {
1952 debug2("channel %d: not open", c->self);
1953 chan_mark_dead(ssh, c);
1954 return -1;
1955 } else {
1956 chan_write_failed(ssh, c);
1957 }
1958 return -1;
1959 }
1960#ifndef BROKEN_TCGETATTR_ICANON
1961 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
1962 if (tcgetattr(c->wfd, &tio) == 0 &&
1963 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1964 /*
1965 * Simulate echo to reduce the impact of
1966 * traffic analysis. We need to match the
1967 * size of a SSH2_MSG_CHANNEL_DATA message
1968 * (4 byte channel id + buf)
1969 */
1970 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
1971 (r = sshpkt_send(ssh)) != 0)
1972 fatal("%s: channel %d: ignore: %s",
1973 __func__, c->self, ssh_err(r));
1974 }
1975 }
1976#endif /* BROKEN_TCGETATTR_ICANON */
1977 if ((r = sshbuf_consume(c->output, len)) != 0) {
1978 fatal("%s: channel %d: consume: %s",
1979 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001980 }
Damien Miller7d457182010-08-05 23:09:48 +10001981 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001982 c->local_consumed += olen - sshbuf_len(c->output);
1983
Damien Miller33b13562000-04-04 14:38:59 +10001984 return 1;
1985}
Damien Miller4f7becb2006-03-26 14:10:14 +11001986
Ben Lindstrombba81212001-06-25 05:01:22 +00001987static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001988channel_handle_efd_write(struct ssh *ssh, Channel *c,
1989 fd_set *readset, fd_set *writeset)
1990{
1991 int r;
1992 ssize_t len;
1993
1994 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
1995 return 1;
1996
1997 len = write(c->efd, sshbuf_ptr(c->extended),
1998 sshbuf_len(c->extended));
1999 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
2000 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
2001 errno == EWOULDBLOCK))
2002 return 1;
2003 if (len <= 0) {
2004 debug2("channel %d: closing write-efd %d", c->self, c->efd);
2005 channel_close_fd(ssh, &c->efd);
2006 } else {
2007 if ((r = sshbuf_consume(c->extended, len)) != 0) {
2008 fatal("%s: channel %d: consume: %s",
2009 __func__, c->self, ssh_err(r));
2010 }
2011 c->local_consumed += len;
2012 }
2013 return 1;
2014}
2015
2016static int
2017channel_handle_efd_read(struct ssh *ssh, Channel *c,
2018 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002019{
Darren Tucker11327cc2005-03-14 23:22:25 +11002020 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002021 int r;
2022 ssize_t len;
Damien Miller33b13562000-04-04 14:38:59 +10002023
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002024 if (!c->detach_close && !FD_ISSET(c->efd, readset))
2025 return 1;
2026
2027 len = read(c->efd, buf, sizeof(buf));
2028 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
2029 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
2030 errno == EWOULDBLOCK) && !c->detach_close)))
2031 return 1;
2032 if (len <= 0) {
2033 debug2("channel %d: closing read-efd %d",
2034 c->self, c->efd);
2035 channel_close_fd(ssh, &c->efd);
2036 } else {
2037 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
2038 debug3("channel %d: discard efd",
2039 c->self);
2040 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
2041 fatal("%s: channel %d: append: %s",
2042 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002043 }
2044 }
2045 return 1;
2046}
Damien Miller4f7becb2006-03-26 14:10:14 +11002047
Damien Miller0e220db2004-06-15 10:34:08 +10002048static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002049channel_handle_efd(struct ssh *ssh, Channel *c,
2050 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002051{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002052 if (c->efd == -1)
2053 return 1;
2054
2055 /** XXX handle drain efd, too */
2056
2057 if (c->extended_usage == CHAN_EXTENDED_WRITE)
2058 return channel_handle_efd_write(ssh, c, readset, writeset);
2059 else if (c->extended_usage == CHAN_EXTENDED_READ ||
2060 c->extended_usage == CHAN_EXTENDED_IGNORE)
2061 return channel_handle_efd_read(ssh, c, readset, writeset);
2062
2063 return 1;
2064}
2065
2066static int
2067channel_check_window(struct ssh *ssh, Channel *c)
2068{
2069 int r;
2070
Ben Lindstromb3921512001-04-11 15:57:50 +00002071 if (c->type == SSH_CHANNEL_OPEN &&
2072 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10002073 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10002074 c->local_maxpacket*3) ||
2075 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10002076 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00002077 if (!c->have_remote_id)
2078 fatal(":%s: channel %d: no remote id",
2079 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002080 if ((r = sshpkt_start(ssh,
2081 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2082 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2083 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2084 (r = sshpkt_send(ssh)) != 0) {
2085 fatal("%s: channel %i: %s", __func__,
2086 c->self, ssh_err(r));
2087 }
Damien Millerd3444942000-09-30 14:20:03 +11002088 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002089 c->self, c->local_window,
2090 c->local_consumed);
2091 c->local_window += c->local_consumed;
2092 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002093 }
2094 return 1;
2095}
2096
Ben Lindstrombba81212001-06-25 05:01:22 +00002097static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002098channel_post_open(struct ssh *ssh, Channel *c,
2099 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002100{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002101 channel_handle_rfd(ssh, c, readset, writeset);
2102 channel_handle_wfd(ssh, c, readset, writeset);
2103 channel_handle_efd(ssh, c, readset, writeset);
2104 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002105}
2106
Damien Millere1537f92010-01-26 13:26:22 +11002107static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002108read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002109{
2110 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002111 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002112 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002113 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002114
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002115 if (sshbuf_len(c->input) < need) {
2116 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002117 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002118 if (len < 0 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002119 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002120 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002121 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002122 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002123 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002124 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002125 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2126 fatal("%s: channel %d: append: %s",
2127 __func__, c->self, ssh_err(r));
2128 }
Damien Millere1537f92010-01-26 13:26:22 +11002129 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002130 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002131}
2132
2133static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002134channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2135 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002136{
2137 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002138
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002139 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2140 return;
2141 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2142 return;
2143 if (c->mux_pause)
2144 return;
2145
2146 /*
2147 * Don't not read past the precise end of packets to
2148 * avoid disrupting fd passing.
2149 */
2150 if (read_mux(ssh, c, 4) < 4) /* read header */
2151 return;
2152 /* XXX sshbuf_peek_u32 */
2153 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002154#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002155 if (need > CHANNEL_MUX_MAX_PACKET) {
2156 debug2("channel %d: packet too big %u > %u",
2157 c->self, CHANNEL_MUX_MAX_PACKET, need);
2158 chan_rcvd_oclose(ssh, c);
2159 return;
Damien Millere1537f92010-01-26 13:26:22 +11002160 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002161 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2162 return;
2163 if (c->mux_rcb(ssh, c) != 0) {
2164 debug("channel %d: mux_rcb failed", c->self);
2165 chan_mark_dead(ssh, c);
2166 return;
Damien Millere1537f92010-01-26 13:26:22 +11002167 }
2168}
2169
2170static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002171channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2172 fd_set *readset, fd_set *writeset)
2173{
2174 ssize_t len;
2175 int r;
2176
2177 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2178 sshbuf_len(c->output) == 0)
2179 return;
2180
2181 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2182 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2183 return;
2184 if (len <= 0) {
2185 chan_mark_dead(ssh, c);
2186 return;
2187 }
2188 if ((r = sshbuf_consume(c->output, len)) != 0)
2189 fatal("%s: channel %d: consume: %s", __func__,
2190 c->self, ssh_err(r));
2191}
2192
2193static void
2194channel_post_mux_client(struct ssh *ssh, Channel *c,
2195 fd_set *readset, fd_set *writeset)
2196{
2197 channel_post_mux_client_read(ssh, c, readset, writeset);
2198 channel_post_mux_client_write(ssh, c, readset, writeset);
2199}
2200
2201static void
2202channel_post_mux_listener(struct ssh *ssh, Channel *c,
2203 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002204{
2205 Channel *nc;
2206 struct sockaddr_storage addr;
2207 socklen_t addrlen;
2208 int newsock;
2209 uid_t euid;
2210 gid_t egid;
2211
2212 if (!FD_ISSET(c->sock, readset))
2213 return;
2214
2215 debug("multiplexing control connection");
2216
2217 /*
2218 * Accept connection on control socket
2219 */
2220 memset(&addr, 0, sizeof(addr));
2221 addrlen = sizeof(addr);
2222 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2223 &addrlen)) == -1) {
2224 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002225 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002226 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002227 return;
2228 }
2229
2230 if (getpeereid(newsock, &euid, &egid) < 0) {
2231 error("%s getpeereid failed: %s", __func__,
2232 strerror(errno));
2233 close(newsock);
2234 return;
2235 }
2236 if ((euid != 0) && (getuid() != euid)) {
2237 error("multiplex uid mismatch: peer euid %u != uid %u",
2238 (u_int)euid, (u_int)getuid());
2239 close(newsock);
2240 return;
2241 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002242 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002243 newsock, newsock, -1, c->local_window_max,
2244 c->local_maxpacket, 0, "mux-control", 1);
2245 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002246 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002247 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002248 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002249 /* mux state transitions must not elicit protocol messages */
2250 nc->flags |= CHAN_LOCAL;
2251}
2252
Ben Lindstrombba81212001-06-25 05:01:22 +00002253static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002254channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002255{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002256 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002257
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002258 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2259 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2260 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002261
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002262 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2263 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2264 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2265 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2266 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2267 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2268 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2269 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2270 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2271 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002272 pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002273 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2274 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2275
2276 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2277 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2278 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2279 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2280 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2281 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2282 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2283 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2284 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002285 post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002286 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2287 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2288
2289 sc->channel_pre = pre;
2290 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002291}
2292
Damien Miller3ec27592001-10-12 11:35:04 +10002293/* gc dead channels */
2294static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002295channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002296{
2297 if (c == NULL)
2298 return;
2299 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002300 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002301 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002302 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002303 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002304 /* if we still have a callback */
2305 if (c->detach_user != NULL)
2306 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002307 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002308 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002309 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002310 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002311 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002312 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002313}
2314
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002315enum channel_table { CHAN_PRE, CHAN_POST };
2316
Ben Lindstrombba81212001-06-25 05:01:22 +00002317static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002318channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002319 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002320{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002321 struct ssh_channels *sc = ssh->chanctxt;
2322 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002323 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002324 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002325 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002326
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002327 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002328 if (unpause_secs != NULL)
2329 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002330 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2331 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002332 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002333 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002334 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002335 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002336 c->delayed = 0;
2337 else
2338 continue;
2339 }
Damien Millera6508752012-04-22 11:21:10 +10002340 if (ftab[c->type] != NULL) {
2341 /*
2342 * Run handlers that are not paused.
2343 */
2344 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002345 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002346 else if (unpause_secs != NULL) {
2347 /*
2348 * Collect the time that the earliest
2349 * channel comes off pause.
2350 */
2351 debug3("%s: chan %d: skip for %d more seconds",
2352 __func__, c->self,
2353 (int)(c->notbefore - now));
2354 if (*unpause_secs == 0 ||
2355 (c->notbefore - now) < *unpause_secs)
2356 *unpause_secs = c->notbefore - now;
2357 }
2358 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002359 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002360 }
Damien Millera6508752012-04-22 11:21:10 +10002361 if (unpause_secs != NULL && *unpause_secs != 0)
2362 debug3("%s: first channel unpauses in %d seconds",
2363 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002364}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002365
Ben Lindstrome9c99912001-06-09 00:41:05 +00002366/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002367 * Create sockets before allocating the select bitmasks.
2368 * This is necessary for things that need to happen after reading
2369 * the network-input but before channel_prepare_select().
2370 */
2371static void
2372channel_before_prepare_select(struct ssh *ssh)
2373{
2374 struct ssh_channels *sc = ssh->chanctxt;
2375 Channel *c;
2376 u_int i, oalloc;
2377
2378 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2379 c = sc->channels[i];
2380 if (c == NULL)
2381 continue;
2382 if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
2383 channel_before_prepare_select_rdynamic(ssh, c);
2384 }
2385}
2386
2387/*
Ben Lindstrome9c99912001-06-09 00:41:05 +00002388 * Allocate/update select bitmasks and add any bits relevant to channels in
2389 * select bitmasks.
2390 */
Damien Miller4af51302000-04-16 11:18:38 +10002391void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002392channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2393 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002394{
Damien Miller36812092006-03-26 14:22:47 +11002395 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002396
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002397 channel_before_prepare_select(ssh); /* might update channel_max_fd */
2398
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002399 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002400
Damien Miller36812092006-03-26 14:22:47 +11002401 nfdset = howmany(n+1, NFDBITS);
2402 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002403 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002404 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2405 sz = nfdset * sizeof(fd_mask);
2406
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002407 /* perhaps check sz < nalloc/2 and shrink? */
2408 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002409 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2410 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002411 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002412 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002413 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002414 memset(*readsetp, 0, sz);
2415 memset(*writesetp, 0, sz);
2416
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002417 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002418 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002419 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002420}
2421
Ben Lindstrome9c99912001-06-09 00:41:05 +00002422/*
2423 * After select, perform any appropriate operations for channels which have
2424 * events pending.
2425 */
Damien Miller4af51302000-04-16 11:18:38 +10002426void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002427channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002428{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002429 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002430}
2431
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002432/*
2433 * Enqueue data for channels with open or draining c->input.
2434 */
2435static void
2436channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2437{
djm@openbsd.org55486f52017-09-23 22:04:07 +00002438 size_t len, plen;
2439 const u_char *pkt;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002440 int r;
2441
2442 if ((len = sshbuf_len(c->input)) == 0) {
2443 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2444 /*
2445 * input-buffer is empty and read-socket shutdown:
2446 * tell peer, that we will not send more data:
2447 * send IEOF.
2448 * hack for extended data: delay EOF if EFD still
2449 * in use.
2450 */
2451 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2452 debug2("channel %d: "
2453 "ibuf_empty delayed efd %d/(%zu)",
2454 c->self, c->efd, sshbuf_len(c->extended));
2455 else
2456 chan_ibuf_empty(ssh, c);
2457 }
2458 return;
2459 }
2460
djm@openbsd.org9f532292017-09-12 06:35:31 +00002461 if (!c->have_remote_id)
2462 fatal(":%s: channel %d: no remote id", __func__, c->self);
2463
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002464 if (c->datagram) {
2465 /* Check datagram will fit; drop if not */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002466 if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
2467 fatal("%s: channel %d: get datagram: %s", __func__,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002468 c->self, ssh_err(r));
2469 /*
2470 * XXX this does tail-drop on the datagram queue which is
2471 * usually suboptimal compared to head-drop. Better to have
2472 * backpressure at read time? (i.e. read + discard)
2473 */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002474 if (plen > c->remote_window || plen > c->remote_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002475 debug("channel %d: datagram too big", c->self);
2476 return;
2477 }
2478 /* Enqueue it */
2479 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2480 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org55486f52017-09-23 22:04:07 +00002481 (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002482 (r = sshpkt_send(ssh)) != 0) {
2483 fatal("%s: channel %i: datagram: %s", __func__,
2484 c->self, ssh_err(r));
2485 }
djm@openbsd.org55486f52017-09-23 22:04:07 +00002486 c->remote_window -= plen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002487 return;
2488 }
2489
2490 /* Enqueue packet for buffered data. */
2491 if (len > c->remote_window)
2492 len = c->remote_window;
2493 if (len > c->remote_maxpacket)
2494 len = c->remote_maxpacket;
2495 if (len == 0)
2496 return;
2497 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2498 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2499 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2500 (r = sshpkt_send(ssh)) != 0) {
2501 fatal("%s: channel %i: data: %s", __func__,
2502 c->self, ssh_err(r));
2503 }
2504 if ((r = sshbuf_consume(c->input, len)) != 0)
2505 fatal("%s: channel %i: consume: %s", __func__,
2506 c->self, ssh_err(r));
2507 c->remote_window -= len;
2508}
2509
2510/*
2511 * Enqueue data for channels with open c->extended in read mode.
2512 */
2513static void
2514channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2515{
2516 size_t len;
2517 int r;
2518
2519 if ((len = sshbuf_len(c->extended)) == 0)
2520 return;
2521
2522 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2523 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2524 if (len > c->remote_window)
2525 len = c->remote_window;
2526 if (len > c->remote_maxpacket)
2527 len = c->remote_maxpacket;
2528 if (len == 0)
2529 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002530 if (!c->have_remote_id)
2531 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002532 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2533 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2534 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2535 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2536 (r = sshpkt_send(ssh)) != 0) {
2537 fatal("%s: channel %i: data: %s", __func__,
2538 c->self, ssh_err(r));
2539 }
2540 if ((r = sshbuf_consume(c->extended, len)) != 0)
2541 fatal("%s: channel %i: consume: %s", __func__,
2542 c->self, ssh_err(r));
2543 c->remote_window -= len;
2544 debug2("channel %d: sent ext data %zu", c->self, len);
2545}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002546
Damien Miller5e953212001-01-30 09:14:00 +11002547/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002548void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002549channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002550{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002551 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002552 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002553 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002554
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002555 for (i = 0; i < sc->channels_alloc; i++) {
2556 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002557 if (c == NULL)
2558 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002559
Ben Lindstrome9c99912001-06-09 00:41:05 +00002560 /*
2561 * We are only interested in channels that can have buffered
2562 * incoming data.
2563 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002564 if (c->type != SSH_CHANNEL_OPEN)
2565 continue;
2566 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002567 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002568 debug3("channel %d: will not send data after close",
2569 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002570 continue;
2571 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002572
Damien Miller95def091999-11-25 00:26:21 +11002573 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002574 if (c->istate == CHAN_INPUT_OPEN ||
2575 c->istate == CHAN_INPUT_WAIT_DRAIN)
2576 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002577 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002578 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002579 c->extended_usage == CHAN_EXTENDED_READ)
2580 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002582}
2583
markus@openbsd.org8d057842016-09-30 09:19:13 +00002584/* -- mux proxy support */
2585
2586/*
2587 * When multiplexing channel messages for mux clients we have to deal
2588 * with downstream messages from the mux client and upstream messages
2589 * from the ssh server:
2590 * 1) Handling downstream messages is straightforward and happens
2591 * in channel_proxy_downstream():
2592 * - We forward all messages (mostly) unmodified to the server.
2593 * - However, in order to route messages from upstream to the correct
2594 * downstream client, we have to replace the channel IDs used by the
2595 * mux clients with a unique channel ID because the mux clients might
2596 * use conflicting channel IDs.
2597 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2598 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2599 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2600 * with the newly allocated channel ID.
2601 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2602 * channels and procesed by channel_proxy_upstream(). The local channel ID
2603 * is then translated back to the original mux client ID.
2604 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2605 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2606 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2607 * downstream mux client are removed.
2608 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2609 * requires more work, because they are not addressed to a specific
2610 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2611 * out whether the request is addressed to the local client or a
2612 * specific downstream client based on the listen-address/port.
2613 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2614 * not supported as the matching session/channel cannot be identified
2615 * easily.
2616 */
2617
2618/*
2619 * receive packets from downstream mux clients:
2620 * channel callback fired on read from mux client, creates
2621 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2622 * on channel creation.
2623 */
2624int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002625channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002626{
2627 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002628 struct sshbuf *original = NULL, *modified = NULL;
2629 const u_char *cp;
2630 char *ctype = NULL, *listen_host = NULL;
2631 u_char type;
2632 size_t have;
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002633 int ret = -1, r;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002634 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002635
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002636 /* sshbuf_dump(downstream->input, stderr); */
2637 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002638 != 0) {
2639 error("%s: malformed message: %s", __func__, ssh_err(r));
2640 return -1;
2641 }
2642 if (have < 2) {
2643 error("%s: short message", __func__);
2644 return -1;
2645 }
2646 type = cp[1];
2647 /* skip padlen + type */
2648 cp += 2;
2649 have -= 2;
2650 if (ssh_packet_log_type(type))
2651 debug3("%s: channel %u: down->up: type %u", __func__,
2652 downstream->self, type);
2653
2654 switch (type) {
2655 case SSH2_MSG_CHANNEL_OPEN:
2656 if ((original = sshbuf_from(cp, have)) == NULL ||
2657 (modified = sshbuf_new()) == NULL) {
2658 error("%s: alloc", __func__);
2659 goto out;
2660 }
2661 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2662 (r = sshbuf_get_u32(original, &id)) != 0) {
2663 error("%s: parse error %s", __func__, ssh_err(r));
2664 goto out;
2665 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002666 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002667 -1, -1, -1, 0, 0, 0, ctype, 1);
2668 c->mux_ctx = downstream; /* point to mux client */
2669 c->mux_downstream_id = id; /* original downstream id */
2670 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2671 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2672 (r = sshbuf_putb(modified, original)) != 0) {
2673 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002674 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002675 goto out;
2676 }
2677 break;
2678 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2679 /*
2680 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2681 * need to parse 'remote_id' instead of 'ctype'.
2682 */
2683 if ((original = sshbuf_from(cp, have)) == NULL ||
2684 (modified = sshbuf_new()) == NULL) {
2685 error("%s: alloc", __func__);
2686 goto out;
2687 }
2688 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2689 (r = sshbuf_get_u32(original, &id)) != 0) {
2690 error("%s: parse error %s", __func__, ssh_err(r));
2691 goto out;
2692 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002693 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002694 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2695 c->mux_ctx = downstream; /* point to mux client */
2696 c->mux_downstream_id = id;
2697 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002698 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002699 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2700 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2701 (r = sshbuf_putb(modified, original)) != 0) {
2702 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002703 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002704 goto out;
2705 }
2706 break;
2707 case SSH2_MSG_GLOBAL_REQUEST:
2708 if ((original = sshbuf_from(cp, have)) == NULL) {
2709 error("%s: alloc", __func__);
2710 goto out;
2711 }
2712 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2713 error("%s: parse error %s", __func__, ssh_err(r));
2714 goto out;
2715 }
2716 if (strcmp(ctype, "tcpip-forward") != 0) {
2717 error("%s: unsupported request %s", __func__, ctype);
2718 goto out;
2719 }
2720 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2721 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2722 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2723 error("%s: parse error %s", __func__, ssh_err(r));
2724 goto out;
2725 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002726 if (listen_port > 65535) {
2727 error("%s: tcpip-forward for %s: bad port %u",
2728 __func__, listen_host, listen_port);
2729 goto out;
2730 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002731 /* Record that connection to this host/port is permitted. */
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002732 fwd_perm_list_add(ssh, FWDPERM_USER, "<mux>", -1,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002733 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002734 listen_host = NULL;
2735 break;
2736 case SSH2_MSG_CHANNEL_CLOSE:
2737 if (have < 4)
2738 break;
2739 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002740 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002741 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002742 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002743 else
2744 c->flags |= CHAN_CLOSE_SENT;
2745 }
2746 break;
2747 }
2748 if (modified) {
2749 if ((r = sshpkt_start(ssh, type)) != 0 ||
2750 (r = sshpkt_putb(ssh, modified)) != 0 ||
2751 (r = sshpkt_send(ssh)) != 0) {
2752 error("%s: send %s", __func__, ssh_err(r));
2753 goto out;
2754 }
2755 } else {
2756 if ((r = sshpkt_start(ssh, type)) != 0 ||
2757 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2758 (r = sshpkt_send(ssh)) != 0) {
2759 error("%s: send %s", __func__, ssh_err(r));
2760 goto out;
2761 }
2762 }
2763 ret = 0;
2764 out:
2765 free(ctype);
2766 free(listen_host);
2767 sshbuf_free(original);
2768 sshbuf_free(modified);
2769 return ret;
2770}
2771
2772/*
2773 * receive packets from upstream server and de-multiplex packets
2774 * to correct downstream:
2775 * implemented as a helper for channel input handlers,
2776 * replaces local (proxy) channel ID with downstream channel ID.
2777 */
2778int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002779channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002780{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002781 struct sshbuf *b = NULL;
2782 Channel *downstream;
2783 const u_char *cp = NULL;
2784 size_t len;
2785 int r;
2786
2787 /*
2788 * When receiving packets from the peer we need to check whether we
2789 * need to forward the packets to the mux client. In this case we
2790 * restore the orignal channel id and keep track of CLOSE messages,
2791 * so we can cleanup the channel.
2792 */
2793 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2794 return 0;
2795 if ((downstream = c->mux_ctx) == NULL)
2796 return 0;
2797 switch (type) {
2798 case SSH2_MSG_CHANNEL_CLOSE:
2799 case SSH2_MSG_CHANNEL_DATA:
2800 case SSH2_MSG_CHANNEL_EOF:
2801 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2802 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2803 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2804 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2805 case SSH2_MSG_CHANNEL_SUCCESS:
2806 case SSH2_MSG_CHANNEL_FAILURE:
2807 case SSH2_MSG_CHANNEL_REQUEST:
2808 break;
2809 default:
2810 debug2("%s: channel %u: unsupported type %u", __func__,
2811 c->self, type);
2812 return 0;
2813 }
2814 if ((b = sshbuf_new()) == NULL) {
2815 error("%s: alloc reply", __func__);
2816 goto out;
2817 }
2818 /* get remaining payload (after id) */
2819 cp = sshpkt_ptr(ssh, &len);
2820 if (cp == NULL) {
2821 error("%s: no packet", __func__);
2822 goto out;
2823 }
2824 /* translate id and send to muxclient */
2825 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2826 (r = sshbuf_put_u8(b, type)) != 0 ||
2827 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2828 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002829 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002830 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2831 goto out;
2832 }
2833 /* sshbuf_dump(b, stderr); */
2834 if (ssh_packet_log_type(type))
2835 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2836 type);
2837 out:
2838 /* update state */
2839 switch (type) {
2840 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2841 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002842 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002843 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002844 c->have_remote_id = 1;
2845 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002846 break;
2847 case SSH2_MSG_CHANNEL_CLOSE:
2848 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002849 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002850 else
2851 c->flags |= CHAN_CLOSE_RCVD;
2852 break;
2853 }
2854 sshbuf_free(b);
2855 return 1;
2856}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002857
2858/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002859
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002860/* Parse a channel ID from the current packet */
2861static int
2862channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2863{
2864 u_int32_t id;
2865 int r;
2866
2867 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2868 error("%s: parse id: %s", where, ssh_err(r));
2869 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2870 }
2871 if (id > INT_MAX) {
2872 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2873 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2874 }
2875 return (int)id;
2876}
2877
2878/* Lookup a channel from an ID in the current packet */
2879static Channel *
2880channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2881{
2882 int id = channel_parse_id(ssh, where, what);
2883 Channel *c;
2884
2885 if ((c = channel_lookup(ssh, id)) == NULL) {
2886 ssh_packet_disconnect(ssh,
2887 "%s packet referred to nonexistent channel %d", what, id);
2888 }
2889 return c;
2890}
2891
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002892int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002893channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002894{
Damien Miller633de332014-05-15 13:48:26 +10002895 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002896 size_t data_len, win_len;
2897 Channel *c = channel_from_packet_id(ssh, __func__, "data");
2898 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002899
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002900 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002901 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002902
Damien Miller95def091999-11-25 00:26:21 +11002903 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002904 if (c->type != SSH_CHANNEL_OPEN &&
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002905 c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
2906 c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
Damien Millerb38eff82000-04-01 11:09:21 +10002907 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002908 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002909
Damien Miller95def091999-11-25 00:26:21 +11002910 /* Get the data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002911 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0)
2912 fatal("%s: channel %d: get data: %s", __func__,
2913 c->self, ssh_err(r));
2914 ssh_packet_check_eom(ssh);
2915
Damien Miller7d457182010-08-05 23:09:48 +10002916 win_len = data_len;
2917 if (c->datagram)
2918 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002919
Damien Millera04ad492004-01-21 11:02:09 +11002920 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002921 * The sending side reduces its window as it sends data, so we
2922 * must 'fake' consumption of the data in order to ensure that window
2923 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11002924 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002925 if (c->ostate != CHAN_OUTPUT_OPEN) {
2926 c->local_window -= win_len;
2927 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002928 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002929 }
2930
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002931 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002932 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002933 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002934 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002935 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002936 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002937 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002938 c->self, win_len, c->local_window);
2939 return 0;
2940 }
2941 c->local_window -= win_len;
2942
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002943 if (c->datagram) {
2944 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
2945 fatal("%s: channel %d: append datagram: %s",
2946 __func__, c->self, ssh_err(r));
2947 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
2948 fatal("%s: channel %d: append data: %s",
2949 __func__, c->self, ssh_err(r));
2950
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002951 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002952}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002953
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002954int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002955channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002956{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002957 const u_char *data;
2958 size_t data_len;
2959 u_int32_t tcode;
2960 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
2961 int r;
Damien Miller33b13562000-04-04 14:38:59 +10002962
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002963 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002964 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002965 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002966 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002967 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002968 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002969 if (c->flags & CHAN_EOF_RCVD) {
2970 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002971 debug("channel %d: accepting ext data after eof",
2972 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002973 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002974 ssh_packet_disconnect(ssh, "Received extended_data "
2975 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002976 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002977
2978 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
2979 error("%s: parse tcode: %s", __func__, ssh_err(r));
2980 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2981 }
Damien Miller33b13562000-04-04 14:38:59 +10002982 if (c->efd == -1 ||
2983 c->extended_usage != CHAN_EXTENDED_WRITE ||
2984 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002985 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002986 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002987 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002988 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) {
2989 error("%s: parse data: %s", __func__, ssh_err(r));
2990 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2991 }
2992 ssh_packet_check_eom(ssh);
2993
Damien Miller33b13562000-04-04 14:38:59 +10002994 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002995 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10002996 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002997 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002998 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002999 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
3000 /* XXX sshpkt_getb? */
3001 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
3002 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10003003 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003004 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003005}
3006
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003007int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003008channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10003009{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003010 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
Damien Millerb38eff82000-04-01 11:09:21 +10003011
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003012 ssh_packet_check_eom(ssh);
3013
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003014 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003015 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003016 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003017
3018 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11003019 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003020 debug("channel %d: FORCE input drain", c->self);
3021 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003022 if (sshbuf_len(c->input) == 0)
3023 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003024 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003025 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10003026}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003027
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003028int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003029channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003030{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003031 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
Damien Miller66823cd2002-01-22 23:11:38 +11003032
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003033 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003034 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003035 ssh_packet_check_eom(ssh);
3036 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003037 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003038}
3039
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003040int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003041channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003042{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003043 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3044 u_int32_t remote_window, remote_maxpacket;
3045 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003046
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003047 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003048 return 0;
3049 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10003050 packet_disconnect("Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003051 "non-opening channel %d.", c->self);
3052 /*
3053 * Record the remote channel number and mark that the channel
3054 * is now open.
3055 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00003056 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3057 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003058 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) {
3059 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
3060 packet_disconnect("Invalid open confirmation message");
3061 }
3062 ssh_packet_check_eom(ssh);
Damien Miller33b13562000-04-04 14:38:59 +10003063
djm@openbsd.org9f532292017-09-12 06:35:31 +00003064 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003065 c->remote_window = remote_window;
3066 c->remote_maxpacket = remote_maxpacket;
3067 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003068 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003069 debug2("%s: channel %d: callback start", __func__, c->self);
3070 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
3071 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003072 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003073 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3074 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003075 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003076}
3077
Ben Lindstrombba81212001-06-25 05:01:22 +00003078static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00003079reason2txt(int reason)
3080{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003081 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00003082 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3083 return "administratively prohibited";
3084 case SSH2_OPEN_CONNECT_FAILED:
3085 return "connect failed";
3086 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3087 return "unknown channel type";
3088 case SSH2_OPEN_RESOURCE_SHORTAGE:
3089 return "resource shortage";
3090 }
Ben Lindstrome2595442001-06-05 20:01:39 +00003091 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00003092}
3093
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003094int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003095channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003096{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003097 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3098 u_int32_t reason;
3099 char *msg = NULL;
3100 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003101
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003102 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003103 return 0;
3104 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10003105 packet_disconnect("Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003106 "non-opening channel %d.", c->self);
3107 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
3108 error("%s: reason: %s", __func__, ssh_err(r));
3109 packet_disconnect("Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003110 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003111 /* skip language */
3112 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
3113 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) {
3114 error("%s: message/lang: %s", __func__, ssh_err(r));
3115 packet_disconnect("Invalid open failure message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003116 }
3117 ssh_packet_check_eom(ssh);
3118 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003119 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3120 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003121 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003122 debug2("%s: channel %d: callback start", __func__, c->self);
3123 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3124 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003125 }
Damien Miller7a606212009-01-28 16:22:34 +11003126 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003127 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003128 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003129}
3130
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003131int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003132channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003133{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003134 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003135 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003136 u_int32_t adjust;
3137 u_int new_rwin;
3138 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003139
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003140 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003141 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003142 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00003143 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003144
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003145 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003146 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003147 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) {
3148 error("%s: adjust: %s", __func__, ssh_err(r));
3149 packet_disconnect("Invalid window adjust message");
3150 }
3151 ssh_packet_check_eom(ssh);
3152 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3153 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003154 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003155 c->self, adjust, c->remote_window);
3156 }
3157 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003158 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003159}
3160
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003161int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003162channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003163{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003164 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003165 Channel *c;
3166 struct channel_confirm *cc;
Damien Millerb84886b2008-05-19 15:05:07 +10003167
3168 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003169 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003170
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003171 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003172
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003173 if ((c = channel_lookup(ssh, id)) == NULL) {
3174 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003175 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003176 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003177 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003178 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003179 ssh_packet_check_eom(ssh);
Damien Millerb84886b2008-05-19 15:05:07 +10003180 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003181 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003182 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003183 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003184 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003185 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003186 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003187}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003188
Ben Lindstrome9c99912001-06-09 00:41:05 +00003189/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003190
Ben Lindstrom908afed2001-10-03 17:34:59 +00003191void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003192channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003193{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003194 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003195}
3196
Damien Millerf6dff7c2011-09-22 21:38:52 +10003197
3198/*
3199 * Determine whether or not a port forward listens to loopback, the
3200 * specified address or wildcard. On the client, a specified bind
3201 * address will always override gateway_ports. On the server, a
3202 * gateway_ports of 1 (``yes'') will override the client's specification
3203 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3204 * will bind to whatever address the client asked for.
3205 *
3206 * Special-case listen_addrs are:
3207 *
3208 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3209 * "" (empty string), "*" -> wildcard v4/v6
3210 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003211 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003212 */
3213static const char *
3214channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003215 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003216{
3217 const char *addr = NULL;
3218 int wildcard = 0;
3219
3220 if (listen_addr == NULL) {
3221 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003222 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003223 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003224 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003225 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3226 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3227 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003228 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003229 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003230 /*
3231 * Notify client if they requested a specific listen
3232 * address and it was overridden.
3233 */
3234 if (*listen_addr != '\0' &&
3235 strcmp(listen_addr, "0.0.0.0") != 0 &&
3236 strcmp(listen_addr, "*") != 0) {
3237 packet_send_debug("Forwarding listen address "
3238 "\"%s\" overridden by server "
3239 "GatewayPorts", listen_addr);
3240 }
Damien Miller602943d2014-07-04 08:59:41 +10003241 } else if (strcmp(listen_addr, "localhost") != 0 ||
3242 strcmp(listen_addr, "127.0.0.1") == 0 ||
3243 strcmp(listen_addr, "::1") == 0) {
3244 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003245 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003246 }
3247 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3248 strcmp(listen_addr, "::1") == 0) {
3249 /*
3250 * If a specific IPv4/IPv6 localhost address has been
3251 * requested then accept it even if gateway_ports is in
3252 * effect. This allows the client to prefer IPv4 or IPv6.
3253 */
3254 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003255 }
3256 if (wildcardp != NULL)
3257 *wildcardp = wildcard;
3258 return addr;
3259}
3260
Damien Millerb16461c2002-01-22 23:29:22 +11003261static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003262channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3263 struct Forward *fwd, int *allocated_listen_port,
3264 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003265{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003266 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003267 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003268 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003269 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003270 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003271 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003272
Damien Millerf91ee4c2005-03-01 21:24:33 +11003273 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003274
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003275 if (is_client && fwd->connect_path != NULL) {
3276 host = fwd->connect_path;
3277 } else {
3278 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3279 fwd->listen_host : fwd->connect_host;
3280 if (host == NULL) {
3281 error("No forward host name.");
3282 return 0;
3283 }
3284 if (strlen(host) >= NI_MAXHOST) {
3285 error("Forward host name too long.");
3286 return 0;
3287 }
Kevin Steves12057502001-02-05 14:54:34 +00003288 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003289
Damien Millerf6dff7c2011-09-22 21:38:52 +10003290 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003291 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3292 is_client, fwd_opts);
3293 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003294 type, wildcard, (addr == NULL) ? "NULL" : addr);
3295
3296 /*
Damien Miller34132e52000-01-14 15:45:46 +11003297 * getaddrinfo returns a loopback address if the hostname is
3298 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003299 */
Damien Miller34132e52000-01-14 15:45:46 +11003300 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003301 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003302 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003303 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003304 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003305 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3306 if (addr == NULL) {
3307 /* This really shouldn't happen */
3308 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003309 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003310 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003311 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003312 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003313 }
Damien Millera7270302005-07-06 09:36:05 +10003314 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003315 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003316 if (allocated_listen_port != NULL)
3317 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003318 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003319 switch (ai->ai_family) {
3320 case AF_INET:
3321 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3322 sin_port;
3323 break;
3324 case AF_INET6:
3325 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3326 sin6_port;
3327 break;
3328 default:
Damien Miller34132e52000-01-14 15:45:46 +11003329 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003330 }
3331 /*
3332 * If allocating a port for -R forwards, then use the
3333 * same port for all address families.
3334 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003335 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3336 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3337 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003338 *lport_p = htons(*allocated_listen_port);
3339
Damien Miller34132e52000-01-14 15:45:46 +11003340 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003341 strport, sizeof(strport),
3342 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003343 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003344 continue;
3345 }
3346 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003347 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003348 if (sock < 0) {
3349 /* this is no error since kernel may not support ipv6 */
djm@openbsd.org927f8512017-12-05 01:30:19 +00003350 verbose("socket [%s]:%s: %.100s", ntop, strport,
3351 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003352 continue;
3353 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003354
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00003355 set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003356 if (ai->ai_family == AF_INET6)
3357 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003358
Damien Miller4bf648f2009-02-14 16:28:21 +11003359 debug("Local forwarding listening on %s port %s.",
3360 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003361
Damien Miller34132e52000-01-14 15:45:46 +11003362 /* Bind the socket to the address. */
3363 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003364 /*
3365 * address can be in if use ipv6 address is
3366 * already bound
3367 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003368 if (!ai->ai_next)
djm@openbsd.org927f8512017-12-05 01:30:19 +00003369 error("bind [%s]:%s: %.100s",
3370 ntop, strport, strerror(errno));
Damien Miller3c7eeb22000-03-03 22:35:33 +11003371 else
djm@openbsd.org927f8512017-12-05 01:30:19 +00003372 verbose("bind [%s]:%s: %.100s",
3373 ntop, strport, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003374
Damien Miller34132e52000-01-14 15:45:46 +11003375 close(sock);
3376 continue;
3377 }
3378 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003379 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003380 error("listen: %.100s", strerror(errno));
djm@openbsd.org927f8512017-12-05 01:30:19 +00003381 error("listen [%s]:%s: %.100s", ntop, strport,
3382 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003383 close(sock);
3384 continue;
3385 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003386
3387 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003388 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003389 * record what we got.
3390 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003391 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3392 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003393 allocated_listen_port != NULL &&
3394 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003395 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003396 debug("Allocated listen port %d",
3397 *allocated_listen_port);
3398 }
3399
Damien Miller34132e52000-01-14 15:45:46 +11003400 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003401 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003402 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003403 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003404 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003405 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003406 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003407 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003408 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3409 c->listening_port = *allocated_listen_port;
3410 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003411 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003412 success = 1;
3413 }
3414 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003415 error("%s: cannot listen to port: %d", __func__,
3416 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003417 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003418 return success;
Damien Miller95def091999-11-25 00:26:21 +11003419}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003420
Damien Miller7acefbb2014-07-18 14:11:24 +10003421static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003422channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3423 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003424{
3425 struct sockaddr_un sunaddr;
3426 const char *path;
3427 Channel *c;
3428 int port, sock;
3429 mode_t omask;
3430
3431 switch (type) {
3432 case SSH_CHANNEL_UNIX_LISTENER:
3433 if (fwd->connect_path != NULL) {
3434 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3435 error("Local connecting path too long: %s",
3436 fwd->connect_path);
3437 return 0;
3438 }
3439 path = fwd->connect_path;
3440 port = PORT_STREAMLOCAL;
3441 } else {
3442 if (fwd->connect_host == NULL) {
3443 error("No forward host name.");
3444 return 0;
3445 }
3446 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3447 error("Forward host name too long.");
3448 return 0;
3449 }
3450 path = fwd->connect_host;
3451 port = fwd->connect_port;
3452 }
3453 break;
3454 case SSH_CHANNEL_RUNIX_LISTENER:
3455 path = fwd->listen_path;
3456 port = PORT_STREAMLOCAL;
3457 break;
3458 default:
3459 error("%s: unexpected channel type %d", __func__, type);
3460 return 0;
3461 }
3462
3463 if (fwd->listen_path == NULL) {
3464 error("No forward path name.");
3465 return 0;
3466 }
3467 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3468 error("Local listening path too long: %s", fwd->listen_path);
3469 return 0;
3470 }
3471
3472 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3473
3474 /* Start a Unix domain listener. */
3475 omask = umask(fwd_opts->streamlocal_bind_mask);
3476 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3477 fwd_opts->streamlocal_bind_unlink);
3478 umask(omask);
3479 if (sock < 0)
3480 return 0;
3481
3482 debug("Local forwarding listening on path %s.", fwd->listen_path);
3483
3484 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003485 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003486 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3487 0, "unix listener", 1);
3488 c->path = xstrdup(path);
3489 c->host_port = port;
3490 c->listening_port = PORT_STREAMLOCAL;
3491 c->listening_addr = xstrdup(fwd->listen_path);
3492 return 1;
3493}
3494
3495static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003496channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3497 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003498{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003499 u_int i;
3500 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003501
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003502 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3503 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003504 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3505 continue;
3506 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3507 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003508 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003509 found = 1;
3510 }
3511 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003512
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003513 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003514}
3515
Damien Miller7acefbb2014-07-18 14:11:24 +10003516static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003517channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003518{
3519 u_int i;
3520 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003521
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003522 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3523 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003524 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3525 continue;
3526 if (c->path == NULL)
3527 continue;
3528 if (strcmp(c->path, path) == 0) {
3529 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003530 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003531 found = 1;
3532 }
3533 }
3534
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003535 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003536}
3537
3538int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003539channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003540{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003541 if (fwd->listen_path != NULL) {
3542 return channel_cancel_rport_listener_streamlocal(ssh,
3543 fwd->listen_path);
3544 } else {
3545 return channel_cancel_rport_listener_tcpip(ssh,
3546 fwd->listen_host, fwd->listen_port);
3547 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003548}
3549
3550static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003551channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3552 const char *lhost, u_short lport, int cport,
3553 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003554{
3555 u_int i;
3556 int found = 0;
3557 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003558
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003559 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3560 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003561 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3562 continue;
Damien Millerff773642011-09-22 21:39:48 +10003563 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003564 continue;
Damien Millerff773642011-09-22 21:39:48 +10003565 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3566 /* skip dynamic forwardings */
3567 if (c->host_port == 0)
3568 continue;
3569 } else {
3570 if (c->host_port != cport)
3571 continue;
3572 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003573 if ((c->listening_addr == NULL && addr != NULL) ||
3574 (c->listening_addr != NULL && addr == NULL))
3575 continue;
3576 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003577 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003578 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003579 found = 1;
3580 }
3581 }
3582
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003583 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003584}
3585
Damien Miller7acefbb2014-07-18 14:11:24 +10003586static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003587channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003588{
3589 u_int i;
3590 int found = 0;
3591
3592 if (path == NULL) {
3593 error("%s: no path specified.", __func__);
3594 return 0;
3595 }
3596
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003597 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3598 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003599 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3600 continue;
3601 if (c->listening_addr == NULL)
3602 continue;
3603 if (strcmp(c->listening_addr, path) == 0) {
3604 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003605 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003606 found = 1;
3607 }
3608 }
3609
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003610 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003611}
3612
3613int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003614channel_cancel_lport_listener(struct ssh *ssh,
3615 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003616{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003617 if (fwd->listen_path != NULL) {
3618 return channel_cancel_lport_listener_streamlocal(ssh,
3619 fwd->listen_path);
3620 } else {
3621 return channel_cancel_lport_listener_tcpip(ssh,
3622 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3623 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003624}
3625
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003626/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003627int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003628channel_setup_local_fwd_listener(struct ssh *ssh,
3629 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003630{
Damien Miller7acefbb2014-07-18 14:11:24 +10003631 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003632 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003633 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3634 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003635 return channel_setup_fwd_listener_tcpip(ssh,
3636 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003637 }
Damien Millerb16461c2002-01-22 23:29:22 +11003638}
3639
3640/* protocol v2 remote port fwd, used by sshd */
3641int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003642channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003643 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003644{
Damien Miller7acefbb2014-07-18 14:11:24 +10003645 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003646 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003647 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3648 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003649 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003650 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3651 fwd_opts);
3652 }
Damien Millerb16461c2002-01-22 23:29:22 +11003653}
3654
Damien Miller5428f641999-11-25 11:54:57 +11003655/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003656 * Translate the requested rfwd listen host to something usable for
3657 * this server.
3658 */
3659static const char *
3660channel_rfwd_bind_host(const char *listen_host)
3661{
3662 if (listen_host == NULL) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003663 return "localhost";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003664 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003665 return "";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003666 } else
3667 return listen_host;
3668}
3669
3670/*
Damien Miller5428f641999-11-25 11:54:57 +11003671 * Initiate forwarding of connections to port "port" on remote host through
3672 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003673 * Returns handle (index) for updating the dynamic listen port with
3674 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003675 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003676int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003677channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003678{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003679 int r, success = 0, idx = -1;
3680 char *host_to_connect, *listen_host, *listen_path;
3681 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003682
Damien Miller95def091999-11-25 00:26:21 +11003683 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003684 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003685 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3686 (r = sshpkt_put_cstring(ssh,
3687 "streamlocal-forward@openssh.com")) != 0 ||
3688 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3689 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3690 (r = sshpkt_send(ssh)) != 0 ||
3691 (r = ssh_packet_write_wait(ssh)) != 0)
3692 fatal("%s: request streamlocal: %s",
3693 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003694 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003695 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3696 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3697 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3698 (r = sshpkt_put_cstring(ssh,
3699 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3700 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3701 (r = sshpkt_send(ssh)) != 0 ||
3702 (r = ssh_packet_write_wait(ssh)) != 0)
3703 fatal("%s: request tcpip-forward: %s",
3704 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003705 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003706 /* Assume that server accepts the request */
3707 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003708 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003709 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003710 host_to_connect = listen_host = listen_path = NULL;
3711 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003712 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003713 host_to_connect = xstrdup(fwd->connect_path);
3714 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003715 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003716 host_to_connect = xstrdup(fwd->connect_host);
3717 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003718 }
3719 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003720 listen_path = xstrdup(fwd->listen_path);
3721 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003722 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003723 if (fwd->listen_host != NULL)
3724 listen_host = xstrdup(fwd->listen_host);
3725 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003726 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003727 idx = fwd_perm_list_add(ssh, FWDPERM_USER,
3728 host_to_connect, port_to_connect,
3729 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003730 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003731 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003732}
3733
Damien Miller4b3ed642014-07-02 15:29:40 +10003734static int
3735open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003736 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003737{
3738 if (allowed_open->host_to_connect == NULL)
3739 return 0;
3740 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3741 allowed_open->port_to_connect != requestedport)
3742 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003743 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3744 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003745 return 0;
3746 return 1;
3747}
3748
3749/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003750 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003751 * we don't support FWD_PERMIT_ANY_PORT and
3752 * need to translate between the configured-host (listen_host)
3753 * and what we've sent to the remote server (channel_rfwd_bind_host)
3754 */
3755static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003756open_listen_match_tcpip(ForwardPermission *allowed_open,
3757 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003758{
3759 const char *allowed_host;
3760
3761 if (allowed_open->host_to_connect == NULL)
3762 return 0;
3763 if (allowed_open->listen_port != requestedport)
3764 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003765 if (!translate && allowed_open->listen_host == NULL &&
3766 requestedhost == NULL)
3767 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003768 allowed_host = translate ?
3769 channel_rfwd_bind_host(allowed_open->listen_host) :
3770 allowed_open->listen_host;
3771 if (allowed_host == NULL ||
3772 strcmp(allowed_host, requestedhost) != 0)
3773 return 0;
3774 return 1;
3775}
3776
Damien Miller7acefbb2014-07-18 14:11:24 +10003777static int
3778open_listen_match_streamlocal(ForwardPermission *allowed_open,
3779 const char *requestedpath)
3780{
3781 if (allowed_open->host_to_connect == NULL)
3782 return 0;
3783 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3784 return 0;
3785 if (allowed_open->listen_path == NULL ||
3786 strcmp(allowed_open->listen_path, requestedpath) != 0)
3787 return 0;
3788 return 1;
3789}
3790
Damien Miller5428f641999-11-25 11:54:57 +11003791/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003792 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003793 * local side.
3794 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003795static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003796channel_request_rforward_cancel_tcpip(struct ssh *ssh,
3797 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003798{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003799 struct ssh_channels *sc = ssh->chanctxt;
3800 int r;
3801 u_int i;
3802 ForwardPermission *fp;
Darren Tuckere7066df2004-05-24 10:18:05 +10003803
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003804 for (i = 0; i < sc->num_permitted_opens; i++) {
3805 fp = &sc->permitted_opens[i];
3806 if (open_listen_match_tcpip(fp, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003807 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003808 fp = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10003809 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003810 if (fp == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003811 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003812 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003813 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003814 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3815 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
3816 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3817 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
3818 (r = sshpkt_put_u32(ssh, port)) != 0 ||
3819 (r = sshpkt_send(ssh)) != 0)
3820 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10003821
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003822 fwd_perm_clear(fp); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10003823
3824 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003825}
3826
3827/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003828 * Request cancellation of remote forwarding of Unix domain socket
3829 * path from local side.
3830 */
3831static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003832channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003833{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003834 struct ssh_channels *sc = ssh->chanctxt;
3835 int r;
3836 u_int i;
3837 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10003838
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003839 for (i = 0; i < sc->num_permitted_opens; i++) {
3840 fp = &sc->permitted_opens[i];
3841 if (open_listen_match_streamlocal(fp, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10003842 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003843 fp = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003844 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003845 if (fp == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003846 debug("%s: requested forward not found", __func__);
3847 return -1;
3848 }
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,
3851 "cancel-streamlocal-forward@openssh.com")) != 0 ||
3852 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3853 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
3854 (r = sshpkt_send(ssh)) != 0)
3855 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003856
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003857 fwd_perm_clear(fp); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10003858
3859 return 0;
3860}
3861
3862/*
3863 * Request cancellation of remote forwarding of a connection from local side.
3864 */
3865int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003866channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003867{
3868 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003869 return channel_request_rforward_cancel_streamlocal(ssh,
3870 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003871 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003872 return channel_request_rforward_cancel_tcpip(ssh,
3873 fwd->listen_host,
3874 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10003875 }
3876}
3877
3878/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003879 * Permits opening to any host/port if permitted_opens[] is empty. This is
3880 * usually called by the server, because the user could connect to any port
3881 * anyway, and the server has no way to know but to trust the client anyway.
3882 */
3883void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003884channel_permit_all_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003885{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003886 if (ssh->chanctxt->num_permitted_opens == 0)
3887 ssh->chanctxt->all_opens_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003888}
3889
Ben Lindstroma3700052001-04-05 23:26:32 +00003890void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003891channel_add_permitted_opens(struct ssh *ssh, char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003892{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003893 struct ssh_channels *sc = ssh->chanctxt;
3894
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003895 debug("allow port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003896 fwd_perm_list_add(ssh, FWDPERM_USER, host, port, NULL, NULL, 0, NULL);
3897 sc->all_opens_permitted = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003898}
3899
Darren Tucker68afb8c2011-10-02 18:59:03 +11003900/*
3901 * Update the listen port for a dynamic remote forward, after
3902 * the actual 'newport' has been allocated. If 'newport' < 0 is
3903 * passed then they entry will be invalidated.
3904 */
3905void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003906channel_update_permitted_opens(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11003907{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003908 struct ssh_channels *sc = ssh->chanctxt;
3909
3910 if (idx < 0 || (u_int)idx >= sc->num_permitted_opens) {
3911 debug("%s: index out of range: %d num_permitted_opens %d",
3912 __func__, idx, sc->num_permitted_opens);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003913 return;
3914 }
3915 debug("%s allowed port %d for forwarding to host %s port %d",
3916 newport > 0 ? "Updating" : "Removing",
3917 newport,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003918 sc->permitted_opens[idx].host_to_connect,
3919 sc->permitted_opens[idx].port_to_connect);
3920 if (newport <= 0)
3921 fwd_perm_clear(&sc->permitted_opens[idx]);
3922 else {
3923 sc->permitted_opens[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11003924 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003925 }
3926}
3927
Damien Millera765cf42006-07-24 14:08:13 +10003928int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003929channel_add_adm_permitted_opens(struct ssh *ssh, char *host, int port)
Damien Miller9b439df2006-07-24 14:04:00 +10003930{
Damien Millera765cf42006-07-24 14:08:13 +10003931 debug("config allows port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003932 return fwd_perm_list_add(ssh, FWDPERM_ADMIN, host, port,
3933 NULL, NULL, 0, NULL);
Damien Miller9b439df2006-07-24 14:04:00 +10003934}
3935
3936void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003937channel_disable_adm_local_opens(struct ssh *ssh)
Damien Millerc6081482012-04-22 11:18:53 +10003938{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003939 channel_clear_adm_permitted_opens(ssh);
3940 fwd_perm_list_add(ssh, FWDPERM_ADMIN, NULL, 0, NULL, NULL, 0, NULL);
Damien Millerc6081482012-04-22 11:18:53 +10003941}
3942
3943void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003944channel_clear_permitted_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003945{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003946 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003947
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003948 sc->permitted_opens = xrecallocarray(sc->permitted_opens,
3949 sc->num_permitted_opens, 0, sizeof(*sc->permitted_opens));
3950 sc->num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003951}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003952
Damien Miller9b439df2006-07-24 14:04:00 +10003953void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003954channel_clear_adm_permitted_opens(struct ssh *ssh)
Damien Miller9b439df2006-07-24 14:04:00 +10003955{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003956 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller9b439df2006-07-24 14:04:00 +10003957
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003958 sc->permitted_adm_opens = xrecallocarray(sc->permitted_adm_opens,
3959 sc->num_adm_permitted_opens, 0, sizeof(*sc->permitted_adm_opens));
3960 sc->num_adm_permitted_opens = 0;
Darren Tuckere7140f22008-06-10 23:01:51 +10003961}
3962
Darren Tucker1338b9e2011-10-02 18:57:35 +11003963/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3964int
3965permitopen_port(const char *p)
3966{
3967 int port;
3968
3969 if (strcmp(p, "*") == 0)
3970 return FWD_PERMIT_ANY_PORT;
3971 if ((port = a2port(p)) > 0)
3972 return port;
3973 return -1;
3974}
3975
Damien Millerbd740252008-05-19 15:37:09 +10003976/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003977static int
Damien Millerbd740252008-05-19 15:37:09 +10003978connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003979{
Damien Millerbd740252008-05-19 15:37:09 +10003980 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003981 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003982 char ntop[NI_MAXHOST];
3983 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003984
Damien Millerbd740252008-05-19 15:37:09 +10003985 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003986 switch (cctx->ai->ai_family) {
3987 case AF_UNIX:
3988 /* unix:pathname instead of host:port */
3989 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3990 strlcpy(ntop, "unix", sizeof(ntop));
3991 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3992 break;
3993 case AF_INET:
3994 case AF_INET6:
3995 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3996 ntop, sizeof(ntop), strport, sizeof(strport),
3997 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3998 error("connect_next: getnameinfo failed");
3999 continue;
4000 }
4001 break;
4002 default:
Damien Miller34132e52000-01-14 15:45:46 +11004003 continue;
4004 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11004005 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4006 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004007 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11004008 error("socket: %.100s", strerror(errno));
4009 else
4010 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004011 continue;
4012 }
Damien Miller232711f2004-06-15 10:35:30 +10004013 if (set_nonblock(sock) == -1)
4014 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10004015 if (connect(sock, cctx->ai->ai_addr,
4016 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
4017 debug("connect_next: host %.100s ([%.100s]:%s): "
4018 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004019 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004020 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004021 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004022 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004023 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004024 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004025 if (cctx->ai->ai_family != AF_UNIX)
4026 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004027 debug("connect_next: host %.100s ([%.100s]:%s) "
4028 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4029 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004030 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004031 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004032 return -1;
4033}
Damien Millerb38eff82000-04-01 11:09:21 +10004034
Damien Millerbd740252008-05-19 15:37:09 +10004035static void
4036channel_connect_ctx_free(struct channel_connect *cctx)
4037{
Darren Tuckera627d422013-06-02 07:31:17 +10004038 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004039 if (cctx->aitop) {
4040 if (cctx->aitop->ai_family == AF_UNIX)
4041 free(cctx->aitop);
4042 else
4043 freeaddrinfo(cctx->aitop);
4044 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004045 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004046}
4047
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004048/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004049 * Return connecting socket to remote host:port or local socket path,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004050 * passing back the failure reason if appropriate.
4051 */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004052static int
4053connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4054 char *ctype, char *rname, struct channel_connect *cctx,
4055 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10004056{
4057 struct addrinfo hints;
4058 int gaierr;
4059 int sock = -1;
4060 char strport[NI_MAXSERV];
Damien Miller7acefbb2014-07-18 14:11:24 +10004061
4062 if (port == PORT_STREAMLOCAL) {
4063 struct sockaddr_un *sunaddr;
4064 struct addrinfo *ai;
4065
4066 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4067 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004068 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004069 }
4070
4071 /*
4072 * Fake up a struct addrinfo for AF_UNIX connections.
4073 * channel_connect_ctx_free() must check ai_family
4074 * and use free() not freeaddirinfo() for AF_UNIX.
4075 */
4076 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4077 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4078 ai->ai_addr = (struct sockaddr *)(ai + 1);
4079 ai->ai_addrlen = sizeof(*sunaddr);
4080 ai->ai_family = AF_UNIX;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004081 ai->ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004082 ai->ai_protocol = PF_UNSPEC;
4083 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4084 sunaddr->sun_family = AF_UNIX;
4085 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004086 cctx->aitop = ai;
Damien Miller7acefbb2014-07-18 14:11:24 +10004087 } else {
4088 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004089 hints.ai_family = ssh->chanctxt->IPv4or6;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004090 hints.ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004091 snprintf(strport, sizeof strport, "%d", port);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004092 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004093 != 0) {
4094 if (errmsg != NULL)
4095 *errmsg = ssh_gai_strerror(gaierr);
4096 if (reason != NULL)
4097 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10004098 error("connect_to %.100s: unknown host (%s)", name,
4099 ssh_gai_strerror(gaierr));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004100 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004101 }
Damien Millerbd740252008-05-19 15:37:09 +10004102 }
4103
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004104 cctx->host = xstrdup(name);
4105 cctx->port = port;
4106 cctx->ai = cctx->aitop;
Damien Millerbd740252008-05-19 15:37:09 +10004107
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004108 if ((sock = connect_next(cctx)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004109 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004110 name, port, strerror(errno));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004111 return -1;
4112 }
4113
4114 return sock;
4115}
4116
4117/* Return CONNECTING channel to remote host:port or local socket path */
4118static Channel *
4119connect_to(struct ssh *ssh, const char *host, int port,
4120 char *ctype, char *rname)
4121{
4122 struct channel_connect cctx;
4123 Channel *c;
4124 int sock;
4125
4126 memset(&cctx, 0, sizeof(cctx));
4127 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4128 &cctx, NULL, NULL);
4129 if (sock == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004130 channel_connect_ctx_free(&cctx);
4131 return NULL;
4132 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004133 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004134 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004135 c->host_port = port;
4136 c->path = xstrdup(host);
Damien Millerbd740252008-05-19 15:37:09 +10004137 c->connect_ctx = cctx;
Damien Millerbd740252008-05-19 15:37:09 +10004138
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004139 return c;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004140}
4141
markus@openbsd.org8d057842016-09-30 09:19:13 +00004142/*
4143 * returns either the newly connected channel or the downstream channel
4144 * that needs to deal with this connection.
4145 */
Damien Millerbd740252008-05-19 15:37:09 +10004146Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004147channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004148 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004149{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004150 struct ssh_channels *sc = ssh->chanctxt;
4151 u_int i;
4152 ForwardPermission *fp;
Damien Millerbd740252008-05-19 15:37:09 +10004153
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004154 for (i = 0; i < sc->num_permitted_opens; i++) {
4155 fp = &sc->permitted_opens[i];
4156 if (open_listen_match_tcpip(fp, listen_host, listen_port, 1)) {
4157 if (fp->downstream)
4158 return fp->downstream;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004159 if (fp->port_to_connect == 0)
4160 return rdynamic_connect_prepare(ssh,
4161 ctype, rname);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004162 return connect_to(ssh,
4163 fp->host_to_connect, fp->port_to_connect,
4164 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004165 }
4166 }
4167 error("WARNING: Server requests forwarding for unknown listen_port %d",
4168 listen_port);
4169 return NULL;
4170}
4171
Damien Miller7acefbb2014-07-18 14:11:24 +10004172Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004173channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4174 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004175{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004176 struct ssh_channels *sc = ssh->chanctxt;
4177 u_int i;
4178 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004179
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004180 for (i = 0; i < sc->num_permitted_opens; i++) {
4181 fp = &sc->permitted_opens[i];
4182 if (open_listen_match_streamlocal(fp, path)) {
4183 return connect_to(ssh,
4184 fp->host_to_connect, fp->port_to_connect,
4185 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004186 }
4187 }
4188 error("WARNING: Server requests forwarding for unknown path %.100s",
4189 path);
4190 return NULL;
4191}
4192
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004193/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004194Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004195channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4196 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004197{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004198 struct ssh_channels *sc = ssh->chanctxt;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004199 struct channel_connect cctx;
4200 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004201 u_int i, permit, permit_adm = 1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004202 int sock;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004203 ForwardPermission *fp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004204
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004205 permit = sc->all_opens_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004206 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004207 for (i = 0; i < sc->num_permitted_opens; i++) {
4208 fp = &sc->permitted_opens[i];
4209 if (open_match(fp, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004210 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004211 break;
4212 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004213 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004214 }
Damien Miller9b439df2006-07-24 14:04:00 +10004215
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004216 if (sc->num_adm_permitted_opens > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004217 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004218 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4219 fp = &sc->permitted_adm_opens[i];
4220 if (open_match(fp, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004221 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004222 break;
4223 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004224 }
Damien Miller9b439df2006-07-24 14:04:00 +10004225 }
4226
4227 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004228 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004229 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004230 if (reason != NULL)
4231 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004232 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004233 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004234
4235 memset(&cctx, 0, sizeof(cctx));
4236 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4237 &cctx, reason, errmsg);
4238 if (sock == -1) {
4239 channel_connect_ctx_free(&cctx);
4240 return NULL;
4241 }
4242
4243 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4244 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4245 c->host_port = port;
4246 c->path = xstrdup(host);
4247 c->connect_ctx = cctx;
4248
4249 return c;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004250}
4251
Damien Miller7acefbb2014-07-18 14:11:24 +10004252/* Check if connecting to that path is permitted and connect. */
4253Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004254channel_connect_to_path(struct ssh *ssh, const char *path,
4255 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004256{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004257 struct ssh_channels *sc = ssh->chanctxt;
4258 u_int i, permit, permit_adm = 1;
4259 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004260
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004261 permit = sc->all_opens_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004262 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004263 for (i = 0; i < sc->num_permitted_opens; i++) {
4264 fp = &sc->permitted_opens[i];
4265 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004266 permit = 1;
4267 break;
4268 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004269 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004270 }
4271
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004272 if (sc->num_adm_permitted_opens > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004273 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004274 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4275 fp = &sc->permitted_adm_opens[i];
4276 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004277 permit_adm = 1;
4278 break;
4279 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004280 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004281 }
4282
4283 if (!permit || !permit_adm) {
4284 logit("Received request to connect to path %.100s, "
4285 "but the request was denied.", path);
4286 return NULL;
4287 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004288 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004289}
4290
Damien Miller0e220db2004-06-15 10:34:08 +10004291void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004292channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004293{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004294 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004295 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004296 int r;
4297 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004298
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004299 for (i = 0; i < sc->channels_alloc; i++) {
4300 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4301 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004302 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004303 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
Damien Miller0e220db2004-06-15 10:34:08 +10004304 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004305 channel_request_start(ssh, i, "window-change", 0);
4306 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4307 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4308 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4309 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4310 (r = sshpkt_send(ssh)) != 0)
4311 fatal("%s: channel %u: send window-change: %s",
4312 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004313 }
4314}
4315
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004316/* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4317static Channel *
4318rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4319{
4320 Channel *c;
4321 int r;
4322
4323 c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4324 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4325 c->host_port = 0;
4326 c->path = NULL;
4327
4328 /*
4329 * We need to open the channel before we have a FD,
4330 * so that we can get SOCKS header from peer.
4331 */
4332 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4333 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4334 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4335 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
4336 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
4337 fatal("%s: channel %i: confirm: %s", __func__,
4338 c->self, ssh_err(r));
4339 }
4340 return c;
4341}
4342
4343/* Return CONNECTING socket to remote host:port or local socket path */
4344static int
4345rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4346{
4347 struct channel_connect cctx;
4348 int sock;
4349
4350 memset(&cctx, 0, sizeof(cctx));
4351 sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4352 NULL, &cctx, NULL, NULL);
4353 if (sock == -1)
4354 channel_connect_ctx_free(&cctx);
4355 else {
4356 /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4357 c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4358 c->connect_ctx = cctx;
4359 channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4360 }
4361 return sock;
4362}
4363
Ben Lindstrome9c99912001-06-09 00:41:05 +00004364/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004365
Damien Miller5428f641999-11-25 11:54:57 +11004366/*
4367 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004368 * Returns 0 and a suitable display number for the DISPLAY variable
4369 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004370 */
Kevin Steves366298c2001-12-19 17:58:01 +00004371int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004372x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4373 int x11_use_localhost, int single_connection,
4374 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004375{
Damien Millere7378562001-12-21 14:58:35 +11004376 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004377 int display_number, sock;
4378 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004379 struct addrinfo hints, *ai, *aitop;
4380 char strport[NI_MAXSERV];
4381 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004382
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004383 if (chanids == NULL)
4384 return -1;
4385
Damien Millera34a28b1999-12-14 10:47:15 +11004386 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004387 display_number < MAX_DISPLAYS;
4388 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004389 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004390 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004391 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004392 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004393 hints.ai_socktype = SOCK_STREAM;
4394 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004395 if ((gaierr = getaddrinfo(NULL, strport,
4396 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004397 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004398 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004399 }
Damien Miller34132e52000-01-14 15:45:46 +11004400 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004401 if (ai->ai_family != AF_INET &&
4402 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004403 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004404 sock = socket(ai->ai_family, ai->ai_socktype,
4405 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004406 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004407 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4408#ifdef EPFNOSUPPORT
4409 && (errno != EPFNOSUPPORT)
4410#endif
4411 ) {
Damien Millere2192732000-01-17 13:22:55 +11004412 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004413 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004414 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004415 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004416 debug("x11_create_display_inet: Socket family %d not supported",
4417 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004418 continue;
4419 }
Damien Miller34132e52000-01-14 15:45:46 +11004420 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004421 if (ai->ai_family == AF_INET6)
4422 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004423 if (x11_use_localhost)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00004424 set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004425 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004426 debug2("%s: bind port %d: %.100s", __func__,
4427 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004428 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004429 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004430 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004431 num_socks = 0;
4432 break;
4433 }
4434 socks[num_socks++] = sock;
4435 if (num_socks == NUM_SOCKS)
4436 break;
Damien Miller95def091999-11-25 00:26:21 +11004437 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004438 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004439 if (num_socks > 0)
4440 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004441 }
Damien Miller95def091999-11-25 00:26:21 +11004442 if (display_number >= MAX_DISPLAYS) {
4443 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004444 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004445 }
Damien Miller95def091999-11-25 00:26:21 +11004446 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004447 for (n = 0; n < num_socks; n++) {
4448 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004449 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004450 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004451 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004452 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004453 }
Damien Miller95def091999-11-25 00:26:21 +11004454 }
Damien Miller34132e52000-01-14 15:45:46 +11004455
Damien Miller34132e52000-01-14 15:45:46 +11004456 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004457 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004458 for (n = 0; n < num_socks; n++) {
4459 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004460 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004461 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4462 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004463 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004464 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004465 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004466 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004467 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004468
Kevin Steves366298c2001-12-19 17:58:01 +00004469 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004470 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004471 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004472}
4473
Ben Lindstrombba81212001-06-25 05:01:22 +00004474static int
Damien Miller819dbb62009-01-21 16:46:26 +11004475connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004476{
Damien Miller95def091999-11-25 00:26:21 +11004477 int sock;
4478 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004479
Damien Miller3afe3752001-12-21 12:39:51 +11004480 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4481 if (sock < 0)
4482 error("socket: %.100s", strerror(errno));
4483 memset(&addr, 0, sizeof(addr));
4484 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004485 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004486 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004487 return sock;
4488 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004489 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4490 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004491}
4492
Damien Miller819dbb62009-01-21 16:46:26 +11004493static int
4494connect_local_xsocket(u_int dnr)
4495{
4496 char buf[1024];
4497 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4498 return connect_local_xsocket_path(buf);
4499}
4500
Darren Tuckerda39b092017-03-10 13:22:32 +11004501#ifdef __APPLE__
4502static int
4503is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4504{
4505 struct stat sbuf;
4506
4507 if (strlcpy(path, display, pathlen) >= pathlen) {
4508 error("%s: display path too long", __func__);
4509 return 0;
4510 }
4511 if (display[0] != '/')
4512 return 0;
4513 if (stat(path, &sbuf) == 0) {
4514 return 1;
4515 } else {
4516 char *dot = strrchr(path, '.');
4517 if (dot != NULL) {
4518 *dot = '\0';
4519 if (stat(path, &sbuf) == 0) {
4520 return 1;
4521 }
4522 }
4523 }
4524 return 0;
4525}
4526#endif
4527
Damien Millerbd483e72000-04-30 10:00:53 +10004528int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004529x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004530{
Damien Miller57c4e872006-03-31 23:11:07 +11004531 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004532 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004533 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004534 struct addrinfo hints, *ai, *aitop;
4535 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004536 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004537
Damien Miller95def091999-11-25 00:26:21 +11004538 /* Try to open a socket for the local X server. */
4539 display = getenv("DISPLAY");
4540 if (!display) {
4541 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004542 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004543 }
Damien Miller5428f641999-11-25 11:54:57 +11004544 /*
4545 * Now we decode the value of the DISPLAY variable and make a
4546 * connection to the real X server.
4547 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004548
Damien Miller819dbb62009-01-21 16:46:26 +11004549#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004550 /* Check if display is a path to a socket (as set by launchd). */
4551 {
4552 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004553
Darren Tuckerda39b092017-03-10 13:22:32 +11004554 if (is_path_to_xsocket(display, path, sizeof(path))) {
4555 debug("x11_connect_display: $DISPLAY is launchd");
4556
4557 /* Create a socket. */
4558 sock = connect_local_xsocket_path(path);
4559 if (sock < 0)
4560 return -1;
4561
4562 /* OK, we now have a connection to the display. */
4563 return sock;
4564 }
Damien Miller819dbb62009-01-21 16:46:26 +11004565 }
4566#endif
Damien Miller5428f641999-11-25 11:54:57 +11004567 /*
4568 * Check if it is a unix domain socket. Unix domain displays are in
4569 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4570 */
Damien Miller95def091999-11-25 00:26:21 +11004571 if (strncmp(display, "unix:", 5) == 0 ||
4572 display[0] == ':') {
4573 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004574 if (sscanf(strrchr(display, ':') + 1, "%u",
4575 &display_number) != 1) {
4576 error("Could not parse display number from DISPLAY: "
4577 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004578 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004579 }
4580 /* Create a socket. */
4581 sock = connect_local_xsocket(display_number);
4582 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004583 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004584
4585 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004586 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004587 }
Damien Miller5428f641999-11-25 11:54:57 +11004588 /*
4589 * Connect to an inet socket. The DISPLAY value is supposedly
4590 * hostname:d[.s], where hostname may also be numeric IP address.
4591 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004592 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004593 cp = strchr(buf, ':');
4594 if (!cp) {
4595 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004596 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004597 }
Damien Miller95def091999-11-25 00:26:21 +11004598 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004599 /*
4600 * buf now contains the host name. But first we parse the
4601 * display number.
4602 */
Damien Miller57c4e872006-03-31 23:11:07 +11004603 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004604 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004605 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004606 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004607 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004608
Damien Miller34132e52000-01-14 15:45:46 +11004609 /* Look up the host address */
4610 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004611 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004612 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004613 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004614 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004615 error("%.100s: unknown host. (%s)", buf,
4616 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004617 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004618 }
Damien Miller34132e52000-01-14 15:45:46 +11004619 for (ai = aitop; ai; ai = ai->ai_next) {
4620 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004621 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004622 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004623 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004624 continue;
4625 }
4626 /* Connect it to the display. */
4627 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004628 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004629 6000 + display_number, strerror(errno));
4630 close(sock);
4631 continue;
4632 }
4633 /* Success */
4634 break;
Damien Miller34132e52000-01-14 15:45:46 +11004635 }
Damien Miller34132e52000-01-14 15:45:46 +11004636 freeaddrinfo(aitop);
4637 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004638 error("connect %.100s port %u: %.100s", buf,
4639 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004640 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004641 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004642 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004643 return sock;
4644}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004645
Damien Millerbd483e72000-04-30 10:00:53 +10004646/*
Damien Miller5428f641999-11-25 11:54:57 +11004647 * Requests forwarding of X11 connections, generates fake authentication
4648 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004649 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004650 */
Damien Miller4af51302000-04-16 11:18:38 +10004651void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004652x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4653 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004654{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004655 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004656 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004657 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004658 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004659 char *new_data;
4660 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004661
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004662 if (sc->x11_saved_display == NULL)
4663 sc->x11_saved_display = xstrdup(disp);
4664 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004665 error("x11_request_forwarding_with_spoofing: different "
4666 "$DISPLAY already forwarded");
4667 return;
4668 }
4669
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004670 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004671 if (cp)
4672 cp = strchr(cp, '.');
4673 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004674 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004675 else
4676 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004677
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004678 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004679 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004680 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004681
4682 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004683 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004684 for (i = 0; i < data_len; i++) {
4685 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4686 fatal("x11_request_forwarding: bad "
4687 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004688 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004689 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004690 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004691
4692 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004693 sc->x11_fake_data = xmalloc(data_len);
4694 arc4random_buf(sc->x11_fake_data, data_len);
4695 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004696 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004697
Damien Miller95def091999-11-25 00:26:21 +11004698 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004699 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004700
Damien Miller95def091999-11-25 00:26:21 +11004701 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004702 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4703 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4704 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4705 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4706 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4707 (r = sshpkt_send(ssh)) != 0 ||
4708 (r = ssh_packet_write_wait(ssh)) != 0)
4709 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004710 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004711}