blob: fbbae9ed7d5b9458f895a6a7a67900450637a2f2 [file] [log] [blame]
Damien Millere6b3b612006-07-24 14:01:23 +10001/* $OpenBSD: channels.c,v 1.256 2006/07/17 01:31:09 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>
48
49#include <netinet/in.h>
50#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110051
Darren Tucker39972492006-07-12 22:22:46 +100052#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100053#if defined(HAVE_NETDB_H)
54# include <netdb.h>
55#endif
Damien Miller99bd21e2006-03-15 11:11:28 +110056#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100057#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "ssh1.h"
61#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "packet.h"
63#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
65#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100069#include "key.h"
70#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110071#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100072#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Ben Lindstrome9c99912001-06-09 00:41:05 +000074/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Pointer to an array containing all allocated channels. The array is
78 * dynamically extended as needed.
79 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000080static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000084 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110085 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100086static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000090 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110091 */
Damien Miller5e953212001-01-30 09:14:00 +110092static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Ben Lindstrome9c99912001-06-09 00:41:05 +000095/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller5428f641999-11-25 11:54:57 +110097/*
98 * Data structure for storing which hosts are permitted for forward requests.
99 * The local sides of any remote forwards are stored in this array to prevent
100 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
101 * network (which might be behind a firewall).
102 */
Damien Miller95def091999-11-25 00:26:21 +1100103typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000104 char *host_to_connect; /* Connect to 'host'. */
105 u_short port_to_connect; /* Connect to 'port'. */
106 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107} ForwardPermission;
108
109/* List of all permitted host/port pairs to connect. */
110static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000111
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112/* Number of permitted host/port pairs in the array. */
113static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100114/*
115 * If this is true, all opens are permitted. This is the case on the server
116 * on which we have to trust the client anyway, and the user could do
117 * anything after logging in anyway.
118 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119static int all_opens_permitted = 0;
120
Ben Lindstrome9c99912001-06-09 00:41:05 +0000121
122/* -- X11 forwarding */
123
124/* Maximum number of fake X11 displays to try. */
125#define MAX_DISPLAYS 1000
126
Damien Miller13390022005-07-06 09:44:19 +1000127/* Saved X11 local (client) display. */
128static char *x11_saved_display = NULL;
129
Ben Lindstrome9c99912001-06-09 00:41:05 +0000130/* Saved X11 authentication protocol name. */
131static char *x11_saved_proto = NULL;
132
133/* Saved X11 authentication data. This is the real data. */
134static char *x11_saved_data = NULL;
135static u_int x11_saved_data_len = 0;
136
137/*
138 * Fake X11 authentication data. This is what the server will be sending us;
139 * we should replace any occurrences of this by the real data.
140 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100141static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000142static u_int x11_fake_data_len;
143
144
145/* -- agent forwarding */
146
147#define NUM_SOCKS 10
148
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000149/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000150static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000151
Ben Lindstrome9c99912001-06-09 00:41:05 +0000152/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000154
Ben Lindstrome9c99912001-06-09 00:41:05 +0000155/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000156
157Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100158channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000159{
160 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000161
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000162 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100163 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000164 return NULL;
165 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000166 c = channels[id];
167 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100168 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000169 return NULL;
170 }
171 return c;
172}
173
Damien Miller5428f641999-11-25 11:54:57 +1100174/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100175 * Returns the channel if it is allowed to receive protocol messages.
176 * Private channels, like listening sockets, may not receive messages.
177 */
178Channel *
179channel_lookup(int id)
180{
181 Channel *c;
182
183 if ((c = channel_by_id(id)) == NULL)
184 return (NULL);
185
Damien Millerd62f2ca2006-03-26 13:57:41 +1100186 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100187 case SSH_CHANNEL_X11_OPEN:
188 case SSH_CHANNEL_LARVAL:
189 case SSH_CHANNEL_CONNECTING:
190 case SSH_CHANNEL_DYNAMIC:
191 case SSH_CHANNEL_OPENING:
192 case SSH_CHANNEL_OPEN:
193 case SSH_CHANNEL_INPUT_DRAINING:
194 case SSH_CHANNEL_OUTPUT_DRAINING:
195 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100196 }
197 logit("Non-public channel %d, type %d.", id, c->type);
198 return (NULL);
199}
200
201/*
Damien Millere247cc42000-05-07 12:03:14 +1000202 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000203 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100204 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100206channel_register_fds(Channel *c, int rfd, int wfd, int efd,
207 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller95def091999-11-25 00:26:21 +1100209 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100210 channel_max_fd = MAX(channel_max_fd, rfd);
211 channel_max_fd = MAX(channel_max_fd, wfd);
212 channel_max_fd = MAX(channel_max_fd, efd);
213
Damien Miller5428f641999-11-25 11:54:57 +1100214 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000215
Damien Miller6f83b8e2000-05-02 09:23:45 +1000216 c->rfd = rfd;
217 c->wfd = wfd;
218 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000219 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000220 c->efd = efd;
221 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100222
Damien Miller79438cc2001-02-16 12:34:57 +1100223 /* XXX ugly hack: nonblock is only set by the server */
224 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000225 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100226 c->isatty = 1;
227 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000228 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100229 c->self, c->wfd);
230 }
231 } else {
232 c->isatty = 0;
233 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000234 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100235
Damien Miller69b69aa2000-10-28 14:19:58 +1100236 /* enable nonblocking mode */
237 if (nonblock) {
238 if (rfd != -1)
239 set_nonblock(rfd);
240 if (wfd != -1)
241 set_nonblock(wfd);
242 if (efd != -1)
243 set_nonblock(efd);
244 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000245}
246
247/*
248 * Allocate a new channel object and set its type and socket. This will cause
249 * remote_name to be freed.
250 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000251Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000252channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000253 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000254{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000255 int found;
256 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000257 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 /* Do initial allocation if this is the first call. */
260 if (channels_alloc == 0) {
261 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100262 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100263 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000264 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100265 }
266 /* Try to find a free slot where to put the new channel. */
267 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000268 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100269 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000270 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100271 break;
272 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000273 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100274 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100275 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000276 if (channels_alloc > 10000)
277 fatal("channel_new: internal error: channels_alloc %d "
278 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100279 channels = xrealloc(channels, channels_alloc + 10,
280 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000281 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100282 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100283 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000284 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100285 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000286 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100287 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100288 buffer_init(&c->input);
289 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000290 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100291 c->ostate = CHAN_OUTPUT_OPEN;
292 c->istate = CHAN_INPUT_OPEN;
293 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100294 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100295 c->self = found;
296 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000297 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000298 c->local_window = window;
299 c->local_window_max = window;
300 c->local_consumed = 0;
301 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100302 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000303 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000304 c->remote_window = 0;
305 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000306 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100307 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000308 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100309 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100310 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000311 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000312 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100313 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100314 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000315 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000317
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000318static int
319channel_find_maxfd(void)
320{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000321 u_int i;
322 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000323 Channel *c;
324
325 for (i = 0; i < channels_alloc; i++) {
326 c = channels[i];
327 if (c != NULL) {
328 max = MAX(max, c->rfd);
329 max = MAX(max, c->wfd);
330 max = MAX(max, c->efd);
331 }
332 }
333 return max;
334}
335
336int
337channel_close_fd(int *fdp)
338{
339 int ret = 0, fd = *fdp;
340
341 if (fd != -1) {
342 ret = close(fd);
343 *fdp = -1;
344 if (fd == channel_max_fd)
345 channel_max_fd = channel_find_maxfd();
346 }
347 return ret;
348}
349
Damien Miller6f83b8e2000-05-02 09:23:45 +1000350/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000351static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000352channel_close_fds(Channel *c)
353{
Damien Miller0e220db2004-06-15 10:34:08 +1000354 debug3("channel %d: close_fds r %d w %d e %d c %d",
355 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000356
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000357 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000358 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000359 channel_close_fd(&c->rfd);
360 channel_close_fd(&c->wfd);
361 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000362}
363
364/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000365void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000366channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000368 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000369 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000370
371 for (n = 0, i = 0; i < channels_alloc; i++)
372 if (channels[i])
373 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000374 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000375 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000376
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000377 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000378 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000379 xfree(s);
380
Damien Miller6f83b8e2000-05-02 09:23:45 +1000381 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000382 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000383 if (c->ctl_fd != -1)
384 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000385 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000386 buffer_free(&c->input);
387 buffer_free(&c->output);
388 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000389 if (c->remote_name) {
390 xfree(c->remote_name);
391 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100392 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000393 channels[c->self] = NULL;
394 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395}
396
Ben Lindstrome9c99912001-06-09 00:41:05 +0000397void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000398channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000399{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000400 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000401
Ben Lindstrom601e4362001-06-21 03:19:23 +0000402 for (i = 0; i < channels_alloc; i++)
403 if (channels[i] != NULL)
404 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000405}
406
407/*
408 * Closes the sockets/fds of all channels. This is used to close extra file
409 * descriptors after a fork.
410 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000411void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000412channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000413{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000414 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000415
416 for (i = 0; i < channels_alloc; i++)
417 if (channels[i] != NULL)
418 channel_close_fds(channels[i]);
419}
420
421/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000422 * Stop listening to channels.
423 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000424void
425channel_stop_listening(void)
426{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000427 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000428 Channel *c;
429
430 for (i = 0; i < channels_alloc; i++) {
431 c = channels[i];
432 if (c != NULL) {
433 switch (c->type) {
434 case SSH_CHANNEL_AUTH_SOCKET:
435 case SSH_CHANNEL_PORT_LISTENER:
436 case SSH_CHANNEL_RPORT_LISTENER:
437 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000438 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000439 channel_free(c);
440 break;
441 }
442 }
443 }
444}
445
446/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000447 * Returns true if no channel has too much buffered data, and false if one or
448 * more channel is overfull.
449 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000450int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000451channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000452{
453 u_int i;
454 Channel *c;
455
456 for (i = 0; i < channels_alloc; i++) {
457 c = channels[i];
458 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000459#if 0
460 if (!compat20 &&
461 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100462 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000463 c->self, buffer_len(&c->input));
464 return 0;
465 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000466#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000467 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000468 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000469 c->self, buffer_len(&c->output),
470 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000471 return 0;
472 }
473 }
474 }
475 return 1;
476}
477
478/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000479int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000480channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000481{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000482 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000483 Channel *c;
484
485 for (i = 0; i < channels_alloc; i++) {
486 c = channels[i];
487 if (c == NULL)
488 continue;
489 switch (c->type) {
490 case SSH_CHANNEL_X11_LISTENER:
491 case SSH_CHANNEL_PORT_LISTENER:
492 case SSH_CHANNEL_RPORT_LISTENER:
493 case SSH_CHANNEL_CLOSED:
494 case SSH_CHANNEL_AUTH_SOCKET:
495 case SSH_CHANNEL_DYNAMIC:
496 case SSH_CHANNEL_CONNECTING:
497 case SSH_CHANNEL_ZOMBIE:
498 continue;
499 case SSH_CHANNEL_LARVAL:
500 if (!compat20)
501 fatal("cannot happen: SSH_CHANNEL_LARVAL");
502 continue;
503 case SSH_CHANNEL_OPENING:
504 case SSH_CHANNEL_OPEN:
505 case SSH_CHANNEL_X11_OPEN:
506 return 1;
507 case SSH_CHANNEL_INPUT_DRAINING:
508 case SSH_CHANNEL_OUTPUT_DRAINING:
509 if (!compat13)
510 fatal("cannot happen: OUT_DRAIN");
511 return 1;
512 default:
513 fatal("channel_still_open: bad channel type %d", c->type);
514 /* NOTREACHED */
515 }
516 }
517 return 0;
518}
519
520/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000521int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000522channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000523{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000524 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000525 Channel *c;
526
527 for (i = 0; i < channels_alloc; i++) {
528 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000529 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000530 continue;
531 switch (c->type) {
532 case SSH_CHANNEL_CLOSED:
533 case SSH_CHANNEL_DYNAMIC:
534 case SSH_CHANNEL_X11_LISTENER:
535 case SSH_CHANNEL_PORT_LISTENER:
536 case SSH_CHANNEL_RPORT_LISTENER:
537 case SSH_CHANNEL_OPENING:
538 case SSH_CHANNEL_CONNECTING:
539 case SSH_CHANNEL_ZOMBIE:
540 continue;
541 case SSH_CHANNEL_LARVAL:
542 case SSH_CHANNEL_AUTH_SOCKET:
543 case SSH_CHANNEL_OPEN:
544 case SSH_CHANNEL_X11_OPEN:
545 return i;
546 case SSH_CHANNEL_INPUT_DRAINING:
547 case SSH_CHANNEL_OUTPUT_DRAINING:
548 if (!compat13)
549 fatal("cannot happen: OUT_DRAIN");
550 return i;
551 default:
552 fatal("channel_find_open: bad channel type %d", c->type);
553 /* NOTREACHED */
554 }
555 }
556 return -1;
557}
558
559
560/*
561 * Returns a message describing the currently open forwarded connections,
562 * suitable for sending to the client. The message contains crlf pairs for
563 * newlines.
564 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000565char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000566channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000567{
568 Buffer buffer;
569 Channel *c;
570 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000571 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000572
573 buffer_init(&buffer);
574 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
575 buffer_append(&buffer, buf, strlen(buf));
576 for (i = 0; i < channels_alloc; i++) {
577 c = channels[i];
578 if (c == NULL)
579 continue;
580 switch (c->type) {
581 case SSH_CHANNEL_X11_LISTENER:
582 case SSH_CHANNEL_PORT_LISTENER:
583 case SSH_CHANNEL_RPORT_LISTENER:
584 case SSH_CHANNEL_CLOSED:
585 case SSH_CHANNEL_AUTH_SOCKET:
586 case SSH_CHANNEL_ZOMBIE:
587 continue;
588 case SSH_CHANNEL_LARVAL:
589 case SSH_CHANNEL_OPENING:
590 case SSH_CHANNEL_CONNECTING:
591 case SSH_CHANNEL_DYNAMIC:
592 case SSH_CHANNEL_OPEN:
593 case SSH_CHANNEL_X11_OPEN:
594 case SSH_CHANNEL_INPUT_DRAINING:
595 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000596 snprintf(buf, sizeof buf,
597 " #%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 +0000598 c->self, c->remote_name,
599 c->type, c->remote_id,
600 c->istate, buffer_len(&c->input),
601 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000602 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000603 buffer_append(&buffer, buf, strlen(buf));
604 continue;
605 default:
606 fatal("channel_open_message: bad channel type %d", c->type);
607 /* NOTREACHED */
608 }
609 }
610 buffer_append(&buffer, "\0", 1);
611 cp = xstrdup(buffer_ptr(&buffer));
612 buffer_free(&buffer);
613 return cp;
614}
615
616void
617channel_send_open(int id)
618{
619 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000620
Ben Lindstrome9c99912001-06-09 00:41:05 +0000621 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000622 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000623 return;
624 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000625 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000626 packet_start(SSH2_MSG_CHANNEL_OPEN);
627 packet_put_cstring(c->ctype);
628 packet_put_int(c->self);
629 packet_put_int(c->local_window);
630 packet_put_int(c->local_maxpacket);
631 packet_send();
632}
633
634void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000635channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000636{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000637 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000638
Ben Lindstrome9c99912001-06-09 00:41:05 +0000639 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000640 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000641 return;
642 }
Damien Miller0e220db2004-06-15 10:34:08 +1000643 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644 packet_start(SSH2_MSG_CHANNEL_REQUEST);
645 packet_put_int(c->remote_id);
646 packet_put_cstring(service);
647 packet_put_char(wantconfirm);
648}
Damien Miller4f7becb2006-03-26 14:10:14 +1100649
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650void
Damien Miller0e220db2004-06-15 10:34:08 +1000651channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000652{
653 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000654
Ben Lindstrome9c99912001-06-09 00:41:05 +0000655 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000656 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000657 return;
658 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100659 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000660 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661}
Damien Miller4f7becb2006-03-26 14:10:14 +1100662
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663void
Damien Miller39eda6e2005-11-05 14:52:50 +1100664channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000665{
Damien Millerd47c62a2005-12-13 19:33:57 +1100666 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000667
Ben Lindstrome9c99912001-06-09 00:41:05 +0000668 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000669 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000670 return;
671 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000672 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100673 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000674}
Damien Miller4f7becb2006-03-26 14:10:14 +1100675
Ben Lindstrome9c99912001-06-09 00:41:05 +0000676void
677channel_cancel_cleanup(int id)
678{
Damien Millerd47c62a2005-12-13 19:33:57 +1100679 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000680
Ben Lindstrome9c99912001-06-09 00:41:05 +0000681 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000682 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000683 return;
684 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000685 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100686 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000687}
Damien Miller4f7becb2006-03-26 14:10:14 +1100688
Ben Lindstrome9c99912001-06-09 00:41:05 +0000689void
Damien Miller077b2382005-12-31 16:22:32 +1100690channel_register_filter(int id, channel_infilter_fn *ifn,
691 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000692{
693 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000694
Ben Lindstrome9c99912001-06-09 00:41:05 +0000695 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000696 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000697 return;
698 }
Damien Miller077b2382005-12-31 16:22:32 +1100699 c->input_filter = ifn;
700 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000701}
702
703void
704channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100705 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000706{
707 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000708
Ben Lindstrome9c99912001-06-09 00:41:05 +0000709 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
710 fatal("channel_activate for non-larval channel %d.", id);
711 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
712 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100713 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000714 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
715 packet_put_int(c->remote_id);
716 packet_put_int(c->local_window);
717 packet_send();
718}
719
Damien Miller5428f641999-11-25 11:54:57 +1100720/*
Damien Millerb38eff82000-04-01 11:09:21 +1000721 * 'channel_pre*' are called just before select() to add any bits relevant to
722 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100723 */
Damien Millerb38eff82000-04-01 11:09:21 +1000724/*
725 * 'channel_post*': perform any appropriate operations for channels which
726 * have events pending.
727 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100728typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000729chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
730chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
731
Ben Lindstrombba81212001-06-25 05:01:22 +0000732static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100733channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000734{
735 FD_SET(c->sock, readset);
736}
737
Ben Lindstrombba81212001-06-25 05:01:22 +0000738static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100739channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000740{
741 debug3("channel %d: waiting for connection", c->self);
742 FD_SET(c->sock, writeset);
743}
744
Ben Lindstrombba81212001-06-25 05:01:22 +0000745static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100746channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000747{
748 if (buffer_len(&c->input) < packet_get_maxsize())
749 FD_SET(c->sock, readset);
750 if (buffer_len(&c->output) > 0)
751 FD_SET(c->sock, writeset);
752}
753
Ben Lindstrombba81212001-06-25 05:01:22 +0000754static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100755channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000756{
Damien Millerde6987c2002-01-22 23:20:40 +1100757 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000758
Damien Miller33b13562000-04-04 14:38:59 +1000759 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100760 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000761 buffer_len(&c->input) < limit &&
762 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000763 FD_SET(c->rfd, readset);
764 if (c->ostate == CHAN_OUTPUT_OPEN ||
765 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
766 if (buffer_len(&c->output) > 0) {
767 FD_SET(c->wfd, writeset);
768 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000769 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000770 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
771 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000772 else
773 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000774 }
775 }
776 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100777 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000778 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
779 buffer_len(&c->extended) > 0)
780 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000781 else if (!(c->flags & CHAN_EOF_SENT) &&
782 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000783 buffer_len(&c->extended) < c->remote_window)
784 FD_SET(c->efd, readset);
785 }
Damien Miller0e220db2004-06-15 10:34:08 +1000786 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000787 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000788 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
789 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000790}
791
Ben Lindstrombba81212001-06-25 05:01:22 +0000792static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100793channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000794{
795 if (buffer_len(&c->input) == 0) {
796 packet_start(SSH_MSG_CHANNEL_CLOSE);
797 packet_put_int(c->remote_id);
798 packet_send();
799 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000800 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000801 }
802}
803
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100805channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000806{
807 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000808 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000809 else
Damien Millerb38eff82000-04-01 11:09:21 +1000810 FD_SET(c->sock, writeset);
811}
812
813/*
814 * This is a special state for X11 authentication spoofing. An opened X11
815 * connection (when authentication spoofing is being done) remains in this
816 * state until the first packet has been completely read. The authentication
817 * data in that packet is then substituted by the real data if it matches the
818 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000819 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000820 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000821 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000822static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000823x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000824{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000825 u_char *ucp;
826 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000827
828 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000829 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000830 return 0;
831
832 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100833 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000834 if (ucp[0] == 0x42) { /* Byte order MSB first. */
835 proto_len = 256 * ucp[6] + ucp[7];
836 data_len = 256 * ucp[8] + ucp[9];
837 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
838 proto_len = ucp[6] + 256 * ucp[7];
839 data_len = ucp[8] + 256 * ucp[9];
840 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000841 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100842 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000843 return -1;
844 }
845
846 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000847 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000848 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
849 return 0;
850
851 /* Check if authentication protocol matches. */
852 if (proto_len != strlen(x11_saved_proto) ||
853 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000854 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000855 return -1;
856 }
857 /* Check if authentication data matches our fake data. */
858 if (data_len != x11_fake_data_len ||
859 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
860 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000861 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000862 return -1;
863 }
864 /* Check fake data length */
865 if (x11_fake_data_len != x11_saved_data_len) {
866 error("X11 fake_data_len %d != saved_data_len %d",
867 x11_fake_data_len, x11_saved_data_len);
868 return -1;
869 }
870 /*
871 * Received authentication protocol and data match
872 * our fake data. Substitute the fake data with real
873 * data.
874 */
875 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
876 x11_saved_data, x11_saved_data_len);
877 return 1;
878}
879
Ben Lindstrombba81212001-06-25 05:01:22 +0000880static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100881channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000882{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000883 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000884
Damien Millerb38eff82000-04-01 11:09:21 +1000885 if (ret == 1) {
886 /* Start normal processing for the channel. */
887 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000888 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000889 } else if (ret == -1) {
890 /*
891 * We have received an X11 connection that has bad
892 * authentication information.
893 */
Damien Miller996acd22003-04-09 20:59:48 +1000894 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000895 buffer_clear(&c->input);
896 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000897 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000898 c->sock = -1;
899 c->type = SSH_CHANNEL_CLOSED;
900 packet_start(SSH_MSG_CHANNEL_CLOSE);
901 packet_put_int(c->remote_id);
902 packet_send();
903 }
904}
905
Ben Lindstrombba81212001-06-25 05:01:22 +0000906static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100907channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000908{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000909 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000910
911 /* c->force_drain = 1; */
912
Damien Millerb38eff82000-04-01 11:09:21 +1000913 if (ret == 1) {
914 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100915 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000916 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000917 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000918 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100919 chan_read_failed(c);
920 buffer_clear(&c->input);
921 chan_ibuf_empty(c);
922 buffer_clear(&c->output);
923 /* for proto v1, the peer will send an IEOF */
924 if (compat20)
925 chan_write_failed(c);
926 else
927 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000928 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000929 }
930}
931
Ben Lindstromb3921512001-04-11 15:57:50 +0000932/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000933static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100934channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000935{
Damien Millera10f5612002-09-12 09:49:15 +1000936 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000937 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100938 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000939 struct {
940 u_int8_t version;
941 u_int8_t command;
942 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000943 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000944 } s4_req, s4_rsp;
945
Ben Lindstromb3921512001-04-11 15:57:50 +0000946 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000947
948 have = buffer_len(&c->input);
949 len = sizeof(s4_req);
950 if (have < len)
951 return 0;
952 p = buffer_ptr(&c->input);
953 for (found = 0, i = len; i < have; i++) {
954 if (p[i] == '\0') {
955 found = 1;
956 break;
957 }
958 if (i > 1024) {
959 /* the peer is probably sending garbage */
960 debug("channel %d: decode socks4: too long",
961 c->self);
962 return -1;
963 }
964 }
965 if (!found)
966 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000967 buffer_get(&c->input, (char *)&s4_req.version, 1);
968 buffer_get(&c->input, (char *)&s4_req.command, 1);
969 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000970 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000971 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000972 p = buffer_ptr(&c->input);
973 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000974 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000975 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000976 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000977 c->self, len, have);
978 strlcpy(username, p, sizeof(username));
979 buffer_consume(&c->input, len);
980 buffer_consume(&c->input, 1); /* trailing '\0' */
981
Ben Lindstromb3921512001-04-11 15:57:50 +0000982 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000983 strlcpy(c->path, host, sizeof(c->path));
984 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100985
Damien Millerfbdeece2003-09-02 22:52:31 +1000986 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000987 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000988
Ben Lindstromb3921512001-04-11 15:57:50 +0000989 if (s4_req.command != 1) {
990 debug("channel %d: cannot handle: socks4 cn %d",
991 c->self, s4_req.command);
992 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000993 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000994 s4_rsp.version = 0; /* vn: 0 for reply */
995 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000996 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000997 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +1100998 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000999 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001000}
1001
Darren Tucker46471c92003-07-03 13:55:19 +10001002/* try to decode a socks5 header */
1003#define SSH_SOCKS5_AUTHDONE 0x1000
1004#define SSH_SOCKS5_NOAUTH 0x00
1005#define SSH_SOCKS5_IPV4 0x01
1006#define SSH_SOCKS5_DOMAIN 0x03
1007#define SSH_SOCKS5_IPV6 0x04
1008#define SSH_SOCKS5_CONNECT 0x01
1009#define SSH_SOCKS5_SUCCESS 0x00
1010
1011static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001012channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001013{
1014 struct {
1015 u_int8_t version;
1016 u_int8_t command;
1017 u_int8_t reserved;
1018 u_int8_t atyp;
1019 } s5_req, s5_rsp;
1020 u_int16_t dest_port;
1021 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001022 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001023
1024 debug2("channel %d: decode socks5", c->self);
1025 p = buffer_ptr(&c->input);
1026 if (p[0] != 0x05)
1027 return -1;
1028 have = buffer_len(&c->input);
1029 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1030 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001031 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001032 return 0;
1033 nmethods = p[1];
1034 if (have < nmethods + 2)
1035 return 0;
1036 /* look for method: "NO AUTHENTICATION REQUIRED" */
1037 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1038 if (p[i] == SSH_SOCKS5_NOAUTH ) {
1039 found = 1;
1040 break;
1041 }
1042 }
1043 if (!found) {
1044 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1045 c->self);
1046 return -1;
1047 }
1048 buffer_consume(&c->input, nmethods + 2);
1049 buffer_put_char(&c->output, 0x05); /* version */
1050 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1051 FD_SET(c->sock, writeset);
1052 c->flags |= SSH_SOCKS5_AUTHDONE;
1053 debug2("channel %d: socks5 auth done", c->self);
1054 return 0; /* need more */
1055 }
1056 debug2("channel %d: socks5 post auth", c->self);
1057 if (have < sizeof(s5_req)+1)
1058 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001059 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001060 if (s5_req.version != 0x05 ||
1061 s5_req.command != SSH_SOCKS5_CONNECT ||
1062 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001063 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001064 return -1;
1065 }
Darren Tucker47eede72005-03-14 23:08:12 +11001066 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001067 case SSH_SOCKS5_IPV4:
1068 addrlen = 4;
1069 af = AF_INET;
1070 break;
1071 case SSH_SOCKS5_DOMAIN:
1072 addrlen = p[sizeof(s5_req)];
1073 af = -1;
1074 break;
1075 case SSH_SOCKS5_IPV6:
1076 addrlen = 16;
1077 af = AF_INET6;
1078 break;
1079 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001080 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001081 return -1;
1082 }
Damien Miller0f077072006-07-10 22:21:02 +10001083 need = sizeof(s5_req) + addrlen + 2;
1084 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1085 need++;
1086 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001087 return 0;
1088 buffer_consume(&c->input, sizeof(s5_req));
1089 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1090 buffer_consume(&c->input, 1); /* host string length */
1091 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1092 buffer_get(&c->input, (char *)&dest_port, 2);
1093 dest_addr[addrlen] = '\0';
1094 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001095 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001096 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1097 return -1;
1098 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001099
Damien Millerfbdeece2003-09-02 22:52:31 +10001100 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001101 c->self, c->path, c->host_port, s5_req.command);
1102
1103 s5_rsp.version = 0x05;
1104 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1105 s5_rsp.reserved = 0; /* ignored */
1106 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1107 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1108 dest_port = 0; /* ignored */
1109
Damien Millera0fdce92006-03-26 14:28:50 +11001110 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1111 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1112 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001113 return 1;
1114}
1115
Ben Lindstromb3921512001-04-11 15:57:50 +00001116/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001117static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001118channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001119{
1120 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001121 u_int have;
1122 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001123
1124 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001125 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001126 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001127 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001128 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001129 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001130 /* need more */
1131 FD_SET(c->sock, readset);
1132 return;
1133 }
1134 /* try to guess the protocol */
1135 p = buffer_ptr(&c->input);
1136 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001137 case 0x04:
1138 ret = channel_decode_socks4(c, readset, writeset);
1139 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001140 case 0x05:
1141 ret = channel_decode_socks5(c, readset, writeset);
1142 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001143 default:
1144 ret = -1;
1145 break;
1146 }
1147 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001148 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001149 } else if (ret == 0) {
1150 debug2("channel %d: pre_dynamic: need more", c->self);
1151 /* need more */
1152 FD_SET(c->sock, readset);
1153 } else {
1154 /* switch to the next state */
1155 c->type = SSH_CHANNEL_OPENING;
1156 port_open_helper(c, "direct-tcpip");
1157 }
1158}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001159
Damien Millerb38eff82000-04-01 11:09:21 +10001160/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001161static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001162channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001163{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001164 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001165 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001166 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001167 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001168 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001169 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001170
1171 if (FD_ISSET(c->sock, readset)) {
1172 debug("X11 connection requested.");
1173 addrlen = sizeof(addr);
1174 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001175 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001176 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001177 channel_close_fd(&c->sock);
1178 chan_mark_dead(c);
1179 }
Damien Millerb38eff82000-04-01 11:09:21 +10001180 if (newsock < 0) {
1181 error("accept: %.100s", strerror(errno));
1182 return;
1183 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001184 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001185 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001186 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001187 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001188 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001189
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001190 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001191 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001192 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001193 if (compat20) {
1194 packet_start(SSH2_MSG_CHANNEL_OPEN);
1195 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001196 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001197 packet_put_int(nc->local_window_max);
1198 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001199 /* originator ipaddr and port */
1200 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001201 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001202 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001203 } else {
1204 packet_put_int(remote_port);
1205 }
Damien Millerbd483e72000-04-30 10:00:53 +10001206 packet_send();
1207 } else {
1208 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001209 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001210 if (packet_get_protocol_flags() &
1211 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001212 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001213 packet_send();
1214 }
Damien Millerd83ff352001-01-30 09:19:34 +11001215 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001216 }
1217}
1218
Ben Lindstrombba81212001-06-25 05:01:22 +00001219static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001220port_open_helper(Channel *c, char *rtype)
1221{
1222 int direct;
1223 char buf[1024];
1224 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001225 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001226
1227 direct = (strcmp(rtype, "direct-tcpip") == 0);
1228
1229 snprintf(buf, sizeof buf,
1230 "%s: listening port %d for %.100s port %d, "
1231 "connect from %.200s port %d",
1232 rtype, c->listening_port, c->path, c->host_port,
1233 remote_ipaddr, remote_port);
1234
1235 xfree(c->remote_name);
1236 c->remote_name = xstrdup(buf);
1237
1238 if (compat20) {
1239 packet_start(SSH2_MSG_CHANNEL_OPEN);
1240 packet_put_cstring(rtype);
1241 packet_put_int(c->self);
1242 packet_put_int(c->local_window_max);
1243 packet_put_int(c->local_maxpacket);
1244 if (direct) {
1245 /* target host, port */
1246 packet_put_cstring(c->path);
1247 packet_put_int(c->host_port);
1248 } else {
1249 /* listen address, port */
1250 packet_put_cstring(c->path);
1251 packet_put_int(c->listening_port);
1252 }
1253 /* originator host and port */
1254 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001255 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001256 packet_send();
1257 } else {
1258 packet_start(SSH_MSG_PORT_OPEN);
1259 packet_put_int(c->self);
1260 packet_put_cstring(c->path);
1261 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001262 if (packet_get_protocol_flags() &
1263 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001264 packet_put_cstring(c->remote_name);
1265 packet_send();
1266 }
1267 xfree(remote_ipaddr);
1268}
1269
Damien Miller5e7fd072005-11-05 14:53:39 +11001270static void
1271channel_set_reuseaddr(int fd)
1272{
1273 int on = 1;
1274
1275 /*
1276 * Set socket options.
1277 * Allow local port reuse in TIME_WAIT.
1278 */
1279 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1280 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1281}
1282
Damien Millerb38eff82000-04-01 11:09:21 +10001283/*
1284 * This socket is listening for connections to a forwarded TCP/IP port.
1285 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001286static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001287channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001288{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001289 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001290 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001291 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001292 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001293 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001294
Damien Millerb38eff82000-04-01 11:09:21 +10001295 if (FD_ISSET(c->sock, readset)) {
1296 debug("Connection to port %d forwarding "
1297 "to %.100s port %d requested.",
1298 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001299
Damien Miller4623a752001-10-10 15:03:58 +10001300 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1301 nextstate = SSH_CHANNEL_OPENING;
1302 rtype = "forwarded-tcpip";
1303 } else {
1304 if (c->host_port == 0) {
1305 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001306 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001307 } else {
1308 nextstate = SSH_CHANNEL_OPENING;
1309 rtype = "direct-tcpip";
1310 }
1311 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001312
Damien Millerb38eff82000-04-01 11:09:21 +10001313 addrlen = sizeof(addr);
1314 newsock = accept(c->sock, &addr, &addrlen);
1315 if (newsock < 0) {
1316 error("accept: %.100s", strerror(errno));
1317 return;
1318 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001319 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001320 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1321 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001322 nc->listening_port = c->listening_port;
1323 nc->host_port = c->host_port;
1324 strlcpy(nc->path, c->path, sizeof(nc->path));
1325
Damien Miller4623a752001-10-10 15:03:58 +10001326 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1327 /*
1328 * do not call the channel_post handler until
1329 * this flag has been reset by a pre-handler.
1330 * otherwise the FD_ISSET calls might overflow
1331 */
1332 nc->delayed = 1;
1333 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001334 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001335 }
Damien Millerb38eff82000-04-01 11:09:21 +10001336 }
1337}
1338
1339/*
1340 * This is the authentication agent socket listening for connections from
1341 * clients.
1342 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001343static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001344channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001345{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001346 Channel *nc;
1347 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001348 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001349 socklen_t addrlen;
1350
1351 if (FD_ISSET(c->sock, readset)) {
1352 addrlen = sizeof(addr);
1353 newsock = accept(c->sock, &addr, &addrlen);
1354 if (newsock < 0) {
1355 error("accept from auth socket: %.100s", strerror(errno));
1356 return;
1357 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001358 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001359 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1360 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001361 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001362 if (compat20) {
1363 packet_start(SSH2_MSG_CHANNEL_OPEN);
1364 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001365 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001366 packet_put_int(c->local_window_max);
1367 packet_put_int(c->local_maxpacket);
1368 } else {
1369 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001370 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001371 }
Damien Millerb38eff82000-04-01 11:09:21 +10001372 packet_send();
1373 }
1374}
1375
Ben Lindstrombba81212001-06-25 05:01:22 +00001376static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001377channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001378{
Ben Lindstrom69128662001-05-08 20:07:39 +00001379 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001380 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001381
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001382 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001383 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001384 err = errno;
1385 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001386 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001387 if (err == 0) {
1388 debug("channel %d: connected", c->self);
1389 c->type = SSH_CHANNEL_OPEN;
1390 if (compat20) {
1391 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1392 packet_put_int(c->remote_id);
1393 packet_put_int(c->self);
1394 packet_put_int(c->local_window);
1395 packet_put_int(c->local_maxpacket);
1396 } else {
1397 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1398 packet_put_int(c->remote_id);
1399 packet_put_int(c->self);
1400 }
1401 } else {
1402 debug("channel %d: not connected: %s",
1403 c->self, strerror(err));
1404 if (compat20) {
1405 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1406 packet_put_int(c->remote_id);
1407 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1408 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1409 packet_put_cstring(strerror(err));
1410 packet_put_cstring("");
1411 }
1412 } else {
1413 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1414 packet_put_int(c->remote_id);
1415 }
1416 chan_mark_dead(c);
1417 }
1418 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001419 }
1420}
1421
Ben Lindstrombba81212001-06-25 05:01:22 +00001422static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001423channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001424{
Darren Tucker11327cc2005-03-14 23:22:25 +11001425 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001426 int len;
1427
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001428 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001429 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001430 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001431 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001432 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1433 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001434#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001435 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001436#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001437 if ((!c->isatty && len <= 0) ||
1438 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001439#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001440 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001441 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001442 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001443 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001444 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001445 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001446 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001447 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001448 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001449 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001450 } else {
1451 chan_read_failed(c);
1452 }
1453 return -1;
1454 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001455 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001456 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001457 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001458 chan_read_failed(c);
1459 }
Damien Millerd27b9472005-12-13 19:29:02 +11001460 } else if (c->datagram) {
1461 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001462 } else {
1463 buffer_append(&c->input, buf, len);
1464 }
Damien Millerb38eff82000-04-01 11:09:21 +10001465 }
1466 return 1;
1467}
Damien Miller4f7becb2006-03-26 14:10:14 +11001468
Ben Lindstrombba81212001-06-25 05:01:22 +00001469static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001470channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001471{
Ben Lindstrome229b252001-03-05 06:28:06 +00001472 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001473 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001474 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001475 int len;
1476
1477 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001478 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001479 FD_ISSET(c->wfd, writeset) &&
1480 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001481 if (c->output_filter != NULL) {
1482 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1483 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001484 if (c->type != SSH_CHANNEL_OPEN)
1485 chan_mark_dead(c);
1486 else
1487 chan_write_failed(c);
1488 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001489 }
1490 } else if (c->datagram) {
1491 buf = data = buffer_get_string(&c->output, &dlen);
1492 } else {
1493 buf = data = buffer_ptr(&c->output);
1494 dlen = buffer_len(&c->output);
1495 }
1496
Damien Millerd27b9472005-12-13 19:29:02 +11001497 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001498 /* ignore truncated writes, datagrams might get lost */
1499 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001500 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001501 xfree(data);
1502 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1503 return 1;
1504 if (len <= 0) {
1505 if (c->type != SSH_CHANNEL_OPEN)
1506 chan_mark_dead(c);
1507 else
1508 chan_write_failed(c);
1509 return -1;
1510 }
1511 return 1;
1512 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001513#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001514 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001515 if (compat20 && c->wfd_isatty)
1516 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001517#endif
Damien Miller077b2382005-12-31 16:22:32 +11001518
1519 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001520 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1521 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001522 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001523 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001524 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001525 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001526 return -1;
1527 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001528 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001529 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001530 c->type = SSH_CHANNEL_INPUT_DRAINING;
1531 } else {
1532 chan_write_failed(c);
1533 }
1534 return -1;
1535 }
Damien Miller077b2382005-12-31 16:22:32 +11001536 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001537 if (tcgetattr(c->wfd, &tio) == 0 &&
1538 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1539 /*
1540 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001541 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001542 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001543 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001544 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001545 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001546 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001547 }
1548 }
Damien Millerb38eff82000-04-01 11:09:21 +10001549 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001550 if (compat20 && len > 0) {
1551 c->local_consumed += len;
1552 }
1553 }
1554 return 1;
1555}
Damien Miller4f7becb2006-03-26 14:10:14 +11001556
Ben Lindstrombba81212001-06-25 05:01:22 +00001557static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001558channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001559{
Darren Tucker11327cc2005-03-14 23:22:25 +11001560 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001561 int len;
1562
Damien Miller1383bd82000-04-06 12:32:37 +10001563/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001564 if (c->efd != -1) {
1565 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1566 FD_ISSET(c->efd, writeset) &&
1567 buffer_len(&c->extended) > 0) {
1568 len = write(c->efd, buffer_ptr(&c->extended),
1569 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001570 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001571 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001572 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1573 return 1;
1574 if (len <= 0) {
1575 debug2("channel %d: closing write-efd %d",
1576 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001577 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001578 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001579 buffer_consume(&c->extended, len);
1580 c->local_consumed += len;
1581 }
1582 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1583 FD_ISSET(c->efd, readset)) {
1584 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001585 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001586 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001587 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1588 return 1;
1589 if (len <= 0) {
1590 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001591 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001592 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001593 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001594 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001595 }
Damien Miller33b13562000-04-04 14:38:59 +10001596 }
1597 }
1598 return 1;
1599}
Damien Miller4f7becb2006-03-26 14:10:14 +11001600
Ben Lindstrombba81212001-06-25 05:01:22 +00001601static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001602channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001603{
1604 char buf[16];
1605 int len;
1606
1607 /* Monitor control fd to detect if the slave client exits */
1608 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1609 len = read(c->ctl_fd, buf, sizeof(buf));
1610 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1611 return 1;
1612 if (len <= 0) {
1613 debug2("channel %d: ctl read<=0", c->self);
1614 if (c->type != SSH_CHANNEL_OPEN) {
1615 debug2("channel %d: not open", c->self);
1616 chan_mark_dead(c);
1617 return -1;
1618 } else {
1619 chan_read_failed(c);
1620 chan_write_failed(c);
1621 }
1622 return -1;
1623 } else
1624 fatal("%s: unexpected data on ctl fd", __func__);
1625 }
1626 return 1;
1627}
Damien Miller4f7becb2006-03-26 14:10:14 +11001628
Damien Miller0e220db2004-06-15 10:34:08 +10001629static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001630channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001631{
Ben Lindstromb3921512001-04-11 15:57:50 +00001632 if (c->type == SSH_CHANNEL_OPEN &&
1633 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001634 c->local_window < c->local_window_max/2 &&
1635 c->local_consumed > 0) {
1636 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1637 packet_put_int(c->remote_id);
1638 packet_put_int(c->local_consumed);
1639 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001640 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001641 c->self, c->local_window,
1642 c->local_consumed);
1643 c->local_window += c->local_consumed;
1644 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001645 }
1646 return 1;
1647}
1648
Ben Lindstrombba81212001-06-25 05:01:22 +00001649static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001650channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001651{
Damien Miller4623a752001-10-10 15:03:58 +10001652 if (c->delayed)
1653 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001654 channel_handle_rfd(c, readset, writeset);
1655 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001656 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001657 return;
Damien Miller33b13562000-04-04 14:38:59 +10001658 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001659 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001660 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001661}
1662
Ben Lindstrombba81212001-06-25 05:01:22 +00001663static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001664channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001665{
1666 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001667
Damien Millerb38eff82000-04-01 11:09:21 +10001668 /* Send buffered output data to the socket. */
1669 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1670 len = write(c->sock, buffer_ptr(&c->output),
1671 buffer_len(&c->output));
1672 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001673 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001674 else
1675 buffer_consume(&c->output, len);
1676 }
1677}
1678
Ben Lindstrombba81212001-06-25 05:01:22 +00001679static void
Damien Miller33b13562000-04-04 14:38:59 +10001680channel_handler_init_20(void)
1681{
Damien Millerde6987c2002-01-22 23:20:40 +11001682 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001683 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001684 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001685 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001686 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001687 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001688 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001689 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001690
Damien Millerde6987c2002-01-22 23:20:40 +11001691 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001692 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001693 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001694 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001695 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001696 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001697 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001698}
1699
Ben Lindstrombba81212001-06-25 05:01:22 +00001700static void
Damien Millerb38eff82000-04-01 11:09:21 +10001701channel_handler_init_13(void)
1702{
1703 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1704 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1705 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1706 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1707 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1708 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1709 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001710 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001711 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001712
Damien Millerde6987c2002-01-22 23:20:40 +11001713 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001714 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1715 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1716 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1717 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001718 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001719 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001720}
1721
Ben Lindstrombba81212001-06-25 05:01:22 +00001722static void
Damien Millerb38eff82000-04-01 11:09:21 +10001723channel_handler_init_15(void)
1724{
Damien Millerde6987c2002-01-22 23:20:40 +11001725 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001726 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001727 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1728 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1729 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001730 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001731 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001732
1733 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1734 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1735 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001736 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001737 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001738 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001739}
1740
Ben Lindstrombba81212001-06-25 05:01:22 +00001741static void
Damien Millerb38eff82000-04-01 11:09:21 +10001742channel_handler_init(void)
1743{
1744 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001745
Damien Miller9f0f5c62001-12-21 14:45:46 +11001746 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001747 channel_pre[i] = NULL;
1748 channel_post[i] = NULL;
1749 }
Damien Miller33b13562000-04-04 14:38:59 +10001750 if (compat20)
1751 channel_handler_init_20();
1752 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001753 channel_handler_init_13();
1754 else
1755 channel_handler_init_15();
1756}
1757
Damien Miller3ec27592001-10-12 11:35:04 +10001758/* gc dead channels */
1759static void
1760channel_garbage_collect(Channel *c)
1761{
1762 if (c == NULL)
1763 return;
1764 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001765 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001766 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001767 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001768 c->detach_user(c->self, NULL);
1769 /* if we still have a callback */
1770 if (c->detach_user != NULL)
1771 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001772 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001773 }
1774 if (!chan_is_dead(c, 1))
1775 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001776 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001777 channel_free(c);
1778}
1779
Ben Lindstrombba81212001-06-25 05:01:22 +00001780static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001781channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001782{
1783 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001784 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001785 Channel *c;
1786
1787 if (!did_init) {
1788 channel_handler_init();
1789 did_init = 1;
1790 }
1791 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001792 c = channels[i];
1793 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001794 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001795 if (ftab[c->type] != NULL)
1796 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001797 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001798 }
1799}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001800
Ben Lindstrome9c99912001-06-09 00:41:05 +00001801/*
1802 * Allocate/update select bitmasks and add any bits relevant to channels in
1803 * select bitmasks.
1804 */
Damien Miller4af51302000-04-16 11:18:38 +10001805void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001806channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001807 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001808{
Damien Miller36812092006-03-26 14:22:47 +11001809 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001810
1811 n = MAX(*maxfdp, channel_max_fd);
1812
Damien Miller36812092006-03-26 14:22:47 +11001813 nfdset = howmany(n+1, NFDBITS);
1814 /* Explicitly test here, because xrealloc isn't always called */
1815 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1816 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1817 sz = nfdset * sizeof(fd_mask);
1818
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001819 /* perhaps check sz < nalloc/2 and shrink? */
1820 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001821 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1822 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001823 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001824 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001825 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001826 memset(*readsetp, 0, sz);
1827 memset(*writesetp, 0, sz);
1828
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001829 if (!rekeying)
1830 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001831}
1832
Ben Lindstrome9c99912001-06-09 00:41:05 +00001833/*
1834 * After select, perform any appropriate operations for channels which have
1835 * events pending.
1836 */
Damien Miller4af51302000-04-16 11:18:38 +10001837void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001838channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001839{
Damien Millerb38eff82000-04-01 11:09:21 +10001840 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001841}
1842
Ben Lindstrome9c99912001-06-09 00:41:05 +00001843
Damien Miller5e953212001-01-30 09:14:00 +11001844/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001845void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001846channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001847{
Damien Millerb38eff82000-04-01 11:09:21 +10001848 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001849 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001850
Damien Miller95def091999-11-25 00:26:21 +11001851 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001852 c = channels[i];
1853 if (c == NULL)
1854 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001855
Ben Lindstrome9c99912001-06-09 00:41:05 +00001856 /*
1857 * We are only interested in channels that can have buffered
1858 * incoming data.
1859 */
Damien Miller34132e52000-01-14 15:45:46 +11001860 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001861 if (c->type != SSH_CHANNEL_OPEN &&
1862 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001863 continue;
1864 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001865 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001866 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001867 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001868 if (compat20 &&
1869 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001870 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001871 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001872 continue;
1873 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001874
Damien Miller95def091999-11-25 00:26:21 +11001875 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001876 if ((c->istate == CHAN_INPUT_OPEN ||
1877 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1878 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001879 if (c->datagram) {
1880 if (len > 0) {
1881 u_char *data;
1882 u_int dlen;
1883
1884 data = buffer_get_string(&c->input,
1885 &dlen);
1886 packet_start(SSH2_MSG_CHANNEL_DATA);
1887 packet_put_int(c->remote_id);
1888 packet_put_string(data, dlen);
1889 packet_send();
1890 c->remote_window -= dlen + 4;
1891 xfree(data);
1892 }
1893 continue;
1894 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001895 /*
1896 * Send some data for the other side over the secure
1897 * connection.
1898 */
Damien Miller33b13562000-04-04 14:38:59 +10001899 if (compat20) {
1900 if (len > c->remote_window)
1901 len = c->remote_window;
1902 if (len > c->remote_maxpacket)
1903 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001904 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001905 if (packet_is_interactive()) {
1906 if (len > 1024)
1907 len = 512;
1908 } else {
1909 /* Keep the packets at reasonable size. */
1910 if (len > packet_get_maxsize()/2)
1911 len = packet_get_maxsize()/2;
1912 }
Damien Miller95def091999-11-25 00:26:21 +11001913 }
Damien Millerb38eff82000-04-01 11:09:21 +10001914 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001915 packet_start(compat20 ?
1916 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001917 packet_put_int(c->remote_id);
1918 packet_put_string(buffer_ptr(&c->input), len);
1919 packet_send();
1920 buffer_consume(&c->input, len);
1921 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001922 }
1923 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001924 if (compat13)
1925 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001926 /*
1927 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001928 * tell peer, that we will not send more data: send IEOF.
1929 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001930 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001931 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001932 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1933 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001934 else
1935 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001936 }
Damien Miller33b13562000-04-04 14:38:59 +10001937 /* Send extended data, i.e. stderr */
1938 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001939 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001940 c->remote_window > 0 &&
1941 (len = buffer_len(&c->extended)) > 0 &&
1942 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001943 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001944 c->self, c->remote_window, buffer_len(&c->extended),
1945 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001946 if (len > c->remote_window)
1947 len = c->remote_window;
1948 if (len > c->remote_maxpacket)
1949 len = c->remote_maxpacket;
1950 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1951 packet_put_int(c->remote_id);
1952 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1953 packet_put_string(buffer_ptr(&c->extended), len);
1954 packet_send();
1955 buffer_consume(&c->extended, len);
1956 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001957 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001958 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001959 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001960}
1961
Ben Lindstrome9c99912001-06-09 00:41:05 +00001962
1963/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001964
1965/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001966void
Damien Miller630d6f42002-01-22 23:17:30 +11001967channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001968{
Damien Miller34132e52000-01-14 15:45:46 +11001969 int id;
Damien Miller95def091999-11-25 00:26:21 +11001970 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001971 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001972 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001973
Damien Miller95def091999-11-25 00:26:21 +11001974 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001975 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001976 c = channel_lookup(id);
1977 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001978 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001979
Damien Miller95def091999-11-25 00:26:21 +11001980 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001981 if (c->type != SSH_CHANNEL_OPEN &&
1982 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001983 return;
1984
Damien Miller95def091999-11-25 00:26:21 +11001985 /* Get the data. */
1986 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001987
Damien Millera04ad492004-01-21 11:02:09 +11001988 /*
1989 * Ignore data for protocol > 1.3 if output end is no longer open.
1990 * For protocol 2 the sending side is reducing its window as it sends
1991 * data, so we must 'fake' consumption of the data in order to ensure
1992 * that window updates are sent back. Otherwise the connection might
1993 * deadlock.
1994 */
1995 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
1996 if (compat20) {
1997 c->local_window -= data_len;
1998 c->local_consumed += data_len;
1999 }
2000 xfree(data);
2001 return;
2002 }
2003
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002004 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002005 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002006 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002007 c->self, data_len, c->local_maxpacket);
2008 }
2009 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002010 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002011 c->self, data_len, c->local_window);
2012 xfree(data);
2013 return;
2014 }
2015 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002016 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002017 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002018 if (c->datagram)
2019 buffer_put_string(&c->output, data, data_len);
2020 else
2021 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002022 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002023}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002024
Damien Millerd79b4242006-03-31 23:11:44 +11002025/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002026void
Damien Miller630d6f42002-01-22 23:17:30 +11002027channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002028{
2029 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002030 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002031 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002032 Channel *c;
2033
2034 /* Get the channel number and verify it. */
2035 id = packet_get_int();
2036 c = channel_lookup(id);
2037
2038 if (c == NULL)
2039 packet_disconnect("Received extended_data for bad channel %d.", id);
2040 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002041 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002042 return;
2043 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002044 if (c->flags & CHAN_EOF_RCVD) {
2045 if (datafellows & SSH_BUG_EXTEOF)
2046 debug("channel %d: accepting ext data after eof", id);
2047 else
2048 packet_disconnect("Received extended_data after EOF "
2049 "on channel %d.", id);
2050 }
Damien Miller33b13562000-04-04 14:38:59 +10002051 tcode = packet_get_int();
2052 if (c->efd == -1 ||
2053 c->extended_usage != CHAN_EXTENDED_WRITE ||
2054 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002055 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002056 return;
2057 }
2058 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002059 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002060 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002061 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002062 c->self, data_len, c->local_window);
2063 xfree(data);
2064 return;
2065 }
Damien Millerd3444942000-09-30 14:20:03 +11002066 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002067 c->local_window -= data_len;
2068 buffer_append(&c->extended, data, data_len);
2069 xfree(data);
2070}
2071
Damien Millerd79b4242006-03-31 23:11:44 +11002072/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002073void
Damien Miller630d6f42002-01-22 23:17:30 +11002074channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002075{
2076 int id;
2077 Channel *c;
2078
Damien Millerb38eff82000-04-01 11:09:21 +10002079 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002080 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002081 c = channel_lookup(id);
2082 if (c == NULL)
2083 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2084 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002085
2086 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002087 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002088 debug("channel %d: FORCE input drain", c->self);
2089 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002090 if (buffer_len(&c->input) == 0)
2091 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002092 }
2093
Damien Millerb38eff82000-04-01 11:09:21 +10002094}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002095
Damien Millerd79b4242006-03-31 23:11:44 +11002096/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002097void
Damien Miller630d6f42002-01-22 23:17:30 +11002098channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099{
Damien Millerb38eff82000-04-01 11:09:21 +10002100 int id;
2101 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002102
Damien Millerb38eff82000-04-01 11:09:21 +10002103 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002104 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002105 c = channel_lookup(id);
2106 if (c == NULL)
2107 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002108
2109 /*
2110 * Send a confirmation that we have closed the channel and no more
2111 * data is coming for it.
2112 */
Damien Miller95def091999-11-25 00:26:21 +11002113 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002114 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002115 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002116
Damien Miller5428f641999-11-25 11:54:57 +11002117 /*
2118 * If the channel is in closed state, we have sent a close request,
2119 * and the other side will eventually respond with a confirmation.
2120 * Thus, we cannot free the channel here, because then there would be
2121 * no-one to receive the confirmation. The channel gets freed when
2122 * the confirmation arrives.
2123 */
Damien Millerb38eff82000-04-01 11:09:21 +10002124 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002125 /*
2126 * Not a closed channel - mark it as draining, which will
2127 * cause it to be freed later.
2128 */
Damien Miller76765c02002-01-22 23:21:15 +11002129 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002130 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002131 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002132}
2133
Damien Millerb38eff82000-04-01 11:09:21 +10002134/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002135/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002136void
Damien Miller630d6f42002-01-22 23:17:30 +11002137channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002138{
Damien Millerb38eff82000-04-01 11:09:21 +10002139 int id = packet_get_int();
2140 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002141
Damien Miller48b03fc2002-01-22 23:11:40 +11002142 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002143 if (c == NULL)
2144 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2145 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002146}
2147
Damien Millerd79b4242006-03-31 23:11:44 +11002148/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002149void
Damien Miller630d6f42002-01-22 23:17:30 +11002150channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002151{
2152 int id = packet_get_int();
2153 Channel *c = channel_lookup(id);
2154
Damien Miller48b03fc2002-01-22 23:11:40 +11002155 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002156 if (c == NULL)
2157 packet_disconnect("Received close confirmation for "
2158 "out-of-range channel %d.", id);
2159 if (c->type != SSH_CHANNEL_CLOSED)
2160 packet_disconnect("Received close confirmation for "
2161 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002162 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002163}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002164
Damien Millerd79b4242006-03-31 23:11:44 +11002165/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002166void
Damien Miller630d6f42002-01-22 23:17:30 +11002167channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002168{
Damien Millerb38eff82000-04-01 11:09:21 +10002169 int id, remote_id;
2170 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171
Damien Millerb38eff82000-04-01 11:09:21 +10002172 id = packet_get_int();
2173 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002174
Damien Millerb38eff82000-04-01 11:09:21 +10002175 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2176 packet_disconnect("Received open confirmation for "
2177 "non-opening channel %d.", id);
2178 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002179 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002180 c->remote_id = remote_id;
2181 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002182
2183 if (compat20) {
2184 c->remote_window = packet_get_int();
2185 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002186 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002187 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002188 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002189 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002190 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002191 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002192 c->remote_window, c->remote_maxpacket);
2193 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002194 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002195}
2196
Ben Lindstrombba81212001-06-25 05:01:22 +00002197static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002198reason2txt(int reason)
2199{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002200 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002201 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2202 return "administratively prohibited";
2203 case SSH2_OPEN_CONNECT_FAILED:
2204 return "connect failed";
2205 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2206 return "unknown channel type";
2207 case SSH2_OPEN_RESOURCE_SHORTAGE:
2208 return "resource shortage";
2209 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002210 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002211}
2212
Damien Millerd79b4242006-03-31 23:11:44 +11002213/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002214void
Damien Miller630d6f42002-01-22 23:17:30 +11002215channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002216{
Kevin Steves12057502001-02-05 14:54:34 +00002217 int id, reason;
2218 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002219 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002220
Damien Millerb38eff82000-04-01 11:09:21 +10002221 id = packet_get_int();
2222 c = channel_lookup(id);
2223
2224 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2225 packet_disconnect("Received open failure for "
2226 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002227 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002228 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002229 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002230 msg = packet_get_string(NULL);
2231 lang = packet_get_string(NULL);
2232 }
Damien Miller996acd22003-04-09 20:59:48 +10002233 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002234 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002235 if (msg != NULL)
2236 xfree(msg);
2237 if (lang != NULL)
2238 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002239 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002240 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002241 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002242 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002243}
2244
Damien Millerd79b4242006-03-31 23:11:44 +11002245/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002246void
Damien Miller630d6f42002-01-22 23:17:30 +11002247channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002248{
2249 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002250 int id;
2251 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002252
2253 if (!compat20)
2254 return;
2255
2256 /* Get the channel number and verify it. */
2257 id = packet_get_int();
2258 c = channel_lookup(id);
2259
Damien Millerd47c62a2005-12-13 19:33:57 +11002260 if (c == NULL) {
2261 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002262 return;
2263 }
2264 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002265 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002266 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002267 c->remote_window += adjust;
2268}
2269
Damien Millerd79b4242006-03-31 23:11:44 +11002270/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002271void
Damien Miller630d6f42002-01-22 23:17:30 +11002272channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002273{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002274 Channel *c = NULL;
2275 u_short host_port;
2276 char *host, *originator_string;
2277 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002278
Ben Lindstrome9c99912001-06-09 00:41:05 +00002279 remote_id = packet_get_int();
2280 host = packet_get_string(NULL);
2281 host_port = packet_get_int();
2282
2283 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2284 originator_string = packet_get_string(NULL);
2285 } else {
2286 originator_string = xstrdup("unknown (remote did not supply name)");
2287 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002288 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002289 sock = channel_connect_to(host, host_port);
2290 if (sock != -1) {
2291 c = channel_new("connected socket",
2292 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2293 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002294 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002295 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002296 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002297 if (c == NULL) {
2298 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2299 packet_put_int(remote_id);
2300 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002301 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002302 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002303}
2304
2305
Ben Lindstrome9c99912001-06-09 00:41:05 +00002306/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002307
Ben Lindstrom908afed2001-10-03 17:34:59 +00002308void
2309channel_set_af(int af)
2310{
2311 IPv4or6 = af;
2312}
2313
Damien Millerb16461c2002-01-22 23:29:22 +11002314static int
2315channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2316 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002317{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002318 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002319 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002320 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002321 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002322 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002323
Damien Millerb16461c2002-01-22 23:29:22 +11002324 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2325 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002326 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002327
Damien Millerb16461c2002-01-22 23:29:22 +11002328 if (host == NULL) {
2329 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002330 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002331 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002332 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002333 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002334 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002336
Damien Miller5428f641999-11-25 11:54:57 +11002337 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002338 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002339 * specified address or wildcard. On the client, a specified bind
2340 * address will always override gateway_ports. On the server, a
2341 * gateway_ports of 1 (``yes'') will override the client's
2342 * specification and force a wildcard bind, whereas a value of 2
2343 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002344 * asked for.
2345 *
2346 * Special-case listen_addrs are:
2347 *
2348 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2349 * "" (empty string), "*" -> wildcard v4/v6
2350 * "localhost" -> loopback v4/v6
2351 */
2352 addr = NULL;
2353 if (listen_addr == NULL) {
2354 /* No address specified: default to gateway_ports setting */
2355 if (gateway_ports)
2356 wildcard = 1;
2357 } else if (gateway_ports || is_client) {
2358 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2359 strcmp(listen_addr, "0.0.0.0") == 0) ||
2360 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2361 (!is_client && gateway_ports == 1))
2362 wildcard = 1;
2363 else if (strcmp(listen_addr, "localhost") != 0)
2364 addr = listen_addr;
2365 }
2366
2367 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2368 type, wildcard, (addr == NULL) ? "NULL" : addr);
2369
2370 /*
Damien Miller34132e52000-01-14 15:45:46 +11002371 * getaddrinfo returns a loopback address if the hostname is
2372 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002373 */
Damien Miller34132e52000-01-14 15:45:46 +11002374 memset(&hints, 0, sizeof(hints));
2375 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002376 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002377 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002378 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002379 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2380 if (addr == NULL) {
2381 /* This really shouldn't happen */
2382 packet_disconnect("getaddrinfo: fatal error: %s",
2383 gai_strerror(r));
2384 } else {
Damien Millera7270302005-07-06 09:36:05 +10002385 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002386 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2387 }
Damien Millera7270302005-07-06 09:36:05 +10002388 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002389 }
Damien Miller5428f641999-11-25 11:54:57 +11002390
Damien Miller34132e52000-01-14 15:45:46 +11002391 for (ai = aitop; ai; ai = ai->ai_next) {
2392 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2393 continue;
2394 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2395 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002396 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002397 continue;
2398 }
2399 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002400 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002401 if (sock < 0) {
2402 /* this is no error since kernel may not support ipv6 */
2403 verbose("socket: %.100s", strerror(errno));
2404 continue;
2405 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002406
2407 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002408
Damien Miller34132e52000-01-14 15:45:46 +11002409 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002410
Damien Miller34132e52000-01-14 15:45:46 +11002411 /* Bind the socket to the address. */
2412 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2413 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002414 if (!ai->ai_next)
2415 error("bind: %.100s", strerror(errno));
2416 else
2417 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002418
Damien Miller34132e52000-01-14 15:45:46 +11002419 close(sock);
2420 continue;
2421 }
2422 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002423 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002424 error("listen: %.100s", strerror(errno));
2425 close(sock);
2426 continue;
2427 }
2428 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002429 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002430 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002431 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002432 strlcpy(c->path, host, sizeof(c->path));
2433 c->host_port = port_to_connect;
2434 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002435 success = 1;
2436 }
2437 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002438 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002439 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002440 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002441 return success;
Damien Miller95def091999-11-25 00:26:21 +11002442}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002443
Darren Tuckere7066df2004-05-24 10:18:05 +10002444int
2445channel_cancel_rport_listener(const char *host, u_short port)
2446{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002447 u_int i;
2448 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002449
Darren Tucker47eede72005-03-14 23:08:12 +11002450 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002451 Channel *c = channels[i];
2452
2453 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2454 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002455 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002456 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002457 channel_free(c);
2458 found = 1;
2459 }
2460 }
2461
2462 return (found);
2463}
2464
Damien Millerb16461c2002-01-22 23:29:22 +11002465/* protocol local port fwd, used by ssh (and sshd in v1) */
2466int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002467channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002468 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2469{
2470 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002471 listen_host, listen_port, host_to_connect, port_to_connect,
2472 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002473}
2474
2475/* protocol v2 remote port fwd, used by sshd */
2476int
2477channel_setup_remote_fwd_listener(const char *listen_address,
2478 u_short listen_port, int gateway_ports)
2479{
2480 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2481 listen_address, listen_port, NULL, 0, gateway_ports);
2482}
2483
Damien Miller5428f641999-11-25 11:54:57 +11002484/*
2485 * Initiate forwarding of connections to port "port" on remote host through
2486 * the secure channel to host:port from local side.
2487 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002488
Darren Tuckere7d4b192006-07-12 22:17:10 +10002489int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002490channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002491 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002492{
Damien Millerdff50992002-01-22 23:16:32 +11002493 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002494
Damien Miller95def091999-11-25 00:26:21 +11002495 /* Record locally that connection to this host/port is permitted. */
2496 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2497 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002498
Damien Miller95def091999-11-25 00:26:21 +11002499 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002500 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002501 const char *address_to_bind;
2502 if (listen_host == NULL)
2503 address_to_bind = "localhost";
2504 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2505 address_to_bind = "";
2506 else
2507 address_to_bind = listen_host;
2508
Damien Miller33b13562000-04-04 14:38:59 +10002509 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2510 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002511 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002512 packet_put_cstring(address_to_bind);
2513 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002514 packet_send();
2515 packet_write_wait();
2516 /* Assume that server accepts the request */
2517 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002518 } else {
2519 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002520 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002521 packet_put_cstring(host_to_connect);
2522 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002523 packet_send();
2524 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002525
2526 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002527 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002528 switch (type) {
2529 case SSH_SMSG_SUCCESS:
2530 success = 1;
2531 break;
2532 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002533 break;
2534 default:
2535 /* Unknown packet */
2536 packet_disconnect("Protocol error for port forward request:"
2537 "received packet type %d.", type);
2538 }
2539 }
2540 if (success) {
2541 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2542 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2543 permitted_opens[num_permitted_opens].listen_port = listen_port;
2544 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002545 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002546 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002547}
2548
Damien Miller5428f641999-11-25 11:54:57 +11002549/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002550 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002551 * local side.
2552 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002553void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002554channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002555{
2556 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002557
2558 if (!compat20)
2559 return;
2560
2561 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002562 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002563 permitted_opens[i].listen_port == port)
2564 break;
2565 }
2566 if (i >= num_permitted_opens) {
2567 debug("%s: requested forward not found", __func__);
2568 return;
2569 }
2570 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2571 packet_put_cstring("cancel-tcpip-forward");
2572 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002573 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002574 packet_put_int(port);
2575 packet_send();
2576
2577 permitted_opens[i].listen_port = 0;
2578 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002579 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002580 permitted_opens[i].host_to_connect = NULL;
2581}
2582
2583/*
Damien Miller5428f641999-11-25 11:54:57 +11002584 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2585 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002586 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002587 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002588int
Damien Millere247cc42000-05-07 12:03:14 +10002589channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002590{
Damien Milleraae6c611999-12-06 11:47:28 +11002591 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002592 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002593 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002594
Damien Miller95def091999-11-25 00:26:21 +11002595 /* Get arguments from the packet. */
2596 port = packet_get_int();
2597 hostname = packet_get_string(NULL);
2598 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002599
Damien Millerbac2d8a2000-09-05 16:13:06 +11002600#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002601 /*
2602 * Check that an unprivileged user is not trying to forward a
2603 * privileged port.
2604 */
Damien Miller95def091999-11-25 00:26:21 +11002605 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002606 packet_disconnect(
2607 "Requested forwarding of port %d but user is not root.",
2608 port);
2609 if (host_port == 0)
2610 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002611#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002612
Damien Miller0bc1bd82000-11-13 22:57:25 +11002613 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002614 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002615 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002616
2617 /* Free the argument string. */
2618 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002619
2620 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002621}
2622
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002623/*
2624 * Permits opening to any host/port if permitted_opens[] is empty. This is
2625 * usually called by the server, because the user could connect to any port
2626 * anyway, and the server has no way to know but to trust the client anyway.
2627 */
2628void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002629channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002630{
2631 if (num_permitted_opens == 0)
2632 all_opens_permitted = 1;
2633}
2634
Ben Lindstroma3700052001-04-05 23:26:32 +00002635void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002636channel_add_permitted_opens(char *host, int port)
2637{
2638 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002639 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002640 debug("allow port forwarding to host %s port %d", host, port);
2641
2642 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2643 permitted_opens[num_permitted_opens].port_to_connect = port;
2644 num_permitted_opens++;
2645
2646 all_opens_permitted = 0;
2647}
2648
Ben Lindstroma3700052001-04-05 23:26:32 +00002649void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002650channel_clear_permitted_opens(void)
2651{
2652 int i;
2653
2654 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002655 if (permitted_opens[i].host_to_connect != NULL)
2656 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002657 num_permitted_opens = 0;
2658
2659}
2660
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002661/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002662static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002663connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002664{
Damien Miller34132e52000-01-14 15:45:46 +11002665 struct addrinfo hints, *ai, *aitop;
2666 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2667 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002668 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002669
Damien Miller34132e52000-01-14 15:45:46 +11002670 memset(&hints, 0, sizeof(hints));
2671 hints.ai_family = IPv4or6;
2672 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002673 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002674 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002675 error("connect_to %.100s: unknown host (%s)", host,
2676 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002677 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002678 }
Damien Miller34132e52000-01-14 15:45:46 +11002679 for (ai = aitop; ai; ai = ai->ai_next) {
2680 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2681 continue;
2682 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2683 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002684 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002685 continue;
2686 }
Damien Miller2372ace2003-05-14 13:42:23 +10002687 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002688 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002689 if (ai->ai_next == NULL)
2690 error("socket: %.100s", strerror(errno));
2691 else
2692 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002693 continue;
2694 }
Damien Miller232711f2004-06-15 10:35:30 +10002695 if (set_nonblock(sock) == -1)
2696 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002697 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2698 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002699 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002700 strerror(errno));
2701 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002702 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002703 }
2704 break; /* success */
2705
2706 }
2707 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002708 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002709 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002710 return -1;
2711 }
2712 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002713 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002714 return sock;
2715}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002716
Damien Miller0bc1bd82000-11-13 22:57:25 +11002717int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002718channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002719{
2720 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002721
Damien Miller0bc1bd82000-11-13 22:57:25 +11002722 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002723 if (permitted_opens[i].host_to_connect != NULL &&
2724 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002725 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002726 permitted_opens[i].host_to_connect,
2727 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002728 error("WARNING: Server requests forwarding for unknown listen_port %d",
2729 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002730 return -1;
2731}
Damien Millerb38eff82000-04-01 11:09:21 +10002732
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002733/* Check if connecting to that port is permitted and connect. */
2734int
2735channel_connect_to(const char *host, u_short port)
2736{
2737 int i, permit;
2738
2739 permit = all_opens_permitted;
2740 if (!permit) {
2741 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002742 if (permitted_opens[i].host_to_connect != NULL &&
2743 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002744 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2745 permit = 1;
2746
2747 }
2748 if (!permit) {
Damien Miller996acd22003-04-09 20:59:48 +10002749 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002750 "but the request was denied.", host, port);
2751 return -1;
2752 }
2753 return connect_to(host, port);
2754}
2755
Damien Miller0e220db2004-06-15 10:34:08 +10002756void
2757channel_send_window_changes(void)
2758{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002759 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002760 struct winsize ws;
2761
2762 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002763 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002764 channels[i]->type != SSH_CHANNEL_OPEN)
2765 continue;
2766 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2767 continue;
2768 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002769 packet_put_int((u_int)ws.ws_col);
2770 packet_put_int((u_int)ws.ws_row);
2771 packet_put_int((u_int)ws.ws_xpixel);
2772 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002773 packet_send();
2774 }
2775}
2776
Ben Lindstrome9c99912001-06-09 00:41:05 +00002777/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002778
Damien Miller5428f641999-11-25 11:54:57 +11002779/*
2780 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002781 * Returns 0 and a suitable display number for the DISPLAY variable
2782 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002783 */
Kevin Steves366298c2001-12-19 17:58:01 +00002784int
Damien Miller95c249f2002-02-05 12:11:34 +11002785x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002786 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002787{
Damien Millere7378562001-12-21 14:58:35 +11002788 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002789 int display_number, sock;
2790 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002791 struct addrinfo hints, *ai, *aitop;
2792 char strport[NI_MAXSERV];
2793 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002794
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002795 if (chanids == NULL)
2796 return -1;
2797
Damien Millera34a28b1999-12-14 10:47:15 +11002798 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002799 display_number < MAX_DISPLAYS;
2800 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002801 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002802 memset(&hints, 0, sizeof(hints));
2803 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002804 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002805 hints.ai_socktype = SOCK_STREAM;
2806 snprintf(strport, sizeof strport, "%d", port);
2807 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2808 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002809 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002810 }
Damien Miller34132e52000-01-14 15:45:46 +11002811 for (ai = aitop; ai; ai = ai->ai_next) {
2812 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2813 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002814 sock = socket(ai->ai_family, ai->ai_socktype,
2815 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002816 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002817 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002818 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002819 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002820 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002821 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002822 debug("x11_create_display_inet: Socket family %d not supported",
2823 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002824 continue;
2825 }
Damien Miller34132e52000-01-14 15:45:46 +11002826 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002827#ifdef IPV6_V6ONLY
2828 if (ai->ai_family == AF_INET6) {
2829 int on = 1;
2830 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2831 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2832 }
2833#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002834 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002835 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002836 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002837 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002838
2839 if (ai->ai_next)
2840 continue;
2841
Damien Miller34132e52000-01-14 15:45:46 +11002842 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002843 close(socks[n]);
2844 }
2845 num_socks = 0;
2846 break;
2847 }
2848 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002849#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002850 if (num_socks == NUM_SOCKS)
2851 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002852#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002853 if (x11_use_localhost) {
2854 if (num_socks == NUM_SOCKS)
2855 break;
2856 } else {
2857 break;
2858 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002859#endif
Damien Miller95def091999-11-25 00:26:21 +11002860 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002861 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002862 if (num_socks > 0)
2863 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002864 }
Damien Miller95def091999-11-25 00:26:21 +11002865 if (display_number >= MAX_DISPLAYS) {
2866 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002867 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002868 }
Damien Miller95def091999-11-25 00:26:21 +11002869 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002870 for (n = 0; n < num_socks; n++) {
2871 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002872 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002873 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002874 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002875 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002876 }
Damien Miller95def091999-11-25 00:26:21 +11002877 }
Damien Miller34132e52000-01-14 15:45:46 +11002878
Damien Miller34132e52000-01-14 15:45:46 +11002879 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002880 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002881 for (n = 0; n < num_socks; n++) {
2882 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002883 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002884 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2885 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002886 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002887 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002888 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002889 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002890 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002891
Kevin Steves366298c2001-12-19 17:58:01 +00002892 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002893 *display_numberp = display_number;
2894 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002895}
2896
Ben Lindstrombba81212001-06-25 05:01:22 +00002897static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002898connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002899{
Damien Miller95def091999-11-25 00:26:21 +11002900 int sock;
2901 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002902
Damien Miller3afe3752001-12-21 12:39:51 +11002903 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2904 if (sock < 0)
2905 error("socket: %.100s", strerror(errno));
2906 memset(&addr, 0, sizeof(addr));
2907 addr.sun_family = AF_UNIX;
2908 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002909 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002910 return sock;
2911 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002912 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2913 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002914}
2915
Damien Millerbd483e72000-04-30 10:00:53 +10002916int
2917x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002918{
Damien Miller57c4e872006-03-31 23:11:07 +11002919 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002920 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002921 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002922 struct addrinfo hints, *ai, *aitop;
2923 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002924 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002925
Damien Miller95def091999-11-25 00:26:21 +11002926 /* Try to open a socket for the local X server. */
2927 display = getenv("DISPLAY");
2928 if (!display) {
2929 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002930 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931 }
Damien Miller5428f641999-11-25 11:54:57 +11002932 /*
2933 * Now we decode the value of the DISPLAY variable and make a
2934 * connection to the real X server.
2935 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002936
Damien Miller5428f641999-11-25 11:54:57 +11002937 /*
2938 * Check if it is a unix domain socket. Unix domain displays are in
2939 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2940 */
Damien Miller95def091999-11-25 00:26:21 +11002941 if (strncmp(display, "unix:", 5) == 0 ||
2942 display[0] == ':') {
2943 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11002944 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002945 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002946 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002947 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002948 }
2949 /* Create a socket. */
2950 sock = connect_local_xsocket(display_number);
2951 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002952 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002953
2954 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002955 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002956 }
Damien Miller5428f641999-11-25 11:54:57 +11002957 /*
2958 * Connect to an inet socket. The DISPLAY value is supposedly
2959 * hostname:d[.s], where hostname may also be numeric IP address.
2960 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00002961 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11002962 cp = strchr(buf, ':');
2963 if (!cp) {
2964 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002965 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002966 }
Damien Miller95def091999-11-25 00:26:21 +11002967 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002968 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11002969 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002970 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002971 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002972 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002973 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002974
Damien Miller34132e52000-01-14 15:45:46 +11002975 /* Look up the host address */
2976 memset(&hints, 0, sizeof(hints));
2977 hints.ai_family = IPv4or6;
2978 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11002979 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11002980 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2981 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002982 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002983 }
Damien Miller34132e52000-01-14 15:45:46 +11002984 for (ai = aitop; ai; ai = ai->ai_next) {
2985 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10002986 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002987 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002988 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002989 continue;
2990 }
2991 /* Connect it to the display. */
2992 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11002993 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10002994 6000 + display_number, strerror(errno));
2995 close(sock);
2996 continue;
2997 }
2998 /* Success */
2999 break;
Damien Miller34132e52000-01-14 15:45:46 +11003000 }
Damien Miller34132e52000-01-14 15:45:46 +11003001 freeaddrinfo(aitop);
3002 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11003003 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11003004 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003005 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003006 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003007 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003008 return sock;
3009}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003010
Damien Millerbd483e72000-04-30 10:00:53 +10003011/*
3012 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3013 * the remote channel number. We should do whatever we want, and respond
3014 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3015 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003016
Damien Millerbd483e72000-04-30 10:00:53 +10003017void
Damien Miller630d6f42002-01-22 23:17:30 +11003018x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003019{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003020 Channel *c = NULL;
3021 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003022 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003023
3024 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003025
3026 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003027
3028 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003029 remote_host = packet_get_string(NULL);
3030 } else {
3031 remote_host = xstrdup("unknown (remote did not supply name)");
3032 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003033 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003034
3035 /* Obtain a connection to the real X display. */
3036 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003037 if (sock != -1) {
3038 /* Allocate a channel for this connection. */
3039 c = channel_new("connected x11 socket",
3040 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3041 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003042 c->remote_id = remote_id;
3043 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003044 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003045 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003046 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003047 /* Send refusal to the remote host. */
3048 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003049 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003050 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003051 /* Send a confirmation to the remote host. */
3052 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003053 packet_put_int(remote_id);
3054 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003055 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003056 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003057}
3058
Damien Miller69b69aa2000-10-28 14:19:58 +11003059/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
3060void
Damien Miller630d6f42002-01-22 23:17:30 +11003061deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003062{
3063 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003064
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003065 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003066 case SSH_SMSG_AGENT_OPEN:
3067 error("Warning: ssh server tried agent forwarding.");
3068 break;
3069 case SSH_SMSG_X11_OPEN:
3070 error("Warning: ssh server tried X11 forwarding.");
3071 break;
3072 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003073 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003074 break;
3075 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003076 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003077 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3078 packet_put_int(rchan);
3079 packet_send();
3080}
3081
Damien Miller5428f641999-11-25 11:54:57 +11003082/*
3083 * Requests forwarding of X11 connections, generates fake authentication
3084 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003085 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003086 */
Damien Miller4af51302000-04-16 11:18:38 +10003087void
Damien Miller17e7ed02005-06-17 12:54:33 +10003088x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003089 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003090{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003091 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003092 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003093 char *new_data;
3094 int screen_number;
3095 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003096 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003097
Damien Millerf92c0792005-07-06 09:45:26 +10003098 if (x11_saved_display == NULL)
3099 x11_saved_display = xstrdup(disp);
3100 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003101 error("x11_request_forwarding_with_spoofing: different "
3102 "$DISPLAY already forwarded");
3103 return;
3104 }
3105
Damien Miller17e7ed02005-06-17 12:54:33 +10003106 cp = disp;
3107 if (disp)
3108 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003109 if (cp)
3110 cp = strchr(cp, '.');
3111 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003112 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003113 else
3114 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003115
Damien Miller13390022005-07-06 09:44:19 +10003116 if (x11_saved_proto == NULL) {
3117 /* Save protocol name. */
3118 x11_saved_proto = xstrdup(proto);
3119 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003120 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003121 * of the same length.
3122 */
3123 x11_saved_data = xmalloc(data_len);
3124 x11_fake_data = xmalloc(data_len);
3125 for (i = 0; i < data_len; i++) {
3126 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3127 fatal("x11_request_forwarding: bad "
3128 "authentication data: %.100s", data);
3129 if (i % 4 == 0)
3130 rnd = arc4random();
3131 x11_saved_data[i] = value;
3132 x11_fake_data[i] = rnd & 0xff;
3133 rnd >>= 8;
3134 }
3135 x11_saved_data_len = data_len;
3136 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003137 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003138
Damien Miller95def091999-11-25 00:26:21 +11003139 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003140 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003141
Damien Miller95def091999-11-25 00:26:21 +11003142 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003143 if (compat20) {
3144 channel_request_start(client_session_id, "x11-req", 0);
3145 packet_put_char(0); /* XXX bool single connection */
3146 } else {
3147 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3148 }
3149 packet_put_cstring(proto);
3150 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003151 packet_put_int(screen_number);
3152 packet_send();
3153 packet_write_wait();
3154 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003155}
3156
Ben Lindstrome9c99912001-06-09 00:41:05 +00003157
3158/* -- agent forwarding */
3159
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003160/* Sends a message to the server to request authentication fd forwarding. */
3161
Damien Miller4af51302000-04-16 11:18:38 +10003162void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003163auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003164{
Damien Miller95def091999-11-25 00:26:21 +11003165 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3166 packet_send();
3167 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003168}