blob: 80cb32461e83affac8d9cb8167523a5d41072551 [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: channels.c,v 1.263 2006/08/01 23:22:47 stevesk 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>
Damien Miller9aec9192006-08-05 10:57:45 +100048#ifdef HAVE_SYS_TIME_H
49# include <sys/time.h>
50#endif
Damien Millerefc04e72006-07-10 20:26:27 +100051
52#include <netinet/in.h>
53#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110054
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100056#include <netdb.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100057#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100058#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100059#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110060#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100061#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "ssh1.h"
65#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "packet.h"
67#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "log.h"
69#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100073#include "key.h"
74#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110075#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100076#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Ben Lindstrome9c99912001-06-09 00:41:05 +000078/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller5428f641999-11-25 11:54:57 +110080/*
81 * Pointer to an array containing all allocated channels. The array is
82 * dynamically extended as needed.
83 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000084static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller5428f641999-11-25 11:54:57 +110086/*
87 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000088 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110089 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100090static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller5428f641999-11-25 11:54:57 +110092/*
93 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000094 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110095 */
Damien Miller5e953212001-01-30 09:14:00 +110096static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Ben Lindstrome9c99912001-06-09 00:41:05 +000099/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Data structure for storing which hosts are permitted for forward requests.
103 * The local sides of any remote forwards are stored in this array to prevent
104 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
105 * network (which might be behind a firewall).
106 */
Damien Miller95def091999-11-25 00:26:21 +1100107typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000108 char *host_to_connect; /* Connect to 'host'. */
109 u_short port_to_connect; /* Connect to 'port'. */
110 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111} ForwardPermission;
112
Damien Miller9b439df2006-07-24 14:04:00 +1000113/* List of all permitted host/port pairs to connect by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000115
Damien Miller9b439df2006-07-24 14:04:00 +1000116/* List of all permitted host/port pairs to connect by the admin. */
117static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
118
119/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000121
122/* Number of permitted host/port pair in the array permitted by the admin. */
123static int num_adm_permitted_opens = 0;
124
Damien Miller5428f641999-11-25 11:54:57 +1100125/*
126 * If this is true, all opens are permitted. This is the case on the server
127 * on which we have to trust the client anyway, and the user could do
128 * anything after logging in anyway.
129 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130static int all_opens_permitted = 0;
131
Ben Lindstrome9c99912001-06-09 00:41:05 +0000132
133/* -- X11 forwarding */
134
135/* Maximum number of fake X11 displays to try. */
136#define MAX_DISPLAYS 1000
137
Damien Miller13390022005-07-06 09:44:19 +1000138/* Saved X11 local (client) display. */
139static char *x11_saved_display = NULL;
140
Ben Lindstrome9c99912001-06-09 00:41:05 +0000141/* Saved X11 authentication protocol name. */
142static char *x11_saved_proto = NULL;
143
144/* Saved X11 authentication data. This is the real data. */
145static char *x11_saved_data = NULL;
146static u_int x11_saved_data_len = 0;
147
148/*
149 * Fake X11 authentication data. This is what the server will be sending us;
150 * we should replace any occurrences of this by the real data.
151 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100152static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000153static u_int x11_fake_data_len;
154
155
156/* -- agent forwarding */
157
158#define NUM_SOCKS 10
159
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000160/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000161static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000162
Ben Lindstrome9c99912001-06-09 00:41:05 +0000163/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000164static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000165
Ben Lindstrome9c99912001-06-09 00:41:05 +0000166/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000167
168Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100169channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000170{
171 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000172
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000173 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100174 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000175 return NULL;
176 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000177 c = channels[id];
178 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100179 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000180 return NULL;
181 }
182 return c;
183}
184
Damien Miller5428f641999-11-25 11:54:57 +1100185/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100186 * Returns the channel if it is allowed to receive protocol messages.
187 * Private channels, like listening sockets, may not receive messages.
188 */
189Channel *
190channel_lookup(int id)
191{
192 Channel *c;
193
194 if ((c = channel_by_id(id)) == NULL)
195 return (NULL);
196
Damien Millerd62f2ca2006-03-26 13:57:41 +1100197 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100198 case SSH_CHANNEL_X11_OPEN:
199 case SSH_CHANNEL_LARVAL:
200 case SSH_CHANNEL_CONNECTING:
201 case SSH_CHANNEL_DYNAMIC:
202 case SSH_CHANNEL_OPENING:
203 case SSH_CHANNEL_OPEN:
204 case SSH_CHANNEL_INPUT_DRAINING:
205 case SSH_CHANNEL_OUTPUT_DRAINING:
206 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100207 }
208 logit("Non-public channel %d, type %d.", id, c->type);
209 return (NULL);
210}
211
212/*
Damien Millere247cc42000-05-07 12:03:14 +1000213 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000214 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100215 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100217channel_register_fds(Channel *c, int rfd, int wfd, int efd,
218 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219{
Damien Miller95def091999-11-25 00:26:21 +1100220 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100221 channel_max_fd = MAX(channel_max_fd, rfd);
222 channel_max_fd = MAX(channel_max_fd, wfd);
223 channel_max_fd = MAX(channel_max_fd, efd);
224
Damien Miller5428f641999-11-25 11:54:57 +1100225 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000226
Damien Miller6f83b8e2000-05-02 09:23:45 +1000227 c->rfd = rfd;
228 c->wfd = wfd;
229 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000230 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000231 c->efd = efd;
232 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100233
Damien Miller79438cc2001-02-16 12:34:57 +1100234 /* XXX ugly hack: nonblock is only set by the server */
235 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000236 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100237 c->isatty = 1;
238 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000239 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100240 c->self, c->wfd);
241 }
242 } else {
243 c->isatty = 0;
244 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000245 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100246
Damien Miller69b69aa2000-10-28 14:19:58 +1100247 /* enable nonblocking mode */
248 if (nonblock) {
249 if (rfd != -1)
250 set_nonblock(rfd);
251 if (wfd != -1)
252 set_nonblock(wfd);
253 if (efd != -1)
254 set_nonblock(efd);
255 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000256}
257
258/*
259 * Allocate a new channel object and set its type and socket. This will cause
260 * remote_name to be freed.
261 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000262Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000263channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000264 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000265{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000266 int found;
267 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000268 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller95def091999-11-25 00:26:21 +1100270 /* Do initial allocation if this is the first call. */
271 if (channels_alloc == 0) {
272 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100273 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100274 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000275 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100276 }
277 /* Try to find a free slot where to put the new channel. */
278 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000279 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100280 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000281 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100282 break;
283 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000284 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100285 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100286 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000287 if (channels_alloc > 10000)
288 fatal("channel_new: internal error: channels_alloc %d "
289 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100290 channels = xrealloc(channels, channels_alloc + 10,
291 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000292 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100293 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100294 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000295 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100296 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000297 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100298 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100299 buffer_init(&c->input);
300 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000301 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100302 c->ostate = CHAN_OUTPUT_OPEN;
303 c->istate = CHAN_INPUT_OPEN;
304 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100305 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100306 c->self = found;
307 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000308 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000309 c->local_window = window;
310 c->local_window_max = window;
311 c->local_consumed = 0;
312 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100313 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000314 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000315 c->remote_window = 0;
316 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000317 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100318 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000319 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100320 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100321 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000322 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000323 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100324 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100325 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000326 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000328
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000329static int
330channel_find_maxfd(void)
331{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000332 u_int i;
333 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000334 Channel *c;
335
336 for (i = 0; i < channels_alloc; i++) {
337 c = channels[i];
338 if (c != NULL) {
339 max = MAX(max, c->rfd);
340 max = MAX(max, c->wfd);
341 max = MAX(max, c->efd);
342 }
343 }
344 return max;
345}
346
347int
348channel_close_fd(int *fdp)
349{
350 int ret = 0, fd = *fdp;
351
352 if (fd != -1) {
353 ret = close(fd);
354 *fdp = -1;
355 if (fd == channel_max_fd)
356 channel_max_fd = channel_find_maxfd();
357 }
358 return ret;
359}
360
Damien Miller6f83b8e2000-05-02 09:23:45 +1000361/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000362static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000363channel_close_fds(Channel *c)
364{
Damien Miller0e220db2004-06-15 10:34:08 +1000365 debug3("channel %d: close_fds r %d w %d e %d c %d",
366 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000367
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000368 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000369 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000370 channel_close_fd(&c->rfd);
371 channel_close_fd(&c->wfd);
372 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000373}
374
375/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000376void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000377channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000379 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000380 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000381
382 for (n = 0, i = 0; i < channels_alloc; i++)
383 if (channels[i])
384 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000385 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000386 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000387
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000388 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000389 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000390 xfree(s);
391
Damien Miller6f83b8e2000-05-02 09:23:45 +1000392 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000393 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000394 if (c->ctl_fd != -1)
395 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000396 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000397 buffer_free(&c->input);
398 buffer_free(&c->output);
399 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000400 if (c->remote_name) {
401 xfree(c->remote_name);
402 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100403 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000404 channels[c->self] = NULL;
405 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406}
407
Ben Lindstrome9c99912001-06-09 00:41:05 +0000408void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000409channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000410{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000411 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000412
Ben Lindstrom601e4362001-06-21 03:19:23 +0000413 for (i = 0; i < channels_alloc; i++)
414 if (channels[i] != NULL)
415 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000416}
417
418/*
419 * Closes the sockets/fds of all channels. This is used to close extra file
420 * descriptors after a fork.
421 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000422void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000423channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000424{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000425 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000426
427 for (i = 0; i < channels_alloc; i++)
428 if (channels[i] != NULL)
429 channel_close_fds(channels[i]);
430}
431
432/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000433 * Stop listening to channels.
434 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000435void
436channel_stop_listening(void)
437{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000438 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000439 Channel *c;
440
441 for (i = 0; i < channels_alloc; i++) {
442 c = channels[i];
443 if (c != NULL) {
444 switch (c->type) {
445 case SSH_CHANNEL_AUTH_SOCKET:
446 case SSH_CHANNEL_PORT_LISTENER:
447 case SSH_CHANNEL_RPORT_LISTENER:
448 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000449 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000450 channel_free(c);
451 break;
452 }
453 }
454 }
455}
456
457/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000458 * Returns true if no channel has too much buffered data, and false if one or
459 * more channel is overfull.
460 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000461int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000462channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000463{
464 u_int i;
465 Channel *c;
466
467 for (i = 0; i < channels_alloc; i++) {
468 c = channels[i];
469 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000470#if 0
471 if (!compat20 &&
472 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100473 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000474 c->self, buffer_len(&c->input));
475 return 0;
476 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000477#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000478 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000479 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000480 c->self, buffer_len(&c->output),
481 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000482 return 0;
483 }
484 }
485 }
486 return 1;
487}
488
489/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000490int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000491channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000492{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000493 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000494 Channel *c;
495
496 for (i = 0; i < channels_alloc; i++) {
497 c = channels[i];
498 if (c == NULL)
499 continue;
500 switch (c->type) {
501 case SSH_CHANNEL_X11_LISTENER:
502 case SSH_CHANNEL_PORT_LISTENER:
503 case SSH_CHANNEL_RPORT_LISTENER:
504 case SSH_CHANNEL_CLOSED:
505 case SSH_CHANNEL_AUTH_SOCKET:
506 case SSH_CHANNEL_DYNAMIC:
507 case SSH_CHANNEL_CONNECTING:
508 case SSH_CHANNEL_ZOMBIE:
509 continue;
510 case SSH_CHANNEL_LARVAL:
511 if (!compat20)
512 fatal("cannot happen: SSH_CHANNEL_LARVAL");
513 continue;
514 case SSH_CHANNEL_OPENING:
515 case SSH_CHANNEL_OPEN:
516 case SSH_CHANNEL_X11_OPEN:
517 return 1;
518 case SSH_CHANNEL_INPUT_DRAINING:
519 case SSH_CHANNEL_OUTPUT_DRAINING:
520 if (!compat13)
521 fatal("cannot happen: OUT_DRAIN");
522 return 1;
523 default:
524 fatal("channel_still_open: bad channel type %d", c->type);
525 /* NOTREACHED */
526 }
527 }
528 return 0;
529}
530
531/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000532int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000533channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000534{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000535 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000536 Channel *c;
537
538 for (i = 0; i < channels_alloc; i++) {
539 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000540 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000541 continue;
542 switch (c->type) {
543 case SSH_CHANNEL_CLOSED:
544 case SSH_CHANNEL_DYNAMIC:
545 case SSH_CHANNEL_X11_LISTENER:
546 case SSH_CHANNEL_PORT_LISTENER:
547 case SSH_CHANNEL_RPORT_LISTENER:
548 case SSH_CHANNEL_OPENING:
549 case SSH_CHANNEL_CONNECTING:
550 case SSH_CHANNEL_ZOMBIE:
551 continue;
552 case SSH_CHANNEL_LARVAL:
553 case SSH_CHANNEL_AUTH_SOCKET:
554 case SSH_CHANNEL_OPEN:
555 case SSH_CHANNEL_X11_OPEN:
556 return i;
557 case SSH_CHANNEL_INPUT_DRAINING:
558 case SSH_CHANNEL_OUTPUT_DRAINING:
559 if (!compat13)
560 fatal("cannot happen: OUT_DRAIN");
561 return i;
562 default:
563 fatal("channel_find_open: bad channel type %d", c->type);
564 /* NOTREACHED */
565 }
566 }
567 return -1;
568}
569
570
571/*
572 * Returns a message describing the currently open forwarded connections,
573 * suitable for sending to the client. The message contains crlf pairs for
574 * newlines.
575 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000576char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000577channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578{
579 Buffer buffer;
580 Channel *c;
581 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000582 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000583
584 buffer_init(&buffer);
585 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
586 buffer_append(&buffer, buf, strlen(buf));
587 for (i = 0; i < channels_alloc; i++) {
588 c = channels[i];
589 if (c == NULL)
590 continue;
591 switch (c->type) {
592 case SSH_CHANNEL_X11_LISTENER:
593 case SSH_CHANNEL_PORT_LISTENER:
594 case SSH_CHANNEL_RPORT_LISTENER:
595 case SSH_CHANNEL_CLOSED:
596 case SSH_CHANNEL_AUTH_SOCKET:
597 case SSH_CHANNEL_ZOMBIE:
598 continue;
599 case SSH_CHANNEL_LARVAL:
600 case SSH_CHANNEL_OPENING:
601 case SSH_CHANNEL_CONNECTING:
602 case SSH_CHANNEL_DYNAMIC:
603 case SSH_CHANNEL_OPEN:
604 case SSH_CHANNEL_X11_OPEN:
605 case SSH_CHANNEL_INPUT_DRAINING:
606 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000607 snprintf(buf, sizeof buf,
608 " #%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 +0000609 c->self, c->remote_name,
610 c->type, c->remote_id,
611 c->istate, buffer_len(&c->input),
612 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000613 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000614 buffer_append(&buffer, buf, strlen(buf));
615 continue;
616 default:
617 fatal("channel_open_message: bad channel type %d", c->type);
618 /* NOTREACHED */
619 }
620 }
621 buffer_append(&buffer, "\0", 1);
622 cp = xstrdup(buffer_ptr(&buffer));
623 buffer_free(&buffer);
624 return cp;
625}
626
627void
628channel_send_open(int id)
629{
630 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000631
Ben Lindstrome9c99912001-06-09 00:41:05 +0000632 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000633 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000634 return;
635 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000636 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000637 packet_start(SSH2_MSG_CHANNEL_OPEN);
638 packet_put_cstring(c->ctype);
639 packet_put_int(c->self);
640 packet_put_int(c->local_window);
641 packet_put_int(c->local_maxpacket);
642 packet_send();
643}
644
645void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000646channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000647{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000648 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000649
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000651 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000652 return;
653 }
Damien Miller0e220db2004-06-15 10:34:08 +1000654 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000655 packet_start(SSH2_MSG_CHANNEL_REQUEST);
656 packet_put_int(c->remote_id);
657 packet_put_cstring(service);
658 packet_put_char(wantconfirm);
659}
Damien Miller4f7becb2006-03-26 14:10:14 +1100660
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661void
Damien Miller0e220db2004-06-15 10:34:08 +1000662channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663{
664 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000665
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000667 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668 return;
669 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100670 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000671 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000672}
Damien Miller4f7becb2006-03-26 14:10:14 +1100673
Ben Lindstrome9c99912001-06-09 00:41:05 +0000674void
Damien Miller39eda6e2005-11-05 14:52:50 +1100675channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000676{
Damien Millerd47c62a2005-12-13 19:33:57 +1100677 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000678
Ben Lindstrome9c99912001-06-09 00:41:05 +0000679 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000680 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000681 return;
682 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000683 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100684 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000685}
Damien Miller4f7becb2006-03-26 14:10:14 +1100686
Ben Lindstrome9c99912001-06-09 00:41:05 +0000687void
688channel_cancel_cleanup(int id)
689{
Damien Millerd47c62a2005-12-13 19:33:57 +1100690 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000691
Ben Lindstrome9c99912001-06-09 00:41:05 +0000692 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000693 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 return;
695 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000696 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100697 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698}
Damien Miller4f7becb2006-03-26 14:10:14 +1100699
Ben Lindstrome9c99912001-06-09 00:41:05 +0000700void
Damien Miller077b2382005-12-31 16:22:32 +1100701channel_register_filter(int id, channel_infilter_fn *ifn,
702 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000703{
704 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000705
Ben Lindstrome9c99912001-06-09 00:41:05 +0000706 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000707 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000708 return;
709 }
Damien Miller077b2382005-12-31 16:22:32 +1100710 c->input_filter = ifn;
711 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000712}
713
714void
715channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100716 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000717{
718 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000719
Ben Lindstrome9c99912001-06-09 00:41:05 +0000720 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
721 fatal("channel_activate for non-larval channel %d.", id);
722 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
723 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100724 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000725 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
726 packet_put_int(c->remote_id);
727 packet_put_int(c->local_window);
728 packet_send();
729}
730
Damien Miller5428f641999-11-25 11:54:57 +1100731/*
Damien Millerb38eff82000-04-01 11:09:21 +1000732 * 'channel_pre*' are called just before select() to add any bits relevant to
733 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100734 */
Damien Millerb38eff82000-04-01 11:09:21 +1000735/*
736 * 'channel_post*': perform any appropriate operations for channels which
737 * have events pending.
738 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100739typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000740chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
741chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
742
Damien Miller8473dd82006-07-24 14:08:32 +1000743/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000744static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100745channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000746{
747 FD_SET(c->sock, readset);
748}
749
Damien Miller8473dd82006-07-24 14:08:32 +1000750/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000751static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100752channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000753{
754 debug3("channel %d: waiting for connection", c->self);
755 FD_SET(c->sock, writeset);
756}
757
Ben Lindstrombba81212001-06-25 05:01:22 +0000758static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100759channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000760{
761 if (buffer_len(&c->input) < packet_get_maxsize())
762 FD_SET(c->sock, readset);
763 if (buffer_len(&c->output) > 0)
764 FD_SET(c->sock, writeset);
765}
766
Ben Lindstrombba81212001-06-25 05:01:22 +0000767static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100768channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000769{
Damien Millerde6987c2002-01-22 23:20:40 +1100770 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000771
Damien Miller33b13562000-04-04 14:38:59 +1000772 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100773 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000774 buffer_len(&c->input) < limit &&
775 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000776 FD_SET(c->rfd, readset);
777 if (c->ostate == CHAN_OUTPUT_OPEN ||
778 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
779 if (buffer_len(&c->output) > 0) {
780 FD_SET(c->wfd, writeset);
781 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000782 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000783 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
784 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000785 else
786 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000787 }
788 }
789 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100790 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000791 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
792 buffer_len(&c->extended) > 0)
793 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000794 else if (!(c->flags & CHAN_EOF_SENT) &&
795 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000796 buffer_len(&c->extended) < c->remote_window)
797 FD_SET(c->efd, readset);
798 }
Damien Miller0e220db2004-06-15 10:34:08 +1000799 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000800 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000801 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
802 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000803}
804
Damien Miller8473dd82006-07-24 14:08:32 +1000805/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000806static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100807channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000808{
809 if (buffer_len(&c->input) == 0) {
810 packet_start(SSH_MSG_CHANNEL_CLOSE);
811 packet_put_int(c->remote_id);
812 packet_send();
813 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000814 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000815 }
816}
817
Damien Miller8473dd82006-07-24 14:08:32 +1000818/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000819static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100820channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000821{
822 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000823 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000824 else
Damien Millerb38eff82000-04-01 11:09:21 +1000825 FD_SET(c->sock, writeset);
826}
827
828/*
829 * This is a special state for X11 authentication spoofing. An opened X11
830 * connection (when authentication spoofing is being done) remains in this
831 * state until the first packet has been completely read. The authentication
832 * data in that packet is then substituted by the real data if it matches the
833 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000834 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000835 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000836 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000837static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000839{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000840 u_char *ucp;
841 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000842
843 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000844 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000845 return 0;
846
847 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100848 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000849 if (ucp[0] == 0x42) { /* Byte order MSB first. */
850 proto_len = 256 * ucp[6] + ucp[7];
851 data_len = 256 * ucp[8] + ucp[9];
852 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
853 proto_len = ucp[6] + 256 * ucp[7];
854 data_len = ucp[8] + 256 * ucp[9];
855 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000856 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100857 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000858 return -1;
859 }
860
861 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000862 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000863 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
864 return 0;
865
866 /* Check if authentication protocol matches. */
867 if (proto_len != strlen(x11_saved_proto) ||
868 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000869 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000870 return -1;
871 }
872 /* Check if authentication data matches our fake data. */
873 if (data_len != x11_fake_data_len ||
874 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
875 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000876 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000877 return -1;
878 }
879 /* Check fake data length */
880 if (x11_fake_data_len != x11_saved_data_len) {
881 error("X11 fake_data_len %d != saved_data_len %d",
882 x11_fake_data_len, x11_saved_data_len);
883 return -1;
884 }
885 /*
886 * Received authentication protocol and data match
887 * our fake data. Substitute the fake data with real
888 * data.
889 */
890 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
891 x11_saved_data, x11_saved_data_len);
892 return 1;
893}
894
Ben Lindstrombba81212001-06-25 05:01:22 +0000895static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100896channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000897{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000898 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000899
Damien Millerb38eff82000-04-01 11:09:21 +1000900 if (ret == 1) {
901 /* Start normal processing for the channel. */
902 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000903 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000904 } else if (ret == -1) {
905 /*
906 * We have received an X11 connection that has bad
907 * authentication information.
908 */
Damien Miller996acd22003-04-09 20:59:48 +1000909 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000910 buffer_clear(&c->input);
911 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000912 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000913 c->sock = -1;
914 c->type = SSH_CHANNEL_CLOSED;
915 packet_start(SSH_MSG_CHANNEL_CLOSE);
916 packet_put_int(c->remote_id);
917 packet_send();
918 }
919}
920
Ben Lindstrombba81212001-06-25 05:01:22 +0000921static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100922channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000923{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000924 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000925
926 /* c->force_drain = 1; */
927
Damien Millerb38eff82000-04-01 11:09:21 +1000928 if (ret == 1) {
929 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100930 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000931 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000932 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000933 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100934 chan_read_failed(c);
935 buffer_clear(&c->input);
936 chan_ibuf_empty(c);
937 buffer_clear(&c->output);
938 /* for proto v1, the peer will send an IEOF */
939 if (compat20)
940 chan_write_failed(c);
941 else
942 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000943 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000944 }
945}
946
Ben Lindstromb3921512001-04-11 15:57:50 +0000947/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +1000948/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000949static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100950channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000951{
Damien Millera10f5612002-09-12 09:49:15 +1000952 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000953 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100954 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000955 struct {
956 u_int8_t version;
957 u_int8_t command;
958 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000959 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000960 } s4_req, s4_rsp;
961
Ben Lindstromb3921512001-04-11 15:57:50 +0000962 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000963
964 have = buffer_len(&c->input);
965 len = sizeof(s4_req);
966 if (have < len)
967 return 0;
968 p = buffer_ptr(&c->input);
969 for (found = 0, i = len; i < have; i++) {
970 if (p[i] == '\0') {
971 found = 1;
972 break;
973 }
974 if (i > 1024) {
975 /* the peer is probably sending garbage */
976 debug("channel %d: decode socks4: too long",
977 c->self);
978 return -1;
979 }
980 }
981 if (!found)
982 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000983 buffer_get(&c->input, (char *)&s4_req.version, 1);
984 buffer_get(&c->input, (char *)&s4_req.command, 1);
985 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000986 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000987 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000988 p = buffer_ptr(&c->input);
989 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000990 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000991 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000992 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000993 c->self, len, have);
994 strlcpy(username, p, sizeof(username));
995 buffer_consume(&c->input, len);
996 buffer_consume(&c->input, 1); /* trailing '\0' */
997
Ben Lindstromb3921512001-04-11 15:57:50 +0000998 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000999 strlcpy(c->path, host, sizeof(c->path));
1000 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001001
Damien Millerfbdeece2003-09-02 22:52:31 +10001002 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001003 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001004
Ben Lindstromb3921512001-04-11 15:57:50 +00001005 if (s4_req.command != 1) {
1006 debug("channel %d: cannot handle: socks4 cn %d",
1007 c->self, s4_req.command);
1008 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001009 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001010 s4_rsp.version = 0; /* vn: 0 for reply */
1011 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001012 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001013 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001014 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001015 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001016}
1017
Darren Tucker46471c92003-07-03 13:55:19 +10001018/* try to decode a socks5 header */
1019#define SSH_SOCKS5_AUTHDONE 0x1000
1020#define SSH_SOCKS5_NOAUTH 0x00
1021#define SSH_SOCKS5_IPV4 0x01
1022#define SSH_SOCKS5_DOMAIN 0x03
1023#define SSH_SOCKS5_IPV6 0x04
1024#define SSH_SOCKS5_CONNECT 0x01
1025#define SSH_SOCKS5_SUCCESS 0x00
1026
Damien Miller8473dd82006-07-24 14:08:32 +10001027/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001028static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001029channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001030{
1031 struct {
1032 u_int8_t version;
1033 u_int8_t command;
1034 u_int8_t reserved;
1035 u_int8_t atyp;
1036 } s5_req, s5_rsp;
1037 u_int16_t dest_port;
1038 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001039 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001040
1041 debug2("channel %d: decode socks5", c->self);
1042 p = buffer_ptr(&c->input);
1043 if (p[0] != 0x05)
1044 return -1;
1045 have = buffer_len(&c->input);
1046 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1047 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001048 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001049 return 0;
1050 nmethods = p[1];
1051 if (have < nmethods + 2)
1052 return 0;
1053 /* look for method: "NO AUTHENTICATION REQUIRED" */
1054 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1055 if (p[i] == SSH_SOCKS5_NOAUTH ) {
1056 found = 1;
1057 break;
1058 }
1059 }
1060 if (!found) {
1061 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1062 c->self);
1063 return -1;
1064 }
1065 buffer_consume(&c->input, nmethods + 2);
1066 buffer_put_char(&c->output, 0x05); /* version */
1067 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1068 FD_SET(c->sock, writeset);
1069 c->flags |= SSH_SOCKS5_AUTHDONE;
1070 debug2("channel %d: socks5 auth done", c->self);
1071 return 0; /* need more */
1072 }
1073 debug2("channel %d: socks5 post auth", c->self);
1074 if (have < sizeof(s5_req)+1)
1075 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001076 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001077 if (s5_req.version != 0x05 ||
1078 s5_req.command != SSH_SOCKS5_CONNECT ||
1079 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001080 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001081 return -1;
1082 }
Darren Tucker47eede72005-03-14 23:08:12 +11001083 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001084 case SSH_SOCKS5_IPV4:
1085 addrlen = 4;
1086 af = AF_INET;
1087 break;
1088 case SSH_SOCKS5_DOMAIN:
1089 addrlen = p[sizeof(s5_req)];
1090 af = -1;
1091 break;
1092 case SSH_SOCKS5_IPV6:
1093 addrlen = 16;
1094 af = AF_INET6;
1095 break;
1096 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001097 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001098 return -1;
1099 }
Damien Miller0f077072006-07-10 22:21:02 +10001100 need = sizeof(s5_req) + addrlen + 2;
1101 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1102 need++;
1103 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001104 return 0;
1105 buffer_consume(&c->input, sizeof(s5_req));
1106 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1107 buffer_consume(&c->input, 1); /* host string length */
1108 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1109 buffer_get(&c->input, (char *)&dest_port, 2);
1110 dest_addr[addrlen] = '\0';
1111 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001112 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001113 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1114 return -1;
1115 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001116
Damien Millerfbdeece2003-09-02 22:52:31 +10001117 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001118 c->self, c->path, c->host_port, s5_req.command);
1119
1120 s5_rsp.version = 0x05;
1121 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1122 s5_rsp.reserved = 0; /* ignored */
1123 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1124 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1125 dest_port = 0; /* ignored */
1126
Damien Millera0fdce92006-03-26 14:28:50 +11001127 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1128 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1129 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001130 return 1;
1131}
1132
Ben Lindstromb3921512001-04-11 15:57:50 +00001133/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001134static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001135channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001136{
1137 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001138 u_int have;
1139 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001140
1141 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001142 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001143 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001144 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001145 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001146 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001147 /* need more */
1148 FD_SET(c->sock, readset);
1149 return;
1150 }
1151 /* try to guess the protocol */
1152 p = buffer_ptr(&c->input);
1153 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001154 case 0x04:
1155 ret = channel_decode_socks4(c, readset, writeset);
1156 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001157 case 0x05:
1158 ret = channel_decode_socks5(c, readset, writeset);
1159 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001160 default:
1161 ret = -1;
1162 break;
1163 }
1164 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001165 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001166 } else if (ret == 0) {
1167 debug2("channel %d: pre_dynamic: need more", c->self);
1168 /* need more */
1169 FD_SET(c->sock, readset);
1170 } else {
1171 /* switch to the next state */
1172 c->type = SSH_CHANNEL_OPENING;
1173 port_open_helper(c, "direct-tcpip");
1174 }
1175}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001176
Damien Millerb38eff82000-04-01 11:09:21 +10001177/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001178/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001179static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001180channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001181{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001182 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001183 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001184 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001185 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001186 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001187 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001188
1189 if (FD_ISSET(c->sock, readset)) {
1190 debug("X11 connection requested.");
1191 addrlen = sizeof(addr);
1192 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001193 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001194 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001195 channel_close_fd(&c->sock);
1196 chan_mark_dead(c);
1197 }
Damien Millerb38eff82000-04-01 11:09:21 +10001198 if (newsock < 0) {
1199 error("accept: %.100s", strerror(errno));
1200 return;
1201 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001202 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001203 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001204 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001205 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001206 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001207
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001208 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001209 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001210 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001211 if (compat20) {
1212 packet_start(SSH2_MSG_CHANNEL_OPEN);
1213 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001214 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001215 packet_put_int(nc->local_window_max);
1216 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001217 /* originator ipaddr and port */
1218 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001219 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001220 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001221 } else {
1222 packet_put_int(remote_port);
1223 }
Damien Millerbd483e72000-04-30 10:00:53 +10001224 packet_send();
1225 } else {
1226 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001227 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001228 if (packet_get_protocol_flags() &
1229 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001230 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001231 packet_send();
1232 }
Damien Millerd83ff352001-01-30 09:19:34 +11001233 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001234 }
1235}
1236
Ben Lindstrombba81212001-06-25 05:01:22 +00001237static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001238port_open_helper(Channel *c, char *rtype)
1239{
1240 int direct;
1241 char buf[1024];
1242 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001243 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001244
1245 direct = (strcmp(rtype, "direct-tcpip") == 0);
1246
1247 snprintf(buf, sizeof buf,
1248 "%s: listening port %d for %.100s port %d, "
1249 "connect from %.200s port %d",
1250 rtype, c->listening_port, c->path, c->host_port,
1251 remote_ipaddr, remote_port);
1252
1253 xfree(c->remote_name);
1254 c->remote_name = xstrdup(buf);
1255
1256 if (compat20) {
1257 packet_start(SSH2_MSG_CHANNEL_OPEN);
1258 packet_put_cstring(rtype);
1259 packet_put_int(c->self);
1260 packet_put_int(c->local_window_max);
1261 packet_put_int(c->local_maxpacket);
1262 if (direct) {
1263 /* target host, port */
1264 packet_put_cstring(c->path);
1265 packet_put_int(c->host_port);
1266 } else {
1267 /* listen address, port */
1268 packet_put_cstring(c->path);
1269 packet_put_int(c->listening_port);
1270 }
1271 /* originator host and port */
1272 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001273 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001274 packet_send();
1275 } else {
1276 packet_start(SSH_MSG_PORT_OPEN);
1277 packet_put_int(c->self);
1278 packet_put_cstring(c->path);
1279 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001280 if (packet_get_protocol_flags() &
1281 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001282 packet_put_cstring(c->remote_name);
1283 packet_send();
1284 }
1285 xfree(remote_ipaddr);
1286}
1287
Damien Miller5e7fd072005-11-05 14:53:39 +11001288static void
1289channel_set_reuseaddr(int fd)
1290{
1291 int on = 1;
1292
1293 /*
1294 * Set socket options.
1295 * Allow local port reuse in TIME_WAIT.
1296 */
1297 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1298 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1299}
1300
Damien Millerb38eff82000-04-01 11:09:21 +10001301/*
1302 * This socket is listening for connections to a forwarded TCP/IP port.
1303 */
Damien Miller8473dd82006-07-24 14:08:32 +10001304/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001305static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001306channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001307{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001308 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001309 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001310 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001311 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001312 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001313
Damien Millerb38eff82000-04-01 11:09:21 +10001314 if (FD_ISSET(c->sock, readset)) {
1315 debug("Connection to port %d forwarding "
1316 "to %.100s port %d requested.",
1317 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001318
Damien Miller4623a752001-10-10 15:03:58 +10001319 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1320 nextstate = SSH_CHANNEL_OPENING;
1321 rtype = "forwarded-tcpip";
1322 } else {
1323 if (c->host_port == 0) {
1324 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001325 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001326 } else {
1327 nextstate = SSH_CHANNEL_OPENING;
1328 rtype = "direct-tcpip";
1329 }
1330 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001331
Damien Millerb38eff82000-04-01 11:09:21 +10001332 addrlen = sizeof(addr);
1333 newsock = accept(c->sock, &addr, &addrlen);
1334 if (newsock < 0) {
1335 error("accept: %.100s", strerror(errno));
1336 return;
1337 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001338 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001339 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1340 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001341 nc->listening_port = c->listening_port;
1342 nc->host_port = c->host_port;
1343 strlcpy(nc->path, c->path, sizeof(nc->path));
1344
Damien Miller4623a752001-10-10 15:03:58 +10001345 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1346 /*
1347 * do not call the channel_post handler until
1348 * this flag has been reset by a pre-handler.
1349 * otherwise the FD_ISSET calls might overflow
1350 */
1351 nc->delayed = 1;
1352 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001353 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001354 }
Damien Millerb38eff82000-04-01 11:09:21 +10001355 }
1356}
1357
1358/*
1359 * This is the authentication agent socket listening for connections from
1360 * clients.
1361 */
Damien Miller8473dd82006-07-24 14:08:32 +10001362/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001363static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001364channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001365{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001366 Channel *nc;
1367 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001368 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001369 socklen_t addrlen;
1370
1371 if (FD_ISSET(c->sock, readset)) {
1372 addrlen = sizeof(addr);
1373 newsock = accept(c->sock, &addr, &addrlen);
1374 if (newsock < 0) {
1375 error("accept from auth socket: %.100s", strerror(errno));
1376 return;
1377 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001378 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001379 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1380 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001381 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001382 if (compat20) {
1383 packet_start(SSH2_MSG_CHANNEL_OPEN);
1384 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001385 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001386 packet_put_int(c->local_window_max);
1387 packet_put_int(c->local_maxpacket);
1388 } else {
1389 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001390 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001391 }
Damien Millerb38eff82000-04-01 11:09:21 +10001392 packet_send();
1393 }
1394}
1395
Damien Miller8473dd82006-07-24 14:08:32 +10001396/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001397static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001398channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001399{
Ben Lindstrom69128662001-05-08 20:07:39 +00001400 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001401 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001402
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001403 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001404 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001405 err = errno;
1406 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001407 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001408 if (err == 0) {
1409 debug("channel %d: connected", c->self);
1410 c->type = SSH_CHANNEL_OPEN;
1411 if (compat20) {
1412 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1413 packet_put_int(c->remote_id);
1414 packet_put_int(c->self);
1415 packet_put_int(c->local_window);
1416 packet_put_int(c->local_maxpacket);
1417 } else {
1418 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1419 packet_put_int(c->remote_id);
1420 packet_put_int(c->self);
1421 }
1422 } else {
1423 debug("channel %d: not connected: %s",
1424 c->self, strerror(err));
1425 if (compat20) {
1426 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1427 packet_put_int(c->remote_id);
1428 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1429 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1430 packet_put_cstring(strerror(err));
1431 packet_put_cstring("");
1432 }
1433 } else {
1434 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1435 packet_put_int(c->remote_id);
1436 }
1437 chan_mark_dead(c);
1438 }
1439 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001440 }
1441}
1442
Damien Miller8473dd82006-07-24 14:08:32 +10001443/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001444static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001445channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001446{
Darren Tucker11327cc2005-03-14 23:22:25 +11001447 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001448 int len;
1449
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001450 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001451 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001452 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001453 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001454 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1455 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001456#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001457 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001458#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001459 if ((!c->isatty && len <= 0) ||
1460 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001461#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001462 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001463 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001464 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001465 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001466 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001467 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001468 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001469 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001470 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001471 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001472 } else {
1473 chan_read_failed(c);
1474 }
1475 return -1;
1476 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001477 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001478 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001479 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001480 chan_read_failed(c);
1481 }
Damien Millerd27b9472005-12-13 19:29:02 +11001482 } else if (c->datagram) {
1483 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001484 } else {
1485 buffer_append(&c->input, buf, len);
1486 }
Damien Millerb38eff82000-04-01 11:09:21 +10001487 }
1488 return 1;
1489}
Damien Miller4f7becb2006-03-26 14:10:14 +11001490
Damien Miller8473dd82006-07-24 14:08:32 +10001491/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001492static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001493channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001494{
Ben Lindstrome229b252001-03-05 06:28:06 +00001495 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001496 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001497 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001498 int len;
1499
1500 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001501 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001502 FD_ISSET(c->wfd, writeset) &&
1503 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001504 if (c->output_filter != NULL) {
1505 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1506 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001507 if (c->type != SSH_CHANNEL_OPEN)
1508 chan_mark_dead(c);
1509 else
1510 chan_write_failed(c);
1511 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001512 }
1513 } else if (c->datagram) {
1514 buf = data = buffer_get_string(&c->output, &dlen);
1515 } else {
1516 buf = data = buffer_ptr(&c->output);
1517 dlen = buffer_len(&c->output);
1518 }
1519
Damien Millerd27b9472005-12-13 19:29:02 +11001520 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001521 /* ignore truncated writes, datagrams might get lost */
1522 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001523 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001524 xfree(data);
1525 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1526 return 1;
1527 if (len <= 0) {
1528 if (c->type != SSH_CHANNEL_OPEN)
1529 chan_mark_dead(c);
1530 else
1531 chan_write_failed(c);
1532 return -1;
1533 }
1534 return 1;
1535 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001536#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001537 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001538 if (compat20 && c->wfd_isatty)
1539 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001540#endif
Damien Miller077b2382005-12-31 16:22:32 +11001541
1542 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001543 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1544 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001545 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001546 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001547 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001548 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001549 return -1;
1550 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001551 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001552 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001553 c->type = SSH_CHANNEL_INPUT_DRAINING;
1554 } else {
1555 chan_write_failed(c);
1556 }
1557 return -1;
1558 }
Damien Miller077b2382005-12-31 16:22:32 +11001559 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001560 if (tcgetattr(c->wfd, &tio) == 0 &&
1561 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1562 /*
1563 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001564 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001565 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001566 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001567 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001568 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001569 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001570 }
1571 }
Damien Millerb38eff82000-04-01 11:09:21 +10001572 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001573 if (compat20 && len > 0) {
1574 c->local_consumed += len;
1575 }
1576 }
1577 return 1;
1578}
Damien Miller4f7becb2006-03-26 14:10:14 +11001579
Ben Lindstrombba81212001-06-25 05:01:22 +00001580static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001581channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001582{
Darren Tucker11327cc2005-03-14 23:22:25 +11001583 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001584 int len;
1585
Damien Miller1383bd82000-04-06 12:32:37 +10001586/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001587 if (c->efd != -1) {
1588 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1589 FD_ISSET(c->efd, writeset) &&
1590 buffer_len(&c->extended) > 0) {
1591 len = write(c->efd, buffer_ptr(&c->extended),
1592 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001593 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001594 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001595 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1596 return 1;
1597 if (len <= 0) {
1598 debug2("channel %d: closing write-efd %d",
1599 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001600 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001601 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001602 buffer_consume(&c->extended, len);
1603 c->local_consumed += len;
1604 }
1605 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1606 FD_ISSET(c->efd, readset)) {
1607 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001608 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001609 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001610 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1611 return 1;
1612 if (len <= 0) {
1613 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001614 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001615 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001616 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001617 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001618 }
Damien Miller33b13562000-04-04 14:38:59 +10001619 }
1620 }
1621 return 1;
1622}
Damien Miller4f7becb2006-03-26 14:10:14 +11001623
Damien Miller8473dd82006-07-24 14:08:32 +10001624/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001625static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001626channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001627{
1628 char buf[16];
1629 int len;
1630
1631 /* Monitor control fd to detect if the slave client exits */
1632 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1633 len = read(c->ctl_fd, buf, sizeof(buf));
1634 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1635 return 1;
1636 if (len <= 0) {
1637 debug2("channel %d: ctl read<=0", c->self);
1638 if (c->type != SSH_CHANNEL_OPEN) {
1639 debug2("channel %d: not open", c->self);
1640 chan_mark_dead(c);
1641 return -1;
1642 } else {
1643 chan_read_failed(c);
1644 chan_write_failed(c);
1645 }
1646 return -1;
1647 } else
1648 fatal("%s: unexpected data on ctl fd", __func__);
1649 }
1650 return 1;
1651}
Damien Miller4f7becb2006-03-26 14:10:14 +11001652
Damien Miller0e220db2004-06-15 10:34:08 +10001653static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001654channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001655{
Ben Lindstromb3921512001-04-11 15:57:50 +00001656 if (c->type == SSH_CHANNEL_OPEN &&
1657 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001658 c->local_window < c->local_window_max/2 &&
1659 c->local_consumed > 0) {
1660 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1661 packet_put_int(c->remote_id);
1662 packet_put_int(c->local_consumed);
1663 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001664 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001665 c->self, c->local_window,
1666 c->local_consumed);
1667 c->local_window += c->local_consumed;
1668 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001669 }
1670 return 1;
1671}
1672
Ben Lindstrombba81212001-06-25 05:01:22 +00001673static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001674channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001675{
Damien Miller4623a752001-10-10 15:03:58 +10001676 if (c->delayed)
1677 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001678 channel_handle_rfd(c, readset, writeset);
1679 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001680 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001681 return;
Damien Miller33b13562000-04-04 14:38:59 +10001682 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001683 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001684 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001685}
1686
Damien Miller8473dd82006-07-24 14:08:32 +10001687/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001688static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001689channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001690{
1691 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001692
Damien Millerb38eff82000-04-01 11:09:21 +10001693 /* Send buffered output data to the socket. */
1694 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1695 len = write(c->sock, buffer_ptr(&c->output),
1696 buffer_len(&c->output));
1697 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001698 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001699 else
1700 buffer_consume(&c->output, len);
1701 }
1702}
1703
Ben Lindstrombba81212001-06-25 05:01:22 +00001704static void
Damien Miller33b13562000-04-04 14:38:59 +10001705channel_handler_init_20(void)
1706{
Damien Millerde6987c2002-01-22 23:20:40 +11001707 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001708 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001709 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001710 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001711 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001712 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001713 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001714 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001715
Damien Millerde6987c2002-01-22 23:20:40 +11001716 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001717 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001718 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001719 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001720 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001721 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001722 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001723}
1724
Ben Lindstrombba81212001-06-25 05:01:22 +00001725static void
Damien Millerb38eff82000-04-01 11:09:21 +10001726channel_handler_init_13(void)
1727{
1728 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1729 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1730 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1731 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1732 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1733 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1734 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001735 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001736 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001737
Damien Millerde6987c2002-01-22 23:20:40 +11001738 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001739 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1740 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1741 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1742 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001743 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001744 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001745}
1746
Ben Lindstrombba81212001-06-25 05:01:22 +00001747static void
Damien Millerb38eff82000-04-01 11:09:21 +10001748channel_handler_init_15(void)
1749{
Damien Millerde6987c2002-01-22 23:20:40 +11001750 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001751 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001752 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1753 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1754 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001755 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001756 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001757
1758 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1759 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1760 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001761 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001762 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001763 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001764}
1765
Ben Lindstrombba81212001-06-25 05:01:22 +00001766static void
Damien Millerb38eff82000-04-01 11:09:21 +10001767channel_handler_init(void)
1768{
1769 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001770
Damien Miller9f0f5c62001-12-21 14:45:46 +11001771 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001772 channel_pre[i] = NULL;
1773 channel_post[i] = NULL;
1774 }
Damien Miller33b13562000-04-04 14:38:59 +10001775 if (compat20)
1776 channel_handler_init_20();
1777 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001778 channel_handler_init_13();
1779 else
1780 channel_handler_init_15();
1781}
1782
Damien Miller3ec27592001-10-12 11:35:04 +10001783/* gc dead channels */
1784static void
1785channel_garbage_collect(Channel *c)
1786{
1787 if (c == NULL)
1788 return;
1789 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001790 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001791 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001792 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001793 c->detach_user(c->self, NULL);
1794 /* if we still have a callback */
1795 if (c->detach_user != NULL)
1796 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001797 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001798 }
1799 if (!chan_is_dead(c, 1))
1800 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001801 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001802 channel_free(c);
1803}
1804
Ben Lindstrombba81212001-06-25 05:01:22 +00001805static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001806channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001807{
1808 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001809 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001810 Channel *c;
1811
1812 if (!did_init) {
1813 channel_handler_init();
1814 did_init = 1;
1815 }
1816 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001817 c = channels[i];
1818 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001819 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001820 if (ftab[c->type] != NULL)
1821 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001822 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001823 }
1824}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001825
Ben Lindstrome9c99912001-06-09 00:41:05 +00001826/*
1827 * Allocate/update select bitmasks and add any bits relevant to channels in
1828 * select bitmasks.
1829 */
Damien Miller4af51302000-04-16 11:18:38 +10001830void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001831channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001832 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001833{
Damien Miller36812092006-03-26 14:22:47 +11001834 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001835
1836 n = MAX(*maxfdp, channel_max_fd);
1837
Damien Miller36812092006-03-26 14:22:47 +11001838 nfdset = howmany(n+1, NFDBITS);
1839 /* Explicitly test here, because xrealloc isn't always called */
1840 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1841 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1842 sz = nfdset * sizeof(fd_mask);
1843
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001844 /* perhaps check sz < nalloc/2 and shrink? */
1845 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001846 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1847 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001848 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001849 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001850 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001851 memset(*readsetp, 0, sz);
1852 memset(*writesetp, 0, sz);
1853
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001854 if (!rekeying)
1855 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001856}
1857
Ben Lindstrome9c99912001-06-09 00:41:05 +00001858/*
1859 * After select, perform any appropriate operations for channels which have
1860 * events pending.
1861 */
Damien Miller4af51302000-04-16 11:18:38 +10001862void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001863channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864{
Damien Millerb38eff82000-04-01 11:09:21 +10001865 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001866}
1867
Ben Lindstrome9c99912001-06-09 00:41:05 +00001868
Damien Miller5e953212001-01-30 09:14:00 +11001869/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001870void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001871channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872{
Damien Millerb38eff82000-04-01 11:09:21 +10001873 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001874 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001875
Damien Miller95def091999-11-25 00:26:21 +11001876 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001877 c = channels[i];
1878 if (c == NULL)
1879 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001880
Ben Lindstrome9c99912001-06-09 00:41:05 +00001881 /*
1882 * We are only interested in channels that can have buffered
1883 * incoming data.
1884 */
Damien Miller34132e52000-01-14 15:45:46 +11001885 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001886 if (c->type != SSH_CHANNEL_OPEN &&
1887 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001888 continue;
1889 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001890 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001891 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001892 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001893 if (compat20 &&
1894 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001895 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001896 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001897 continue;
1898 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001899
Damien Miller95def091999-11-25 00:26:21 +11001900 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001901 if ((c->istate == CHAN_INPUT_OPEN ||
1902 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1903 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001904 if (c->datagram) {
1905 if (len > 0) {
1906 u_char *data;
1907 u_int dlen;
1908
1909 data = buffer_get_string(&c->input,
1910 &dlen);
1911 packet_start(SSH2_MSG_CHANNEL_DATA);
1912 packet_put_int(c->remote_id);
1913 packet_put_string(data, dlen);
1914 packet_send();
1915 c->remote_window -= dlen + 4;
1916 xfree(data);
1917 }
1918 continue;
1919 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001920 /*
1921 * Send some data for the other side over the secure
1922 * connection.
1923 */
Damien Miller33b13562000-04-04 14:38:59 +10001924 if (compat20) {
1925 if (len > c->remote_window)
1926 len = c->remote_window;
1927 if (len > c->remote_maxpacket)
1928 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001929 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001930 if (packet_is_interactive()) {
1931 if (len > 1024)
1932 len = 512;
1933 } else {
1934 /* Keep the packets at reasonable size. */
1935 if (len > packet_get_maxsize()/2)
1936 len = packet_get_maxsize()/2;
1937 }
Damien Miller95def091999-11-25 00:26:21 +11001938 }
Damien Millerb38eff82000-04-01 11:09:21 +10001939 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001940 packet_start(compat20 ?
1941 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001942 packet_put_int(c->remote_id);
1943 packet_put_string(buffer_ptr(&c->input), len);
1944 packet_send();
1945 buffer_consume(&c->input, len);
1946 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001947 }
1948 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001949 if (compat13)
1950 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001951 /*
1952 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001953 * tell peer, that we will not send more data: send IEOF.
1954 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001955 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001956 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001957 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1958 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001959 else
1960 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001961 }
Damien Miller33b13562000-04-04 14:38:59 +10001962 /* Send extended data, i.e. stderr */
1963 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001964 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001965 c->remote_window > 0 &&
1966 (len = buffer_len(&c->extended)) > 0 &&
1967 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001968 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001969 c->self, c->remote_window, buffer_len(&c->extended),
1970 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001971 if (len > c->remote_window)
1972 len = c->remote_window;
1973 if (len > c->remote_maxpacket)
1974 len = c->remote_maxpacket;
1975 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1976 packet_put_int(c->remote_id);
1977 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1978 packet_put_string(buffer_ptr(&c->extended), len);
1979 packet_send();
1980 buffer_consume(&c->extended, len);
1981 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001982 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001983 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001984 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001985}
1986
Ben Lindstrome9c99912001-06-09 00:41:05 +00001987
1988/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001989
1990/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001991void
Damien Miller630d6f42002-01-22 23:17:30 +11001992channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001993{
Damien Miller34132e52000-01-14 15:45:46 +11001994 int id;
Damien Miller95def091999-11-25 00:26:21 +11001995 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001996 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001997 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001998
Damien Miller95def091999-11-25 00:26:21 +11001999 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002000 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002001 c = channel_lookup(id);
2002 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002003 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002004
Damien Miller95def091999-11-25 00:26:21 +11002005 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002006 if (c->type != SSH_CHANNEL_OPEN &&
2007 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002008 return;
2009
Damien Miller95def091999-11-25 00:26:21 +11002010 /* Get the data. */
2011 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10002012
Damien Millera04ad492004-01-21 11:02:09 +11002013 /*
2014 * Ignore data for protocol > 1.3 if output end is no longer open.
2015 * For protocol 2 the sending side is reducing its window as it sends
2016 * data, so we must 'fake' consumption of the data in order to ensure
2017 * that window updates are sent back. Otherwise the connection might
2018 * deadlock.
2019 */
2020 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2021 if (compat20) {
2022 c->local_window -= data_len;
2023 c->local_consumed += data_len;
2024 }
2025 xfree(data);
2026 return;
2027 }
2028
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002029 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002030 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002031 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002032 c->self, data_len, c->local_maxpacket);
2033 }
2034 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002035 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002036 c->self, data_len, c->local_window);
2037 xfree(data);
2038 return;
2039 }
2040 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002041 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002042 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002043 if (c->datagram)
2044 buffer_put_string(&c->output, data, data_len);
2045 else
2046 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002047 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002048}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002049
Damien Millerd79b4242006-03-31 23:11:44 +11002050/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002051void
Damien Miller630d6f42002-01-22 23:17:30 +11002052channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002053{
2054 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002055 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002056 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002057 Channel *c;
2058
2059 /* Get the channel number and verify it. */
2060 id = packet_get_int();
2061 c = channel_lookup(id);
2062
2063 if (c == NULL)
2064 packet_disconnect("Received extended_data for bad channel %d.", id);
2065 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002066 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002067 return;
2068 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002069 if (c->flags & CHAN_EOF_RCVD) {
2070 if (datafellows & SSH_BUG_EXTEOF)
2071 debug("channel %d: accepting ext data after eof", id);
2072 else
2073 packet_disconnect("Received extended_data after EOF "
2074 "on channel %d.", id);
2075 }
Damien Miller33b13562000-04-04 14:38:59 +10002076 tcode = packet_get_int();
2077 if (c->efd == -1 ||
2078 c->extended_usage != CHAN_EXTENDED_WRITE ||
2079 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002080 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002081 return;
2082 }
2083 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002084 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002085 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002086 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002087 c->self, data_len, c->local_window);
2088 xfree(data);
2089 return;
2090 }
Damien Millerd3444942000-09-30 14:20:03 +11002091 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002092 c->local_window -= data_len;
2093 buffer_append(&c->extended, data, data_len);
2094 xfree(data);
2095}
2096
Damien Millerd79b4242006-03-31 23:11:44 +11002097/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002098void
Damien Miller630d6f42002-01-22 23:17:30 +11002099channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002100{
2101 int id;
2102 Channel *c;
2103
Damien Millerb38eff82000-04-01 11:09:21 +10002104 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002105 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002106 c = channel_lookup(id);
2107 if (c == NULL)
2108 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2109 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002110
2111 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002112 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002113 debug("channel %d: FORCE input drain", c->self);
2114 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002115 if (buffer_len(&c->input) == 0)
2116 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002117 }
2118
Damien Millerb38eff82000-04-01 11:09:21 +10002119}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002120
Damien Millerd79b4242006-03-31 23:11:44 +11002121/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002122void
Damien Miller630d6f42002-01-22 23:17:30 +11002123channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002124{
Damien Millerb38eff82000-04-01 11:09:21 +10002125 int id;
2126 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002127
Damien Millerb38eff82000-04-01 11:09:21 +10002128 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002129 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002130 c = channel_lookup(id);
2131 if (c == NULL)
2132 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002133
2134 /*
2135 * Send a confirmation that we have closed the channel and no more
2136 * data is coming for it.
2137 */
Damien Miller95def091999-11-25 00:26:21 +11002138 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002139 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002140 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002141
Damien Miller5428f641999-11-25 11:54:57 +11002142 /*
2143 * If the channel is in closed state, we have sent a close request,
2144 * and the other side will eventually respond with a confirmation.
2145 * Thus, we cannot free the channel here, because then there would be
2146 * no-one to receive the confirmation. The channel gets freed when
2147 * the confirmation arrives.
2148 */
Damien Millerb38eff82000-04-01 11:09:21 +10002149 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002150 /*
2151 * Not a closed channel - mark it as draining, which will
2152 * cause it to be freed later.
2153 */
Damien Miller76765c02002-01-22 23:21:15 +11002154 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002155 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002156 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002157}
2158
Damien Millerb38eff82000-04-01 11:09:21 +10002159/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002160/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002161void
Damien Miller630d6f42002-01-22 23:17:30 +11002162channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002163{
Damien Millerb38eff82000-04-01 11:09:21 +10002164 int id = packet_get_int();
2165 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002166
Damien Miller48b03fc2002-01-22 23:11:40 +11002167 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002168 if (c == NULL)
2169 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2170 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171}
2172
Damien Millerd79b4242006-03-31 23:11:44 +11002173/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002174void
Damien Miller630d6f42002-01-22 23:17:30 +11002175channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002176{
2177 int id = packet_get_int();
2178 Channel *c = channel_lookup(id);
2179
Damien Miller48b03fc2002-01-22 23:11:40 +11002180 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002181 if (c == NULL)
2182 packet_disconnect("Received close confirmation for "
2183 "out-of-range channel %d.", id);
2184 if (c->type != SSH_CHANNEL_CLOSED)
2185 packet_disconnect("Received close confirmation for "
2186 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002187 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002188}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002189
Damien Millerd79b4242006-03-31 23:11:44 +11002190/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002191void
Damien Miller630d6f42002-01-22 23:17:30 +11002192channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002193{
Damien Millerb38eff82000-04-01 11:09:21 +10002194 int id, remote_id;
2195 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002196
Damien Millerb38eff82000-04-01 11:09:21 +10002197 id = packet_get_int();
2198 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199
Damien Millerb38eff82000-04-01 11:09:21 +10002200 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2201 packet_disconnect("Received open confirmation for "
2202 "non-opening channel %d.", id);
2203 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002204 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002205 c->remote_id = remote_id;
2206 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002207
2208 if (compat20) {
2209 c->remote_window = packet_get_int();
2210 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002211 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002212 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002213 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002214 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002215 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002216 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002217 c->remote_window, c->remote_maxpacket);
2218 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002219 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002220}
2221
Ben Lindstrombba81212001-06-25 05:01:22 +00002222static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002223reason2txt(int reason)
2224{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002225 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002226 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2227 return "administratively prohibited";
2228 case SSH2_OPEN_CONNECT_FAILED:
2229 return "connect failed";
2230 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2231 return "unknown channel type";
2232 case SSH2_OPEN_RESOURCE_SHORTAGE:
2233 return "resource shortage";
2234 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002235 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002236}
2237
Damien Millerd79b4242006-03-31 23:11:44 +11002238/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002239void
Damien Miller630d6f42002-01-22 23:17:30 +11002240channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002241{
Kevin Steves12057502001-02-05 14:54:34 +00002242 int id, reason;
2243 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002244 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002245
Damien Millerb38eff82000-04-01 11:09:21 +10002246 id = packet_get_int();
2247 c = channel_lookup(id);
2248
2249 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2250 packet_disconnect("Received open failure for "
2251 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002252 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002253 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002254 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002255 msg = packet_get_string(NULL);
2256 lang = packet_get_string(NULL);
2257 }
Damien Miller996acd22003-04-09 20:59:48 +10002258 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002259 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002260 if (msg != NULL)
2261 xfree(msg);
2262 if (lang != NULL)
2263 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002264 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002265 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002266 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002267 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268}
2269
Damien Millerd79b4242006-03-31 23:11:44 +11002270/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002271void
Damien Miller630d6f42002-01-22 23:17:30 +11002272channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002273{
2274 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002275 int id;
2276 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002277
2278 if (!compat20)
2279 return;
2280
2281 /* Get the channel number and verify it. */
2282 id = packet_get_int();
2283 c = channel_lookup(id);
2284
Damien Millerd47c62a2005-12-13 19:33:57 +11002285 if (c == NULL) {
2286 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002287 return;
2288 }
2289 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002290 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002291 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002292 c->remote_window += adjust;
2293}
2294
Damien Millerd79b4242006-03-31 23:11:44 +11002295/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002296void
Damien Miller630d6f42002-01-22 23:17:30 +11002297channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002298{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002299 Channel *c = NULL;
2300 u_short host_port;
2301 char *host, *originator_string;
2302 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002303
Ben Lindstrome9c99912001-06-09 00:41:05 +00002304 remote_id = packet_get_int();
2305 host = packet_get_string(NULL);
2306 host_port = packet_get_int();
2307
2308 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2309 originator_string = packet_get_string(NULL);
2310 } else {
2311 originator_string = xstrdup("unknown (remote did not supply name)");
2312 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002313 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002314 sock = channel_connect_to(host, host_port);
2315 if (sock != -1) {
2316 c = channel_new("connected socket",
2317 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2318 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002319 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002321 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002322 if (c == NULL) {
2323 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2324 packet_put_int(remote_id);
2325 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002326 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002327 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002328}
2329
2330
Ben Lindstrome9c99912001-06-09 00:41:05 +00002331/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002332
Ben Lindstrom908afed2001-10-03 17:34:59 +00002333void
2334channel_set_af(int af)
2335{
2336 IPv4or6 = af;
2337}
2338
Damien Millerb16461c2002-01-22 23:29:22 +11002339static int
2340channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2341 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002342{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002343 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002344 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002345 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002346 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002347 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002348
Damien Millerb16461c2002-01-22 23:29:22 +11002349 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2350 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002351 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002352
Damien Millerb16461c2002-01-22 23:29:22 +11002353 if (host == NULL) {
2354 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002355 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002356 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002357 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002358 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002359 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002360 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002361
Damien Miller5428f641999-11-25 11:54:57 +11002362 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002363 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002364 * specified address or wildcard. On the client, a specified bind
2365 * address will always override gateway_ports. On the server, a
2366 * gateway_ports of 1 (``yes'') will override the client's
2367 * specification and force a wildcard bind, whereas a value of 2
2368 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002369 * asked for.
2370 *
2371 * Special-case listen_addrs are:
2372 *
2373 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2374 * "" (empty string), "*" -> wildcard v4/v6
2375 * "localhost" -> loopback v4/v6
2376 */
2377 addr = NULL;
2378 if (listen_addr == NULL) {
2379 /* No address specified: default to gateway_ports setting */
2380 if (gateway_ports)
2381 wildcard = 1;
2382 } else if (gateway_ports || is_client) {
2383 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2384 strcmp(listen_addr, "0.0.0.0") == 0) ||
2385 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2386 (!is_client && gateway_ports == 1))
2387 wildcard = 1;
2388 else if (strcmp(listen_addr, "localhost") != 0)
2389 addr = listen_addr;
2390 }
2391
2392 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2393 type, wildcard, (addr == NULL) ? "NULL" : addr);
2394
2395 /*
Damien Miller34132e52000-01-14 15:45:46 +11002396 * getaddrinfo returns a loopback address if the hostname is
2397 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002398 */
Damien Miller34132e52000-01-14 15:45:46 +11002399 memset(&hints, 0, sizeof(hints));
2400 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002401 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002402 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002403 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002404 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2405 if (addr == NULL) {
2406 /* This really shouldn't happen */
2407 packet_disconnect("getaddrinfo: fatal error: %s",
2408 gai_strerror(r));
2409 } else {
Damien Millera7270302005-07-06 09:36:05 +10002410 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002411 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2412 }
Damien Millera7270302005-07-06 09:36:05 +10002413 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002414 }
Damien Miller5428f641999-11-25 11:54:57 +11002415
Damien Miller34132e52000-01-14 15:45:46 +11002416 for (ai = aitop; ai; ai = ai->ai_next) {
2417 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2418 continue;
2419 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2420 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002421 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002422 continue;
2423 }
2424 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002425 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002426 if (sock < 0) {
2427 /* this is no error since kernel may not support ipv6 */
2428 verbose("socket: %.100s", strerror(errno));
2429 continue;
2430 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002431
2432 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002433
Damien Miller34132e52000-01-14 15:45:46 +11002434 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002435
Damien Miller34132e52000-01-14 15:45:46 +11002436 /* Bind the socket to the address. */
2437 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2438 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002439 if (!ai->ai_next)
2440 error("bind: %.100s", strerror(errno));
2441 else
2442 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002443
Damien Miller34132e52000-01-14 15:45:46 +11002444 close(sock);
2445 continue;
2446 }
2447 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002448 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002449 error("listen: %.100s", strerror(errno));
2450 close(sock);
2451 continue;
2452 }
2453 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002454 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002455 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002456 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002457 strlcpy(c->path, host, sizeof(c->path));
2458 c->host_port = port_to_connect;
2459 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002460 success = 1;
2461 }
2462 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002463 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002464 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002465 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002466 return success;
Damien Miller95def091999-11-25 00:26:21 +11002467}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002468
Darren Tuckere7066df2004-05-24 10:18:05 +10002469int
2470channel_cancel_rport_listener(const char *host, u_short port)
2471{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002472 u_int i;
2473 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002474
Darren Tucker47eede72005-03-14 23:08:12 +11002475 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002476 Channel *c = channels[i];
2477
2478 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2479 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002480 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002481 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002482 channel_free(c);
2483 found = 1;
2484 }
2485 }
2486
2487 return (found);
2488}
2489
Damien Millerb16461c2002-01-22 23:29:22 +11002490/* protocol local port fwd, used by ssh (and sshd in v1) */
2491int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002492channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002493 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2494{
2495 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002496 listen_host, listen_port, host_to_connect, port_to_connect,
2497 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002498}
2499
2500/* protocol v2 remote port fwd, used by sshd */
2501int
2502channel_setup_remote_fwd_listener(const char *listen_address,
2503 u_short listen_port, int gateway_ports)
2504{
2505 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2506 listen_address, listen_port, NULL, 0, gateway_ports);
2507}
2508
Damien Miller5428f641999-11-25 11:54:57 +11002509/*
2510 * Initiate forwarding of connections to port "port" on remote host through
2511 * the secure channel to host:port from local side.
2512 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002513
Darren Tuckere7d4b192006-07-12 22:17:10 +10002514int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002515channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002516 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002517{
Damien Millerdff50992002-01-22 23:16:32 +11002518 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002519
Damien Miller95def091999-11-25 00:26:21 +11002520 /* Record locally that connection to this host/port is permitted. */
2521 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2522 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002523
Damien Miller95def091999-11-25 00:26:21 +11002524 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002525 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002526 const char *address_to_bind;
2527 if (listen_host == NULL)
2528 address_to_bind = "localhost";
2529 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2530 address_to_bind = "";
2531 else
2532 address_to_bind = listen_host;
2533
Damien Miller33b13562000-04-04 14:38:59 +10002534 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2535 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002536 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002537 packet_put_cstring(address_to_bind);
2538 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002539 packet_send();
2540 packet_write_wait();
2541 /* Assume that server accepts the request */
2542 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002543 } else {
2544 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002545 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002546 packet_put_cstring(host_to_connect);
2547 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002548 packet_send();
2549 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002550
2551 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002552 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002553 switch (type) {
2554 case SSH_SMSG_SUCCESS:
2555 success = 1;
2556 break;
2557 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002558 break;
2559 default:
2560 /* Unknown packet */
2561 packet_disconnect("Protocol error for port forward request:"
2562 "received packet type %d.", type);
2563 }
2564 }
2565 if (success) {
2566 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2567 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2568 permitted_opens[num_permitted_opens].listen_port = listen_port;
2569 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002570 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002571 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002572}
2573
Damien Miller5428f641999-11-25 11:54:57 +11002574/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002575 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002576 * local side.
2577 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002578void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002579channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002580{
2581 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002582
2583 if (!compat20)
2584 return;
2585
2586 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002587 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002588 permitted_opens[i].listen_port == port)
2589 break;
2590 }
2591 if (i >= num_permitted_opens) {
2592 debug("%s: requested forward not found", __func__);
2593 return;
2594 }
2595 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2596 packet_put_cstring("cancel-tcpip-forward");
2597 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002598 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002599 packet_put_int(port);
2600 packet_send();
2601
2602 permitted_opens[i].listen_port = 0;
2603 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002604 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002605 permitted_opens[i].host_to_connect = NULL;
2606}
2607
2608/*
Damien Miller5428f641999-11-25 11:54:57 +11002609 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2610 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002611 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002612 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002613int
Damien Millere247cc42000-05-07 12:03:14 +10002614channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002615{
Damien Milleraae6c611999-12-06 11:47:28 +11002616 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002617 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002618 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002619
Damien Miller95def091999-11-25 00:26:21 +11002620 /* Get arguments from the packet. */
2621 port = packet_get_int();
2622 hostname = packet_get_string(NULL);
2623 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002624
Damien Millerbac2d8a2000-09-05 16:13:06 +11002625#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002626 /*
2627 * Check that an unprivileged user is not trying to forward a
2628 * privileged port.
2629 */
Damien Miller95def091999-11-25 00:26:21 +11002630 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002631 packet_disconnect(
2632 "Requested forwarding of port %d but user is not root.",
2633 port);
2634 if (host_port == 0)
2635 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002636#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002637
Damien Miller0bc1bd82000-11-13 22:57:25 +11002638 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002639 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002640 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002641
2642 /* Free the argument string. */
2643 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002644
2645 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002646}
2647
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002648/*
2649 * Permits opening to any host/port if permitted_opens[] is empty. This is
2650 * usually called by the server, because the user could connect to any port
2651 * anyway, and the server has no way to know but to trust the client anyway.
2652 */
2653void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002654channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002655{
2656 if (num_permitted_opens == 0)
2657 all_opens_permitted = 1;
2658}
2659
Ben Lindstroma3700052001-04-05 23:26:32 +00002660void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002661channel_add_permitted_opens(char *host, int port)
2662{
2663 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002664 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002665 debug("allow port forwarding to host %s port %d", host, port);
2666
2667 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2668 permitted_opens[num_permitted_opens].port_to_connect = port;
2669 num_permitted_opens++;
2670
2671 all_opens_permitted = 0;
2672}
2673
Damien Millera765cf42006-07-24 14:08:13 +10002674int
Damien Miller9b439df2006-07-24 14:04:00 +10002675channel_add_adm_permitted_opens(char *host, int port)
2676{
2677 if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2678 fatal("channel_add_adm_permitted_opens: too many forwards");
Damien Millera765cf42006-07-24 14:08:13 +10002679 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10002680
2681 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2682 = xstrdup(host);
2683 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Millera765cf42006-07-24 14:08:13 +10002684 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10002685}
2686
2687void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002688channel_clear_permitted_opens(void)
2689{
2690 int i;
2691
2692 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002693 if (permitted_opens[i].host_to_connect != NULL)
2694 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002695 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10002696}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002697
Damien Miller9b439df2006-07-24 14:04:00 +10002698void
2699channel_clear_adm_permitted_opens(void)
2700{
2701 int i;
2702
2703 for (i = 0; i < num_adm_permitted_opens; i++)
2704 if (permitted_adm_opens[i].host_to_connect != NULL)
2705 xfree(permitted_adm_opens[i].host_to_connect);
2706 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002707}
2708
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002709/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002710static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002711connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002712{
Damien Miller34132e52000-01-14 15:45:46 +11002713 struct addrinfo hints, *ai, *aitop;
2714 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2715 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002716 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002717
Damien Miller34132e52000-01-14 15:45:46 +11002718 memset(&hints, 0, sizeof(hints));
2719 hints.ai_family = IPv4or6;
2720 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002721 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002722 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002723 error("connect_to %.100s: unknown host (%s)", host,
2724 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002725 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002726 }
Damien Miller34132e52000-01-14 15:45:46 +11002727 for (ai = aitop; ai; ai = ai->ai_next) {
2728 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2729 continue;
2730 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2731 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002732 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002733 continue;
2734 }
Damien Miller2372ace2003-05-14 13:42:23 +10002735 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002736 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002737 if (ai->ai_next == NULL)
2738 error("socket: %.100s", strerror(errno));
2739 else
2740 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002741 continue;
2742 }
Damien Miller232711f2004-06-15 10:35:30 +10002743 if (set_nonblock(sock) == -1)
2744 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002745 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2746 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002747 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002748 strerror(errno));
2749 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002750 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002751 }
2752 break; /* success */
2753
2754 }
2755 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002756 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002757 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002758 return -1;
2759 }
2760 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002761 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002762 return sock;
2763}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002764
Damien Miller0bc1bd82000-11-13 22:57:25 +11002765int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002766channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002767{
2768 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002769
Damien Miller0bc1bd82000-11-13 22:57:25 +11002770 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002771 if (permitted_opens[i].host_to_connect != NULL &&
2772 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002773 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002774 permitted_opens[i].host_to_connect,
2775 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002776 error("WARNING: Server requests forwarding for unknown listen_port %d",
2777 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002778 return -1;
2779}
Damien Millerb38eff82000-04-01 11:09:21 +10002780
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002781/* Check if connecting to that port is permitted and connect. */
2782int
2783channel_connect_to(const char *host, u_short port)
2784{
Damien Miller9b439df2006-07-24 14:04:00 +10002785 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002786
2787 permit = all_opens_permitted;
2788 if (!permit) {
2789 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002790 if (permitted_opens[i].host_to_connect != NULL &&
2791 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002792 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2793 permit = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002794 }
Damien Miller9b439df2006-07-24 14:04:00 +10002795
2796 if (num_adm_permitted_opens > 0) {
2797 permit_adm = 0;
2798 for (i = 0; i < num_adm_permitted_opens; i++)
2799 if (permitted_adm_opens[i].host_to_connect != NULL &&
2800 permitted_adm_opens[i].port_to_connect == port &&
2801 strcmp(permitted_adm_opens[i].host_to_connect, host)
2802 == 0)
2803 permit_adm = 1;
2804 }
2805
2806 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10002807 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002808 "but the request was denied.", host, port);
2809 return -1;
2810 }
2811 return connect_to(host, port);
2812}
2813
Damien Miller0e220db2004-06-15 10:34:08 +10002814void
2815channel_send_window_changes(void)
2816{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002817 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002818 struct winsize ws;
2819
2820 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002821 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002822 channels[i]->type != SSH_CHANNEL_OPEN)
2823 continue;
2824 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2825 continue;
2826 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002827 packet_put_int((u_int)ws.ws_col);
2828 packet_put_int((u_int)ws.ws_row);
2829 packet_put_int((u_int)ws.ws_xpixel);
2830 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002831 packet_send();
2832 }
2833}
2834
Ben Lindstrome9c99912001-06-09 00:41:05 +00002835/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002836
Damien Miller5428f641999-11-25 11:54:57 +11002837/*
2838 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002839 * Returns 0 and a suitable display number for the DISPLAY variable
2840 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002841 */
Kevin Steves366298c2001-12-19 17:58:01 +00002842int
Damien Miller95c249f2002-02-05 12:11:34 +11002843x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002844 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002845{
Damien Millere7378562001-12-21 14:58:35 +11002846 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002847 int display_number, sock;
2848 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002849 struct addrinfo hints, *ai, *aitop;
2850 char strport[NI_MAXSERV];
2851 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002852
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002853 if (chanids == NULL)
2854 return -1;
2855
Damien Millera34a28b1999-12-14 10:47:15 +11002856 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002857 display_number < MAX_DISPLAYS;
2858 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002859 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002860 memset(&hints, 0, sizeof(hints));
2861 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002862 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002863 hints.ai_socktype = SOCK_STREAM;
2864 snprintf(strport, sizeof strport, "%d", port);
2865 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2866 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002867 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002868 }
Damien Miller34132e52000-01-14 15:45:46 +11002869 for (ai = aitop; ai; ai = ai->ai_next) {
2870 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2871 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002872 sock = socket(ai->ai_family, ai->ai_socktype,
2873 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002874 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002875 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002876 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002877 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002878 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002879 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002880 debug("x11_create_display_inet: Socket family %d not supported",
2881 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002882 continue;
2883 }
Damien Miller34132e52000-01-14 15:45:46 +11002884 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002885#ifdef IPV6_V6ONLY
2886 if (ai->ai_family == AF_INET6) {
2887 int on = 1;
2888 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2889 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2890 }
2891#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002892 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002893 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002894 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002895 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002896
2897 if (ai->ai_next)
2898 continue;
2899
Damien Miller34132e52000-01-14 15:45:46 +11002900 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002901 close(socks[n]);
2902 }
2903 num_socks = 0;
2904 break;
2905 }
2906 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002907#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002908 if (num_socks == NUM_SOCKS)
2909 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002910#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002911 if (x11_use_localhost) {
2912 if (num_socks == NUM_SOCKS)
2913 break;
2914 } else {
2915 break;
2916 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002917#endif
Damien Miller95def091999-11-25 00:26:21 +11002918 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002919 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002920 if (num_socks > 0)
2921 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002922 }
Damien Miller95def091999-11-25 00:26:21 +11002923 if (display_number >= MAX_DISPLAYS) {
2924 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002925 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002926 }
Damien Miller95def091999-11-25 00:26:21 +11002927 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002928 for (n = 0; n < num_socks; n++) {
2929 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002930 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002931 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002932 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002933 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002934 }
Damien Miller95def091999-11-25 00:26:21 +11002935 }
Damien Miller34132e52000-01-14 15:45:46 +11002936
Damien Miller34132e52000-01-14 15:45:46 +11002937 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002938 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002939 for (n = 0; n < num_socks; n++) {
2940 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002941 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002942 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2943 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002944 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002945 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002946 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002947 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002948 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002949
Kevin Steves366298c2001-12-19 17:58:01 +00002950 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002951 *display_numberp = display_number;
2952 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002953}
2954
Ben Lindstrombba81212001-06-25 05:01:22 +00002955static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002956connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002957{
Damien Miller95def091999-11-25 00:26:21 +11002958 int sock;
2959 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002960
Damien Miller3afe3752001-12-21 12:39:51 +11002961 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2962 if (sock < 0)
2963 error("socket: %.100s", strerror(errno));
2964 memset(&addr, 0, sizeof(addr));
2965 addr.sun_family = AF_UNIX;
2966 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002967 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002968 return sock;
2969 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002970 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2971 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002972}
2973
Damien Millerbd483e72000-04-30 10:00:53 +10002974int
2975x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002976{
Damien Miller57c4e872006-03-31 23:11:07 +11002977 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002978 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002979 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002980 struct addrinfo hints, *ai, *aitop;
2981 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002982 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002983
Damien Miller95def091999-11-25 00:26:21 +11002984 /* Try to open a socket for the local X server. */
2985 display = getenv("DISPLAY");
2986 if (!display) {
2987 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002988 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002989 }
Damien Miller5428f641999-11-25 11:54:57 +11002990 /*
2991 * Now we decode the value of the DISPLAY variable and make a
2992 * connection to the real X server.
2993 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002994
Damien Miller5428f641999-11-25 11:54:57 +11002995 /*
2996 * Check if it is a unix domain socket. Unix domain displays are in
2997 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2998 */
Damien Miller95def091999-11-25 00:26:21 +11002999 if (strncmp(display, "unix:", 5) == 0 ||
3000 display[0] == ':') {
3001 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11003002 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003003 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003004 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003005 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003006 }
3007 /* Create a socket. */
3008 sock = connect_local_xsocket(display_number);
3009 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10003010 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003011
3012 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10003013 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003014 }
Damien Miller5428f641999-11-25 11:54:57 +11003015 /*
3016 * Connect to an inet socket. The DISPLAY value is supposedly
3017 * hostname:d[.s], where hostname may also be numeric IP address.
3018 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00003019 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11003020 cp = strchr(buf, ':');
3021 if (!cp) {
3022 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10003023 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003024 }
Damien Miller95def091999-11-25 00:26:21 +11003025 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11003026 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11003027 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003028 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003029 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003030 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003031 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003032
Damien Miller34132e52000-01-14 15:45:46 +11003033 /* Look up the host address */
3034 memset(&hints, 0, sizeof(hints));
3035 hints.ai_family = IPv4or6;
3036 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11003037 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11003038 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
3039 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10003040 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003041 }
Damien Miller34132e52000-01-14 15:45:46 +11003042 for (ai = aitop; ai; ai = ai->ai_next) {
3043 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10003044 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003045 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003046 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10003047 continue;
3048 }
3049 /* Connect it to the display. */
3050 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11003051 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10003052 6000 + display_number, strerror(errno));
3053 close(sock);
3054 continue;
3055 }
3056 /* Success */
3057 break;
Damien Miller34132e52000-01-14 15:45:46 +11003058 }
Damien Miller34132e52000-01-14 15:45:46 +11003059 freeaddrinfo(aitop);
3060 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11003061 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11003062 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003063 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003064 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003065 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003066 return sock;
3067}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003068
Damien Millerbd483e72000-04-30 10:00:53 +10003069/*
3070 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3071 * the remote channel number. We should do whatever we want, and respond
3072 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3073 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003074
Damien Miller8473dd82006-07-24 14:08:32 +10003075/* ARGSUSED */
Damien Millerbd483e72000-04-30 10:00:53 +10003076void
Damien Miller630d6f42002-01-22 23:17:30 +11003077x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003078{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003079 Channel *c = NULL;
3080 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003081 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003082
3083 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003084
3085 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003086
3087 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003088 remote_host = packet_get_string(NULL);
3089 } else {
3090 remote_host = xstrdup("unknown (remote did not supply name)");
3091 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003092 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003093
3094 /* Obtain a connection to the real X display. */
3095 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003096 if (sock != -1) {
3097 /* Allocate a channel for this connection. */
3098 c = channel_new("connected x11 socket",
3099 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3100 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003101 c->remote_id = remote_id;
3102 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003103 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003104 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003105 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003106 /* Send refusal to the remote host. */
3107 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003108 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003109 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003110 /* Send a confirmation to the remote host. */
3111 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003112 packet_put_int(remote_id);
3113 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003114 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003115 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003116}
3117
Damien Miller69b69aa2000-10-28 14:19:58 +11003118/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10003119/* ARGSUSED */
Damien Miller69b69aa2000-10-28 14:19:58 +11003120void
Damien Miller630d6f42002-01-22 23:17:30 +11003121deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003122{
3123 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003124
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003125 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003126 case SSH_SMSG_AGENT_OPEN:
3127 error("Warning: ssh server tried agent forwarding.");
3128 break;
3129 case SSH_SMSG_X11_OPEN:
3130 error("Warning: ssh server tried X11 forwarding.");
3131 break;
3132 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003133 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003134 break;
3135 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003136 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003137 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3138 packet_put_int(rchan);
3139 packet_send();
3140}
3141
Damien Miller5428f641999-11-25 11:54:57 +11003142/*
3143 * Requests forwarding of X11 connections, generates fake authentication
3144 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003145 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003146 */
Damien Miller4af51302000-04-16 11:18:38 +10003147void
Damien Miller17e7ed02005-06-17 12:54:33 +10003148x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003149 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003150{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003151 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003152 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003153 char *new_data;
3154 int screen_number;
3155 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003156 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003157
Damien Millerf92c0792005-07-06 09:45:26 +10003158 if (x11_saved_display == NULL)
3159 x11_saved_display = xstrdup(disp);
3160 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003161 error("x11_request_forwarding_with_spoofing: different "
3162 "$DISPLAY already forwarded");
3163 return;
3164 }
3165
Damien Miller17e7ed02005-06-17 12:54:33 +10003166 cp = disp;
3167 if (disp)
3168 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003169 if (cp)
3170 cp = strchr(cp, '.');
3171 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003172 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003173 else
3174 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003175
Damien Miller13390022005-07-06 09:44:19 +10003176 if (x11_saved_proto == NULL) {
3177 /* Save protocol name. */
3178 x11_saved_proto = xstrdup(proto);
3179 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003180 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003181 * of the same length.
3182 */
3183 x11_saved_data = xmalloc(data_len);
3184 x11_fake_data = xmalloc(data_len);
3185 for (i = 0; i < data_len; i++) {
3186 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3187 fatal("x11_request_forwarding: bad "
3188 "authentication data: %.100s", data);
3189 if (i % 4 == 0)
3190 rnd = arc4random();
3191 x11_saved_data[i] = value;
3192 x11_fake_data[i] = rnd & 0xff;
3193 rnd >>= 8;
3194 }
3195 x11_saved_data_len = data_len;
3196 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003197 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003198
Damien Miller95def091999-11-25 00:26:21 +11003199 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003200 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003201
Damien Miller95def091999-11-25 00:26:21 +11003202 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003203 if (compat20) {
3204 channel_request_start(client_session_id, "x11-req", 0);
3205 packet_put_char(0); /* XXX bool single connection */
3206 } else {
3207 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3208 }
3209 packet_put_cstring(proto);
3210 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003211 packet_put_int(screen_number);
3212 packet_send();
3213 packet_write_wait();
3214 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003215}
3216
Ben Lindstrome9c99912001-06-09 00:41:05 +00003217
3218/* -- agent forwarding */
3219
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003220/* Sends a message to the server to request authentication fd forwarding. */
3221
Damien Miller4af51302000-04-16 11:18:38 +10003222void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003223auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003224{
Damien Miller95def091999-11-25 00:26:21 +11003225 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3226 packet_send();
3227 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003228}