blob: 1c381e0e223fcd6c72a20f4116275364849d1075 [file] [log] [blame]
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001/* $OpenBSD: channels.c,v 1.378 2018/01/23 05:27:21 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000058#include <limits.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <netdb.h>
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000060#include <stdarg.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#ifdef HAVE_STDINT_H
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000062 #include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110063#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100064#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100065#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110067#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Millerb84886b2008-05-19 15:05:07 +100070#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100071#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000074#include "ssherr.h"
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000075#include "sshbuf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
78#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100082#include "key.h"
83#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110084#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000086/* -- agent forwarding */
87#define NUM_SOCKS 10
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Ben Lindstrome9c99912001-06-09 00:41:05 +000089/* -- tcp forwarding */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +000090/* special-case port number meaning allow any port */
91#define FWD_PERMIT_ANY_PORT 0
92
93/* special-case wildcard meaning allow any host */
94#define FWD_PERMIT_ANY_HOST "*"
95
96/* -- X11 forwarding */
97/* Maximum number of fake X11 displays to try. */
98#define MAX_DISPLAYS 1000
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Data structure for storing which hosts are permitted for forward requests.
102 * The local sides of any remote forwards are stored in this array to prevent
103 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
104 * network (which might be behind a firewall).
105 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000106/* XXX: streamlocal wants a path instead of host:port */
107/* Overload host_to_connect; we could just make this match Forward */
108/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100109typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000110 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000111 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000112 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000113 char *listen_path; /* Remote side should listen path. */
114 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000115 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116} ForwardPermission;
117
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000118typedef void chan_fn(struct ssh *, Channel *c,
119 fd_set *readset, fd_set *writeset);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000120
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000121/* Master structure for channels state */
122struct ssh_channels {
123 /*
124 * Pointer to an array containing all allocated channels. The array
125 * is dynamically extended as needed.
126 */
127 Channel **channels;
Damien Miller9b439df2006-07-24 14:04:00 +1000128
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000129 /*
130 * Size of the channel array. All slots of the array must always be
131 * initialized (at least the type field); unused slots set to NULL
132 */
133 u_int channels_alloc;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000135 /*
136 * Maximum file descriptor value used in any of the channels. This is
137 * updated in channel_new.
138 */
139 int channel_max_fd;
Damien Miller9b439df2006-07-24 14:04:00 +1000140
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000141 /*
142 * 'channel_pre*' are called just before select() to add any bits
143 * relevant to channels in the select bitmasks.
144 *
145 * 'channel_post*': perform any appropriate operations for
146 * channels which have events pending.
147 */
148 chan_fn **channel_pre;
149 chan_fn **channel_post;
Darren Tucker1338b9e2011-10-02 18:57:35 +1100150
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000151 /* -- tcp forwarding */
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000152
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000153 /* List of all permitted host/port pairs to connect by the user. */
154 ForwardPermission *permitted_opens;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000156 /* List of all permitted host/port pairs to connect by the admin. */
157 ForwardPermission *permitted_adm_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000158
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000159 /*
160 * Number of permitted host/port pairs in the array permitted by
161 * the user.
162 */
163 u_int num_permitted_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000164
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000165 /*
166 * Number of permitted host/port pair in the array permitted by
167 * the admin.
168 */
169 u_int num_adm_permitted_opens;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000171 /*
172 * If this is true, all opens are permitted. This is the case on
173 * the server on which we have to trust the client anyway, and the
174 * user could do anything after logging in anyway.
175 */
176 int all_opens_permitted;
Damien Miller13390022005-07-06 09:44:19 +1000177
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000178 /* -- X11 forwarding */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000179
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000180 /* Saved X11 local (client) display. */
181 char *x11_saved_display;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000182
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000183 /* Saved X11 authentication protocol name. */
184 char *x11_saved_proto;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000185
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000186 /* Saved X11 authentication data. This is the real data. */
187 char *x11_saved_data;
188 u_int x11_saved_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000189
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000190 /* Deadline after which all X11 connections are refused */
191 u_int x11_refuse_time;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000192
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000193 /*
194 * Fake X11 authentication data. This is what the server will be
195 * sending us; we should replace any occurrences of this by the
196 * real data.
197 */
198 u_char *x11_fake_data;
199 u_int x11_fake_data_len;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000200
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000201 /* AF_UNSPEC or AF_INET or AF_INET6 */
202 int IPv4or6;
203};
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000204
Ben Lindstrome9c99912001-06-09 00:41:05 +0000205/* helper */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000206static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000207static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000208
Damien Millerbd740252008-05-19 15:37:09 +1000209/* non-blocking connect helpers */
210static int connect_next(struct channel_connect *);
211static void channel_connect_ctx_free(struct channel_connect *);
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{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000439 channel_close_fd(ssh, &c->sock);
440 channel_close_fd(ssh, &c->rfd);
441 channel_close_fd(ssh, &c->wfd);
442 channel_close_fd(ssh, &c->efd);
443}
444
445static void
446fwd_perm_clear(ForwardPermission *fp)
447{
448 free(fp->host_to_connect);
449 free(fp->listen_host);
450 free(fp->listen_path);
451 bzero(fp, sizeof(*fp));
452}
453
454enum { FWDPERM_USER, FWDPERM_ADMIN };
455
456static int
457fwd_perm_list_add(struct ssh *ssh, int which,
458 const char *host_to_connect, int port_to_connect,
459 const char *listen_host, const char *listen_path, int listen_port,
460 Channel *downstream)
461{
462 ForwardPermission **fpl;
463 u_int n, *nfpl;
464
465 switch (which) {
466 case FWDPERM_USER:
467 fpl = &ssh->chanctxt->permitted_opens;
468 nfpl = &ssh->chanctxt->num_permitted_opens;
469 break;
470 case FWDPERM_ADMIN:
471 fpl = &ssh->chanctxt->permitted_adm_opens;
472 nfpl = &ssh->chanctxt->num_adm_permitted_opens;
473 break;
474 default:
475 fatal("%s: invalid list %d", __func__, which);
476 }
477
478 if (*nfpl >= INT_MAX)
479 fatal("%s: overflow", __func__);
480
481 *fpl = xrecallocarray(*fpl, *nfpl, *nfpl + 1, sizeof(**fpl));
482 n = (*nfpl)++;
483#define MAYBE_DUP(s) ((s == NULL) ? NULL : xstrdup(s))
484 (*fpl)[n].host_to_connect = MAYBE_DUP(host_to_connect);
485 (*fpl)[n].port_to_connect = port_to_connect;
486 (*fpl)[n].listen_host = MAYBE_DUP(listen_host);
487 (*fpl)[n].listen_path = MAYBE_DUP(listen_path);
488 (*fpl)[n].listen_port = listen_port;
489 (*fpl)[n].downstream = downstream;
490#undef MAYBE_DUP
491 return (int)n;
492}
493
494static void
495mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
496{
497 struct ssh_channels *sc = ssh->chanctxt;
498 ForwardPermission *fp;
499 int r;
500 u_int i;
501
502 for (i = 0; i < sc->num_permitted_opens; i++) {
503 fp = &sc->permitted_opens[i];
504 if (fp->downstream != c)
505 continue;
506
507 /* cancel on the server, since mux client is gone */
508 debug("channel %d: cleanup remote forward for %s:%u",
509 c->self, fp->listen_host, fp->listen_port);
510 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
511 (r = sshpkt_put_cstring(ssh,
512 "cancel-tcpip-forward")) != 0 ||
513 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
514 (r = sshpkt_put_cstring(ssh,
515 channel_rfwd_bind_host(fp->listen_host))) != 0 ||
516 (r = sshpkt_put_u32(ssh, fp->listen_port)) != 0 ||
517 (r = sshpkt_send(ssh)) != 0) {
518 fatal("%s: channel %i: %s", __func__,
519 c->self, ssh_err(r));
520 }
521 fwd_perm_clear(fp); /* unregister */
522 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000523}
524
525/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000526void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000527channel_free(struct ssh *ssh, Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000529 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000530 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000531 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000532 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000533 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000534
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000535 for (n = 0, i = 0; i < sc->channels_alloc; i++) {
536 if ((other = sc->channels[i]) == NULL)
537 continue;
538 n++;
539 /* detach from mux client and prepare for closing */
540 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
541 other->type == SSH_CHANNEL_MUX_PROXY &&
542 other->mux_ctx == c) {
543 other->mux_ctx = NULL;
544 other->type = SSH_CHANNEL_OPEN;
545 other->istate = CHAN_INPUT_CLOSED;
546 other->ostate = CHAN_OUTPUT_CLOSED;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000547 }
548 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000549 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000550 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000551
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000552 if (c->type == SSH_CHANNEL_MUX_CLIENT)
553 mux_remove_remote_forwardings(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000554
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000555 s = channel_open_message(ssh);
Damien Millerfbdeece2003-09-02 22:52:31 +1000556 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000557 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000558
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000559 channel_close_fds(ssh, c);
560 sshbuf_free(c->input);
561 sshbuf_free(c->output);
562 sshbuf_free(c->extended);
563 c->input = c->output = c->extended = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000564 free(c->remote_name);
565 c->remote_name = NULL;
566 free(c->path);
567 c->path = NULL;
568 free(c->listening_addr);
569 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000570 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
571 if (cc->abandon_cb != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000572 cc->abandon_cb(ssh, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +1000573 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100574 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000575 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000576 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000577 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000578 c->filter_cleanup(ssh, c->self, c->filter_ctx);
579 sc->channels[c->self] = NULL;
millert@openbsd.org3e8d1852017-09-19 12:10:30 +0000580 explicit_bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +1000581 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000582}
583
Ben Lindstrome9c99912001-06-09 00:41:05 +0000584void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000585channel_free_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000586{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000587 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000588
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000589 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
590 if (ssh->chanctxt->channels[i] != NULL)
591 channel_free(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000592}
593
594/*
595 * Closes the sockets/fds of all channels. This is used to close extra file
596 * descriptors after a fork.
597 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000599channel_close_all(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000600{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000601 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000602
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000603 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
604 if (ssh->chanctxt->channels[i] != NULL)
605 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000606}
607
608/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000609 * Stop listening to channels.
610 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000611void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000612channel_stop_listening(struct ssh *ssh)
Ben Lindstrom809744e2001-07-04 05:26:06 +0000613{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000614 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000615 Channel *c;
616
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000617 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
618 c = ssh->chanctxt->channels[i];
Ben Lindstrom809744e2001-07-04 05:26:06 +0000619 if (c != NULL) {
620 switch (c->type) {
621 case SSH_CHANNEL_AUTH_SOCKET:
622 case SSH_CHANNEL_PORT_LISTENER:
623 case SSH_CHANNEL_RPORT_LISTENER:
624 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000625 case SSH_CHANNEL_UNIX_LISTENER:
626 case SSH_CHANNEL_RUNIX_LISTENER:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000627 channel_close_fd(ssh, &c->sock);
628 channel_free(ssh, c);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000629 break;
630 }
631 }
632 }
633}
634
635/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000636 * Returns true if no channel has too much buffered data, and false if one or
637 * more channel is overfull.
638 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000639int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000640channel_not_very_much_buffered_data(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000641{
642 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000643 u_int maxsize = ssh_packet_get_maxsize(ssh);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644 Channel *c;
645
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000646 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
647 c = ssh->chanctxt->channels[i];
648 if (c == NULL || c->type != SSH_CHANNEL_OPEN)
649 continue;
650 if (sshbuf_len(c->output) > maxsize) {
651 debug2("channel %d: big output buffer %zu > %u",
652 c->self, sshbuf_len(c->output), maxsize);
653 return 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000654 }
655 }
656 return 1;
657}
658
659/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000660int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000661channel_still_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000662{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000663 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000664 Channel *c;
665
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000666 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
667 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668 if (c == NULL)
669 continue;
670 switch (c->type) {
671 case SSH_CHANNEL_X11_LISTENER:
672 case SSH_CHANNEL_PORT_LISTENER:
673 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100674 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000675 case SSH_CHANNEL_CLOSED:
676 case SSH_CHANNEL_AUTH_SOCKET:
677 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000678 case SSH_CHANNEL_RDYNAMIC_OPEN:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000679 case SSH_CHANNEL_CONNECTING:
680 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000681 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000682 case SSH_CHANNEL_UNIX_LISTENER:
683 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000684 continue;
685 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000686 continue;
687 case SSH_CHANNEL_OPENING:
688 case SSH_CHANNEL_OPEN:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000689 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100691 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000692 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000693 return 1;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000695 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000696 /* NOTREACHED */
697 }
698 }
699 return 0;
700}
701
702/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000703int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000704channel_find_open(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000705{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000706 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000707 Channel *c;
708
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000709 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
710 c = ssh->chanctxt->channels[i];
djm@openbsd.org9f532292017-09-12 06:35:31 +0000711 if (c == NULL || !c->have_remote_id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000712 continue;
713 switch (c->type) {
714 case SSH_CHANNEL_CLOSED:
715 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000716 case SSH_CHANNEL_RDYNAMIC_OPEN:
717 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000718 case SSH_CHANNEL_X11_LISTENER:
719 case SSH_CHANNEL_PORT_LISTENER:
720 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100721 case SSH_CHANNEL_MUX_LISTENER:
722 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000723 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000724 case SSH_CHANNEL_OPENING:
725 case SSH_CHANNEL_CONNECTING:
726 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000727 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000728 case SSH_CHANNEL_UNIX_LISTENER:
729 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000730 continue;
731 case SSH_CHANNEL_LARVAL:
732 case SSH_CHANNEL_AUTH_SOCKET:
733 case SSH_CHANNEL_OPEN:
734 case SSH_CHANNEL_X11_OPEN:
735 return i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000736 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000737 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000738 /* NOTREACHED */
739 }
740 }
741 return -1;
742}
743
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744/*
745 * Returns a message describing the currently open forwarded connections,
746 * suitable for sending to the client. The message contains crlf pairs for
747 * newlines.
748 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000749char *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000750channel_open_message(struct ssh *ssh)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000751{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000752 struct sshbuf *buf;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000754 u_int i;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000755 int r;
756 char *ret;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000758 if ((buf = sshbuf_new()) == NULL)
759 fatal("%s: sshbuf_new", __func__);
760 if ((r = sshbuf_putf(buf,
761 "The following connections are open:\r\n")) != 0)
762 fatal("%s: sshbuf_putf: %s", __func__, ssh_err(r));
763 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
764 c = ssh->chanctxt->channels[i];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000765 if (c == NULL)
766 continue;
767 switch (c->type) {
768 case SSH_CHANNEL_X11_LISTENER:
769 case SSH_CHANNEL_PORT_LISTENER:
770 case SSH_CHANNEL_RPORT_LISTENER:
771 case SSH_CHANNEL_CLOSED:
772 case SSH_CHANNEL_AUTH_SOCKET:
773 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000774 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100775 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000776 case SSH_CHANNEL_UNIX_LISTENER:
777 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000778 continue;
779 case SSH_CHANNEL_LARVAL:
780 case SSH_CHANNEL_OPENING:
781 case SSH_CHANNEL_CONNECTING:
782 case SSH_CHANNEL_DYNAMIC:
markus@openbsd.org609d7a62017-09-21 19:16:53 +0000783 case SSH_CHANNEL_RDYNAMIC_OPEN:
784 case SSH_CHANNEL_RDYNAMIC_FINISH:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000785 case SSH_CHANNEL_OPEN:
786 case SSH_CHANNEL_X11_OPEN:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000787 case SSH_CHANNEL_MUX_PROXY:
788 case SSH_CHANNEL_MUX_CLIENT:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000789 if ((r = sshbuf_putf(buf, " #%d %.300s "
djm@openbsd.org9f532292017-09-12 06:35:31 +0000790 "(t%d %s%u i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000791 c->self, c->remote_name,
djm@openbsd.org9f532292017-09-12 06:35:31 +0000792 c->type,
793 c->have_remote_id ? "r" : "nr", c->remote_id,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000794 c->istate, sshbuf_len(c->input),
795 c->ostate, sshbuf_len(c->output),
796 c->rfd, c->wfd, c->ctl_chan)) != 0)
797 fatal("%s: sshbuf_putf: %s",
798 __func__, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000799 continue;
800 default:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000801 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000802 /* NOTREACHED */
803 }
804 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000805 if ((ret = sshbuf_dup_string(buf)) == NULL)
806 fatal("%s: sshbuf_dup_string", __func__);
807 sshbuf_free(buf);
808 return ret;
809}
810
811static void
812open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
813{
814 int r;
815
816 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
817 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
818 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
819 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
820 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
821 fatal("%s: channel %i: open: %s", where, c->self, ssh_err(r));
822 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000823}
824
825void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000826channel_send_open(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000827{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000828 Channel *c = channel_lookup(ssh, id);
829 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000830
Ben Lindstrome9c99912001-06-09 00:41:05 +0000831 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000832 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000833 return;
834 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000835 debug2("channel %d: send open", id);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000836 open_preamble(ssh, __func__, c, c->ctype);
837 if ((r = sshpkt_send(ssh)) != 0)
838 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000839}
840
841void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000842channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000843{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000844 Channel *c = channel_lookup(ssh, id);
845 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000846
Ben Lindstrome9c99912001-06-09 00:41:05 +0000847 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000848 logit("%s: %d: unknown channel id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000849 return;
850 }
djm@openbsd.org9f532292017-09-12 06:35:31 +0000851 if (!c->have_remote_id)
852 fatal(":%s: channel %d: no remote id", __func__, c->self);
853
Damien Miller0e220db2004-06-15 10:34:08 +1000854 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000855 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
856 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
857 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
858 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
859 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
860 }
Ben Lindstrome9c99912001-06-09 00:41:05 +0000861}
Damien Miller4f7becb2006-03-26 14:10:14 +1100862
Ben Lindstrome9c99912001-06-09 00:41:05 +0000863void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000864channel_register_status_confirm(struct ssh *ssh, int id,
865 channel_confirm_cb *cb, channel_confirm_abandon_cb *abandon_cb, void *ctx)
Damien Millerb84886b2008-05-19 15:05:07 +1000866{
867 struct channel_confirm *cc;
868 Channel *c;
869
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000870 if ((c = channel_lookup(ssh, id)) == NULL)
871 fatal("%s: %d: bad id", __func__, id);
Damien Millerb84886b2008-05-19 15:05:07 +1000872
Damien Miller6c81fee2013-11-08 12:19:55 +1100873 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000874 cc->cb = cb;
875 cc->abandon_cb = abandon_cb;
876 cc->ctx = ctx;
877 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
878}
879
880void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000881channel_register_open_confirm(struct ssh *ssh, int id,
882 channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000883{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000884 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000885
Ben Lindstrome9c99912001-06-09 00:41:05 +0000886 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000887 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000888 return;
889 }
Damien Millerb84886b2008-05-19 15:05:07 +1000890 c->open_confirm = fn;
891 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000892}
Damien Miller4f7becb2006-03-26 14:10:14 +1100893
Ben Lindstrome9c99912001-06-09 00:41:05 +0000894void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000895channel_register_cleanup(struct ssh *ssh, int id,
896 channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000897{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000898 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000899
Ben Lindstrome9c99912001-06-09 00:41:05 +0000900 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000901 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000902 return;
903 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000904 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100905 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000906}
Damien Miller4f7becb2006-03-26 14:10:14 +1100907
Ben Lindstrome9c99912001-06-09 00:41:05 +0000908void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000909channel_cancel_cleanup(struct ssh *ssh, int id)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000910{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000911 Channel *c = channel_by_id(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000912
Ben Lindstrome9c99912001-06-09 00:41:05 +0000913 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000914 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000915 return;
916 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000917 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100918 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000919}
Damien Miller4f7becb2006-03-26 14:10:14 +1100920
Ben Lindstrome9c99912001-06-09 00:41:05 +0000921void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000922channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000923 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000924{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000925 Channel *c = channel_lookup(ssh, id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000926
Ben Lindstrome9c99912001-06-09 00:41:05 +0000927 if (c == NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000928 logit("%s: %d: bad id", __func__, id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000929 return;
930 }
Damien Miller077b2382005-12-31 16:22:32 +1100931 c->input_filter = ifn;
932 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000933 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000934 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000935}
936
937void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000938channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000939 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000940{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000941 Channel *c = channel_lookup(ssh, id);
942 int r;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000943
Ben Lindstrome9c99912001-06-09 00:41:05 +0000944 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
945 fatal("channel_activate for non-larval channel %d.", id);
djm@openbsd.org9f532292017-09-12 06:35:31 +0000946 if (!c->have_remote_id)
947 fatal(":%s: channel %d: no remote id", __func__, c->self);
948
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000949 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000950 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100951 c->local_window = c->local_window_max = window_max;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000952
953 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
954 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
955 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
956 (r = sshpkt_send(ssh)) != 0)
957 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000958}
959
Ben Lindstrombba81212001-06-25 05:01:22 +0000960static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000961channel_pre_listener(struct ssh *ssh, Channel *c,
962 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000963{
964 FD_SET(c->sock, readset);
965}
966
Ben Lindstrombba81212001-06-25 05:01:22 +0000967static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000968channel_pre_connecting(struct ssh *ssh, Channel *c,
969 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000970{
971 debug3("channel %d: waiting for connection", c->self);
972 FD_SET(c->sock, writeset);
973}
974
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000976channel_pre_open(struct ssh *ssh, Channel *c,
977 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000978{
Damien Miller33b13562000-04-04 14:38:59 +1000979 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000980 c->remote_window > 0 &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000981 sshbuf_len(c->input) < c->remote_window &&
982 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Miller33b13562000-04-04 14:38:59 +1000983 FD_SET(c->rfd, readset);
984 if (c->ostate == CHAN_OUTPUT_OPEN ||
985 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000986 if (sshbuf_len(c->output) > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000987 FD_SET(c->wfd, writeset);
988 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000989 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000990 debug2("channel %d: "
991 "obuf_empty delayed efd %d/(%zu)", c->self,
992 c->efd, sshbuf_len(c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000993 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +0000994 chan_obuf_empty(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +1000995 }
996 }
997 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000998 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
999 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +10001000 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001001 sshbuf_len(c->extended) > 0)
Damien Miller33b13562000-04-04 14:38:59 +10001002 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +10001003 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
1004 (c->extended_usage == CHAN_EXTENDED_READ ||
1005 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001006 sshbuf_len(c->extended) < c->remote_window)
Damien Miller33b13562000-04-04 14:38:59 +10001007 FD_SET(c->efd, readset);
1008 }
Damien Miller0e220db2004-06-15 10:34:08 +10001009 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +10001010}
1011
Damien Millerb38eff82000-04-01 11:09:21 +10001012/*
1013 * This is a special state for X11 authentication spoofing. An opened X11
1014 * connection (when authentication spoofing is being done) remains in this
1015 * state until the first packet has been completely read. The authentication
1016 * data in that packet is then substituted by the real data if it matches the
1017 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +10001018 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +00001019 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +10001020 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001021static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001022x11_open_helper(struct ssh *ssh, struct sshbuf *b)
Damien Millerb38eff82000-04-01 11:09:21 +10001023{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001024 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001025 u_char *ucp;
1026 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001027
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001028 /* Is this being called after the refusal deadline? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001029 if (sc->x11_refuse_time != 0 &&
1030 (u_int)monotime() >= sc->x11_refuse_time) {
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001031 verbose("Rejected X11 connection after ForwardX11Timeout "
1032 "expired");
1033 return -1;
1034 }
1035
Damien Millerb38eff82000-04-01 11:09:21 +10001036 /* Check if the fixed size part of the packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001037 if (sshbuf_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +10001038 return 0;
1039
1040 /* Parse the lengths of variable-length fields. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001041 ucp = sshbuf_mutable_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +10001042 if (ucp[0] == 0x42) { /* Byte order MSB first. */
1043 proto_len = 256 * ucp[6] + ucp[7];
1044 data_len = 256 * ucp[8] + ucp[9];
1045 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
1046 proto_len = ucp[6] + 256 * ucp[7];
1047 data_len = ucp[8] + 256 * ucp[9];
1048 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001049 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001050 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001051 return -1;
1052 }
1053
1054 /* Check if the whole packet is in buffer. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001055 if (sshbuf_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001056 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1057 return 0;
1058
1059 /* Check if authentication protocol matches. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001060 if (proto_len != strlen(sc->x11_saved_proto) ||
1061 memcmp(ucp + 12, sc->x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001062 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001063 return -1;
1064 }
1065 /* Check if authentication data matches our fake data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001066 if (data_len != sc->x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001067 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001068 sc->x11_fake_data, sc->x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001069 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001070 return -1;
1071 }
1072 /* Check fake data length */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001073 if (sc->x11_fake_data_len != sc->x11_saved_data_len) {
Damien Millerb38eff82000-04-01 11:09:21 +10001074 error("X11 fake_data_len %d != saved_data_len %d",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001075 sc->x11_fake_data_len, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001076 return -1;
1077 }
1078 /*
1079 * Received authentication protocol and data match
1080 * our fake data. Substitute the fake data with real
1081 * data.
1082 */
1083 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001084 sc->x11_saved_data, sc->x11_saved_data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001085 return 1;
1086}
1087
Ben Lindstrombba81212001-06-25 05:01:22 +00001088static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001089channel_pre_x11_open(struct ssh *ssh, Channel *c,
1090 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001091{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001092 int ret = x11_open_helper(ssh, c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001093
1094 /* c->force_drain = 1; */
1095
Damien Millerb38eff82000-04-01 11:09:21 +10001096 if (ret == 1) {
1097 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001098 channel_pre_open(ssh, c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001099 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001100 logit("X11 connection rejected because of wrong authentication.");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001101 debug2("X11 rejected %d i%d/o%d",
1102 c->self, c->istate, c->ostate);
1103 chan_read_failed(ssh, c);
1104 sshbuf_reset(c->input);
1105 chan_ibuf_empty(ssh, c);
1106 sshbuf_reset(c->output);
1107 chan_write_failed(ssh, c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001108 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001109 }
1110}
1111
Damien Millere1537f92010-01-26 13:26:22 +11001112static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001113channel_pre_mux_client(struct ssh *ssh,
1114 Channel *c, fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11001115{
Damien Millerd530f5f2010-05-21 14:57:10 +10001116 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001117 sshbuf_check_reserve(c->input, CHAN_RBUF) == 0)
Damien Millere1537f92010-01-26 13:26:22 +11001118 FD_SET(c->rfd, readset);
1119 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1120 /* clear buffer immediately (discard any partial packet) */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001121 sshbuf_reset(c->input);
1122 chan_ibuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001123 /* Start output drain. XXX just kill chan? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001124 chan_rcvd_oclose(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001125 }
1126 if (c->ostate == CHAN_OUTPUT_OPEN ||
1127 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001128 if (sshbuf_len(c->output) > 0)
Damien Millere1537f92010-01-26 13:26:22 +11001129 FD_SET(c->wfd, writeset);
1130 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001131 chan_obuf_empty(ssh, c);
Damien Millere1537f92010-01-26 13:26:22 +11001132 }
1133}
1134
Ben Lindstromb3921512001-04-11 15:57:50 +00001135/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +00001136static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001137channel_decode_socks4(Channel *c, struct sshbuf *input, struct sshbuf *output)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001138{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001139 const u_char *p;
1140 char *host;
Damien Miller1781f532009-01-28 16:24:41 +11001141 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001142 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001143 struct {
1144 u_int8_t version;
1145 u_int8_t command;
1146 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001147 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001148 } s4_req, s4_rsp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001149 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001150
Ben Lindstromb3921512001-04-11 15:57:50 +00001151 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001152
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001153 have = sshbuf_len(input);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001154 len = sizeof(s4_req);
1155 if (have < len)
1156 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001157 p = sshbuf_ptr(input);
Damien Miller1781f532009-01-28 16:24:41 +11001158
1159 need = 1;
1160 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1161 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1162 debug2("channel %d: socks4a request", c->self);
1163 /* ... and needs an extra string (the hostname) */
1164 need = 2;
1165 }
1166 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001167 for (found = 0, i = len; i < have; i++) {
1168 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001169 found++;
1170 if (found == need)
1171 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001172 }
1173 if (i > 1024) {
1174 /* the peer is probably sending garbage */
1175 debug("channel %d: decode socks4: too long",
1176 c->self);
1177 return -1;
1178 }
1179 }
Damien Miller1781f532009-01-28 16:24:41 +11001180 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001181 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001182 if ((r = sshbuf_get(input, &s4_req.version, 1)) != 0 ||
1183 (r = sshbuf_get(input, &s4_req.command, 1)) != 0 ||
1184 (r = sshbuf_get(input, &s4_req.dest_port, 2)) != 0 ||
1185 (r = sshbuf_get(input, &s4_req.dest_addr, 4)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001186 debug("channels %d: decode socks4: %s", c->self, ssh_err(r));
1187 return -1;
1188 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001189 have = sshbuf_len(input);
1190 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001191 if (memchr(p, '\0', have) == NULL) {
1192 error("channel %d: decode socks4: user not nul terminated",
Damien Miller13481292014-02-27 10:18:32 +11001193 c->self);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001194 return -1;
1195 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001196 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001197 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001198 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001199 strlcpy(username, p, sizeof(username));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001200 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001201 fatal("%s: channel %d: consume: %s", __func__,
1202 c->self, ssh_err(r));
1203 }
Darren Tuckera627d422013-06-02 07:31:17 +10001204 free(c->path);
1205 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001206 if (need == 1) { /* SOCKS4: one string */
1207 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001208 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001209 } else { /* SOCKS4A: two strings */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001210 have = sshbuf_len(input);
1211 p = sshbuf_ptr(input);
djm@openbsd.orgc04e9792017-05-31 00:43:04 +00001212 if (memchr(p, '\0', have) == NULL) {
1213 error("channel %d: decode socks4a: host not nul "
1214 "terminated", c->self);
1215 return -1;
1216 }
Damien Miller1781f532009-01-28 16:24:41 +11001217 len = strlen(p);
1218 debug2("channel %d: decode socks4a: host %s/%d",
1219 c->self, p, len);
1220 len++; /* trailing '\0' */
Damien Millera1c1b6c2009-01-28 16:29:49 +11001221 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001222 error("channel %d: hostname \"%.100s\" too long",
1223 c->self, p);
1224 return -1;
1225 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001226 c->path = xstrdup(p);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001227 if ((r = sshbuf_consume(input, len)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001228 fatal("%s: channel %d: consume: %s", __func__,
1229 c->self, ssh_err(r));
1230 }
Damien Miller1781f532009-01-28 16:24:41 +11001231 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001232 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001233
Damien Millerfbdeece2003-09-02 22:52:31 +10001234 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001235 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001236
Ben Lindstromb3921512001-04-11 15:57:50 +00001237 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001238 debug("channel %d: cannot handle: %s cn %d",
1239 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001240 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001241 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001242 s4_rsp.version = 0; /* vn: 0 for reply */
1243 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001244 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001245 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001246 if ((r = sshbuf_put(output, &s4_rsp, sizeof(s4_rsp))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001247 fatal("%s: channel %d: append reply: %s", __func__,
1248 c->self, ssh_err(r));
1249 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001250 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001251}
1252
Darren Tucker46471c92003-07-03 13:55:19 +10001253/* try to decode a socks5 header */
1254#define SSH_SOCKS5_AUTHDONE 0x1000
1255#define SSH_SOCKS5_NOAUTH 0x00
1256#define SSH_SOCKS5_IPV4 0x01
1257#define SSH_SOCKS5_DOMAIN 0x03
1258#define SSH_SOCKS5_IPV6 0x04
1259#define SSH_SOCKS5_CONNECT 0x01
1260#define SSH_SOCKS5_SUCCESS 0x00
1261
1262static int
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001263channel_decode_socks5(Channel *c, struct sshbuf *input, struct sshbuf *output)
Darren Tucker46471c92003-07-03 13:55:19 +10001264{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001265 /* XXX use get/put_u8 instead of trusting struct padding */
Darren Tucker46471c92003-07-03 13:55:19 +10001266 struct {
1267 u_int8_t version;
1268 u_int8_t command;
1269 u_int8_t reserved;
1270 u_int8_t atyp;
1271 } s5_req, s5_rsp;
1272 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001273 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001274 const u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001275 u_int have, need, i, found, nmethods, addrlen, af;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001276 int r;
Darren Tucker46471c92003-07-03 13:55:19 +10001277
1278 debug2("channel %d: decode socks5", c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001279 p = sshbuf_ptr(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001280 if (p[0] != 0x05)
1281 return -1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001282 have = sshbuf_len(input);
Darren Tucker46471c92003-07-03 13:55:19 +10001283 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1284 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001285 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001286 return 0;
1287 nmethods = p[1];
1288 if (have < nmethods + 2)
1289 return 0;
1290 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001291 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001292 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001293 found = 1;
1294 break;
1295 }
1296 }
1297 if (!found) {
1298 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1299 c->self);
1300 return -1;
1301 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001302 if ((r = sshbuf_consume(input, nmethods + 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001303 fatal("%s: channel %d: consume: %s", __func__,
1304 c->self, ssh_err(r));
1305 }
1306 /* version, method */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001307 if ((r = sshbuf_put_u8(output, 0x05)) != 0 ||
1308 (r = sshbuf_put_u8(output, SSH_SOCKS5_NOAUTH)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001309 fatal("%s: channel %d: append reply: %s", __func__,
1310 c->self, ssh_err(r));
1311 }
Darren Tucker46471c92003-07-03 13:55:19 +10001312 c->flags |= SSH_SOCKS5_AUTHDONE;
1313 debug2("channel %d: socks5 auth done", c->self);
1314 return 0; /* need more */
1315 }
1316 debug2("channel %d: socks5 post auth", c->self);
1317 if (have < sizeof(s5_req)+1)
1318 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001319 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001320 if (s5_req.version != 0x05 ||
1321 s5_req.command != SSH_SOCKS5_CONNECT ||
1322 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001323 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001324 return -1;
1325 }
Darren Tucker47eede72005-03-14 23:08:12 +11001326 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001327 case SSH_SOCKS5_IPV4:
1328 addrlen = 4;
1329 af = AF_INET;
1330 break;
1331 case SSH_SOCKS5_DOMAIN:
1332 addrlen = p[sizeof(s5_req)];
1333 af = -1;
1334 break;
1335 case SSH_SOCKS5_IPV6:
1336 addrlen = 16;
1337 af = AF_INET6;
1338 break;
1339 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001340 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001341 return -1;
1342 }
Damien Miller0f077072006-07-10 22:21:02 +10001343 need = sizeof(s5_req) + addrlen + 2;
1344 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1345 need++;
1346 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001347 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001348 if ((r = sshbuf_consume(input, sizeof(s5_req))) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001349 fatal("%s: channel %d: consume: %s", __func__,
1350 c->self, ssh_err(r));
1351 }
1352 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
1353 /* host string length */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001354 if ((r = sshbuf_consume(input, 1)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001355 fatal("%s: channel %d: consume: %s", __func__,
1356 c->self, ssh_err(r));
1357 }
1358 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001359 if ((r = sshbuf_get(input, &dest_addr, addrlen)) != 0 ||
1360 (r = sshbuf_get(input, &dest_port, 2)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001361 debug("channel %d: parse addr/port: %s", c->self, ssh_err(r));
1362 return -1;
1363 }
Darren Tucker46471c92003-07-03 13:55:19 +10001364 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001365 free(c->path);
1366 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001367 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001368 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001369 error("channel %d: dynamic request: socks5 hostname "
1370 "\"%.100s\" too long", c->self, dest_addr);
1371 return -1;
1372 }
1373 c->path = xstrdup(dest_addr);
1374 } else {
1375 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1376 return -1;
1377 c->path = xstrdup(ntop);
1378 }
Darren Tucker46471c92003-07-03 13:55:19 +10001379 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001380
Damien Millerfbdeece2003-09-02 22:52:31 +10001381 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001382 c->self, c->path, c->host_port, s5_req.command);
1383
1384 s5_rsp.version = 0x05;
1385 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1386 s5_rsp.reserved = 0; /* ignored */
1387 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001388 dest_port = 0; /* ignored */
1389
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001390 if ((r = sshbuf_put(output, &s5_rsp, sizeof(s5_rsp))) != 0 ||
1391 (r = sshbuf_put_u32(output, ntohl(INADDR_ANY))) != 0 ||
1392 (r = sshbuf_put(output, &dest_port, sizeof(dest_port))) != 0)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001393 fatal("%s: channel %d: append reply: %s", __func__,
1394 c->self, ssh_err(r));
Darren Tucker46471c92003-07-03 13:55:19 +10001395 return 1;
1396}
1397
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001398Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001399channel_connect_stdio_fwd(struct ssh *ssh,
1400 const char *host_to_connect, u_short port_to_connect, int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001401{
1402 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001403
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001404 debug("%s %s:%d", __func__, host_to_connect, port_to_connect);
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001405
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001406 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001407 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1408 0, "stdio-forward", /*nonblock*/0);
1409
1410 c->path = xstrdup(host_to_connect);
1411 c->host_port = port_to_connect;
1412 c->listening_port = 0;
1413 c->force_drain = 1;
1414
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001415 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1416 port_open_helper(ssh, c, "direct-tcpip");
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001417
1418 return c;
1419}
1420
Ben Lindstromb3921512001-04-11 15:57:50 +00001421/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001422static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001423channel_pre_dynamic(struct ssh *ssh, Channel *c,
1424 fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001425{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001426 const u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001427 u_int have;
1428 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001429
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001430 have = sshbuf_len(c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001431 debug2("channel %d: pre_dynamic: have %d", c->self, have);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001432 /* sshbuf_dump(c->input, stderr); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001433 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001434 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001435 /* need more */
1436 FD_SET(c->sock, readset);
1437 return;
1438 }
1439 /* try to guess the protocol */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001440 p = sshbuf_ptr(c->input);
1441 /* XXX sshbuf_peek_u8? */
Ben Lindstromb3921512001-04-11 15:57:50 +00001442 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001443 case 0x04:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001444 ret = channel_decode_socks4(c, c->input, c->output);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001445 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001446 case 0x05:
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001447 ret = channel_decode_socks5(c, c->input, c->output);
Darren Tucker46471c92003-07-03 13:55:19 +10001448 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001449 default:
1450 ret = -1;
1451 break;
1452 }
1453 if (ret < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001454 chan_mark_dead(ssh, c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001455 } else if (ret == 0) {
1456 debug2("channel %d: pre_dynamic: need more", c->self);
1457 /* need more */
1458 FD_SET(c->sock, readset);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001459 if (sshbuf_len(c->output))
1460 FD_SET(c->sock, writeset);
Ben Lindstromb3921512001-04-11 15:57:50 +00001461 } else {
1462 /* switch to the next state */
1463 c->type = SSH_CHANNEL_OPENING;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001464 port_open_helper(ssh, c, "direct-tcpip");
Ben Lindstromb3921512001-04-11 15:57:50 +00001465 }
1466}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001467
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001468/* simulate read-error */
1469static void
1470rdynamic_close(struct ssh *ssh, Channel *c)
1471{
1472 c->type = SSH_CHANNEL_OPEN;
1473 chan_read_failed(ssh, c);
1474 sshbuf_reset(c->input);
1475 chan_ibuf_empty(ssh, c);
1476 sshbuf_reset(c->output);
1477 chan_write_failed(ssh, c);
1478}
1479
1480/* reverse dynamic port forwarding */
1481static void
1482channel_before_prepare_select_rdynamic(struct ssh *ssh, Channel *c)
1483{
1484 const u_char *p;
1485 u_int have, len;
1486 int r, ret;
1487
1488 have = sshbuf_len(c->output);
1489 debug2("channel %d: pre_rdynamic: have %d", c->self, have);
1490 /* sshbuf_dump(c->output, stderr); */
1491 /* EOF received */
1492 if (c->flags & CHAN_EOF_RCVD) {
1493 if ((r = sshbuf_consume(c->output, have)) != 0) {
1494 fatal("%s: channel %d: consume: %s",
1495 __func__, c->self, ssh_err(r));
1496 }
1497 rdynamic_close(ssh, c);
1498 return;
1499 }
1500 /* check if the fixed size part of the packet is in buffer. */
1501 if (have < 3)
1502 return;
1503 /* try to guess the protocol */
1504 p = sshbuf_ptr(c->output);
1505 switch (p[0]) {
1506 case 0x04:
1507 /* switch input/output for reverse forwarding */
1508 ret = channel_decode_socks4(c, c->output, c->input);
1509 break;
1510 case 0x05:
1511 ret = channel_decode_socks5(c, c->output, c->input);
1512 break;
1513 default:
1514 ret = -1;
1515 break;
1516 }
1517 if (ret < 0) {
1518 rdynamic_close(ssh, c);
1519 } else if (ret == 0) {
1520 debug2("channel %d: pre_rdynamic: need more", c->self);
1521 /* send socks request to peer */
1522 len = sshbuf_len(c->input);
1523 if (len > 0 && len < c->remote_window) {
1524 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1525 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1526 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1527 (r = sshpkt_send(ssh)) != 0) {
1528 fatal("%s: channel %i: rdynamic: %s", __func__,
1529 c->self, ssh_err(r));
1530 }
1531 if ((r = sshbuf_consume(c->input, len)) != 0) {
1532 fatal("%s: channel %d: consume: %s",
1533 __func__, c->self, ssh_err(r));
1534 }
1535 c->remote_window -= len;
1536 }
1537 } else if (rdynamic_connect_finish(ssh, c) < 0) {
1538 /* the connect failed */
1539 rdynamic_close(ssh, c);
1540 }
1541}
1542
Damien Millerb38eff82000-04-01 11:09:21 +10001543/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001544static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001545channel_post_x11_listener(struct ssh *ssh, Channel *c,
1546 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001547{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001548 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001549 struct sockaddr_storage addr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001550 int r, newsock, oerrno, remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001551 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001552 char buf[16384], *remote_ipaddr;
Damien Millerb38eff82000-04-01 11:09:21 +10001553
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001554 if (!FD_ISSET(c->sock, readset))
1555 return;
Damien Millerbd483e72000-04-30 10:00:53 +10001556
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001557 debug("X11 connection requested.");
1558 addrlen = sizeof(addr);
1559 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1560 if (c->single_connection) {
1561 oerrno = errno;
1562 debug2("single_connection: closing X11 listener.");
1563 channel_close_fd(ssh, &c->sock);
1564 chan_mark_dead(ssh, c);
1565 errno = oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001566 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001567 if (newsock < 0) {
1568 if (errno != EINTR && errno != EWOULDBLOCK &&
1569 errno != ECONNABORTED)
1570 error("accept: %.100s", strerror(errno));
1571 if (errno == EMFILE || errno == ENFILE)
1572 c->notbefore = monotime() + 1;
1573 return;
1574 }
1575 set_nodelay(newsock);
1576 remote_ipaddr = get_peer_ipaddr(newsock);
1577 remote_port = get_peer_port(newsock);
1578 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1579 remote_ipaddr, remote_port);
1580
1581 nc = channel_new(ssh, "accepted x11 socket",
1582 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1583 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1584 open_preamble(ssh, __func__, nc, "x11");
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001585 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1586 (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001587 fatal("%s: channel %i: reply %s", __func__,
1588 c->self, ssh_err(r));
1589 }
1590 if ((r = sshpkt_send(ssh)) != 0)
1591 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
1592 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001593}
1594
Ben Lindstrombba81212001-06-25 05:01:22 +00001595static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001596port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001597{
Damien Miller4def1842013-12-29 17:45:26 +11001598 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001599 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001600 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001601 int remote_port = get_peer_port(c->sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001602 int r;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001603
Damien Millerd6369432010-02-02 17:02:07 +11001604 if (remote_port == -1) {
1605 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001606 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001607 remote_ipaddr = xstrdup("127.0.0.1");
1608 remote_port = 65535;
1609 }
1610
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001611 free(c->remote_name);
1612 xasprintf(&c->remote_name,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001613 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001614 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001615 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001616 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001617
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001618 open_preamble(ssh, __func__, c, rtype);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001619 if (strcmp(rtype, "direct-tcpip") == 0) {
1620 /* target host, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001621 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1622 (r = sshpkt_put_u32(ssh, c->host_port)) != 0) {
1623 fatal("%s: channel %i: reply %s", __func__,
1624 c->self, ssh_err(r));
1625 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001626 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1627 /* target path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001628 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1629 fatal("%s: channel %i: reply %s", __func__,
1630 c->self, ssh_err(r));
1631 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001632 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1633 /* listen path */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001634 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0) {
1635 fatal("%s: channel %i: reply %s", __func__,
1636 c->self, ssh_err(r));
1637 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001638 } else {
1639 /* listen address, port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001640 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1641 (r = sshpkt_put_u32(ssh, local_port)) != 0) {
1642 fatal("%s: channel %i: reply %s", __func__,
1643 c->self, ssh_err(r));
1644 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001645 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001646 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1647 /* reserved for future owner/mode info */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001648 if ((r = sshpkt_put_cstring(ssh, "")) != 0) {
1649 fatal("%s: channel %i: reply %s", __func__,
1650 c->self, ssh_err(r));
1651 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001652 } else {
1653 /* originator host and port */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001654 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1655 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0) {
1656 fatal("%s: channel %i: reply %s", __func__,
1657 c->self, ssh_err(r));
1658 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001659 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001660 if ((r = sshpkt_send(ssh)) != 0)
1661 fatal("%s: channel %i: send %s", __func__, c->self, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10001662 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001663 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001664}
1665
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001666void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001667channel_set_x11_refuse_time(struct ssh *ssh, u_int refuse_time)
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001668{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001669 ssh->chanctxt->x11_refuse_time = refuse_time;
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001670}
1671
Damien Millerb38eff82000-04-01 11:09:21 +10001672/*
1673 * This socket is listening for connections to a forwarded TCP/IP port.
1674 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001675static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001676channel_post_port_listener(struct ssh *ssh, Channel *c,
1677 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001678{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001679 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001680 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001681 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001682 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001683 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001684
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001685 if (!FD_ISSET(c->sock, readset))
1686 return;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001687
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001688 debug("Connection to port %d forwarding to %.100s port %d requested.",
1689 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001690
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001691 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1692 nextstate = SSH_CHANNEL_OPENING;
1693 rtype = "forwarded-tcpip";
1694 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1695 nextstate = SSH_CHANNEL_OPENING;
1696 rtype = "forwarded-streamlocal@openssh.com";
1697 } else if (c->host_port == PORT_STREAMLOCAL) {
1698 nextstate = SSH_CHANNEL_OPENING;
1699 rtype = "direct-streamlocal@openssh.com";
1700 } else if (c->host_port == 0) {
1701 nextstate = SSH_CHANNEL_DYNAMIC;
1702 rtype = "dynamic-tcpip";
1703 } else {
1704 nextstate = SSH_CHANNEL_OPENING;
1705 rtype = "direct-tcpip";
Damien Millerb38eff82000-04-01 11:09:21 +10001706 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001707
1708 addrlen = sizeof(addr);
1709 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1710 if (newsock < 0) {
1711 if (errno != EINTR && errno != EWOULDBLOCK &&
1712 errno != ECONNABORTED)
1713 error("accept: %.100s", strerror(errno));
1714 if (errno == EMFILE || errno == ENFILE)
1715 c->notbefore = monotime() + 1;
1716 return;
1717 }
1718 if (c->host_port != PORT_STREAMLOCAL)
1719 set_nodelay(newsock);
1720 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1721 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1722 nc->listening_port = c->listening_port;
1723 nc->host_port = c->host_port;
1724 if (c->path != NULL)
1725 nc->path = xstrdup(c->path);
1726
1727 if (nextstate != SSH_CHANNEL_DYNAMIC)
1728 port_open_helper(ssh, nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001729}
1730
1731/*
1732 * This is the authentication agent socket listening for connections from
1733 * clients.
1734 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001735static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001736channel_post_auth_listener(struct ssh *ssh, Channel *c,
1737 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001738{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001739 Channel *nc;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001740 int r, newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001741 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001742 socklen_t addrlen;
1743
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001744 if (!FD_ISSET(c->sock, readset))
1745 return;
1746
1747 addrlen = sizeof(addr);
1748 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1749 if (newsock < 0) {
1750 error("accept from auth socket: %.100s", strerror(errno));
1751 if (errno == EMFILE || errno == ENFILE)
1752 c->notbefore = monotime() + 1;
1753 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001754 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001755 nc = channel_new(ssh, "accepted auth socket",
1756 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1757 c->local_window_max, c->local_maxpacket,
1758 0, "accepted auth socket", 1);
1759 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
1760 if ((r = sshpkt_send(ssh)) != 0)
1761 fatal("%s: channel %i: %s", __func__, c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001762}
1763
Ben Lindstrombba81212001-06-25 05:01:22 +00001764static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001765channel_post_connecting(struct ssh *ssh, Channel *c,
1766 fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001767{
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001768 int err = 0, sock, isopen, r;
Ben Lindstrom11180952001-07-04 05:13:35 +00001769 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001770
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001771 if (!FD_ISSET(c->sock, writeset))
1772 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00001773 if (!c->have_remote_id)
1774 fatal(":%s: channel %d: no remote id", __func__, c->self);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001775 /* for rdynamic the OPEN_CONFIRMATION has been sent already */
1776 isopen = (c->type == SSH_CHANNEL_RDYNAMIC_FINISH);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001777 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1778 err = errno;
1779 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001780 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001781 if (err == 0) {
1782 debug("channel %d: connected to %s port %d",
1783 c->self, c->connect_ctx.host, c->connect_ctx.port);
1784 channel_connect_ctx_free(&c->connect_ctx);
1785 c->type = SSH_CHANNEL_OPEN;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001786 if (isopen) {
1787 /* no message necessary */
1788 } else {
1789 if ((r = sshpkt_start(ssh,
1790 SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1791 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1792 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1793 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1794 (r = sshpkt_put_u32(ssh, c->local_maxpacket))
1795 != 0)
1796 fatal("%s: channel %i: confirm: %s", __func__,
1797 c->self, ssh_err(r));
1798 if ((r = sshpkt_send(ssh)) != 0)
1799 fatal("%s: channel %i: %s", __func__, c->self,
1800 ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001801 }
1802 } else {
1803 debug("channel %d: connection failed: %s",
1804 c->self, strerror(err));
1805 /* Try next address, if any */
1806 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1807 close(c->sock);
1808 c->sock = c->rfd = c->wfd = sock;
1809 channel_find_maxfd(ssh->chanctxt);
1810 return;
1811 }
1812 /* Exhausted all addresses */
1813 error("connect_to %.100s port %d: failed.",
1814 c->connect_ctx.host, c->connect_ctx.port);
1815 channel_connect_ctx_free(&c->connect_ctx);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001816 if (isopen) {
1817 rdynamic_close(ssh, c);
1818 } else {
1819 if ((r = sshpkt_start(ssh,
1820 SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1821 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001822 (r = sshpkt_put_u32(ssh,
1823 SSH2_OPEN_CONNECT_FAILED)) != 0 ||
1824 (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
1825 (r = sshpkt_put_cstring(ssh, "")) != 0) {
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001826 fatal("%s: channel %i: failure: %s", __func__,
1827 c->self, ssh_err(r));
djm@openbsd.org14b5c632018-01-23 05:27:21 +00001828 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00001829 if ((r = sshpkt_send(ssh)) != 0)
1830 fatal("%s: channel %i: %s", __func__, c->self,
1831 ssh_err(r));
1832 chan_mark_dead(ssh, c);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001833 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001834 }
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001835}
1836
Ben Lindstrombba81212001-06-25 05:01:22 +00001837static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001838channel_handle_rfd(struct ssh *ssh, Channel *c,
1839 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001840{
Darren Tucker11327cc2005-03-14 23:22:25 +11001841 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001842 ssize_t len;
1843 int r, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001844
Damien Miller835284b2007-06-11 13:03:16 +10001845 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001846
1847 if (c->rfd == -1 || (!force && !FD_ISSET(c->rfd, readset)))
1848 return 1;
1849
1850 errno = 0;
1851 len = read(c->rfd, buf, sizeof(buf));
1852 if (len < 0 && (errno == EINTR ||
1853 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1854 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001855#ifndef PTY_ZEROREAD
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001856 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001857#else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001858 if ((!c->isatty && len <= 0) ||
1859 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001860#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001861 debug2("channel %d: read<=0 rfd %d len %zd",
1862 c->self, c->rfd, len);
1863 if (c->type != SSH_CHANNEL_OPEN) {
1864 debug2("channel %d: not open", c->self);
1865 chan_mark_dead(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001866 return -1;
Damien Millerad833b32000-08-23 10:46:23 +10001867 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001868 chan_read_failed(ssh, c);
Damien Millerad833b32000-08-23 10:46:23 +10001869 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001870 return -1;
1871 }
1872 if (c->input_filter != NULL) {
1873 if (c->input_filter(ssh, c, buf, len) == -1) {
1874 debug2("channel %d: filter stops", c->self);
1875 chan_read_failed(ssh, c);
1876 }
1877 } else if (c->datagram) {
1878 if ((r = sshbuf_put_string(c->input, buf, len)) != 0)
1879 fatal("%s: channel %d: put datagram: %s", __func__,
1880 c->self, ssh_err(r));
1881 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
1882 fatal("%s: channel %d: put data: %s", __func__,
1883 c->self, ssh_err(r));
Damien Millerb38eff82000-04-01 11:09:21 +10001884 }
1885 return 1;
1886}
Damien Miller4f7becb2006-03-26 14:10:14 +11001887
Ben Lindstrombba81212001-06-25 05:01:22 +00001888static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001889channel_handle_wfd(struct ssh *ssh, Channel *c,
1890 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001891{
Ben Lindstrome229b252001-03-05 06:28:06 +00001892 struct termios tio;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001893 u_char *data = NULL, *buf; /* XXX const; need filter API change */
1894 size_t dlen, olen = 0;
1895 int r, len;
1896
1897 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
1898 sshbuf_len(c->output) == 0)
1899 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001900
1901 /* Send buffered output data to the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001902 olen = sshbuf_len(c->output);
1903 if (c->output_filter != NULL) {
1904 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
1905 debug2("channel %d: filter stops", c->self);
1906 if (c->type != SSH_CHANNEL_OPEN)
1907 chan_mark_dead(ssh, c);
1908 else
1909 chan_write_failed(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10001910 return -1;
1911 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001912 } else if (c->datagram) {
1913 if ((r = sshbuf_get_string(c->output, &data, &dlen)) != 0)
1914 fatal("%s: channel %d: get datagram: %s", __func__,
1915 c->self, ssh_err(r));
djm@openbsd.org9145a732017-09-12 07:32:04 +00001916 buf = data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001917 } else {
1918 buf = data = sshbuf_mutable_ptr(c->output);
1919 dlen = sshbuf_len(c->output);
1920 }
1921
1922 if (c->datagram) {
1923 /* ignore truncated writes, datagrams might get lost */
djm@openbsd.orgc704f642017-09-24 09:50:01 +00001924 len = write(c->wfd, buf, dlen);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001925 free(data);
1926 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1927 errno == EWOULDBLOCK))
1928 return 1;
1929 if (len <= 0)
1930 goto write_fail;
1931 goto out;
1932 }
1933
1934#ifdef _AIX
1935 /* XXX: Later AIX versions can't push as much data to tty */
1936 if (c->wfd_isatty)
1937 dlen = MIN(dlen, 8*1024);
Darren Tucker3980b632009-08-28 11:02:37 +10001938#endif
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001939
1940 len = write(c->wfd, buf, dlen);
1941 if (len < 0 &&
1942 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
1943 return 1;
1944 if (len <= 0) {
1945 write_fail:
1946 if (c->type != SSH_CHANNEL_OPEN) {
1947 debug2("channel %d: not open", c->self);
1948 chan_mark_dead(ssh, c);
1949 return -1;
1950 } else {
1951 chan_write_failed(ssh, c);
1952 }
1953 return -1;
1954 }
1955#ifndef BROKEN_TCGETATTR_ICANON
1956 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
1957 if (tcgetattr(c->wfd, &tio) == 0 &&
1958 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1959 /*
1960 * Simulate echo to reduce the impact of
1961 * traffic analysis. We need to match the
1962 * size of a SSH2_MSG_CHANNEL_DATA message
1963 * (4 byte channel id + buf)
1964 */
1965 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
1966 (r = sshpkt_send(ssh)) != 0)
1967 fatal("%s: channel %d: ignore: %s",
1968 __func__, c->self, ssh_err(r));
1969 }
1970 }
1971#endif /* BROKEN_TCGETATTR_ICANON */
1972 if ((r = sshbuf_consume(c->output, len)) != 0) {
1973 fatal("%s: channel %d: consume: %s",
1974 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10001975 }
Damien Miller7d457182010-08-05 23:09:48 +10001976 out:
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001977 c->local_consumed += olen - sshbuf_len(c->output);
1978
Damien Miller33b13562000-04-04 14:38:59 +10001979 return 1;
1980}
Damien Miller4f7becb2006-03-26 14:10:14 +11001981
Ben Lindstrombba81212001-06-25 05:01:22 +00001982static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00001983channel_handle_efd_write(struct ssh *ssh, Channel *c,
1984 fd_set *readset, fd_set *writeset)
1985{
1986 int r;
1987 ssize_t len;
1988
1989 if (!FD_ISSET(c->efd, writeset) || sshbuf_len(c->extended) == 0)
1990 return 1;
1991
1992 len = write(c->efd, sshbuf_ptr(c->extended),
1993 sshbuf_len(c->extended));
1994 debug2("channel %d: written %zd to efd %d", c->self, len, c->efd);
1995 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1996 errno == EWOULDBLOCK))
1997 return 1;
1998 if (len <= 0) {
1999 debug2("channel %d: closing write-efd %d", c->self, c->efd);
2000 channel_close_fd(ssh, &c->efd);
2001 } else {
2002 if ((r = sshbuf_consume(c->extended, len)) != 0) {
2003 fatal("%s: channel %d: consume: %s",
2004 __func__, c->self, ssh_err(r));
2005 }
2006 c->local_consumed += len;
2007 }
2008 return 1;
2009}
2010
2011static int
2012channel_handle_efd_read(struct ssh *ssh, Channel *c,
2013 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002014{
Darren Tucker11327cc2005-03-14 23:22:25 +11002015 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002016 int r;
2017 ssize_t len;
Damien Miller33b13562000-04-04 14:38:59 +10002018
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002019 if (!c->detach_close && !FD_ISSET(c->efd, readset))
2020 return 1;
2021
2022 len = read(c->efd, buf, sizeof(buf));
2023 debug2("channel %d: read %zd from efd %d", c->self, len, c->efd);
2024 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
2025 errno == EWOULDBLOCK) && !c->detach_close)))
2026 return 1;
2027 if (len <= 0) {
2028 debug2("channel %d: closing read-efd %d",
2029 c->self, c->efd);
2030 channel_close_fd(ssh, &c->efd);
2031 } else {
2032 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
2033 debug3("channel %d: discard efd",
2034 c->self);
2035 } else if ((r = sshbuf_put(c->extended, buf, len)) != 0) {
2036 fatal("%s: channel %d: append: %s",
2037 __func__, c->self, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002038 }
2039 }
2040 return 1;
2041}
Damien Miller4f7becb2006-03-26 14:10:14 +11002042
Damien Miller0e220db2004-06-15 10:34:08 +10002043static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002044channel_handle_efd(struct ssh *ssh, Channel *c,
2045 fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10002046{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002047 if (c->efd == -1)
2048 return 1;
2049
2050 /** XXX handle drain efd, too */
2051
2052 if (c->extended_usage == CHAN_EXTENDED_WRITE)
2053 return channel_handle_efd_write(ssh, c, readset, writeset);
2054 else if (c->extended_usage == CHAN_EXTENDED_READ ||
2055 c->extended_usage == CHAN_EXTENDED_IGNORE)
2056 return channel_handle_efd_read(ssh, c, readset, writeset);
2057
2058 return 1;
2059}
2060
2061static int
2062channel_check_window(struct ssh *ssh, Channel *c)
2063{
2064 int r;
2065
Ben Lindstromb3921512001-04-11 15:57:50 +00002066 if (c->type == SSH_CHANNEL_OPEN &&
2067 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10002068 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10002069 c->local_maxpacket*3) ||
2070 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10002071 c->local_consumed > 0) {
djm@openbsd.org9f532292017-09-12 06:35:31 +00002072 if (!c->have_remote_id)
2073 fatal(":%s: channel %d: no remote id",
2074 __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002075 if ((r = sshpkt_start(ssh,
2076 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
2077 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2078 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2079 (r = sshpkt_send(ssh)) != 0) {
2080 fatal("%s: channel %i: %s", __func__,
2081 c->self, ssh_err(r));
2082 }
Damien Millerd3444942000-09-30 14:20:03 +11002083 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10002084 c->self, c->local_window,
2085 c->local_consumed);
2086 c->local_window += c->local_consumed;
2087 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002088 }
2089 return 1;
2090}
2091
Ben Lindstrombba81212001-06-25 05:01:22 +00002092static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002093channel_post_open(struct ssh *ssh, Channel *c,
2094 fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002095{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002096 channel_handle_rfd(ssh, c, readset, writeset);
2097 channel_handle_wfd(ssh, c, readset, writeset);
2098 channel_handle_efd(ssh, c, readset, writeset);
2099 channel_check_window(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002100}
2101
Damien Millere1537f92010-01-26 13:26:22 +11002102static u_int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002103read_mux(struct ssh *ssh, Channel *c, u_int need)
Damien Millere1537f92010-01-26 13:26:22 +11002104{
2105 char buf[CHAN_RBUF];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002106 ssize_t len;
Damien Millere1537f92010-01-26 13:26:22 +11002107 u_int rlen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002108 int r;
Damien Millere1537f92010-01-26 13:26:22 +11002109
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002110 if (sshbuf_len(c->input) < need) {
2111 rlen = need - sshbuf_len(c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002112 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002113 if (len < 0 && (errno == EINTR || errno == EAGAIN))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002114 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002115 if (len <= 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002116 debug2("channel %d: ctl read<=0 rfd %d len %zd",
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002117 c->self, c->rfd, len);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002118 chan_read_failed(ssh, c);
naddy@openbsd.org603ba412016-02-05 13:28:19 +00002119 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002120 } else if ((r = sshbuf_put(c->input, buf, len)) != 0) {
2121 fatal("%s: channel %d: append: %s",
2122 __func__, c->self, ssh_err(r));
2123 }
Damien Millere1537f92010-01-26 13:26:22 +11002124 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002125 return sshbuf_len(c->input);
Damien Millere1537f92010-01-26 13:26:22 +11002126}
2127
2128static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002129channel_post_mux_client_read(struct ssh *ssh, Channel *c,
2130 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002131{
2132 u_int need;
Damien Millere1537f92010-01-26 13:26:22 +11002133
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002134 if (c->rfd == -1 || !FD_ISSET(c->rfd, readset))
2135 return;
2136 if (c->istate != CHAN_INPUT_OPEN && c->istate != CHAN_INPUT_WAIT_DRAIN)
2137 return;
2138 if (c->mux_pause)
2139 return;
2140
2141 /*
2142 * Don't not read past the precise end of packets to
2143 * avoid disrupting fd passing.
2144 */
2145 if (read_mux(ssh, c, 4) < 4) /* read header */
2146 return;
2147 /* XXX sshbuf_peek_u32 */
2148 need = PEEK_U32(sshbuf_ptr(c->input));
Damien Millere1537f92010-01-26 13:26:22 +11002149#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002150 if (need > CHANNEL_MUX_MAX_PACKET) {
2151 debug2("channel %d: packet too big %u > %u",
2152 c->self, CHANNEL_MUX_MAX_PACKET, need);
2153 chan_rcvd_oclose(ssh, c);
2154 return;
Damien Millere1537f92010-01-26 13:26:22 +11002155 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002156 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2157 return;
2158 if (c->mux_rcb(ssh, c) != 0) {
2159 debug("channel %d: mux_rcb failed", c->self);
2160 chan_mark_dead(ssh, c);
2161 return;
Damien Millere1537f92010-01-26 13:26:22 +11002162 }
2163}
2164
2165static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002166channel_post_mux_client_write(struct ssh *ssh, Channel *c,
2167 fd_set *readset, fd_set *writeset)
2168{
2169 ssize_t len;
2170 int r;
2171
2172 if (c->wfd == -1 || !FD_ISSET(c->wfd, writeset) ||
2173 sshbuf_len(c->output) == 0)
2174 return;
2175
2176 len = write(c->wfd, sshbuf_ptr(c->output), sshbuf_len(c->output));
2177 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2178 return;
2179 if (len <= 0) {
2180 chan_mark_dead(ssh, c);
2181 return;
2182 }
2183 if ((r = sshbuf_consume(c->output, len)) != 0)
2184 fatal("%s: channel %d: consume: %s", __func__,
2185 c->self, ssh_err(r));
2186}
2187
2188static void
2189channel_post_mux_client(struct ssh *ssh, Channel *c,
2190 fd_set *readset, fd_set *writeset)
2191{
2192 channel_post_mux_client_read(ssh, c, readset, writeset);
2193 channel_post_mux_client_write(ssh, c, readset, writeset);
2194}
2195
2196static void
2197channel_post_mux_listener(struct ssh *ssh, Channel *c,
2198 fd_set *readset, fd_set *writeset)
Damien Millere1537f92010-01-26 13:26:22 +11002199{
2200 Channel *nc;
2201 struct sockaddr_storage addr;
2202 socklen_t addrlen;
2203 int newsock;
2204 uid_t euid;
2205 gid_t egid;
2206
2207 if (!FD_ISSET(c->sock, readset))
2208 return;
2209
2210 debug("multiplexing control connection");
2211
2212 /*
2213 * Accept connection on control socket
2214 */
2215 memset(&addr, 0, sizeof(addr));
2216 addrlen = sizeof(addr);
2217 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2218 &addrlen)) == -1) {
2219 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002220 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002221 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002222 return;
2223 }
2224
2225 if (getpeereid(newsock, &euid, &egid) < 0) {
2226 error("%s getpeereid failed: %s", __func__,
2227 strerror(errno));
2228 close(newsock);
2229 return;
2230 }
2231 if ((euid != 0) && (getuid() != euid)) {
2232 error("multiplex uid mismatch: peer euid %u != uid %u",
2233 (u_int)euid, (u_int)getuid());
2234 close(newsock);
2235 return;
2236 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002237 nc = channel_new(ssh, "multiplex client", SSH_CHANNEL_MUX_CLIENT,
Damien Millere1537f92010-01-26 13:26:22 +11002238 newsock, newsock, -1, c->local_window_max,
2239 c->local_maxpacket, 0, "mux-control", 1);
2240 nc->mux_rcb = c->mux_rcb;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002241 debug3("%s: new mux channel %d fd %d", __func__, nc->self, nc->sock);
Damien Millere1537f92010-01-26 13:26:22 +11002242 /* establish state */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002243 nc->mux_rcb(ssh, nc);
Damien Millere1537f92010-01-26 13:26:22 +11002244 /* mux state transitions must not elicit protocol messages */
2245 nc->flags |= CHAN_LOCAL;
2246}
2247
Ben Lindstrombba81212001-06-25 05:01:22 +00002248static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002249channel_handler_init(struct ssh_channels *sc)
Damien Millerb38eff82000-04-01 11:09:21 +10002250{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002251 chan_fn **pre, **post;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002252
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002253 if ((pre = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*pre))) == NULL ||
2254 (post = calloc(SSH_CHANNEL_MAX_TYPE, sizeof(*post))) == NULL)
2255 fatal("%s: allocation failed", __func__);
Damien Miller33b13562000-04-04 14:38:59 +10002256
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002257 pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
2258 pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
2259 pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2260 pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
2261 pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2262 pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
2263 pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2264 pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2265 pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
2266 pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002267 pre[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_pre_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002268 pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2269 pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
2270
2271 post[SSH_CHANNEL_OPEN] = &channel_post_open;
2272 post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2273 post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
2274 post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2275 post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
2276 post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2277 post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2278 post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
2279 post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002280 post[SSH_CHANNEL_RDYNAMIC_FINISH] = &channel_post_connecting;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002281 post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2282 post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
2283
2284 sc->channel_pre = pre;
2285 sc->channel_post = post;
Damien Miller33b13562000-04-04 14:38:59 +10002286}
2287
Damien Miller3ec27592001-10-12 11:35:04 +10002288/* gc dead channels */
2289static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002290channel_garbage_collect(struct ssh *ssh, Channel *c)
Damien Miller3ec27592001-10-12 11:35:04 +10002291{
2292 if (c == NULL)
2293 return;
2294 if (c->detach_user != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002295 if (!chan_is_dead(ssh, c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002296 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002297 debug2("channel %d: gc: notify user", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002298 c->detach_user(ssh, c->self, NULL);
Damien Miller3ec27592001-10-12 11:35:04 +10002299 /* if we still have a callback */
2300 if (c->detach_user != NULL)
2301 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002302 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002303 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002304 if (!chan_is_dead(ssh, c, 1))
Damien Miller3ec27592001-10-12 11:35:04 +10002305 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002306 debug2("channel %d: garbage collecting", c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002307 channel_free(ssh, c);
Damien Miller3ec27592001-10-12 11:35:04 +10002308}
2309
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002310enum channel_table { CHAN_PRE, CHAN_POST };
2311
Ben Lindstrombba81212001-06-25 05:01:22 +00002312static void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002313channel_handler(struct ssh *ssh, int table,
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002314 fd_set *readset, fd_set *writeset, time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002315{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002316 struct ssh_channels *sc = ssh->chanctxt;
2317 chan_fn **ftab = table == CHAN_PRE ? sc->channel_pre : sc->channel_post;
Darren Tucker876045b2010-01-08 17:08:00 +11002318 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002319 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002320 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002321
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002322 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002323 if (unpause_secs != NULL)
2324 *unpause_secs = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002325 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2326 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002327 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002328 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002329 if (c->delayed) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002330 if (table == CHAN_PRE)
Darren Tucker876045b2010-01-08 17:08:00 +11002331 c->delayed = 0;
2332 else
2333 continue;
2334 }
Damien Millera6508752012-04-22 11:21:10 +10002335 if (ftab[c->type] != NULL) {
2336 /*
2337 * Run handlers that are not paused.
2338 */
2339 if (c->notbefore <= now)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002340 (*ftab[c->type])(ssh, c, readset, writeset);
Damien Millera6508752012-04-22 11:21:10 +10002341 else if (unpause_secs != NULL) {
2342 /*
2343 * Collect the time that the earliest
2344 * channel comes off pause.
2345 */
2346 debug3("%s: chan %d: skip for %d more seconds",
2347 __func__, c->self,
2348 (int)(c->notbefore - now));
2349 if (*unpause_secs == 0 ||
2350 (c->notbefore - now) < *unpause_secs)
2351 *unpause_secs = c->notbefore - now;
2352 }
2353 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002354 channel_garbage_collect(ssh, c);
Damien Millerb38eff82000-04-01 11:09:21 +10002355 }
Damien Millera6508752012-04-22 11:21:10 +10002356 if (unpause_secs != NULL && *unpause_secs != 0)
2357 debug3("%s: first channel unpauses in %d seconds",
2358 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002359}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002360
Ben Lindstrome9c99912001-06-09 00:41:05 +00002361/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002362 * Create sockets before allocating the select bitmasks.
2363 * This is necessary for things that need to happen after reading
2364 * the network-input but before channel_prepare_select().
2365 */
2366static void
2367channel_before_prepare_select(struct ssh *ssh)
2368{
2369 struct ssh_channels *sc = ssh->chanctxt;
2370 Channel *c;
2371 u_int i, oalloc;
2372
2373 for (i = 0, oalloc = sc->channels_alloc; i < oalloc; i++) {
2374 c = sc->channels[i];
2375 if (c == NULL)
2376 continue;
2377 if (c->type == SSH_CHANNEL_RDYNAMIC_OPEN)
2378 channel_before_prepare_select_rdynamic(ssh, c);
2379 }
2380}
2381
2382/*
Ben Lindstrome9c99912001-06-09 00:41:05 +00002383 * Allocate/update select bitmasks and add any bits relevant to channels in
2384 * select bitmasks.
2385 */
Damien Miller4af51302000-04-16 11:18:38 +10002386void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002387channel_prepare_select(struct ssh *ssh, fd_set **readsetp, fd_set **writesetp,
2388 int *maxfdp, u_int *nallocp, time_t *minwait_secs)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002389{
Damien Miller36812092006-03-26 14:22:47 +11002390 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002391
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002392 channel_before_prepare_select(ssh); /* might update channel_max_fd */
2393
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002394 n = MAXIMUM(*maxfdp, ssh->chanctxt->channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002395
Damien Miller36812092006-03-26 14:22:47 +11002396 nfdset = howmany(n+1, NFDBITS);
2397 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002398 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002399 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2400 sz = nfdset * sizeof(fd_mask);
2401
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002402 /* perhaps check sz < nalloc/2 and shrink? */
2403 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002404 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2405 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002406 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002407 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002408 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002409 memset(*readsetp, 0, sz);
2410 memset(*writesetp, 0, sz);
2411
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002412 if (!ssh_packet_is_rekeying(ssh))
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002413 channel_handler(ssh, CHAN_PRE, *readsetp, *writesetp,
Damien Millera6508752012-04-22 11:21:10 +10002414 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002415}
2416
Ben Lindstrome9c99912001-06-09 00:41:05 +00002417/*
2418 * After select, perform any appropriate operations for channels which have
2419 * events pending.
2420 */
Damien Miller4af51302000-04-16 11:18:38 +10002421void
djm@openbsd.org71e5a532017-08-30 03:59:08 +00002422channel_after_select(struct ssh *ssh, fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002423{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002424 channel_handler(ssh, CHAN_POST, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002425}
2426
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002427/*
2428 * Enqueue data for channels with open or draining c->input.
2429 */
2430static void
2431channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2432{
djm@openbsd.org55486f52017-09-23 22:04:07 +00002433 size_t len, plen;
2434 const u_char *pkt;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002435 int r;
2436
2437 if ((len = sshbuf_len(c->input)) == 0) {
2438 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2439 /*
2440 * input-buffer is empty and read-socket shutdown:
2441 * tell peer, that we will not send more data:
2442 * send IEOF.
2443 * hack for extended data: delay EOF if EFD still
2444 * in use.
2445 */
2446 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2447 debug2("channel %d: "
2448 "ibuf_empty delayed efd %d/(%zu)",
2449 c->self, c->efd, sshbuf_len(c->extended));
2450 else
2451 chan_ibuf_empty(ssh, c);
2452 }
2453 return;
2454 }
2455
djm@openbsd.org9f532292017-09-12 06:35:31 +00002456 if (!c->have_remote_id)
2457 fatal(":%s: channel %d: no remote id", __func__, c->self);
2458
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002459 if (c->datagram) {
2460 /* Check datagram will fit; drop if not */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002461 if ((r = sshbuf_get_string_direct(c->input, &pkt, &plen)) != 0)
2462 fatal("%s: channel %d: get datagram: %s", __func__,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002463 c->self, ssh_err(r));
2464 /*
2465 * XXX this does tail-drop on the datagram queue which is
2466 * usually suboptimal compared to head-drop. Better to have
2467 * backpressure at read time? (i.e. read + discard)
2468 */
djm@openbsd.org55486f52017-09-23 22:04:07 +00002469 if (plen > c->remote_window || plen > c->remote_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002470 debug("channel %d: datagram too big", c->self);
2471 return;
2472 }
2473 /* Enqueue it */
2474 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2475 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
djm@openbsd.org55486f52017-09-23 22:04:07 +00002476 (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002477 (r = sshpkt_send(ssh)) != 0) {
2478 fatal("%s: channel %i: datagram: %s", __func__,
2479 c->self, ssh_err(r));
2480 }
djm@openbsd.org55486f52017-09-23 22:04:07 +00002481 c->remote_window -= plen;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002482 return;
2483 }
2484
2485 /* Enqueue packet for buffered data. */
2486 if (len > c->remote_window)
2487 len = c->remote_window;
2488 if (len > c->remote_maxpacket)
2489 len = c->remote_maxpacket;
2490 if (len == 0)
2491 return;
2492 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2493 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2494 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2495 (r = sshpkt_send(ssh)) != 0) {
2496 fatal("%s: channel %i: data: %s", __func__,
2497 c->self, ssh_err(r));
2498 }
2499 if ((r = sshbuf_consume(c->input, len)) != 0)
2500 fatal("%s: channel %i: consume: %s", __func__,
2501 c->self, ssh_err(r));
2502 c->remote_window -= len;
2503}
2504
2505/*
2506 * Enqueue data for channels with open c->extended in read mode.
2507 */
2508static void
2509channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2510{
2511 size_t len;
2512 int r;
2513
2514 if ((len = sshbuf_len(c->extended)) == 0)
2515 return;
2516
2517 debug2("channel %d: rwin %u elen %zu euse %d", c->self,
2518 c->remote_window, sshbuf_len(c->extended), c->extended_usage);
2519 if (len > c->remote_window)
2520 len = c->remote_window;
2521 if (len > c->remote_maxpacket)
2522 len = c->remote_maxpacket;
2523 if (len == 0)
2524 return;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002525 if (!c->have_remote_id)
2526 fatal(":%s: channel %d: no remote id", __func__, c->self);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002527 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2528 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2529 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
2530 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
2531 (r = sshpkt_send(ssh)) != 0) {
2532 fatal("%s: channel %i: data: %s", __func__,
2533 c->self, ssh_err(r));
2534 }
2535 if ((r = sshbuf_consume(c->extended, len)) != 0)
2536 fatal("%s: channel %i: consume: %s", __func__,
2537 c->self, ssh_err(r));
2538 c->remote_window -= len;
2539 debug2("channel %d: sent ext data %zu", c->self, len);
2540}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002541
Damien Miller5e953212001-01-30 09:14:00 +11002542/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002543void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002544channel_output_poll(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002545{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002546 struct ssh_channels *sc = ssh->chanctxt;
Damien Millerb38eff82000-04-01 11:09:21 +10002547 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002548 u_int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002549
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002550 for (i = 0; i < sc->channels_alloc; i++) {
2551 c = sc->channels[i];
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002552 if (c == NULL)
2553 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002554
Ben Lindstrome9c99912001-06-09 00:41:05 +00002555 /*
2556 * We are only interested in channels that can have buffered
2557 * incoming data.
2558 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002559 if (c->type != SSH_CHANNEL_OPEN)
2560 continue;
2561 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002562 /* XXX is this true? */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002563 debug3("channel %d: will not send data after close",
2564 c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002565 continue;
2566 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002567
Damien Miller95def091999-11-25 00:26:21 +11002568 /* Get the amount of buffered data for this channel. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002569 if (c->istate == CHAN_INPUT_OPEN ||
2570 c->istate == CHAN_INPUT_WAIT_DRAIN)
2571 channel_output_poll_input_open(ssh, c);
Damien Miller33b13562000-04-04 14:38:59 +10002572 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002573 if (!(c->flags & CHAN_EOF_SENT) &&
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002574 c->extended_usage == CHAN_EXTENDED_READ)
2575 channel_output_poll_extended_read(ssh, c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002576 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002577}
2578
markus@openbsd.org8d057842016-09-30 09:19:13 +00002579/* -- mux proxy support */
2580
2581/*
2582 * When multiplexing channel messages for mux clients we have to deal
2583 * with downstream messages from the mux client and upstream messages
2584 * from the ssh server:
2585 * 1) Handling downstream messages is straightforward and happens
2586 * in channel_proxy_downstream():
2587 * - We forward all messages (mostly) unmodified to the server.
2588 * - However, in order to route messages from upstream to the correct
2589 * downstream client, we have to replace the channel IDs used by the
2590 * mux clients with a unique channel ID because the mux clients might
2591 * use conflicting channel IDs.
2592 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2593 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2594 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2595 * with the newly allocated channel ID.
2596 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2597 * channels and procesed by channel_proxy_upstream(). The local channel ID
2598 * is then translated back to the original mux client ID.
2599 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2600 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2601 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2602 * downstream mux client are removed.
2603 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2604 * requires more work, because they are not addressed to a specific
2605 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2606 * out whether the request is addressed to the local client or a
2607 * specific downstream client based on the listen-address/port.
2608 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2609 * not supported as the matching session/channel cannot be identified
2610 * easily.
2611 */
2612
2613/*
2614 * receive packets from downstream mux clients:
2615 * channel callback fired on read from mux client, creates
2616 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2617 * on channel creation.
2618 */
2619int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002620channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002621{
2622 Channel *c = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002623 struct sshbuf *original = NULL, *modified = NULL;
2624 const u_char *cp;
2625 char *ctype = NULL, *listen_host = NULL;
2626 u_char type;
2627 size_t have;
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002628 int ret = -1, r;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002629 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002630
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002631 /* sshbuf_dump(downstream->input, stderr); */
2632 if ((r = sshbuf_get_string_direct(downstream->input, &cp, &have))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002633 != 0) {
2634 error("%s: malformed message: %s", __func__, ssh_err(r));
2635 return -1;
2636 }
2637 if (have < 2) {
2638 error("%s: short message", __func__);
2639 return -1;
2640 }
2641 type = cp[1];
2642 /* skip padlen + type */
2643 cp += 2;
2644 have -= 2;
2645 if (ssh_packet_log_type(type))
2646 debug3("%s: channel %u: down->up: type %u", __func__,
2647 downstream->self, type);
2648
2649 switch (type) {
2650 case SSH2_MSG_CHANNEL_OPEN:
2651 if ((original = sshbuf_from(cp, have)) == NULL ||
2652 (modified = sshbuf_new()) == NULL) {
2653 error("%s: alloc", __func__);
2654 goto out;
2655 }
2656 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2657 (r = sshbuf_get_u32(original, &id)) != 0) {
2658 error("%s: parse error %s", __func__, ssh_err(r));
2659 goto out;
2660 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002661 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002662 -1, -1, -1, 0, 0, 0, ctype, 1);
2663 c->mux_ctx = downstream; /* point to mux client */
2664 c->mux_downstream_id = id; /* original downstream id */
2665 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2666 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2667 (r = sshbuf_putb(modified, original)) != 0) {
2668 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002669 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002670 goto out;
2671 }
2672 break;
2673 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2674 /*
2675 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2676 * need to parse 'remote_id' instead of 'ctype'.
2677 */
2678 if ((original = sshbuf_from(cp, have)) == NULL ||
2679 (modified = sshbuf_new()) == NULL) {
2680 error("%s: alloc", __func__);
2681 goto out;
2682 }
2683 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2684 (r = sshbuf_get_u32(original, &id)) != 0) {
2685 error("%s: parse error %s", __func__, ssh_err(r));
2686 goto out;
2687 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002688 c = channel_new(ssh, "mux proxy", SSH_CHANNEL_MUX_PROXY,
markus@openbsd.org8d057842016-09-30 09:19:13 +00002689 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2690 c->mux_ctx = downstream; /* point to mux client */
2691 c->mux_downstream_id = id;
2692 c->remote_id = remote_id;
djm@openbsd.org9f532292017-09-12 06:35:31 +00002693 c->have_remote_id = 1;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002694 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2695 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2696 (r = sshbuf_putb(modified, original)) != 0) {
2697 error("%s: compose error %s", __func__, ssh_err(r));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002698 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002699 goto out;
2700 }
2701 break;
2702 case SSH2_MSG_GLOBAL_REQUEST:
2703 if ((original = sshbuf_from(cp, have)) == NULL) {
2704 error("%s: alloc", __func__);
2705 goto out;
2706 }
2707 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2708 error("%s: parse error %s", __func__, ssh_err(r));
2709 goto out;
2710 }
2711 if (strcmp(ctype, "tcpip-forward") != 0) {
2712 error("%s: unsupported request %s", __func__, ctype);
2713 goto out;
2714 }
2715 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2716 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2717 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2718 error("%s: parse error %s", __func__, ssh_err(r));
2719 goto out;
2720 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002721 if (listen_port > 65535) {
2722 error("%s: tcpip-forward for %s: bad port %u",
2723 __func__, listen_host, listen_port);
2724 goto out;
2725 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002726 /* Record that connection to this host/port is permitted. */
djm@openbsd.org4ec0bb92017-09-12 07:55:48 +00002727 fwd_perm_list_add(ssh, FWDPERM_USER, "<mux>", -1,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002728 listen_host, NULL, (int)listen_port, downstream);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002729 listen_host = NULL;
2730 break;
2731 case SSH2_MSG_CHANNEL_CLOSE:
2732 if (have < 4)
2733 break;
2734 remote_id = PEEK_U32(cp);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002735 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002736 if (c->flags & CHAN_CLOSE_RCVD)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002737 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002738 else
2739 c->flags |= CHAN_CLOSE_SENT;
2740 }
2741 break;
2742 }
2743 if (modified) {
2744 if ((r = sshpkt_start(ssh, type)) != 0 ||
2745 (r = sshpkt_putb(ssh, modified)) != 0 ||
2746 (r = sshpkt_send(ssh)) != 0) {
2747 error("%s: send %s", __func__, ssh_err(r));
2748 goto out;
2749 }
2750 } else {
2751 if ((r = sshpkt_start(ssh, type)) != 0 ||
2752 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2753 (r = sshpkt_send(ssh)) != 0) {
2754 error("%s: send %s", __func__, ssh_err(r));
2755 goto out;
2756 }
2757 }
2758 ret = 0;
2759 out:
2760 free(ctype);
2761 free(listen_host);
2762 sshbuf_free(original);
2763 sshbuf_free(modified);
2764 return ret;
2765}
2766
2767/*
2768 * receive packets from upstream server and de-multiplex packets
2769 * to correct downstream:
2770 * implemented as a helper for channel input handlers,
2771 * replaces local (proxy) channel ID with downstream channel ID.
2772 */
2773int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002774channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
markus@openbsd.org8d057842016-09-30 09:19:13 +00002775{
markus@openbsd.org8d057842016-09-30 09:19:13 +00002776 struct sshbuf *b = NULL;
2777 Channel *downstream;
2778 const u_char *cp = NULL;
2779 size_t len;
2780 int r;
2781
2782 /*
2783 * When receiving packets from the peer we need to check whether we
2784 * need to forward the packets to the mux client. In this case we
2785 * restore the orignal channel id and keep track of CLOSE messages,
2786 * so we can cleanup the channel.
2787 */
2788 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2789 return 0;
2790 if ((downstream = c->mux_ctx) == NULL)
2791 return 0;
2792 switch (type) {
2793 case SSH2_MSG_CHANNEL_CLOSE:
2794 case SSH2_MSG_CHANNEL_DATA:
2795 case SSH2_MSG_CHANNEL_EOF:
2796 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2797 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2798 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2799 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2800 case SSH2_MSG_CHANNEL_SUCCESS:
2801 case SSH2_MSG_CHANNEL_FAILURE:
2802 case SSH2_MSG_CHANNEL_REQUEST:
2803 break;
2804 default:
2805 debug2("%s: channel %u: unsupported type %u", __func__,
2806 c->self, type);
2807 return 0;
2808 }
2809 if ((b = sshbuf_new()) == NULL) {
2810 error("%s: alloc reply", __func__);
2811 goto out;
2812 }
2813 /* get remaining payload (after id) */
2814 cp = sshpkt_ptr(ssh, &len);
2815 if (cp == NULL) {
2816 error("%s: no packet", __func__);
2817 goto out;
2818 }
2819 /* translate id and send to muxclient */
2820 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2821 (r = sshbuf_put_u8(b, type)) != 0 ||
2822 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2823 (r = sshbuf_put(b, cp, len)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002824 (r = sshbuf_put_stringb(downstream->output, b)) != 0) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002825 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2826 goto out;
2827 }
2828 /* sshbuf_dump(b, stderr); */
2829 if (ssh_packet_log_type(type))
2830 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2831 type);
2832 out:
2833 /* update state */
2834 switch (type) {
2835 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2836 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
djm@openbsd.org9f532292017-09-12 06:35:31 +00002837 if (cp && len > 4) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00002838 c->remote_id = PEEK_U32(cp);
djm@openbsd.org9f532292017-09-12 06:35:31 +00002839 c->have_remote_id = 1;
2840 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002841 break;
2842 case SSH2_MSG_CHANNEL_CLOSE:
2843 if (c->flags & CHAN_CLOSE_SENT)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002844 channel_free(ssh, c);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002845 else
2846 c->flags |= CHAN_CLOSE_RCVD;
2847 break;
2848 }
2849 sshbuf_free(b);
2850 return 1;
2851}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002852
2853/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002854
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002855/* Parse a channel ID from the current packet */
2856static int
2857channel_parse_id(struct ssh *ssh, const char *where, const char *what)
2858{
2859 u_int32_t id;
2860 int r;
2861
2862 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
2863 error("%s: parse id: %s", where, ssh_err(r));
2864 ssh_packet_disconnect(ssh, "Invalid %s message", what);
2865 }
2866 if (id > INT_MAX) {
2867 error("%s: bad channel id %u: %s", where, id, ssh_err(r));
2868 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
2869 }
2870 return (int)id;
2871}
2872
2873/* Lookup a channel from an ID in the current packet */
2874static Channel *
2875channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
2876{
2877 int id = channel_parse_id(ssh, where, what);
2878 Channel *c;
2879
2880 if ((c = channel_lookup(ssh, id)) == NULL) {
2881 ssh_packet_disconnect(ssh,
2882 "%s packet referred to nonexistent channel %d", what, id);
2883 }
2884 return c;
2885}
2886
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002887int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002888channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002889{
Damien Miller633de332014-05-15 13:48:26 +10002890 const u_char *data;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002891 size_t data_len, win_len;
2892 Channel *c = channel_from_packet_id(ssh, __func__, "data");
2893 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002894
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002895 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002896 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002897
Damien Miller95def091999-11-25 00:26:21 +11002898 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002899 if (c->type != SSH_CHANNEL_OPEN &&
markus@openbsd.org609d7a62017-09-21 19:16:53 +00002900 c->type != SSH_CHANNEL_RDYNAMIC_OPEN &&
2901 c->type != SSH_CHANNEL_RDYNAMIC_FINISH &&
Damien Millerb38eff82000-04-01 11:09:21 +10002902 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002903 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002904
Damien Miller95def091999-11-25 00:26:21 +11002905 /* Get the data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002906 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0)
2907 fatal("%s: channel %d: get data: %s", __func__,
2908 c->self, ssh_err(r));
2909 ssh_packet_check_eom(ssh);
2910
Damien Miller7d457182010-08-05 23:09:48 +10002911 win_len = data_len;
2912 if (c->datagram)
2913 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002914
Damien Millera04ad492004-01-21 11:02:09 +11002915 /*
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002916 * The sending side reduces its window as it sends data, so we
2917 * must 'fake' consumption of the data in order to ensure that window
2918 * updates are sent back. Otherwise the connection might deadlock.
Damien Millera04ad492004-01-21 11:02:09 +11002919 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002920 if (c->ostate != CHAN_OUTPUT_OPEN) {
2921 c->local_window -= win_len;
2922 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002923 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002924 }
2925
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002926 if (win_len > c->local_maxpacket) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002927 logit("channel %d: rcvd big packet %zu, maxpack %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002928 c->self, win_len, c->local_maxpacket);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002929 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002930 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002931 if (win_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002932 logit("channel %d: rcvd too much data %zu, win %u",
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002933 c->self, win_len, c->local_window);
2934 return 0;
2935 }
2936 c->local_window -= win_len;
2937
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002938 if (c->datagram) {
2939 if ((r = sshbuf_put_string(c->output, data, data_len)) != 0)
2940 fatal("%s: channel %d: append datagram: %s",
2941 __func__, c->self, ssh_err(r));
2942 } else if ((r = sshbuf_put(c->output, data, data_len)) != 0)
2943 fatal("%s: channel %d: append data: %s",
2944 __func__, c->self, ssh_err(r));
2945
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002946 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002947}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002948
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002949int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002950channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10002951{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002952 const u_char *data;
2953 size_t data_len;
2954 u_int32_t tcode;
2955 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
2956 int r;
Damien Miller33b13562000-04-04 14:38:59 +10002957
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00002958 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00002959 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002960 if (c->type != SSH_CHANNEL_OPEN) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002961 logit("channel %d: ext data for non open", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002962 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002963 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002964 if (c->flags & CHAN_EOF_RCVD) {
2965 if (datafellows & SSH_BUG_EXTEOF)
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002966 debug("channel %d: accepting ext data after eof",
2967 c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002968 else
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002969 ssh_packet_disconnect(ssh, "Received extended_data "
2970 "after EOF on channel %d.", c->self);
Ben Lindstromcf159442002-03-26 03:26:24 +00002971 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002972
2973 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
2974 error("%s: parse tcode: %s", __func__, ssh_err(r));
2975 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2976 }
Damien Miller33b13562000-04-04 14:38:59 +10002977 if (c->efd == -1 ||
2978 c->extended_usage != CHAN_EXTENDED_WRITE ||
2979 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002980 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002981 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002982 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002983 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0) {
2984 error("%s: parse data: %s", __func__, ssh_err(r));
2985 ssh_packet_disconnect(ssh, "Invalid extended_data message");
2986 }
2987 ssh_packet_check_eom(ssh);
2988
Damien Miller33b13562000-04-04 14:38:59 +10002989 if (data_len > c->local_window) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002990 logit("channel %d: rcvd too much extended_data %zu, win %u",
Damien Miller33b13562000-04-04 14:38:59 +10002991 c->self, data_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002992 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002993 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00002994 debug2("channel %d: rcvd ext data %zu", c->self, data_len);
2995 /* XXX sshpkt_getb? */
2996 if ((r = sshbuf_put(c->extended, data, data_len)) != 0)
2997 error("%s: append: %s", __func__, ssh_err(r));
Damien Miller33b13562000-04-04 14:38:59 +10002998 c->local_window -= data_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002999 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003000}
3001
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003002int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003003channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb38eff82000-04-01 11:09:21 +10003004{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003005 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
Damien Millerb38eff82000-04-01 11:09:21 +10003006
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003007 ssh_packet_check_eom(ssh);
3008
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003009 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003010 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003011 chan_rcvd_ieof(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003012
3013 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11003014 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003015 debug("channel %d: FORCE input drain", c->self);
3016 c->istate = CHAN_INPUT_WAIT_DRAIN;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003017 if (sshbuf_len(c->input) == 0)
3018 chan_ibuf_empty(ssh, c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00003019 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003020 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10003021}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003022
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003023int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003024channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003025{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003026 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
Damien Miller66823cd2002-01-22 23:11:38 +11003027
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003028 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003029 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003030 ssh_packet_check_eom(ssh);
3031 chan_rcvd_oclose(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003032 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003033}
3034
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003035int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003036channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003037{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003038 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3039 u_int32_t remote_window, remote_maxpacket;
3040 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003041
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003042 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003043 return 0;
3044 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10003045 packet_disconnect("Received open confirmation for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003046 "non-opening channel %d.", c->self);
3047 /*
3048 * Record the remote channel number and mark that the channel
3049 * is now open.
3050 */
djm@openbsd.org9f532292017-09-12 06:35:31 +00003051 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3052 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003053 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) {
3054 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
3055 packet_disconnect("Invalid open confirmation message");
3056 }
3057 ssh_packet_check_eom(ssh);
Damien Miller33b13562000-04-04 14:38:59 +10003058
djm@openbsd.org9f532292017-09-12 06:35:31 +00003059 c->have_remote_id = 1;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003060 c->remote_window = remote_window;
3061 c->remote_maxpacket = remote_maxpacket;
3062 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003063 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003064 debug2("%s: channel %d: callback start", __func__, c->self);
3065 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
3066 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003067 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003068 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
3069 c->remote_window, c->remote_maxpacket);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003070 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003071}
3072
Ben Lindstrombba81212001-06-25 05:01:22 +00003073static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00003074reason2txt(int reason)
3075{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003076 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00003077 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
3078 return "administratively prohibited";
3079 case SSH2_OPEN_CONNECT_FAILED:
3080 return "connect failed";
3081 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
3082 return "unknown channel type";
3083 case SSH2_OPEN_RESOURCE_SHORTAGE:
3084 return "resource shortage";
3085 }
Ben Lindstrome2595442001-06-05 20:01:39 +00003086 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00003087}
3088
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003089int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003090channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003091{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003092 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3093 u_int32_t reason;
3094 char *msg = NULL;
3095 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003096
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003097 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003098 return 0;
3099 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10003100 packet_disconnect("Received open failure for "
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003101 "non-opening channel %d.", c->self);
3102 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
3103 error("%s: reason: %s", __func__, ssh_err(r));
3104 packet_disconnect("Invalid open failure message");
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003105 }
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003106 /* skip language */
3107 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
3108 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0) {
3109 error("%s: message/lang: %s", __func__, ssh_err(r));
3110 packet_disconnect("Invalid open failure message");
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003111 }
3112 ssh_packet_check_eom(ssh);
3113 logit("channel %d: open failed: %s%s%s", c->self,
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003114 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
3115 free(msg);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003116 if (c->open_confirm) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003117 debug2("%s: channel %d: callback start", __func__, c->self);
3118 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3119 debug2("%s: channel %d: callback done", __func__, c->self);
Damien Miller33b13562000-04-04 14:38:59 +10003120 }
Damien Miller7a606212009-01-28 16:22:34 +11003121 /* Schedule the channel for cleanup/deletion. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003122 chan_mark_dead(ssh, c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003123 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003124}
3125
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003126int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003127channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
Damien Miller33b13562000-04-04 14:38:59 +10003128{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003129 int id = channel_parse_id(ssh, __func__, "window adjust");
Damien Miller33b13562000-04-04 14:38:59 +10003130 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003131 u_int32_t adjust;
3132 u_int new_rwin;
3133 int r;
Damien Miller33b13562000-04-04 14:38:59 +10003134
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003135 if ((c = channel_lookup(ssh, id)) == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +11003136 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003137 return 0;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00003138 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003139
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003140 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003141 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003142 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0) {
3143 error("%s: adjust: %s", __func__, ssh_err(r));
3144 packet_disconnect("Invalid window adjust message");
3145 }
3146 ssh_packet_check_eom(ssh);
3147 debug2("channel %d: rcvd adjust %u", c->self, adjust);
3148 if ((new_rwin = c->remote_window + adjust) < c->remote_window) {
djm@openbsd.org629df772015-06-30 05:25:07 +00003149 fatal("channel %d: adjust %u overflows remote window %u",
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003150 c->self, adjust, c->remote_window);
3151 }
3152 c->remote_window = new_rwin;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003153 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003154}
3155
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003156int
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003157channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
Damien Millerb84886b2008-05-19 15:05:07 +10003158{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003159 int id = channel_parse_id(ssh, __func__, "status confirm");
Damien Millerb84886b2008-05-19 15:05:07 +10003160 Channel *c;
3161 struct channel_confirm *cc;
Damien Millerb84886b2008-05-19 15:05:07 +10003162
3163 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003164 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003165
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003166 debug2("%s: type %d id %d", __func__, type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003167
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003168 if ((c = channel_lookup(ssh, id)) == NULL) {
3169 logit("%s: %d: unknown", __func__, id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003170 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003171 }
markus@openbsd.org2ae666a2017-05-30 14:23:52 +00003172 if (channel_proxy_upstream(c, type, seq, ssh))
markus@openbsd.org8d057842016-09-30 09:19:13 +00003173 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003174 ssh_packet_check_eom(ssh);
Damien Millerb84886b2008-05-19 15:05:07 +10003175 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003176 return 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003177 cc->cb(ssh, type, c, cc->ctx);
Damien Millerb84886b2008-05-19 15:05:07 +10003178 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003179 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003180 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003181 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003182}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003183
Ben Lindstrome9c99912001-06-09 00:41:05 +00003184/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003185
Ben Lindstrom908afed2001-10-03 17:34:59 +00003186void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003187channel_set_af(struct ssh *ssh, int af)
Ben Lindstrom908afed2001-10-03 17:34:59 +00003188{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003189 ssh->chanctxt->IPv4or6 = af;
Ben Lindstrom908afed2001-10-03 17:34:59 +00003190}
3191
Damien Millerf6dff7c2011-09-22 21:38:52 +10003192
3193/*
3194 * Determine whether or not a port forward listens to loopback, the
3195 * specified address or wildcard. On the client, a specified bind
3196 * address will always override gateway_ports. On the server, a
3197 * gateway_ports of 1 (``yes'') will override the client's specification
3198 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3199 * will bind to whatever address the client asked for.
3200 *
3201 * Special-case listen_addrs are:
3202 *
3203 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3204 * "" (empty string), "*" -> wildcard v4/v6
3205 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003206 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003207 */
3208static const char *
3209channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003210 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003211{
3212 const char *addr = NULL;
3213 int wildcard = 0;
3214
3215 if (listen_addr == NULL) {
3216 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003217 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003218 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003219 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003220 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3221 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3222 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003223 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003224 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003225 /*
3226 * Notify client if they requested a specific listen
3227 * address and it was overridden.
3228 */
3229 if (*listen_addr != '\0' &&
3230 strcmp(listen_addr, "0.0.0.0") != 0 &&
3231 strcmp(listen_addr, "*") != 0) {
3232 packet_send_debug("Forwarding listen address "
3233 "\"%s\" overridden by server "
3234 "GatewayPorts", listen_addr);
3235 }
Damien Miller602943d2014-07-04 08:59:41 +10003236 } else if (strcmp(listen_addr, "localhost") != 0 ||
3237 strcmp(listen_addr, "127.0.0.1") == 0 ||
3238 strcmp(listen_addr, "::1") == 0) {
3239 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003240 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003241 }
3242 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3243 strcmp(listen_addr, "::1") == 0) {
3244 /*
3245 * If a specific IPv4/IPv6 localhost address has been
3246 * requested then accept it even if gateway_ports is in
3247 * effect. This allows the client to prefer IPv4 or IPv6.
3248 */
3249 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003250 }
3251 if (wildcardp != NULL)
3252 *wildcardp = wildcard;
3253 return addr;
3254}
3255
Damien Millerb16461c2002-01-22 23:29:22 +11003256static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003257channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3258 struct Forward *fwd, int *allocated_listen_port,
3259 struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003260{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003261 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003262 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003263 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003264 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003265 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003266 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003267
Damien Millerf91ee4c2005-03-01 21:24:33 +11003268 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003269
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003270 if (is_client && fwd->connect_path != NULL) {
3271 host = fwd->connect_path;
3272 } else {
3273 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3274 fwd->listen_host : fwd->connect_host;
3275 if (host == NULL) {
3276 error("No forward host name.");
3277 return 0;
3278 }
3279 if (strlen(host) >= NI_MAXHOST) {
3280 error("Forward host name too long.");
3281 return 0;
3282 }
Kevin Steves12057502001-02-05 14:54:34 +00003283 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003284
Damien Millerf6dff7c2011-09-22 21:38:52 +10003285 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003286 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3287 is_client, fwd_opts);
3288 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003289 type, wildcard, (addr == NULL) ? "NULL" : addr);
3290
3291 /*
Damien Miller34132e52000-01-14 15:45:46 +11003292 * getaddrinfo returns a loopback address if the hostname is
3293 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003294 */
Damien Miller34132e52000-01-14 15:45:46 +11003295 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003296 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003297 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003298 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003299 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003300 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3301 if (addr == NULL) {
3302 /* This really shouldn't happen */
3303 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003304 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003305 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003306 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003307 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003308 }
Damien Millera7270302005-07-06 09:36:05 +10003309 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003310 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003311 if (allocated_listen_port != NULL)
3312 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003313 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003314 switch (ai->ai_family) {
3315 case AF_INET:
3316 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3317 sin_port;
3318 break;
3319 case AF_INET6:
3320 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3321 sin6_port;
3322 break;
3323 default:
Damien Miller34132e52000-01-14 15:45:46 +11003324 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003325 }
3326 /*
3327 * If allocating a port for -R forwards, then use the
3328 * same port for all address families.
3329 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003330 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3331 fwd->listen_port == 0 && allocated_listen_port != NULL &&
3332 *allocated_listen_port > 0)
Damien Miller4bf648f2009-02-14 16:28:21 +11003333 *lport_p = htons(*allocated_listen_port);
3334
Damien Miller34132e52000-01-14 15:45:46 +11003335 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003336 strport, sizeof(strport),
3337 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003338 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003339 continue;
3340 }
3341 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003342 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003343 if (sock < 0) {
3344 /* this is no error since kernel may not support ipv6 */
djm@openbsd.org927f8512017-12-05 01:30:19 +00003345 verbose("socket [%s]:%s: %.100s", ntop, strport,
3346 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003347 continue;
3348 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003349
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00003350 set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003351 if (ai->ai_family == AF_INET6)
3352 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003353
Damien Miller4bf648f2009-02-14 16:28:21 +11003354 debug("Local forwarding listening on %s port %s.",
3355 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003356
Damien Miller34132e52000-01-14 15:45:46 +11003357 /* Bind the socket to the address. */
3358 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003359 /*
3360 * address can be in if use ipv6 address is
3361 * already bound
3362 */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003363 if (!ai->ai_next)
djm@openbsd.org927f8512017-12-05 01:30:19 +00003364 error("bind [%s]:%s: %.100s",
3365 ntop, strport, strerror(errno));
Damien Miller3c7eeb22000-03-03 22:35:33 +11003366 else
djm@openbsd.org927f8512017-12-05 01:30:19 +00003367 verbose("bind [%s]:%s: %.100s",
3368 ntop, strport, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003369
Damien Miller34132e52000-01-14 15:45:46 +11003370 close(sock);
3371 continue;
3372 }
3373 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003374 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003375 error("listen: %.100s", strerror(errno));
djm@openbsd.org927f8512017-12-05 01:30:19 +00003376 error("listen [%s]:%s: %.100s", ntop, strport,
3377 strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003378 close(sock);
3379 continue;
3380 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003381
3382 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003383 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003384 * record what we got.
3385 */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003386 if (type == SSH_CHANNEL_RPORT_LISTENER &&
3387 fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003388 allocated_listen_port != NULL &&
3389 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003390 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003391 debug("Allocated listen port %d",
3392 *allocated_listen_port);
3393 }
3394
Damien Miller34132e52000-01-14 15:45:46 +11003395 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003396 c = channel_new(ssh, "port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003397 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003398 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003399 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003400 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003401 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003402 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003403 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3404 c->listening_port = *allocated_listen_port;
3405 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003406 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003407 success = 1;
3408 }
3409 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003410 error("%s: cannot listen to port: %d", __func__,
3411 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003412 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003413 return success;
Damien Miller95def091999-11-25 00:26:21 +11003414}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003415
Damien Miller7acefbb2014-07-18 14:11:24 +10003416static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003417channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3418 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003419{
3420 struct sockaddr_un sunaddr;
3421 const char *path;
3422 Channel *c;
3423 int port, sock;
3424 mode_t omask;
3425
3426 switch (type) {
3427 case SSH_CHANNEL_UNIX_LISTENER:
3428 if (fwd->connect_path != NULL) {
3429 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3430 error("Local connecting path too long: %s",
3431 fwd->connect_path);
3432 return 0;
3433 }
3434 path = fwd->connect_path;
3435 port = PORT_STREAMLOCAL;
3436 } else {
3437 if (fwd->connect_host == NULL) {
3438 error("No forward host name.");
3439 return 0;
3440 }
3441 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3442 error("Forward host name too long.");
3443 return 0;
3444 }
3445 path = fwd->connect_host;
3446 port = fwd->connect_port;
3447 }
3448 break;
3449 case SSH_CHANNEL_RUNIX_LISTENER:
3450 path = fwd->listen_path;
3451 port = PORT_STREAMLOCAL;
3452 break;
3453 default:
3454 error("%s: unexpected channel type %d", __func__, type);
3455 return 0;
3456 }
3457
3458 if (fwd->listen_path == NULL) {
3459 error("No forward path name.");
3460 return 0;
3461 }
3462 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3463 error("Local listening path too long: %s", fwd->listen_path);
3464 return 0;
3465 }
3466
3467 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3468
3469 /* Start a Unix domain listener. */
3470 omask = umask(fwd_opts->streamlocal_bind_mask);
3471 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3472 fwd_opts->streamlocal_bind_unlink);
3473 umask(omask);
3474 if (sock < 0)
3475 return 0;
3476
3477 debug("Local forwarding listening on path %s.", fwd->listen_path);
3478
3479 /* Allocate a channel number for the socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003480 c = channel_new(ssh, "unix listener", type, sock, sock, -1,
Damien Miller7acefbb2014-07-18 14:11:24 +10003481 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3482 0, "unix listener", 1);
3483 c->path = xstrdup(path);
3484 c->host_port = port;
3485 c->listening_port = PORT_STREAMLOCAL;
3486 c->listening_addr = xstrdup(fwd->listen_path);
3487 return 1;
3488}
3489
3490static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003491channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3492 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003493{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003494 u_int i;
3495 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003496
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003497 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3498 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003499 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3500 continue;
3501 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3502 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003503 channel_free(ssh, c);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003504 found = 1;
3505 }
3506 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003507
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003508 return found;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003509}
3510
Damien Miller7acefbb2014-07-18 14:11:24 +10003511static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003512channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003513{
3514 u_int i;
3515 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003516
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003517 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3518 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003519 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3520 continue;
3521 if (c->path == NULL)
3522 continue;
3523 if (strcmp(c->path, path) == 0) {
3524 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003525 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003526 found = 1;
3527 }
3528 }
3529
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003530 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003531}
3532
3533int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003534channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003535{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003536 if (fwd->listen_path != NULL) {
3537 return channel_cancel_rport_listener_streamlocal(ssh,
3538 fwd->listen_path);
3539 } else {
3540 return channel_cancel_rport_listener_tcpip(ssh,
3541 fwd->listen_host, fwd->listen_port);
3542 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003543}
3544
3545static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003546channel_cancel_lport_listener_tcpip(struct ssh *ssh,
3547 const char *lhost, u_short lport, int cport,
3548 struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003549{
3550 u_int i;
3551 int found = 0;
3552 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003553
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003554 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3555 Channel *c = ssh->chanctxt->channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003556 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3557 continue;
Damien Millerff773642011-09-22 21:39:48 +10003558 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003559 continue;
Damien Millerff773642011-09-22 21:39:48 +10003560 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3561 /* skip dynamic forwardings */
3562 if (c->host_port == 0)
3563 continue;
3564 } else {
3565 if (c->host_port != cport)
3566 continue;
3567 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003568 if ((c->listening_addr == NULL && addr != NULL) ||
3569 (c->listening_addr != NULL && addr == NULL))
3570 continue;
3571 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003572 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003573 channel_free(ssh, c);
Darren Tuckere7066df2004-05-24 10:18:05 +10003574 found = 1;
3575 }
3576 }
3577
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003578 return found;
Darren Tuckere7066df2004-05-24 10:18:05 +10003579}
3580
Damien Miller7acefbb2014-07-18 14:11:24 +10003581static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003582channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003583{
3584 u_int i;
3585 int found = 0;
3586
3587 if (path == NULL) {
3588 error("%s: no path specified.", __func__);
3589 return 0;
3590 }
3591
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003592 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3593 Channel *c = ssh->chanctxt->channels[i];
Damien Miller7acefbb2014-07-18 14:11:24 +10003594 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3595 continue;
3596 if (c->listening_addr == NULL)
3597 continue;
3598 if (strcmp(c->listening_addr, path) == 0) {
3599 debug2("%s: close channel %d", __func__, i);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003600 channel_free(ssh, c);
Damien Miller7acefbb2014-07-18 14:11:24 +10003601 found = 1;
3602 }
3603 }
3604
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003605 return found;
Damien Miller7acefbb2014-07-18 14:11:24 +10003606}
3607
3608int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003609channel_cancel_lport_listener(struct ssh *ssh,
3610 struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
Damien Miller7acefbb2014-07-18 14:11:24 +10003611{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003612 if (fwd->listen_path != NULL) {
3613 return channel_cancel_lport_listener_streamlocal(ssh,
3614 fwd->listen_path);
3615 } else {
3616 return channel_cancel_lport_listener_tcpip(ssh,
3617 fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3618 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003619}
3620
markus@openbsd.orgc2212192017-05-30 08:49:58 +00003621/* protocol local port fwd, used by ssh */
Damien Millerb16461c2002-01-22 23:29:22 +11003622int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003623channel_setup_local_fwd_listener(struct ssh *ssh,
3624 struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003625{
Damien Miller7acefbb2014-07-18 14:11:24 +10003626 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003627 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003628 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3629 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003630 return channel_setup_fwd_listener_tcpip(ssh,
3631 SSH_CHANNEL_PORT_LISTENER, fwd, NULL, fwd_opts);
Damien Miller7acefbb2014-07-18 14:11:24 +10003632 }
Damien Millerb16461c2002-01-22 23:29:22 +11003633}
3634
3635/* protocol v2 remote port fwd, used by sshd */
3636int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003637channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
Damien Miller7acefbb2014-07-18 14:11:24 +10003638 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003639{
Damien Miller7acefbb2014-07-18 14:11:24 +10003640 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003641 return channel_setup_fwd_listener_streamlocal(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003642 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3643 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003644 return channel_setup_fwd_listener_tcpip(ssh,
Damien Miller7acefbb2014-07-18 14:11:24 +10003645 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3646 fwd_opts);
3647 }
Damien Millerb16461c2002-01-22 23:29:22 +11003648}
3649
Damien Miller5428f641999-11-25 11:54:57 +11003650/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003651 * Translate the requested rfwd listen host to something usable for
3652 * this server.
3653 */
3654static const char *
3655channel_rfwd_bind_host(const char *listen_host)
3656{
3657 if (listen_host == NULL) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003658 return "localhost";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003659 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
djm@openbsd.org14b5c632018-01-23 05:27:21 +00003660 return "";
Damien Millerf6dff7c2011-09-22 21:38:52 +10003661 } else
3662 return listen_host;
3663}
3664
3665/*
Damien Miller5428f641999-11-25 11:54:57 +11003666 * Initiate forwarding of connections to port "port" on remote host through
3667 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003668 * Returns handle (index) for updating the dynamic listen port with
3669 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003670 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003671int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003672channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003673{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003674 int r, success = 0, idx = -1;
3675 char *host_to_connect, *listen_host, *listen_path;
3676 int port_to_connect, listen_port;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003677
Damien Miller95def091999-11-25 00:26:21 +11003678 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003679 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003680 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3681 (r = sshpkt_put_cstring(ssh,
3682 "streamlocal-forward@openssh.com")) != 0 ||
3683 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3684 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
3685 (r = sshpkt_send(ssh)) != 0 ||
3686 (r = ssh_packet_write_wait(ssh)) != 0)
3687 fatal("%s: request streamlocal: %s",
3688 __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003689 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003690 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3691 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
3692 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
3693 (r = sshpkt_put_cstring(ssh,
3694 channel_rfwd_bind_host(fwd->listen_host))) != 0 ||
3695 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
3696 (r = sshpkt_send(ssh)) != 0 ||
3697 (r = ssh_packet_write_wait(ssh)) != 0)
3698 fatal("%s: request tcpip-forward: %s",
3699 __func__, ssh_err(r));
Damien Miller0bc1bd82000-11-13 22:57:25 +11003700 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003701 /* Assume that server accepts the request */
3702 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003703 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003704 /* Record that connection to this host/port is permitted. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003705 host_to_connect = listen_host = listen_path = NULL;
3706 port_to_connect = listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003707 if (fwd->connect_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003708 host_to_connect = xstrdup(fwd->connect_path);
3709 port_to_connect = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003710 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003711 host_to_connect = xstrdup(fwd->connect_host);
3712 port_to_connect = fwd->connect_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003713 }
3714 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003715 listen_path = xstrdup(fwd->listen_path);
3716 listen_port = PORT_STREAMLOCAL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003717 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003718 if (fwd->listen_host != NULL)
3719 listen_host = xstrdup(fwd->listen_host);
3720 listen_port = fwd->listen_port;
Damien Miller7acefbb2014-07-18 14:11:24 +10003721 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003722 idx = fwd_perm_list_add(ssh, FWDPERM_USER,
3723 host_to_connect, port_to_connect,
3724 listen_host, listen_path, listen_port, NULL);
Damien Miller33b13562000-04-04 14:38:59 +10003725 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003726 return idx;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003727}
3728
Damien Miller4b3ed642014-07-02 15:29:40 +10003729static int
3730open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003731 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003732{
3733 if (allowed_open->host_to_connect == NULL)
3734 return 0;
3735 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3736 allowed_open->port_to_connect != requestedport)
3737 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003738 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3739 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003740 return 0;
3741 return 1;
3742}
3743
3744/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003745 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003746 * we don't support FWD_PERMIT_ANY_PORT and
3747 * need to translate between the configured-host (listen_host)
3748 * and what we've sent to the remote server (channel_rfwd_bind_host)
3749 */
3750static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003751open_listen_match_tcpip(ForwardPermission *allowed_open,
3752 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003753{
3754 const char *allowed_host;
3755
3756 if (allowed_open->host_to_connect == NULL)
3757 return 0;
3758 if (allowed_open->listen_port != requestedport)
3759 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003760 if (!translate && allowed_open->listen_host == NULL &&
3761 requestedhost == NULL)
3762 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003763 allowed_host = translate ?
3764 channel_rfwd_bind_host(allowed_open->listen_host) :
3765 allowed_open->listen_host;
3766 if (allowed_host == NULL ||
3767 strcmp(allowed_host, requestedhost) != 0)
3768 return 0;
3769 return 1;
3770}
3771
Damien Miller7acefbb2014-07-18 14:11:24 +10003772static int
3773open_listen_match_streamlocal(ForwardPermission *allowed_open,
3774 const char *requestedpath)
3775{
3776 if (allowed_open->host_to_connect == NULL)
3777 return 0;
3778 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3779 return 0;
3780 if (allowed_open->listen_path == NULL ||
3781 strcmp(allowed_open->listen_path, requestedpath) != 0)
3782 return 0;
3783 return 1;
3784}
3785
Damien Miller5428f641999-11-25 11:54:57 +11003786/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003787 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003788 * local side.
3789 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003790static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003791channel_request_rforward_cancel_tcpip(struct ssh *ssh,
3792 const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003793{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003794 struct ssh_channels *sc = ssh->chanctxt;
3795 int r;
3796 u_int i;
3797 ForwardPermission *fp;
Darren Tuckere7066df2004-05-24 10:18:05 +10003798
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003799 for (i = 0; i < sc->num_permitted_opens; i++) {
3800 fp = &sc->permitted_opens[i];
3801 if (open_listen_match_tcpip(fp, host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003802 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003803 fp = NULL;
Darren Tuckere7066df2004-05-24 10:18:05 +10003804 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003805 if (fp == NULL) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003806 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003807 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003808 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003809 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3810 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
3811 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3812 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
3813 (r = sshpkt_put_u32(ssh, port)) != 0 ||
3814 (r = sshpkt_send(ssh)) != 0)
3815 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Darren Tuckere7066df2004-05-24 10:18:05 +10003816
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003817 fwd_perm_clear(fp); /* unregister */
Damien Millerf6dff7c2011-09-22 21:38:52 +10003818
3819 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003820}
3821
3822/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003823 * Request cancellation of remote forwarding of Unix domain socket
3824 * path from local side.
3825 */
3826static int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003827channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
Damien Miller7acefbb2014-07-18 14:11:24 +10003828{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003829 struct ssh_channels *sc = ssh->chanctxt;
3830 int r;
3831 u_int i;
3832 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10003833
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003834 for (i = 0; i < sc->num_permitted_opens; i++) {
3835 fp = &sc->permitted_opens[i];
3836 if (open_listen_match_streamlocal(fp, path))
Damien Miller7acefbb2014-07-18 14:11:24 +10003837 break;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003838 fp = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003839 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003840 if (fp == NULL) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003841 debug("%s: requested forward not found", __func__);
3842 return -1;
3843 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003844 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
3845 (r = sshpkt_put_cstring(ssh,
3846 "cancel-streamlocal-forward@openssh.com")) != 0 ||
3847 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
3848 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
3849 (r = sshpkt_send(ssh)) != 0)
3850 fatal("%s: send cancel: %s", __func__, ssh_err(r));
Damien Miller7acefbb2014-07-18 14:11:24 +10003851
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003852 fwd_perm_clear(fp); /* unregister */
Damien Miller7acefbb2014-07-18 14:11:24 +10003853
3854 return 0;
3855}
3856
3857/*
3858 * Request cancellation of remote forwarding of a connection from local side.
3859 */
3860int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003861channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
Damien Miller7acefbb2014-07-18 14:11:24 +10003862{
3863 if (fwd->listen_path != NULL) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003864 return channel_request_rforward_cancel_streamlocal(ssh,
3865 fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003866 } else {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003867 return channel_request_rforward_cancel_tcpip(ssh,
3868 fwd->listen_host,
3869 fwd->listen_port ? fwd->listen_port : fwd->allocated_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10003870 }
3871}
3872
3873/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003874 * Permits opening to any host/port if permitted_opens[] is empty. This is
3875 * usually called by the server, because the user could connect to any port
3876 * anyway, and the server has no way to know but to trust the client anyway.
3877 */
3878void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003879channel_permit_all_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003880{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003881 if (ssh->chanctxt->num_permitted_opens == 0)
3882 ssh->chanctxt->all_opens_permitted = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003883}
3884
Ben Lindstroma3700052001-04-05 23:26:32 +00003885void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003886channel_add_permitted_opens(struct ssh *ssh, char *host, int port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003887{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003888 struct ssh_channels *sc = ssh->chanctxt;
3889
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003890 debug("allow port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003891 fwd_perm_list_add(ssh, FWDPERM_USER, host, port, NULL, NULL, 0, NULL);
3892 sc->all_opens_permitted = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003893}
3894
Darren Tucker68afb8c2011-10-02 18:59:03 +11003895/*
3896 * Update the listen port for a dynamic remote forward, after
3897 * the actual 'newport' has been allocated. If 'newport' < 0 is
3898 * passed then they entry will be invalidated.
3899 */
3900void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003901channel_update_permitted_opens(struct ssh *ssh, int idx, int newport)
Darren Tucker68afb8c2011-10-02 18:59:03 +11003902{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003903 struct ssh_channels *sc = ssh->chanctxt;
3904
3905 if (idx < 0 || (u_int)idx >= sc->num_permitted_opens) {
3906 debug("%s: index out of range: %d num_permitted_opens %d",
3907 __func__, idx, sc->num_permitted_opens);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003908 return;
3909 }
3910 debug("%s allowed port %d for forwarding to host %s port %d",
3911 newport > 0 ? "Updating" : "Removing",
3912 newport,
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003913 sc->permitted_opens[idx].host_to_connect,
3914 sc->permitted_opens[idx].port_to_connect);
3915 if (newport <= 0)
3916 fwd_perm_clear(&sc->permitted_opens[idx]);
3917 else {
3918 sc->permitted_opens[idx].listen_port =
Darren Tucker68afb8c2011-10-02 18:59:03 +11003919 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003920 }
3921}
3922
Damien Millera765cf42006-07-24 14:08:13 +10003923int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003924channel_add_adm_permitted_opens(struct ssh *ssh, char *host, int port)
Damien Miller9b439df2006-07-24 14:04:00 +10003925{
Damien Millera765cf42006-07-24 14:08:13 +10003926 debug("config allows port forwarding to host %s port %d", host, port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003927 return fwd_perm_list_add(ssh, FWDPERM_ADMIN, host, port,
3928 NULL, NULL, 0, NULL);
Damien Miller9b439df2006-07-24 14:04:00 +10003929}
3930
3931void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003932channel_disable_adm_local_opens(struct ssh *ssh)
Damien Millerc6081482012-04-22 11:18:53 +10003933{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003934 channel_clear_adm_permitted_opens(ssh);
3935 fwd_perm_list_add(ssh, FWDPERM_ADMIN, NULL, 0, NULL, NULL, 0, NULL);
Damien Millerc6081482012-04-22 11:18:53 +10003936}
3937
3938void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003939channel_clear_permitted_opens(struct ssh *ssh)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003940{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003941 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003942
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003943 sc->permitted_opens = xrecallocarray(sc->permitted_opens,
3944 sc->num_permitted_opens, 0, sizeof(*sc->permitted_opens));
3945 sc->num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003946}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003947
Damien Miller9b439df2006-07-24 14:04:00 +10003948void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003949channel_clear_adm_permitted_opens(struct ssh *ssh)
Damien Miller9b439df2006-07-24 14:04:00 +10003950{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003951 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller9b439df2006-07-24 14:04:00 +10003952
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003953 sc->permitted_adm_opens = xrecallocarray(sc->permitted_adm_opens,
3954 sc->num_adm_permitted_opens, 0, sizeof(*sc->permitted_adm_opens));
3955 sc->num_adm_permitted_opens = 0;
Darren Tuckere7140f22008-06-10 23:01:51 +10003956}
3957
Darren Tucker1338b9e2011-10-02 18:57:35 +11003958/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3959int
3960permitopen_port(const char *p)
3961{
3962 int port;
3963
3964 if (strcmp(p, "*") == 0)
3965 return FWD_PERMIT_ANY_PORT;
3966 if ((port = a2port(p)) > 0)
3967 return port;
3968 return -1;
3969}
3970
Damien Millerbd740252008-05-19 15:37:09 +10003971/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003972static int
Damien Millerbd740252008-05-19 15:37:09 +10003973connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003974{
Damien Millerbd740252008-05-19 15:37:09 +10003975 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003976 struct sockaddr_un *sunaddr;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00003977 char ntop[NI_MAXHOST];
3978 char strport[MAXIMUM(NI_MAXSERV, sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003979
Damien Millerbd740252008-05-19 15:37:09 +10003980 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003981 switch (cctx->ai->ai_family) {
3982 case AF_UNIX:
3983 /* unix:pathname instead of host:port */
3984 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3985 strlcpy(ntop, "unix", sizeof(ntop));
3986 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3987 break;
3988 case AF_INET:
3989 case AF_INET6:
3990 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3991 ntop, sizeof(ntop), strport, sizeof(strport),
3992 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3993 error("connect_next: getnameinfo failed");
3994 continue;
3995 }
3996 break;
3997 default:
Damien Miller34132e52000-01-14 15:45:46 +11003998 continue;
3999 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11004000 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4001 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004002 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11004003 error("socket: %.100s", strerror(errno));
4004 else
4005 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004006 continue;
4007 }
Damien Miller232711f2004-06-15 10:35:30 +10004008 if (set_nonblock(sock) == -1)
4009 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10004010 if (connect(sock, cctx->ai->ai_addr,
4011 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
4012 debug("connect_next: host %.100s ([%.100s]:%s): "
4013 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004014 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004015 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004016 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004017 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004018 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004019 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004020 if (cctx->ai->ai_family != AF_UNIX)
4021 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004022 debug("connect_next: host %.100s ([%.100s]:%s) "
4023 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4024 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004025 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004026 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004027 return -1;
4028}
Damien Millerb38eff82000-04-01 11:09:21 +10004029
Damien Millerbd740252008-05-19 15:37:09 +10004030static void
4031channel_connect_ctx_free(struct channel_connect *cctx)
4032{
Darren Tuckera627d422013-06-02 07:31:17 +10004033 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004034 if (cctx->aitop) {
4035 if (cctx->aitop->ai_family == AF_UNIX)
4036 free(cctx->aitop);
4037 else
4038 freeaddrinfo(cctx->aitop);
4039 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004040 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004041}
4042
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004043/*
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004044 * Return connecting socket to remote host:port or local socket path,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004045 * passing back the failure reason if appropriate.
4046 */
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004047static int
4048connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4049 char *ctype, char *rname, struct channel_connect *cctx,
4050 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10004051{
4052 struct addrinfo hints;
4053 int gaierr;
4054 int sock = -1;
4055 char strport[NI_MAXSERV];
Damien Miller7acefbb2014-07-18 14:11:24 +10004056
4057 if (port == PORT_STREAMLOCAL) {
4058 struct sockaddr_un *sunaddr;
4059 struct addrinfo *ai;
4060
4061 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4062 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004063 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004064 }
4065
4066 /*
4067 * Fake up a struct addrinfo for AF_UNIX connections.
4068 * channel_connect_ctx_free() must check ai_family
4069 * and use free() not freeaddirinfo() for AF_UNIX.
4070 */
4071 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4072 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4073 ai->ai_addr = (struct sockaddr *)(ai + 1);
4074 ai->ai_addrlen = sizeof(*sunaddr);
4075 ai->ai_family = AF_UNIX;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004076 ai->ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004077 ai->ai_protocol = PF_UNSPEC;
4078 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4079 sunaddr->sun_family = AF_UNIX;
4080 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004081 cctx->aitop = ai;
Damien Miller7acefbb2014-07-18 14:11:24 +10004082 } else {
4083 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004084 hints.ai_family = ssh->chanctxt->IPv4or6;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004085 hints.ai_socktype = socktype;
Damien Miller7acefbb2014-07-18 14:11:24 +10004086 snprintf(strport, sizeof strport, "%d", port);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004087 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx->aitop))
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004088 != 0) {
4089 if (errmsg != NULL)
4090 *errmsg = ssh_gai_strerror(gaierr);
4091 if (reason != NULL)
4092 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10004093 error("connect_to %.100s: unknown host (%s)", name,
4094 ssh_gai_strerror(gaierr));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004095 return -1;
Damien Miller7acefbb2014-07-18 14:11:24 +10004096 }
Damien Millerbd740252008-05-19 15:37:09 +10004097 }
4098
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004099 cctx->host = xstrdup(name);
4100 cctx->port = port;
4101 cctx->ai = cctx->aitop;
Damien Millerbd740252008-05-19 15:37:09 +10004102
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004103 if ((sock = connect_next(cctx)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004104 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004105 name, port, strerror(errno));
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004106 return -1;
4107 }
4108
4109 return sock;
4110}
4111
4112/* Return CONNECTING channel to remote host:port or local socket path */
4113static Channel *
4114connect_to(struct ssh *ssh, const char *host, int port,
4115 char *ctype, char *rname)
4116{
4117 struct channel_connect cctx;
4118 Channel *c;
4119 int sock;
4120
4121 memset(&cctx, 0, sizeof(cctx));
4122 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4123 &cctx, NULL, NULL);
4124 if (sock == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004125 channel_connect_ctx_free(&cctx);
4126 return NULL;
4127 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004128 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
Damien Millerbd740252008-05-19 15:37:09 +10004129 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004130 c->host_port = port;
4131 c->path = xstrdup(host);
Damien Millerbd740252008-05-19 15:37:09 +10004132 c->connect_ctx = cctx;
Damien Millerbd740252008-05-19 15:37:09 +10004133
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004134 return c;
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004135}
4136
markus@openbsd.org8d057842016-09-30 09:19:13 +00004137/*
4138 * returns either the newly connected channel or the downstream channel
4139 * that needs to deal with this connection.
4140 */
Damien Millerbd740252008-05-19 15:37:09 +10004141Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004142channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004143 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004144{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004145 struct ssh_channels *sc = ssh->chanctxt;
4146 u_int i;
4147 ForwardPermission *fp;
Damien Millerbd740252008-05-19 15:37:09 +10004148
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004149 for (i = 0; i < sc->num_permitted_opens; i++) {
4150 fp = &sc->permitted_opens[i];
4151 if (open_listen_match_tcpip(fp, listen_host, listen_port, 1)) {
4152 if (fp->downstream)
4153 return fp->downstream;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004154 if (fp->port_to_connect == 0)
4155 return rdynamic_connect_prepare(ssh,
4156 ctype, rname);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004157 return connect_to(ssh,
4158 fp->host_to_connect, fp->port_to_connect,
4159 ctype, rname);
Damien Millerbd740252008-05-19 15:37:09 +10004160 }
4161 }
4162 error("WARNING: Server requests forwarding for unknown listen_port %d",
4163 listen_port);
4164 return NULL;
4165}
4166
Damien Miller7acefbb2014-07-18 14:11:24 +10004167Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004168channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4169 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004170{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004171 struct ssh_channels *sc = ssh->chanctxt;
4172 u_int i;
4173 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004174
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004175 for (i = 0; i < sc->num_permitted_opens; i++) {
4176 fp = &sc->permitted_opens[i];
4177 if (open_listen_match_streamlocal(fp, path)) {
4178 return connect_to(ssh,
4179 fp->host_to_connect, fp->port_to_connect,
4180 ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004181 }
4182 }
4183 error("WARNING: Server requests forwarding for unknown path %.100s",
4184 path);
4185 return NULL;
4186}
4187
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004188/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004189Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004190channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4191 char *ctype, char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004192{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004193 struct ssh_channels *sc = ssh->chanctxt;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004194 struct channel_connect cctx;
4195 Channel *c;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004196 u_int i, permit, permit_adm = 1;
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004197 int sock;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004198 ForwardPermission *fp;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004199
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004200 permit = sc->all_opens_permitted;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004201 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004202 for (i = 0; i < sc->num_permitted_opens; i++) {
4203 fp = &sc->permitted_opens[i];
4204 if (open_match(fp, host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004205 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004206 break;
4207 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004208 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004209 }
Damien Miller9b439df2006-07-24 14:04:00 +10004210
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004211 if (sc->num_adm_permitted_opens > 0) {
Damien Miller9b439df2006-07-24 14:04:00 +10004212 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004213 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4214 fp = &sc->permitted_adm_opens[i];
4215 if (open_match(fp, host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004216 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004217 break;
4218 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004219 }
Damien Miller9b439df2006-07-24 14:04:00 +10004220 }
4221
4222 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004223 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004224 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00004225 if (reason != NULL)
4226 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10004227 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004228 }
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004229
4230 memset(&cctx, 0, sizeof(cctx));
4231 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4232 &cctx, reason, errmsg);
4233 if (sock == -1) {
4234 channel_connect_ctx_free(&cctx);
4235 return NULL;
4236 }
4237
4238 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4239 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4240 c->host_port = port;
4241 c->path = xstrdup(host);
4242 c->connect_ctx = cctx;
4243
4244 return c;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004245}
4246
Damien Miller7acefbb2014-07-18 14:11:24 +10004247/* Check if connecting to that path is permitted and connect. */
4248Channel *
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004249channel_connect_to_path(struct ssh *ssh, const char *path,
4250 char *ctype, char *rname)
Damien Miller7acefbb2014-07-18 14:11:24 +10004251{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004252 struct ssh_channels *sc = ssh->chanctxt;
4253 u_int i, permit, permit_adm = 1;
4254 ForwardPermission *fp;
Damien Miller7acefbb2014-07-18 14:11:24 +10004255
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004256 permit = sc->all_opens_permitted;
Damien Miller7acefbb2014-07-18 14:11:24 +10004257 if (!permit) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004258 for (i = 0; i < sc->num_permitted_opens; i++) {
4259 fp = &sc->permitted_opens[i];
4260 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004261 permit = 1;
4262 break;
4263 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004264 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004265 }
4266
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004267 if (sc->num_adm_permitted_opens > 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004268 permit_adm = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004269 for (i = 0; i < sc->num_adm_permitted_opens; i++) {
4270 fp = &sc->permitted_adm_opens[i];
4271 if (open_match(fp, path, PORT_STREAMLOCAL)) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004272 permit_adm = 1;
4273 break;
4274 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004275 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004276 }
4277
4278 if (!permit || !permit_adm) {
4279 logit("Received request to connect to path %.100s, "
4280 "but the request was denied.", path);
4281 return NULL;
4282 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004283 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
Damien Miller7acefbb2014-07-18 14:11:24 +10004284}
4285
Damien Miller0e220db2004-06-15 10:34:08 +10004286void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004287channel_send_window_changes(struct ssh *ssh)
Damien Miller0e220db2004-06-15 10:34:08 +10004288{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004289 struct ssh_channels *sc = ssh->chanctxt;
Damien Miller0e220db2004-06-15 10:34:08 +10004290 struct winsize ws;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004291 int r;
4292 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004293
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004294 for (i = 0; i < sc->channels_alloc; i++) {
4295 if (sc->channels[i] == NULL || !sc->channels[i]->client_tty ||
4296 sc->channels[i]->type != SSH_CHANNEL_OPEN)
Damien Miller0e220db2004-06-15 10:34:08 +10004297 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004298 if (ioctl(sc->channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
Damien Miller0e220db2004-06-15 10:34:08 +10004299 continue;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004300 channel_request_start(ssh, i, "window-change", 0);
4301 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4302 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4303 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4304 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4305 (r = sshpkt_send(ssh)) != 0)
4306 fatal("%s: channel %u: send window-change: %s",
4307 __func__, i, ssh_err(r));
Damien Miller0e220db2004-06-15 10:34:08 +10004308 }
4309}
4310
markus@openbsd.org609d7a62017-09-21 19:16:53 +00004311/* Return RDYNAMIC_OPEN channel: channel allows SOCKS, but is not connected */
4312static Channel *
4313rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4314{
4315 Channel *c;
4316 int r;
4317
4318 c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4319 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4320 c->host_port = 0;
4321 c->path = NULL;
4322
4323 /*
4324 * We need to open the channel before we have a FD,
4325 * so that we can get SOCKS header from peer.
4326 */
4327 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4328 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4329 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4330 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
4331 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
4332 fatal("%s: channel %i: confirm: %s", __func__,
4333 c->self, ssh_err(r));
4334 }
4335 return c;
4336}
4337
4338/* Return CONNECTING socket to remote host:port or local socket path */
4339static int
4340rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4341{
4342 struct channel_connect cctx;
4343 int sock;
4344
4345 memset(&cctx, 0, sizeof(cctx));
4346 sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4347 NULL, &cctx, NULL, NULL);
4348 if (sock == -1)
4349 channel_connect_ctx_free(&cctx);
4350 else {
4351 /* similar to SSH_CHANNEL_CONNECTING but we've already sent the open */
4352 c->type = SSH_CHANNEL_RDYNAMIC_FINISH;
4353 c->connect_ctx = cctx;
4354 channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4355 }
4356 return sock;
4357}
4358
Ben Lindstrome9c99912001-06-09 00:41:05 +00004359/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004360
Damien Miller5428f641999-11-25 11:54:57 +11004361/*
4362 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004363 * Returns 0 and a suitable display number for the DISPLAY variable
4364 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004365 */
Kevin Steves366298c2001-12-19 17:58:01 +00004366int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004367x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4368 int x11_use_localhost, int single_connection,
4369 u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004370{
Damien Millere7378562001-12-21 14:58:35 +11004371 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004372 int display_number, sock;
4373 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004374 struct addrinfo hints, *ai, *aitop;
4375 char strport[NI_MAXSERV];
4376 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004377
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004378 if (chanids == NULL)
4379 return -1;
4380
Damien Millera34a28b1999-12-14 10:47:15 +11004381 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004382 display_number < MAX_DISPLAYS;
4383 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004384 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004385 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004386 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004387 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004388 hints.ai_socktype = SOCK_STREAM;
4389 snprintf(strport, sizeof strport, "%d", port);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004390 if ((gaierr = getaddrinfo(NULL, strport,
4391 &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004392 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004393 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004394 }
Damien Miller34132e52000-01-14 15:45:46 +11004395 for (ai = aitop; ai; ai = ai->ai_next) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004396 if (ai->ai_family != AF_INET &&
4397 ai->ai_family != AF_INET6)
Damien Miller34132e52000-01-14 15:45:46 +11004398 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004399 sock = socket(ai->ai_family, ai->ai_socktype,
4400 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004401 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004402 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4403#ifdef EPFNOSUPPORT
4404 && (errno != EPFNOSUPPORT)
4405#endif
4406 ) {
Damien Millere2192732000-01-17 13:22:55 +11004407 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004408 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004409 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004410 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004411 debug("x11_create_display_inet: Socket family %d not supported",
4412 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004413 continue;
4414 }
Damien Miller34132e52000-01-14 15:45:46 +11004415 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004416 if (ai->ai_family == AF_INET6)
4417 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004418 if (x11_use_localhost)
djm@openbsd.orgacf559e2017-10-25 00:15:35 +00004419 set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004420 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004421 debug2("%s: bind port %d: %.100s", __func__,
4422 port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004423 close(sock);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004424 for (n = 0; n < num_socks; n++)
Damien Miller34132e52000-01-14 15:45:46 +11004425 close(socks[n]);
Damien Miller34132e52000-01-14 15:45:46 +11004426 num_socks = 0;
4427 break;
4428 }
4429 socks[num_socks++] = sock;
4430 if (num_socks == NUM_SOCKS)
4431 break;
Damien Miller95def091999-11-25 00:26:21 +11004432 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004433 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004434 if (num_socks > 0)
4435 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004436 }
Damien Miller95def091999-11-25 00:26:21 +11004437 if (display_number >= MAX_DISPLAYS) {
4438 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004439 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004440 }
Damien Miller95def091999-11-25 00:26:21 +11004441 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004442 for (n = 0; n < num_socks; n++) {
4443 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004444 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004445 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004446 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004447 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004448 }
Damien Miller95def091999-11-25 00:26:21 +11004449 }
Damien Miller34132e52000-01-14 15:45:46 +11004450
Damien Miller34132e52000-01-14 15:45:46 +11004451 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004452 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004453 for (n = 0; n < num_socks; n++) {
4454 sock = socks[n];
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004455 nc = channel_new(ssh, "x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004456 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4457 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004458 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004459 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004460 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004461 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004462 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004463
Kevin Steves366298c2001-12-19 17:58:01 +00004464 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004465 *display_numberp = display_number;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004466 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004467}
4468
Ben Lindstrombba81212001-06-25 05:01:22 +00004469static int
Damien Miller819dbb62009-01-21 16:46:26 +11004470connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004471{
Damien Miller95def091999-11-25 00:26:21 +11004472 int sock;
4473 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004474
Damien Miller3afe3752001-12-21 12:39:51 +11004475 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4476 if (sock < 0)
4477 error("socket: %.100s", strerror(errno));
4478 memset(&addr, 0, sizeof(addr));
4479 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004480 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004481 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004482 return sock;
4483 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004484 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4485 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004486}
4487
Damien Miller819dbb62009-01-21 16:46:26 +11004488static int
4489connect_local_xsocket(u_int dnr)
4490{
4491 char buf[1024];
4492 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4493 return connect_local_xsocket_path(buf);
4494}
4495
Darren Tuckerda39b092017-03-10 13:22:32 +11004496#ifdef __APPLE__
4497static int
4498is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4499{
4500 struct stat sbuf;
4501
4502 if (strlcpy(path, display, pathlen) >= pathlen) {
4503 error("%s: display path too long", __func__);
4504 return 0;
4505 }
4506 if (display[0] != '/')
4507 return 0;
4508 if (stat(path, &sbuf) == 0) {
4509 return 1;
4510 } else {
4511 char *dot = strrchr(path, '.');
4512 if (dot != NULL) {
4513 *dot = '\0';
4514 if (stat(path, &sbuf) == 0) {
4515 return 1;
4516 }
4517 }
4518 }
4519 return 0;
4520}
4521#endif
4522
Damien Millerbd483e72000-04-30 10:00:53 +10004523int
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004524x11_connect_display(struct ssh *ssh)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004525{
Damien Miller57c4e872006-03-31 23:11:07 +11004526 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004527 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004528 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004529 struct addrinfo hints, *ai, *aitop;
4530 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004531 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004532
Damien Miller95def091999-11-25 00:26:21 +11004533 /* Try to open a socket for the local X server. */
4534 display = getenv("DISPLAY");
4535 if (!display) {
4536 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004537 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004538 }
Damien Miller5428f641999-11-25 11:54:57 +11004539 /*
4540 * Now we decode the value of the DISPLAY variable and make a
4541 * connection to the real X server.
4542 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004543
Damien Miller819dbb62009-01-21 16:46:26 +11004544#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004545 /* Check if display is a path to a socket (as set by launchd). */
4546 {
4547 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004548
Darren Tuckerda39b092017-03-10 13:22:32 +11004549 if (is_path_to_xsocket(display, path, sizeof(path))) {
4550 debug("x11_connect_display: $DISPLAY is launchd");
4551
4552 /* Create a socket. */
4553 sock = connect_local_xsocket_path(path);
4554 if (sock < 0)
4555 return -1;
4556
4557 /* OK, we now have a connection to the display. */
4558 return sock;
4559 }
Damien Miller819dbb62009-01-21 16:46:26 +11004560 }
4561#endif
Damien Miller5428f641999-11-25 11:54:57 +11004562 /*
4563 * Check if it is a unix domain socket. Unix domain displays are in
4564 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4565 */
Damien Miller95def091999-11-25 00:26:21 +11004566 if (strncmp(display, "unix:", 5) == 0 ||
4567 display[0] == ':') {
4568 /* Connect to the unix domain socket. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004569 if (sscanf(strrchr(display, ':') + 1, "%u",
4570 &display_number) != 1) {
4571 error("Could not parse display number from DISPLAY: "
4572 "%.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004573 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004574 }
4575 /* Create a socket. */
4576 sock = connect_local_xsocket(display_number);
4577 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004578 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004579
4580 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004581 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004582 }
Damien Miller5428f641999-11-25 11:54:57 +11004583 /*
4584 * Connect to an inet socket. The DISPLAY value is supposedly
4585 * hostname:d[.s], where hostname may also be numeric IP address.
4586 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004587 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004588 cp = strchr(buf, ':');
4589 if (!cp) {
4590 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004591 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004592 }
Damien Miller95def091999-11-25 00:26:21 +11004593 *cp = 0;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004594 /*
4595 * buf now contains the host name. But first we parse the
4596 * display number.
4597 */
Damien Miller57c4e872006-03-31 23:11:07 +11004598 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004599 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004600 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004601 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004602 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004603
Damien Miller34132e52000-01-14 15:45:46 +11004604 /* Look up the host address */
4605 memset(&hints, 0, sizeof(hints));
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004606 hints.ai_family = ssh->chanctxt->IPv4or6;
Damien Miller34132e52000-01-14 15:45:46 +11004607 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004608 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004609 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004610 error("%.100s: unknown host. (%s)", buf,
4611 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004612 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004613 }
Damien Miller34132e52000-01-14 15:45:46 +11004614 for (ai = aitop; ai; ai = ai->ai_next) {
4615 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004616 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004617 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004618 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004619 continue;
4620 }
4621 /* Connect it to the display. */
4622 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004623 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004624 6000 + display_number, strerror(errno));
4625 close(sock);
4626 continue;
4627 }
4628 /* Success */
4629 break;
Damien Miller34132e52000-01-14 15:45:46 +11004630 }
Damien Miller34132e52000-01-14 15:45:46 +11004631 freeaddrinfo(aitop);
4632 if (!ai) {
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004633 error("connect %.100s port %u: %.100s", buf,
4634 6000 + display_number, strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004635 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004636 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004637 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004638 return sock;
4639}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004640
Damien Millerbd483e72000-04-30 10:00:53 +10004641/*
Damien Miller5428f641999-11-25 11:54:57 +11004642 * Requests forwarding of X11 connections, generates fake authentication
4643 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004644 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004645 */
Damien Miller4af51302000-04-16 11:18:38 +10004646void
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004647x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
4648 const char *disp, const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004649{
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004650 struct ssh_channels *sc = ssh->chanctxt;
Ben Lindstrom46c16222000-12-22 01:43:59 +00004651 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004652 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004653 const char *cp;
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004654 char *new_data;
4655 int r, screen_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004656
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004657 if (sc->x11_saved_display == NULL)
4658 sc->x11_saved_display = xstrdup(disp);
4659 else if (strcmp(disp, sc->x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004660 error("x11_request_forwarding_with_spoofing: different "
4661 "$DISPLAY already forwarded");
4662 return;
4663 }
4664
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004665 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004666 if (cp)
4667 cp = strchr(cp, '.');
4668 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004669 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004670 else
4671 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004672
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004673 if (sc->x11_saved_proto == NULL) {
Damien Miller13390022005-07-06 09:44:19 +10004674 /* Save protocol name. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004675 sc->x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004676
4677 /* Extract real authentication data. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004678 sc->x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004679 for (i = 0; i < data_len; i++) {
4680 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4681 fatal("x11_request_forwarding: bad "
4682 "authentication data: %.100s", data);
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004683 sc->x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004684 }
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004685 sc->x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004686
4687 /* Generate fake data of the same length. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004688 sc->x11_fake_data = xmalloc(data_len);
4689 arc4random_buf(sc->x11_fake_data, data_len);
4690 sc->x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004691 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004692
Damien Miller95def091999-11-25 00:26:21 +11004693 /* Convert the fake data into hex. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004694 new_data = tohex(sc->x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004695
Damien Miller95def091999-11-25 00:26:21 +11004696 /* Send the request packet. */
djm@openbsd.orgdbee4112017-09-12 06:32:07 +00004697 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
4698 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
4699 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
4700 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
4701 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
4702 (r = sshpkt_send(ssh)) != 0 ||
4703 (r = ssh_packet_write_wait(ssh)) != 0)
4704 fatal("%s: send x11-req: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +10004705 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004706}