blob: 2fedaf89d91a363dad213bb75440549ec165e334 [file] [log] [blame]
deraadt@openbsd.org087266e2015-01-20 23:14:00 +00001/* $OpenBSD: channels.c,v 1.340 2015/01/20 23:14:00 deraadt 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>
Damien Millera7a73ee2006-08-05 11:37:59 +100060#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110063#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100064#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000065#include <limits.h>
Damien Millerd7834352006-08-05 12:39:39 +100066#include <stdarg.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Millerb84886b2008-05-19 15:05:07 +100068#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000071#include "ssh1.h"
72#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "log.h"
75#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100076#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000079#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100080#include "key.h"
81#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110082#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Ben Lindstrome9c99912001-06-09 00:41:05 +000084/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller5428f641999-11-25 11:54:57 +110086/*
87 * Pointer to an array containing all allocated channels. The array is
88 * dynamically extended as needed.
89 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000090static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller5428f641999-11-25 11:54:57 +110092/*
93 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000094 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110095 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100096static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller5428f641999-11-25 11:54:57 +110098/*
99 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000100 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100101 */
Damien Miller5e953212001-01-30 09:14:00 +1100102static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Ben Lindstrome9c99912001-06-09 00:41:05 +0000105/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Data structure for storing which hosts are permitted for forward requests.
109 * The local sides of any remote forwards are stored in this array to prevent
110 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
111 * network (which might be behind a firewall).
112 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000113/* XXX: streamlocal wants a path instead of host:port */
114/* Overload host_to_connect; we could just make this match Forward */
115/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100116typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000117 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000118 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000119 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000120 char *listen_path; /* Remote side should listen path. */
121 int listen_port; /* Remote side should listen port. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122} ForwardPermission;
123
Damien Miller9b439df2006-07-24 14:04:00 +1000124/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000125static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000126
Damien Miller9b439df2006-07-24 14:04:00 +1000127/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000128static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000129
130/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000132
133/* Number of permitted host/port pair in the array permitted by the admin. */
134static int num_adm_permitted_opens = 0;
135
Darren Tucker1338b9e2011-10-02 18:57:35 +1100136/* special-case port number meaning allow any port */
137#define FWD_PERMIT_ANY_PORT 0
138
Damien Miller5428f641999-11-25 11:54:57 +1100139/*
140 * If this is true, all opens are permitted. This is the case on the server
141 * on which we have to trust the client anyway, and the user could do
142 * anything after logging in anyway.
143 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144static int all_opens_permitted = 0;
145
Ben Lindstrome9c99912001-06-09 00:41:05 +0000146
147/* -- X11 forwarding */
148
149/* Maximum number of fake X11 displays to try. */
150#define MAX_DISPLAYS 1000
151
Damien Miller13390022005-07-06 09:44:19 +1000152/* Saved X11 local (client) display. */
153static char *x11_saved_display = NULL;
154
Ben Lindstrome9c99912001-06-09 00:41:05 +0000155/* Saved X11 authentication protocol name. */
156static char *x11_saved_proto = NULL;
157
158/* Saved X11 authentication data. This is the real data. */
159static char *x11_saved_data = NULL;
160static u_int x11_saved_data_len = 0;
161
162/*
163 * Fake X11 authentication data. This is what the server will be sending us;
164 * we should replace any occurrences of this by the real data.
165 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100166static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000167static u_int x11_fake_data_len;
168
169
170/* -- agent forwarding */
171
172#define NUM_SOCKS 10
173
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000174/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000175static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000176
Ben Lindstrome9c99912001-06-09 00:41:05 +0000177/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000178static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000179
Damien Millerbd740252008-05-19 15:37:09 +1000180/* non-blocking connect helpers */
181static int connect_next(struct channel_connect *);
182static void channel_connect_ctx_free(struct channel_connect *);
183
Ben Lindstrome9c99912001-06-09 00:41:05 +0000184/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000185
186Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100187channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000188{
189 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000190
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000191 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100192 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000193 return NULL;
194 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000195 c = channels[id];
196 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100197 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000198 return NULL;
199 }
200 return c;
201}
202
Damien Miller5428f641999-11-25 11:54:57 +1100203/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100204 * Returns the channel if it is allowed to receive protocol messages.
205 * Private channels, like listening sockets, may not receive messages.
206 */
207Channel *
208channel_lookup(int id)
209{
210 Channel *c;
211
212 if ((c = channel_by_id(id)) == NULL)
213 return (NULL);
214
Damien Millerd62f2ca2006-03-26 13:57:41 +1100215 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100216 case SSH_CHANNEL_X11_OPEN:
217 case SSH_CHANNEL_LARVAL:
218 case SSH_CHANNEL_CONNECTING:
219 case SSH_CHANNEL_DYNAMIC:
220 case SSH_CHANNEL_OPENING:
221 case SSH_CHANNEL_OPEN:
222 case SSH_CHANNEL_INPUT_DRAINING:
223 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller36187092013-06-10 13:07:11 +1000224 case SSH_CHANNEL_ABANDONED:
Damien Millerd47c62a2005-12-13 19:33:57 +1100225 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100226 }
227 logit("Non-public channel %d, type %d.", id, c->type);
228 return (NULL);
229}
230
231/*
Damien Millere247cc42000-05-07 12:03:14 +1000232 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000233 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100234 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100236channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000237 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Damien Miller95def091999-11-25 00:26:21 +1100239 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100240 channel_max_fd = MAX(channel_max_fd, rfd);
241 channel_max_fd = MAX(channel_max_fd, wfd);
242 channel_max_fd = MAX(channel_max_fd, efd);
243
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100244 if (rfd != -1)
245 fcntl(rfd, F_SETFD, FD_CLOEXEC);
246 if (wfd != -1 && wfd != rfd)
247 fcntl(wfd, F_SETFD, FD_CLOEXEC);
248 if (efd != -1 && efd != rfd && efd != wfd)
249 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000250
Damien Miller6f83b8e2000-05-02 09:23:45 +1000251 c->rfd = rfd;
252 c->wfd = wfd;
253 c->sock = (rfd == wfd) ? rfd : -1;
254 c->efd = efd;
255 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100256
Darren Tuckered3cdc02008-06-16 23:29:18 +1000257 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000258 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000259#ifdef _AIX
260 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000261 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000262#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100263
Damien Miller69b69aa2000-10-28 14:19:58 +1100264 /* enable nonblocking mode */
265 if (nonblock) {
266 if (rfd != -1)
267 set_nonblock(rfd);
268 if (wfd != -1)
269 set_nonblock(wfd);
270 if (efd != -1)
271 set_nonblock(efd);
272 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000273}
274
275/*
276 * Allocate a new channel object and set its type and socket. This will cause
277 * remote_name to be freed.
278 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000279Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000280channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000281 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000282{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000283 int found;
284 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000285 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Miller95def091999-11-25 00:26:21 +1100287 /* Do initial allocation if this is the first call. */
288 if (channels_alloc == 0) {
289 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100290 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100291 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000292 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100293 }
294 /* Try to find a free slot where to put the new channel. */
295 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000296 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100297 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000298 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100299 break;
300 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000301 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100302 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100303 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000304 if (channels_alloc > 10000)
305 fatal("channel_new: internal error: channels_alloc %d "
306 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100307 channels = xrealloc(channels, channels_alloc + 10,
308 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000309 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100310 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100311 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000312 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100313 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000314 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100315 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100316 buffer_init(&c->input);
317 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000318 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100319 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000320 c->listening_addr = NULL;
321 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100322 c->ostate = CHAN_OUTPUT_OPEN;
323 c->istate = CHAN_INPUT_OPEN;
324 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000325 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000326 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100327 c->self = found;
328 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000329 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000330 c->local_window = window;
331 c->local_window_max = window;
332 c->local_consumed = 0;
333 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100334 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000335 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000336 c->remote_window = 0;
337 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000338 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100339 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000340 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100341 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000342 c->open_confirm = NULL;
343 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000344 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100345 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000346 c->filter_ctx = NULL;
347 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100348 c->ctl_chan = -1;
349 c->mux_rcb = NULL;
350 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000351 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100352 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000353 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100354 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000355 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000357
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000358static int
359channel_find_maxfd(void)
360{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000361 u_int i;
362 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000363 Channel *c;
364
365 for (i = 0; i < channels_alloc; i++) {
366 c = channels[i];
367 if (c != NULL) {
368 max = MAX(max, c->rfd);
369 max = MAX(max, c->wfd);
370 max = MAX(max, c->efd);
371 }
372 }
373 return max;
374}
375
376int
377channel_close_fd(int *fdp)
378{
379 int ret = 0, fd = *fdp;
380
381 if (fd != -1) {
382 ret = close(fd);
383 *fdp = -1;
384 if (fd == channel_max_fd)
385 channel_max_fd = channel_find_maxfd();
386 }
387 return ret;
388}
389
Damien Miller6f83b8e2000-05-02 09:23:45 +1000390/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000391static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000392channel_close_fds(Channel *c)
393{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000394 channel_close_fd(&c->sock);
395 channel_close_fd(&c->rfd);
396 channel_close_fd(&c->wfd);
397 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000398}
399
400/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000401void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000402channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000404 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000405 u_int i, n;
Damien Millerb84886b2008-05-19 15:05:07 +1000406 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000407
408 for (n = 0, i = 0; i < channels_alloc; i++)
409 if (channels[i])
410 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000411 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000412 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000413
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000414 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000415 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000416 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000417
Damien Miller6f83b8e2000-05-02 09:23:45 +1000418 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000419 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000420 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000421 buffer_free(&c->input);
422 buffer_free(&c->output);
423 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000424 free(c->remote_name);
425 c->remote_name = NULL;
426 free(c->path);
427 c->path = NULL;
428 free(c->listening_addr);
429 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000430 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
431 if (cc->abandon_cb != NULL)
432 cc->abandon_cb(c, cc->ctx);
433 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100434 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000435 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000436 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000437 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
438 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000439 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000440 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441}
442
Ben Lindstrome9c99912001-06-09 00:41:05 +0000443void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000444channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000445{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000446 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000447
Ben Lindstrom601e4362001-06-21 03:19:23 +0000448 for (i = 0; i < channels_alloc; i++)
449 if (channels[i] != NULL)
450 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000451}
452
453/*
454 * Closes the sockets/fds of all channels. This is used to close extra file
455 * descriptors after a fork.
456 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000457void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000458channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000459{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000460 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000461
462 for (i = 0; i < channels_alloc; i++)
463 if (channels[i] != NULL)
464 channel_close_fds(channels[i]);
465}
466
467/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000468 * Stop listening to channels.
469 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000470void
471channel_stop_listening(void)
472{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000473 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000474 Channel *c;
475
476 for (i = 0; i < channels_alloc; i++) {
477 c = channels[i];
478 if (c != NULL) {
479 switch (c->type) {
480 case SSH_CHANNEL_AUTH_SOCKET:
481 case SSH_CHANNEL_PORT_LISTENER:
482 case SSH_CHANNEL_RPORT_LISTENER:
483 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000484 case SSH_CHANNEL_UNIX_LISTENER:
485 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000486 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000487 channel_free(c);
488 break;
489 }
490 }
491 }
492}
493
494/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000495 * Returns true if no channel has too much buffered data, and false if one or
496 * more channel is overfull.
497 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000498int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000499channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000500{
501 u_int i;
502 Channel *c;
503
504 for (i = 0; i < channels_alloc; i++) {
505 c = channels[i];
506 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000507#if 0
508 if (!compat20 &&
509 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100510 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000511 c->self, buffer_len(&c->input));
512 return 0;
513 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000514#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000515 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000516 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000517 c->self, buffer_len(&c->output),
518 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000519 return 0;
520 }
521 }
522 }
523 return 1;
524}
525
526/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000527int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000528channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000529{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000530 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000531 Channel *c;
532
533 for (i = 0; i < channels_alloc; i++) {
534 c = channels[i];
535 if (c == NULL)
536 continue;
537 switch (c->type) {
538 case SSH_CHANNEL_X11_LISTENER:
539 case SSH_CHANNEL_PORT_LISTENER:
540 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100541 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000542 case SSH_CHANNEL_CLOSED:
543 case SSH_CHANNEL_AUTH_SOCKET:
544 case SSH_CHANNEL_DYNAMIC:
545 case SSH_CHANNEL_CONNECTING:
546 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000547 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000548 case SSH_CHANNEL_UNIX_LISTENER:
549 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000550 continue;
551 case SSH_CHANNEL_LARVAL:
552 if (!compat20)
553 fatal("cannot happen: SSH_CHANNEL_LARVAL");
554 continue;
555 case SSH_CHANNEL_OPENING:
556 case SSH_CHANNEL_OPEN:
557 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100558 case SSH_CHANNEL_MUX_CLIENT:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000559 return 1;
560 case SSH_CHANNEL_INPUT_DRAINING:
561 case SSH_CHANNEL_OUTPUT_DRAINING:
562 if (!compat13)
563 fatal("cannot happen: OUT_DRAIN");
564 return 1;
565 default:
566 fatal("channel_still_open: bad channel type %d", c->type);
567 /* NOTREACHED */
568 }
569 }
570 return 0;
571}
572
573/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000574int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000575channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000576{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000577 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578 Channel *c;
579
580 for (i = 0; i < channels_alloc; i++) {
581 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000582 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000583 continue;
584 switch (c->type) {
585 case SSH_CHANNEL_CLOSED:
586 case SSH_CHANNEL_DYNAMIC:
587 case SSH_CHANNEL_X11_LISTENER:
588 case SSH_CHANNEL_PORT_LISTENER:
589 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100590 case SSH_CHANNEL_MUX_LISTENER:
591 case SSH_CHANNEL_MUX_CLIENT:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000592 case SSH_CHANNEL_OPENING:
593 case SSH_CHANNEL_CONNECTING:
594 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000595 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000596 case SSH_CHANNEL_UNIX_LISTENER:
597 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 continue;
599 case SSH_CHANNEL_LARVAL:
600 case SSH_CHANNEL_AUTH_SOCKET:
601 case SSH_CHANNEL_OPEN:
602 case SSH_CHANNEL_X11_OPEN:
603 return i;
604 case SSH_CHANNEL_INPUT_DRAINING:
605 case SSH_CHANNEL_OUTPUT_DRAINING:
606 if (!compat13)
607 fatal("cannot happen: OUT_DRAIN");
608 return i;
609 default:
610 fatal("channel_find_open: bad channel type %d", c->type);
611 /* NOTREACHED */
612 }
613 }
614 return -1;
615}
616
617
618/*
619 * Returns a message describing the currently open forwarded connections,
620 * suitable for sending to the client. The message contains crlf pairs for
621 * newlines.
622 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000623char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000624channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000625{
626 Buffer buffer;
627 Channel *c;
628 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000629 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000630
631 buffer_init(&buffer);
632 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
633 buffer_append(&buffer, buf, strlen(buf));
634 for (i = 0; i < channels_alloc; i++) {
635 c = channels[i];
636 if (c == NULL)
637 continue;
638 switch (c->type) {
639 case SSH_CHANNEL_X11_LISTENER:
640 case SSH_CHANNEL_PORT_LISTENER:
641 case SSH_CHANNEL_RPORT_LISTENER:
642 case SSH_CHANNEL_CLOSED:
643 case SSH_CHANNEL_AUTH_SOCKET:
644 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000645 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100646 case SSH_CHANNEL_MUX_CLIENT:
647 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000648 case SSH_CHANNEL_UNIX_LISTENER:
649 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650 continue;
651 case SSH_CHANNEL_LARVAL:
652 case SSH_CHANNEL_OPENING:
653 case SSH_CHANNEL_CONNECTING:
654 case SSH_CHANNEL_DYNAMIC:
655 case SSH_CHANNEL_OPEN:
656 case SSH_CHANNEL_X11_OPEN:
657 case SSH_CHANNEL_INPUT_DRAINING:
658 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000659 snprintf(buf, sizeof buf,
Damien Millere1537f92010-01-26 13:26:22 +1100660 " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661 c->self, c->remote_name,
662 c->type, c->remote_id,
663 c->istate, buffer_len(&c->input),
664 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100665 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666 buffer_append(&buffer, buf, strlen(buf));
667 continue;
668 default:
669 fatal("channel_open_message: bad channel type %d", c->type);
670 /* NOTREACHED */
671 }
672 }
673 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000674 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000675 buffer_free(&buffer);
676 return cp;
677}
678
679void
680channel_send_open(int id)
681{
682 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000683
Ben Lindstrome9c99912001-06-09 00:41:05 +0000684 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000685 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000686 return;
687 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000688 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000689 packet_start(SSH2_MSG_CHANNEL_OPEN);
690 packet_put_cstring(c->ctype);
691 packet_put_int(c->self);
692 packet_put_int(c->local_window);
693 packet_put_int(c->local_maxpacket);
694 packet_send();
695}
696
697void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000698channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000699{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000700 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000701
Ben Lindstrome9c99912001-06-09 00:41:05 +0000702 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000703 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000704 return;
705 }
Damien Miller0e220db2004-06-15 10:34:08 +1000706 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000707 packet_start(SSH2_MSG_CHANNEL_REQUEST);
708 packet_put_int(c->remote_id);
709 packet_put_cstring(service);
710 packet_put_char(wantconfirm);
711}
Damien Miller4f7becb2006-03-26 14:10:14 +1100712
Ben Lindstrome9c99912001-06-09 00:41:05 +0000713void
Damien Millerb84886b2008-05-19 15:05:07 +1000714channel_register_status_confirm(int id, channel_confirm_cb *cb,
715 channel_confirm_abandon_cb *abandon_cb, void *ctx)
716{
717 struct channel_confirm *cc;
718 Channel *c;
719
720 if ((c = channel_lookup(id)) == NULL)
721 fatal("channel_register_expect: %d: bad id", id);
722
Damien Miller6c81fee2013-11-08 12:19:55 +1100723 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000724 cc->cb = cb;
725 cc->abandon_cb = abandon_cb;
726 cc->ctx = ctx;
727 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
728}
729
730void
Damien Millerd530f5f2010-05-21 14:57:10 +1000731channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000732{
733 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000734
Ben Lindstrome9c99912001-06-09 00:41:05 +0000735 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100736 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000737 return;
738 }
Damien Millerb84886b2008-05-19 15:05:07 +1000739 c->open_confirm = fn;
740 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000741}
Damien Miller4f7becb2006-03-26 14:10:14 +1100742
Ben Lindstrome9c99912001-06-09 00:41:05 +0000743void
Damien Miller39eda6e2005-11-05 14:52:50 +1100744channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000745{
Damien Millerd47c62a2005-12-13 19:33:57 +1100746 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000747
Ben Lindstrome9c99912001-06-09 00:41:05 +0000748 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000749 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000750 return;
751 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000752 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100753 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000754}
Damien Miller4f7becb2006-03-26 14:10:14 +1100755
Ben Lindstrome9c99912001-06-09 00:41:05 +0000756void
757channel_cancel_cleanup(int id)
758{
Damien Millerd47c62a2005-12-13 19:33:57 +1100759 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000760
Ben Lindstrome9c99912001-06-09 00:41:05 +0000761 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000762 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000763 return;
764 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000765 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100766 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000767}
Damien Miller4f7becb2006-03-26 14:10:14 +1100768
Ben Lindstrome9c99912001-06-09 00:41:05 +0000769void
Damien Miller077b2382005-12-31 16:22:32 +1100770channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000771 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000772{
773 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000774
Ben Lindstrome9c99912001-06-09 00:41:05 +0000775 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000776 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000777 return;
778 }
Damien Miller077b2382005-12-31 16:22:32 +1100779 c->input_filter = ifn;
780 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000781 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000782 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000783}
784
785void
786channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000787 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000788{
789 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000790
Ben Lindstrome9c99912001-06-09 00:41:05 +0000791 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
792 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000793 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000794 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100795 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
797 packet_put_int(c->remote_id);
798 packet_put_int(c->local_window);
799 packet_send();
800}
801
Damien Miller5428f641999-11-25 11:54:57 +1100802/*
Damien Millerb38eff82000-04-01 11:09:21 +1000803 * 'channel_pre*' are called just before select() to add any bits relevant to
804 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100805 */
Damien Millerb38eff82000-04-01 11:09:21 +1000806/*
807 * 'channel_post*': perform any appropriate operations for channels which
808 * have events pending.
809 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100810typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000811chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
812chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
813
Damien Miller8473dd82006-07-24 14:08:32 +1000814/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000815static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100816channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000817{
818 FD_SET(c->sock, readset);
819}
820
Damien Miller8473dd82006-07-24 14:08:32 +1000821/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000822static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100823channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000824{
825 debug3("channel %d: waiting for connection", c->self);
826 FD_SET(c->sock, writeset);
827}
828
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100830channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000831{
832 if (buffer_len(&c->input) < packet_get_maxsize())
833 FD_SET(c->sock, readset);
834 if (buffer_len(&c->output) > 0)
835 FD_SET(c->sock, writeset);
836}
837
Ben Lindstrombba81212001-06-25 05:01:22 +0000838static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100839channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000840{
Damien Millerde6987c2002-01-22 23:20:40 +1100841 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000842
Damien Miller33b13562000-04-04 14:38:59 +1000843 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100844 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000845 buffer_len(&c->input) < limit &&
846 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000847 FD_SET(c->rfd, readset);
848 if (c->ostate == CHAN_OUTPUT_OPEN ||
849 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
850 if (buffer_len(&c->output) > 0) {
851 FD_SET(c->wfd, writeset);
852 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000853 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000854 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
855 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000856 else
857 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000858 }
859 }
860 /** XXX check close conditions, too */
Damien Millerd654dd22008-05-19 16:05:41 +1000861 if (compat20 && c->efd != -1 &&
862 !(c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000863 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
864 buffer_len(&c->extended) > 0)
865 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000866 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
867 (c->extended_usage == CHAN_EXTENDED_READ ||
868 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000869 buffer_len(&c->extended) < c->remote_window)
870 FD_SET(c->efd, readset);
871 }
Damien Miller0e220db2004-06-15 10:34:08 +1000872 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000873}
874
Damien Miller8473dd82006-07-24 14:08:32 +1000875/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000876static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100877channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000878{
879 if (buffer_len(&c->input) == 0) {
880 packet_start(SSH_MSG_CHANNEL_CLOSE);
881 packet_put_int(c->remote_id);
882 packet_send();
883 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000884 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000885 }
886}
887
Damien Miller8473dd82006-07-24 14:08:32 +1000888/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000889static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100890channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000891{
892 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000893 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000894 else
Damien Millerb38eff82000-04-01 11:09:21 +1000895 FD_SET(c->sock, writeset);
896}
897
898/*
899 * This is a special state for X11 authentication spoofing. An opened X11
900 * connection (when authentication spoofing is being done) remains in this
901 * state until the first packet has been completely read. The authentication
902 * data in that packet is then substituted by the real data if it matches the
903 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000904 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000905 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000906 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000907static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000908x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000909{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000910 u_char *ucp;
911 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000912
913 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000914 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000915 return 0;
916
917 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100918 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000919 if (ucp[0] == 0x42) { /* Byte order MSB first. */
920 proto_len = 256 * ucp[6] + ucp[7];
921 data_len = 256 * ucp[8] + ucp[9];
922 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
923 proto_len = ucp[6] + 256 * ucp[7];
924 data_len = ucp[8] + 256 * ucp[9];
925 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000926 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100927 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000928 return -1;
929 }
930
931 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000932 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000933 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
934 return 0;
935
936 /* Check if authentication protocol matches. */
937 if (proto_len != strlen(x11_saved_proto) ||
938 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000939 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000940 return -1;
941 }
942 /* Check if authentication data matches our fake data. */
943 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000944 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000945 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000946 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000947 return -1;
948 }
949 /* Check fake data length */
950 if (x11_fake_data_len != x11_saved_data_len) {
951 error("X11 fake_data_len %d != saved_data_len %d",
952 x11_fake_data_len, x11_saved_data_len);
953 return -1;
954 }
955 /*
956 * Received authentication protocol and data match
957 * our fake data. Substitute the fake data with real
958 * data.
959 */
960 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
961 x11_saved_data, x11_saved_data_len);
962 return 1;
963}
964
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100966channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000967{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000968 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000969
Damien Millerb38eff82000-04-01 11:09:21 +1000970 if (ret == 1) {
971 /* Start normal processing for the channel. */
972 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000973 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000974 } else if (ret == -1) {
975 /*
976 * We have received an X11 connection that has bad
977 * authentication information.
978 */
Damien Miller996acd22003-04-09 20:59:48 +1000979 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000980 buffer_clear(&c->input);
981 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000982 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000983 c->sock = -1;
984 c->type = SSH_CHANNEL_CLOSED;
985 packet_start(SSH_MSG_CHANNEL_CLOSE);
986 packet_put_int(c->remote_id);
987 packet_send();
988 }
989}
990
Ben Lindstrombba81212001-06-25 05:01:22 +0000991static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100992channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000993{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000994 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000995
996 /* c->force_drain = 1; */
997
Damien Millerb38eff82000-04-01 11:09:21 +1000998 if (ret == 1) {
999 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +11001000 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001001 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001002 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +10001003 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +11001004 chan_read_failed(c);
1005 buffer_clear(&c->input);
1006 chan_ibuf_empty(c);
1007 buffer_clear(&c->output);
1008 /* for proto v1, the peer will send an IEOF */
1009 if (compat20)
1010 chan_write_failed(c);
1011 else
1012 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +10001013 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001014 }
1015}
1016
Damien Millere1537f92010-01-26 13:26:22 +11001017static void
1018channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1019{
Damien Millerd530f5f2010-05-21 14:57:10 +10001020 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001021 buffer_check_alloc(&c->input, CHAN_RBUF))
1022 FD_SET(c->rfd, readset);
1023 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1024 /* clear buffer immediately (discard any partial packet) */
1025 buffer_clear(&c->input);
1026 chan_ibuf_empty(c);
1027 /* Start output drain. XXX just kill chan? */
1028 chan_rcvd_oclose(c);
1029 }
1030 if (c->ostate == CHAN_OUTPUT_OPEN ||
1031 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1032 if (buffer_len(&c->output) > 0)
1033 FD_SET(c->wfd, writeset);
1034 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1035 chan_obuf_empty(c);
1036 }
1037}
1038
Ben Lindstromb3921512001-04-11 15:57:50 +00001039/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001040/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001041static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001042channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001043{
Damien Millera10f5612002-09-12 09:49:15 +10001044 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001045 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001046 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001047 struct {
1048 u_int8_t version;
1049 u_int8_t command;
1050 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001051 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001052 } s4_req, s4_rsp;
1053
Ben Lindstromb3921512001-04-11 15:57:50 +00001054 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001055
1056 have = buffer_len(&c->input);
1057 len = sizeof(s4_req);
1058 if (have < len)
1059 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001060 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001061
1062 need = 1;
1063 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1064 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1065 debug2("channel %d: socks4a request", c->self);
1066 /* ... and needs an extra string (the hostname) */
1067 need = 2;
1068 }
1069 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001070 for (found = 0, i = len; i < have; i++) {
1071 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001072 found++;
1073 if (found == need)
1074 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001075 }
1076 if (i > 1024) {
1077 /* the peer is probably sending garbage */
1078 debug("channel %d: decode socks4: too long",
1079 c->self);
1080 return -1;
1081 }
1082 }
Damien Miller1781f532009-01-28 16:24:41 +11001083 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001084 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001085 buffer_get(&c->input, (char *)&s4_req.version, 1);
1086 buffer_get(&c->input, (char *)&s4_req.command, 1);
1087 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001088 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001089 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001090 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001091 if (memchr(p, '\0', have) == NULL)
1092 fatal("channel %d: decode socks4: user not nul terminated",
1093 c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001094 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001095 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001096 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001097 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001098 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001099 c->self, len, have);
1100 strlcpy(username, p, sizeof(username));
1101 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001102
Darren Tuckera627d422013-06-02 07:31:17 +10001103 free(c->path);
1104 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001105 if (need == 1) { /* SOCKS4: one string */
1106 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001107 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001108 } else { /* SOCKS4A: two strings */
1109 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001110 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001111 len = strlen(p);
1112 debug2("channel %d: decode socks4a: host %s/%d",
1113 c->self, p, len);
1114 len++; /* trailing '\0' */
1115 if (len > have)
1116 fatal("channel %d: decode socks4a: len %d > have %d",
1117 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001118 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001119 error("channel %d: hostname \"%.100s\" too long",
1120 c->self, p);
1121 return -1;
1122 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001123 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001124 buffer_consume(&c->input, len);
1125 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001126 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001127
Damien Millerfbdeece2003-09-02 22:52:31 +10001128 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001129 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001130
Ben Lindstromb3921512001-04-11 15:57:50 +00001131 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001132 debug("channel %d: cannot handle: %s cn %d",
1133 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001134 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001135 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001136 s4_rsp.version = 0; /* vn: 0 for reply */
1137 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001138 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001139 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001140 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001141 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001142}
1143
Darren Tucker46471c92003-07-03 13:55:19 +10001144/* try to decode a socks5 header */
1145#define SSH_SOCKS5_AUTHDONE 0x1000
1146#define SSH_SOCKS5_NOAUTH 0x00
1147#define SSH_SOCKS5_IPV4 0x01
1148#define SSH_SOCKS5_DOMAIN 0x03
1149#define SSH_SOCKS5_IPV6 0x04
1150#define SSH_SOCKS5_CONNECT 0x01
1151#define SSH_SOCKS5_SUCCESS 0x00
1152
Damien Miller8473dd82006-07-24 14:08:32 +10001153/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001154static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001155channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001156{
1157 struct {
1158 u_int8_t version;
1159 u_int8_t command;
1160 u_int8_t reserved;
1161 u_int8_t atyp;
1162 } s5_req, s5_rsp;
1163 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001164 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1165 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001166 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001167
1168 debug2("channel %d: decode socks5", c->self);
1169 p = buffer_ptr(&c->input);
1170 if (p[0] != 0x05)
1171 return -1;
1172 have = buffer_len(&c->input);
1173 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1174 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001175 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001176 return 0;
1177 nmethods = p[1];
1178 if (have < nmethods + 2)
1179 return 0;
1180 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001181 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001182 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001183 found = 1;
1184 break;
1185 }
1186 }
1187 if (!found) {
1188 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1189 c->self);
1190 return -1;
1191 }
1192 buffer_consume(&c->input, nmethods + 2);
1193 buffer_put_char(&c->output, 0x05); /* version */
1194 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1195 FD_SET(c->sock, writeset);
1196 c->flags |= SSH_SOCKS5_AUTHDONE;
1197 debug2("channel %d: socks5 auth done", c->self);
1198 return 0; /* need more */
1199 }
1200 debug2("channel %d: socks5 post auth", c->self);
1201 if (have < sizeof(s5_req)+1)
1202 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001203 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001204 if (s5_req.version != 0x05 ||
1205 s5_req.command != SSH_SOCKS5_CONNECT ||
1206 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001207 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001208 return -1;
1209 }
Darren Tucker47eede72005-03-14 23:08:12 +11001210 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001211 case SSH_SOCKS5_IPV4:
1212 addrlen = 4;
1213 af = AF_INET;
1214 break;
1215 case SSH_SOCKS5_DOMAIN:
1216 addrlen = p[sizeof(s5_req)];
1217 af = -1;
1218 break;
1219 case SSH_SOCKS5_IPV6:
1220 addrlen = 16;
1221 af = AF_INET6;
1222 break;
1223 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001224 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001225 return -1;
1226 }
Damien Miller0f077072006-07-10 22:21:02 +10001227 need = sizeof(s5_req) + addrlen + 2;
1228 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1229 need++;
1230 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001231 return 0;
1232 buffer_consume(&c->input, sizeof(s5_req));
1233 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1234 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001235 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001236 buffer_get(&c->input, (char *)&dest_port, 2);
1237 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001238 free(c->path);
1239 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001240 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001241 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001242 error("channel %d: dynamic request: socks5 hostname "
1243 "\"%.100s\" too long", c->self, dest_addr);
1244 return -1;
1245 }
1246 c->path = xstrdup(dest_addr);
1247 } else {
1248 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1249 return -1;
1250 c->path = xstrdup(ntop);
1251 }
Darren Tucker46471c92003-07-03 13:55:19 +10001252 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001253
Damien Millerfbdeece2003-09-02 22:52:31 +10001254 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001255 c->self, c->path, c->host_port, s5_req.command);
1256
1257 s5_rsp.version = 0x05;
1258 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1259 s5_rsp.reserved = 0; /* ignored */
1260 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001261 dest_port = 0; /* ignored */
1262
Damien Millera0fdce92006-03-26 14:28:50 +11001263 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001264 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001265 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001266 return 1;
1267}
1268
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001269Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001270channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1271 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001272{
1273 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001274
1275 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1276 port_to_connect);
1277
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001278 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1279 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1280 0, "stdio-forward", /*nonblock*/0);
1281
1282 c->path = xstrdup(host_to_connect);
1283 c->host_port = port_to_connect;
1284 c->listening_port = 0;
1285 c->force_drain = 1;
1286
1287 channel_register_fds(c, in, out, -1, 0, 1, 0);
1288 port_open_helper(c, "direct-tcpip");
1289
1290 return c;
1291}
1292
Ben Lindstromb3921512001-04-11 15:57:50 +00001293/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001294static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001295channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001296{
1297 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001298 u_int have;
1299 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001300
1301 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001302 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001303 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001304 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001305 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001306 /* need more */
1307 FD_SET(c->sock, readset);
1308 return;
1309 }
1310 /* try to guess the protocol */
1311 p = buffer_ptr(&c->input);
1312 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001313 case 0x04:
1314 ret = channel_decode_socks4(c, readset, writeset);
1315 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001316 case 0x05:
1317 ret = channel_decode_socks5(c, readset, writeset);
1318 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001319 default:
1320 ret = -1;
1321 break;
1322 }
1323 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001324 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001325 } else if (ret == 0) {
1326 debug2("channel %d: pre_dynamic: need more", c->self);
1327 /* need more */
1328 FD_SET(c->sock, readset);
1329 } else {
1330 /* switch to the next state */
1331 c->type = SSH_CHANNEL_OPENING;
1332 port_open_helper(c, "direct-tcpip");
1333 }
1334}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001335
Damien Millerb38eff82000-04-01 11:09:21 +10001336/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001337/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001338static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001339channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001340{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001341 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001342 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001343 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001344 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001345 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001346 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001347
1348 if (FD_ISSET(c->sock, readset)) {
1349 debug("X11 connection requested.");
1350 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001351 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001352 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001353 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001354 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001355 channel_close_fd(&c->sock);
1356 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001357 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001358 }
Damien Millerb38eff82000-04-01 11:09:21 +10001359 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001360 if (errno != EINTR && errno != EWOULDBLOCK &&
1361 errno != ECONNABORTED)
1362 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001363 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001364 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001365 return;
1366 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001367 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001368 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001369 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001370 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001371 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001372
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001373 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001374 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001375 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001376 if (compat20) {
1377 packet_start(SSH2_MSG_CHANNEL_OPEN);
1378 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001379 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001380 packet_put_int(nc->local_window_max);
1381 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001382 /* originator ipaddr and port */
1383 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001384 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001385 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001386 } else {
1387 packet_put_int(remote_port);
1388 }
Damien Millerbd483e72000-04-30 10:00:53 +10001389 packet_send();
1390 } else {
1391 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001392 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001393 if (packet_get_protocol_flags() &
1394 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001395 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001396 packet_send();
1397 }
Darren Tuckera627d422013-06-02 07:31:17 +10001398 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001399 }
1400}
1401
Ben Lindstrombba81212001-06-25 05:01:22 +00001402static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001403port_open_helper(Channel *c, char *rtype)
1404{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001405 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001406 char *local_ipaddr = get_local_ipaddr(c->sock);
Damien Miller0890dc82014-02-24 15:56:07 +11001407 int local_port = c->sock == -1 ? 65536 : get_sock_port(c->sock, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001408 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001409 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001410
Damien Millerd6369432010-02-02 17:02:07 +11001411 if (remote_port == -1) {
1412 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001413 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001414 remote_ipaddr = xstrdup("127.0.0.1");
1415 remote_port = 65535;
1416 }
1417
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001418 snprintf(buf, sizeof buf,
1419 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001420 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001421 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001422 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001423
Darren Tuckera627d422013-06-02 07:31:17 +10001424 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001425 c->remote_name = xstrdup(buf);
1426
1427 if (compat20) {
1428 packet_start(SSH2_MSG_CHANNEL_OPEN);
1429 packet_put_cstring(rtype);
1430 packet_put_int(c->self);
1431 packet_put_int(c->local_window_max);
1432 packet_put_int(c->local_maxpacket);
Damien Miller7acefbb2014-07-18 14:11:24 +10001433 if (strcmp(rtype, "direct-tcpip") == 0) {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001434 /* target host, port */
1435 packet_put_cstring(c->path);
1436 packet_put_int(c->host_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001437 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1438 /* target path */
1439 packet_put_cstring(c->path);
1440 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1441 /* listen path */
1442 packet_put_cstring(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001443 } else {
1444 /* listen address, port */
1445 packet_put_cstring(c->path);
Damien Miller4def1842013-12-29 17:45:26 +11001446 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001447 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001448 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1449 /* reserved for future owner/mode info */
1450 packet_put_cstring("");
1451 } else {
1452 /* originator host and port */
1453 packet_put_cstring(remote_ipaddr);
1454 packet_put_int((u_int)remote_port);
1455 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001456 packet_send();
1457 } else {
1458 packet_start(SSH_MSG_PORT_OPEN);
1459 packet_put_int(c->self);
1460 packet_put_cstring(c->path);
1461 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001462 if (packet_get_protocol_flags() &
1463 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001464 packet_put_cstring(c->remote_name);
1465 packet_send();
1466 }
Darren Tuckera627d422013-06-02 07:31:17 +10001467 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001468 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001469}
1470
Damien Miller5e7fd072005-11-05 14:53:39 +11001471static void
1472channel_set_reuseaddr(int fd)
1473{
1474 int on = 1;
1475
1476 /*
1477 * Set socket options.
1478 * Allow local port reuse in TIME_WAIT.
1479 */
1480 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1481 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1482}
1483
Damien Millerb38eff82000-04-01 11:09:21 +10001484/*
1485 * This socket is listening for connections to a forwarded TCP/IP port.
1486 */
Damien Miller8473dd82006-07-24 14:08:32 +10001487/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001488static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001489channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001490{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001491 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001492 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001493 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001494 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001495 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001496
Damien Millerb38eff82000-04-01 11:09:21 +10001497 if (FD_ISSET(c->sock, readset)) {
1498 debug("Connection to port %d forwarding "
1499 "to %.100s port %d requested.",
1500 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001501
Damien Miller4623a752001-10-10 15:03:58 +10001502 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1503 nextstate = SSH_CHANNEL_OPENING;
1504 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001505 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1506 nextstate = SSH_CHANNEL_OPENING;
1507 rtype = "forwarded-streamlocal@openssh.com";
1508 } else if (c->host_port == PORT_STREAMLOCAL) {
1509 nextstate = SSH_CHANNEL_OPENING;
1510 rtype = "direct-streamlocal@openssh.com";
1511 } else if (c->host_port == 0) {
1512 nextstate = SSH_CHANNEL_DYNAMIC;
1513 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001514 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001515 nextstate = SSH_CHANNEL_OPENING;
1516 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001517 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001518
Damien Millerb38eff82000-04-01 11:09:21 +10001519 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001520 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001521 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001522 if (errno != EINTR && errno != EWOULDBLOCK &&
1523 errno != ECONNABORTED)
1524 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001525 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001526 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001527 return;
1528 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001529 if (c->host_port != PORT_STREAMLOCAL)
1530 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001531 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1532 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001533 nc->listening_port = c->listening_port;
1534 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001535 if (c->path != NULL)
1536 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001537
Darren Tucker876045b2010-01-08 17:08:00 +11001538 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001539 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001540 }
1541}
1542
1543/*
1544 * This is the authentication agent socket listening for connections from
1545 * clients.
1546 */
Damien Miller8473dd82006-07-24 14:08:32 +10001547/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001548static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001549channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001550{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001551 Channel *nc;
1552 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001553 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001554 socklen_t addrlen;
1555
1556 if (FD_ISSET(c->sock, readset)) {
1557 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001558 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001559 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001560 error("accept from auth socket: %.100s",
1561 strerror(errno));
1562 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001563 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001564 return;
1565 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001566 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001567 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1568 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001569 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001570 if (compat20) {
1571 packet_start(SSH2_MSG_CHANNEL_OPEN);
1572 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001573 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001574 packet_put_int(c->local_window_max);
1575 packet_put_int(c->local_maxpacket);
1576 } else {
1577 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001578 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001579 }
Damien Millerb38eff82000-04-01 11:09:21 +10001580 packet_send();
1581 }
1582}
1583
Damien Miller8473dd82006-07-24 14:08:32 +10001584/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001585static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001586channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001587{
Damien Millerbd740252008-05-19 15:37:09 +10001588 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001589 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001590
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001591 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001592 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001593 err = errno;
1594 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001595 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001596 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001597 debug("channel %d: connected to %s port %d",
1598 c->self, c->connect_ctx.host, c->connect_ctx.port);
1599 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001600 c->type = SSH_CHANNEL_OPEN;
1601 if (compat20) {
1602 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1603 packet_put_int(c->remote_id);
1604 packet_put_int(c->self);
1605 packet_put_int(c->local_window);
1606 packet_put_int(c->local_maxpacket);
1607 } else {
1608 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1609 packet_put_int(c->remote_id);
1610 packet_put_int(c->self);
1611 }
1612 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001613 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001614 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001615 /* Try next address, if any */
1616 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1617 close(c->sock);
1618 c->sock = c->rfd = c->wfd = sock;
1619 channel_max_fd = channel_find_maxfd();
1620 return;
1621 }
1622 /* Exhausted all addresses */
1623 error("connect_to %.100s port %d: failed.",
1624 c->connect_ctx.host, c->connect_ctx.port);
1625 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001626 if (compat20) {
1627 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1628 packet_put_int(c->remote_id);
1629 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1630 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1631 packet_put_cstring(strerror(err));
1632 packet_put_cstring("");
1633 }
1634 } else {
1635 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1636 packet_put_int(c->remote_id);
1637 }
1638 chan_mark_dead(c);
1639 }
1640 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001641 }
1642}
1643
Damien Miller8473dd82006-07-24 14:08:32 +10001644/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001645static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001646channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001647{
Darren Tucker11327cc2005-03-14 23:22:25 +11001648 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001649 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001650
Damien Miller835284b2007-06-11 13:03:16 +10001651 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1652 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001653 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001654 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001655 if (len < 0 && (errno == EINTR ||
1656 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001657 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001658#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001659 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001660#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001661 if ((!c->isatty && len <= 0) ||
1662 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001663#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001664 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001665 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001666 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001667 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001668 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001669 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001670 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001671 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001672 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001673 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001674 } else {
1675 chan_read_failed(c);
1676 }
1677 return -1;
1678 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001679 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001680 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001681 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001682 chan_read_failed(c);
1683 }
Damien Millerd27b9472005-12-13 19:29:02 +11001684 } else if (c->datagram) {
1685 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001686 } else {
1687 buffer_append(&c->input, buf, len);
1688 }
Damien Millerb38eff82000-04-01 11:09:21 +10001689 }
1690 return 1;
1691}
Damien Miller4f7becb2006-03-26 14:10:14 +11001692
Damien Miller8473dd82006-07-24 14:08:32 +10001693/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001694static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001695channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001696{
Ben Lindstrome229b252001-03-05 06:28:06 +00001697 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001698 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001699 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001700 int len;
1701
1702 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001703 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001704 FD_ISSET(c->wfd, writeset) &&
1705 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001706 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001707 if (c->output_filter != NULL) {
1708 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1709 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001710 if (c->type != SSH_CHANNEL_OPEN)
1711 chan_mark_dead(c);
1712 else
1713 chan_write_failed(c);
1714 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001715 }
1716 } else if (c->datagram) {
1717 buf = data = buffer_get_string(&c->output, &dlen);
1718 } else {
1719 buf = data = buffer_ptr(&c->output);
1720 dlen = buffer_len(&c->output);
1721 }
1722
Damien Millerd27b9472005-12-13 19:29:02 +11001723 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001724 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001725 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001726 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001727 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1728 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001729 return 1;
1730 if (len <= 0) {
1731 if (c->type != SSH_CHANNEL_OPEN)
1732 chan_mark_dead(c);
1733 else
1734 chan_write_failed(c);
1735 return -1;
1736 }
Damien Miller7d457182010-08-05 23:09:48 +10001737 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001738 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001739#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001740 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001741 if (compat20 && c->wfd_isatty)
1742 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001743#endif
Damien Miller077b2382005-12-31 16:22:32 +11001744
1745 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001746 if (len < 0 &&
1747 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001748 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001749 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001750 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001751 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001752 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001753 return -1;
1754 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001755 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001756 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001757 c->type = SSH_CHANNEL_INPUT_DRAINING;
1758 } else {
1759 chan_write_failed(c);
1760 }
1761 return -1;
1762 }
Darren Tucker3980b632009-08-28 11:02:37 +10001763#ifndef BROKEN_TCGETATTR_ICANON
Damien Miller077b2382005-12-31 16:22:32 +11001764 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001765 if (tcgetattr(c->wfd, &tio) == 0 &&
1766 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1767 /*
1768 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001769 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001770 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001771 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001772 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001773 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001774 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001775 }
1776 }
Darren Tucker3980b632009-08-28 11:02:37 +10001777#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001778 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001779 }
Damien Miller7d457182010-08-05 23:09:48 +10001780 out:
1781 if (compat20 && olen > 0)
1782 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001783 return 1;
1784}
Damien Miller4f7becb2006-03-26 14:10:14 +11001785
Ben Lindstrombba81212001-06-25 05:01:22 +00001786static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001787channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001788{
Darren Tucker11327cc2005-03-14 23:22:25 +11001789 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001790 int len;
1791
Damien Miller1383bd82000-04-06 12:32:37 +10001792/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001793 if (c->efd != -1) {
1794 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1795 FD_ISSET(c->efd, writeset) &&
1796 buffer_len(&c->extended) > 0) {
1797 len = write(c->efd, buffer_ptr(&c->extended),
1798 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001799 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001800 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001801 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1802 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001803 return 1;
1804 if (len <= 0) {
1805 debug2("channel %d: closing write-efd %d",
1806 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001807 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001808 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001809 buffer_consume(&c->extended, len);
1810 c->local_consumed += len;
1811 }
Damien Miller8853ca52010-06-26 10:00:14 +10001812 } else if (c->efd != -1 &&
1813 (c->extended_usage == CHAN_EXTENDED_READ ||
1814 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001815 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001816 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001817 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001818 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001819 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1820 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001821 return 1;
1822 if (len <= 0) {
1823 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001824 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001825 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001826 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001827 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1828 debug3("channel %d: discard efd",
1829 c->self);
1830 } else
1831 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001832 }
Damien Miller33b13562000-04-04 14:38:59 +10001833 }
1834 }
1835 return 1;
1836}
Damien Miller4f7becb2006-03-26 14:10:14 +11001837
Damien Miller0e220db2004-06-15 10:34:08 +10001838static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001839channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001840{
Ben Lindstromb3921512001-04-11 15:57:50 +00001841 if (c->type == SSH_CHANNEL_OPEN &&
1842 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001843 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001844 c->local_maxpacket*3) ||
1845 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001846 c->local_consumed > 0) {
1847 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1848 packet_put_int(c->remote_id);
1849 packet_put_int(c->local_consumed);
1850 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001851 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001852 c->self, c->local_window,
1853 c->local_consumed);
1854 c->local_window += c->local_consumed;
1855 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001856 }
1857 return 1;
1858}
1859
Ben Lindstrombba81212001-06-25 05:01:22 +00001860static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001861channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001862{
1863 channel_handle_rfd(c, readset, writeset);
1864 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001865 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001866 return;
Damien Miller33b13562000-04-04 14:38:59 +10001867 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001868 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001869}
1870
Damien Millere1537f92010-01-26 13:26:22 +11001871static u_int
1872read_mux(Channel *c, u_int need)
1873{
1874 char buf[CHAN_RBUF];
1875 int len;
1876 u_int rlen;
1877
1878 if (buffer_len(&c->input) < need) {
1879 rlen = need - buffer_len(&c->input);
1880 len = read(c->rfd, buf, MIN(rlen, CHAN_RBUF));
1881 if (len <= 0) {
1882 if (errno != EINTR && errno != EAGAIN) {
1883 debug2("channel %d: ctl read<=0 rfd %d len %d",
1884 c->self, c->rfd, len);
1885 chan_read_failed(c);
1886 return 0;
1887 }
1888 } else
1889 buffer_append(&c->input, buf, len);
1890 }
1891 return buffer_len(&c->input);
1892}
1893
1894static void
1895channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1896{
1897 u_int need;
1898 ssize_t len;
1899
1900 if (!compat20)
1901 fatal("%s: entered with !compat20", __func__);
1902
Damien Millerd530f5f2010-05-21 14:57:10 +10001903 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001904 (c->istate == CHAN_INPUT_OPEN ||
1905 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1906 /*
1907 * Don't not read past the precise end of packets to
1908 * avoid disrupting fd passing.
1909 */
1910 if (read_mux(c, 4) < 4) /* read header */
1911 return;
1912 need = get_u32(buffer_ptr(&c->input));
1913#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1914 if (need > CHANNEL_MUX_MAX_PACKET) {
1915 debug2("channel %d: packet too big %u > %u",
1916 c->self, CHANNEL_MUX_MAX_PACKET, need);
1917 chan_rcvd_oclose(c);
1918 return;
1919 }
1920 if (read_mux(c, need + 4) < need + 4) /* read body */
1921 return;
1922 if (c->mux_rcb(c) != 0) {
1923 debug("channel %d: mux_rcb failed", c->self);
1924 chan_mark_dead(c);
1925 return;
1926 }
1927 }
1928
1929 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1930 buffer_len(&c->output) > 0) {
1931 len = write(c->wfd, buffer_ptr(&c->output),
1932 buffer_len(&c->output));
1933 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1934 return;
1935 if (len <= 0) {
1936 chan_mark_dead(c);
1937 return;
1938 }
1939 buffer_consume(&c->output, len);
1940 }
1941}
1942
1943static void
1944channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1945{
1946 Channel *nc;
1947 struct sockaddr_storage addr;
1948 socklen_t addrlen;
1949 int newsock;
1950 uid_t euid;
1951 gid_t egid;
1952
1953 if (!FD_ISSET(c->sock, readset))
1954 return;
1955
1956 debug("multiplexing control connection");
1957
1958 /*
1959 * Accept connection on control socket
1960 */
1961 memset(&addr, 0, sizeof(addr));
1962 addrlen = sizeof(addr);
1963 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1964 &addrlen)) == -1) {
1965 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001966 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001967 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001968 return;
1969 }
1970
1971 if (getpeereid(newsock, &euid, &egid) < 0) {
1972 error("%s getpeereid failed: %s", __func__,
1973 strerror(errno));
1974 close(newsock);
1975 return;
1976 }
1977 if ((euid != 0) && (getuid() != euid)) {
1978 error("multiplex uid mismatch: peer euid %u != uid %u",
1979 (u_int)euid, (u_int)getuid());
1980 close(newsock);
1981 return;
1982 }
1983 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
1984 newsock, newsock, -1, c->local_window_max,
1985 c->local_maxpacket, 0, "mux-control", 1);
1986 nc->mux_rcb = c->mux_rcb;
1987 debug3("%s: new mux channel %d fd %d", __func__,
1988 nc->self, nc->sock);
1989 /* establish state */
1990 nc->mux_rcb(nc);
1991 /* mux state transitions must not elicit protocol messages */
1992 nc->flags |= CHAN_LOCAL;
1993}
1994
Damien Miller8473dd82006-07-24 14:08:32 +10001995/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001996static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001997channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001998{
1999 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002000
Damien Millerb38eff82000-04-01 11:09:21 +10002001 /* Send buffered output data to the socket. */
2002 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
2003 len = write(c->sock, buffer_ptr(&c->output),
2004 buffer_len(&c->output));
2005 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11002006 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10002007 else
2008 buffer_consume(&c->output, len);
2009 }
2010}
2011
Ben Lindstrombba81212001-06-25 05:01:22 +00002012static void
Damien Miller33b13562000-04-04 14:38:59 +10002013channel_handler_init_20(void)
2014{
Damien Millerde6987c2002-01-22 23:20:40 +11002015 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002016 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10002017 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002018 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002019 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2020 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002021 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002022 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002023 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002024 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11002025 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2026 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002027
Damien Millerde6987c2002-01-22 23:20:40 +11002028 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10002029 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002030 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002031 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2032 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002033 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002034 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002035 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002036 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11002037 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2038 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002039}
2040
Ben Lindstrombba81212001-06-25 05:01:22 +00002041static void
Damien Millerb38eff82000-04-01 11:09:21 +10002042channel_handler_init_13(void)
2043{
2044 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
2045 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
2046 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2047 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2048 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2049 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
2050 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002051 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002052 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002053
Damien Millerde6987c2002-01-22 23:20:40 +11002054 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002055 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2056 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2057 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2058 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002059 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002060 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002061}
2062
Ben Lindstrombba81212001-06-25 05:01:22 +00002063static void
Damien Millerb38eff82000-04-01 11:09:21 +10002064channel_handler_init_15(void)
2065{
Damien Millerde6987c2002-01-22 23:20:40 +11002066 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002067 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002068 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2069 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2070 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002071 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002072 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002073
2074 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2075 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2076 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11002077 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002078 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002079 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002080}
2081
Ben Lindstrombba81212001-06-25 05:01:22 +00002082static void
Damien Millerb38eff82000-04-01 11:09:21 +10002083channel_handler_init(void)
2084{
2085 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002086
Damien Miller9f0f5c62001-12-21 14:45:46 +11002087 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10002088 channel_pre[i] = NULL;
2089 channel_post[i] = NULL;
2090 }
Damien Miller33b13562000-04-04 14:38:59 +10002091 if (compat20)
2092 channel_handler_init_20();
2093 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002094 channel_handler_init_13();
2095 else
2096 channel_handler_init_15();
2097}
2098
Damien Miller3ec27592001-10-12 11:35:04 +10002099/* gc dead channels */
2100static void
2101channel_garbage_collect(Channel *c)
2102{
2103 if (c == NULL)
2104 return;
2105 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11002106 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002107 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002108 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002109 c->detach_user(c->self, NULL);
2110 /* if we still have a callback */
2111 if (c->detach_user != NULL)
2112 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002113 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002114 }
2115 if (!chan_is_dead(c, 1))
2116 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002117 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002118 channel_free(c);
2119}
2120
Ben Lindstrombba81212001-06-25 05:01:22 +00002121static void
Damien Millera6508752012-04-22 11:21:10 +10002122channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2123 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002124{
2125 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002126 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002127 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002128 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002129
2130 if (!did_init) {
2131 channel_handler_init();
2132 did_init = 1;
2133 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002134 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002135 if (unpause_secs != NULL)
2136 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002137 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002138 c = channels[i];
2139 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002140 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002141 if (c->delayed) {
2142 if (ftab == channel_pre)
2143 c->delayed = 0;
2144 else
2145 continue;
2146 }
Damien Millera6508752012-04-22 11:21:10 +10002147 if (ftab[c->type] != NULL) {
2148 /*
2149 * Run handlers that are not paused.
2150 */
2151 if (c->notbefore <= now)
2152 (*ftab[c->type])(c, readset, writeset);
2153 else if (unpause_secs != NULL) {
2154 /*
2155 * Collect the time that the earliest
2156 * channel comes off pause.
2157 */
2158 debug3("%s: chan %d: skip for %d more seconds",
2159 __func__, c->self,
2160 (int)(c->notbefore - now));
2161 if (*unpause_secs == 0 ||
2162 (c->notbefore - now) < *unpause_secs)
2163 *unpause_secs = c->notbefore - now;
2164 }
2165 }
Damien Miller3ec27592001-10-12 11:35:04 +10002166 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002167 }
Damien Millera6508752012-04-22 11:21:10 +10002168 if (unpause_secs != NULL && *unpause_secs != 0)
2169 debug3("%s: first channel unpauses in %d seconds",
2170 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002171}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002172
Ben Lindstrome9c99912001-06-09 00:41:05 +00002173/*
2174 * Allocate/update select bitmasks and add any bits relevant to channels in
2175 * select bitmasks.
2176 */
Damien Miller4af51302000-04-16 11:18:38 +10002177void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002178channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002179 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002180{
Damien Miller36812092006-03-26 14:22:47 +11002181 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002182
2183 n = MAX(*maxfdp, channel_max_fd);
2184
Damien Miller36812092006-03-26 14:22:47 +11002185 nfdset = howmany(n+1, NFDBITS);
2186 /* Explicitly test here, because xrealloc isn't always called */
2187 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
2188 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2189 sz = nfdset * sizeof(fd_mask);
2190
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002191 /* perhaps check sz < nalloc/2 and shrink? */
2192 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11002193 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
2194 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002195 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002196 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002197 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002198 memset(*readsetp, 0, sz);
2199 memset(*writesetp, 0, sz);
2200
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002201 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002202 channel_handler(channel_pre, *readsetp, *writesetp,
2203 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002204}
2205
Ben Lindstrome9c99912001-06-09 00:41:05 +00002206/*
2207 * After select, perform any appropriate operations for channels which have
2208 * events pending.
2209 */
Damien Miller4af51302000-04-16 11:18:38 +10002210void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002211channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002212{
Damien Millera6508752012-04-22 11:21:10 +10002213 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002214}
2215
Ben Lindstrome9c99912001-06-09 00:41:05 +00002216
Damien Miller5e953212001-01-30 09:14:00 +11002217/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002218void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002219channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002220{
Damien Millerb38eff82000-04-01 11:09:21 +10002221 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002222 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002223
Damien Miller95def091999-11-25 00:26:21 +11002224 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002225 c = channels[i];
2226 if (c == NULL)
2227 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002228
Ben Lindstrome9c99912001-06-09 00:41:05 +00002229 /*
2230 * We are only interested in channels that can have buffered
2231 * incoming data.
2232 */
Damien Miller34132e52000-01-14 15:45:46 +11002233 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10002234 if (c->type != SSH_CHANNEL_OPEN &&
2235 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11002236 continue;
2237 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10002238 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002239 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002240 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002241 if (compat20 &&
2242 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002243 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002244 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002245 continue;
2246 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002247
Damien Miller95def091999-11-25 00:26:21 +11002248 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002249 if ((c->istate == CHAN_INPUT_OPEN ||
2250 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2251 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002252 if (c->datagram) {
2253 if (len > 0) {
2254 u_char *data;
2255 u_int dlen;
2256
2257 data = buffer_get_string(&c->input,
2258 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002259 if (dlen > c->remote_window ||
2260 dlen > c->remote_maxpacket) {
2261 debug("channel %d: datagram "
2262 "too big for channel",
2263 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002264 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002265 continue;
2266 }
Damien Millerd27b9472005-12-13 19:29:02 +11002267 packet_start(SSH2_MSG_CHANNEL_DATA);
2268 packet_put_int(c->remote_id);
2269 packet_put_string(data, dlen);
2270 packet_send();
2271 c->remote_window -= dlen + 4;
Darren Tuckera627d422013-06-02 07:31:17 +10002272 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002273 }
2274 continue;
2275 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002276 /*
2277 * Send some data for the other side over the secure
2278 * connection.
2279 */
Damien Miller33b13562000-04-04 14:38:59 +10002280 if (compat20) {
2281 if (len > c->remote_window)
2282 len = c->remote_window;
2283 if (len > c->remote_maxpacket)
2284 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11002285 } else {
Damien Miller33b13562000-04-04 14:38:59 +10002286 if (packet_is_interactive()) {
2287 if (len > 1024)
2288 len = 512;
2289 } else {
2290 /* Keep the packets at reasonable size. */
2291 if (len > packet_get_maxsize()/2)
2292 len = packet_get_maxsize()/2;
2293 }
Damien Miller95def091999-11-25 00:26:21 +11002294 }
Damien Millerb38eff82000-04-01 11:09:21 +10002295 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10002296 packet_start(compat20 ?
2297 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002298 packet_put_int(c->remote_id);
2299 packet_put_string(buffer_ptr(&c->input), len);
2300 packet_send();
2301 buffer_consume(&c->input, len);
2302 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002303 }
2304 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11002305 if (compat13)
2306 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11002307 /*
2308 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002309 * tell peer, that we will not send more data: send IEOF.
2310 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002311 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002312 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002313 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2314 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002315 else
2316 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002317 }
Damien Miller33b13562000-04-04 14:38:59 +10002318 /* Send extended data, i.e. stderr */
2319 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00002320 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002321 c->remote_window > 0 &&
2322 (len = buffer_len(&c->extended)) > 0 &&
2323 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002324 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002325 c->self, c->remote_window, buffer_len(&c->extended),
2326 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002327 if (len > c->remote_window)
2328 len = c->remote_window;
2329 if (len > c->remote_maxpacket)
2330 len = c->remote_maxpacket;
2331 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2332 packet_put_int(c->remote_id);
2333 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2334 packet_put_string(buffer_ptr(&c->extended), len);
2335 packet_send();
2336 buffer_consume(&c->extended, len);
2337 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002338 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002339 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002340 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002341}
2342
Ben Lindstrome9c99912001-06-09 00:41:05 +00002343
2344/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002345
2346/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002347int
Damien Miller630d6f42002-01-22 23:17:30 +11002348channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002349{
Damien Miller34132e52000-01-14 15:45:46 +11002350 int id;
Damien Miller633de332014-05-15 13:48:26 +10002351 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002352 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002353 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002354
Damien Miller95def091999-11-25 00:26:21 +11002355 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002356 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002357 c = channel_lookup(id);
2358 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002359 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002360
Damien Miller95def091999-11-25 00:26:21 +11002361 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002362 if (c->type != SSH_CHANNEL_OPEN &&
2363 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002364 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002365
Damien Miller95def091999-11-25 00:26:21 +11002366 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002367 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002368 win_len = data_len;
2369 if (c->datagram)
2370 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002371
Damien Millera04ad492004-01-21 11:02:09 +11002372 /*
2373 * Ignore data for protocol > 1.3 if output end is no longer open.
2374 * For protocol 2 the sending side is reducing its window as it sends
2375 * data, so we must 'fake' consumption of the data in order to ensure
2376 * that window updates are sent back. Otherwise the connection might
2377 * deadlock.
2378 */
2379 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2380 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002381 c->local_window -= win_len;
2382 c->local_consumed += win_len;
Damien Millera04ad492004-01-21 11:02:09 +11002383 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002384 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002385 }
2386
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002387 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002388 if (win_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002389 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller7d457182010-08-05 23:09:48 +10002390 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002391 }
Damien Miller7d457182010-08-05 23:09:48 +10002392 if (win_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002393 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller7d457182010-08-05 23:09:48 +10002394 c->self, win_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002395 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002396 }
Damien Miller7d457182010-08-05 23:09:48 +10002397 c->local_window -= win_len;
Damien Miller33b13562000-04-04 14:38:59 +10002398 }
Damien Millerd27b9472005-12-13 19:29:02 +11002399 if (c->datagram)
2400 buffer_put_string(&c->output, data, data_len);
2401 else
2402 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002403 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002404 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002405}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002406
Damien Millerd79b4242006-03-31 23:11:44 +11002407/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002408int
Damien Miller630d6f42002-01-22 23:17:30 +11002409channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002410{
2411 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002412 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002413 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002414 Channel *c;
2415
2416 /* Get the channel number and verify it. */
2417 id = packet_get_int();
2418 c = channel_lookup(id);
2419
2420 if (c == NULL)
2421 packet_disconnect("Received extended_data for bad channel %d.", id);
2422 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002423 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002424 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002425 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002426 if (c->flags & CHAN_EOF_RCVD) {
2427 if (datafellows & SSH_BUG_EXTEOF)
2428 debug("channel %d: accepting ext data after eof", id);
2429 else
2430 packet_disconnect("Received extended_data after EOF "
2431 "on channel %d.", id);
2432 }
Damien Miller33b13562000-04-04 14:38:59 +10002433 tcode = packet_get_int();
2434 if (c->efd == -1 ||
2435 c->extended_usage != CHAN_EXTENDED_WRITE ||
2436 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002437 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002438 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002439 }
2440 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002441 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002442 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002443 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002444 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002445 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002446 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002447 }
Damien Millerd3444942000-09-30 14:20:03 +11002448 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002449 c->local_window -= data_len;
2450 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002451 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002452 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002453}
2454
Damien Millerd79b4242006-03-31 23:11:44 +11002455/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002456int
Damien Miller630d6f42002-01-22 23:17:30 +11002457channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002458{
2459 int id;
2460 Channel *c;
2461
Damien Millerb38eff82000-04-01 11:09:21 +10002462 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002463 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002464 c = channel_lookup(id);
2465 if (c == NULL)
2466 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2467 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002468
2469 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002470 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002471 debug("channel %d: FORCE input drain", c->self);
2472 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002473 if (buffer_len(&c->input) == 0)
2474 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002475 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002476 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002477}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002478
Damien Millerd79b4242006-03-31 23:11:44 +11002479/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002480int
Damien Miller630d6f42002-01-22 23:17:30 +11002481channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002482{
Damien Millerb38eff82000-04-01 11:09:21 +10002483 int id;
2484 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002485
Damien Millerb38eff82000-04-01 11:09:21 +10002486 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002487 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002488 c = channel_lookup(id);
2489 if (c == NULL)
2490 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002491
2492 /*
2493 * Send a confirmation that we have closed the channel and no more
2494 * data is coming for it.
2495 */
Damien Miller95def091999-11-25 00:26:21 +11002496 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002497 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002498 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002499
Damien Miller5428f641999-11-25 11:54:57 +11002500 /*
2501 * If the channel is in closed state, we have sent a close request,
2502 * and the other side will eventually respond with a confirmation.
2503 * Thus, we cannot free the channel here, because then there would be
2504 * no-one to receive the confirmation. The channel gets freed when
2505 * the confirmation arrives.
2506 */
Damien Millerb38eff82000-04-01 11:09:21 +10002507 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002508 /*
2509 * Not a closed channel - mark it as draining, which will
2510 * cause it to be freed later.
2511 */
Damien Miller76765c02002-01-22 23:21:15 +11002512 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002513 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002514 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002515 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002516}
2517
Damien Millerb38eff82000-04-01 11:09:21 +10002518/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002519/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002520int
Damien Miller630d6f42002-01-22 23:17:30 +11002521channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002522{
Damien Millerb38eff82000-04-01 11:09:21 +10002523 int id = packet_get_int();
2524 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002525
Damien Miller48b03fc2002-01-22 23:11:40 +11002526 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002527 if (c == NULL)
2528 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2529 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002530 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002531}
2532
Damien Millerd79b4242006-03-31 23:11:44 +11002533/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002534int
Damien Miller630d6f42002-01-22 23:17:30 +11002535channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002536{
2537 int id = packet_get_int();
2538 Channel *c = channel_lookup(id);
2539
Damien Miller48b03fc2002-01-22 23:11:40 +11002540 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002541 if (c == NULL)
2542 packet_disconnect("Received close confirmation for "
2543 "out-of-range channel %d.", id);
Damien Miller36187092013-06-10 13:07:11 +10002544 if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
Damien Millerb38eff82000-04-01 11:09:21 +10002545 packet_disconnect("Received close confirmation for "
2546 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002547 channel_free(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002548 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002549}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002550
Damien Millerd79b4242006-03-31 23:11:44 +11002551/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002552int
Damien Miller630d6f42002-01-22 23:17:30 +11002553channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002554{
Damien Millerb38eff82000-04-01 11:09:21 +10002555 int id, remote_id;
2556 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002557
Damien Millerb38eff82000-04-01 11:09:21 +10002558 id = packet_get_int();
2559 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002560
Damien Millerb38eff82000-04-01 11:09:21 +10002561 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2562 packet_disconnect("Received open confirmation for "
2563 "non-opening channel %d.", id);
2564 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002565 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002566 c->remote_id = remote_id;
2567 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002568
2569 if (compat20) {
2570 c->remote_window = packet_get_int();
2571 c->remote_maxpacket = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002572 if (c->open_confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002573 debug2("callback start");
Damien Millerd530f5f2010-05-21 14:57:10 +10002574 c->open_confirm(c->self, 1, c->open_confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002575 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002576 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002577 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002578 c->remote_window, c->remote_maxpacket);
2579 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002580 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002581 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002582}
2583
Ben Lindstrombba81212001-06-25 05:01:22 +00002584static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002585reason2txt(int reason)
2586{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002587 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002588 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2589 return "administratively prohibited";
2590 case SSH2_OPEN_CONNECT_FAILED:
2591 return "connect failed";
2592 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2593 return "unknown channel type";
2594 case SSH2_OPEN_RESOURCE_SHORTAGE:
2595 return "resource shortage";
2596 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002597 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002598}
2599
Damien Millerd79b4242006-03-31 23:11:44 +11002600/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002601int
Damien Miller630d6f42002-01-22 23:17:30 +11002602channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002603{
Kevin Steves12057502001-02-05 14:54:34 +00002604 int id, reason;
2605 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002606 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002607
Damien Millerb38eff82000-04-01 11:09:21 +10002608 id = packet_get_int();
2609 c = channel_lookup(id);
2610
2611 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2612 packet_disconnect("Received open failure for "
2613 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002614 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002615 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002616 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002617 msg = packet_get_string(NULL);
2618 lang = packet_get_string(NULL);
2619 }
Damien Miller996acd22003-04-09 20:59:48 +10002620 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002621 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Darren Tuckera627d422013-06-02 07:31:17 +10002622 free(msg);
2623 free(lang);
Damien Millerd530f5f2010-05-21 14:57:10 +10002624 if (c->open_confirm) {
2625 debug2("callback start");
2626 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2627 debug2("callback done");
2628 }
Damien Miller33b13562000-04-04 14:38:59 +10002629 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002630 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002631 /* Schedule the channel for cleanup/deletion. */
2632 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002633 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002634}
2635
Damien Millerd79b4242006-03-31 23:11:44 +11002636/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002637int
Damien Miller630d6f42002-01-22 23:17:30 +11002638channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002639{
2640 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002641 int id;
2642 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002643
2644 if (!compat20)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002645 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002646
2647 /* Get the channel number and verify it. */
2648 id = packet_get_int();
2649 c = channel_lookup(id);
2650
Damien Millerd47c62a2005-12-13 19:33:57 +11002651 if (c == NULL) {
2652 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002653 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002654 }
2655 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002656 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002657 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002658 c->remote_window += adjust;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002659 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002660}
2661
Damien Millerd79b4242006-03-31 23:11:44 +11002662/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002663int
Damien Miller630d6f42002-01-22 23:17:30 +11002664channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002665{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002666 Channel *c = NULL;
2667 u_short host_port;
2668 char *host, *originator_string;
Damien Millerbd740252008-05-19 15:37:09 +10002669 int remote_id;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002670
Ben Lindstrome9c99912001-06-09 00:41:05 +00002671 remote_id = packet_get_int();
2672 host = packet_get_string(NULL);
2673 host_port = packet_get_int();
2674
2675 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2676 originator_string = packet_get_string(NULL);
2677 } else {
2678 originator_string = xstrdup("unknown (remote did not supply name)");
2679 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002680 packet_check_eom();
Damien Miller7acefbb2014-07-18 14:11:24 +10002681 c = channel_connect_to_port(host, host_port,
Damien Millerbd740252008-05-19 15:37:09 +10002682 "connected socket", originator_string);
Darren Tuckera627d422013-06-02 07:31:17 +10002683 free(originator_string);
2684 free(host);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002685 if (c == NULL) {
2686 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2687 packet_put_int(remote_id);
2688 packet_send();
Damien Millerbd740252008-05-19 15:37:09 +10002689 } else
2690 c->remote_id = remote_id;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002691 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002692}
2693
Damien Millerb84886b2008-05-19 15:05:07 +10002694/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002695int
Damien Millerb84886b2008-05-19 15:05:07 +10002696channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2697{
2698 Channel *c;
2699 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002700 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002701
2702 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002703 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002704
Damien Miller16a73072008-12-08 09:55:25 +11002705 id = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002706 packet_check_eom();
2707
Damien Miller16a73072008-12-08 09:55:25 +11002708 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002709
Damien Miller16a73072008-12-08 09:55:25 +11002710 if ((c = channel_lookup(id)) == NULL) {
2711 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002712 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002713 }
Damien Millerb84886b2008-05-19 15:05:07 +10002714 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002715 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002716 cc->cb(type, c, cc->ctx);
2717 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002718 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002719 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002720 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002721}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002722
Ben Lindstrome9c99912001-06-09 00:41:05 +00002723/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002724
Ben Lindstrom908afed2001-10-03 17:34:59 +00002725void
2726channel_set_af(int af)
2727{
2728 IPv4or6 = af;
2729}
2730
Damien Millerf6dff7c2011-09-22 21:38:52 +10002731
2732/*
2733 * Determine whether or not a port forward listens to loopback, the
2734 * specified address or wildcard. On the client, a specified bind
2735 * address will always override gateway_ports. On the server, a
2736 * gateway_ports of 1 (``yes'') will override the client's specification
2737 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2738 * will bind to whatever address the client asked for.
2739 *
2740 * Special-case listen_addrs are:
2741 *
2742 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2743 * "" (empty string), "*" -> wildcard v4/v6
2744 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002745 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002746 */
2747static const char *
2748channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002749 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002750{
2751 const char *addr = NULL;
2752 int wildcard = 0;
2753
2754 if (listen_addr == NULL) {
2755 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002756 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002757 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002758 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002759 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2760 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2761 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002762 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002763 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002764 /*
2765 * Notify client if they requested a specific listen
2766 * address and it was overridden.
2767 */
2768 if (*listen_addr != '\0' &&
2769 strcmp(listen_addr, "0.0.0.0") != 0 &&
2770 strcmp(listen_addr, "*") != 0) {
2771 packet_send_debug("Forwarding listen address "
2772 "\"%s\" overridden by server "
2773 "GatewayPorts", listen_addr);
2774 }
Damien Miller602943d2014-07-04 08:59:41 +10002775 } else if (strcmp(listen_addr, "localhost") != 0 ||
2776 strcmp(listen_addr, "127.0.0.1") == 0 ||
2777 strcmp(listen_addr, "::1") == 0) {
2778 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002779 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002780 }
2781 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2782 strcmp(listen_addr, "::1") == 0) {
2783 /*
2784 * If a specific IPv4/IPv6 localhost address has been
2785 * requested then accept it even if gateway_ports is in
2786 * effect. This allows the client to prefer IPv4 or IPv6.
2787 */
2788 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002789 }
2790 if (wildcardp != NULL)
2791 *wildcardp = wildcard;
2792 return addr;
2793}
2794
Damien Millerb16461c2002-01-22 23:29:22 +11002795static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002796channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2797 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002798{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002799 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002800 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002801 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002802 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002803 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002804 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002805
Damien Millerb16461c2002-01-22 23:29:22 +11002806 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
Damien Miller7acefbb2014-07-18 14:11:24 +10002807 fwd->listen_host : fwd->connect_host;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002808 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002809
Damien Millerb16461c2002-01-22 23:29:22 +11002810 if (host == NULL) {
2811 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002812 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002813 }
Damien Miller9576ac42009-01-28 16:30:33 +11002814 if (strlen(host) >= NI_MAXHOST) {
Kevin Steves12057502001-02-05 14:54:34 +00002815 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002816 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002817 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002818
Damien Millerf6dff7c2011-09-22 21:38:52 +10002819 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002820 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2821 is_client, fwd_opts);
2822 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002823 type, wildcard, (addr == NULL) ? "NULL" : addr);
2824
2825 /*
Damien Miller34132e52000-01-14 15:45:46 +11002826 * getaddrinfo returns a loopback address if the hostname is
2827 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002828 */
Damien Miller34132e52000-01-14 15:45:46 +11002829 memset(&hints, 0, sizeof(hints));
2830 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002831 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002832 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002833 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002834 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2835 if (addr == NULL) {
2836 /* This really shouldn't happen */
2837 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002838 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002839 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10002840 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11002841 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002842 }
Damien Millera7270302005-07-06 09:36:05 +10002843 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002844 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002845 if (allocated_listen_port != NULL)
2846 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11002847 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11002848 switch (ai->ai_family) {
2849 case AF_INET:
2850 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
2851 sin_port;
2852 break;
2853 case AF_INET6:
2854 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
2855 sin6_port;
2856 break;
2857 default:
Damien Miller34132e52000-01-14 15:45:46 +11002858 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11002859 }
2860 /*
2861 * If allocating a port for -R forwards, then use the
2862 * same port for all address families.
2863 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002864 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002865 allocated_listen_port != NULL && *allocated_listen_port > 0)
2866 *lport_p = htons(*allocated_listen_port);
2867
Damien Miller34132e52000-01-14 15:45:46 +11002868 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2869 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10002870 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11002871 continue;
2872 }
2873 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11002874 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002875 if (sock < 0) {
2876 /* this is no error since kernel may not support ipv6 */
2877 verbose("socket: %.100s", strerror(errno));
2878 continue;
2879 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002880
2881 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11002882 if (ai->ai_family == AF_INET6)
2883 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002884
Damien Miller4bf648f2009-02-14 16:28:21 +11002885 debug("Local forwarding listening on %s port %s.",
2886 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002887
Damien Miller34132e52000-01-14 15:45:46 +11002888 /* Bind the socket to the address. */
2889 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2890 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002891 if (!ai->ai_next)
2892 error("bind: %.100s", strerror(errno));
2893 else
2894 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002895
Damien Miller34132e52000-01-14 15:45:46 +11002896 close(sock);
2897 continue;
2898 }
2899 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002900 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002901 error("listen: %.100s", strerror(errno));
2902 close(sock);
2903 continue;
2904 }
Damien Miller4bf648f2009-02-14 16:28:21 +11002905
2906 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10002907 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11002908 * record what we got.
2909 */
Damien Miller7acefbb2014-07-18 14:11:24 +10002910 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11002911 allocated_listen_port != NULL &&
2912 *allocated_listen_port == 0) {
2913 *allocated_listen_port = get_sock_port(sock, 1);
2914 debug("Allocated listen port %d",
2915 *allocated_listen_port);
2916 }
2917
Damien Miller34132e52000-01-14 15:45:46 +11002918 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002919 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002920 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002921 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11002922 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10002923 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002924 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10002925 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11002926 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
2927 c->listening_port = *allocated_listen_port;
2928 else
Damien Miller7acefbb2014-07-18 14:11:24 +10002929 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002930 success = 1;
2931 }
2932 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10002933 error("%s: cannot listen to port: %d", __func__,
2934 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002935 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002936 return success;
Damien Miller95def091999-11-25 00:26:21 +11002937}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002938
Damien Miller7acefbb2014-07-18 14:11:24 +10002939static int
2940channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
2941 struct ForwardOptions *fwd_opts)
2942{
2943 struct sockaddr_un sunaddr;
2944 const char *path;
2945 Channel *c;
2946 int port, sock;
2947 mode_t omask;
2948
2949 switch (type) {
2950 case SSH_CHANNEL_UNIX_LISTENER:
2951 if (fwd->connect_path != NULL) {
2952 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
2953 error("Local connecting path too long: %s",
2954 fwd->connect_path);
2955 return 0;
2956 }
2957 path = fwd->connect_path;
2958 port = PORT_STREAMLOCAL;
2959 } else {
2960 if (fwd->connect_host == NULL) {
2961 error("No forward host name.");
2962 return 0;
2963 }
2964 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
2965 error("Forward host name too long.");
2966 return 0;
2967 }
2968 path = fwd->connect_host;
2969 port = fwd->connect_port;
2970 }
2971 break;
2972 case SSH_CHANNEL_RUNIX_LISTENER:
2973 path = fwd->listen_path;
2974 port = PORT_STREAMLOCAL;
2975 break;
2976 default:
2977 error("%s: unexpected channel type %d", __func__, type);
2978 return 0;
2979 }
2980
2981 if (fwd->listen_path == NULL) {
2982 error("No forward path name.");
2983 return 0;
2984 }
2985 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
2986 error("Local listening path too long: %s", fwd->listen_path);
2987 return 0;
2988 }
2989
2990 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
2991
2992 /* Start a Unix domain listener. */
2993 omask = umask(fwd_opts->streamlocal_bind_mask);
2994 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
2995 fwd_opts->streamlocal_bind_unlink);
2996 umask(omask);
2997 if (sock < 0)
2998 return 0;
2999
3000 debug("Local forwarding listening on path %s.", fwd->listen_path);
3001
3002 /* Allocate a channel number for the socket. */
3003 c = channel_new("unix listener", type, sock, sock, -1,
3004 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3005 0, "unix listener", 1);
3006 c->path = xstrdup(path);
3007 c->host_port = port;
3008 c->listening_port = PORT_STREAMLOCAL;
3009 c->listening_addr = xstrdup(fwd->listen_path);
3010 return 1;
3011}
3012
3013static int
3014channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003015{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003016 u_int i;
3017 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003018
Darren Tucker47eede72005-03-14 23:08:12 +11003019 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003020 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003021 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3022 continue;
3023 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3024 debug2("%s: close channel %d", __func__, i);
3025 channel_free(c);
3026 found = 1;
3027 }
3028 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003029
Damien Millerf6dff7c2011-09-22 21:38:52 +10003030 return (found);
3031}
3032
Damien Miller7acefbb2014-07-18 14:11:24 +10003033static int
3034channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003035{
3036 u_int i;
3037 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003038
3039 for (i = 0; i < channels_alloc; i++) {
3040 Channel *c = channels[i];
3041 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3042 continue;
3043 if (c->path == NULL)
3044 continue;
3045 if (strcmp(c->path, path) == 0) {
3046 debug2("%s: close channel %d", __func__, i);
3047 channel_free(c);
3048 found = 1;
3049 }
3050 }
3051
3052 return (found);
3053}
3054
3055int
3056channel_cancel_rport_listener(struct Forward *fwd)
3057{
3058 if (fwd->listen_path != NULL)
3059 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3060 else
3061 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3062}
3063
3064static int
3065channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3066 int cport, struct ForwardOptions *fwd_opts)
3067{
3068 u_int i;
3069 int found = 0;
3070 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003071
3072 for (i = 0; i < channels_alloc; i++) {
3073 Channel *c = channels[i];
3074 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3075 continue;
Damien Millerff773642011-09-22 21:39:48 +10003076 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003077 continue;
Damien Millerff773642011-09-22 21:39:48 +10003078 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3079 /* skip dynamic forwardings */
3080 if (c->host_port == 0)
3081 continue;
3082 } else {
3083 if (c->host_port != cport)
3084 continue;
3085 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003086 if ((c->listening_addr == NULL && addr != NULL) ||
3087 (c->listening_addr != NULL && addr == NULL))
3088 continue;
3089 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003090 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003091 channel_free(c);
3092 found = 1;
3093 }
3094 }
3095
3096 return (found);
3097}
3098
Damien Miller7acefbb2014-07-18 14:11:24 +10003099static int
3100channel_cancel_lport_listener_streamlocal(const char *path)
3101{
3102 u_int i;
3103 int found = 0;
3104
3105 if (path == NULL) {
3106 error("%s: no path specified.", __func__);
3107 return 0;
3108 }
3109
3110 for (i = 0; i < channels_alloc; i++) {
3111 Channel *c = channels[i];
3112 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3113 continue;
3114 if (c->listening_addr == NULL)
3115 continue;
3116 if (strcmp(c->listening_addr, path) == 0) {
3117 debug2("%s: close channel %d", __func__, i);
3118 channel_free(c);
3119 found = 1;
3120 }
3121 }
3122
3123 return (found);
3124}
3125
3126int
3127channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3128{
3129 if (fwd->listen_path != NULL)
3130 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3131 else
3132 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3133}
3134
Damien Millerb16461c2002-01-22 23:29:22 +11003135/* protocol local port fwd, used by ssh (and sshd in v1) */
3136int
Damien Miller7acefbb2014-07-18 14:11:24 +10003137channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003138{
Damien Miller7acefbb2014-07-18 14:11:24 +10003139 if (fwd->listen_path != NULL) {
3140 return channel_setup_fwd_listener_streamlocal(
3141 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3142 } else {
3143 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3144 fwd, NULL, fwd_opts);
3145 }
Damien Millerb16461c2002-01-22 23:29:22 +11003146}
3147
3148/* protocol v2 remote port fwd, used by sshd */
3149int
Damien Miller7acefbb2014-07-18 14:11:24 +10003150channel_setup_remote_fwd_listener(struct Forward *fwd,
3151 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003152{
Damien Miller7acefbb2014-07-18 14:11:24 +10003153 if (fwd->listen_path != NULL) {
3154 return channel_setup_fwd_listener_streamlocal(
3155 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3156 } else {
3157 return channel_setup_fwd_listener_tcpip(
3158 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3159 fwd_opts);
3160 }
Damien Millerb16461c2002-01-22 23:29:22 +11003161}
3162
Damien Miller5428f641999-11-25 11:54:57 +11003163/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003164 * Translate the requested rfwd listen host to something usable for
3165 * this server.
3166 */
3167static const char *
3168channel_rfwd_bind_host(const char *listen_host)
3169{
3170 if (listen_host == NULL) {
3171 if (datafellows & SSH_BUG_RFWD_ADDR)
3172 return "127.0.0.1";
3173 else
3174 return "localhost";
3175 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3176 if (datafellows & SSH_BUG_RFWD_ADDR)
3177 return "0.0.0.0";
3178 else
3179 return "";
3180 } else
3181 return listen_host;
3182}
3183
3184/*
Damien Miller5428f641999-11-25 11:54:57 +11003185 * Initiate forwarding of connections to port "port" on remote host through
3186 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003187 * Returns handle (index) for updating the dynamic listen port with
3188 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003189 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003190int
Damien Miller7acefbb2014-07-18 14:11:24 +10003191channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003192{
Darren Tucker68afb8c2011-10-02 18:59:03 +11003193 int type, success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003194
Damien Miller95def091999-11-25 00:26:21 +11003195 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10003196 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10003197 packet_start(SSH2_MSG_GLOBAL_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003198 if (fwd->listen_path != NULL) {
3199 packet_put_cstring("streamlocal-forward@openssh.com");
3200 packet_put_char(1); /* boolean: want reply */
3201 packet_put_cstring(fwd->listen_path);
3202 } else {
3203 packet_put_cstring("tcpip-forward");
3204 packet_put_char(1); /* boolean: want reply */
3205 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3206 packet_put_int(fwd->listen_port);
3207 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003208 packet_send();
3209 packet_write_wait();
3210 /* Assume that server accepts the request */
3211 success = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003212 } else if (fwd->listen_path == NULL) {
Damien Miller33b13562000-04-04 14:38:59 +10003213 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003214 packet_put_int(fwd->listen_port);
3215 packet_put_cstring(fwd->connect_host);
3216 packet_put_int(fwd->connect_port);
Damien Miller33b13562000-04-04 14:38:59 +10003217 packet_send();
3218 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003219
3220 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11003221 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003222 switch (type) {
3223 case SSH_SMSG_SUCCESS:
3224 success = 1;
3225 break;
3226 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11003227 break;
3228 default:
3229 /* Unknown packet */
3230 packet_disconnect("Protocol error for port forward request:"
3231 "received packet type %d.", type);
3232 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003233 } else {
3234 logit("Warning: Server does not support remote stream local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11003235 }
3236 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003237 /* Record that connection to this host/port is permitted. */
3238 permitted_opens = xrealloc(permitted_opens,
3239 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003240 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003241 if (fwd->connect_path != NULL) {
3242 permitted_opens[idx].host_to_connect =
3243 xstrdup(fwd->connect_path);
3244 permitted_opens[idx].port_to_connect =
3245 PORT_STREAMLOCAL;
3246 } else {
3247 permitted_opens[idx].host_to_connect =
3248 xstrdup(fwd->connect_host);
3249 permitted_opens[idx].port_to_connect =
3250 fwd->connect_port;
3251 }
3252 if (fwd->listen_path != NULL) {
3253 permitted_opens[idx].listen_host = NULL;
3254 permitted_opens[idx].listen_path =
3255 xstrdup(fwd->listen_path);
3256 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3257 } else {
3258 permitted_opens[idx].listen_host =
3259 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3260 permitted_opens[idx].listen_path = NULL;
3261 permitted_opens[idx].listen_port = fwd->listen_port;
3262 }
Damien Miller33b13562000-04-04 14:38:59 +10003263 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003264 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003265}
3266
Damien Miller4b3ed642014-07-02 15:29:40 +10003267static int
3268open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003269 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003270{
3271 if (allowed_open->host_to_connect == NULL)
3272 return 0;
3273 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3274 allowed_open->port_to_connect != requestedport)
3275 return 0;
3276 if (strcmp(allowed_open->host_to_connect, requestedhost) != 0)
3277 return 0;
3278 return 1;
3279}
3280
3281/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003282 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003283 * we don't support FWD_PERMIT_ANY_PORT and
3284 * need to translate between the configured-host (listen_host)
3285 * and what we've sent to the remote server (channel_rfwd_bind_host)
3286 */
3287static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003288open_listen_match_tcpip(ForwardPermission *allowed_open,
3289 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003290{
3291 const char *allowed_host;
3292
3293 if (allowed_open->host_to_connect == NULL)
3294 return 0;
3295 if (allowed_open->listen_port != requestedport)
3296 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003297 if (!translate && allowed_open->listen_host == NULL &&
3298 requestedhost == NULL)
3299 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003300 allowed_host = translate ?
3301 channel_rfwd_bind_host(allowed_open->listen_host) :
3302 allowed_open->listen_host;
3303 if (allowed_host == NULL ||
3304 strcmp(allowed_host, requestedhost) != 0)
3305 return 0;
3306 return 1;
3307}
3308
Damien Miller7acefbb2014-07-18 14:11:24 +10003309static int
3310open_listen_match_streamlocal(ForwardPermission *allowed_open,
3311 const char *requestedpath)
3312{
3313 if (allowed_open->host_to_connect == NULL)
3314 return 0;
3315 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3316 return 0;
3317 if (allowed_open->listen_path == NULL ||
3318 strcmp(allowed_open->listen_path, requestedpath) != 0)
3319 return 0;
3320 return 1;
3321}
3322
Damien Miller5428f641999-11-25 11:54:57 +11003323/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003324 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003325 * local side.
3326 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003327static int
3328channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003329{
3330 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003331
3332 if (!compat20)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003333 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003334
3335 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003336 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003337 break;
3338 }
3339 if (i >= num_permitted_opens) {
3340 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003341 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003342 }
3343 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3344 packet_put_cstring("cancel-tcpip-forward");
3345 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003346 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003347 packet_put_int(port);
3348 packet_send();
3349
Damien Miller4b3ed642014-07-02 15:29:40 +10003350 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003351 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003352 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003353 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003354 free(permitted_opens[i].listen_host);
3355 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003356 permitted_opens[i].listen_path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003357
3358 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003359}
3360
3361/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003362 * Request cancellation of remote forwarding of Unix domain socket
3363 * path from local side.
3364 */
3365static int
3366channel_request_rforward_cancel_streamlocal(const char *path)
3367{
3368 int i;
3369
3370 if (!compat20)
3371 return -1;
3372
3373 for (i = 0; i < num_permitted_opens; i++) {
3374 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3375 break;
3376 }
3377 if (i >= num_permitted_opens) {
3378 debug("%s: requested forward not found", __func__);
3379 return -1;
3380 }
3381 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3382 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3383 packet_put_char(0);
3384 packet_put_cstring(path);
3385 packet_send();
3386
3387 permitted_opens[i].listen_port = 0;
3388 permitted_opens[i].port_to_connect = 0;
3389 free(permitted_opens[i].host_to_connect);
3390 permitted_opens[i].host_to_connect = NULL;
3391 permitted_opens[i].listen_host = NULL;
3392 free(permitted_opens[i].listen_path);
3393 permitted_opens[i].listen_path = NULL;
3394
3395 return 0;
3396}
3397
3398/*
3399 * Request cancellation of remote forwarding of a connection from local side.
3400 */
3401int
3402channel_request_rforward_cancel(struct Forward *fwd)
3403{
3404 if (fwd->listen_path != NULL) {
3405 return (channel_request_rforward_cancel_streamlocal(
3406 fwd->listen_path));
3407 } else {
3408 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3409 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3410 }
3411}
3412
3413/*
Damien Miller5428f641999-11-25 11:54:57 +11003414 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
3415 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10003416 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11003417 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003418int
Damien Miller7acefbb2014-07-18 14:11:24 +10003419channel_input_port_forward_request(int is_root, struct ForwardOptions *fwd_opts)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003420{
Darren Tuckere7d4b192006-07-12 22:17:10 +10003421 int success = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003422 struct Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003423
Damien Miller95def091999-11-25 00:26:21 +11003424 /* Get arguments from the packet. */
Damien Miller7acefbb2014-07-18 14:11:24 +10003425 memset(&fwd, 0, sizeof(fwd));
3426 fwd.listen_port = packet_get_int();
3427 fwd.connect_host = packet_get_string(NULL);
3428 fwd.connect_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003429
Damien Millerbac2d8a2000-09-05 16:13:06 +11003430#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11003431 /*
3432 * Check that an unprivileged user is not trying to forward a
3433 * privileged port.
3434 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003435 if (fwd.listen_port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10003436 packet_disconnect(
3437 "Requested forwarding of port %d but user is not root.",
Damien Miller7acefbb2014-07-18 14:11:24 +10003438 fwd.listen_port);
3439 if (fwd.connect_port == 0)
Darren Tucker9189ff82003-07-03 13:52:04 +10003440 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11003441#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10003442
Damien Miller0bc1bd82000-11-13 22:57:25 +11003443 /* Initiate forwarding */
Damien Miller7acefbb2014-07-18 14:11:24 +10003444 success = channel_setup_local_fwd_listener(&fwd, fwd_opts);
Damien Miller95def091999-11-25 00:26:21 +11003445
3446 /* Free the argument string. */
Damien Miller7acefbb2014-07-18 14:11:24 +10003447 free(fwd.connect_host);
Darren Tuckere7d4b192006-07-12 22:17:10 +10003448
3449 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003450}
3451
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003452/*
3453 * Permits opening to any host/port if permitted_opens[] is empty. This is
3454 * usually called by the server, because the user could connect to any port
3455 * anyway, and the server has no way to know but to trust the client anyway.
3456 */
3457void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003458channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003459{
3460 if (num_permitted_opens == 0)
3461 all_opens_permitted = 1;
3462}
3463
Ben Lindstroma3700052001-04-05 23:26:32 +00003464void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003465channel_add_permitted_opens(char *host, int port)
3466{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003467 debug("allow port forwarding to host %s port %d", host, port);
3468
Damien Miller232cfb12010-06-26 09:50:30 +10003469 permitted_opens = xrealloc(permitted_opens,
3470 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003471 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3472 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003473 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003474 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003475 permitted_opens[num_permitted_opens].listen_port = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003476 num_permitted_opens++;
3477
3478 all_opens_permitted = 0;
3479}
3480
Darren Tucker68afb8c2011-10-02 18:59:03 +11003481/*
3482 * Update the listen port for a dynamic remote forward, after
3483 * the actual 'newport' has been allocated. If 'newport' < 0 is
3484 * passed then they entry will be invalidated.
3485 */
3486void
3487channel_update_permitted_opens(int idx, int newport)
3488{
3489 if (idx < 0 || idx >= num_permitted_opens) {
3490 debug("channel_update_permitted_opens: index out of range:"
3491 " %d num_permitted_opens %d", idx, num_permitted_opens);
3492 return;
3493 }
3494 debug("%s allowed port %d for forwarding to host %s port %d",
3495 newport > 0 ? "Updating" : "Removing",
3496 newport,
3497 permitted_opens[idx].host_to_connect,
3498 permitted_opens[idx].port_to_connect);
3499 if (newport >= 0) {
3500 permitted_opens[idx].listen_port =
3501 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3502 } else {
3503 permitted_opens[idx].listen_port = 0;
3504 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003505 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003506 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003507 free(permitted_opens[idx].listen_host);
3508 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003509 free(permitted_opens[idx].listen_path);
3510 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003511 }
3512}
3513
Damien Millera765cf42006-07-24 14:08:13 +10003514int
Damien Miller9b439df2006-07-24 14:04:00 +10003515channel_add_adm_permitted_opens(char *host, int port)
3516{
Damien Millera765cf42006-07-24 14:08:13 +10003517 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003518
Damien Miller232cfb12010-06-26 09:50:30 +10003519 permitted_adm_opens = xrealloc(permitted_adm_opens,
3520 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003521 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3522 = xstrdup(host);
3523 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003524 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003525 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003526 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003527 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003528}
3529
3530void
Damien Millerc6081482012-04-22 11:18:53 +10003531channel_disable_adm_local_opens(void)
3532{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003533 channel_clear_adm_permitted_opens();
3534 permitted_adm_opens = xmalloc(sizeof(*permitted_adm_opens));
3535 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3536 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003537}
3538
3539void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003540channel_clear_permitted_opens(void)
3541{
3542 int i;
3543
Damien Miller4b3ed642014-07-02 15:29:40 +10003544 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003545 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003546 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003547 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003548 }
Darren Tuckera627d422013-06-02 07:31:17 +10003549 free(permitted_opens);
3550 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003551 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003552}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003553
Damien Miller9b439df2006-07-24 14:04:00 +10003554void
3555channel_clear_adm_permitted_opens(void)
3556{
3557 int i;
3558
Damien Miller4b3ed642014-07-02 15:29:40 +10003559 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003560 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003561 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003562 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003563 }
Darren Tuckera627d422013-06-02 07:31:17 +10003564 free(permitted_adm_opens);
3565 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003566 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003567}
3568
Darren Tuckere7140f22008-06-10 23:01:51 +10003569void
3570channel_print_adm_permitted_opens(void)
3571{
Damien Miller6ef17492008-07-16 22:42:06 +10003572 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003573
Damien Millerb53d8a12009-01-28 16:13:04 +11003574 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003575 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003576 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003577 return;
3578 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003579 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003580 if (permitted_adm_opens[i].host_to_connect == NULL)
3581 printf(" none");
3582 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003583 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3584 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003585 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003586}
3587
Darren Tucker1338b9e2011-10-02 18:57:35 +11003588/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3589int
3590permitopen_port(const char *p)
3591{
3592 int port;
3593
3594 if (strcmp(p, "*") == 0)
3595 return FWD_PERMIT_ANY_PORT;
3596 if ((port = a2port(p)) > 0)
3597 return port;
3598 return -1;
3599}
3600
Damien Millerbd740252008-05-19 15:37:09 +10003601/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003602static int
Damien Millerbd740252008-05-19 15:37:09 +10003603connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003604{
Damien Millerbd740252008-05-19 15:37:09 +10003605 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003606 struct sockaddr_un *sunaddr;
3607 char ntop[NI_MAXHOST], strport[MAX(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003608
Damien Millerbd740252008-05-19 15:37:09 +10003609 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003610 switch (cctx->ai->ai_family) {
3611 case AF_UNIX:
3612 /* unix:pathname instead of host:port */
3613 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3614 strlcpy(ntop, "unix", sizeof(ntop));
3615 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3616 break;
3617 case AF_INET:
3618 case AF_INET6:
3619 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3620 ntop, sizeof(ntop), strport, sizeof(strport),
3621 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3622 error("connect_next: getnameinfo failed");
3623 continue;
3624 }
3625 break;
3626 default:
Damien Miller34132e52000-01-14 15:45:46 +11003627 continue;
3628 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003629 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3630 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003631 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003632 error("socket: %.100s", strerror(errno));
3633 else
3634 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003635 continue;
3636 }
Damien Miller232711f2004-06-15 10:35:30 +10003637 if (set_nonblock(sock) == -1)
3638 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003639 if (connect(sock, cctx->ai->ai_addr,
3640 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3641 debug("connect_next: host %.100s ([%.100s]:%s): "
3642 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003643 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003644 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003645 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003646 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003647 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003648 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003649 if (cctx->ai->ai_family != AF_UNIX)
3650 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003651 debug("connect_next: host %.100s ([%.100s]:%s) "
3652 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3653 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003654 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003655 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003656 return -1;
3657}
Damien Millerb38eff82000-04-01 11:09:21 +10003658
Damien Millerbd740252008-05-19 15:37:09 +10003659static void
3660channel_connect_ctx_free(struct channel_connect *cctx)
3661{
Darren Tuckera627d422013-06-02 07:31:17 +10003662 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003663 if (cctx->aitop) {
3664 if (cctx->aitop->ai_family == AF_UNIX)
3665 free(cctx->aitop);
3666 else
3667 freeaddrinfo(cctx->aitop);
3668 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003669 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003670}
3671
Damien Miller7acefbb2014-07-18 14:11:24 +10003672/* Return CONNECTING channel to remote host:port or local socket path */
Damien Millerbd740252008-05-19 15:37:09 +10003673static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10003674connect_to(const char *name, int port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003675{
3676 struct addrinfo hints;
3677 int gaierr;
3678 int sock = -1;
3679 char strport[NI_MAXSERV];
3680 struct channel_connect cctx;
3681 Channel *c;
3682
Damien Miller2bcb8662008-07-12 17:12:29 +10003683 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003684
3685 if (port == PORT_STREAMLOCAL) {
3686 struct sockaddr_un *sunaddr;
3687 struct addrinfo *ai;
3688
3689 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3690 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3691 return (NULL);
3692 }
3693
3694 /*
3695 * Fake up a struct addrinfo for AF_UNIX connections.
3696 * channel_connect_ctx_free() must check ai_family
3697 * and use free() not freeaddirinfo() for AF_UNIX.
3698 */
3699 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3700 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3701 ai->ai_addr = (struct sockaddr *)(ai + 1);
3702 ai->ai_addrlen = sizeof(*sunaddr);
3703 ai->ai_family = AF_UNIX;
3704 ai->ai_socktype = SOCK_STREAM;
3705 ai->ai_protocol = PF_UNSPEC;
3706 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3707 sunaddr->sun_family = AF_UNIX;
3708 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3709 cctx.aitop = ai;
3710 } else {
3711 memset(&hints, 0, sizeof(hints));
3712 hints.ai_family = IPv4or6;
3713 hints.ai_socktype = SOCK_STREAM;
3714 snprintf(strport, sizeof strport, "%d", port);
3715 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop)) != 0) {
3716 error("connect_to %.100s: unknown host (%s)", name,
3717 ssh_gai_strerror(gaierr));
3718 return NULL;
3719 }
Damien Millerbd740252008-05-19 15:37:09 +10003720 }
3721
Damien Miller7acefbb2014-07-18 14:11:24 +10003722 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003723 cctx.port = port;
3724 cctx.ai = cctx.aitop;
3725
3726 if ((sock = connect_next(&cctx)) == -1) {
3727 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003728 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003729 channel_connect_ctx_free(&cctx);
3730 return NULL;
3731 }
3732 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3733 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3734 c->connect_ctx = cctx;
3735 return c;
3736}
3737
3738Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003739channel_connect_by_listen_address(const char *listen_host,
3740 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003741{
3742 int i;
3743
3744 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003745 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003746 listen_port, 1)) {
Damien Millerbd740252008-05-19 15:37:09 +10003747 return connect_to(
3748 permitted_opens[i].host_to_connect,
3749 permitted_opens[i].port_to_connect, ctype, rname);
3750 }
3751 }
3752 error("WARNING: Server requests forwarding for unknown listen_port %d",
3753 listen_port);
3754 return NULL;
3755}
3756
Damien Miller7acefbb2014-07-18 14:11:24 +10003757Channel *
3758channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3759{
3760 int i;
3761
3762 for (i = 0; i < num_permitted_opens; i++) {
3763 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3764 return connect_to(
3765 permitted_opens[i].host_to_connect,
3766 permitted_opens[i].port_to_connect, ctype, rname);
3767 }
3768 }
3769 error("WARNING: Server requests forwarding for unknown path %.100s",
3770 path);
3771 return NULL;
3772}
3773
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003774/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003775Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10003776channel_connect_to_port(const char *host, u_short port, char *ctype, char *rname)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003777{
Damien Miller9b439df2006-07-24 14:04:00 +10003778 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003779
3780 permit = all_opens_permitted;
3781 if (!permit) {
3782 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003783 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003784 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003785 break;
3786 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003787 }
Damien Miller9b439df2006-07-24 14:04:00 +10003788
3789 if (num_adm_permitted_opens > 0) {
3790 permit_adm = 0;
3791 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003792 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003793 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003794 break;
3795 }
Damien Miller9b439df2006-07-24 14:04:00 +10003796 }
3797
3798 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003799 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003800 "but the request was denied.", host, port);
Damien Millerbd740252008-05-19 15:37:09 +10003801 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003802 }
Damien Millerbd740252008-05-19 15:37:09 +10003803 return connect_to(host, port, ctype, rname);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003804}
3805
Damien Miller7acefbb2014-07-18 14:11:24 +10003806/* Check if connecting to that path is permitted and connect. */
3807Channel *
3808channel_connect_to_path(const char *path, char *ctype, char *rname)
3809{
3810 int i, permit, permit_adm = 1;
3811
3812 permit = all_opens_permitted;
3813 if (!permit) {
3814 for (i = 0; i < num_permitted_opens; i++)
3815 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3816 permit = 1;
3817 break;
3818 }
3819 }
3820
3821 if (num_adm_permitted_opens > 0) {
3822 permit_adm = 0;
3823 for (i = 0; i < num_adm_permitted_opens; i++)
3824 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3825 permit_adm = 1;
3826 break;
3827 }
3828 }
3829
3830 if (!permit || !permit_adm) {
3831 logit("Received request to connect to path %.100s, "
3832 "but the request was denied.", path);
3833 return NULL;
3834 }
3835 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3836}
3837
Damien Miller0e220db2004-06-15 10:34:08 +10003838void
3839channel_send_window_changes(void)
3840{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003841 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003842 struct winsize ws;
3843
3844 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003845 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003846 channels[i]->type != SSH_CHANNEL_OPEN)
3847 continue;
3848 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3849 continue;
3850 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003851 packet_put_int((u_int)ws.ws_col);
3852 packet_put_int((u_int)ws.ws_row);
3853 packet_put_int((u_int)ws.ws_xpixel);
3854 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003855 packet_send();
3856 }
3857}
3858
Ben Lindstrome9c99912001-06-09 00:41:05 +00003859/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003860
Damien Miller5428f641999-11-25 11:54:57 +11003861/*
3862 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003863 * Returns 0 and a suitable display number for the DISPLAY variable
3864 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003865 */
Kevin Steves366298c2001-12-19 17:58:01 +00003866int
Damien Miller95c249f2002-02-05 12:11:34 +11003867x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003868 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003869{
Damien Millere7378562001-12-21 14:58:35 +11003870 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003871 int display_number, sock;
3872 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003873 struct addrinfo hints, *ai, *aitop;
3874 char strport[NI_MAXSERV];
3875 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003876
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003877 if (chanids == NULL)
3878 return -1;
3879
Damien Millera34a28b1999-12-14 10:47:15 +11003880 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11003881 display_number < MAX_DISPLAYS;
3882 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11003883 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11003884 memset(&hints, 0, sizeof(hints));
3885 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11003886 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11003887 hints.ai_socktype = SOCK_STREAM;
3888 snprintf(strport, sizeof strport, "%d", port);
3889 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003890 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00003891 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003892 }
Damien Miller34132e52000-01-14 15:45:46 +11003893 for (ai = aitop; ai; ai = ai->ai_next) {
3894 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3895 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11003896 sock = socket(ai->ai_family, ai->ai_socktype,
3897 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003898 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11003899 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
3900#ifdef EPFNOSUPPORT
3901 && (errno != EPFNOSUPPORT)
3902#endif
3903 ) {
Damien Millere2192732000-01-17 13:22:55 +11003904 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10003905 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00003906 return -1;
Damien Millere2192732000-01-17 13:22:55 +11003907 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11003908 debug("x11_create_display_inet: Socket family %d not supported",
3909 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11003910 continue;
3911 }
Damien Miller34132e52000-01-14 15:45:46 +11003912 }
Damien Miller04ee0f82009-11-18 17:48:30 +11003913 if (ai->ai_family == AF_INET6)
3914 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10003915 if (x11_use_localhost)
3916 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11003917 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003918 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003919 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11003920
Damien Miller34132e52000-01-14 15:45:46 +11003921 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11003922 close(socks[n]);
3923 }
3924 num_socks = 0;
3925 break;
3926 }
3927 socks[num_socks++] = sock;
3928 if (num_socks == NUM_SOCKS)
3929 break;
Damien Miller95def091999-11-25 00:26:21 +11003930 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00003931 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11003932 if (num_socks > 0)
3933 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003934 }
Damien Miller95def091999-11-25 00:26:21 +11003935 if (display_number >= MAX_DISPLAYS) {
3936 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00003937 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003938 }
Damien Miller95def091999-11-25 00:26:21 +11003939 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11003940 for (n = 0; n < num_socks; n++) {
3941 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11003942 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003943 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003944 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00003945 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11003946 }
Damien Miller95def091999-11-25 00:26:21 +11003947 }
Damien Miller34132e52000-01-14 15:45:46 +11003948
Damien Miller34132e52000-01-14 15:45:46 +11003949 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11003950 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11003951 for (n = 0; n < num_socks; n++) {
3952 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11003953 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10003954 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3955 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003956 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11003957 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003958 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11003959 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003960 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003961
Kevin Steves366298c2001-12-19 17:58:01 +00003962 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003963 *display_numberp = display_number;
3964 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003965}
3966
Ben Lindstrombba81212001-06-25 05:01:22 +00003967static int
Damien Miller819dbb62009-01-21 16:46:26 +11003968connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003969{
Damien Miller95def091999-11-25 00:26:21 +11003970 int sock;
3971 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003972
Damien Miller3afe3752001-12-21 12:39:51 +11003973 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3974 if (sock < 0)
3975 error("socket: %.100s", strerror(errno));
3976 memset(&addr, 0, sizeof(addr));
3977 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11003978 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11003979 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11003980 return sock;
3981 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11003982 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
3983 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003984}
3985
Damien Miller819dbb62009-01-21 16:46:26 +11003986static int
3987connect_local_xsocket(u_int dnr)
3988{
3989 char buf[1024];
3990 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
3991 return connect_local_xsocket_path(buf);
3992}
3993
Damien Millerbd483e72000-04-30 10:00:53 +10003994int
3995x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003996{
Damien Miller57c4e872006-03-31 23:11:07 +11003997 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11003998 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10003999 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004000 struct addrinfo hints, *ai, *aitop;
4001 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004002 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004003
Damien Miller95def091999-11-25 00:26:21 +11004004 /* Try to open a socket for the local X server. */
4005 display = getenv("DISPLAY");
4006 if (!display) {
4007 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004008 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004009 }
Damien Miller5428f641999-11-25 11:54:57 +11004010 /*
4011 * Now we decode the value of the DISPLAY variable and make a
4012 * connection to the real X server.
4013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004014
Damien Miller819dbb62009-01-21 16:46:26 +11004015 /* Check if the display is from launchd. */
4016#ifdef __APPLE__
4017 if (strncmp(display, "/tmp/launch", 11) == 0) {
4018 sock = connect_local_xsocket_path(display);
4019 if (sock < 0)
4020 return -1;
4021
4022 /* OK, we now have a connection to the display. */
4023 return sock;
4024 }
4025#endif
Damien Miller5428f641999-11-25 11:54:57 +11004026 /*
4027 * Check if it is a unix domain socket. Unix domain displays are in
4028 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4029 */
Damien Miller95def091999-11-25 00:26:21 +11004030 if (strncmp(display, "unix:", 5) == 0 ||
4031 display[0] == ':') {
4032 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004033 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004034 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004035 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004036 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004037 }
4038 /* Create a socket. */
4039 sock = connect_local_xsocket(display_number);
4040 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004041 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004042
4043 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004044 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004045 }
Damien Miller5428f641999-11-25 11:54:57 +11004046 /*
4047 * Connect to an inet socket. The DISPLAY value is supposedly
4048 * hostname:d[.s], where hostname may also be numeric IP address.
4049 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004050 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004051 cp = strchr(buf, ':');
4052 if (!cp) {
4053 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004054 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004055 }
Damien Miller95def091999-11-25 00:26:21 +11004056 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004057 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004058 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004059 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004060 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004061 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004062 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004063
Damien Miller34132e52000-01-14 15:45:46 +11004064 /* Look up the host address */
4065 memset(&hints, 0, sizeof(hints));
4066 hints.ai_family = IPv4or6;
4067 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004068 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004069 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004070 error("%.100s: unknown host. (%s)", buf,
4071 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004072 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004073 }
Damien Miller34132e52000-01-14 15:45:46 +11004074 for (ai = aitop; ai; ai = ai->ai_next) {
4075 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004076 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004077 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004078 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004079 continue;
4080 }
4081 /* Connect it to the display. */
4082 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004083 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004084 6000 + display_number, strerror(errno));
4085 close(sock);
4086 continue;
4087 }
4088 /* Success */
4089 break;
Damien Miller34132e52000-01-14 15:45:46 +11004090 }
Damien Miller34132e52000-01-14 15:45:46 +11004091 freeaddrinfo(aitop);
4092 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004093 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004094 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004095 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004096 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004097 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004098 return sock;
4099}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004100
Damien Millerbd483e72000-04-30 10:00:53 +10004101/*
4102 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
4103 * the remote channel number. We should do whatever we want, and respond
4104 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
4105 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004106
Damien Miller8473dd82006-07-24 14:08:32 +10004107/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004108int
Damien Miller630d6f42002-01-22 23:17:30 +11004109x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10004110{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004111 Channel *c = NULL;
4112 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10004113 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10004114
4115 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004116
4117 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00004118
4119 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004120 remote_host = packet_get_string(NULL);
4121 } else {
4122 remote_host = xstrdup("unknown (remote did not supply name)");
4123 }
Damien Miller48b03fc2002-01-22 23:11:40 +11004124 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10004125
4126 /* Obtain a connection to the real X display. */
4127 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004128 if (sock != -1) {
4129 /* Allocate a channel for this connection. */
4130 c = channel_new("connected x11 socket",
4131 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
4132 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11004133 c->remote_id = remote_id;
4134 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004135 }
Darren Tuckera627d422013-06-02 07:31:17 +10004136 free(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004137 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10004138 /* Send refusal to the remote host. */
4139 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004140 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10004141 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10004142 /* Send a confirmation to the remote host. */
4143 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004144 packet_put_int(remote_id);
4145 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10004146 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004147 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004148 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004149}
4150
Damien Miller69b69aa2000-10-28 14:19:58 +11004151/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10004152/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004153int
Damien Miller630d6f42002-01-22 23:17:30 +11004154deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11004155{
4156 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00004157
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00004158 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11004159 case SSH_SMSG_AGENT_OPEN:
4160 error("Warning: ssh server tried agent forwarding.");
4161 break;
4162 case SSH_SMSG_X11_OPEN:
4163 error("Warning: ssh server tried X11 forwarding.");
4164 break;
4165 default:
Damien Miller630d6f42002-01-22 23:17:30 +11004166 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11004167 break;
4168 }
Damien Miller5eb137c2005-12-31 16:19:53 +11004169 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11004170 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
4171 packet_put_int(rchan);
4172 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004173 return 0;
Damien Miller69b69aa2000-10-28 14:19:58 +11004174}
4175
Damien Miller5428f641999-11-25 11:54:57 +11004176/*
4177 * Requests forwarding of X11 connections, generates fake authentication
4178 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004179 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004180 */
Damien Miller4af51302000-04-16 11:18:38 +10004181void
Damien Miller17e7ed02005-06-17 12:54:33 +10004182x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004183 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004184{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004185 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004186 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004187 char *new_data;
4188 int screen_number;
4189 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10004190 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004191
Damien Millerf92c0792005-07-06 09:45:26 +10004192 if (x11_saved_display == NULL)
4193 x11_saved_display = xstrdup(disp);
4194 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004195 error("x11_request_forwarding_with_spoofing: different "
4196 "$DISPLAY already forwarded");
4197 return;
4198 }
4199
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004200 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004201 if (cp)
4202 cp = strchr(cp, '.');
4203 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004204 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004205 else
4206 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004207
Damien Miller13390022005-07-06 09:44:19 +10004208 if (x11_saved_proto == NULL) {
4209 /* Save protocol name. */
4210 x11_saved_proto = xstrdup(proto);
4211 /*
Damien Miller46d38de2005-07-17 17:02:09 +10004212 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10004213 * of the same length.
4214 */
4215 x11_saved_data = xmalloc(data_len);
4216 x11_fake_data = xmalloc(data_len);
4217 for (i = 0; i < data_len; i++) {
4218 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4219 fatal("x11_request_forwarding: bad "
4220 "authentication data: %.100s", data);
4221 if (i % 4 == 0)
4222 rnd = arc4random();
4223 x11_saved_data[i] = value;
4224 x11_fake_data[i] = rnd & 0xff;
4225 rnd >>= 8;
4226 }
4227 x11_saved_data_len = data_len;
4228 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004229 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004230
Damien Miller95def091999-11-25 00:26:21 +11004231 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004232 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004233
Damien Miller95def091999-11-25 00:26:21 +11004234 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10004235 if (compat20) {
Damien Miller6d7b4372011-06-23 08:31:57 +10004236 channel_request_start(client_session_id, "x11-req", want_reply);
Damien Millerbd483e72000-04-30 10:00:53 +10004237 packet_put_char(0); /* XXX bool single connection */
4238 } else {
4239 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
4240 }
4241 packet_put_cstring(proto);
4242 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004243 packet_put_int(screen_number);
4244 packet_send();
4245 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004246 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004247}
4248
Ben Lindstrome9c99912001-06-09 00:41:05 +00004249
4250/* -- agent forwarding */
4251
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004252/* Sends a message to the server to request authentication fd forwarding. */
4253
Damien Miller4af51302000-04-16 11:18:38 +10004254void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00004255auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004256{
Damien Miller95def091999-11-25 00:26:21 +11004257 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
4258 packet_send();
4259 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004260}