blob: 9f9e972f4291982427ec6e70774a519d038bf3ab [file] [log] [blame]
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00001/* $OpenBSD: channels.c,v 1.351 2016/07/19 11:38:53 dtucker 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>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000045#include <sys/param.h> /* MIN MAX */
Damien Miller7acefbb2014-07-18 14:11:24 +100046#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100047#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110048#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100049#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100050#ifdef HAVE_SYS_TIME_H
51# include <sys/time.h>
52#endif
Damien Millerefc04e72006-07-10 20:26:27 +100053
54#include <netinet/in.h>
55#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110056
Darren Tucker39972492006-07-12 22:22:46 +100057#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110058#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100059#include <netdb.h>
Darren Tucker37f92202015-02-23 03:07:24 +110060#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000061#include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110062#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100063#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100064#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100065#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110066#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100067#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100068#include <stdarg.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 "ssh1.h"
74#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100078#include "buffer.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
Ben Lindstrome9c99912001-06-09 00:41:05 +000086/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * Pointer to an array containing all allocated channels. The array is
90 * dynamically extended as needed.
91 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000092static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000096 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110097 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100098static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000102 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100103 */
Damien Miller5e953212001-01-30 09:14:00 +1100104static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Ben Lindstrome9c99912001-06-09 00:41:05 +0000107/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * Data structure for storing which hosts are permitted for forward requests.
111 * The local sides of any remote forwards are stored in this array to prevent
112 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
113 * network (which might be behind a firewall).
114 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000115/* XXX: streamlocal wants a path instead of host:port */
116/* Overload host_to_connect; we could just make this match Forward */
117/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100118typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000119 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000120 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000121 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000122 char *listen_path; /* Remote side should listen path. */
123 int listen_port; /* Remote side should listen port. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124} ForwardPermission;
125
Damien Miller9b439df2006-07-24 14:04:00 +1000126/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000127static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000128
Damien Miller9b439df2006-07-24 14:04:00 +1000129/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000130static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000131
132/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000134
135/* Number of permitted host/port pair in the array permitted by the admin. */
136static int num_adm_permitted_opens = 0;
137
Darren Tucker1338b9e2011-10-02 18:57:35 +1100138/* special-case port number meaning allow any port */
139#define FWD_PERMIT_ANY_PORT 0
140
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000141/* special-case wildcard meaning allow any host */
142#define FWD_PERMIT_ANY_HOST "*"
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * If this is true, all opens are permitted. This is the case on the server
146 * on which we have to trust the client anyway, and the user could do
147 * anything after logging in anyway.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149static int all_opens_permitted = 0;
150
Ben Lindstrome9c99912001-06-09 00:41:05 +0000151
152/* -- X11 forwarding */
153
154/* Maximum number of fake X11 displays to try. */
155#define MAX_DISPLAYS 1000
156
Damien Miller13390022005-07-06 09:44:19 +1000157/* Saved X11 local (client) display. */
158static char *x11_saved_display = NULL;
159
Ben Lindstrome9c99912001-06-09 00:41:05 +0000160/* Saved X11 authentication protocol name. */
161static char *x11_saved_proto = NULL;
162
163/* Saved X11 authentication data. This is the real data. */
164static char *x11_saved_data = NULL;
165static u_int x11_saved_data_len = 0;
166
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000167/* Deadline after which all X11 connections are refused */
168static u_int x11_refuse_time;
169
Ben Lindstrome9c99912001-06-09 00:41:05 +0000170/*
171 * Fake X11 authentication data. This is what the server will be sending us;
172 * we should replace any occurrences of this by the real data.
173 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100174static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000175static u_int x11_fake_data_len;
176
177
178/* -- agent forwarding */
179
180#define NUM_SOCKS 10
181
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000182/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000183static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000184
Ben Lindstrome9c99912001-06-09 00:41:05 +0000185/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000187
Damien Millerbd740252008-05-19 15:37:09 +1000188/* non-blocking connect helpers */
189static int connect_next(struct channel_connect *);
190static void channel_connect_ctx_free(struct channel_connect *);
191
Ben Lindstrome9c99912001-06-09 00:41:05 +0000192/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000193
194Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100195channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000196{
197 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000198
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000199 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100200 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000201 return NULL;
202 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000203 c = channels[id];
204 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100205 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000206 return NULL;
207 }
208 return c;
209}
210
Damien Miller5428f641999-11-25 11:54:57 +1100211/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100212 * Returns the channel if it is allowed to receive protocol messages.
213 * Private channels, like listening sockets, may not receive messages.
214 */
215Channel *
216channel_lookup(int id)
217{
218 Channel *c;
219
220 if ((c = channel_by_id(id)) == NULL)
221 return (NULL);
222
Damien Millerd62f2ca2006-03-26 13:57:41 +1100223 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100224 case SSH_CHANNEL_X11_OPEN:
225 case SSH_CHANNEL_LARVAL:
226 case SSH_CHANNEL_CONNECTING:
227 case SSH_CHANNEL_DYNAMIC:
228 case SSH_CHANNEL_OPENING:
229 case SSH_CHANNEL_OPEN:
230 case SSH_CHANNEL_INPUT_DRAINING:
231 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller36187092013-06-10 13:07:11 +1000232 case SSH_CHANNEL_ABANDONED:
Damien Millerd47c62a2005-12-13 19:33:57 +1100233 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100234 }
235 logit("Non-public channel %d, type %d.", id, c->type);
236 return (NULL);
237}
238
239/*
Damien Millere247cc42000-05-07 12:03:14 +1000240 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000241 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100242 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100244channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000245 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246{
Damien Miller95def091999-11-25 00:26:21 +1100247 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100248 channel_max_fd = MAX(channel_max_fd, rfd);
249 channel_max_fd = MAX(channel_max_fd, wfd);
250 channel_max_fd = MAX(channel_max_fd, efd);
251
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100252 if (rfd != -1)
253 fcntl(rfd, F_SETFD, FD_CLOEXEC);
254 if (wfd != -1 && wfd != rfd)
255 fcntl(wfd, F_SETFD, FD_CLOEXEC);
256 if (efd != -1 && efd != rfd && efd != wfd)
257 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000258
Damien Miller6f83b8e2000-05-02 09:23:45 +1000259 c->rfd = rfd;
260 c->wfd = wfd;
261 c->sock = (rfd == wfd) ? rfd : -1;
262 c->efd = efd;
263 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100264
Darren Tuckered3cdc02008-06-16 23:29:18 +1000265 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000266 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000267#ifdef _AIX
268 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000269 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000270#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100271
Damien Miller69b69aa2000-10-28 14:19:58 +1100272 /* enable nonblocking mode */
273 if (nonblock) {
274 if (rfd != -1)
275 set_nonblock(rfd);
276 if (wfd != -1)
277 set_nonblock(wfd);
278 if (efd != -1)
279 set_nonblock(efd);
280 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000281}
282
283/*
284 * Allocate a new channel object and set its type and socket. This will cause
285 * remote_name to be freed.
286 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000287Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000288channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000289 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000290{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000291 int found;
292 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000293 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
Damien Miller95def091999-11-25 00:26:21 +1100295 /* Do initial allocation if this is the first call. */
296 if (channels_alloc == 0) {
297 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100298 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100299 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000300 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100301 }
302 /* Try to find a free slot where to put the new channel. */
303 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100305 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000306 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100307 break;
308 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000309 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100310 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100311 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000312 if (channels_alloc > 10000)
313 fatal("channel_new: internal error: channels_alloc %d "
314 "too big.", channels_alloc);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000315 channels = xreallocarray(channels, channels_alloc + 10,
Damien Miller36812092006-03-26 14:22:47 +1100316 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000317 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100318 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100319 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000320 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100321 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000322 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100323 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100324 buffer_init(&c->input);
325 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000326 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100327 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000328 c->listening_addr = NULL;
329 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100330 c->ostate = CHAN_OUTPUT_OPEN;
331 c->istate = CHAN_INPUT_OPEN;
332 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000333 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000334 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100335 c->self = found;
336 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000337 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000338 c->local_window = window;
339 c->local_window_max = window;
340 c->local_consumed = 0;
341 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100342 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000343 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000344 c->remote_window = 0;
345 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000346 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100347 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000348 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100349 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000350 c->open_confirm = NULL;
351 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000352 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100353 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000354 c->filter_ctx = NULL;
355 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100356 c->ctl_chan = -1;
357 c->mux_rcb = NULL;
358 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000359 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100360 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000361 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100362 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000363 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000365
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000366static int
367channel_find_maxfd(void)
368{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000369 u_int i;
370 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000371 Channel *c;
372
373 for (i = 0; i < channels_alloc; i++) {
374 c = channels[i];
375 if (c != NULL) {
376 max = MAX(max, c->rfd);
377 max = MAX(max, c->wfd);
378 max = MAX(max, c->efd);
379 }
380 }
381 return max;
382}
383
384int
385channel_close_fd(int *fdp)
386{
387 int ret = 0, fd = *fdp;
388
389 if (fd != -1) {
390 ret = close(fd);
391 *fdp = -1;
392 if (fd == channel_max_fd)
393 channel_max_fd = channel_find_maxfd();
394 }
395 return ret;
396}
397
Damien Miller6f83b8e2000-05-02 09:23:45 +1000398/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000399static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000400channel_close_fds(Channel *c)
401{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000402 channel_close_fd(&c->sock);
403 channel_close_fd(&c->rfd);
404 channel_close_fd(&c->wfd);
405 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000406}
407
408/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000409void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000410channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000412 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000413 u_int i, n;
Damien Millerb84886b2008-05-19 15:05:07 +1000414 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000415
416 for (n = 0, i = 0; i < channels_alloc; i++)
417 if (channels[i])
418 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000419 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000420 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000421
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000422 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000423 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000424 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000425
Damien Miller6f83b8e2000-05-02 09:23:45 +1000426 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000427 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000428 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000429 buffer_free(&c->input);
430 buffer_free(&c->output);
431 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000432 free(c->remote_name);
433 c->remote_name = NULL;
434 free(c->path);
435 c->path = NULL;
436 free(c->listening_addr);
437 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000438 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
439 if (cc->abandon_cb != NULL)
440 cc->abandon_cb(c, cc->ctx);
441 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100442 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000443 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000444 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000445 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
446 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000447 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000448 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000449}
450
Ben Lindstrome9c99912001-06-09 00:41:05 +0000451void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000452channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000453{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000454 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000455
Ben Lindstrom601e4362001-06-21 03:19:23 +0000456 for (i = 0; i < channels_alloc; i++)
457 if (channels[i] != NULL)
458 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000459}
460
461/*
462 * Closes the sockets/fds of all channels. This is used to close extra file
463 * descriptors after a fork.
464 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000465void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000466channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000467{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000468 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000469
470 for (i = 0; i < channels_alloc; i++)
471 if (channels[i] != NULL)
472 channel_close_fds(channels[i]);
473}
474
475/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000476 * Stop listening to channels.
477 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000478void
479channel_stop_listening(void)
480{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000481 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000482 Channel *c;
483
484 for (i = 0; i < channels_alloc; i++) {
485 c = channels[i];
486 if (c != NULL) {
487 switch (c->type) {
488 case SSH_CHANNEL_AUTH_SOCKET:
489 case SSH_CHANNEL_PORT_LISTENER:
490 case SSH_CHANNEL_RPORT_LISTENER:
491 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000492 case SSH_CHANNEL_UNIX_LISTENER:
493 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000494 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000495 channel_free(c);
496 break;
497 }
498 }
499 }
500}
501
502/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000503 * Returns true if no channel has too much buffered data, and false if one or
504 * more channel is overfull.
505 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000506int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000507channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000508{
509 u_int i;
510 Channel *c;
511
512 for (i = 0; i < channels_alloc; i++) {
513 c = channels[i];
514 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000515#if 0
516 if (!compat20 &&
517 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100518 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000519 c->self, buffer_len(&c->input));
520 return 0;
521 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000522#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000523 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000524 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000525 c->self, buffer_len(&c->output),
526 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000527 return 0;
528 }
529 }
530 }
531 return 1;
532}
533
534/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000535int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000536channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000537{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000538 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000539 Channel *c;
540
541 for (i = 0; i < channels_alloc; i++) {
542 c = channels[i];
543 if (c == NULL)
544 continue;
545 switch (c->type) {
546 case SSH_CHANNEL_X11_LISTENER:
547 case SSH_CHANNEL_PORT_LISTENER:
548 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100549 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000550 case SSH_CHANNEL_CLOSED:
551 case SSH_CHANNEL_AUTH_SOCKET:
552 case SSH_CHANNEL_DYNAMIC:
553 case SSH_CHANNEL_CONNECTING:
554 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000555 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000556 case SSH_CHANNEL_UNIX_LISTENER:
557 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000558 continue;
559 case SSH_CHANNEL_LARVAL:
560 if (!compat20)
561 fatal("cannot happen: SSH_CHANNEL_LARVAL");
562 continue;
563 case SSH_CHANNEL_OPENING:
564 case SSH_CHANNEL_OPEN:
565 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100566 case SSH_CHANNEL_MUX_CLIENT:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000567 return 1;
568 case SSH_CHANNEL_INPUT_DRAINING:
569 case SSH_CHANNEL_OUTPUT_DRAINING:
570 if (!compat13)
571 fatal("cannot happen: OUT_DRAIN");
572 return 1;
573 default:
574 fatal("channel_still_open: bad channel type %d", c->type);
575 /* NOTREACHED */
576 }
577 }
578 return 0;
579}
580
581/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000582int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000583channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000584{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000585 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000586 Channel *c;
587
588 for (i = 0; i < channels_alloc; i++) {
589 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000590 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000591 continue;
592 switch (c->type) {
593 case SSH_CHANNEL_CLOSED:
594 case SSH_CHANNEL_DYNAMIC:
595 case SSH_CHANNEL_X11_LISTENER:
596 case SSH_CHANNEL_PORT_LISTENER:
597 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100598 case SSH_CHANNEL_MUX_LISTENER:
599 case SSH_CHANNEL_MUX_CLIENT:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000600 case SSH_CHANNEL_OPENING:
601 case SSH_CHANNEL_CONNECTING:
602 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000603 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000604 case SSH_CHANNEL_UNIX_LISTENER:
605 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000606 continue;
607 case SSH_CHANNEL_LARVAL:
608 case SSH_CHANNEL_AUTH_SOCKET:
609 case SSH_CHANNEL_OPEN:
610 case SSH_CHANNEL_X11_OPEN:
611 return i;
612 case SSH_CHANNEL_INPUT_DRAINING:
613 case SSH_CHANNEL_OUTPUT_DRAINING:
614 if (!compat13)
615 fatal("cannot happen: OUT_DRAIN");
616 return i;
617 default:
618 fatal("channel_find_open: bad channel type %d", c->type);
619 /* NOTREACHED */
620 }
621 }
622 return -1;
623}
624
625
626/*
627 * Returns a message describing the currently open forwarded connections,
628 * suitable for sending to the client. The message contains crlf pairs for
629 * newlines.
630 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000631char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000632channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633{
634 Buffer buffer;
635 Channel *c;
636 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000637 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000638
639 buffer_init(&buffer);
640 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
641 buffer_append(&buffer, buf, strlen(buf));
642 for (i = 0; i < channels_alloc; i++) {
643 c = channels[i];
644 if (c == NULL)
645 continue;
646 switch (c->type) {
647 case SSH_CHANNEL_X11_LISTENER:
648 case SSH_CHANNEL_PORT_LISTENER:
649 case SSH_CHANNEL_RPORT_LISTENER:
650 case SSH_CHANNEL_CLOSED:
651 case SSH_CHANNEL_AUTH_SOCKET:
652 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000653 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100654 case SSH_CHANNEL_MUX_CLIENT:
655 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000656 case SSH_CHANNEL_UNIX_LISTENER:
657 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000658 continue;
659 case SSH_CHANNEL_LARVAL:
660 case SSH_CHANNEL_OPENING:
661 case SSH_CHANNEL_CONNECTING:
662 case SSH_CHANNEL_DYNAMIC:
663 case SSH_CHANNEL_OPEN:
664 case SSH_CHANNEL_X11_OPEN:
665 case SSH_CHANNEL_INPUT_DRAINING:
666 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000667 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000668 " #%d %.300s (t%d r%d i%u/%d o%u/%d fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000669 c->self, c->remote_name,
670 c->type, c->remote_id,
671 c->istate, buffer_len(&c->input),
672 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100673 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000674 buffer_append(&buffer, buf, strlen(buf));
675 continue;
676 default:
677 fatal("channel_open_message: bad channel type %d", c->type);
678 /* NOTREACHED */
679 }
680 }
681 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000682 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000683 buffer_free(&buffer);
684 return cp;
685}
686
687void
688channel_send_open(int id)
689{
690 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000691
Ben Lindstrome9c99912001-06-09 00:41:05 +0000692 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000693 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 return;
695 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000696 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000697 packet_start(SSH2_MSG_CHANNEL_OPEN);
698 packet_put_cstring(c->ctype);
699 packet_put_int(c->self);
700 packet_put_int(c->local_window);
701 packet_put_int(c->local_maxpacket);
702 packet_send();
703}
704
705void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000706channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000707{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000708 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000709
Ben Lindstrome9c99912001-06-09 00:41:05 +0000710 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000711 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000712 return;
713 }
Damien Miller0e220db2004-06-15 10:34:08 +1000714 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000715 packet_start(SSH2_MSG_CHANNEL_REQUEST);
716 packet_put_int(c->remote_id);
717 packet_put_cstring(service);
718 packet_put_char(wantconfirm);
719}
Damien Miller4f7becb2006-03-26 14:10:14 +1100720
Ben Lindstrome9c99912001-06-09 00:41:05 +0000721void
Damien Millerb84886b2008-05-19 15:05:07 +1000722channel_register_status_confirm(int id, channel_confirm_cb *cb,
723 channel_confirm_abandon_cb *abandon_cb, void *ctx)
724{
725 struct channel_confirm *cc;
726 Channel *c;
727
728 if ((c = channel_lookup(id)) == NULL)
729 fatal("channel_register_expect: %d: bad id", id);
730
Damien Miller6c81fee2013-11-08 12:19:55 +1100731 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000732 cc->cb = cb;
733 cc->abandon_cb = abandon_cb;
734 cc->ctx = ctx;
735 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
736}
737
738void
Damien Millerd530f5f2010-05-21 14:57:10 +1000739channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000740{
741 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000742
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100744 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000745 return;
746 }
Damien Millerb84886b2008-05-19 15:05:07 +1000747 c->open_confirm = fn;
748 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000749}
Damien Miller4f7becb2006-03-26 14:10:14 +1100750
Ben Lindstrome9c99912001-06-09 00:41:05 +0000751void
Damien Miller39eda6e2005-11-05 14:52:50 +1100752channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753{
Damien Millerd47c62a2005-12-13 19:33:57 +1100754 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000755
Ben Lindstrome9c99912001-06-09 00:41:05 +0000756 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000757 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000758 return;
759 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000760 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100761 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000762}
Damien Miller4f7becb2006-03-26 14:10:14 +1100763
Ben Lindstrome9c99912001-06-09 00:41:05 +0000764void
765channel_cancel_cleanup(int id)
766{
Damien Millerd47c62a2005-12-13 19:33:57 +1100767 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000768
Ben Lindstrome9c99912001-06-09 00:41:05 +0000769 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000770 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000771 return;
772 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000773 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100774 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000775}
Damien Miller4f7becb2006-03-26 14:10:14 +1100776
Ben Lindstrome9c99912001-06-09 00:41:05 +0000777void
Damien Miller077b2382005-12-31 16:22:32 +1100778channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000779 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000780{
781 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000782
Ben Lindstrome9c99912001-06-09 00:41:05 +0000783 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000784 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000785 return;
786 }
Damien Miller077b2382005-12-31 16:22:32 +1100787 c->input_filter = ifn;
788 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000789 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000790 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000791}
792
793void
794channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000795 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796{
797 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000798
Ben Lindstrome9c99912001-06-09 00:41:05 +0000799 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
800 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000801 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000802 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100803 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000804 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
805 packet_put_int(c->remote_id);
806 packet_put_int(c->local_window);
807 packet_send();
808}
809
Damien Miller5428f641999-11-25 11:54:57 +1100810/*
Damien Millerb38eff82000-04-01 11:09:21 +1000811 * 'channel_pre*' are called just before select() to add any bits relevant to
812 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100813 */
Damien Millerb38eff82000-04-01 11:09:21 +1000814/*
815 * 'channel_post*': perform any appropriate operations for channels which
816 * have events pending.
817 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100818typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000819chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
820chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
821
Damien Miller8473dd82006-07-24 14:08:32 +1000822/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000823static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100824channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000825{
826 FD_SET(c->sock, readset);
827}
828
Damien Miller8473dd82006-07-24 14:08:32 +1000829/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100831channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000832{
833 debug3("channel %d: waiting for connection", c->self);
834 FD_SET(c->sock, writeset);
835}
836
Ben Lindstrombba81212001-06-25 05:01:22 +0000837static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100838channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000839{
840 if (buffer_len(&c->input) < packet_get_maxsize())
841 FD_SET(c->sock, readset);
842 if (buffer_len(&c->output) > 0)
843 FD_SET(c->sock, writeset);
844}
845
Ben Lindstrombba81212001-06-25 05:01:22 +0000846static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100847channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000848{
Damien Millerde6987c2002-01-22 23:20:40 +1100849 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000850
Damien Miller33b13562000-04-04 14:38:59 +1000851 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100852 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000853 buffer_len(&c->input) < limit &&
854 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000855 FD_SET(c->rfd, readset);
856 if (c->ostate == CHAN_OUTPUT_OPEN ||
857 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
858 if (buffer_len(&c->output) > 0) {
859 FD_SET(c->wfd, writeset);
860 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000861 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000862 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
863 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000864 else
865 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000866 }
867 }
868 /** XXX check close conditions, too */
Damien Millerd654dd22008-05-19 16:05:41 +1000869 if (compat20 && c->efd != -1 &&
870 !(c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000871 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
872 buffer_len(&c->extended) > 0)
873 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000874 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
875 (c->extended_usage == CHAN_EXTENDED_READ ||
876 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000877 buffer_len(&c->extended) < c->remote_window)
878 FD_SET(c->efd, readset);
879 }
Damien Miller0e220db2004-06-15 10:34:08 +1000880 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000881}
882
Damien Miller8473dd82006-07-24 14:08:32 +1000883/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000884static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100885channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000886{
887 if (buffer_len(&c->input) == 0) {
888 packet_start(SSH_MSG_CHANNEL_CLOSE);
889 packet_put_int(c->remote_id);
890 packet_send();
891 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000892 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000893 }
894}
895
Damien Miller8473dd82006-07-24 14:08:32 +1000896/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000897static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100898channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000899{
900 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000901 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000902 else
Damien Millerb38eff82000-04-01 11:09:21 +1000903 FD_SET(c->sock, writeset);
904}
905
906/*
907 * This is a special state for X11 authentication spoofing. An opened X11
908 * connection (when authentication spoofing is being done) remains in this
909 * state until the first packet has been completely read. The authentication
910 * data in that packet is then substituted by the real data if it matches the
911 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000912 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000913 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000914 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000915static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000916x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000917{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000918 u_char *ucp;
919 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000920
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000921 /* Is this being called after the refusal deadline? */
922 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
923 verbose("Rejected X11 connection after ForwardX11Timeout "
924 "expired");
925 return -1;
926 }
927
Damien Millerb38eff82000-04-01 11:09:21 +1000928 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000929 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000930 return 0;
931
932 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100933 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000934 if (ucp[0] == 0x42) { /* Byte order MSB first. */
935 proto_len = 256 * ucp[6] + ucp[7];
936 data_len = 256 * ucp[8] + ucp[9];
937 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
938 proto_len = ucp[6] + 256 * ucp[7];
939 data_len = ucp[8] + 256 * ucp[9];
940 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000941 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100942 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000943 return -1;
944 }
945
946 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000947 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000948 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
949 return 0;
950
951 /* Check if authentication protocol matches. */
952 if (proto_len != strlen(x11_saved_proto) ||
953 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000954 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000955 return -1;
956 }
957 /* Check if authentication data matches our fake data. */
958 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000959 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000960 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000961 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000962 return -1;
963 }
964 /* Check fake data length */
965 if (x11_fake_data_len != x11_saved_data_len) {
966 error("X11 fake_data_len %d != saved_data_len %d",
967 x11_fake_data_len, x11_saved_data_len);
968 return -1;
969 }
970 /*
971 * Received authentication protocol and data match
972 * our fake data. Substitute the fake data with real
973 * data.
974 */
975 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
976 x11_saved_data, x11_saved_data_len);
977 return 1;
978}
979
Ben Lindstrombba81212001-06-25 05:01:22 +0000980static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100981channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000982{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000983 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000984
Damien Millerb38eff82000-04-01 11:09:21 +1000985 if (ret == 1) {
986 /* Start normal processing for the channel. */
987 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000988 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000989 } else if (ret == -1) {
990 /*
991 * We have received an X11 connection that has bad
992 * authentication information.
993 */
Damien Miller996acd22003-04-09 20:59:48 +1000994 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000995 buffer_clear(&c->input);
996 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000997 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000998 c->sock = -1;
999 c->type = SSH_CHANNEL_CLOSED;
1000 packet_start(SSH_MSG_CHANNEL_CLOSE);
1001 packet_put_int(c->remote_id);
1002 packet_send();
1003 }
1004}
1005
Ben Lindstrombba81212001-06-25 05:01:22 +00001006static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001007channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001008{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001009 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001010
1011 /* c->force_drain = 1; */
1012
Damien Millerb38eff82000-04-01 11:09:21 +10001013 if (ret == 1) {
1014 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +11001015 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001016 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001017 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +10001018 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +11001019 chan_read_failed(c);
1020 buffer_clear(&c->input);
1021 chan_ibuf_empty(c);
1022 buffer_clear(&c->output);
1023 /* for proto v1, the peer will send an IEOF */
1024 if (compat20)
1025 chan_write_failed(c);
1026 else
1027 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +10001028 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001029 }
1030}
1031
Damien Millere1537f92010-01-26 13:26:22 +11001032static void
1033channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1034{
Damien Millerd530f5f2010-05-21 14:57:10 +10001035 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001036 buffer_check_alloc(&c->input, CHAN_RBUF))
1037 FD_SET(c->rfd, readset);
1038 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1039 /* clear buffer immediately (discard any partial packet) */
1040 buffer_clear(&c->input);
1041 chan_ibuf_empty(c);
1042 /* Start output drain. XXX just kill chan? */
1043 chan_rcvd_oclose(c);
1044 }
1045 if (c->ostate == CHAN_OUTPUT_OPEN ||
1046 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1047 if (buffer_len(&c->output) > 0)
1048 FD_SET(c->wfd, writeset);
1049 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1050 chan_obuf_empty(c);
1051 }
1052}
1053
Ben Lindstromb3921512001-04-11 15:57:50 +00001054/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001055/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001056static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001057channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001058{
Damien Millera10f5612002-09-12 09:49:15 +10001059 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001060 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001061 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001062 struct {
1063 u_int8_t version;
1064 u_int8_t command;
1065 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001066 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001067 } s4_req, s4_rsp;
1068
Ben Lindstromb3921512001-04-11 15:57:50 +00001069 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001070
1071 have = buffer_len(&c->input);
1072 len = sizeof(s4_req);
1073 if (have < len)
1074 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001075 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001076
1077 need = 1;
1078 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1079 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1080 debug2("channel %d: socks4a request", c->self);
1081 /* ... and needs an extra string (the hostname) */
1082 need = 2;
1083 }
1084 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001085 for (found = 0, i = len; i < have; i++) {
1086 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001087 found++;
1088 if (found == need)
1089 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001090 }
1091 if (i > 1024) {
1092 /* the peer is probably sending garbage */
1093 debug("channel %d: decode socks4: too long",
1094 c->self);
1095 return -1;
1096 }
1097 }
Damien Miller1781f532009-01-28 16:24:41 +11001098 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001099 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001100 buffer_get(&c->input, (char *)&s4_req.version, 1);
1101 buffer_get(&c->input, (char *)&s4_req.command, 1);
1102 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001103 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001104 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001105 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001106 if (memchr(p, '\0', have) == NULL)
1107 fatal("channel %d: decode socks4: user not nul terminated",
1108 c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001109 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001110 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001111 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001112 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001113 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001114 c->self, len, have);
1115 strlcpy(username, p, sizeof(username));
1116 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001117
Darren Tuckera627d422013-06-02 07:31:17 +10001118 free(c->path);
1119 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001120 if (need == 1) { /* SOCKS4: one string */
1121 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001122 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001123 } else { /* SOCKS4A: two strings */
1124 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001125 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001126 len = strlen(p);
1127 debug2("channel %d: decode socks4a: host %s/%d",
1128 c->self, p, len);
1129 len++; /* trailing '\0' */
1130 if (len > have)
1131 fatal("channel %d: decode socks4a: len %d > have %d",
1132 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001133 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001134 error("channel %d: hostname \"%.100s\" too long",
1135 c->self, p);
1136 return -1;
1137 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001138 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001139 buffer_consume(&c->input, len);
1140 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001141 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001142
Damien Millerfbdeece2003-09-02 22:52:31 +10001143 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001144 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001145
Ben Lindstromb3921512001-04-11 15:57:50 +00001146 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001147 debug("channel %d: cannot handle: %s cn %d",
1148 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001149 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001150 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001151 s4_rsp.version = 0; /* vn: 0 for reply */
1152 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001153 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001154 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001155 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001156 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001157}
1158
Darren Tucker46471c92003-07-03 13:55:19 +10001159/* try to decode a socks5 header */
1160#define SSH_SOCKS5_AUTHDONE 0x1000
1161#define SSH_SOCKS5_NOAUTH 0x00
1162#define SSH_SOCKS5_IPV4 0x01
1163#define SSH_SOCKS5_DOMAIN 0x03
1164#define SSH_SOCKS5_IPV6 0x04
1165#define SSH_SOCKS5_CONNECT 0x01
1166#define SSH_SOCKS5_SUCCESS 0x00
1167
Damien Miller8473dd82006-07-24 14:08:32 +10001168/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001169static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001170channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001171{
1172 struct {
1173 u_int8_t version;
1174 u_int8_t command;
1175 u_int8_t reserved;
1176 u_int8_t atyp;
1177 } s5_req, s5_rsp;
1178 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001179 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1180 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001181 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001182
1183 debug2("channel %d: decode socks5", c->self);
1184 p = buffer_ptr(&c->input);
1185 if (p[0] != 0x05)
1186 return -1;
1187 have = buffer_len(&c->input);
1188 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1189 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001190 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001191 return 0;
1192 nmethods = p[1];
1193 if (have < nmethods + 2)
1194 return 0;
1195 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001196 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001197 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001198 found = 1;
1199 break;
1200 }
1201 }
1202 if (!found) {
1203 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1204 c->self);
1205 return -1;
1206 }
1207 buffer_consume(&c->input, nmethods + 2);
1208 buffer_put_char(&c->output, 0x05); /* version */
1209 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1210 FD_SET(c->sock, writeset);
1211 c->flags |= SSH_SOCKS5_AUTHDONE;
1212 debug2("channel %d: socks5 auth done", c->self);
1213 return 0; /* need more */
1214 }
1215 debug2("channel %d: socks5 post auth", c->self);
1216 if (have < sizeof(s5_req)+1)
1217 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001218 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001219 if (s5_req.version != 0x05 ||
1220 s5_req.command != SSH_SOCKS5_CONNECT ||
1221 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001222 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001223 return -1;
1224 }
Darren Tucker47eede72005-03-14 23:08:12 +11001225 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001226 case SSH_SOCKS5_IPV4:
1227 addrlen = 4;
1228 af = AF_INET;
1229 break;
1230 case SSH_SOCKS5_DOMAIN:
1231 addrlen = p[sizeof(s5_req)];
1232 af = -1;
1233 break;
1234 case SSH_SOCKS5_IPV6:
1235 addrlen = 16;
1236 af = AF_INET6;
1237 break;
1238 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001239 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001240 return -1;
1241 }
Damien Miller0f077072006-07-10 22:21:02 +10001242 need = sizeof(s5_req) + addrlen + 2;
1243 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1244 need++;
1245 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001246 return 0;
1247 buffer_consume(&c->input, sizeof(s5_req));
1248 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1249 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001250 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001251 buffer_get(&c->input, (char *)&dest_port, 2);
1252 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001253 free(c->path);
1254 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001255 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001256 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001257 error("channel %d: dynamic request: socks5 hostname "
1258 "\"%.100s\" too long", c->self, dest_addr);
1259 return -1;
1260 }
1261 c->path = xstrdup(dest_addr);
1262 } else {
1263 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1264 return -1;
1265 c->path = xstrdup(ntop);
1266 }
Darren Tucker46471c92003-07-03 13:55:19 +10001267 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001268
Damien Millerfbdeece2003-09-02 22:52:31 +10001269 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001270 c->self, c->path, c->host_port, s5_req.command);
1271
1272 s5_rsp.version = 0x05;
1273 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1274 s5_rsp.reserved = 0; /* ignored */
1275 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001276 dest_port = 0; /* ignored */
1277
Damien Millera0fdce92006-03-26 14:28:50 +11001278 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001279 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001280 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001281 return 1;
1282}
1283
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001284Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001285channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1286 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001287{
1288 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001289
1290 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1291 port_to_connect);
1292
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001293 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1294 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1295 0, "stdio-forward", /*nonblock*/0);
1296
1297 c->path = xstrdup(host_to_connect);
1298 c->host_port = port_to_connect;
1299 c->listening_port = 0;
1300 c->force_drain = 1;
1301
1302 channel_register_fds(c, in, out, -1, 0, 1, 0);
1303 port_open_helper(c, "direct-tcpip");
1304
1305 return c;
1306}
1307
Ben Lindstromb3921512001-04-11 15:57:50 +00001308/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001309static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001310channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001311{
1312 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001313 u_int have;
1314 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001315
1316 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001317 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001318 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001319 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001320 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001321 /* need more */
1322 FD_SET(c->sock, readset);
1323 return;
1324 }
1325 /* try to guess the protocol */
1326 p = buffer_ptr(&c->input);
1327 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001328 case 0x04:
1329 ret = channel_decode_socks4(c, readset, writeset);
1330 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001331 case 0x05:
1332 ret = channel_decode_socks5(c, readset, writeset);
1333 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001334 default:
1335 ret = -1;
1336 break;
1337 }
1338 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001339 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001340 } else if (ret == 0) {
1341 debug2("channel %d: pre_dynamic: need more", c->self);
1342 /* need more */
1343 FD_SET(c->sock, readset);
1344 } else {
1345 /* switch to the next state */
1346 c->type = SSH_CHANNEL_OPENING;
1347 port_open_helper(c, "direct-tcpip");
1348 }
1349}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001350
Damien Millerb38eff82000-04-01 11:09:21 +10001351/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001352/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001353static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001354channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001355{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001356 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001357 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001358 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001359 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001360 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001361 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001362
1363 if (FD_ISSET(c->sock, readset)) {
1364 debug("X11 connection requested.");
1365 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001366 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001367 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001368 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001369 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001370 channel_close_fd(&c->sock);
1371 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001372 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001373 }
Damien Millerb38eff82000-04-01 11:09:21 +10001374 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001375 if (errno != EINTR && errno != EWOULDBLOCK &&
1376 errno != ECONNABORTED)
1377 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001378 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001379 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001380 return;
1381 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001382 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001383 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001384 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001385 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001386 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001387
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001388 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001389 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001390 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001391 if (compat20) {
1392 packet_start(SSH2_MSG_CHANNEL_OPEN);
1393 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001394 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001395 packet_put_int(nc->local_window_max);
1396 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001397 /* originator ipaddr and port */
1398 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001399 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001400 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001401 } else {
1402 packet_put_int(remote_port);
1403 }
Damien Millerbd483e72000-04-30 10:00:53 +10001404 packet_send();
1405 } else {
1406 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001407 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001408 if (packet_get_protocol_flags() &
1409 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001410 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001411 packet_send();
1412 }
Darren Tuckera627d422013-06-02 07:31:17 +10001413 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001414 }
1415}
1416
Ben Lindstrombba81212001-06-25 05:01:22 +00001417static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001418port_open_helper(Channel *c, char *rtype)
1419{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001420 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001421 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001422 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001423 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001424 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001425
Damien Millerd6369432010-02-02 17:02:07 +11001426 if (remote_port == -1) {
1427 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001428 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001429 remote_ipaddr = xstrdup("127.0.0.1");
1430 remote_port = 65535;
1431 }
1432
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001433 snprintf(buf, sizeof buf,
1434 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001435 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001436 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001437 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001438
Darren Tuckera627d422013-06-02 07:31:17 +10001439 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001440 c->remote_name = xstrdup(buf);
1441
1442 if (compat20) {
1443 packet_start(SSH2_MSG_CHANNEL_OPEN);
1444 packet_put_cstring(rtype);
1445 packet_put_int(c->self);
1446 packet_put_int(c->local_window_max);
1447 packet_put_int(c->local_maxpacket);
Damien Miller7acefbb2014-07-18 14:11:24 +10001448 if (strcmp(rtype, "direct-tcpip") == 0) {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001449 /* target host, port */
1450 packet_put_cstring(c->path);
1451 packet_put_int(c->host_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001452 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1453 /* target path */
1454 packet_put_cstring(c->path);
1455 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1456 /* listen path */
1457 packet_put_cstring(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001458 } else {
1459 /* listen address, port */
1460 packet_put_cstring(c->path);
Damien Miller4def1842013-12-29 17:45:26 +11001461 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001462 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001463 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1464 /* reserved for future owner/mode info */
1465 packet_put_cstring("");
1466 } else {
1467 /* originator host and port */
1468 packet_put_cstring(remote_ipaddr);
1469 packet_put_int((u_int)remote_port);
1470 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001471 packet_send();
1472 } else {
1473 packet_start(SSH_MSG_PORT_OPEN);
1474 packet_put_int(c->self);
1475 packet_put_cstring(c->path);
1476 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001477 if (packet_get_protocol_flags() &
1478 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001479 packet_put_cstring(c->remote_name);
1480 packet_send();
1481 }
Darren Tuckera627d422013-06-02 07:31:17 +10001482 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001483 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001484}
1485
Damien Miller5e7fd072005-11-05 14:53:39 +11001486static void
1487channel_set_reuseaddr(int fd)
1488{
1489 int on = 1;
1490
1491 /*
1492 * Set socket options.
1493 * Allow local port reuse in TIME_WAIT.
1494 */
1495 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1496 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1497}
1498
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001499void
1500channel_set_x11_refuse_time(u_int refuse_time)
1501{
1502 x11_refuse_time = refuse_time;
1503}
1504
Damien Millerb38eff82000-04-01 11:09:21 +10001505/*
1506 * This socket is listening for connections to a forwarded TCP/IP port.
1507 */
Damien Miller8473dd82006-07-24 14:08:32 +10001508/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001509static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001510channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001511{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001512 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001513 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001514 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001515 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001516 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001517
Damien Millerb38eff82000-04-01 11:09:21 +10001518 if (FD_ISSET(c->sock, readset)) {
1519 debug("Connection to port %d forwarding "
1520 "to %.100s port %d requested.",
1521 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001522
Damien Miller4623a752001-10-10 15:03:58 +10001523 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1524 nextstate = SSH_CHANNEL_OPENING;
1525 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001526 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1527 nextstate = SSH_CHANNEL_OPENING;
1528 rtype = "forwarded-streamlocal@openssh.com";
1529 } else if (c->host_port == PORT_STREAMLOCAL) {
1530 nextstate = SSH_CHANNEL_OPENING;
1531 rtype = "direct-streamlocal@openssh.com";
1532 } else if (c->host_port == 0) {
1533 nextstate = SSH_CHANNEL_DYNAMIC;
1534 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001535 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001536 nextstate = SSH_CHANNEL_OPENING;
1537 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001538 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001539
Damien Millerb38eff82000-04-01 11:09:21 +10001540 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001541 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001542 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001543 if (errno != EINTR && errno != EWOULDBLOCK &&
1544 errno != ECONNABORTED)
1545 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001546 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001547 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001548 return;
1549 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001550 if (c->host_port != PORT_STREAMLOCAL)
1551 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001552 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1553 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001554 nc->listening_port = c->listening_port;
1555 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001556 if (c->path != NULL)
1557 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001558
Darren Tucker876045b2010-01-08 17:08:00 +11001559 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001560 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001561 }
1562}
1563
1564/*
1565 * This is the authentication agent socket listening for connections from
1566 * clients.
1567 */
Damien Miller8473dd82006-07-24 14:08:32 +10001568/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001569static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001570channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001571{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001572 Channel *nc;
1573 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001574 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001575 socklen_t addrlen;
1576
1577 if (FD_ISSET(c->sock, readset)) {
1578 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001579 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001580 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001581 error("accept from auth socket: %.100s",
1582 strerror(errno));
1583 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001584 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001585 return;
1586 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001587 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001588 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1589 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001590 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001591 if (compat20) {
1592 packet_start(SSH2_MSG_CHANNEL_OPEN);
1593 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001594 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001595 packet_put_int(c->local_window_max);
1596 packet_put_int(c->local_maxpacket);
1597 } else {
1598 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001599 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001600 }
Damien Millerb38eff82000-04-01 11:09:21 +10001601 packet_send();
1602 }
1603}
1604
Damien Miller8473dd82006-07-24 14:08:32 +10001605/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001606static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001607channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001608{
Damien Millerbd740252008-05-19 15:37:09 +10001609 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001610 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001611
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001612 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001613 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001614 err = errno;
1615 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001616 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001617 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001618 debug("channel %d: connected to %s port %d",
1619 c->self, c->connect_ctx.host, c->connect_ctx.port);
1620 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001621 c->type = SSH_CHANNEL_OPEN;
1622 if (compat20) {
1623 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1624 packet_put_int(c->remote_id);
1625 packet_put_int(c->self);
1626 packet_put_int(c->local_window);
1627 packet_put_int(c->local_maxpacket);
1628 } else {
1629 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1630 packet_put_int(c->remote_id);
1631 packet_put_int(c->self);
1632 }
1633 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001634 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001635 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001636 /* Try next address, if any */
1637 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1638 close(c->sock);
1639 c->sock = c->rfd = c->wfd = sock;
1640 channel_max_fd = channel_find_maxfd();
1641 return;
1642 }
1643 /* Exhausted all addresses */
1644 error("connect_to %.100s port %d: failed.",
1645 c->connect_ctx.host, c->connect_ctx.port);
1646 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001647 if (compat20) {
1648 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1649 packet_put_int(c->remote_id);
1650 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1651 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1652 packet_put_cstring(strerror(err));
1653 packet_put_cstring("");
1654 }
1655 } else {
1656 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1657 packet_put_int(c->remote_id);
1658 }
1659 chan_mark_dead(c);
1660 }
1661 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001662 }
1663}
1664
Damien Miller8473dd82006-07-24 14:08:32 +10001665/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001666static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001667channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001668{
Darren Tucker11327cc2005-03-14 23:22:25 +11001669 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001670 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001671
Damien Miller835284b2007-06-11 13:03:16 +10001672 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1673 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001674 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001675 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001676 if (len < 0 && (errno == EINTR ||
1677 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001678 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001679#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001680 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001681#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001682 if ((!c->isatty && len <= 0) ||
1683 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001684#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001685 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001686 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001687 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001688 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001689 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001690 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001691 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001692 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001693 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001694 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001695 } else {
1696 chan_read_failed(c);
1697 }
1698 return -1;
1699 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001700 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001701 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001702 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001703 chan_read_failed(c);
1704 }
Damien Millerd27b9472005-12-13 19:29:02 +11001705 } else if (c->datagram) {
1706 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001707 } else {
1708 buffer_append(&c->input, buf, len);
1709 }
Damien Millerb38eff82000-04-01 11:09:21 +10001710 }
1711 return 1;
1712}
Damien Miller4f7becb2006-03-26 14:10:14 +11001713
Damien Miller8473dd82006-07-24 14:08:32 +10001714/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001715static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001716channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001717{
Ben Lindstrome229b252001-03-05 06:28:06 +00001718 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001719 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001720 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001721 int len;
1722
1723 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001724 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001725 FD_ISSET(c->wfd, writeset) &&
1726 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001727 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001728 if (c->output_filter != NULL) {
1729 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1730 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001731 if (c->type != SSH_CHANNEL_OPEN)
1732 chan_mark_dead(c);
1733 else
1734 chan_write_failed(c);
1735 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001736 }
1737 } else if (c->datagram) {
1738 buf = data = buffer_get_string(&c->output, &dlen);
1739 } else {
1740 buf = data = buffer_ptr(&c->output);
1741 dlen = buffer_len(&c->output);
1742 }
1743
Damien Millerd27b9472005-12-13 19:29:02 +11001744 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001745 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001746 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001747 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001748 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1749 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001750 return 1;
1751 if (len <= 0) {
1752 if (c->type != SSH_CHANNEL_OPEN)
1753 chan_mark_dead(c);
1754 else
1755 chan_write_failed(c);
1756 return -1;
1757 }
Damien Miller7d457182010-08-05 23:09:48 +10001758 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001759 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001760#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001761 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001762 if (compat20 && c->wfd_isatty)
1763 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001764#endif
Damien Miller077b2382005-12-31 16:22:32 +11001765
1766 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001767 if (len < 0 &&
1768 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001769 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001770 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001771 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001772 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001773 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001774 return -1;
1775 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001776 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001777 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001778 c->type = SSH_CHANNEL_INPUT_DRAINING;
1779 } else {
1780 chan_write_failed(c);
1781 }
1782 return -1;
1783 }
Darren Tucker3980b632009-08-28 11:02:37 +10001784#ifndef BROKEN_TCGETATTR_ICANON
Damien Miller077b2382005-12-31 16:22:32 +11001785 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001786 if (tcgetattr(c->wfd, &tio) == 0 &&
1787 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1788 /*
1789 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001790 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001791 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001792 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001793 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001794 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001795 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001796 }
1797 }
Darren Tucker3980b632009-08-28 11:02:37 +10001798#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001799 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001800 }
Damien Miller7d457182010-08-05 23:09:48 +10001801 out:
1802 if (compat20 && olen > 0)
1803 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001804 return 1;
1805}
Damien Miller4f7becb2006-03-26 14:10:14 +11001806
Ben Lindstrombba81212001-06-25 05:01:22 +00001807static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001808channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001809{
Darren Tucker11327cc2005-03-14 23:22:25 +11001810 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001811 int len;
1812
Damien Miller1383bd82000-04-06 12:32:37 +10001813/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001814 if (c->efd != -1) {
1815 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1816 FD_ISSET(c->efd, writeset) &&
1817 buffer_len(&c->extended) > 0) {
1818 len = write(c->efd, buffer_ptr(&c->extended),
1819 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001820 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001821 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001822 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1823 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001824 return 1;
1825 if (len <= 0) {
1826 debug2("channel %d: closing write-efd %d",
1827 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001828 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001829 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001830 buffer_consume(&c->extended, len);
1831 c->local_consumed += len;
1832 }
Damien Miller8853ca52010-06-26 10:00:14 +10001833 } else if (c->efd != -1 &&
1834 (c->extended_usage == CHAN_EXTENDED_READ ||
1835 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001836 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001837 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001838 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001839 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001840 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1841 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001842 return 1;
1843 if (len <= 0) {
1844 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001845 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001846 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001847 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001848 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1849 debug3("channel %d: discard efd",
1850 c->self);
1851 } else
1852 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001853 }
Damien Miller33b13562000-04-04 14:38:59 +10001854 }
1855 }
1856 return 1;
1857}
Damien Miller4f7becb2006-03-26 14:10:14 +11001858
Damien Miller0e220db2004-06-15 10:34:08 +10001859static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001860channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001861{
Ben Lindstromb3921512001-04-11 15:57:50 +00001862 if (c->type == SSH_CHANNEL_OPEN &&
1863 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001864 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001865 c->local_maxpacket*3) ||
1866 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001867 c->local_consumed > 0) {
1868 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1869 packet_put_int(c->remote_id);
1870 packet_put_int(c->local_consumed);
1871 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001872 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001873 c->self, c->local_window,
1874 c->local_consumed);
1875 c->local_window += c->local_consumed;
1876 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001877 }
1878 return 1;
1879}
1880
Ben Lindstrombba81212001-06-25 05:01:22 +00001881static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001882channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001883{
1884 channel_handle_rfd(c, readset, writeset);
1885 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001886 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001887 return;
Damien Miller33b13562000-04-04 14:38:59 +10001888 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001889 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001890}
1891
Damien Millere1537f92010-01-26 13:26:22 +11001892static u_int
1893read_mux(Channel *c, u_int need)
1894{
1895 char buf[CHAN_RBUF];
1896 int len;
1897 u_int rlen;
1898
1899 if (buffer_len(&c->input) < need) {
1900 rlen = need - buffer_len(&c->input);
1901 len = read(c->rfd, buf, MIN(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001902 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1903 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001904 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001905 debug2("channel %d: ctl read<=0 rfd %d len %d",
1906 c->self, c->rfd, len);
1907 chan_read_failed(c);
1908 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001909 } else
1910 buffer_append(&c->input, buf, len);
1911 }
1912 return buffer_len(&c->input);
1913}
1914
1915static void
1916channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1917{
1918 u_int need;
1919 ssize_t len;
1920
1921 if (!compat20)
1922 fatal("%s: entered with !compat20", __func__);
1923
Damien Millerd530f5f2010-05-21 14:57:10 +10001924 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001925 (c->istate == CHAN_INPUT_OPEN ||
1926 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1927 /*
1928 * Don't not read past the precise end of packets to
1929 * avoid disrupting fd passing.
1930 */
1931 if (read_mux(c, 4) < 4) /* read header */
1932 return;
1933 need = get_u32(buffer_ptr(&c->input));
1934#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1935 if (need > CHANNEL_MUX_MAX_PACKET) {
1936 debug2("channel %d: packet too big %u > %u",
1937 c->self, CHANNEL_MUX_MAX_PACKET, need);
1938 chan_rcvd_oclose(c);
1939 return;
1940 }
1941 if (read_mux(c, need + 4) < need + 4) /* read body */
1942 return;
1943 if (c->mux_rcb(c) != 0) {
1944 debug("channel %d: mux_rcb failed", c->self);
1945 chan_mark_dead(c);
1946 return;
1947 }
1948 }
1949
1950 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1951 buffer_len(&c->output) > 0) {
1952 len = write(c->wfd, buffer_ptr(&c->output),
1953 buffer_len(&c->output));
1954 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1955 return;
1956 if (len <= 0) {
1957 chan_mark_dead(c);
1958 return;
1959 }
1960 buffer_consume(&c->output, len);
1961 }
1962}
1963
1964static void
1965channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1966{
1967 Channel *nc;
1968 struct sockaddr_storage addr;
1969 socklen_t addrlen;
1970 int newsock;
1971 uid_t euid;
1972 gid_t egid;
1973
1974 if (!FD_ISSET(c->sock, readset))
1975 return;
1976
1977 debug("multiplexing control connection");
1978
1979 /*
1980 * Accept connection on control socket
1981 */
1982 memset(&addr, 0, sizeof(addr));
1983 addrlen = sizeof(addr);
1984 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1985 &addrlen)) == -1) {
1986 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001987 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001988 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001989 return;
1990 }
1991
1992 if (getpeereid(newsock, &euid, &egid) < 0) {
1993 error("%s getpeereid failed: %s", __func__,
1994 strerror(errno));
1995 close(newsock);
1996 return;
1997 }
1998 if ((euid != 0) && (getuid() != euid)) {
1999 error("multiplex uid mismatch: peer euid %u != uid %u",
2000 (u_int)euid, (u_int)getuid());
2001 close(newsock);
2002 return;
2003 }
2004 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
2005 newsock, newsock, -1, c->local_window_max,
2006 c->local_maxpacket, 0, "mux-control", 1);
2007 nc->mux_rcb = c->mux_rcb;
2008 debug3("%s: new mux channel %d fd %d", __func__,
2009 nc->self, nc->sock);
2010 /* establish state */
2011 nc->mux_rcb(nc);
2012 /* mux state transitions must not elicit protocol messages */
2013 nc->flags |= CHAN_LOCAL;
2014}
2015
Damien Miller8473dd82006-07-24 14:08:32 +10002016/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00002017static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002018channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002019{
2020 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002021
Damien Millerb38eff82000-04-01 11:09:21 +10002022 /* Send buffered output data to the socket. */
2023 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
2024 len = write(c->sock, buffer_ptr(&c->output),
2025 buffer_len(&c->output));
2026 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11002027 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10002028 else
2029 buffer_consume(&c->output, len);
2030 }
2031}
2032
Ben Lindstrombba81212001-06-25 05:01:22 +00002033static void
Damien Miller33b13562000-04-04 14:38:59 +10002034channel_handler_init_20(void)
2035{
Damien Millerde6987c2002-01-22 23:20:40 +11002036 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002037 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10002038 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002039 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002040 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2041 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002042 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002043 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002044 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002045 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11002046 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2047 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002048
Damien Millerde6987c2002-01-22 23:20:40 +11002049 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10002050 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002051 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002052 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2053 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002054 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002055 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002056 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002057 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11002058 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2059 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002060}
2061
Ben Lindstrombba81212001-06-25 05:01:22 +00002062static void
Damien Millerb38eff82000-04-01 11:09:21 +10002063channel_handler_init_13(void)
2064{
2065 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
2066 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
2067 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2068 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2069 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2070 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
2071 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002072 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002073 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002074
Damien Millerde6987c2002-01-22 23:20:40 +11002075 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002076 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2077 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2078 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2079 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002080 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002081 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002082}
2083
Ben Lindstrombba81212001-06-25 05:01:22 +00002084static void
Damien Millerb38eff82000-04-01 11:09:21 +10002085channel_handler_init_15(void)
2086{
Damien Millerde6987c2002-01-22 23:20:40 +11002087 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002088 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002089 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2090 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2091 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002092 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002093 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002094
2095 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2096 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2097 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11002098 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002099 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002100 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002101}
2102
Ben Lindstrombba81212001-06-25 05:01:22 +00002103static void
Damien Millerb38eff82000-04-01 11:09:21 +10002104channel_handler_init(void)
2105{
2106 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002107
Damien Miller9f0f5c62001-12-21 14:45:46 +11002108 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10002109 channel_pre[i] = NULL;
2110 channel_post[i] = NULL;
2111 }
Damien Miller33b13562000-04-04 14:38:59 +10002112 if (compat20)
2113 channel_handler_init_20();
2114 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002115 channel_handler_init_13();
2116 else
2117 channel_handler_init_15();
2118}
2119
Damien Miller3ec27592001-10-12 11:35:04 +10002120/* gc dead channels */
2121static void
2122channel_garbage_collect(Channel *c)
2123{
2124 if (c == NULL)
2125 return;
2126 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11002127 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002128 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002129 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002130 c->detach_user(c->self, NULL);
2131 /* if we still have a callback */
2132 if (c->detach_user != NULL)
2133 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002134 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002135 }
2136 if (!chan_is_dead(c, 1))
2137 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002138 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002139 channel_free(c);
2140}
2141
Ben Lindstrombba81212001-06-25 05:01:22 +00002142static void
Damien Millera6508752012-04-22 11:21:10 +10002143channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2144 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002145{
2146 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002147 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002148 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002149 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002150
2151 if (!did_init) {
2152 channel_handler_init();
2153 did_init = 1;
2154 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002155 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002156 if (unpause_secs != NULL)
2157 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002158 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002159 c = channels[i];
2160 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002161 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002162 if (c->delayed) {
2163 if (ftab == channel_pre)
2164 c->delayed = 0;
2165 else
2166 continue;
2167 }
Damien Millera6508752012-04-22 11:21:10 +10002168 if (ftab[c->type] != NULL) {
2169 /*
2170 * Run handlers that are not paused.
2171 */
2172 if (c->notbefore <= now)
2173 (*ftab[c->type])(c, readset, writeset);
2174 else if (unpause_secs != NULL) {
2175 /*
2176 * Collect the time that the earliest
2177 * channel comes off pause.
2178 */
2179 debug3("%s: chan %d: skip for %d more seconds",
2180 __func__, c->self,
2181 (int)(c->notbefore - now));
2182 if (*unpause_secs == 0 ||
2183 (c->notbefore - now) < *unpause_secs)
2184 *unpause_secs = c->notbefore - now;
2185 }
2186 }
Damien Miller3ec27592001-10-12 11:35:04 +10002187 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002188 }
Damien Millera6508752012-04-22 11:21:10 +10002189 if (unpause_secs != NULL && *unpause_secs != 0)
2190 debug3("%s: first channel unpauses in %d seconds",
2191 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002192}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002193
Ben Lindstrome9c99912001-06-09 00:41:05 +00002194/*
2195 * Allocate/update select bitmasks and add any bits relevant to channels in
2196 * select bitmasks.
2197 */
Damien Miller4af51302000-04-16 11:18:38 +10002198void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002199channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002200 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002201{
Damien Miller36812092006-03-26 14:22:47 +11002202 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002203
2204 n = MAX(*maxfdp, channel_max_fd);
2205
Damien Miller36812092006-03-26 14:22:47 +11002206 nfdset = howmany(n+1, NFDBITS);
2207 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002208 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002209 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2210 sz = nfdset * sizeof(fd_mask);
2211
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002212 /* perhaps check sz < nalloc/2 and shrink? */
2213 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002214 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2215 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002216 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002217 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002218 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002219 memset(*readsetp, 0, sz);
2220 memset(*writesetp, 0, sz);
2221
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002222 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002223 channel_handler(channel_pre, *readsetp, *writesetp,
2224 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002225}
2226
Ben Lindstrome9c99912001-06-09 00:41:05 +00002227/*
2228 * After select, perform any appropriate operations for channels which have
2229 * events pending.
2230 */
Damien Miller4af51302000-04-16 11:18:38 +10002231void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002232channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002233{
Damien Millera6508752012-04-22 11:21:10 +10002234 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002235}
2236
Ben Lindstrome9c99912001-06-09 00:41:05 +00002237
Damien Miller5e953212001-01-30 09:14:00 +11002238/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002239void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002240channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002241{
Damien Millerb38eff82000-04-01 11:09:21 +10002242 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002243 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002244
Damien Miller95def091999-11-25 00:26:21 +11002245 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002246 c = channels[i];
2247 if (c == NULL)
2248 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002249
Ben Lindstrome9c99912001-06-09 00:41:05 +00002250 /*
2251 * We are only interested in channels that can have buffered
2252 * incoming data.
2253 */
Damien Miller34132e52000-01-14 15:45:46 +11002254 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10002255 if (c->type != SSH_CHANNEL_OPEN &&
2256 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11002257 continue;
2258 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10002259 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002260 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002261 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002262 if (compat20 &&
2263 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002264 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002265 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002266 continue;
2267 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268
Damien Miller95def091999-11-25 00:26:21 +11002269 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002270 if ((c->istate == CHAN_INPUT_OPEN ||
2271 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2272 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002273 if (c->datagram) {
2274 if (len > 0) {
2275 u_char *data;
2276 u_int dlen;
2277
2278 data = buffer_get_string(&c->input,
2279 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002280 if (dlen > c->remote_window ||
2281 dlen > c->remote_maxpacket) {
2282 debug("channel %d: datagram "
2283 "too big for channel",
2284 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002285 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002286 continue;
2287 }
Damien Millerd27b9472005-12-13 19:29:02 +11002288 packet_start(SSH2_MSG_CHANNEL_DATA);
2289 packet_put_int(c->remote_id);
2290 packet_put_string(data, dlen);
2291 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002292 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002293 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002294 }
2295 continue;
2296 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002297 /*
2298 * Send some data for the other side over the secure
2299 * connection.
2300 */
Damien Miller33b13562000-04-04 14:38:59 +10002301 if (compat20) {
2302 if (len > c->remote_window)
2303 len = c->remote_window;
2304 if (len > c->remote_maxpacket)
2305 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11002306 } else {
Damien Miller33b13562000-04-04 14:38:59 +10002307 if (packet_is_interactive()) {
2308 if (len > 1024)
2309 len = 512;
2310 } else {
2311 /* Keep the packets at reasonable size. */
2312 if (len > packet_get_maxsize()/2)
2313 len = packet_get_maxsize()/2;
2314 }
Damien Miller95def091999-11-25 00:26:21 +11002315 }
Damien Millerb38eff82000-04-01 11:09:21 +10002316 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10002317 packet_start(compat20 ?
2318 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002319 packet_put_int(c->remote_id);
2320 packet_put_string(buffer_ptr(&c->input), len);
2321 packet_send();
2322 buffer_consume(&c->input, len);
2323 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002324 }
2325 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11002326 if (compat13)
2327 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11002328 /*
2329 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002330 * tell peer, that we will not send more data: send IEOF.
2331 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002332 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002333 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002334 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2335 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002336 else
2337 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002338 }
Damien Miller33b13562000-04-04 14:38:59 +10002339 /* Send extended data, i.e. stderr */
2340 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00002341 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002342 c->remote_window > 0 &&
2343 (len = buffer_len(&c->extended)) > 0 &&
2344 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002345 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002346 c->self, c->remote_window, buffer_len(&c->extended),
2347 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002348 if (len > c->remote_window)
2349 len = c->remote_window;
2350 if (len > c->remote_maxpacket)
2351 len = c->remote_maxpacket;
2352 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2353 packet_put_int(c->remote_id);
2354 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2355 packet_put_string(buffer_ptr(&c->extended), len);
2356 packet_send();
2357 buffer_consume(&c->extended, len);
2358 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002359 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002360 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002362}
2363
Ben Lindstrome9c99912001-06-09 00:41:05 +00002364
2365/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002366
2367/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002368int
Damien Miller630d6f42002-01-22 23:17:30 +11002369channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002370{
Damien Miller34132e52000-01-14 15:45:46 +11002371 int id;
Damien Miller633de332014-05-15 13:48:26 +10002372 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002373 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002374 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002375
Damien Miller95def091999-11-25 00:26:21 +11002376 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002377 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002378 c = channel_lookup(id);
2379 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002380 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002381
Damien Miller95def091999-11-25 00:26:21 +11002382 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002383 if (c->type != SSH_CHANNEL_OPEN &&
2384 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002385 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002386
Damien Miller95def091999-11-25 00:26:21 +11002387 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002388 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002389 win_len = data_len;
2390 if (c->datagram)
2391 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002392
Damien Millera04ad492004-01-21 11:02:09 +11002393 /*
2394 * Ignore data for protocol > 1.3 if output end is no longer open.
2395 * For protocol 2 the sending side is reducing its window as it sends
2396 * data, so we must 'fake' consumption of the data in order to ensure
2397 * that window updates are sent back. Otherwise the connection might
2398 * deadlock.
2399 */
2400 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2401 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002402 c->local_window -= win_len;
2403 c->local_consumed += win_len;
Damien Millera04ad492004-01-21 11:02:09 +11002404 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002405 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002406 }
2407
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002408 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002409 if (win_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002410 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller7d457182010-08-05 23:09:48 +10002411 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002412 }
Damien Miller7d457182010-08-05 23:09:48 +10002413 if (win_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002414 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller7d457182010-08-05 23:09:48 +10002415 c->self, win_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002416 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002417 }
Damien Miller7d457182010-08-05 23:09:48 +10002418 c->local_window -= win_len;
Damien Miller33b13562000-04-04 14:38:59 +10002419 }
Damien Millerd27b9472005-12-13 19:29:02 +11002420 if (c->datagram)
2421 buffer_put_string(&c->output, data, data_len);
2422 else
2423 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002424 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002425 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002426}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002427
Damien Millerd79b4242006-03-31 23:11:44 +11002428/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002429int
Damien Miller630d6f42002-01-22 23:17:30 +11002430channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002431{
2432 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002433 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002434 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002435 Channel *c;
2436
2437 /* Get the channel number and verify it. */
2438 id = packet_get_int();
2439 c = channel_lookup(id);
2440
2441 if (c == NULL)
2442 packet_disconnect("Received extended_data for bad channel %d.", id);
2443 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002444 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002445 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002446 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002447 if (c->flags & CHAN_EOF_RCVD) {
2448 if (datafellows & SSH_BUG_EXTEOF)
2449 debug("channel %d: accepting ext data after eof", id);
2450 else
2451 packet_disconnect("Received extended_data after EOF "
2452 "on channel %d.", id);
2453 }
Damien Miller33b13562000-04-04 14:38:59 +10002454 tcode = packet_get_int();
2455 if (c->efd == -1 ||
2456 c->extended_usage != CHAN_EXTENDED_WRITE ||
2457 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002458 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002459 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002460 }
2461 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002462 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002463 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002464 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002465 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002466 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002467 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002468 }
Damien Millerd3444942000-09-30 14:20:03 +11002469 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002470 c->local_window -= data_len;
2471 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002472 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002473 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002474}
2475
Damien Millerd79b4242006-03-31 23:11:44 +11002476/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002477int
Damien Miller630d6f42002-01-22 23:17:30 +11002478channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002479{
2480 int id;
2481 Channel *c;
2482
Damien Millerb38eff82000-04-01 11:09:21 +10002483 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002484 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002485 c = channel_lookup(id);
2486 if (c == NULL)
2487 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2488 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002489
2490 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002491 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002492 debug("channel %d: FORCE input drain", c->self);
2493 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002494 if (buffer_len(&c->input) == 0)
2495 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002496 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002497 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002498}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002499
Damien Millerd79b4242006-03-31 23:11:44 +11002500/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002501int
Damien Miller630d6f42002-01-22 23:17:30 +11002502channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002503{
Damien Millerb38eff82000-04-01 11:09:21 +10002504 int id;
2505 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002506
Damien Millerb38eff82000-04-01 11:09:21 +10002507 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002508 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002509 c = channel_lookup(id);
2510 if (c == NULL)
2511 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002512
2513 /*
2514 * Send a confirmation that we have closed the channel and no more
2515 * data is coming for it.
2516 */
Damien Miller95def091999-11-25 00:26:21 +11002517 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002518 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002519 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002520
Damien Miller5428f641999-11-25 11:54:57 +11002521 /*
2522 * If the channel is in closed state, we have sent a close request,
2523 * and the other side will eventually respond with a confirmation.
2524 * Thus, we cannot free the channel here, because then there would be
2525 * no-one to receive the confirmation. The channel gets freed when
2526 * the confirmation arrives.
2527 */
Damien Millerb38eff82000-04-01 11:09:21 +10002528 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002529 /*
2530 * Not a closed channel - mark it as draining, which will
2531 * cause it to be freed later.
2532 */
Damien Miller76765c02002-01-22 23:21:15 +11002533 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002534 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002535 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002536 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002537}
2538
Damien Millerb38eff82000-04-01 11:09:21 +10002539/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002540/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002541int
Damien Miller630d6f42002-01-22 23:17:30 +11002542channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002543{
Damien Millerb38eff82000-04-01 11:09:21 +10002544 int id = packet_get_int();
2545 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002546
Damien Miller48b03fc2002-01-22 23:11:40 +11002547 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002548 if (c == NULL)
2549 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2550 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002551 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002552}
2553
Damien Millerd79b4242006-03-31 23:11:44 +11002554/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002555int
Damien Miller630d6f42002-01-22 23:17:30 +11002556channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002557{
2558 int id = packet_get_int();
2559 Channel *c = channel_lookup(id);
2560
Damien Miller48b03fc2002-01-22 23:11:40 +11002561 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002562 if (c == NULL)
2563 packet_disconnect("Received close confirmation for "
2564 "out-of-range channel %d.", id);
Damien Miller36187092013-06-10 13:07:11 +10002565 if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
Damien Millerb38eff82000-04-01 11:09:21 +10002566 packet_disconnect("Received close confirmation for "
2567 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002568 channel_free(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002569 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002570}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002571
Damien Millerd79b4242006-03-31 23:11:44 +11002572/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002573int
Damien Miller630d6f42002-01-22 23:17:30 +11002574channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002575{
Damien Millerb38eff82000-04-01 11:09:21 +10002576 int id, remote_id;
2577 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002578
Damien Millerb38eff82000-04-01 11:09:21 +10002579 id = packet_get_int();
2580 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581
Damien Millerb38eff82000-04-01 11:09:21 +10002582 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2583 packet_disconnect("Received open confirmation for "
2584 "non-opening channel %d.", id);
2585 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002586 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002587 c->remote_id = remote_id;
2588 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002589
2590 if (compat20) {
2591 c->remote_window = packet_get_int();
2592 c->remote_maxpacket = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002593 if (c->open_confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002594 debug2("callback start");
Damien Millerd530f5f2010-05-21 14:57:10 +10002595 c->open_confirm(c->self, 1, c->open_confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002596 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002597 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002598 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002599 c->remote_window, c->remote_maxpacket);
2600 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002601 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002602 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002603}
2604
Ben Lindstrombba81212001-06-25 05:01:22 +00002605static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002606reason2txt(int reason)
2607{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002608 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002609 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2610 return "administratively prohibited";
2611 case SSH2_OPEN_CONNECT_FAILED:
2612 return "connect failed";
2613 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2614 return "unknown channel type";
2615 case SSH2_OPEN_RESOURCE_SHORTAGE:
2616 return "resource shortage";
2617 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002618 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002619}
2620
Damien Millerd79b4242006-03-31 23:11:44 +11002621/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002622int
Damien Miller630d6f42002-01-22 23:17:30 +11002623channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002624{
Kevin Steves12057502001-02-05 14:54:34 +00002625 int id, reason;
2626 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002627 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002628
Damien Millerb38eff82000-04-01 11:09:21 +10002629 id = packet_get_int();
2630 c = channel_lookup(id);
2631
2632 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2633 packet_disconnect("Received open failure for "
2634 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002635 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002636 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002637 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002638 msg = packet_get_string(NULL);
2639 lang = packet_get_string(NULL);
2640 }
Damien Miller996acd22003-04-09 20:59:48 +10002641 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002642 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Darren Tuckera627d422013-06-02 07:31:17 +10002643 free(msg);
2644 free(lang);
Damien Millerd530f5f2010-05-21 14:57:10 +10002645 if (c->open_confirm) {
2646 debug2("callback start");
2647 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2648 debug2("callback done");
2649 }
Damien Miller33b13562000-04-04 14:38:59 +10002650 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002651 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002652 /* Schedule the channel for cleanup/deletion. */
2653 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002654 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002655}
2656
Damien Millerd79b4242006-03-31 23:11:44 +11002657/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002658int
Damien Miller630d6f42002-01-22 23:17:30 +11002659channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002660{
2661 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002662 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00002663 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10002664
2665 if (!compat20)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002666 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002667
2668 /* Get the channel number and verify it. */
2669 id = packet_get_int();
2670 c = channel_lookup(id);
2671
Damien Millerd47c62a2005-12-13 19:33:57 +11002672 if (c == NULL) {
2673 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002674 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002675 }
2676 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002677 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002678 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00002679 if ((tmp = c->remote_window + adjust) < c->remote_window)
2680 fatal("channel %d: adjust %u overflows remote window %u",
2681 id, adjust, c->remote_window);
2682 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002683 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002684}
2685
Damien Millerd79b4242006-03-31 23:11:44 +11002686/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002687int
Damien Miller630d6f42002-01-22 23:17:30 +11002688channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002689{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002690 Channel *c = NULL;
2691 u_short host_port;
2692 char *host, *originator_string;
Damien Millerbd740252008-05-19 15:37:09 +10002693 int remote_id;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002694
Ben Lindstrome9c99912001-06-09 00:41:05 +00002695 remote_id = packet_get_int();
2696 host = packet_get_string(NULL);
2697 host_port = packet_get_int();
2698
2699 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2700 originator_string = packet_get_string(NULL);
2701 } else {
2702 originator_string = xstrdup("unknown (remote did not supply name)");
2703 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002704 packet_check_eom();
Damien Miller7acefbb2014-07-18 14:11:24 +10002705 c = channel_connect_to_port(host, host_port,
Damien Millerbd740252008-05-19 15:37:09 +10002706 "connected socket", originator_string);
Darren Tuckera627d422013-06-02 07:31:17 +10002707 free(originator_string);
2708 free(host);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002709 if (c == NULL) {
2710 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2711 packet_put_int(remote_id);
2712 packet_send();
Damien Millerbd740252008-05-19 15:37:09 +10002713 } else
2714 c->remote_id = remote_id;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002715 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002716}
2717
Damien Millerb84886b2008-05-19 15:05:07 +10002718/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002719int
Damien Millerb84886b2008-05-19 15:05:07 +10002720channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2721{
2722 Channel *c;
2723 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002724 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002725
2726 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002727 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002728
Damien Miller16a73072008-12-08 09:55:25 +11002729 id = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002730 packet_check_eom();
2731
Damien Miller16a73072008-12-08 09:55:25 +11002732 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002733
Damien Miller16a73072008-12-08 09:55:25 +11002734 if ((c = channel_lookup(id)) == NULL) {
2735 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002736 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002737 }
Damien Millerb84886b2008-05-19 15:05:07 +10002738 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002739 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002740 cc->cb(type, c, cc->ctx);
2741 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002742 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002743 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002744 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002745}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002746
Ben Lindstrome9c99912001-06-09 00:41:05 +00002747/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002748
Ben Lindstrom908afed2001-10-03 17:34:59 +00002749void
2750channel_set_af(int af)
2751{
2752 IPv4or6 = af;
2753}
2754
Damien Millerf6dff7c2011-09-22 21:38:52 +10002755
2756/*
2757 * Determine whether or not a port forward listens to loopback, the
2758 * specified address or wildcard. On the client, a specified bind
2759 * address will always override gateway_ports. On the server, a
2760 * gateway_ports of 1 (``yes'') will override the client's specification
2761 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2762 * will bind to whatever address the client asked for.
2763 *
2764 * Special-case listen_addrs are:
2765 *
2766 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2767 * "" (empty string), "*" -> wildcard v4/v6
2768 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002769 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002770 */
2771static const char *
2772channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002773 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002774{
2775 const char *addr = NULL;
2776 int wildcard = 0;
2777
2778 if (listen_addr == NULL) {
2779 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002780 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002781 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002782 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002783 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2784 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2785 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002786 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002787 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002788 /*
2789 * Notify client if they requested a specific listen
2790 * address and it was overridden.
2791 */
2792 if (*listen_addr != '\0' &&
2793 strcmp(listen_addr, "0.0.0.0") != 0 &&
2794 strcmp(listen_addr, "*") != 0) {
2795 packet_send_debug("Forwarding listen address "
2796 "\"%s\" overridden by server "
2797 "GatewayPorts", listen_addr);
2798 }
Damien Miller602943d2014-07-04 08:59:41 +10002799 } else if (strcmp(listen_addr, "localhost") != 0 ||
2800 strcmp(listen_addr, "127.0.0.1") == 0 ||
2801 strcmp(listen_addr, "::1") == 0) {
2802 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002803 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002804 }
2805 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2806 strcmp(listen_addr, "::1") == 0) {
2807 /*
2808 * If a specific IPv4/IPv6 localhost address has been
2809 * requested then accept it even if gateway_ports is in
2810 * effect. This allows the client to prefer IPv4 or IPv6.
2811 */
2812 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002813 }
2814 if (wildcardp != NULL)
2815 *wildcardp = wildcard;
2816 return addr;
2817}
2818
Damien Millerb16461c2002-01-22 23:29:22 +11002819static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002820channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2821 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002822{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002823 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002824 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002825 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002826 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002827 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002828 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002829
Damien Millerf91ee4c2005-03-01 21:24:33 +11002830 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002831
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00002832 if (is_client && fwd->connect_path != NULL) {
2833 host = fwd->connect_path;
2834 } else {
2835 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2836 fwd->listen_host : fwd->connect_host;
2837 if (host == NULL) {
2838 error("No forward host name.");
2839 return 0;
2840 }
2841 if (strlen(host) >= NI_MAXHOST) {
2842 error("Forward host name too long.");
2843 return 0;
2844 }
Kevin Steves12057502001-02-05 14:54:34 +00002845 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002846
Damien Millerf6dff7c2011-09-22 21:38:52 +10002847 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002848 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2849 is_client, fwd_opts);
2850 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002851 type, wildcard, (addr == NULL) ? "NULL" : addr);
2852
2853 /*
Damien Miller34132e52000-01-14 15:45:46 +11002854 * getaddrinfo returns a loopback address if the hostname is
2855 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002856 */
Damien Miller34132e52000-01-14 15:45:46 +11002857 memset(&hints, 0, sizeof(hints));
2858 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002859 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002860 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002861 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002862 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2863 if (addr == NULL) {
2864 /* This really shouldn't happen */
2865 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002866 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002867 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002868 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11002869 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002870 }
Damien Millera7270302005-07-06 09:36:05 +10002871 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002872 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002873 if (allocated_listen_port != NULL)
2874 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11002875 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11002876 switch (ai->ai_family) {
2877 case AF_INET:
2878 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2879 sin_port;
2880 break;
2881 case AF_INET6:
2882 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2883 sin6_port;
2884 break;
2885 default:
Damien Miller34132e52000-01-14 15:45:46 +11002886 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11002887 }
2888 /*
2889 * If allocating a port for -R forwards, then use the
2890 * same port for all address families.
2891 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002892 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002893 allocated_listen_port != NULL && *allocated_listen_port > 0)
2894 *lport_p = htons(*allocated_listen_port);
2895
Damien Miller34132e52000-01-14 15:45:46 +11002896 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2897 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10002898 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11002899 continue;
2900 }
2901 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11002902 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002903 if (sock < 0) {
2904 /* this is no error since kernel may not support ipv6 */
2905 verbose("socket: %.100s", strerror(errno));
2906 continue;
2907 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002908
2909 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11002910 if (ai->ai_family == AF_INET6)
2911 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002912
Damien Miller4bf648f2009-02-14 16:28:21 +11002913 debug("Local forwarding listening on %s port %s.",
2914 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002915
Damien Miller34132e52000-01-14 15:45:46 +11002916 /* Bind the socket to the address. */
2917 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2918 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002919 if (!ai->ai_next)
2920 error("bind: %.100s", strerror(errno));
2921 else
2922 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002923
Damien Miller34132e52000-01-14 15:45:46 +11002924 close(sock);
2925 continue;
2926 }
2927 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002928 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002929 error("listen: %.100s", strerror(errno));
2930 close(sock);
2931 continue;
2932 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002933
2934 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10002935 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11002936 * record what we got.
2937 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002938 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002939 allocated_listen_port != NULL &&
2940 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00002941 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11002942 debug("Allocated listen port %d",
2943 *allocated_listen_port);
2944 }
2945
Damien Miller34132e52000-01-14 15:45:46 +11002946 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002947 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002948 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002949 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11002950 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10002951 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002952 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10002953 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11002954 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
2955 c->listening_port = *allocated_listen_port;
2956 else
Damien Miller7acefbb2014-07-18 14:11:24 +10002957 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002958 success = 1;
2959 }
2960 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10002961 error("%s: cannot listen to port: %d", __func__,
2962 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002963 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002964 return success;
Damien Miller95def091999-11-25 00:26:21 +11002965}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002966
Damien Miller7acefbb2014-07-18 14:11:24 +10002967static int
2968channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
2969 struct ForwardOptions *fwd_opts)
2970{
2971 struct sockaddr_un sunaddr;
2972 const char *path;
2973 Channel *c;
2974 int port, sock;
2975 mode_t omask;
2976
2977 switch (type) {
2978 case SSH_CHANNEL_UNIX_LISTENER:
2979 if (fwd->connect_path != NULL) {
2980 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
2981 error("Local connecting path too long: %s",
2982 fwd->connect_path);
2983 return 0;
2984 }
2985 path = fwd->connect_path;
2986 port = PORT_STREAMLOCAL;
2987 } else {
2988 if (fwd->connect_host == NULL) {
2989 error("No forward host name.");
2990 return 0;
2991 }
2992 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
2993 error("Forward host name too long.");
2994 return 0;
2995 }
2996 path = fwd->connect_host;
2997 port = fwd->connect_port;
2998 }
2999 break;
3000 case SSH_CHANNEL_RUNIX_LISTENER:
3001 path = fwd->listen_path;
3002 port = PORT_STREAMLOCAL;
3003 break;
3004 default:
3005 error("%s: unexpected channel type %d", __func__, type);
3006 return 0;
3007 }
3008
3009 if (fwd->listen_path == NULL) {
3010 error("No forward path name.");
3011 return 0;
3012 }
3013 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3014 error("Local listening path too long: %s", fwd->listen_path);
3015 return 0;
3016 }
3017
3018 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3019
3020 /* Start a Unix domain listener. */
3021 omask = umask(fwd_opts->streamlocal_bind_mask);
3022 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3023 fwd_opts->streamlocal_bind_unlink);
3024 umask(omask);
3025 if (sock < 0)
3026 return 0;
3027
3028 debug("Local forwarding listening on path %s.", fwd->listen_path);
3029
3030 /* Allocate a channel number for the socket. */
3031 c = channel_new("unix listener", type, sock, sock, -1,
3032 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3033 0, "unix listener", 1);
3034 c->path = xstrdup(path);
3035 c->host_port = port;
3036 c->listening_port = PORT_STREAMLOCAL;
3037 c->listening_addr = xstrdup(fwd->listen_path);
3038 return 1;
3039}
3040
3041static int
3042channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003043{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003044 u_int i;
3045 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003046
Darren Tucker47eede72005-03-14 23:08:12 +11003047 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003048 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003049 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3050 continue;
3051 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3052 debug2("%s: close channel %d", __func__, i);
3053 channel_free(c);
3054 found = 1;
3055 }
3056 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003057
Damien Millerf6dff7c2011-09-22 21:38:52 +10003058 return (found);
3059}
3060
Damien Miller7acefbb2014-07-18 14:11:24 +10003061static int
3062channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003063{
3064 u_int i;
3065 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003066
3067 for (i = 0; i < channels_alloc; i++) {
3068 Channel *c = channels[i];
3069 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3070 continue;
3071 if (c->path == NULL)
3072 continue;
3073 if (strcmp(c->path, path) == 0) {
3074 debug2("%s: close channel %d", __func__, i);
3075 channel_free(c);
3076 found = 1;
3077 }
3078 }
3079
3080 return (found);
3081}
3082
3083int
3084channel_cancel_rport_listener(struct Forward *fwd)
3085{
3086 if (fwd->listen_path != NULL)
3087 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3088 else
3089 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3090}
3091
3092static int
3093channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3094 int cport, struct ForwardOptions *fwd_opts)
3095{
3096 u_int i;
3097 int found = 0;
3098 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003099
3100 for (i = 0; i < channels_alloc; i++) {
3101 Channel *c = channels[i];
3102 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3103 continue;
Damien Millerff773642011-09-22 21:39:48 +10003104 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003105 continue;
Damien Millerff773642011-09-22 21:39:48 +10003106 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3107 /* skip dynamic forwardings */
3108 if (c->host_port == 0)
3109 continue;
3110 } else {
3111 if (c->host_port != cport)
3112 continue;
3113 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003114 if ((c->listening_addr == NULL && addr != NULL) ||
3115 (c->listening_addr != NULL && addr == NULL))
3116 continue;
3117 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003118 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003119 channel_free(c);
3120 found = 1;
3121 }
3122 }
3123
3124 return (found);
3125}
3126
Damien Miller7acefbb2014-07-18 14:11:24 +10003127static int
3128channel_cancel_lport_listener_streamlocal(const char *path)
3129{
3130 u_int i;
3131 int found = 0;
3132
3133 if (path == NULL) {
3134 error("%s: no path specified.", __func__);
3135 return 0;
3136 }
3137
3138 for (i = 0; i < channels_alloc; i++) {
3139 Channel *c = channels[i];
3140 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3141 continue;
3142 if (c->listening_addr == NULL)
3143 continue;
3144 if (strcmp(c->listening_addr, path) == 0) {
3145 debug2("%s: close channel %d", __func__, i);
3146 channel_free(c);
3147 found = 1;
3148 }
3149 }
3150
3151 return (found);
3152}
3153
3154int
3155channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3156{
3157 if (fwd->listen_path != NULL)
3158 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3159 else
3160 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3161}
3162
Damien Millerb16461c2002-01-22 23:29:22 +11003163/* protocol local port fwd, used by ssh (and sshd in v1) */
3164int
Damien Miller7acefbb2014-07-18 14:11:24 +10003165channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003166{
Damien Miller7acefbb2014-07-18 14:11:24 +10003167 if (fwd->listen_path != NULL) {
3168 return channel_setup_fwd_listener_streamlocal(
3169 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3170 } else {
3171 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3172 fwd, NULL, fwd_opts);
3173 }
Damien Millerb16461c2002-01-22 23:29:22 +11003174}
3175
3176/* protocol v2 remote port fwd, used by sshd */
3177int
Damien Miller7acefbb2014-07-18 14:11:24 +10003178channel_setup_remote_fwd_listener(struct Forward *fwd,
3179 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003180{
Damien Miller7acefbb2014-07-18 14:11:24 +10003181 if (fwd->listen_path != NULL) {
3182 return channel_setup_fwd_listener_streamlocal(
3183 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3184 } else {
3185 return channel_setup_fwd_listener_tcpip(
3186 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3187 fwd_opts);
3188 }
Damien Millerb16461c2002-01-22 23:29:22 +11003189}
3190
Damien Miller5428f641999-11-25 11:54:57 +11003191/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003192 * Translate the requested rfwd listen host to something usable for
3193 * this server.
3194 */
3195static const char *
3196channel_rfwd_bind_host(const char *listen_host)
3197{
3198 if (listen_host == NULL) {
3199 if (datafellows & SSH_BUG_RFWD_ADDR)
3200 return "127.0.0.1";
3201 else
3202 return "localhost";
3203 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3204 if (datafellows & SSH_BUG_RFWD_ADDR)
3205 return "0.0.0.0";
3206 else
3207 return "";
3208 } else
3209 return listen_host;
3210}
3211
3212/*
Damien Miller5428f641999-11-25 11:54:57 +11003213 * Initiate forwarding of connections to port "port" on remote host through
3214 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003215 * Returns handle (index) for updating the dynamic listen port with
3216 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003217 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003218int
Damien Miller7acefbb2014-07-18 14:11:24 +10003219channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003220{
Darren Tucker68afb8c2011-10-02 18:59:03 +11003221 int type, success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003222
Damien Miller95def091999-11-25 00:26:21 +11003223 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10003224 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10003225 packet_start(SSH2_MSG_GLOBAL_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003226 if (fwd->listen_path != NULL) {
3227 packet_put_cstring("streamlocal-forward@openssh.com");
3228 packet_put_char(1); /* boolean: want reply */
3229 packet_put_cstring(fwd->listen_path);
3230 } else {
3231 packet_put_cstring("tcpip-forward");
3232 packet_put_char(1); /* boolean: want reply */
3233 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3234 packet_put_int(fwd->listen_port);
3235 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003236 packet_send();
3237 packet_write_wait();
3238 /* Assume that server accepts the request */
3239 success = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003240 } else if (fwd->listen_path == NULL) {
Damien Miller33b13562000-04-04 14:38:59 +10003241 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003242 packet_put_int(fwd->listen_port);
3243 packet_put_cstring(fwd->connect_host);
3244 packet_put_int(fwd->connect_port);
Damien Miller33b13562000-04-04 14:38:59 +10003245 packet_send();
3246 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003247
3248 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11003249 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003250 switch (type) {
3251 case SSH_SMSG_SUCCESS:
3252 success = 1;
3253 break;
3254 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11003255 break;
3256 default:
3257 /* Unknown packet */
3258 packet_disconnect("Protocol error for port forward request:"
3259 "received packet type %d.", type);
3260 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003261 } else {
3262 logit("Warning: Server does not support remote stream local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11003263 }
3264 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003265 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003266 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003267 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003268 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003269 if (fwd->connect_path != NULL) {
3270 permitted_opens[idx].host_to_connect =
3271 xstrdup(fwd->connect_path);
3272 permitted_opens[idx].port_to_connect =
3273 PORT_STREAMLOCAL;
3274 } else {
3275 permitted_opens[idx].host_to_connect =
3276 xstrdup(fwd->connect_host);
3277 permitted_opens[idx].port_to_connect =
3278 fwd->connect_port;
3279 }
3280 if (fwd->listen_path != NULL) {
3281 permitted_opens[idx].listen_host = NULL;
3282 permitted_opens[idx].listen_path =
3283 xstrdup(fwd->listen_path);
3284 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3285 } else {
3286 permitted_opens[idx].listen_host =
3287 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3288 permitted_opens[idx].listen_path = NULL;
3289 permitted_opens[idx].listen_port = fwd->listen_port;
3290 }
Damien Miller33b13562000-04-04 14:38:59 +10003291 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003292 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003293}
3294
Damien Miller4b3ed642014-07-02 15:29:40 +10003295static int
3296open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003297 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003298{
3299 if (allowed_open->host_to_connect == NULL)
3300 return 0;
3301 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3302 allowed_open->port_to_connect != requestedport)
3303 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003304 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3305 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003306 return 0;
3307 return 1;
3308}
3309
3310/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003311 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003312 * we don't support FWD_PERMIT_ANY_PORT and
3313 * need to translate between the configured-host (listen_host)
3314 * and what we've sent to the remote server (channel_rfwd_bind_host)
3315 */
3316static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003317open_listen_match_tcpip(ForwardPermission *allowed_open,
3318 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003319{
3320 const char *allowed_host;
3321
3322 if (allowed_open->host_to_connect == NULL)
3323 return 0;
3324 if (allowed_open->listen_port != requestedport)
3325 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003326 if (!translate && allowed_open->listen_host == NULL &&
3327 requestedhost == NULL)
3328 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003329 allowed_host = translate ?
3330 channel_rfwd_bind_host(allowed_open->listen_host) :
3331 allowed_open->listen_host;
3332 if (allowed_host == NULL ||
3333 strcmp(allowed_host, requestedhost) != 0)
3334 return 0;
3335 return 1;
3336}
3337
Damien Miller7acefbb2014-07-18 14:11:24 +10003338static int
3339open_listen_match_streamlocal(ForwardPermission *allowed_open,
3340 const char *requestedpath)
3341{
3342 if (allowed_open->host_to_connect == NULL)
3343 return 0;
3344 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3345 return 0;
3346 if (allowed_open->listen_path == NULL ||
3347 strcmp(allowed_open->listen_path, requestedpath) != 0)
3348 return 0;
3349 return 1;
3350}
3351
Damien Miller5428f641999-11-25 11:54:57 +11003352/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003353 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003354 * local side.
3355 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003356static int
3357channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003358{
3359 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003360
3361 if (!compat20)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003362 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003363
3364 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003365 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003366 break;
3367 }
3368 if (i >= num_permitted_opens) {
3369 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003370 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003371 }
3372 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3373 packet_put_cstring("cancel-tcpip-forward");
3374 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003375 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003376 packet_put_int(port);
3377 packet_send();
3378
Damien Miller4b3ed642014-07-02 15:29:40 +10003379 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003380 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003381 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003382 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003383 free(permitted_opens[i].listen_host);
3384 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003385 permitted_opens[i].listen_path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003386
3387 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003388}
3389
3390/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003391 * Request cancellation of remote forwarding of Unix domain socket
3392 * path from local side.
3393 */
3394static int
3395channel_request_rforward_cancel_streamlocal(const char *path)
3396{
3397 int i;
3398
3399 if (!compat20)
3400 return -1;
3401
3402 for (i = 0; i < num_permitted_opens; i++) {
3403 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3404 break;
3405 }
3406 if (i >= num_permitted_opens) {
3407 debug("%s: requested forward not found", __func__);
3408 return -1;
3409 }
3410 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3411 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3412 packet_put_char(0);
3413 packet_put_cstring(path);
3414 packet_send();
3415
3416 permitted_opens[i].listen_port = 0;
3417 permitted_opens[i].port_to_connect = 0;
3418 free(permitted_opens[i].host_to_connect);
3419 permitted_opens[i].host_to_connect = NULL;
3420 permitted_opens[i].listen_host = NULL;
3421 free(permitted_opens[i].listen_path);
3422 permitted_opens[i].listen_path = NULL;
3423
3424 return 0;
3425}
3426
3427/*
3428 * Request cancellation of remote forwarding of a connection from local side.
3429 */
3430int
3431channel_request_rforward_cancel(struct Forward *fwd)
3432{
3433 if (fwd->listen_path != NULL) {
3434 return (channel_request_rforward_cancel_streamlocal(
3435 fwd->listen_path));
3436 } else {
3437 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3438 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3439 }
3440}
3441
3442/*
Damien Miller5428f641999-11-25 11:54:57 +11003443 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
3444 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10003445 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11003446 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003447int
Damien Miller7acefbb2014-07-18 14:11:24 +10003448channel_input_port_forward_request(int is_root, struct ForwardOptions *fwd_opts)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003449{
Darren Tuckere7d4b192006-07-12 22:17:10 +10003450 int success = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003451 struct Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003452
Damien Miller95def091999-11-25 00:26:21 +11003453 /* Get arguments from the packet. */
Damien Miller7acefbb2014-07-18 14:11:24 +10003454 memset(&fwd, 0, sizeof(fwd));
3455 fwd.listen_port = packet_get_int();
3456 fwd.connect_host = packet_get_string(NULL);
3457 fwd.connect_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003458
Damien Millerbac2d8a2000-09-05 16:13:06 +11003459#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11003460 /*
3461 * Check that an unprivileged user is not trying to forward a
3462 * privileged port.
3463 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003464 if (fwd.listen_port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10003465 packet_disconnect(
3466 "Requested forwarding of port %d but user is not root.",
Damien Miller7acefbb2014-07-18 14:11:24 +10003467 fwd.listen_port);
3468 if (fwd.connect_port == 0)
Darren Tucker9189ff82003-07-03 13:52:04 +10003469 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11003470#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10003471
Damien Miller0bc1bd82000-11-13 22:57:25 +11003472 /* Initiate forwarding */
Damien Miller7acefbb2014-07-18 14:11:24 +10003473 success = channel_setup_local_fwd_listener(&fwd, fwd_opts);
Damien Miller95def091999-11-25 00:26:21 +11003474
3475 /* Free the argument string. */
Damien Miller7acefbb2014-07-18 14:11:24 +10003476 free(fwd.connect_host);
Darren Tuckere7d4b192006-07-12 22:17:10 +10003477
3478 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003479}
3480
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003481/*
3482 * Permits opening to any host/port if permitted_opens[] is empty. This is
3483 * usually called by the server, because the user could connect to any port
3484 * anyway, and the server has no way to know but to trust the client anyway.
3485 */
3486void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003487channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003488{
3489 if (num_permitted_opens == 0)
3490 all_opens_permitted = 1;
3491}
3492
Ben Lindstroma3700052001-04-05 23:26:32 +00003493void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003494channel_add_permitted_opens(char *host, int port)
3495{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003496 debug("allow port forwarding to host %s port %d", host, port);
3497
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003498 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003499 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003500 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3501 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003502 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003503 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003504 permitted_opens[num_permitted_opens].listen_port = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003505 num_permitted_opens++;
3506
3507 all_opens_permitted = 0;
3508}
3509
Darren Tucker68afb8c2011-10-02 18:59:03 +11003510/*
3511 * Update the listen port for a dynamic remote forward, after
3512 * the actual 'newport' has been allocated. If 'newport' < 0 is
3513 * passed then they entry will be invalidated.
3514 */
3515void
3516channel_update_permitted_opens(int idx, int newport)
3517{
3518 if (idx < 0 || idx >= num_permitted_opens) {
3519 debug("channel_update_permitted_opens: index out of range:"
3520 " %d num_permitted_opens %d", idx, num_permitted_opens);
3521 return;
3522 }
3523 debug("%s allowed port %d for forwarding to host %s port %d",
3524 newport > 0 ? "Updating" : "Removing",
3525 newport,
3526 permitted_opens[idx].host_to_connect,
3527 permitted_opens[idx].port_to_connect);
3528 if (newport >= 0) {
3529 permitted_opens[idx].listen_port =
3530 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3531 } else {
3532 permitted_opens[idx].listen_port = 0;
3533 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003534 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003535 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003536 free(permitted_opens[idx].listen_host);
3537 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003538 free(permitted_opens[idx].listen_path);
3539 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003540 }
3541}
3542
Damien Millera765cf42006-07-24 14:08:13 +10003543int
Damien Miller9b439df2006-07-24 14:04:00 +10003544channel_add_adm_permitted_opens(char *host, int port)
3545{
Damien Millera765cf42006-07-24 14:08:13 +10003546 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003547
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003548 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003549 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003550 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3551 = xstrdup(host);
3552 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003553 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003554 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003555 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003556 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003557}
3558
3559void
Damien Millerc6081482012-04-22 11:18:53 +10003560channel_disable_adm_local_opens(void)
3561{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003562 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003563 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003564 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3565 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003566}
3567
3568void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003569channel_clear_permitted_opens(void)
3570{
3571 int i;
3572
Damien Miller4b3ed642014-07-02 15:29:40 +10003573 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003574 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003575 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003576 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003577 }
Darren Tuckera627d422013-06-02 07:31:17 +10003578 free(permitted_opens);
3579 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003580 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003581}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003582
Damien Miller9b439df2006-07-24 14:04:00 +10003583void
3584channel_clear_adm_permitted_opens(void)
3585{
3586 int i;
3587
Damien Miller4b3ed642014-07-02 15:29:40 +10003588 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003589 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003590 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003591 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003592 }
Darren Tuckera627d422013-06-02 07:31:17 +10003593 free(permitted_adm_opens);
3594 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003595 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003596}
3597
Darren Tuckere7140f22008-06-10 23:01:51 +10003598void
3599channel_print_adm_permitted_opens(void)
3600{
Damien Miller6ef17492008-07-16 22:42:06 +10003601 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003602
Damien Millerb53d8a12009-01-28 16:13:04 +11003603 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003604 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003605 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003606 return;
3607 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003608 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003609 if (permitted_adm_opens[i].host_to_connect == NULL)
3610 printf(" none");
3611 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003612 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3613 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003614 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003615}
3616
Darren Tucker1338b9e2011-10-02 18:57:35 +11003617/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3618int
3619permitopen_port(const char *p)
3620{
3621 int port;
3622
3623 if (strcmp(p, "*") == 0)
3624 return FWD_PERMIT_ANY_PORT;
3625 if ((port = a2port(p)) > 0)
3626 return port;
3627 return -1;
3628}
3629
Damien Millerbd740252008-05-19 15:37:09 +10003630/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003631static int
Damien Millerbd740252008-05-19 15:37:09 +10003632connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003633{
Damien Millerbd740252008-05-19 15:37:09 +10003634 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003635 struct sockaddr_un *sunaddr;
3636 char ntop[NI_MAXHOST], strport[MAX(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003637
Damien Millerbd740252008-05-19 15:37:09 +10003638 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003639 switch (cctx->ai->ai_family) {
3640 case AF_UNIX:
3641 /* unix:pathname instead of host:port */
3642 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3643 strlcpy(ntop, "unix", sizeof(ntop));
3644 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3645 break;
3646 case AF_INET:
3647 case AF_INET6:
3648 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3649 ntop, sizeof(ntop), strport, sizeof(strport),
3650 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3651 error("connect_next: getnameinfo failed");
3652 continue;
3653 }
3654 break;
3655 default:
Damien Miller34132e52000-01-14 15:45:46 +11003656 continue;
3657 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003658 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3659 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003660 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003661 error("socket: %.100s", strerror(errno));
3662 else
3663 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003664 continue;
3665 }
Damien Miller232711f2004-06-15 10:35:30 +10003666 if (set_nonblock(sock) == -1)
3667 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003668 if (connect(sock, cctx->ai->ai_addr,
3669 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3670 debug("connect_next: host %.100s ([%.100s]:%s): "
3671 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003672 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003673 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003674 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003675 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003676 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003677 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003678 if (cctx->ai->ai_family != AF_UNIX)
3679 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003680 debug("connect_next: host %.100s ([%.100s]:%s) "
3681 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3682 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003683 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003684 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003685 return -1;
3686}
Damien Millerb38eff82000-04-01 11:09:21 +10003687
Damien Millerbd740252008-05-19 15:37:09 +10003688static void
3689channel_connect_ctx_free(struct channel_connect *cctx)
3690{
Darren Tuckera627d422013-06-02 07:31:17 +10003691 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003692 if (cctx->aitop) {
3693 if (cctx->aitop->ai_family == AF_UNIX)
3694 free(cctx->aitop);
3695 else
3696 freeaddrinfo(cctx->aitop);
3697 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003698 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003699}
3700
Damien Miller7acefbb2014-07-18 14:11:24 +10003701/* Return CONNECTING channel to remote host:port or local socket path */
Damien Millerbd740252008-05-19 15:37:09 +10003702static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10003703connect_to(const char *name, int port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003704{
3705 struct addrinfo hints;
3706 int gaierr;
3707 int sock = -1;
3708 char strport[NI_MAXSERV];
3709 struct channel_connect cctx;
3710 Channel *c;
3711
Damien Miller2bcb8662008-07-12 17:12:29 +10003712 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003713
3714 if (port == PORT_STREAMLOCAL) {
3715 struct sockaddr_un *sunaddr;
3716 struct addrinfo *ai;
3717
3718 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3719 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3720 return (NULL);
3721 }
3722
3723 /*
3724 * Fake up a struct addrinfo for AF_UNIX connections.
3725 * channel_connect_ctx_free() must check ai_family
3726 * and use free() not freeaddirinfo() for AF_UNIX.
3727 */
3728 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3729 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3730 ai->ai_addr = (struct sockaddr *)(ai + 1);
3731 ai->ai_addrlen = sizeof(*sunaddr);
3732 ai->ai_family = AF_UNIX;
3733 ai->ai_socktype = SOCK_STREAM;
3734 ai->ai_protocol = PF_UNSPEC;
3735 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3736 sunaddr->sun_family = AF_UNIX;
3737 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3738 cctx.aitop = ai;
3739 } else {
3740 memset(&hints, 0, sizeof(hints));
3741 hints.ai_family = IPv4or6;
3742 hints.ai_socktype = SOCK_STREAM;
3743 snprintf(strport, sizeof strport, "%d", port);
3744 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop)) != 0) {
3745 error("connect_to %.100s: unknown host (%s)", name,
3746 ssh_gai_strerror(gaierr));
3747 return NULL;
3748 }
Damien Millerbd740252008-05-19 15:37:09 +10003749 }
3750
Damien Miller7acefbb2014-07-18 14:11:24 +10003751 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003752 cctx.port = port;
3753 cctx.ai = cctx.aitop;
3754
3755 if ((sock = connect_next(&cctx)) == -1) {
3756 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003757 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003758 channel_connect_ctx_free(&cctx);
3759 return NULL;
3760 }
3761 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3762 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3763 c->connect_ctx = cctx;
3764 return c;
3765}
3766
3767Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003768channel_connect_by_listen_address(const char *listen_host,
3769 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003770{
3771 int i;
3772
3773 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003774 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003775 listen_port, 1)) {
Damien Millerbd740252008-05-19 15:37:09 +10003776 return connect_to(
3777 permitted_opens[i].host_to_connect,
3778 permitted_opens[i].port_to_connect, ctype, rname);
3779 }
3780 }
3781 error("WARNING: Server requests forwarding for unknown listen_port %d",
3782 listen_port);
3783 return NULL;
3784}
3785
Damien Miller7acefbb2014-07-18 14:11:24 +10003786Channel *
3787channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3788{
3789 int i;
3790
3791 for (i = 0; i < num_permitted_opens; i++) {
3792 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3793 return connect_to(
3794 permitted_opens[i].host_to_connect,
3795 permitted_opens[i].port_to_connect, ctype, rname);
3796 }
3797 }
3798 error("WARNING: Server requests forwarding for unknown path %.100s",
3799 path);
3800 return NULL;
3801}
3802
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003803/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003804Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10003805channel_connect_to_port(const char *host, u_short port, char *ctype, char *rname)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003806{
Damien Miller9b439df2006-07-24 14:04:00 +10003807 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003808
3809 permit = all_opens_permitted;
3810 if (!permit) {
3811 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003812 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003813 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003814 break;
3815 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003816 }
Damien Miller9b439df2006-07-24 14:04:00 +10003817
3818 if (num_adm_permitted_opens > 0) {
3819 permit_adm = 0;
3820 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003821 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003822 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003823 break;
3824 }
Damien Miller9b439df2006-07-24 14:04:00 +10003825 }
3826
3827 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003828 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003829 "but the request was denied.", host, port);
Damien Millerbd740252008-05-19 15:37:09 +10003830 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003831 }
Damien Millerbd740252008-05-19 15:37:09 +10003832 return connect_to(host, port, ctype, rname);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003833}
3834
Damien Miller7acefbb2014-07-18 14:11:24 +10003835/* Check if connecting to that path is permitted and connect. */
3836Channel *
3837channel_connect_to_path(const char *path, char *ctype, char *rname)
3838{
3839 int i, permit, permit_adm = 1;
3840
3841 permit = all_opens_permitted;
3842 if (!permit) {
3843 for (i = 0; i < num_permitted_opens; i++)
3844 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3845 permit = 1;
3846 break;
3847 }
3848 }
3849
3850 if (num_adm_permitted_opens > 0) {
3851 permit_adm = 0;
3852 for (i = 0; i < num_adm_permitted_opens; i++)
3853 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3854 permit_adm = 1;
3855 break;
3856 }
3857 }
3858
3859 if (!permit || !permit_adm) {
3860 logit("Received request to connect to path %.100s, "
3861 "but the request was denied.", path);
3862 return NULL;
3863 }
3864 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3865}
3866
Damien Miller0e220db2004-06-15 10:34:08 +10003867void
3868channel_send_window_changes(void)
3869{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003870 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003871 struct winsize ws;
3872
3873 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003874 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003875 channels[i]->type != SSH_CHANNEL_OPEN)
3876 continue;
3877 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3878 continue;
3879 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003880 packet_put_int((u_int)ws.ws_col);
3881 packet_put_int((u_int)ws.ws_row);
3882 packet_put_int((u_int)ws.ws_xpixel);
3883 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003884 packet_send();
3885 }
3886}
3887
Ben Lindstrome9c99912001-06-09 00:41:05 +00003888/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003889
Damien Miller5428f641999-11-25 11:54:57 +11003890/*
3891 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003892 * Returns 0 and a suitable display number for the DISPLAY variable
3893 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003894 */
Kevin Steves366298c2001-12-19 17:58:01 +00003895int
Damien Miller95c249f2002-02-05 12:11:34 +11003896x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003897 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003898{
Damien Millere7378562001-12-21 14:58:35 +11003899 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003900 int display_number, sock;
3901 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003902 struct addrinfo hints, *ai, *aitop;
3903 char strport[NI_MAXSERV];
3904 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003905
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003906 if (chanids == NULL)
3907 return -1;
3908
Damien Millera34a28b1999-12-14 10:47:15 +11003909 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11003910 display_number < MAX_DISPLAYS;
3911 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11003912 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11003913 memset(&hints, 0, sizeof(hints));
3914 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11003915 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11003916 hints.ai_socktype = SOCK_STREAM;
3917 snprintf(strport, sizeof strport, "%d", port);
3918 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003919 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00003920 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003921 }
Damien Miller34132e52000-01-14 15:45:46 +11003922 for (ai = aitop; ai; ai = ai->ai_next) {
3923 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3924 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11003925 sock = socket(ai->ai_family, ai->ai_socktype,
3926 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003927 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11003928 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
3929#ifdef EPFNOSUPPORT
3930 && (errno != EPFNOSUPPORT)
3931#endif
3932 ) {
Damien Millere2192732000-01-17 13:22:55 +11003933 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10003934 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00003935 return -1;
Damien Millere2192732000-01-17 13:22:55 +11003936 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11003937 debug("x11_create_display_inet: Socket family %d not supported",
3938 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11003939 continue;
3940 }
Damien Miller34132e52000-01-14 15:45:46 +11003941 }
Damien Miller04ee0f82009-11-18 17:48:30 +11003942 if (ai->ai_family == AF_INET6)
3943 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10003944 if (x11_use_localhost)
3945 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11003946 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003947 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003948 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11003949
Damien Miller34132e52000-01-14 15:45:46 +11003950 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11003951 close(socks[n]);
3952 }
3953 num_socks = 0;
3954 break;
3955 }
3956 socks[num_socks++] = sock;
3957 if (num_socks == NUM_SOCKS)
3958 break;
Damien Miller95def091999-11-25 00:26:21 +11003959 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00003960 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11003961 if (num_socks > 0)
3962 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003963 }
Damien Miller95def091999-11-25 00:26:21 +11003964 if (display_number >= MAX_DISPLAYS) {
3965 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00003966 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003967 }
Damien Miller95def091999-11-25 00:26:21 +11003968 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11003969 for (n = 0; n < num_socks; n++) {
3970 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11003971 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003972 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003973 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00003974 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11003975 }
Damien Miller95def091999-11-25 00:26:21 +11003976 }
Damien Miller34132e52000-01-14 15:45:46 +11003977
Damien Miller34132e52000-01-14 15:45:46 +11003978 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11003979 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11003980 for (n = 0; n < num_socks; n++) {
3981 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11003982 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10003983 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3984 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003985 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11003986 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003987 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11003988 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003989 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003990
Kevin Steves366298c2001-12-19 17:58:01 +00003991 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003992 *display_numberp = display_number;
3993 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003994}
3995
Ben Lindstrombba81212001-06-25 05:01:22 +00003996static int
Damien Miller819dbb62009-01-21 16:46:26 +11003997connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003998{
Damien Miller95def091999-11-25 00:26:21 +11003999 int sock;
4000 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004001
Damien Miller3afe3752001-12-21 12:39:51 +11004002 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4003 if (sock < 0)
4004 error("socket: %.100s", strerror(errno));
4005 memset(&addr, 0, sizeof(addr));
4006 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004007 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004008 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004009 return sock;
4010 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004011 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4012 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004013}
4014
Damien Miller819dbb62009-01-21 16:46:26 +11004015static int
4016connect_local_xsocket(u_int dnr)
4017{
4018 char buf[1024];
4019 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4020 return connect_local_xsocket_path(buf);
4021}
4022
Damien Millerbd483e72000-04-30 10:00:53 +10004023int
4024x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004025{
Damien Miller57c4e872006-03-31 23:11:07 +11004026 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004027 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004028 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004029 struct addrinfo hints, *ai, *aitop;
4030 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004031 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004032
Damien Miller95def091999-11-25 00:26:21 +11004033 /* Try to open a socket for the local X server. */
4034 display = getenv("DISPLAY");
4035 if (!display) {
4036 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004037 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004038 }
Damien Miller5428f641999-11-25 11:54:57 +11004039 /*
4040 * Now we decode the value of the DISPLAY variable and make a
4041 * connection to the real X server.
4042 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004043
Damien Miller819dbb62009-01-21 16:46:26 +11004044 /* Check if the display is from launchd. */
4045#ifdef __APPLE__
4046 if (strncmp(display, "/tmp/launch", 11) == 0) {
4047 sock = connect_local_xsocket_path(display);
4048 if (sock < 0)
4049 return -1;
4050
4051 /* OK, we now have a connection to the display. */
4052 return sock;
4053 }
4054#endif
Damien Miller5428f641999-11-25 11:54:57 +11004055 /*
4056 * Check if it is a unix domain socket. Unix domain displays are in
4057 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4058 */
Damien Miller95def091999-11-25 00:26:21 +11004059 if (strncmp(display, "unix:", 5) == 0 ||
4060 display[0] == ':') {
4061 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004062 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004063 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004064 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004065 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004066 }
4067 /* Create a socket. */
4068 sock = connect_local_xsocket(display_number);
4069 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004070 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004071
4072 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004073 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004074 }
Damien Miller5428f641999-11-25 11:54:57 +11004075 /*
4076 * Connect to an inet socket. The DISPLAY value is supposedly
4077 * hostname:d[.s], where hostname may also be numeric IP address.
4078 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004079 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004080 cp = strchr(buf, ':');
4081 if (!cp) {
4082 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004083 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004084 }
Damien Miller95def091999-11-25 00:26:21 +11004085 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004086 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004087 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004088 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004089 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004090 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004091 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004092
Damien Miller34132e52000-01-14 15:45:46 +11004093 /* Look up the host address */
4094 memset(&hints, 0, sizeof(hints));
4095 hints.ai_family = IPv4or6;
4096 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004097 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004098 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004099 error("%.100s: unknown host. (%s)", buf,
4100 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004101 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004102 }
Damien Miller34132e52000-01-14 15:45:46 +11004103 for (ai = aitop; ai; ai = ai->ai_next) {
4104 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004105 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004106 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004107 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004108 continue;
4109 }
4110 /* Connect it to the display. */
4111 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004112 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004113 6000 + display_number, strerror(errno));
4114 close(sock);
4115 continue;
4116 }
4117 /* Success */
4118 break;
Damien Miller34132e52000-01-14 15:45:46 +11004119 }
Damien Miller34132e52000-01-14 15:45:46 +11004120 freeaddrinfo(aitop);
4121 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004122 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004123 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004124 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004125 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004126 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004127 return sock;
4128}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004129
Damien Millerbd483e72000-04-30 10:00:53 +10004130/*
4131 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
4132 * the remote channel number. We should do whatever we want, and respond
4133 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
4134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004135
Damien Miller8473dd82006-07-24 14:08:32 +10004136/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004137int
Damien Miller630d6f42002-01-22 23:17:30 +11004138x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10004139{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004140 Channel *c = NULL;
4141 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10004142 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10004143
4144 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004145
4146 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00004147
4148 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004149 remote_host = packet_get_string(NULL);
4150 } else {
4151 remote_host = xstrdup("unknown (remote did not supply name)");
4152 }
Damien Miller48b03fc2002-01-22 23:11:40 +11004153 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10004154
4155 /* Obtain a connection to the real X display. */
4156 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004157 if (sock != -1) {
4158 /* Allocate a channel for this connection. */
4159 c = channel_new("connected x11 socket",
4160 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
4161 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11004162 c->remote_id = remote_id;
4163 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004164 }
Darren Tuckera627d422013-06-02 07:31:17 +10004165 free(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004166 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10004167 /* Send refusal to the remote host. */
4168 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004169 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10004170 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10004171 /* Send a confirmation to the remote host. */
4172 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004173 packet_put_int(remote_id);
4174 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10004175 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004176 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004177 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004178}
4179
Damien Miller69b69aa2000-10-28 14:19:58 +11004180/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10004181/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004182int
Damien Miller630d6f42002-01-22 23:17:30 +11004183deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11004184{
4185 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00004186
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00004187 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11004188 case SSH_SMSG_AGENT_OPEN:
4189 error("Warning: ssh server tried agent forwarding.");
4190 break;
4191 case SSH_SMSG_X11_OPEN:
4192 error("Warning: ssh server tried X11 forwarding.");
4193 break;
4194 default:
Damien Miller630d6f42002-01-22 23:17:30 +11004195 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11004196 break;
4197 }
Damien Miller5eb137c2005-12-31 16:19:53 +11004198 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11004199 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
4200 packet_put_int(rchan);
4201 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004202 return 0;
Damien Miller69b69aa2000-10-28 14:19:58 +11004203}
4204
Damien Miller5428f641999-11-25 11:54:57 +11004205/*
4206 * Requests forwarding of X11 connections, generates fake authentication
4207 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004208 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004209 */
Damien Miller4af51302000-04-16 11:18:38 +10004210void
Damien Miller17e7ed02005-06-17 12:54:33 +10004211x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004212 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004213{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004214 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004215 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004216 char *new_data;
4217 int screen_number;
4218 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10004219 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004220
Damien Millerf92c0792005-07-06 09:45:26 +10004221 if (x11_saved_display == NULL)
4222 x11_saved_display = xstrdup(disp);
4223 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004224 error("x11_request_forwarding_with_spoofing: different "
4225 "$DISPLAY already forwarded");
4226 return;
4227 }
4228
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004229 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004230 if (cp)
4231 cp = strchr(cp, '.');
4232 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004233 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004234 else
4235 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004236
Damien Miller13390022005-07-06 09:44:19 +10004237 if (x11_saved_proto == NULL) {
4238 /* Save protocol name. */
4239 x11_saved_proto = xstrdup(proto);
4240 /*
Damien Miller46d38de2005-07-17 17:02:09 +10004241 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10004242 * of the same length.
4243 */
4244 x11_saved_data = xmalloc(data_len);
4245 x11_fake_data = xmalloc(data_len);
4246 for (i = 0; i < data_len; i++) {
4247 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4248 fatal("x11_request_forwarding: bad "
4249 "authentication data: %.100s", data);
4250 if (i % 4 == 0)
4251 rnd = arc4random();
4252 x11_saved_data[i] = value;
4253 x11_fake_data[i] = rnd & 0xff;
4254 rnd >>= 8;
4255 }
4256 x11_saved_data_len = data_len;
4257 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004258 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004259
Damien Miller95def091999-11-25 00:26:21 +11004260 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004261 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004262
Damien Miller95def091999-11-25 00:26:21 +11004263 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10004264 if (compat20) {
Damien Miller6d7b4372011-06-23 08:31:57 +10004265 channel_request_start(client_session_id, "x11-req", want_reply);
Damien Millerbd483e72000-04-30 10:00:53 +10004266 packet_put_char(0); /* XXX bool single connection */
4267 } else {
4268 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
4269 }
4270 packet_put_cstring(proto);
4271 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004272 packet_put_int(screen_number);
4273 packet_send();
4274 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004275 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004276}
4277
Ben Lindstrome9c99912001-06-09 00:41:05 +00004278
4279/* -- agent forwarding */
4280
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004281/* Sends a message to the server to request authentication fd forwarding. */
4282
Damien Miller4af51302000-04-16 11:18:38 +10004283void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00004284auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004285{
Damien Miller95def091999-11-25 00:26:21 +11004286 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
4287 packet_send();
4288 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004289}