blob: 9aaf7e9d7a7dcb5f8d1803e31a0281be53a785e4 [file] [log] [blame]
Damien Miller9b439df2006-07-24 14:04:00 +10001/* $OpenBSD: channels.c,v 1.257 2006/07/17 12:06:00 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
44#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110045#include <sys/types.h>
46#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100047#include <sys/socket.h>
48
49#include <netinet/in.h>
50#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110051
Darren Tucker39972492006-07-12 22:22:46 +100052#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100053#if defined(HAVE_NETDB_H)
54# include <netdb.h>
55#endif
Damien Miller99bd21e2006-03-15 11:11:28 +110056#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100057#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "ssh1.h"
61#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "packet.h"
63#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
65#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100069#include "key.h"
70#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110071#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100072#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Ben Lindstrome9c99912001-06-09 00:41:05 +000074/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Pointer to an array containing all allocated channels. The array is
78 * dynamically extended as needed.
79 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000080static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000084 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110085 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100086static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000090 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110091 */
Damien Miller5e953212001-01-30 09:14:00 +110092static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Ben Lindstrome9c99912001-06-09 00:41:05 +000095/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller5428f641999-11-25 11:54:57 +110097/*
98 * Data structure for storing which hosts are permitted for forward requests.
99 * The local sides of any remote forwards are stored in this array to prevent
100 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
101 * network (which might be behind a firewall).
102 */
Damien Miller95def091999-11-25 00:26:21 +1100103typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000104 char *host_to_connect; /* Connect to 'host'. */
105 u_short port_to_connect; /* Connect to 'port'. */
106 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107} ForwardPermission;
108
Damien Miller9b439df2006-07-24 14:04:00 +1000109/* List of all permitted host/port pairs to connect by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000111
Damien Miller9b439df2006-07-24 14:04:00 +1000112/* List of all permitted host/port pairs to connect by the admin. */
113static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
114
115/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000117
118/* Number of permitted host/port pair in the array permitted by the admin. */
119static int num_adm_permitted_opens = 0;
120
Damien Miller5428f641999-11-25 11:54:57 +1100121/*
122 * If this is true, all opens are permitted. This is the case on the server
123 * on which we have to trust the client anyway, and the user could do
124 * anything after logging in anyway.
125 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126static int all_opens_permitted = 0;
127
Ben Lindstrome9c99912001-06-09 00:41:05 +0000128
129/* -- X11 forwarding */
130
131/* Maximum number of fake X11 displays to try. */
132#define MAX_DISPLAYS 1000
133
Damien Miller13390022005-07-06 09:44:19 +1000134/* Saved X11 local (client) display. */
135static char *x11_saved_display = NULL;
136
Ben Lindstrome9c99912001-06-09 00:41:05 +0000137/* Saved X11 authentication protocol name. */
138static char *x11_saved_proto = NULL;
139
140/* Saved X11 authentication data. This is the real data. */
141static char *x11_saved_data = NULL;
142static u_int x11_saved_data_len = 0;
143
144/*
145 * Fake X11 authentication data. This is what the server will be sending us;
146 * we should replace any occurrences of this by the real data.
147 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100148static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000149static u_int x11_fake_data_len;
150
151
152/* -- agent forwarding */
153
154#define NUM_SOCKS 10
155
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000156/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000157static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000158
Ben Lindstrome9c99912001-06-09 00:41:05 +0000159/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000161
Ben Lindstrome9c99912001-06-09 00:41:05 +0000162/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000163
164Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100165channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000166{
167 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000168
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000169 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100170 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000171 return NULL;
172 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000173 c = channels[id];
174 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100175 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000176 return NULL;
177 }
178 return c;
179}
180
Damien Miller5428f641999-11-25 11:54:57 +1100181/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100182 * Returns the channel if it is allowed to receive protocol messages.
183 * Private channels, like listening sockets, may not receive messages.
184 */
185Channel *
186channel_lookup(int id)
187{
188 Channel *c;
189
190 if ((c = channel_by_id(id)) == NULL)
191 return (NULL);
192
Damien Millerd62f2ca2006-03-26 13:57:41 +1100193 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100194 case SSH_CHANNEL_X11_OPEN:
195 case SSH_CHANNEL_LARVAL:
196 case SSH_CHANNEL_CONNECTING:
197 case SSH_CHANNEL_DYNAMIC:
198 case SSH_CHANNEL_OPENING:
199 case SSH_CHANNEL_OPEN:
200 case SSH_CHANNEL_INPUT_DRAINING:
201 case SSH_CHANNEL_OUTPUT_DRAINING:
202 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100203 }
204 logit("Non-public channel %d, type %d.", id, c->type);
205 return (NULL);
206}
207
208/*
Damien Millere247cc42000-05-07 12:03:14 +1000209 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000210 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100211 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100213channel_register_fds(Channel *c, int rfd, int wfd, int efd,
214 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215{
Damien Miller95def091999-11-25 00:26:21 +1100216 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100217 channel_max_fd = MAX(channel_max_fd, rfd);
218 channel_max_fd = MAX(channel_max_fd, wfd);
219 channel_max_fd = MAX(channel_max_fd, efd);
220
Damien Miller5428f641999-11-25 11:54:57 +1100221 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000222
Damien Miller6f83b8e2000-05-02 09:23:45 +1000223 c->rfd = rfd;
224 c->wfd = wfd;
225 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000226 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000227 c->efd = efd;
228 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100229
Damien Miller79438cc2001-02-16 12:34:57 +1100230 /* XXX ugly hack: nonblock is only set by the server */
231 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000232 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100233 c->isatty = 1;
234 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000235 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100236 c->self, c->wfd);
237 }
238 } else {
239 c->isatty = 0;
240 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000241 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100242
Damien Miller69b69aa2000-10-28 14:19:58 +1100243 /* enable nonblocking mode */
244 if (nonblock) {
245 if (rfd != -1)
246 set_nonblock(rfd);
247 if (wfd != -1)
248 set_nonblock(wfd);
249 if (efd != -1)
250 set_nonblock(efd);
251 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000252}
253
254/*
255 * Allocate a new channel object and set its type and socket. This will cause
256 * remote_name to be freed.
257 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000258Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000259channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000260 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000261{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000262 int found;
263 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000264 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265
Damien Miller95def091999-11-25 00:26:21 +1100266 /* Do initial allocation if this is the first call. */
267 if (channels_alloc == 0) {
268 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100269 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100270 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000271 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100272 }
273 /* Try to find a free slot where to put the new channel. */
274 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000275 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100276 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000277 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100278 break;
279 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000280 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100281 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100282 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000283 if (channels_alloc > 10000)
284 fatal("channel_new: internal error: channels_alloc %d "
285 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100286 channels = xrealloc(channels, channels_alloc + 10,
287 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000288 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100289 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100290 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000291 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100292 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000293 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100294 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100295 buffer_init(&c->input);
296 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000297 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100298 c->ostate = CHAN_OUTPUT_OPEN;
299 c->istate = CHAN_INPUT_OPEN;
300 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100301 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100302 c->self = found;
303 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000304 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000305 c->local_window = window;
306 c->local_window_max = window;
307 c->local_consumed = 0;
308 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100309 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000310 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000311 c->remote_window = 0;
312 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000313 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100314 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000315 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100316 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100317 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000318 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000319 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100320 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100321 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000322 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000323}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000324
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000325static int
326channel_find_maxfd(void)
327{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000328 u_int i;
329 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000330 Channel *c;
331
332 for (i = 0; i < channels_alloc; i++) {
333 c = channels[i];
334 if (c != NULL) {
335 max = MAX(max, c->rfd);
336 max = MAX(max, c->wfd);
337 max = MAX(max, c->efd);
338 }
339 }
340 return max;
341}
342
343int
344channel_close_fd(int *fdp)
345{
346 int ret = 0, fd = *fdp;
347
348 if (fd != -1) {
349 ret = close(fd);
350 *fdp = -1;
351 if (fd == channel_max_fd)
352 channel_max_fd = channel_find_maxfd();
353 }
354 return ret;
355}
356
Damien Miller6f83b8e2000-05-02 09:23:45 +1000357/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000358static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000359channel_close_fds(Channel *c)
360{
Damien Miller0e220db2004-06-15 10:34:08 +1000361 debug3("channel %d: close_fds r %d w %d e %d c %d",
362 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000363
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000364 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000365 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000366 channel_close_fd(&c->rfd);
367 channel_close_fd(&c->wfd);
368 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000369}
370
371/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000372void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000373channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000374{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000375 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000376 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000377
378 for (n = 0, i = 0; i < channels_alloc; i++)
379 if (channels[i])
380 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000381 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000382 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000383
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000384 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000385 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000386 xfree(s);
387
Damien Miller6f83b8e2000-05-02 09:23:45 +1000388 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000389 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000390 if (c->ctl_fd != -1)
391 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000392 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000393 buffer_free(&c->input);
394 buffer_free(&c->output);
395 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000396 if (c->remote_name) {
397 xfree(c->remote_name);
398 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100399 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000400 channels[c->self] = NULL;
401 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402}
403
Ben Lindstrome9c99912001-06-09 00:41:05 +0000404void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000405channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000406{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000407 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000408
Ben Lindstrom601e4362001-06-21 03:19:23 +0000409 for (i = 0; i < channels_alloc; i++)
410 if (channels[i] != NULL)
411 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000412}
413
414/*
415 * Closes the sockets/fds of all channels. This is used to close extra file
416 * descriptors after a fork.
417 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000418void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000419channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000420{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000421 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000422
423 for (i = 0; i < channels_alloc; i++)
424 if (channels[i] != NULL)
425 channel_close_fds(channels[i]);
426}
427
428/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000429 * Stop listening to channels.
430 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000431void
432channel_stop_listening(void)
433{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000434 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000435 Channel *c;
436
437 for (i = 0; i < channels_alloc; i++) {
438 c = channels[i];
439 if (c != NULL) {
440 switch (c->type) {
441 case SSH_CHANNEL_AUTH_SOCKET:
442 case SSH_CHANNEL_PORT_LISTENER:
443 case SSH_CHANNEL_RPORT_LISTENER:
444 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000445 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000446 channel_free(c);
447 break;
448 }
449 }
450 }
451}
452
453/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000454 * Returns true if no channel has too much buffered data, and false if one or
455 * more channel is overfull.
456 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000457int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000458channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000459{
460 u_int i;
461 Channel *c;
462
463 for (i = 0; i < channels_alloc; i++) {
464 c = channels[i];
465 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000466#if 0
467 if (!compat20 &&
468 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100469 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000470 c->self, buffer_len(&c->input));
471 return 0;
472 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000473#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000474 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000475 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000476 c->self, buffer_len(&c->output),
477 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000478 return 0;
479 }
480 }
481 }
482 return 1;
483}
484
485/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000486int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000487channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000488{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000489 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000490 Channel *c;
491
492 for (i = 0; i < channels_alloc; i++) {
493 c = channels[i];
494 if (c == NULL)
495 continue;
496 switch (c->type) {
497 case SSH_CHANNEL_X11_LISTENER:
498 case SSH_CHANNEL_PORT_LISTENER:
499 case SSH_CHANNEL_RPORT_LISTENER:
500 case SSH_CHANNEL_CLOSED:
501 case SSH_CHANNEL_AUTH_SOCKET:
502 case SSH_CHANNEL_DYNAMIC:
503 case SSH_CHANNEL_CONNECTING:
504 case SSH_CHANNEL_ZOMBIE:
505 continue;
506 case SSH_CHANNEL_LARVAL:
507 if (!compat20)
508 fatal("cannot happen: SSH_CHANNEL_LARVAL");
509 continue;
510 case SSH_CHANNEL_OPENING:
511 case SSH_CHANNEL_OPEN:
512 case SSH_CHANNEL_X11_OPEN:
513 return 1;
514 case SSH_CHANNEL_INPUT_DRAINING:
515 case SSH_CHANNEL_OUTPUT_DRAINING:
516 if (!compat13)
517 fatal("cannot happen: OUT_DRAIN");
518 return 1;
519 default:
520 fatal("channel_still_open: bad channel type %d", c->type);
521 /* NOTREACHED */
522 }
523 }
524 return 0;
525}
526
527/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000528int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000529channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000530{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000531 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000532 Channel *c;
533
534 for (i = 0; i < channels_alloc; i++) {
535 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000536 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000537 continue;
538 switch (c->type) {
539 case SSH_CHANNEL_CLOSED:
540 case SSH_CHANNEL_DYNAMIC:
541 case SSH_CHANNEL_X11_LISTENER:
542 case SSH_CHANNEL_PORT_LISTENER:
543 case SSH_CHANNEL_RPORT_LISTENER:
544 case SSH_CHANNEL_OPENING:
545 case SSH_CHANNEL_CONNECTING:
546 case SSH_CHANNEL_ZOMBIE:
547 continue;
548 case SSH_CHANNEL_LARVAL:
549 case SSH_CHANNEL_AUTH_SOCKET:
550 case SSH_CHANNEL_OPEN:
551 case SSH_CHANNEL_X11_OPEN:
552 return i;
553 case SSH_CHANNEL_INPUT_DRAINING:
554 case SSH_CHANNEL_OUTPUT_DRAINING:
555 if (!compat13)
556 fatal("cannot happen: OUT_DRAIN");
557 return i;
558 default:
559 fatal("channel_find_open: bad channel type %d", c->type);
560 /* NOTREACHED */
561 }
562 }
563 return -1;
564}
565
566
567/*
568 * Returns a message describing the currently open forwarded connections,
569 * suitable for sending to the client. The message contains crlf pairs for
570 * newlines.
571 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000572char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000573channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000574{
575 Buffer buffer;
576 Channel *c;
577 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000578 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000579
580 buffer_init(&buffer);
581 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
582 buffer_append(&buffer, buf, strlen(buf));
583 for (i = 0; i < channels_alloc; i++) {
584 c = channels[i];
585 if (c == NULL)
586 continue;
587 switch (c->type) {
588 case SSH_CHANNEL_X11_LISTENER:
589 case SSH_CHANNEL_PORT_LISTENER:
590 case SSH_CHANNEL_RPORT_LISTENER:
591 case SSH_CHANNEL_CLOSED:
592 case SSH_CHANNEL_AUTH_SOCKET:
593 case SSH_CHANNEL_ZOMBIE:
594 continue;
595 case SSH_CHANNEL_LARVAL:
596 case SSH_CHANNEL_OPENING:
597 case SSH_CHANNEL_CONNECTING:
598 case SSH_CHANNEL_DYNAMIC:
599 case SSH_CHANNEL_OPEN:
600 case SSH_CHANNEL_X11_OPEN:
601 case SSH_CHANNEL_INPUT_DRAINING:
602 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000603 snprintf(buf, sizeof buf,
604 " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000605 c->self, c->remote_name,
606 c->type, c->remote_id,
607 c->istate, buffer_len(&c->input),
608 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000609 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000610 buffer_append(&buffer, buf, strlen(buf));
611 continue;
612 default:
613 fatal("channel_open_message: bad channel type %d", c->type);
614 /* NOTREACHED */
615 }
616 }
617 buffer_append(&buffer, "\0", 1);
618 cp = xstrdup(buffer_ptr(&buffer));
619 buffer_free(&buffer);
620 return cp;
621}
622
623void
624channel_send_open(int id)
625{
626 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000627
Ben Lindstrome9c99912001-06-09 00:41:05 +0000628 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000629 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000630 return;
631 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000632 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633 packet_start(SSH2_MSG_CHANNEL_OPEN);
634 packet_put_cstring(c->ctype);
635 packet_put_int(c->self);
636 packet_put_int(c->local_window);
637 packet_put_int(c->local_maxpacket);
638 packet_send();
639}
640
641void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000642channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000643{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000644 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000645
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000647 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000648 return;
649 }
Damien Miller0e220db2004-06-15 10:34:08 +1000650 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000651 packet_start(SSH2_MSG_CHANNEL_REQUEST);
652 packet_put_int(c->remote_id);
653 packet_put_cstring(service);
654 packet_put_char(wantconfirm);
655}
Damien Miller4f7becb2006-03-26 14:10:14 +1100656
Ben Lindstrome9c99912001-06-09 00:41:05 +0000657void
Damien Miller0e220db2004-06-15 10:34:08 +1000658channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000659{
660 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000661
Ben Lindstrome9c99912001-06-09 00:41:05 +0000662 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000663 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000664 return;
665 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100666 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000667 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668}
Damien Miller4f7becb2006-03-26 14:10:14 +1100669
Ben Lindstrome9c99912001-06-09 00:41:05 +0000670void
Damien Miller39eda6e2005-11-05 14:52:50 +1100671channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000672{
Damien Millerd47c62a2005-12-13 19:33:57 +1100673 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000674
Ben Lindstrome9c99912001-06-09 00:41:05 +0000675 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000676 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000677 return;
678 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000679 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100680 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000681}
Damien Miller4f7becb2006-03-26 14:10:14 +1100682
Ben Lindstrome9c99912001-06-09 00:41:05 +0000683void
684channel_cancel_cleanup(int id)
685{
Damien Millerd47c62a2005-12-13 19:33:57 +1100686 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000687
Ben Lindstrome9c99912001-06-09 00:41:05 +0000688 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000689 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 return;
691 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000692 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100693 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694}
Damien Miller4f7becb2006-03-26 14:10:14 +1100695
Ben Lindstrome9c99912001-06-09 00:41:05 +0000696void
Damien Miller077b2382005-12-31 16:22:32 +1100697channel_register_filter(int id, channel_infilter_fn *ifn,
698 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000699{
700 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_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000704 return;
705 }
Damien Miller077b2382005-12-31 16:22:32 +1100706 c->input_filter = ifn;
707 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000708}
709
710void
711channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100712 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000713{
714 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000715
Ben Lindstrome9c99912001-06-09 00:41:05 +0000716 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
717 fatal("channel_activate for non-larval channel %d.", id);
718 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
719 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100720 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000721 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
722 packet_put_int(c->remote_id);
723 packet_put_int(c->local_window);
724 packet_send();
725}
726
Damien Miller5428f641999-11-25 11:54:57 +1100727/*
Damien Millerb38eff82000-04-01 11:09:21 +1000728 * 'channel_pre*' are called just before select() to add any bits relevant to
729 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100730 */
Damien Millerb38eff82000-04-01 11:09:21 +1000731/*
732 * 'channel_post*': perform any appropriate operations for channels which
733 * have events pending.
734 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100735typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000736chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
737chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
738
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100740channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000741{
742 FD_SET(c->sock, readset);
743}
744
Ben Lindstrombba81212001-06-25 05:01:22 +0000745static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100746channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000747{
748 debug3("channel %d: waiting for connection", c->self);
749 FD_SET(c->sock, writeset);
750}
751
Ben Lindstrombba81212001-06-25 05:01:22 +0000752static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100753channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000754{
755 if (buffer_len(&c->input) < packet_get_maxsize())
756 FD_SET(c->sock, readset);
757 if (buffer_len(&c->output) > 0)
758 FD_SET(c->sock, writeset);
759}
760
Ben Lindstrombba81212001-06-25 05:01:22 +0000761static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100762channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000763{
Damien Millerde6987c2002-01-22 23:20:40 +1100764 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000765
Damien Miller33b13562000-04-04 14:38:59 +1000766 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100767 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000768 buffer_len(&c->input) < limit &&
769 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000770 FD_SET(c->rfd, readset);
771 if (c->ostate == CHAN_OUTPUT_OPEN ||
772 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
773 if (buffer_len(&c->output) > 0) {
774 FD_SET(c->wfd, writeset);
775 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000776 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000777 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
778 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000779 else
780 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000781 }
782 }
783 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100784 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000785 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
786 buffer_len(&c->extended) > 0)
787 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000788 else if (!(c->flags & CHAN_EOF_SENT) &&
789 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000790 buffer_len(&c->extended) < c->remote_window)
791 FD_SET(c->efd, readset);
792 }
Damien Miller0e220db2004-06-15 10:34:08 +1000793 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000794 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000795 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
796 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000797}
798
Ben Lindstrombba81212001-06-25 05:01:22 +0000799static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100800channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000801{
802 if (buffer_len(&c->input) == 0) {
803 packet_start(SSH_MSG_CHANNEL_CLOSE);
804 packet_put_int(c->remote_id);
805 packet_send();
806 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000807 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000808 }
809}
810
Ben Lindstrombba81212001-06-25 05:01:22 +0000811static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100812channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000813{
814 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000815 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000816 else
Damien Millerb38eff82000-04-01 11:09:21 +1000817 FD_SET(c->sock, writeset);
818}
819
820/*
821 * This is a special state for X11 authentication spoofing. An opened X11
822 * connection (when authentication spoofing is being done) remains in this
823 * state until the first packet has been completely read. The authentication
824 * data in that packet is then substituted by the real data if it matches the
825 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000826 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000827 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000828 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000830x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000831{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000832 u_char *ucp;
833 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000834
835 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000836 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000837 return 0;
838
839 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100840 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000841 if (ucp[0] == 0x42) { /* Byte order MSB first. */
842 proto_len = 256 * ucp[6] + ucp[7];
843 data_len = 256 * ucp[8] + ucp[9];
844 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
845 proto_len = ucp[6] + 256 * ucp[7];
846 data_len = ucp[8] + 256 * ucp[9];
847 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000848 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100849 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000850 return -1;
851 }
852
853 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000854 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000855 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
856 return 0;
857
858 /* Check if authentication protocol matches. */
859 if (proto_len != strlen(x11_saved_proto) ||
860 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000861 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000862 return -1;
863 }
864 /* Check if authentication data matches our fake data. */
865 if (data_len != x11_fake_data_len ||
866 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
867 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000868 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000869 return -1;
870 }
871 /* Check fake data length */
872 if (x11_fake_data_len != x11_saved_data_len) {
873 error("X11 fake_data_len %d != saved_data_len %d",
874 x11_fake_data_len, x11_saved_data_len);
875 return -1;
876 }
877 /*
878 * Received authentication protocol and data match
879 * our fake data. Substitute the fake data with real
880 * data.
881 */
882 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
883 x11_saved_data, x11_saved_data_len);
884 return 1;
885}
886
Ben Lindstrombba81212001-06-25 05:01:22 +0000887static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100888channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000889{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000890 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000891
Damien Millerb38eff82000-04-01 11:09:21 +1000892 if (ret == 1) {
893 /* Start normal processing for the channel. */
894 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000895 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000896 } else if (ret == -1) {
897 /*
898 * We have received an X11 connection that has bad
899 * authentication information.
900 */
Damien Miller996acd22003-04-09 20:59:48 +1000901 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000902 buffer_clear(&c->input);
903 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000904 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000905 c->sock = -1;
906 c->type = SSH_CHANNEL_CLOSED;
907 packet_start(SSH_MSG_CHANNEL_CLOSE);
908 packet_put_int(c->remote_id);
909 packet_send();
910 }
911}
912
Ben Lindstrombba81212001-06-25 05:01:22 +0000913static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100914channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000915{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000916 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000917
918 /* c->force_drain = 1; */
919
Damien Millerb38eff82000-04-01 11:09:21 +1000920 if (ret == 1) {
921 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100922 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000923 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000924 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000925 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100926 chan_read_failed(c);
927 buffer_clear(&c->input);
928 chan_ibuf_empty(c);
929 buffer_clear(&c->output);
930 /* for proto v1, the peer will send an IEOF */
931 if (compat20)
932 chan_write_failed(c);
933 else
934 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000935 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000936 }
937}
938
Ben Lindstromb3921512001-04-11 15:57:50 +0000939/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000940static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100941channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000942{
Damien Millera10f5612002-09-12 09:49:15 +1000943 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000944 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100945 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000946 struct {
947 u_int8_t version;
948 u_int8_t command;
949 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000950 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000951 } s4_req, s4_rsp;
952
Ben Lindstromb3921512001-04-11 15:57:50 +0000953 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000954
955 have = buffer_len(&c->input);
956 len = sizeof(s4_req);
957 if (have < len)
958 return 0;
959 p = buffer_ptr(&c->input);
960 for (found = 0, i = len; i < have; i++) {
961 if (p[i] == '\0') {
962 found = 1;
963 break;
964 }
965 if (i > 1024) {
966 /* the peer is probably sending garbage */
967 debug("channel %d: decode socks4: too long",
968 c->self);
969 return -1;
970 }
971 }
972 if (!found)
973 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000974 buffer_get(&c->input, (char *)&s4_req.version, 1);
975 buffer_get(&c->input, (char *)&s4_req.command, 1);
976 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000977 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000978 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000979 p = buffer_ptr(&c->input);
980 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000981 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000982 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000983 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000984 c->self, len, have);
985 strlcpy(username, p, sizeof(username));
986 buffer_consume(&c->input, len);
987 buffer_consume(&c->input, 1); /* trailing '\0' */
988
Ben Lindstromb3921512001-04-11 15:57:50 +0000989 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000990 strlcpy(c->path, host, sizeof(c->path));
991 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100992
Damien Millerfbdeece2003-09-02 22:52:31 +1000993 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000994 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000995
Ben Lindstromb3921512001-04-11 15:57:50 +0000996 if (s4_req.command != 1) {
997 debug("channel %d: cannot handle: socks4 cn %d",
998 c->self, s4_req.command);
999 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001000 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001001 s4_rsp.version = 0; /* vn: 0 for reply */
1002 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001003 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001004 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001005 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001006 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001007}
1008
Darren Tucker46471c92003-07-03 13:55:19 +10001009/* try to decode a socks5 header */
1010#define SSH_SOCKS5_AUTHDONE 0x1000
1011#define SSH_SOCKS5_NOAUTH 0x00
1012#define SSH_SOCKS5_IPV4 0x01
1013#define SSH_SOCKS5_DOMAIN 0x03
1014#define SSH_SOCKS5_IPV6 0x04
1015#define SSH_SOCKS5_CONNECT 0x01
1016#define SSH_SOCKS5_SUCCESS 0x00
1017
1018static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001019channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001020{
1021 struct {
1022 u_int8_t version;
1023 u_int8_t command;
1024 u_int8_t reserved;
1025 u_int8_t atyp;
1026 } s5_req, s5_rsp;
1027 u_int16_t dest_port;
1028 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001029 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001030
1031 debug2("channel %d: decode socks5", c->self);
1032 p = buffer_ptr(&c->input);
1033 if (p[0] != 0x05)
1034 return -1;
1035 have = buffer_len(&c->input);
1036 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1037 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001038 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001039 return 0;
1040 nmethods = p[1];
1041 if (have < nmethods + 2)
1042 return 0;
1043 /* look for method: "NO AUTHENTICATION REQUIRED" */
1044 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1045 if (p[i] == SSH_SOCKS5_NOAUTH ) {
1046 found = 1;
1047 break;
1048 }
1049 }
1050 if (!found) {
1051 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1052 c->self);
1053 return -1;
1054 }
1055 buffer_consume(&c->input, nmethods + 2);
1056 buffer_put_char(&c->output, 0x05); /* version */
1057 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1058 FD_SET(c->sock, writeset);
1059 c->flags |= SSH_SOCKS5_AUTHDONE;
1060 debug2("channel %d: socks5 auth done", c->self);
1061 return 0; /* need more */
1062 }
1063 debug2("channel %d: socks5 post auth", c->self);
1064 if (have < sizeof(s5_req)+1)
1065 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001066 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001067 if (s5_req.version != 0x05 ||
1068 s5_req.command != SSH_SOCKS5_CONNECT ||
1069 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001070 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001071 return -1;
1072 }
Darren Tucker47eede72005-03-14 23:08:12 +11001073 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001074 case SSH_SOCKS5_IPV4:
1075 addrlen = 4;
1076 af = AF_INET;
1077 break;
1078 case SSH_SOCKS5_DOMAIN:
1079 addrlen = p[sizeof(s5_req)];
1080 af = -1;
1081 break;
1082 case SSH_SOCKS5_IPV6:
1083 addrlen = 16;
1084 af = AF_INET6;
1085 break;
1086 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001087 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001088 return -1;
1089 }
Damien Miller0f077072006-07-10 22:21:02 +10001090 need = sizeof(s5_req) + addrlen + 2;
1091 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1092 need++;
1093 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001094 return 0;
1095 buffer_consume(&c->input, sizeof(s5_req));
1096 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1097 buffer_consume(&c->input, 1); /* host string length */
1098 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1099 buffer_get(&c->input, (char *)&dest_port, 2);
1100 dest_addr[addrlen] = '\0';
1101 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001102 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001103 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1104 return -1;
1105 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001106
Damien Millerfbdeece2003-09-02 22:52:31 +10001107 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001108 c->self, c->path, c->host_port, s5_req.command);
1109
1110 s5_rsp.version = 0x05;
1111 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1112 s5_rsp.reserved = 0; /* ignored */
1113 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1114 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1115 dest_port = 0; /* ignored */
1116
Damien Millera0fdce92006-03-26 14:28:50 +11001117 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1118 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1119 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001120 return 1;
1121}
1122
Ben Lindstromb3921512001-04-11 15:57:50 +00001123/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001124static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001125channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001126{
1127 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001128 u_int have;
1129 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001130
1131 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001132 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001133 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001134 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001135 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001136 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001137 /* need more */
1138 FD_SET(c->sock, readset);
1139 return;
1140 }
1141 /* try to guess the protocol */
1142 p = buffer_ptr(&c->input);
1143 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001144 case 0x04:
1145 ret = channel_decode_socks4(c, readset, writeset);
1146 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001147 case 0x05:
1148 ret = channel_decode_socks5(c, readset, writeset);
1149 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001150 default:
1151 ret = -1;
1152 break;
1153 }
1154 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001155 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001156 } else if (ret == 0) {
1157 debug2("channel %d: pre_dynamic: need more", c->self);
1158 /* need more */
1159 FD_SET(c->sock, readset);
1160 } else {
1161 /* switch to the next state */
1162 c->type = SSH_CHANNEL_OPENING;
1163 port_open_helper(c, "direct-tcpip");
1164 }
1165}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001166
Damien Millerb38eff82000-04-01 11:09:21 +10001167/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001168static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001169channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001170{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001171 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001172 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001173 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001174 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001175 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001176 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001177
1178 if (FD_ISSET(c->sock, readset)) {
1179 debug("X11 connection requested.");
1180 addrlen = sizeof(addr);
1181 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001182 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001183 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001184 channel_close_fd(&c->sock);
1185 chan_mark_dead(c);
1186 }
Damien Millerb38eff82000-04-01 11:09:21 +10001187 if (newsock < 0) {
1188 error("accept: %.100s", strerror(errno));
1189 return;
1190 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001191 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001192 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001193 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001194 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001195 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001196
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001197 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001198 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001199 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001200 if (compat20) {
1201 packet_start(SSH2_MSG_CHANNEL_OPEN);
1202 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001203 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001204 packet_put_int(nc->local_window_max);
1205 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001206 /* originator ipaddr and port */
1207 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001208 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001209 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001210 } else {
1211 packet_put_int(remote_port);
1212 }
Damien Millerbd483e72000-04-30 10:00:53 +10001213 packet_send();
1214 } else {
1215 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001216 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001217 if (packet_get_protocol_flags() &
1218 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001219 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001220 packet_send();
1221 }
Damien Millerd83ff352001-01-30 09:19:34 +11001222 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001223 }
1224}
1225
Ben Lindstrombba81212001-06-25 05:01:22 +00001226static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001227port_open_helper(Channel *c, char *rtype)
1228{
1229 int direct;
1230 char buf[1024];
1231 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001232 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001233
1234 direct = (strcmp(rtype, "direct-tcpip") == 0);
1235
1236 snprintf(buf, sizeof buf,
1237 "%s: listening port %d for %.100s port %d, "
1238 "connect from %.200s port %d",
1239 rtype, c->listening_port, c->path, c->host_port,
1240 remote_ipaddr, remote_port);
1241
1242 xfree(c->remote_name);
1243 c->remote_name = xstrdup(buf);
1244
1245 if (compat20) {
1246 packet_start(SSH2_MSG_CHANNEL_OPEN);
1247 packet_put_cstring(rtype);
1248 packet_put_int(c->self);
1249 packet_put_int(c->local_window_max);
1250 packet_put_int(c->local_maxpacket);
1251 if (direct) {
1252 /* target host, port */
1253 packet_put_cstring(c->path);
1254 packet_put_int(c->host_port);
1255 } else {
1256 /* listen address, port */
1257 packet_put_cstring(c->path);
1258 packet_put_int(c->listening_port);
1259 }
1260 /* originator host and port */
1261 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001262 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001263 packet_send();
1264 } else {
1265 packet_start(SSH_MSG_PORT_OPEN);
1266 packet_put_int(c->self);
1267 packet_put_cstring(c->path);
1268 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001269 if (packet_get_protocol_flags() &
1270 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001271 packet_put_cstring(c->remote_name);
1272 packet_send();
1273 }
1274 xfree(remote_ipaddr);
1275}
1276
Damien Miller5e7fd072005-11-05 14:53:39 +11001277static void
1278channel_set_reuseaddr(int fd)
1279{
1280 int on = 1;
1281
1282 /*
1283 * Set socket options.
1284 * Allow local port reuse in TIME_WAIT.
1285 */
1286 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1287 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1288}
1289
Damien Millerb38eff82000-04-01 11:09:21 +10001290/*
1291 * This socket is listening for connections to a forwarded TCP/IP port.
1292 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001293static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001294channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001295{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001296 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001297 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001298 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001299 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001300 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001301
Damien Millerb38eff82000-04-01 11:09:21 +10001302 if (FD_ISSET(c->sock, readset)) {
1303 debug("Connection to port %d forwarding "
1304 "to %.100s port %d requested.",
1305 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001306
Damien Miller4623a752001-10-10 15:03:58 +10001307 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1308 nextstate = SSH_CHANNEL_OPENING;
1309 rtype = "forwarded-tcpip";
1310 } else {
1311 if (c->host_port == 0) {
1312 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001313 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001314 } else {
1315 nextstate = SSH_CHANNEL_OPENING;
1316 rtype = "direct-tcpip";
1317 }
1318 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001319
Damien Millerb38eff82000-04-01 11:09:21 +10001320 addrlen = sizeof(addr);
1321 newsock = accept(c->sock, &addr, &addrlen);
1322 if (newsock < 0) {
1323 error("accept: %.100s", strerror(errno));
1324 return;
1325 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001326 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001327 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1328 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001329 nc->listening_port = c->listening_port;
1330 nc->host_port = c->host_port;
1331 strlcpy(nc->path, c->path, sizeof(nc->path));
1332
Damien Miller4623a752001-10-10 15:03:58 +10001333 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1334 /*
1335 * do not call the channel_post handler until
1336 * this flag has been reset by a pre-handler.
1337 * otherwise the FD_ISSET calls might overflow
1338 */
1339 nc->delayed = 1;
1340 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001341 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001342 }
Damien Millerb38eff82000-04-01 11:09:21 +10001343 }
1344}
1345
1346/*
1347 * This is the authentication agent socket listening for connections from
1348 * clients.
1349 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001350static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001351channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001352{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001353 Channel *nc;
1354 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001355 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001356 socklen_t addrlen;
1357
1358 if (FD_ISSET(c->sock, readset)) {
1359 addrlen = sizeof(addr);
1360 newsock = accept(c->sock, &addr, &addrlen);
1361 if (newsock < 0) {
1362 error("accept from auth socket: %.100s", strerror(errno));
1363 return;
1364 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001365 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001366 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1367 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001368 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001369 if (compat20) {
1370 packet_start(SSH2_MSG_CHANNEL_OPEN);
1371 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001372 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001373 packet_put_int(c->local_window_max);
1374 packet_put_int(c->local_maxpacket);
1375 } else {
1376 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001377 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001378 }
Damien Millerb38eff82000-04-01 11:09:21 +10001379 packet_send();
1380 }
1381}
1382
Ben Lindstrombba81212001-06-25 05:01:22 +00001383static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001384channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001385{
Ben Lindstrom69128662001-05-08 20:07:39 +00001386 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001387 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001388
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001389 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001390 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001391 err = errno;
1392 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001393 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001394 if (err == 0) {
1395 debug("channel %d: connected", c->self);
1396 c->type = SSH_CHANNEL_OPEN;
1397 if (compat20) {
1398 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1399 packet_put_int(c->remote_id);
1400 packet_put_int(c->self);
1401 packet_put_int(c->local_window);
1402 packet_put_int(c->local_maxpacket);
1403 } else {
1404 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1405 packet_put_int(c->remote_id);
1406 packet_put_int(c->self);
1407 }
1408 } else {
1409 debug("channel %d: not connected: %s",
1410 c->self, strerror(err));
1411 if (compat20) {
1412 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1413 packet_put_int(c->remote_id);
1414 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1415 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1416 packet_put_cstring(strerror(err));
1417 packet_put_cstring("");
1418 }
1419 } else {
1420 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1421 packet_put_int(c->remote_id);
1422 }
1423 chan_mark_dead(c);
1424 }
1425 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001426 }
1427}
1428
Ben Lindstrombba81212001-06-25 05:01:22 +00001429static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001430channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001431{
Darren Tucker11327cc2005-03-14 23:22:25 +11001432 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001433 int len;
1434
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001435 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001436 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001437 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001438 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001439 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1440 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001441#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001442 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001443#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001444 if ((!c->isatty && len <= 0) ||
1445 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001446#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001447 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001448 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001449 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001450 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001451 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001452 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001453 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001454 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001455 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001456 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001457 } else {
1458 chan_read_failed(c);
1459 }
1460 return -1;
1461 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001462 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001463 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001464 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001465 chan_read_failed(c);
1466 }
Damien Millerd27b9472005-12-13 19:29:02 +11001467 } else if (c->datagram) {
1468 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001469 } else {
1470 buffer_append(&c->input, buf, len);
1471 }
Damien Millerb38eff82000-04-01 11:09:21 +10001472 }
1473 return 1;
1474}
Damien Miller4f7becb2006-03-26 14:10:14 +11001475
Ben Lindstrombba81212001-06-25 05:01:22 +00001476static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001477channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001478{
Ben Lindstrome229b252001-03-05 06:28:06 +00001479 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001480 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001481 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001482 int len;
1483
1484 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001485 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001486 FD_ISSET(c->wfd, writeset) &&
1487 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001488 if (c->output_filter != NULL) {
1489 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1490 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001491 if (c->type != SSH_CHANNEL_OPEN)
1492 chan_mark_dead(c);
1493 else
1494 chan_write_failed(c);
1495 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001496 }
1497 } else if (c->datagram) {
1498 buf = data = buffer_get_string(&c->output, &dlen);
1499 } else {
1500 buf = data = buffer_ptr(&c->output);
1501 dlen = buffer_len(&c->output);
1502 }
1503
Damien Millerd27b9472005-12-13 19:29:02 +11001504 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001505 /* ignore truncated writes, datagrams might get lost */
1506 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001507 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001508 xfree(data);
1509 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1510 return 1;
1511 if (len <= 0) {
1512 if (c->type != SSH_CHANNEL_OPEN)
1513 chan_mark_dead(c);
1514 else
1515 chan_write_failed(c);
1516 return -1;
1517 }
1518 return 1;
1519 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001520#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001521 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001522 if (compat20 && c->wfd_isatty)
1523 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001524#endif
Damien Miller077b2382005-12-31 16:22:32 +11001525
1526 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001527 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1528 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001529 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001530 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001531 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001532 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001533 return -1;
1534 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001535 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001536 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001537 c->type = SSH_CHANNEL_INPUT_DRAINING;
1538 } else {
1539 chan_write_failed(c);
1540 }
1541 return -1;
1542 }
Damien Miller077b2382005-12-31 16:22:32 +11001543 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001544 if (tcgetattr(c->wfd, &tio) == 0 &&
1545 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1546 /*
1547 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001548 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001549 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001550 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001551 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001552 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001553 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001554 }
1555 }
Damien Millerb38eff82000-04-01 11:09:21 +10001556 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001557 if (compat20 && len > 0) {
1558 c->local_consumed += len;
1559 }
1560 }
1561 return 1;
1562}
Damien Miller4f7becb2006-03-26 14:10:14 +11001563
Ben Lindstrombba81212001-06-25 05:01:22 +00001564static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001565channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001566{
Darren Tucker11327cc2005-03-14 23:22:25 +11001567 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001568 int len;
1569
Damien Miller1383bd82000-04-06 12:32:37 +10001570/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001571 if (c->efd != -1) {
1572 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1573 FD_ISSET(c->efd, writeset) &&
1574 buffer_len(&c->extended) > 0) {
1575 len = write(c->efd, buffer_ptr(&c->extended),
1576 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001577 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001578 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001579 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1580 return 1;
1581 if (len <= 0) {
1582 debug2("channel %d: closing write-efd %d",
1583 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001584 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001585 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001586 buffer_consume(&c->extended, len);
1587 c->local_consumed += len;
1588 }
1589 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1590 FD_ISSET(c->efd, readset)) {
1591 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001592 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001593 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001594 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1595 return 1;
1596 if (len <= 0) {
1597 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001598 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001599 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001600 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001601 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001602 }
Damien Miller33b13562000-04-04 14:38:59 +10001603 }
1604 }
1605 return 1;
1606}
Damien Miller4f7becb2006-03-26 14:10:14 +11001607
Ben Lindstrombba81212001-06-25 05:01:22 +00001608static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001609channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001610{
1611 char buf[16];
1612 int len;
1613
1614 /* Monitor control fd to detect if the slave client exits */
1615 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1616 len = read(c->ctl_fd, buf, sizeof(buf));
1617 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1618 return 1;
1619 if (len <= 0) {
1620 debug2("channel %d: ctl read<=0", c->self);
1621 if (c->type != SSH_CHANNEL_OPEN) {
1622 debug2("channel %d: not open", c->self);
1623 chan_mark_dead(c);
1624 return -1;
1625 } else {
1626 chan_read_failed(c);
1627 chan_write_failed(c);
1628 }
1629 return -1;
1630 } else
1631 fatal("%s: unexpected data on ctl fd", __func__);
1632 }
1633 return 1;
1634}
Damien Miller4f7becb2006-03-26 14:10:14 +11001635
Damien Miller0e220db2004-06-15 10:34:08 +10001636static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001637channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001638{
Ben Lindstromb3921512001-04-11 15:57:50 +00001639 if (c->type == SSH_CHANNEL_OPEN &&
1640 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001641 c->local_window < c->local_window_max/2 &&
1642 c->local_consumed > 0) {
1643 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1644 packet_put_int(c->remote_id);
1645 packet_put_int(c->local_consumed);
1646 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001647 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001648 c->self, c->local_window,
1649 c->local_consumed);
1650 c->local_window += c->local_consumed;
1651 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001652 }
1653 return 1;
1654}
1655
Ben Lindstrombba81212001-06-25 05:01:22 +00001656static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001657channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001658{
Damien Miller4623a752001-10-10 15:03:58 +10001659 if (c->delayed)
1660 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001661 channel_handle_rfd(c, readset, writeset);
1662 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001663 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001664 return;
Damien Miller33b13562000-04-04 14:38:59 +10001665 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001666 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001667 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001668}
1669
Ben Lindstrombba81212001-06-25 05:01:22 +00001670static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001671channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001672{
1673 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001674
Damien Millerb38eff82000-04-01 11:09:21 +10001675 /* Send buffered output data to the socket. */
1676 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1677 len = write(c->sock, buffer_ptr(&c->output),
1678 buffer_len(&c->output));
1679 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001680 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001681 else
1682 buffer_consume(&c->output, len);
1683 }
1684}
1685
Ben Lindstrombba81212001-06-25 05:01:22 +00001686static void
Damien Miller33b13562000-04-04 14:38:59 +10001687channel_handler_init_20(void)
1688{
Damien Millerde6987c2002-01-22 23:20:40 +11001689 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001690 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001691 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001692 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001693 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001694 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001695 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001696 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001697
Damien Millerde6987c2002-01-22 23:20:40 +11001698 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001699 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001700 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001701 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001702 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001703 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001704 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001705}
1706
Ben Lindstrombba81212001-06-25 05:01:22 +00001707static void
Damien Millerb38eff82000-04-01 11:09:21 +10001708channel_handler_init_13(void)
1709{
1710 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1711 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1712 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1713 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1714 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1715 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1716 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001717 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001718 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001719
Damien Millerde6987c2002-01-22 23:20:40 +11001720 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001721 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1722 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1723 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1724 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001725 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001726 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001727}
1728
Ben Lindstrombba81212001-06-25 05:01:22 +00001729static void
Damien Millerb38eff82000-04-01 11:09:21 +10001730channel_handler_init_15(void)
1731{
Damien Millerde6987c2002-01-22 23:20:40 +11001732 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001733 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001734 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1735 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1736 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001737 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001738 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001739
1740 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1741 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1742 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001743 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001744 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001745 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001746}
1747
Ben Lindstrombba81212001-06-25 05:01:22 +00001748static void
Damien Millerb38eff82000-04-01 11:09:21 +10001749channel_handler_init(void)
1750{
1751 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001752
Damien Miller9f0f5c62001-12-21 14:45:46 +11001753 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001754 channel_pre[i] = NULL;
1755 channel_post[i] = NULL;
1756 }
Damien Miller33b13562000-04-04 14:38:59 +10001757 if (compat20)
1758 channel_handler_init_20();
1759 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001760 channel_handler_init_13();
1761 else
1762 channel_handler_init_15();
1763}
1764
Damien Miller3ec27592001-10-12 11:35:04 +10001765/* gc dead channels */
1766static void
1767channel_garbage_collect(Channel *c)
1768{
1769 if (c == NULL)
1770 return;
1771 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001772 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001773 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001774 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001775 c->detach_user(c->self, NULL);
1776 /* if we still have a callback */
1777 if (c->detach_user != NULL)
1778 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001779 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001780 }
1781 if (!chan_is_dead(c, 1))
1782 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001783 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001784 channel_free(c);
1785}
1786
Ben Lindstrombba81212001-06-25 05:01:22 +00001787static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001788channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001789{
1790 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001791 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001792 Channel *c;
1793
1794 if (!did_init) {
1795 channel_handler_init();
1796 did_init = 1;
1797 }
1798 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001799 c = channels[i];
1800 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001801 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001802 if (ftab[c->type] != NULL)
1803 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001804 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001805 }
1806}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807
Ben Lindstrome9c99912001-06-09 00:41:05 +00001808/*
1809 * Allocate/update select bitmasks and add any bits relevant to channels in
1810 * select bitmasks.
1811 */
Damien Miller4af51302000-04-16 11:18:38 +10001812void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001813channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001814 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001815{
Damien Miller36812092006-03-26 14:22:47 +11001816 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001817
1818 n = MAX(*maxfdp, channel_max_fd);
1819
Damien Miller36812092006-03-26 14:22:47 +11001820 nfdset = howmany(n+1, NFDBITS);
1821 /* Explicitly test here, because xrealloc isn't always called */
1822 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1823 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1824 sz = nfdset * sizeof(fd_mask);
1825
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001826 /* perhaps check sz < nalloc/2 and shrink? */
1827 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001828 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1829 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001830 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001831 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001832 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001833 memset(*readsetp, 0, sz);
1834 memset(*writesetp, 0, sz);
1835
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001836 if (!rekeying)
1837 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001838}
1839
Ben Lindstrome9c99912001-06-09 00:41:05 +00001840/*
1841 * After select, perform any appropriate operations for channels which have
1842 * events pending.
1843 */
Damien Miller4af51302000-04-16 11:18:38 +10001844void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001845channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001846{
Damien Millerb38eff82000-04-01 11:09:21 +10001847 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001848}
1849
Ben Lindstrome9c99912001-06-09 00:41:05 +00001850
Damien Miller5e953212001-01-30 09:14:00 +11001851/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001852void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001853channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001854{
Damien Millerb38eff82000-04-01 11:09:21 +10001855 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001856 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001857
Damien Miller95def091999-11-25 00:26:21 +11001858 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001859 c = channels[i];
1860 if (c == NULL)
1861 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001862
Ben Lindstrome9c99912001-06-09 00:41:05 +00001863 /*
1864 * We are only interested in channels that can have buffered
1865 * incoming data.
1866 */
Damien Miller34132e52000-01-14 15:45:46 +11001867 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001868 if (c->type != SSH_CHANNEL_OPEN &&
1869 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001870 continue;
1871 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001872 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001873 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001874 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001875 if (compat20 &&
1876 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001877 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001878 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001879 continue;
1880 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881
Damien Miller95def091999-11-25 00:26:21 +11001882 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001883 if ((c->istate == CHAN_INPUT_OPEN ||
1884 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1885 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001886 if (c->datagram) {
1887 if (len > 0) {
1888 u_char *data;
1889 u_int dlen;
1890
1891 data = buffer_get_string(&c->input,
1892 &dlen);
1893 packet_start(SSH2_MSG_CHANNEL_DATA);
1894 packet_put_int(c->remote_id);
1895 packet_put_string(data, dlen);
1896 packet_send();
1897 c->remote_window -= dlen + 4;
1898 xfree(data);
1899 }
1900 continue;
1901 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001902 /*
1903 * Send some data for the other side over the secure
1904 * connection.
1905 */
Damien Miller33b13562000-04-04 14:38:59 +10001906 if (compat20) {
1907 if (len > c->remote_window)
1908 len = c->remote_window;
1909 if (len > c->remote_maxpacket)
1910 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001911 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001912 if (packet_is_interactive()) {
1913 if (len > 1024)
1914 len = 512;
1915 } else {
1916 /* Keep the packets at reasonable size. */
1917 if (len > packet_get_maxsize()/2)
1918 len = packet_get_maxsize()/2;
1919 }
Damien Miller95def091999-11-25 00:26:21 +11001920 }
Damien Millerb38eff82000-04-01 11:09:21 +10001921 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001922 packet_start(compat20 ?
1923 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001924 packet_put_int(c->remote_id);
1925 packet_put_string(buffer_ptr(&c->input), len);
1926 packet_send();
1927 buffer_consume(&c->input, len);
1928 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001929 }
1930 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001931 if (compat13)
1932 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001933 /*
1934 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001935 * tell peer, that we will not send more data: send IEOF.
1936 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001937 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001938 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001939 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1940 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001941 else
1942 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001943 }
Damien Miller33b13562000-04-04 14:38:59 +10001944 /* Send extended data, i.e. stderr */
1945 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001946 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001947 c->remote_window > 0 &&
1948 (len = buffer_len(&c->extended)) > 0 &&
1949 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001950 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001951 c->self, c->remote_window, buffer_len(&c->extended),
1952 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001953 if (len > c->remote_window)
1954 len = c->remote_window;
1955 if (len > c->remote_maxpacket)
1956 len = c->remote_maxpacket;
1957 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1958 packet_put_int(c->remote_id);
1959 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1960 packet_put_string(buffer_ptr(&c->extended), len);
1961 packet_send();
1962 buffer_consume(&c->extended, len);
1963 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001964 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001965 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001966 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001967}
1968
Ben Lindstrome9c99912001-06-09 00:41:05 +00001969
1970/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001971
1972/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001973void
Damien Miller630d6f42002-01-22 23:17:30 +11001974channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001975{
Damien Miller34132e52000-01-14 15:45:46 +11001976 int id;
Damien Miller95def091999-11-25 00:26:21 +11001977 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001978 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001979 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001980
Damien Miller95def091999-11-25 00:26:21 +11001981 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001982 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001983 c = channel_lookup(id);
1984 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001985 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001986
Damien Miller95def091999-11-25 00:26:21 +11001987 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001988 if (c->type != SSH_CHANNEL_OPEN &&
1989 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001990 return;
1991
Damien Miller95def091999-11-25 00:26:21 +11001992 /* Get the data. */
1993 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001994
Damien Millera04ad492004-01-21 11:02:09 +11001995 /*
1996 * Ignore data for protocol > 1.3 if output end is no longer open.
1997 * For protocol 2 the sending side is reducing its window as it sends
1998 * data, so we must 'fake' consumption of the data in order to ensure
1999 * that window updates are sent back. Otherwise the connection might
2000 * deadlock.
2001 */
2002 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2003 if (compat20) {
2004 c->local_window -= data_len;
2005 c->local_consumed += data_len;
2006 }
2007 xfree(data);
2008 return;
2009 }
2010
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002011 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002012 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002013 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002014 c->self, data_len, c->local_maxpacket);
2015 }
2016 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002017 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002018 c->self, data_len, c->local_window);
2019 xfree(data);
2020 return;
2021 }
2022 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002023 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002024 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002025 if (c->datagram)
2026 buffer_put_string(&c->output, data, data_len);
2027 else
2028 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002029 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002031
Damien Millerd79b4242006-03-31 23:11:44 +11002032/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002033void
Damien Miller630d6f42002-01-22 23:17:30 +11002034channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002035{
2036 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002037 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002038 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002039 Channel *c;
2040
2041 /* Get the channel number and verify it. */
2042 id = packet_get_int();
2043 c = channel_lookup(id);
2044
2045 if (c == NULL)
2046 packet_disconnect("Received extended_data for bad channel %d.", id);
2047 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002048 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002049 return;
2050 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002051 if (c->flags & CHAN_EOF_RCVD) {
2052 if (datafellows & SSH_BUG_EXTEOF)
2053 debug("channel %d: accepting ext data after eof", id);
2054 else
2055 packet_disconnect("Received extended_data after EOF "
2056 "on channel %d.", id);
2057 }
Damien Miller33b13562000-04-04 14:38:59 +10002058 tcode = packet_get_int();
2059 if (c->efd == -1 ||
2060 c->extended_usage != CHAN_EXTENDED_WRITE ||
2061 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002062 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002063 return;
2064 }
2065 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002066 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002067 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002068 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002069 c->self, data_len, c->local_window);
2070 xfree(data);
2071 return;
2072 }
Damien Millerd3444942000-09-30 14:20:03 +11002073 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002074 c->local_window -= data_len;
2075 buffer_append(&c->extended, data, data_len);
2076 xfree(data);
2077}
2078
Damien Millerd79b4242006-03-31 23:11:44 +11002079/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002080void
Damien Miller630d6f42002-01-22 23:17:30 +11002081channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002082{
2083 int id;
2084 Channel *c;
2085
Damien Millerb38eff82000-04-01 11:09:21 +10002086 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002087 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002088 c = channel_lookup(id);
2089 if (c == NULL)
2090 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2091 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002092
2093 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002094 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002095 debug("channel %d: FORCE input drain", c->self);
2096 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002097 if (buffer_len(&c->input) == 0)
2098 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002099 }
2100
Damien Millerb38eff82000-04-01 11:09:21 +10002101}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002102
Damien Millerd79b4242006-03-31 23:11:44 +11002103/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002104void
Damien Miller630d6f42002-01-22 23:17:30 +11002105channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002106{
Damien Millerb38eff82000-04-01 11:09:21 +10002107 int id;
2108 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002109
Damien Millerb38eff82000-04-01 11:09:21 +10002110 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002111 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002112 c = channel_lookup(id);
2113 if (c == NULL)
2114 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002115
2116 /*
2117 * Send a confirmation that we have closed the channel and no more
2118 * data is coming for it.
2119 */
Damien Miller95def091999-11-25 00:26:21 +11002120 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002121 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002122 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002123
Damien Miller5428f641999-11-25 11:54:57 +11002124 /*
2125 * If the channel is in closed state, we have sent a close request,
2126 * and the other side will eventually respond with a confirmation.
2127 * Thus, we cannot free the channel here, because then there would be
2128 * no-one to receive the confirmation. The channel gets freed when
2129 * the confirmation arrives.
2130 */
Damien Millerb38eff82000-04-01 11:09:21 +10002131 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002132 /*
2133 * Not a closed channel - mark it as draining, which will
2134 * cause it to be freed later.
2135 */
Damien Miller76765c02002-01-22 23:21:15 +11002136 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002137 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002138 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139}
2140
Damien Millerb38eff82000-04-01 11:09:21 +10002141/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002142/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002143void
Damien Miller630d6f42002-01-22 23:17:30 +11002144channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002145{
Damien Millerb38eff82000-04-01 11:09:21 +10002146 int id = packet_get_int();
2147 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002148
Damien Miller48b03fc2002-01-22 23:11:40 +11002149 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002150 if (c == NULL)
2151 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2152 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002153}
2154
Damien Millerd79b4242006-03-31 23:11:44 +11002155/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002156void
Damien Miller630d6f42002-01-22 23:17:30 +11002157channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002158{
2159 int id = packet_get_int();
2160 Channel *c = channel_lookup(id);
2161
Damien Miller48b03fc2002-01-22 23:11:40 +11002162 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002163 if (c == NULL)
2164 packet_disconnect("Received close confirmation for "
2165 "out-of-range channel %d.", id);
2166 if (c->type != SSH_CHANNEL_CLOSED)
2167 packet_disconnect("Received close confirmation for "
2168 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002169 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002170}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171
Damien Millerd79b4242006-03-31 23:11:44 +11002172/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002173void
Damien Miller630d6f42002-01-22 23:17:30 +11002174channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002175{
Damien Millerb38eff82000-04-01 11:09:21 +10002176 int id, remote_id;
2177 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002178
Damien Millerb38eff82000-04-01 11:09:21 +10002179 id = packet_get_int();
2180 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002181
Damien Millerb38eff82000-04-01 11:09:21 +10002182 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2183 packet_disconnect("Received open confirmation for "
2184 "non-opening channel %d.", id);
2185 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002186 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002187 c->remote_id = remote_id;
2188 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002189
2190 if (compat20) {
2191 c->remote_window = packet_get_int();
2192 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002193 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002194 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002195 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002196 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002197 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002198 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002199 c->remote_window, c->remote_maxpacket);
2200 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002201 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002202}
2203
Ben Lindstrombba81212001-06-25 05:01:22 +00002204static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002205reason2txt(int reason)
2206{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002207 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002208 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2209 return "administratively prohibited";
2210 case SSH2_OPEN_CONNECT_FAILED:
2211 return "connect failed";
2212 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2213 return "unknown channel type";
2214 case SSH2_OPEN_RESOURCE_SHORTAGE:
2215 return "resource shortage";
2216 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002217 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002218}
2219
Damien Millerd79b4242006-03-31 23:11:44 +11002220/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002221void
Damien Miller630d6f42002-01-22 23:17:30 +11002222channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002223{
Kevin Steves12057502001-02-05 14:54:34 +00002224 int id, reason;
2225 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002226 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002227
Damien Millerb38eff82000-04-01 11:09:21 +10002228 id = packet_get_int();
2229 c = channel_lookup(id);
2230
2231 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2232 packet_disconnect("Received open failure for "
2233 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002234 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002235 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002236 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002237 msg = packet_get_string(NULL);
2238 lang = packet_get_string(NULL);
2239 }
Damien Miller996acd22003-04-09 20:59:48 +10002240 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002241 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002242 if (msg != NULL)
2243 xfree(msg);
2244 if (lang != NULL)
2245 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002246 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002247 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002248 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002249 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002250}
2251
Damien Millerd79b4242006-03-31 23:11:44 +11002252/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002253void
Damien Miller630d6f42002-01-22 23:17:30 +11002254channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002255{
2256 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002257 int id;
2258 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002259
2260 if (!compat20)
2261 return;
2262
2263 /* Get the channel number and verify it. */
2264 id = packet_get_int();
2265 c = channel_lookup(id);
2266
Damien Millerd47c62a2005-12-13 19:33:57 +11002267 if (c == NULL) {
2268 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002269 return;
2270 }
2271 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002272 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002273 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002274 c->remote_window += adjust;
2275}
2276
Damien Millerd79b4242006-03-31 23:11:44 +11002277/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002278void
Damien Miller630d6f42002-01-22 23:17:30 +11002279channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002280{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002281 Channel *c = NULL;
2282 u_short host_port;
2283 char *host, *originator_string;
2284 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002285
Ben Lindstrome9c99912001-06-09 00:41:05 +00002286 remote_id = packet_get_int();
2287 host = packet_get_string(NULL);
2288 host_port = packet_get_int();
2289
2290 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2291 originator_string = packet_get_string(NULL);
2292 } else {
2293 originator_string = xstrdup("unknown (remote did not supply name)");
2294 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002295 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002296 sock = channel_connect_to(host, host_port);
2297 if (sock != -1) {
2298 c = channel_new("connected socket",
2299 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2300 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002301 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002302 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002303 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002304 if (c == NULL) {
2305 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2306 packet_put_int(remote_id);
2307 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002308 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002309 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002310}
2311
2312
Ben Lindstrome9c99912001-06-09 00:41:05 +00002313/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002314
Ben Lindstrom908afed2001-10-03 17:34:59 +00002315void
2316channel_set_af(int af)
2317{
2318 IPv4or6 = af;
2319}
2320
Damien Millerb16461c2002-01-22 23:29:22 +11002321static int
2322channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2323 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002324{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002325 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002326 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002327 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002328 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002329 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002330
Damien Millerb16461c2002-01-22 23:29:22 +11002331 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2332 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002333 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002334
Damien Millerb16461c2002-01-22 23:29:22 +11002335 if (host == NULL) {
2336 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002337 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002338 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002339 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002340 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002341 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002342 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002343
Damien Miller5428f641999-11-25 11:54:57 +11002344 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002345 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002346 * specified address or wildcard. On the client, a specified bind
2347 * address will always override gateway_ports. On the server, a
2348 * gateway_ports of 1 (``yes'') will override the client's
2349 * specification and force a wildcard bind, whereas a value of 2
2350 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002351 * asked for.
2352 *
2353 * Special-case listen_addrs are:
2354 *
2355 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2356 * "" (empty string), "*" -> wildcard v4/v6
2357 * "localhost" -> loopback v4/v6
2358 */
2359 addr = NULL;
2360 if (listen_addr == NULL) {
2361 /* No address specified: default to gateway_ports setting */
2362 if (gateway_ports)
2363 wildcard = 1;
2364 } else if (gateway_ports || is_client) {
2365 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2366 strcmp(listen_addr, "0.0.0.0") == 0) ||
2367 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2368 (!is_client && gateway_ports == 1))
2369 wildcard = 1;
2370 else if (strcmp(listen_addr, "localhost") != 0)
2371 addr = listen_addr;
2372 }
2373
2374 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2375 type, wildcard, (addr == NULL) ? "NULL" : addr);
2376
2377 /*
Damien Miller34132e52000-01-14 15:45:46 +11002378 * getaddrinfo returns a loopback address if the hostname is
2379 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002380 */
Damien Miller34132e52000-01-14 15:45:46 +11002381 memset(&hints, 0, sizeof(hints));
2382 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002383 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002384 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002385 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002386 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2387 if (addr == NULL) {
2388 /* This really shouldn't happen */
2389 packet_disconnect("getaddrinfo: fatal error: %s",
2390 gai_strerror(r));
2391 } else {
Damien Millera7270302005-07-06 09:36:05 +10002392 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002393 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2394 }
Damien Millera7270302005-07-06 09:36:05 +10002395 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002396 }
Damien Miller5428f641999-11-25 11:54:57 +11002397
Damien Miller34132e52000-01-14 15:45:46 +11002398 for (ai = aitop; ai; ai = ai->ai_next) {
2399 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2400 continue;
2401 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2402 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002403 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002404 continue;
2405 }
2406 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002407 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002408 if (sock < 0) {
2409 /* this is no error since kernel may not support ipv6 */
2410 verbose("socket: %.100s", strerror(errno));
2411 continue;
2412 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002413
2414 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002415
Damien Miller34132e52000-01-14 15:45:46 +11002416 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002417
Damien Miller34132e52000-01-14 15:45:46 +11002418 /* Bind the socket to the address. */
2419 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2420 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002421 if (!ai->ai_next)
2422 error("bind: %.100s", strerror(errno));
2423 else
2424 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002425
Damien Miller34132e52000-01-14 15:45:46 +11002426 close(sock);
2427 continue;
2428 }
2429 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002430 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002431 error("listen: %.100s", strerror(errno));
2432 close(sock);
2433 continue;
2434 }
2435 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002436 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002437 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002438 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002439 strlcpy(c->path, host, sizeof(c->path));
2440 c->host_port = port_to_connect;
2441 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002442 success = 1;
2443 }
2444 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002445 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002446 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002447 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002448 return success;
Damien Miller95def091999-11-25 00:26:21 +11002449}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002450
Darren Tuckere7066df2004-05-24 10:18:05 +10002451int
2452channel_cancel_rport_listener(const char *host, u_short port)
2453{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002454 u_int i;
2455 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002456
Darren Tucker47eede72005-03-14 23:08:12 +11002457 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002458 Channel *c = channels[i];
2459
2460 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2461 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002462 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002463 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002464 channel_free(c);
2465 found = 1;
2466 }
2467 }
2468
2469 return (found);
2470}
2471
Damien Millerb16461c2002-01-22 23:29:22 +11002472/* protocol local port fwd, used by ssh (and sshd in v1) */
2473int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002474channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002475 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2476{
2477 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002478 listen_host, listen_port, host_to_connect, port_to_connect,
2479 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002480}
2481
2482/* protocol v2 remote port fwd, used by sshd */
2483int
2484channel_setup_remote_fwd_listener(const char *listen_address,
2485 u_short listen_port, int gateway_ports)
2486{
2487 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2488 listen_address, listen_port, NULL, 0, gateway_ports);
2489}
2490
Damien Miller5428f641999-11-25 11:54:57 +11002491/*
2492 * Initiate forwarding of connections to port "port" on remote host through
2493 * the secure channel to host:port from local side.
2494 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002495
Darren Tuckere7d4b192006-07-12 22:17:10 +10002496int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002497channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002498 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002499{
Damien Millerdff50992002-01-22 23:16:32 +11002500 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002501
Damien Miller95def091999-11-25 00:26:21 +11002502 /* Record locally that connection to this host/port is permitted. */
2503 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2504 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002505
Damien Miller95def091999-11-25 00:26:21 +11002506 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002507 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002508 const char *address_to_bind;
2509 if (listen_host == NULL)
2510 address_to_bind = "localhost";
2511 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2512 address_to_bind = "";
2513 else
2514 address_to_bind = listen_host;
2515
Damien Miller33b13562000-04-04 14:38:59 +10002516 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2517 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002518 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002519 packet_put_cstring(address_to_bind);
2520 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002521 packet_send();
2522 packet_write_wait();
2523 /* Assume that server accepts the request */
2524 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002525 } else {
2526 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002527 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002528 packet_put_cstring(host_to_connect);
2529 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002530 packet_send();
2531 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002532
2533 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002534 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002535 switch (type) {
2536 case SSH_SMSG_SUCCESS:
2537 success = 1;
2538 break;
2539 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002540 break;
2541 default:
2542 /* Unknown packet */
2543 packet_disconnect("Protocol error for port forward request:"
2544 "received packet type %d.", type);
2545 }
2546 }
2547 if (success) {
2548 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2549 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2550 permitted_opens[num_permitted_opens].listen_port = listen_port;
2551 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002552 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002553 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002554}
2555
Damien Miller5428f641999-11-25 11:54:57 +11002556/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002557 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002558 * local side.
2559 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002560void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002561channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002562{
2563 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002564
2565 if (!compat20)
2566 return;
2567
2568 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002569 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002570 permitted_opens[i].listen_port == port)
2571 break;
2572 }
2573 if (i >= num_permitted_opens) {
2574 debug("%s: requested forward not found", __func__);
2575 return;
2576 }
2577 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2578 packet_put_cstring("cancel-tcpip-forward");
2579 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002580 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002581 packet_put_int(port);
2582 packet_send();
2583
2584 permitted_opens[i].listen_port = 0;
2585 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002586 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002587 permitted_opens[i].host_to_connect = NULL;
2588}
2589
2590/*
Damien Miller5428f641999-11-25 11:54:57 +11002591 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2592 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002593 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002594 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002595int
Damien Millere247cc42000-05-07 12:03:14 +10002596channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002597{
Damien Milleraae6c611999-12-06 11:47:28 +11002598 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002599 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002600 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002601
Damien Miller95def091999-11-25 00:26:21 +11002602 /* Get arguments from the packet. */
2603 port = packet_get_int();
2604 hostname = packet_get_string(NULL);
2605 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002606
Damien Millerbac2d8a2000-09-05 16:13:06 +11002607#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002608 /*
2609 * Check that an unprivileged user is not trying to forward a
2610 * privileged port.
2611 */
Damien Miller95def091999-11-25 00:26:21 +11002612 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002613 packet_disconnect(
2614 "Requested forwarding of port %d but user is not root.",
2615 port);
2616 if (host_port == 0)
2617 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002618#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002619
Damien Miller0bc1bd82000-11-13 22:57:25 +11002620 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002621 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002622 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002623
2624 /* Free the argument string. */
2625 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002626
2627 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002628}
2629
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002630/*
2631 * Permits opening to any host/port if permitted_opens[] is empty. This is
2632 * usually called by the server, because the user could connect to any port
2633 * anyway, and the server has no way to know but to trust the client anyway.
2634 */
2635void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002636channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002637{
2638 if (num_permitted_opens == 0)
2639 all_opens_permitted = 1;
2640}
2641
Ben Lindstroma3700052001-04-05 23:26:32 +00002642void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002643channel_add_permitted_opens(char *host, int port)
2644{
2645 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002646 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002647 debug("allow port forwarding to host %s port %d", host, port);
2648
2649 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2650 permitted_opens[num_permitted_opens].port_to_connect = port;
2651 num_permitted_opens++;
2652
2653 all_opens_permitted = 0;
2654}
2655
Ben Lindstroma3700052001-04-05 23:26:32 +00002656void
Damien Miller9b439df2006-07-24 14:04:00 +10002657channel_add_adm_permitted_opens(char *host, int port)
2658{
2659 if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2660 fatal("channel_add_adm_permitted_opens: too many forwards");
2661 debug("allow port forwarding to host %s port %d", host, port);
2662
2663 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2664 = xstrdup(host);
2665 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
2666 num_adm_permitted_opens++;
2667}
2668
2669void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002670channel_clear_permitted_opens(void)
2671{
2672 int i;
2673
2674 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002675 if (permitted_opens[i].host_to_connect != NULL)
2676 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002677 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10002678}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002679
Damien Miller9b439df2006-07-24 14:04:00 +10002680void
2681channel_clear_adm_permitted_opens(void)
2682{
2683 int i;
2684
2685 for (i = 0; i < num_adm_permitted_opens; i++)
2686 if (permitted_adm_opens[i].host_to_connect != NULL)
2687 xfree(permitted_adm_opens[i].host_to_connect);
2688 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002689}
2690
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002691/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002692static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002693connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002694{
Damien Miller34132e52000-01-14 15:45:46 +11002695 struct addrinfo hints, *ai, *aitop;
2696 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2697 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002698 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002699
Damien Miller34132e52000-01-14 15:45:46 +11002700 memset(&hints, 0, sizeof(hints));
2701 hints.ai_family = IPv4or6;
2702 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002703 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002704 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002705 error("connect_to %.100s: unknown host (%s)", host,
2706 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002707 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002708 }
Damien Miller34132e52000-01-14 15:45:46 +11002709 for (ai = aitop; ai; ai = ai->ai_next) {
2710 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2711 continue;
2712 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2713 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002714 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002715 continue;
2716 }
Damien Miller2372ace2003-05-14 13:42:23 +10002717 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002718 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002719 if (ai->ai_next == NULL)
2720 error("socket: %.100s", strerror(errno));
2721 else
2722 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002723 continue;
2724 }
Damien Miller232711f2004-06-15 10:35:30 +10002725 if (set_nonblock(sock) == -1)
2726 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002727 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2728 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002729 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002730 strerror(errno));
2731 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002732 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002733 }
2734 break; /* success */
2735
2736 }
2737 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002738 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002739 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002740 return -1;
2741 }
2742 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002743 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002744 return sock;
2745}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002746
Damien Miller0bc1bd82000-11-13 22:57:25 +11002747int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002748channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002749{
2750 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002751
Damien Miller0bc1bd82000-11-13 22:57:25 +11002752 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002753 if (permitted_opens[i].host_to_connect != NULL &&
2754 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002755 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002756 permitted_opens[i].host_to_connect,
2757 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002758 error("WARNING: Server requests forwarding for unknown listen_port %d",
2759 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002760 return -1;
2761}
Damien Millerb38eff82000-04-01 11:09:21 +10002762
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002763/* Check if connecting to that port is permitted and connect. */
2764int
2765channel_connect_to(const char *host, u_short port)
2766{
Damien Miller9b439df2006-07-24 14:04:00 +10002767 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002768
2769 permit = all_opens_permitted;
2770 if (!permit) {
2771 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002772 if (permitted_opens[i].host_to_connect != NULL &&
2773 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002774 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2775 permit = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002776 }
Damien Miller9b439df2006-07-24 14:04:00 +10002777
2778 if (num_adm_permitted_opens > 0) {
2779 permit_adm = 0;
2780 for (i = 0; i < num_adm_permitted_opens; i++)
2781 if (permitted_adm_opens[i].host_to_connect != NULL &&
2782 permitted_adm_opens[i].port_to_connect == port &&
2783 strcmp(permitted_adm_opens[i].host_to_connect, host)
2784 == 0)
2785 permit_adm = 1;
2786 }
2787
2788 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10002789 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002790 "but the request was denied.", host, port);
2791 return -1;
2792 }
2793 return connect_to(host, port);
2794}
2795
Damien Miller0e220db2004-06-15 10:34:08 +10002796void
2797channel_send_window_changes(void)
2798{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002799 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002800 struct winsize ws;
2801
2802 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002803 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002804 channels[i]->type != SSH_CHANNEL_OPEN)
2805 continue;
2806 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2807 continue;
2808 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002809 packet_put_int((u_int)ws.ws_col);
2810 packet_put_int((u_int)ws.ws_row);
2811 packet_put_int((u_int)ws.ws_xpixel);
2812 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002813 packet_send();
2814 }
2815}
2816
Ben Lindstrome9c99912001-06-09 00:41:05 +00002817/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002818
Damien Miller5428f641999-11-25 11:54:57 +11002819/*
2820 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002821 * Returns 0 and a suitable display number for the DISPLAY variable
2822 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002823 */
Kevin Steves366298c2001-12-19 17:58:01 +00002824int
Damien Miller95c249f2002-02-05 12:11:34 +11002825x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002826 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002827{
Damien Millere7378562001-12-21 14:58:35 +11002828 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002829 int display_number, sock;
2830 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002831 struct addrinfo hints, *ai, *aitop;
2832 char strport[NI_MAXSERV];
2833 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002834
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002835 if (chanids == NULL)
2836 return -1;
2837
Damien Millera34a28b1999-12-14 10:47:15 +11002838 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002839 display_number < MAX_DISPLAYS;
2840 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002841 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002842 memset(&hints, 0, sizeof(hints));
2843 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002844 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002845 hints.ai_socktype = SOCK_STREAM;
2846 snprintf(strport, sizeof strport, "%d", port);
2847 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2848 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002849 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002850 }
Damien Miller34132e52000-01-14 15:45:46 +11002851 for (ai = aitop; ai; ai = ai->ai_next) {
2852 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2853 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002854 sock = socket(ai->ai_family, ai->ai_socktype,
2855 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002856 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002857 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002858 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002859 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002860 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002861 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002862 debug("x11_create_display_inet: Socket family %d not supported",
2863 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002864 continue;
2865 }
Damien Miller34132e52000-01-14 15:45:46 +11002866 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002867#ifdef IPV6_V6ONLY
2868 if (ai->ai_family == AF_INET6) {
2869 int on = 1;
2870 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2871 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2872 }
2873#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002874 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002875 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002876 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002877 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002878
2879 if (ai->ai_next)
2880 continue;
2881
Damien Miller34132e52000-01-14 15:45:46 +11002882 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002883 close(socks[n]);
2884 }
2885 num_socks = 0;
2886 break;
2887 }
2888 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002889#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002890 if (num_socks == NUM_SOCKS)
2891 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002892#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002893 if (x11_use_localhost) {
2894 if (num_socks == NUM_SOCKS)
2895 break;
2896 } else {
2897 break;
2898 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002899#endif
Damien Miller95def091999-11-25 00:26:21 +11002900 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002901 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002902 if (num_socks > 0)
2903 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002904 }
Damien Miller95def091999-11-25 00:26:21 +11002905 if (display_number >= MAX_DISPLAYS) {
2906 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002907 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002908 }
Damien Miller95def091999-11-25 00:26:21 +11002909 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002910 for (n = 0; n < num_socks; n++) {
2911 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002912 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002913 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002914 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002915 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002916 }
Damien Miller95def091999-11-25 00:26:21 +11002917 }
Damien Miller34132e52000-01-14 15:45:46 +11002918
Damien Miller34132e52000-01-14 15:45:46 +11002919 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002920 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002921 for (n = 0; n < num_socks; n++) {
2922 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002923 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002924 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2925 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002926 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002927 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002928 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002929 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002930 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931
Kevin Steves366298c2001-12-19 17:58:01 +00002932 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002933 *display_numberp = display_number;
2934 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002935}
2936
Ben Lindstrombba81212001-06-25 05:01:22 +00002937static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002938connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002939{
Damien Miller95def091999-11-25 00:26:21 +11002940 int sock;
2941 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002942
Damien Miller3afe3752001-12-21 12:39:51 +11002943 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2944 if (sock < 0)
2945 error("socket: %.100s", strerror(errno));
2946 memset(&addr, 0, sizeof(addr));
2947 addr.sun_family = AF_UNIX;
2948 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002949 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002950 return sock;
2951 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002952 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2953 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002954}
2955
Damien Millerbd483e72000-04-30 10:00:53 +10002956int
2957x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002958{
Damien Miller57c4e872006-03-31 23:11:07 +11002959 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002960 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002961 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002962 struct addrinfo hints, *ai, *aitop;
2963 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002964 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002965
Damien Miller95def091999-11-25 00:26:21 +11002966 /* Try to open a socket for the local X server. */
2967 display = getenv("DISPLAY");
2968 if (!display) {
2969 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002970 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002971 }
Damien Miller5428f641999-11-25 11:54:57 +11002972 /*
2973 * Now we decode the value of the DISPLAY variable and make a
2974 * connection to the real X server.
2975 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002976
Damien Miller5428f641999-11-25 11:54:57 +11002977 /*
2978 * Check if it is a unix domain socket. Unix domain displays are in
2979 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2980 */
Damien Miller95def091999-11-25 00:26:21 +11002981 if (strncmp(display, "unix:", 5) == 0 ||
2982 display[0] == ':') {
2983 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11002984 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002985 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002986 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002987 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002988 }
2989 /* Create a socket. */
2990 sock = connect_local_xsocket(display_number);
2991 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002992 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002993
2994 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002995 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002996 }
Damien Miller5428f641999-11-25 11:54:57 +11002997 /*
2998 * Connect to an inet socket. The DISPLAY value is supposedly
2999 * hostname:d[.s], where hostname may also be numeric IP address.
3000 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00003001 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11003002 cp = strchr(buf, ':');
3003 if (!cp) {
3004 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10003005 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003006 }
Damien Miller95def091999-11-25 00:26:21 +11003007 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11003008 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11003009 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003010 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003011 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003012 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003013 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003014
Damien Miller34132e52000-01-14 15:45:46 +11003015 /* Look up the host address */
3016 memset(&hints, 0, sizeof(hints));
3017 hints.ai_family = IPv4or6;
3018 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11003019 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11003020 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
3021 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10003022 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003023 }
Damien Miller34132e52000-01-14 15:45:46 +11003024 for (ai = aitop; ai; ai = ai->ai_next) {
3025 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10003026 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003027 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003028 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10003029 continue;
3030 }
3031 /* Connect it to the display. */
3032 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11003033 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10003034 6000 + display_number, strerror(errno));
3035 close(sock);
3036 continue;
3037 }
3038 /* Success */
3039 break;
Damien Miller34132e52000-01-14 15:45:46 +11003040 }
Damien Miller34132e52000-01-14 15:45:46 +11003041 freeaddrinfo(aitop);
3042 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11003043 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11003044 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003045 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003046 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003047 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003048 return sock;
3049}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003050
Damien Millerbd483e72000-04-30 10:00:53 +10003051/*
3052 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3053 * the remote channel number. We should do whatever we want, and respond
3054 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3055 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003056
Damien Millerbd483e72000-04-30 10:00:53 +10003057void
Damien Miller630d6f42002-01-22 23:17:30 +11003058x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003059{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003060 Channel *c = NULL;
3061 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003062 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003063
3064 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003065
3066 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003067
3068 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003069 remote_host = packet_get_string(NULL);
3070 } else {
3071 remote_host = xstrdup("unknown (remote did not supply name)");
3072 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003073 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003074
3075 /* Obtain a connection to the real X display. */
3076 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003077 if (sock != -1) {
3078 /* Allocate a channel for this connection. */
3079 c = channel_new("connected x11 socket",
3080 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3081 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003082 c->remote_id = remote_id;
3083 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003084 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003085 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003086 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003087 /* Send refusal to the remote host. */
3088 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003089 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003090 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003091 /* Send a confirmation to the remote host. */
3092 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003093 packet_put_int(remote_id);
3094 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003095 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003096 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003097}
3098
Damien Miller69b69aa2000-10-28 14:19:58 +11003099/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
3100void
Damien Miller630d6f42002-01-22 23:17:30 +11003101deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003102{
3103 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003104
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003105 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003106 case SSH_SMSG_AGENT_OPEN:
3107 error("Warning: ssh server tried agent forwarding.");
3108 break;
3109 case SSH_SMSG_X11_OPEN:
3110 error("Warning: ssh server tried X11 forwarding.");
3111 break;
3112 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003113 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003114 break;
3115 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003116 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003117 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3118 packet_put_int(rchan);
3119 packet_send();
3120}
3121
Damien Miller5428f641999-11-25 11:54:57 +11003122/*
3123 * Requests forwarding of X11 connections, generates fake authentication
3124 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003125 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003126 */
Damien Miller4af51302000-04-16 11:18:38 +10003127void
Damien Miller17e7ed02005-06-17 12:54:33 +10003128x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003129 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003130{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003131 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003132 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003133 char *new_data;
3134 int screen_number;
3135 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003136 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003137
Damien Millerf92c0792005-07-06 09:45:26 +10003138 if (x11_saved_display == NULL)
3139 x11_saved_display = xstrdup(disp);
3140 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003141 error("x11_request_forwarding_with_spoofing: different "
3142 "$DISPLAY already forwarded");
3143 return;
3144 }
3145
Damien Miller17e7ed02005-06-17 12:54:33 +10003146 cp = disp;
3147 if (disp)
3148 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003149 if (cp)
3150 cp = strchr(cp, '.');
3151 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003152 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003153 else
3154 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003155
Damien Miller13390022005-07-06 09:44:19 +10003156 if (x11_saved_proto == NULL) {
3157 /* Save protocol name. */
3158 x11_saved_proto = xstrdup(proto);
3159 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003160 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003161 * of the same length.
3162 */
3163 x11_saved_data = xmalloc(data_len);
3164 x11_fake_data = xmalloc(data_len);
3165 for (i = 0; i < data_len; i++) {
3166 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3167 fatal("x11_request_forwarding: bad "
3168 "authentication data: %.100s", data);
3169 if (i % 4 == 0)
3170 rnd = arc4random();
3171 x11_saved_data[i] = value;
3172 x11_fake_data[i] = rnd & 0xff;
3173 rnd >>= 8;
3174 }
3175 x11_saved_data_len = data_len;
3176 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003177 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003178
Damien Miller95def091999-11-25 00:26:21 +11003179 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003180 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003181
Damien Miller95def091999-11-25 00:26:21 +11003182 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003183 if (compat20) {
3184 channel_request_start(client_session_id, "x11-req", 0);
3185 packet_put_char(0); /* XXX bool single connection */
3186 } else {
3187 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3188 }
3189 packet_put_cstring(proto);
3190 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003191 packet_put_int(screen_number);
3192 packet_send();
3193 packet_write_wait();
3194 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003195}
3196
Ben Lindstrome9c99912001-06-09 00:41:05 +00003197
3198/* -- agent forwarding */
3199
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003200/* Sends a message to the server to request authentication fd forwarding. */
3201
Damien Miller4af51302000-04-16 11:18:38 +10003202void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003203auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003204{
Damien Miller95def091999-11-25 00:26:21 +11003205 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3206 packet_send();
3207 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003208}