blob: b5e28dabf16491697486f84080acc44caae4245e [file] [log] [blame]
Damien Millerb84886b2008-05-19 15:05:07 +10001/* $OpenBSD: channels.c,v 1.275 2008/05/08 12:02:23 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100045#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110046#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100047#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100048#ifdef HAVE_SYS_TIME_H
49# include <sys/time.h>
50#endif
Damien Millerefc04e72006-07-10 20:26:27 +100051
52#include <netinet/in.h>
53#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110054
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100056#include <netdb.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100057#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100058#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100059#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110060#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100061#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100062#include <stdarg.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Damien Millerb84886b2008-05-19 15:05:07 +100064#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100065#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "ssh1.h"
68#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "log.h"
71#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100072#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100076#include "key.h"
77#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110078#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Ben Lindstrome9c99912001-06-09 00:41:05 +000080/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * Pointer to an array containing all allocated channels. The array is
84 * dynamically extended as needed.
85 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000086static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000090 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110091 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100092static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000096 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110097 */
Damien Miller5e953212001-01-30 09:14:00 +110098static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Ben Lindstrome9c99912001-06-09 00:41:05 +0000101/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller5428f641999-11-25 11:54:57 +1100103/*
104 * Data structure for storing which hosts are permitted for forward requests.
105 * The local sides of any remote forwards are stored in this array to prevent
106 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
107 * network (which might be behind a firewall).
108 */
Damien Miller95def091999-11-25 00:26:21 +1100109typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000110 char *host_to_connect; /* Connect to 'host'. */
111 u_short port_to_connect; /* Connect to 'port'. */
112 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113} ForwardPermission;
114
Damien Miller9b439df2006-07-24 14:04:00 +1000115/* List of all permitted host/port pairs to connect by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000117
Damien Miller9b439df2006-07-24 14:04:00 +1000118/* List of all permitted host/port pairs to connect by the admin. */
119static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
120
121/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000123
124/* Number of permitted host/port pair in the array permitted by the admin. */
125static int num_adm_permitted_opens = 0;
126
Damien Miller5428f641999-11-25 11:54:57 +1100127/*
128 * If this is true, all opens are permitted. This is the case on the server
129 * on which we have to trust the client anyway, and the user could do
130 * anything after logging in anyway.
131 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132static int all_opens_permitted = 0;
133
Ben Lindstrome9c99912001-06-09 00:41:05 +0000134
135/* -- X11 forwarding */
136
137/* Maximum number of fake X11 displays to try. */
138#define MAX_DISPLAYS 1000
139
Damien Miller13390022005-07-06 09:44:19 +1000140/* Saved X11 local (client) display. */
141static char *x11_saved_display = NULL;
142
Ben Lindstrome9c99912001-06-09 00:41:05 +0000143/* Saved X11 authentication protocol name. */
144static char *x11_saved_proto = NULL;
145
146/* Saved X11 authentication data. This is the real data. */
147static char *x11_saved_data = NULL;
148static u_int x11_saved_data_len = 0;
149
150/*
151 * Fake X11 authentication data. This is what the server will be sending us;
152 * we should replace any occurrences of this by the real data.
153 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100154static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000155static u_int x11_fake_data_len;
156
157
158/* -- agent forwarding */
159
160#define NUM_SOCKS 10
161
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000162/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000163static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000164
Ben Lindstrome9c99912001-06-09 00:41:05 +0000165/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000167
Ben Lindstrome9c99912001-06-09 00:41:05 +0000168/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000169
170Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100171channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000172{
173 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000174
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000175 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100176 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000177 return NULL;
178 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000179 c = channels[id];
180 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100181 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000182 return NULL;
183 }
184 return c;
185}
186
Damien Miller5428f641999-11-25 11:54:57 +1100187/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100188 * Returns the channel if it is allowed to receive protocol messages.
189 * Private channels, like listening sockets, may not receive messages.
190 */
191Channel *
192channel_lookup(int id)
193{
194 Channel *c;
195
196 if ((c = channel_by_id(id)) == NULL)
197 return (NULL);
198
Damien Millerd62f2ca2006-03-26 13:57:41 +1100199 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100200 case SSH_CHANNEL_X11_OPEN:
201 case SSH_CHANNEL_LARVAL:
202 case SSH_CHANNEL_CONNECTING:
203 case SSH_CHANNEL_DYNAMIC:
204 case SSH_CHANNEL_OPENING:
205 case SSH_CHANNEL_OPEN:
206 case SSH_CHANNEL_INPUT_DRAINING:
207 case SSH_CHANNEL_OUTPUT_DRAINING:
208 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100209 }
210 logit("Non-public channel %d, type %d.", id, c->type);
211 return (NULL);
212}
213
214/*
Damien Millere247cc42000-05-07 12:03:14 +1000215 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000216 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100217 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100219channel_register_fds(Channel *c, int rfd, int wfd, int efd,
220 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221{
Damien Miller95def091999-11-25 00:26:21 +1100222 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100223 channel_max_fd = MAX(channel_max_fd, rfd);
224 channel_max_fd = MAX(channel_max_fd, wfd);
225 channel_max_fd = MAX(channel_max_fd, efd);
226
Damien Miller5428f641999-11-25 11:54:57 +1100227 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000228
Damien Miller6f83b8e2000-05-02 09:23:45 +1000229 c->rfd = rfd;
230 c->wfd = wfd;
231 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000232 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000233 c->efd = efd;
234 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100235
Damien Miller79438cc2001-02-16 12:34:57 +1100236 /* XXX ugly hack: nonblock is only set by the server */
237 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000238 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100239 c->isatty = 1;
240 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000241 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100242 c->self, c->wfd);
243 }
244 } else {
245 c->isatty = 0;
246 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000247 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100248
Damien Miller69b69aa2000-10-28 14:19:58 +1100249 /* enable nonblocking mode */
250 if (nonblock) {
251 if (rfd != -1)
252 set_nonblock(rfd);
253 if (wfd != -1)
254 set_nonblock(wfd);
255 if (efd != -1)
256 set_nonblock(efd);
257 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000258}
259
260/*
261 * Allocate a new channel object and set its type and socket. This will cause
262 * remote_name to be freed.
263 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000264Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000265channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000266 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000267{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000268 int found;
269 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000270 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller95def091999-11-25 00:26:21 +1100272 /* Do initial allocation if this is the first call. */
273 if (channels_alloc == 0) {
274 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100275 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100276 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000277 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100278 }
279 /* Try to find a free slot where to put the new channel. */
280 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000281 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100282 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000283 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100284 break;
285 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000286 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100287 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100288 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000289 if (channels_alloc > 10000)
290 fatal("channel_new: internal error: channels_alloc %d "
291 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100292 channels = xrealloc(channels, channels_alloc + 10,
293 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000294 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100295 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100296 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000297 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100298 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000299 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100300 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100301 buffer_init(&c->input);
302 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000303 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100304 c->ostate = CHAN_OUTPUT_OPEN;
305 c->istate = CHAN_INPUT_OPEN;
306 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100307 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100308 c->self = found;
309 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000310 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000311 c->local_window = window;
312 c->local_window_max = window;
313 c->local_consumed = 0;
314 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100315 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000316 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000317 c->remote_window = 0;
318 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000319 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100320 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000321 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100322 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000323 c->open_confirm = NULL;
324 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000325 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100326 c->output_filter = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000327 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100328 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000329 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000331
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000332static int
333channel_find_maxfd(void)
334{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000335 u_int i;
336 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000337 Channel *c;
338
339 for (i = 0; i < channels_alloc; i++) {
340 c = channels[i];
341 if (c != NULL) {
342 max = MAX(max, c->rfd);
343 max = MAX(max, c->wfd);
344 max = MAX(max, c->efd);
345 }
346 }
347 return max;
348}
349
350int
351channel_close_fd(int *fdp)
352{
353 int ret = 0, fd = *fdp;
354
355 if (fd != -1) {
356 ret = close(fd);
357 *fdp = -1;
358 if (fd == channel_max_fd)
359 channel_max_fd = channel_find_maxfd();
360 }
361 return ret;
362}
363
Damien Miller6f83b8e2000-05-02 09:23:45 +1000364/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000366channel_close_fds(Channel *c)
367{
Damien Miller0e220db2004-06-15 10:34:08 +1000368 debug3("channel %d: close_fds r %d w %d e %d c %d",
369 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000370
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000371 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000372 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000373 channel_close_fd(&c->rfd);
374 channel_close_fd(&c->wfd);
375 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000376}
377
378/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000379void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000380channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000382 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000383 u_int i, n;
Damien Millerb84886b2008-05-19 15:05:07 +1000384 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000385
386 for (n = 0, i = 0; i < channels_alloc; i++)
387 if (channels[i])
388 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000389 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000390 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000391
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000392 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000393 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000394 xfree(s);
395
Damien Miller6f83b8e2000-05-02 09:23:45 +1000396 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000397 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000398 if (c->ctl_fd != -1)
399 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000400 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000401 buffer_free(&c->input);
402 buffer_free(&c->output);
403 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000404 if (c->remote_name) {
405 xfree(c->remote_name);
406 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100407 }
Damien Millerb84886b2008-05-19 15:05:07 +1000408 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
409 if (cc->abandon_cb != NULL)
410 cc->abandon_cb(c, cc->ctx);
411 TAILQ_REMOVE(&c->status_confirms, cc, entry);
412 bzero(cc, sizeof(*cc));
413 xfree(cc);
414 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000415 channels[c->self] = NULL;
416 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417}
418
Ben Lindstrome9c99912001-06-09 00:41:05 +0000419void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000420channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000421{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000422 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000423
Ben Lindstrom601e4362001-06-21 03:19:23 +0000424 for (i = 0; i < channels_alloc; i++)
425 if (channels[i] != NULL)
426 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000427}
428
429/*
430 * Closes the sockets/fds of all channels. This is used to close extra file
431 * descriptors after a fork.
432 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000433void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000434channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000435{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000436 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000437
438 for (i = 0; i < channels_alloc; i++)
439 if (channels[i] != NULL)
440 channel_close_fds(channels[i]);
441}
442
443/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000444 * Stop listening to channels.
445 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000446void
447channel_stop_listening(void)
448{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000449 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000450 Channel *c;
451
452 for (i = 0; i < channels_alloc; i++) {
453 c = channels[i];
454 if (c != NULL) {
455 switch (c->type) {
456 case SSH_CHANNEL_AUTH_SOCKET:
457 case SSH_CHANNEL_PORT_LISTENER:
458 case SSH_CHANNEL_RPORT_LISTENER:
459 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000460 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000461 channel_free(c);
462 break;
463 }
464 }
465 }
466}
467
468/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000469 * Returns true if no channel has too much buffered data, and false if one or
470 * more channel is overfull.
471 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000472int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000473channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000474{
475 u_int i;
476 Channel *c;
477
478 for (i = 0; i < channels_alloc; i++) {
479 c = channels[i];
480 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000481#if 0
482 if (!compat20 &&
483 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100484 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000485 c->self, buffer_len(&c->input));
486 return 0;
487 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000488#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000489 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000490 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000491 c->self, buffer_len(&c->output),
492 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000493 return 0;
494 }
495 }
496 }
497 return 1;
498}
499
500/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000501int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000502channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000503{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000504 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000505 Channel *c;
506
507 for (i = 0; i < channels_alloc; i++) {
508 c = channels[i];
509 if (c == NULL)
510 continue;
511 switch (c->type) {
512 case SSH_CHANNEL_X11_LISTENER:
513 case SSH_CHANNEL_PORT_LISTENER:
514 case SSH_CHANNEL_RPORT_LISTENER:
515 case SSH_CHANNEL_CLOSED:
516 case SSH_CHANNEL_AUTH_SOCKET:
517 case SSH_CHANNEL_DYNAMIC:
518 case SSH_CHANNEL_CONNECTING:
519 case SSH_CHANNEL_ZOMBIE:
520 continue;
521 case SSH_CHANNEL_LARVAL:
522 if (!compat20)
523 fatal("cannot happen: SSH_CHANNEL_LARVAL");
524 continue;
525 case SSH_CHANNEL_OPENING:
526 case SSH_CHANNEL_OPEN:
527 case SSH_CHANNEL_X11_OPEN:
528 return 1;
529 case SSH_CHANNEL_INPUT_DRAINING:
530 case SSH_CHANNEL_OUTPUT_DRAINING:
531 if (!compat13)
532 fatal("cannot happen: OUT_DRAIN");
533 return 1;
534 default:
535 fatal("channel_still_open: bad channel type %d", c->type);
536 /* NOTREACHED */
537 }
538 }
539 return 0;
540}
541
542/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000543int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000544channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000545{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000546 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000547 Channel *c;
548
549 for (i = 0; i < channels_alloc; i++) {
550 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000551 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000552 continue;
553 switch (c->type) {
554 case SSH_CHANNEL_CLOSED:
555 case SSH_CHANNEL_DYNAMIC:
556 case SSH_CHANNEL_X11_LISTENER:
557 case SSH_CHANNEL_PORT_LISTENER:
558 case SSH_CHANNEL_RPORT_LISTENER:
559 case SSH_CHANNEL_OPENING:
560 case SSH_CHANNEL_CONNECTING:
561 case SSH_CHANNEL_ZOMBIE:
562 continue;
563 case SSH_CHANNEL_LARVAL:
564 case SSH_CHANNEL_AUTH_SOCKET:
565 case SSH_CHANNEL_OPEN:
566 case SSH_CHANNEL_X11_OPEN:
567 return i;
568 case SSH_CHANNEL_INPUT_DRAINING:
569 case SSH_CHANNEL_OUTPUT_DRAINING:
570 if (!compat13)
571 fatal("cannot happen: OUT_DRAIN");
572 return i;
573 default:
574 fatal("channel_find_open: bad channel type %d", c->type);
575 /* NOTREACHED */
576 }
577 }
578 return -1;
579}
580
581
582/*
583 * Returns a message describing the currently open forwarded connections,
584 * suitable for sending to the client. The message contains crlf pairs for
585 * newlines.
586 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000587char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000588channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000589{
590 Buffer buffer;
591 Channel *c;
592 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000593 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000594
595 buffer_init(&buffer);
596 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
597 buffer_append(&buffer, buf, strlen(buf));
598 for (i = 0; i < channels_alloc; i++) {
599 c = channels[i];
600 if (c == NULL)
601 continue;
602 switch (c->type) {
603 case SSH_CHANNEL_X11_LISTENER:
604 case SSH_CHANNEL_PORT_LISTENER:
605 case SSH_CHANNEL_RPORT_LISTENER:
606 case SSH_CHANNEL_CLOSED:
607 case SSH_CHANNEL_AUTH_SOCKET:
608 case SSH_CHANNEL_ZOMBIE:
609 continue;
610 case SSH_CHANNEL_LARVAL:
611 case SSH_CHANNEL_OPENING:
612 case SSH_CHANNEL_CONNECTING:
613 case SSH_CHANNEL_DYNAMIC:
614 case SSH_CHANNEL_OPEN:
615 case SSH_CHANNEL_X11_OPEN:
616 case SSH_CHANNEL_INPUT_DRAINING:
617 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000618 snprintf(buf, sizeof buf,
619 " #%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 +0000620 c->self, c->remote_name,
621 c->type, c->remote_id,
622 c->istate, buffer_len(&c->input),
623 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000624 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000625 buffer_append(&buffer, buf, strlen(buf));
626 continue;
627 default:
628 fatal("channel_open_message: bad channel type %d", c->type);
629 /* NOTREACHED */
630 }
631 }
632 buffer_append(&buffer, "\0", 1);
633 cp = xstrdup(buffer_ptr(&buffer));
634 buffer_free(&buffer);
635 return cp;
636}
637
638void
639channel_send_open(int id)
640{
641 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000642
Ben Lindstrome9c99912001-06-09 00:41:05 +0000643 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000644 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000645 return;
646 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000647 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000648 packet_start(SSH2_MSG_CHANNEL_OPEN);
649 packet_put_cstring(c->ctype);
650 packet_put_int(c->self);
651 packet_put_int(c->local_window);
652 packet_put_int(c->local_maxpacket);
653 packet_send();
654}
655
656void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000657channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000658{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000659 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000660
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000662 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663 return;
664 }
Damien Miller0e220db2004-06-15 10:34:08 +1000665 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666 packet_start(SSH2_MSG_CHANNEL_REQUEST);
667 packet_put_int(c->remote_id);
668 packet_put_cstring(service);
669 packet_put_char(wantconfirm);
670}
Damien Miller4f7becb2006-03-26 14:10:14 +1100671
Ben Lindstrome9c99912001-06-09 00:41:05 +0000672void
Damien Millerb84886b2008-05-19 15:05:07 +1000673channel_register_status_confirm(int id, channel_confirm_cb *cb,
674 channel_confirm_abandon_cb *abandon_cb, void *ctx)
675{
676 struct channel_confirm *cc;
677 Channel *c;
678
679 if ((c = channel_lookup(id)) == NULL)
680 fatal("channel_register_expect: %d: bad id", id);
681
682 cc = xmalloc(sizeof(*cc));
683 cc->cb = cb;
684 cc->abandon_cb = abandon_cb;
685 cc->ctx = ctx;
686 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
687}
688
689void
690channel_register_open_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000691{
692 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000693
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694 if (c == NULL) {
Damien Millerb84886b2008-05-19 15:05:07 +1000695 logit("channel_register_open_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000696 return;
697 }
Damien Millerb84886b2008-05-19 15:05:07 +1000698 c->open_confirm = fn;
699 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000700}
Damien Miller4f7becb2006-03-26 14:10:14 +1100701
Ben Lindstrome9c99912001-06-09 00:41:05 +0000702void
Damien Miller39eda6e2005-11-05 14:52:50 +1100703channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000704{
Damien Millerd47c62a2005-12-13 19:33:57 +1100705 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000706
Ben Lindstrome9c99912001-06-09 00:41:05 +0000707 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000708 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000709 return;
710 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000711 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100712 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000713}
Damien Miller4f7becb2006-03-26 14:10:14 +1100714
Ben Lindstrome9c99912001-06-09 00:41:05 +0000715void
716channel_cancel_cleanup(int id)
717{
Damien Millerd47c62a2005-12-13 19:33:57 +1100718 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000719
Ben Lindstrome9c99912001-06-09 00:41:05 +0000720 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000721 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000722 return;
723 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000724 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100725 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000726}
Damien Miller4f7becb2006-03-26 14:10:14 +1100727
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728void
Damien Miller077b2382005-12-31 16:22:32 +1100729channel_register_filter(int id, channel_infilter_fn *ifn,
730 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000731{
732 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000733
Ben Lindstrome9c99912001-06-09 00:41:05 +0000734 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000735 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000736 return;
737 }
Damien Miller077b2382005-12-31 16:22:32 +1100738 c->input_filter = ifn;
739 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000740}
741
742void
743channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100744 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000745{
746 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000747
Ben Lindstrome9c99912001-06-09 00:41:05 +0000748 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
749 fatal("channel_activate for non-larval channel %d.", id);
750 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
751 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100752 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
754 packet_put_int(c->remote_id);
755 packet_put_int(c->local_window);
756 packet_send();
757}
758
Damien Miller5428f641999-11-25 11:54:57 +1100759/*
Damien Millerb38eff82000-04-01 11:09:21 +1000760 * 'channel_pre*' are called just before select() to add any bits relevant to
761 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100762 */
Damien Millerb38eff82000-04-01 11:09:21 +1000763/*
764 * 'channel_post*': perform any appropriate operations for channels which
765 * have events pending.
766 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100767typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000768chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
769chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
770
Damien Miller8473dd82006-07-24 14:08:32 +1000771/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000772static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100773channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000774{
775 FD_SET(c->sock, readset);
776}
777
Damien Miller8473dd82006-07-24 14:08:32 +1000778/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000779static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100780channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000781{
782 debug3("channel %d: waiting for connection", c->self);
783 FD_SET(c->sock, writeset);
784}
785
Ben Lindstrombba81212001-06-25 05:01:22 +0000786static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100787channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000788{
789 if (buffer_len(&c->input) < packet_get_maxsize())
790 FD_SET(c->sock, readset);
791 if (buffer_len(&c->output) > 0)
792 FD_SET(c->sock, writeset);
793}
794
Ben Lindstrombba81212001-06-25 05:01:22 +0000795static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100796channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000797{
Damien Millerde6987c2002-01-22 23:20:40 +1100798 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000799
Damien Miller33b13562000-04-04 14:38:59 +1000800 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100801 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000802 buffer_len(&c->input) < limit &&
803 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000804 FD_SET(c->rfd, readset);
805 if (c->ostate == CHAN_OUTPUT_OPEN ||
806 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
807 if (buffer_len(&c->output) > 0) {
808 FD_SET(c->wfd, writeset);
809 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000810 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000811 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
812 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000813 else
814 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000815 }
816 }
817 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100818 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000819 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
820 buffer_len(&c->extended) > 0)
821 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000822 else if (!(c->flags & CHAN_EOF_SENT) &&
823 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000824 buffer_len(&c->extended) < c->remote_window)
825 FD_SET(c->efd, readset);
826 }
Damien Miller0e220db2004-06-15 10:34:08 +1000827 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000828 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000829 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
830 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000831}
832
Damien Miller8473dd82006-07-24 14:08:32 +1000833/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000834static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100835channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000836{
837 if (buffer_len(&c->input) == 0) {
838 packet_start(SSH_MSG_CHANNEL_CLOSE);
839 packet_put_int(c->remote_id);
840 packet_send();
841 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000842 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000843 }
844}
845
Damien Miller8473dd82006-07-24 14:08:32 +1000846/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000847static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100848channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000849{
850 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000851 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000852 else
Damien Millerb38eff82000-04-01 11:09:21 +1000853 FD_SET(c->sock, writeset);
854}
855
856/*
857 * This is a special state for X11 authentication spoofing. An opened X11
858 * connection (when authentication spoofing is being done) remains in this
859 * state until the first packet has been completely read. The authentication
860 * data in that packet is then substituted by the real data if it matches the
861 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000862 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000863 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000864 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000865static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000866x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000867{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000868 u_char *ucp;
869 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000870
871 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000872 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000873 return 0;
874
875 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100876 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000877 if (ucp[0] == 0x42) { /* Byte order MSB first. */
878 proto_len = 256 * ucp[6] + ucp[7];
879 data_len = 256 * ucp[8] + ucp[9];
880 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
881 proto_len = ucp[6] + 256 * ucp[7];
882 data_len = ucp[8] + 256 * ucp[9];
883 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000884 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100885 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000886 return -1;
887 }
888
889 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000890 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000891 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
892 return 0;
893
894 /* Check if authentication protocol matches. */
895 if (proto_len != strlen(x11_saved_proto) ||
896 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000897 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000898 return -1;
899 }
900 /* Check if authentication data matches our fake data. */
901 if (data_len != x11_fake_data_len ||
902 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
903 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000904 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000905 return -1;
906 }
907 /* Check fake data length */
908 if (x11_fake_data_len != x11_saved_data_len) {
909 error("X11 fake_data_len %d != saved_data_len %d",
910 x11_fake_data_len, x11_saved_data_len);
911 return -1;
912 }
913 /*
914 * Received authentication protocol and data match
915 * our fake data. Substitute the fake data with real
916 * data.
917 */
918 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
919 x11_saved_data, x11_saved_data_len);
920 return 1;
921}
922
Ben Lindstrombba81212001-06-25 05:01:22 +0000923static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100924channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000925{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000926 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000927
Damien Millerb38eff82000-04-01 11:09:21 +1000928 if (ret == 1) {
929 /* Start normal processing for the channel. */
930 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000931 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000932 } else if (ret == -1) {
933 /*
934 * We have received an X11 connection that has bad
935 * authentication information.
936 */
Damien Miller996acd22003-04-09 20:59:48 +1000937 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000938 buffer_clear(&c->input);
939 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000940 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000941 c->sock = -1;
942 c->type = SSH_CHANNEL_CLOSED;
943 packet_start(SSH_MSG_CHANNEL_CLOSE);
944 packet_put_int(c->remote_id);
945 packet_send();
946 }
947}
948
Ben Lindstrombba81212001-06-25 05:01:22 +0000949static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100950channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000951{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000952 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000953
954 /* c->force_drain = 1; */
955
Damien Millerb38eff82000-04-01 11:09:21 +1000956 if (ret == 1) {
957 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100958 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000959 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000960 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000961 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100962 chan_read_failed(c);
963 buffer_clear(&c->input);
964 chan_ibuf_empty(c);
965 buffer_clear(&c->output);
966 /* for proto v1, the peer will send an IEOF */
967 if (compat20)
968 chan_write_failed(c);
969 else
970 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000971 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000972 }
973}
974
Ben Lindstromb3921512001-04-11 15:57:50 +0000975/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +1000976/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000977static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100978channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000979{
Damien Millera10f5612002-09-12 09:49:15 +1000980 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000981 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100982 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000983 struct {
984 u_int8_t version;
985 u_int8_t command;
986 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000987 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000988 } s4_req, s4_rsp;
989
Ben Lindstromb3921512001-04-11 15:57:50 +0000990 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000991
992 have = buffer_len(&c->input);
993 len = sizeof(s4_req);
994 if (have < len)
995 return 0;
996 p = buffer_ptr(&c->input);
997 for (found = 0, i = len; i < have; i++) {
998 if (p[i] == '\0') {
999 found = 1;
1000 break;
1001 }
1002 if (i > 1024) {
1003 /* the peer is probably sending garbage */
1004 debug("channel %d: decode socks4: too long",
1005 c->self);
1006 return -1;
1007 }
1008 }
1009 if (!found)
1010 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001011 buffer_get(&c->input, (char *)&s4_req.version, 1);
1012 buffer_get(&c->input, (char *)&s4_req.command, 1);
1013 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001014 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001015 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001016 p = buffer_ptr(&c->input);
1017 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001018 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001019 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001020 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001021 c->self, len, have);
1022 strlcpy(username, p, sizeof(username));
1023 buffer_consume(&c->input, len);
1024 buffer_consume(&c->input, 1); /* trailing '\0' */
1025
Ben Lindstromb3921512001-04-11 15:57:50 +00001026 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001027 strlcpy(c->path, host, sizeof(c->path));
1028 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001029
Damien Millerfbdeece2003-09-02 22:52:31 +10001030 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001031 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001032
Ben Lindstromb3921512001-04-11 15:57:50 +00001033 if (s4_req.command != 1) {
1034 debug("channel %d: cannot handle: socks4 cn %d",
1035 c->self, s4_req.command);
1036 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001037 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001038 s4_rsp.version = 0; /* vn: 0 for reply */
1039 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001040 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001041 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001042 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001043 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001044}
1045
Darren Tucker46471c92003-07-03 13:55:19 +10001046/* try to decode a socks5 header */
1047#define SSH_SOCKS5_AUTHDONE 0x1000
1048#define SSH_SOCKS5_NOAUTH 0x00
1049#define SSH_SOCKS5_IPV4 0x01
1050#define SSH_SOCKS5_DOMAIN 0x03
1051#define SSH_SOCKS5_IPV6 0x04
1052#define SSH_SOCKS5_CONNECT 0x01
1053#define SSH_SOCKS5_SUCCESS 0x00
1054
Damien Miller8473dd82006-07-24 14:08:32 +10001055/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001056static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001057channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001058{
1059 struct {
1060 u_int8_t version;
1061 u_int8_t command;
1062 u_int8_t reserved;
1063 u_int8_t atyp;
1064 } s5_req, s5_rsp;
1065 u_int16_t dest_port;
1066 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001067 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001068
1069 debug2("channel %d: decode socks5", c->self);
1070 p = buffer_ptr(&c->input);
1071 if (p[0] != 0x05)
1072 return -1;
1073 have = buffer_len(&c->input);
1074 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1075 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001076 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001077 return 0;
1078 nmethods = p[1];
1079 if (have < nmethods + 2)
1080 return 0;
1081 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001082 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001083 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001084 found = 1;
1085 break;
1086 }
1087 }
1088 if (!found) {
1089 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1090 c->self);
1091 return -1;
1092 }
1093 buffer_consume(&c->input, nmethods + 2);
1094 buffer_put_char(&c->output, 0x05); /* version */
1095 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1096 FD_SET(c->sock, writeset);
1097 c->flags |= SSH_SOCKS5_AUTHDONE;
1098 debug2("channel %d: socks5 auth done", c->self);
1099 return 0; /* need more */
1100 }
1101 debug2("channel %d: socks5 post auth", c->self);
1102 if (have < sizeof(s5_req)+1)
1103 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001104 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001105 if (s5_req.version != 0x05 ||
1106 s5_req.command != SSH_SOCKS5_CONNECT ||
1107 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001108 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001109 return -1;
1110 }
Darren Tucker47eede72005-03-14 23:08:12 +11001111 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001112 case SSH_SOCKS5_IPV4:
1113 addrlen = 4;
1114 af = AF_INET;
1115 break;
1116 case SSH_SOCKS5_DOMAIN:
1117 addrlen = p[sizeof(s5_req)];
1118 af = -1;
1119 break;
1120 case SSH_SOCKS5_IPV6:
1121 addrlen = 16;
1122 af = AF_INET6;
1123 break;
1124 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001125 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001126 return -1;
1127 }
Damien Miller0f077072006-07-10 22:21:02 +10001128 need = sizeof(s5_req) + addrlen + 2;
1129 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1130 need++;
1131 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001132 return 0;
1133 buffer_consume(&c->input, sizeof(s5_req));
1134 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1135 buffer_consume(&c->input, 1); /* host string length */
1136 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1137 buffer_get(&c->input, (char *)&dest_port, 2);
1138 dest_addr[addrlen] = '\0';
1139 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001140 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001141 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1142 return -1;
1143 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001144
Damien Millerfbdeece2003-09-02 22:52:31 +10001145 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001146 c->self, c->path, c->host_port, s5_req.command);
1147
1148 s5_rsp.version = 0x05;
1149 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1150 s5_rsp.reserved = 0; /* ignored */
1151 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1152 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1153 dest_port = 0; /* ignored */
1154
Damien Millera0fdce92006-03-26 14:28:50 +11001155 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1156 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1157 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001158 return 1;
1159}
1160
Ben Lindstromb3921512001-04-11 15:57:50 +00001161/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001162static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001163channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001164{
1165 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001166 u_int have;
1167 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001168
1169 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001170 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001171 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001172 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001173 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001174 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001175 /* need more */
1176 FD_SET(c->sock, readset);
1177 return;
1178 }
1179 /* try to guess the protocol */
1180 p = buffer_ptr(&c->input);
1181 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001182 case 0x04:
1183 ret = channel_decode_socks4(c, readset, writeset);
1184 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001185 case 0x05:
1186 ret = channel_decode_socks5(c, readset, writeset);
1187 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001188 default:
1189 ret = -1;
1190 break;
1191 }
1192 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001193 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001194 } else if (ret == 0) {
1195 debug2("channel %d: pre_dynamic: need more", c->self);
1196 /* need more */
1197 FD_SET(c->sock, readset);
1198 } else {
1199 /* switch to the next state */
1200 c->type = SSH_CHANNEL_OPENING;
1201 port_open_helper(c, "direct-tcpip");
1202 }
1203}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001204
Damien Millerb38eff82000-04-01 11:09:21 +10001205/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001206/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001207static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001208channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001209{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001210 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001211 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001212 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001213 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001214 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001215 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001216
1217 if (FD_ISSET(c->sock, readset)) {
1218 debug("X11 connection requested.");
1219 addrlen = sizeof(addr);
1220 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001221 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001222 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001223 channel_close_fd(&c->sock);
1224 chan_mark_dead(c);
1225 }
Damien Millerb38eff82000-04-01 11:09:21 +10001226 if (newsock < 0) {
1227 error("accept: %.100s", strerror(errno));
1228 return;
1229 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001230 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001231 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001232 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001233 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001234 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001235
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001236 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001237 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001238 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001239 if (compat20) {
1240 packet_start(SSH2_MSG_CHANNEL_OPEN);
1241 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001242 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001243 packet_put_int(nc->local_window_max);
1244 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001245 /* originator ipaddr and port */
1246 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001247 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001248 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001249 } else {
1250 packet_put_int(remote_port);
1251 }
Damien Millerbd483e72000-04-30 10:00:53 +10001252 packet_send();
1253 } else {
1254 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001255 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001256 if (packet_get_protocol_flags() &
1257 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001258 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001259 packet_send();
1260 }
Damien Millerd83ff352001-01-30 09:19:34 +11001261 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001262 }
1263}
1264
Ben Lindstrombba81212001-06-25 05:01:22 +00001265static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001266port_open_helper(Channel *c, char *rtype)
1267{
1268 int direct;
1269 char buf[1024];
1270 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001271 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001272
1273 direct = (strcmp(rtype, "direct-tcpip") == 0);
1274
1275 snprintf(buf, sizeof buf,
1276 "%s: listening port %d for %.100s port %d, "
1277 "connect from %.200s port %d",
1278 rtype, c->listening_port, c->path, c->host_port,
1279 remote_ipaddr, remote_port);
1280
1281 xfree(c->remote_name);
1282 c->remote_name = xstrdup(buf);
1283
1284 if (compat20) {
1285 packet_start(SSH2_MSG_CHANNEL_OPEN);
1286 packet_put_cstring(rtype);
1287 packet_put_int(c->self);
1288 packet_put_int(c->local_window_max);
1289 packet_put_int(c->local_maxpacket);
1290 if (direct) {
1291 /* target host, port */
1292 packet_put_cstring(c->path);
1293 packet_put_int(c->host_port);
1294 } else {
1295 /* listen address, port */
1296 packet_put_cstring(c->path);
1297 packet_put_int(c->listening_port);
1298 }
1299 /* originator host and port */
1300 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001301 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001302 packet_send();
1303 } else {
1304 packet_start(SSH_MSG_PORT_OPEN);
1305 packet_put_int(c->self);
1306 packet_put_cstring(c->path);
1307 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001308 if (packet_get_protocol_flags() &
1309 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001310 packet_put_cstring(c->remote_name);
1311 packet_send();
1312 }
1313 xfree(remote_ipaddr);
1314}
1315
Damien Miller5e7fd072005-11-05 14:53:39 +11001316static void
1317channel_set_reuseaddr(int fd)
1318{
1319 int on = 1;
1320
1321 /*
1322 * Set socket options.
1323 * Allow local port reuse in TIME_WAIT.
1324 */
1325 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1326 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1327}
1328
Damien Millerb38eff82000-04-01 11:09:21 +10001329/*
1330 * This socket is listening for connections to a forwarded TCP/IP port.
1331 */
Damien Miller8473dd82006-07-24 14:08:32 +10001332/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001333static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001334channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001335{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001336 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001337 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001338 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001339 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001340 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001341
Damien Millerb38eff82000-04-01 11:09:21 +10001342 if (FD_ISSET(c->sock, readset)) {
1343 debug("Connection to port %d forwarding "
1344 "to %.100s port %d requested.",
1345 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001346
Damien Miller4623a752001-10-10 15:03:58 +10001347 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1348 nextstate = SSH_CHANNEL_OPENING;
1349 rtype = "forwarded-tcpip";
1350 } else {
1351 if (c->host_port == 0) {
1352 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001353 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001354 } else {
1355 nextstate = SSH_CHANNEL_OPENING;
1356 rtype = "direct-tcpip";
1357 }
1358 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001359
Damien Millerb38eff82000-04-01 11:09:21 +10001360 addrlen = sizeof(addr);
1361 newsock = accept(c->sock, &addr, &addrlen);
1362 if (newsock < 0) {
1363 error("accept: %.100s", strerror(errno));
1364 return;
1365 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001366 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001367 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1368 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001369 nc->listening_port = c->listening_port;
1370 nc->host_port = c->host_port;
1371 strlcpy(nc->path, c->path, sizeof(nc->path));
1372
Damien Miller4623a752001-10-10 15:03:58 +10001373 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1374 /*
1375 * do not call the channel_post handler until
1376 * this flag has been reset by a pre-handler.
1377 * otherwise the FD_ISSET calls might overflow
1378 */
1379 nc->delayed = 1;
1380 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001381 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001382 }
Damien Millerb38eff82000-04-01 11:09:21 +10001383 }
1384}
1385
1386/*
1387 * This is the authentication agent socket listening for connections from
1388 * clients.
1389 */
Damien Miller8473dd82006-07-24 14:08:32 +10001390/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001391static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001392channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001393{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001394 Channel *nc;
1395 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001396 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001397 socklen_t addrlen;
1398
1399 if (FD_ISSET(c->sock, readset)) {
1400 addrlen = sizeof(addr);
1401 newsock = accept(c->sock, &addr, &addrlen);
1402 if (newsock < 0) {
1403 error("accept from auth socket: %.100s", strerror(errno));
1404 return;
1405 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001406 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001407 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1408 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001409 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001410 if (compat20) {
1411 packet_start(SSH2_MSG_CHANNEL_OPEN);
1412 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001413 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001414 packet_put_int(c->local_window_max);
1415 packet_put_int(c->local_maxpacket);
1416 } else {
1417 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001418 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001419 }
Damien Millerb38eff82000-04-01 11:09:21 +10001420 packet_send();
1421 }
1422}
1423
Damien Miller8473dd82006-07-24 14:08:32 +10001424/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001425static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001426channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001427{
Ben Lindstrom69128662001-05-08 20:07:39 +00001428 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001429 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001430
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001431 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001432 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001433 err = errno;
1434 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001435 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001436 if (err == 0) {
1437 debug("channel %d: connected", c->self);
1438 c->type = SSH_CHANNEL_OPEN;
1439 if (compat20) {
1440 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1441 packet_put_int(c->remote_id);
1442 packet_put_int(c->self);
1443 packet_put_int(c->local_window);
1444 packet_put_int(c->local_maxpacket);
1445 } else {
1446 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1447 packet_put_int(c->remote_id);
1448 packet_put_int(c->self);
1449 }
1450 } else {
1451 debug("channel %d: not connected: %s",
1452 c->self, strerror(err));
1453 if (compat20) {
1454 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1455 packet_put_int(c->remote_id);
1456 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1457 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1458 packet_put_cstring(strerror(err));
1459 packet_put_cstring("");
1460 }
1461 } else {
1462 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1463 packet_put_int(c->remote_id);
1464 }
1465 chan_mark_dead(c);
1466 }
1467 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001468 }
1469}
1470
Damien Miller8473dd82006-07-24 14:08:32 +10001471/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001472static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001473channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001474{
Darren Tucker11327cc2005-03-14 23:22:25 +11001475 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001476 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001477
Damien Miller835284b2007-06-11 13:03:16 +10001478 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1479 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001480 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001481 len = read(c->rfd, buf, sizeof(buf));
Damien Miller835284b2007-06-11 13:03:16 +10001482 if (len < 0 && (errno == EINTR || (errno == EAGAIN && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001483 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001484#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001485 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001486#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001487 if ((!c->isatty && len <= 0) ||
1488 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001489#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001490 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001491 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001492 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001493 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001494 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001495 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001496 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001497 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001498 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001499 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001500 } else {
1501 chan_read_failed(c);
1502 }
1503 return -1;
1504 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001505 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001506 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001507 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001508 chan_read_failed(c);
1509 }
Damien Millerd27b9472005-12-13 19:29:02 +11001510 } else if (c->datagram) {
1511 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001512 } else {
1513 buffer_append(&c->input, buf, len);
1514 }
Damien Millerb38eff82000-04-01 11:09:21 +10001515 }
1516 return 1;
1517}
Damien Miller4f7becb2006-03-26 14:10:14 +11001518
Damien Miller8473dd82006-07-24 14:08:32 +10001519/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001520static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001521channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001522{
Ben Lindstrome229b252001-03-05 06:28:06 +00001523 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001524 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001525 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001526 int len;
1527
1528 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001529 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001530 FD_ISSET(c->wfd, writeset) &&
1531 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001532 if (c->output_filter != NULL) {
1533 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1534 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001535 if (c->type != SSH_CHANNEL_OPEN)
1536 chan_mark_dead(c);
1537 else
1538 chan_write_failed(c);
1539 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001540 }
1541 } else if (c->datagram) {
1542 buf = data = buffer_get_string(&c->output, &dlen);
1543 } else {
1544 buf = data = buffer_ptr(&c->output);
1545 dlen = buffer_len(&c->output);
1546 }
1547
Damien Millerd27b9472005-12-13 19:29:02 +11001548 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001549 /* ignore truncated writes, datagrams might get lost */
1550 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001551 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001552 xfree(data);
1553 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1554 return 1;
1555 if (len <= 0) {
1556 if (c->type != SSH_CHANNEL_OPEN)
1557 chan_mark_dead(c);
1558 else
1559 chan_write_failed(c);
1560 return -1;
1561 }
1562 return 1;
1563 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001564#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001565 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001566 if (compat20 && c->wfd_isatty)
1567 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001568#endif
Damien Miller077b2382005-12-31 16:22:32 +11001569
1570 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001571 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1572 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001573 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001574 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001575 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001576 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001577 return -1;
1578 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001579 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001580 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001581 c->type = SSH_CHANNEL_INPUT_DRAINING;
1582 } else {
1583 chan_write_failed(c);
1584 }
1585 return -1;
1586 }
Damien Miller077b2382005-12-31 16:22:32 +11001587 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001588 if (tcgetattr(c->wfd, &tio) == 0 &&
1589 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1590 /*
1591 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001592 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001593 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001594 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001595 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001596 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001597 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001598 }
1599 }
Damien Millerb38eff82000-04-01 11:09:21 +10001600 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001601 if (compat20 && len > 0) {
1602 c->local_consumed += len;
1603 }
1604 }
1605 return 1;
1606}
Damien Miller4f7becb2006-03-26 14:10:14 +11001607
Ben Lindstrombba81212001-06-25 05:01:22 +00001608static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001609channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001610{
Darren Tucker11327cc2005-03-14 23:22:25 +11001611 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001612 int len;
1613
Damien Miller1383bd82000-04-06 12:32:37 +10001614/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001615 if (c->efd != -1) {
1616 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1617 FD_ISSET(c->efd, writeset) &&
1618 buffer_len(&c->extended) > 0) {
1619 len = write(c->efd, buffer_ptr(&c->extended),
1620 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001621 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001622 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001623 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1624 return 1;
1625 if (len <= 0) {
1626 debug2("channel %d: closing write-efd %d",
1627 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001628 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001629 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001630 buffer_consume(&c->extended, len);
1631 c->local_consumed += len;
1632 }
1633 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
Damien Millere42bd242007-01-29 10:16:28 +11001634 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001635 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001636 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001637 c->self, len, c->efd);
Damien Millere42bd242007-01-29 10:16:28 +11001638 if (len < 0 && (errno == EINTR ||
1639 (errno == EAGAIN && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001640 return 1;
1641 if (len <= 0) {
1642 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001643 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001644 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001645 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001646 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001647 }
Damien Miller33b13562000-04-04 14:38:59 +10001648 }
1649 }
1650 return 1;
1651}
Damien Miller4f7becb2006-03-26 14:10:14 +11001652
Damien Miller8473dd82006-07-24 14:08:32 +10001653/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001654static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001655channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001656{
1657 char buf[16];
1658 int len;
1659
1660 /* Monitor control fd to detect if the slave client exits */
1661 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1662 len = read(c->ctl_fd, buf, sizeof(buf));
1663 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1664 return 1;
1665 if (len <= 0) {
1666 debug2("channel %d: ctl read<=0", c->self);
1667 if (c->type != SSH_CHANNEL_OPEN) {
1668 debug2("channel %d: not open", c->self);
1669 chan_mark_dead(c);
1670 return -1;
1671 } else {
1672 chan_read_failed(c);
1673 chan_write_failed(c);
1674 }
1675 return -1;
1676 } else
1677 fatal("%s: unexpected data on ctl fd", __func__);
1678 }
1679 return 1;
1680}
Damien Miller4f7becb2006-03-26 14:10:14 +11001681
Damien Miller0e220db2004-06-15 10:34:08 +10001682static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001683channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001684{
Ben Lindstromb3921512001-04-11 15:57:50 +00001685 if (c->type == SSH_CHANNEL_OPEN &&
1686 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001687 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001688 c->local_maxpacket*3) ||
1689 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001690 c->local_consumed > 0) {
1691 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1692 packet_put_int(c->remote_id);
1693 packet_put_int(c->local_consumed);
1694 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001695 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001696 c->self, c->local_window,
1697 c->local_consumed);
1698 c->local_window += c->local_consumed;
1699 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001700 }
1701 return 1;
1702}
1703
Ben Lindstrombba81212001-06-25 05:01:22 +00001704static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001705channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001706{
Damien Miller4623a752001-10-10 15:03:58 +10001707 if (c->delayed)
1708 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001709 channel_handle_rfd(c, readset, writeset);
1710 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001711 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001712 return;
Damien Miller33b13562000-04-04 14:38:59 +10001713 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001714 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001715 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001716}
1717
Damien Miller8473dd82006-07-24 14:08:32 +10001718/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001719static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001720channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001721{
1722 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001723
Damien Millerb38eff82000-04-01 11:09:21 +10001724 /* Send buffered output data to the socket. */
1725 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1726 len = write(c->sock, buffer_ptr(&c->output),
1727 buffer_len(&c->output));
1728 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001729 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001730 else
1731 buffer_consume(&c->output, len);
1732 }
1733}
1734
Ben Lindstrombba81212001-06-25 05:01:22 +00001735static void
Damien Miller33b13562000-04-04 14:38:59 +10001736channel_handler_init_20(void)
1737{
Damien Millerde6987c2002-01-22 23:20:40 +11001738 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001739 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001740 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001741 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001742 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001743 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001744 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001745 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001746
Damien Millerde6987c2002-01-22 23:20:40 +11001747 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001748 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001749 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001750 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001751 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001752 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001753 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001754}
1755
Ben Lindstrombba81212001-06-25 05:01:22 +00001756static void
Damien Millerb38eff82000-04-01 11:09:21 +10001757channel_handler_init_13(void)
1758{
1759 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1760 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1761 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1762 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1763 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1764 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1765 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001766 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001767 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001768
Damien Millerde6987c2002-01-22 23:20:40 +11001769 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001770 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1771 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1772 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1773 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001774 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001775 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001776}
1777
Ben Lindstrombba81212001-06-25 05:01:22 +00001778static void
Damien Millerb38eff82000-04-01 11:09:21 +10001779channel_handler_init_15(void)
1780{
Damien Millerde6987c2002-01-22 23:20:40 +11001781 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001782 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001783 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1784 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1785 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001786 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001787 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001788
1789 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1790 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1791 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001792 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001793 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001794 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001795}
1796
Ben Lindstrombba81212001-06-25 05:01:22 +00001797static void
Damien Millerb38eff82000-04-01 11:09:21 +10001798channel_handler_init(void)
1799{
1800 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001801
Damien Miller9f0f5c62001-12-21 14:45:46 +11001802 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001803 channel_pre[i] = NULL;
1804 channel_post[i] = NULL;
1805 }
Damien Miller33b13562000-04-04 14:38:59 +10001806 if (compat20)
1807 channel_handler_init_20();
1808 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001809 channel_handler_init_13();
1810 else
1811 channel_handler_init_15();
1812}
1813
Damien Miller3ec27592001-10-12 11:35:04 +10001814/* gc dead channels */
1815static void
1816channel_garbage_collect(Channel *c)
1817{
1818 if (c == NULL)
1819 return;
1820 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001821 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001822 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001823 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001824 c->detach_user(c->self, NULL);
1825 /* if we still have a callback */
1826 if (c->detach_user != NULL)
1827 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001828 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001829 }
1830 if (!chan_is_dead(c, 1))
1831 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001832 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001833 channel_free(c);
1834}
1835
Ben Lindstrombba81212001-06-25 05:01:22 +00001836static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001837channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001838{
1839 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001840 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001841 Channel *c;
1842
1843 if (!did_init) {
1844 channel_handler_init();
1845 did_init = 1;
1846 }
1847 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001848 c = channels[i];
1849 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001850 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001851 if (ftab[c->type] != NULL)
1852 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001853 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001854 }
1855}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001856
Ben Lindstrome9c99912001-06-09 00:41:05 +00001857/*
1858 * Allocate/update select bitmasks and add any bits relevant to channels in
1859 * select bitmasks.
1860 */
Damien Miller4af51302000-04-16 11:18:38 +10001861void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001862channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001863 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864{
Damien Miller36812092006-03-26 14:22:47 +11001865 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001866
1867 n = MAX(*maxfdp, channel_max_fd);
1868
Damien Miller36812092006-03-26 14:22:47 +11001869 nfdset = howmany(n+1, NFDBITS);
1870 /* Explicitly test here, because xrealloc isn't always called */
1871 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1872 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1873 sz = nfdset * sizeof(fd_mask);
1874
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001875 /* perhaps check sz < nalloc/2 and shrink? */
1876 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001877 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1878 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001879 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001880 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001881 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001882 memset(*readsetp, 0, sz);
1883 memset(*writesetp, 0, sz);
1884
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001885 if (!rekeying)
1886 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001887}
1888
Ben Lindstrome9c99912001-06-09 00:41:05 +00001889/*
1890 * After select, perform any appropriate operations for channels which have
1891 * events pending.
1892 */
Damien Miller4af51302000-04-16 11:18:38 +10001893void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001894channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001895{
Damien Millerb38eff82000-04-01 11:09:21 +10001896 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897}
1898
Ben Lindstrome9c99912001-06-09 00:41:05 +00001899
Damien Miller5e953212001-01-30 09:14:00 +11001900/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001901void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001902channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001903{
Damien Millerb38eff82000-04-01 11:09:21 +10001904 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001905 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001906
Damien Miller95def091999-11-25 00:26:21 +11001907 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001908 c = channels[i];
1909 if (c == NULL)
1910 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001911
Ben Lindstrome9c99912001-06-09 00:41:05 +00001912 /*
1913 * We are only interested in channels that can have buffered
1914 * incoming data.
1915 */
Damien Miller34132e52000-01-14 15:45:46 +11001916 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001917 if (c->type != SSH_CHANNEL_OPEN &&
1918 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001919 continue;
1920 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001921 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001922 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001923 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001924 if (compat20 &&
1925 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001926 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001927 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001928 continue;
1929 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001930
Damien Miller95def091999-11-25 00:26:21 +11001931 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001932 if ((c->istate == CHAN_INPUT_OPEN ||
1933 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1934 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001935 if (c->datagram) {
1936 if (len > 0) {
1937 u_char *data;
1938 u_int dlen;
1939
1940 data = buffer_get_string(&c->input,
1941 &dlen);
1942 packet_start(SSH2_MSG_CHANNEL_DATA);
1943 packet_put_int(c->remote_id);
1944 packet_put_string(data, dlen);
1945 packet_send();
1946 c->remote_window -= dlen + 4;
1947 xfree(data);
1948 }
1949 continue;
1950 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001951 /*
1952 * Send some data for the other side over the secure
1953 * connection.
1954 */
Damien Miller33b13562000-04-04 14:38:59 +10001955 if (compat20) {
1956 if (len > c->remote_window)
1957 len = c->remote_window;
1958 if (len > c->remote_maxpacket)
1959 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001960 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001961 if (packet_is_interactive()) {
1962 if (len > 1024)
1963 len = 512;
1964 } else {
1965 /* Keep the packets at reasonable size. */
1966 if (len > packet_get_maxsize()/2)
1967 len = packet_get_maxsize()/2;
1968 }
Damien Miller95def091999-11-25 00:26:21 +11001969 }
Damien Millerb38eff82000-04-01 11:09:21 +10001970 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001971 packet_start(compat20 ?
1972 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001973 packet_put_int(c->remote_id);
1974 packet_put_string(buffer_ptr(&c->input), len);
1975 packet_send();
1976 buffer_consume(&c->input, len);
1977 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001978 }
1979 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001980 if (compat13)
1981 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001982 /*
1983 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001984 * tell peer, that we will not send more data: send IEOF.
1985 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001986 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001987 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001988 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1989 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001990 else
1991 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001992 }
Damien Miller33b13562000-04-04 14:38:59 +10001993 /* Send extended data, i.e. stderr */
1994 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001995 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001996 c->remote_window > 0 &&
1997 (len = buffer_len(&c->extended)) > 0 &&
1998 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001999 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002000 c->self, c->remote_window, buffer_len(&c->extended),
2001 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002002 if (len > c->remote_window)
2003 len = c->remote_window;
2004 if (len > c->remote_maxpacket)
2005 len = c->remote_maxpacket;
2006 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2007 packet_put_int(c->remote_id);
2008 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2009 packet_put_string(buffer_ptr(&c->extended), len);
2010 packet_send();
2011 buffer_consume(&c->extended, len);
2012 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002013 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002014 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002016}
2017
Ben Lindstrome9c99912001-06-09 00:41:05 +00002018
2019/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002020
2021/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002022void
Damien Miller630d6f42002-01-22 23:17:30 +11002023channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024{
Damien Miller34132e52000-01-14 15:45:46 +11002025 int id;
Damien Miller95def091999-11-25 00:26:21 +11002026 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002027 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002028 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002029
Damien Miller95def091999-11-25 00:26:21 +11002030 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002031 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002032 c = channel_lookup(id);
2033 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002034 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002035
Damien Miller95def091999-11-25 00:26:21 +11002036 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002037 if (c->type != SSH_CHANNEL_OPEN &&
2038 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002039 return;
2040
Damien Miller95def091999-11-25 00:26:21 +11002041 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002042 data = packet_get_string_ptr(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10002043
Damien Millera04ad492004-01-21 11:02:09 +11002044 /*
2045 * Ignore data for protocol > 1.3 if output end is no longer open.
2046 * For protocol 2 the sending side is reducing its window as it sends
2047 * data, so we must 'fake' consumption of the data in order to ensure
2048 * that window updates are sent back. Otherwise the connection might
2049 * deadlock.
2050 */
2051 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2052 if (compat20) {
2053 c->local_window -= data_len;
2054 c->local_consumed += data_len;
2055 }
Damien Millera04ad492004-01-21 11:02:09 +11002056 return;
2057 }
2058
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002059 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002060 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002061 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002062 c->self, data_len, c->local_maxpacket);
2063 }
2064 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002065 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002066 c->self, data_len, c->local_window);
Damien Miller33b13562000-04-04 14:38:59 +10002067 return;
2068 }
2069 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002070 }
Damien Millerd27b9472005-12-13 19:29:02 +11002071 if (c->datagram)
2072 buffer_put_string(&c->output, data, data_len);
2073 else
2074 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002075 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002076}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002077
Damien Millerd79b4242006-03-31 23:11:44 +11002078/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002079void
Damien Miller630d6f42002-01-22 23:17:30 +11002080channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002081{
2082 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002083 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002084 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002085 Channel *c;
2086
2087 /* Get the channel number and verify it. */
2088 id = packet_get_int();
2089 c = channel_lookup(id);
2090
2091 if (c == NULL)
2092 packet_disconnect("Received extended_data for bad channel %d.", id);
2093 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002094 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002095 return;
2096 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002097 if (c->flags & CHAN_EOF_RCVD) {
2098 if (datafellows & SSH_BUG_EXTEOF)
2099 debug("channel %d: accepting ext data after eof", id);
2100 else
2101 packet_disconnect("Received extended_data after EOF "
2102 "on channel %d.", id);
2103 }
Damien Miller33b13562000-04-04 14:38:59 +10002104 tcode = packet_get_int();
2105 if (c->efd == -1 ||
2106 c->extended_usage != CHAN_EXTENDED_WRITE ||
2107 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002108 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002109 return;
2110 }
2111 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002112 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002113 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002114 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002115 c->self, data_len, c->local_window);
2116 xfree(data);
2117 return;
2118 }
Damien Millerd3444942000-09-30 14:20:03 +11002119 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002120 c->local_window -= data_len;
2121 buffer_append(&c->extended, data, data_len);
2122 xfree(data);
2123}
2124
Damien Millerd79b4242006-03-31 23:11:44 +11002125/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002126void
Damien Miller630d6f42002-01-22 23:17:30 +11002127channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002128{
2129 int id;
2130 Channel *c;
2131
Damien Millerb38eff82000-04-01 11:09:21 +10002132 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002133 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002134 c = channel_lookup(id);
2135 if (c == NULL)
2136 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2137 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002138
2139 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002140 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002141 debug("channel %d: FORCE input drain", c->self);
2142 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002143 if (buffer_len(&c->input) == 0)
2144 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002145 }
2146
Damien Millerb38eff82000-04-01 11:09:21 +10002147}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002148
Damien Millerd79b4242006-03-31 23:11:44 +11002149/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002150void
Damien Miller630d6f42002-01-22 23:17:30 +11002151channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002152{
Damien Millerb38eff82000-04-01 11:09:21 +10002153 int id;
2154 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155
Damien Millerb38eff82000-04-01 11:09:21 +10002156 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002157 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002158 c = channel_lookup(id);
2159 if (c == NULL)
2160 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002161
2162 /*
2163 * Send a confirmation that we have closed the channel and no more
2164 * data is coming for it.
2165 */
Damien Miller95def091999-11-25 00:26:21 +11002166 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002167 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002168 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002169
Damien Miller5428f641999-11-25 11:54:57 +11002170 /*
2171 * If the channel is in closed state, we have sent a close request,
2172 * and the other side will eventually respond with a confirmation.
2173 * Thus, we cannot free the channel here, because then there would be
2174 * no-one to receive the confirmation. The channel gets freed when
2175 * the confirmation arrives.
2176 */
Damien Millerb38eff82000-04-01 11:09:21 +10002177 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002178 /*
2179 * Not a closed channel - mark it as draining, which will
2180 * cause it to be freed later.
2181 */
Damien Miller76765c02002-01-22 23:21:15 +11002182 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002183 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002184 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002185}
2186
Damien Millerb38eff82000-04-01 11:09:21 +10002187/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002188/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002189void
Damien Miller630d6f42002-01-22 23:17:30 +11002190channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191{
Damien Millerb38eff82000-04-01 11:09:21 +10002192 int id = packet_get_int();
2193 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002194
Damien Miller48b03fc2002-01-22 23:11:40 +11002195 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002196 if (c == NULL)
2197 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2198 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199}
2200
Damien Millerd79b4242006-03-31 23:11:44 +11002201/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002202void
Damien Miller630d6f42002-01-22 23:17:30 +11002203channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002204{
2205 int id = packet_get_int();
2206 Channel *c = channel_lookup(id);
2207
Damien Miller48b03fc2002-01-22 23:11:40 +11002208 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002209 if (c == NULL)
2210 packet_disconnect("Received close confirmation for "
2211 "out-of-range channel %d.", id);
2212 if (c->type != SSH_CHANNEL_CLOSED)
2213 packet_disconnect("Received close confirmation for "
2214 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002215 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002216}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002217
Damien Millerd79b4242006-03-31 23:11:44 +11002218/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002219void
Damien Miller630d6f42002-01-22 23:17:30 +11002220channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002221{
Damien Millerb38eff82000-04-01 11:09:21 +10002222 int id, remote_id;
2223 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002224
Damien Millerb38eff82000-04-01 11:09:21 +10002225 id = packet_get_int();
2226 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002227
Damien Millerb38eff82000-04-01 11:09:21 +10002228 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2229 packet_disconnect("Received open confirmation for "
2230 "non-opening channel %d.", id);
2231 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002232 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002233 c->remote_id = remote_id;
2234 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002235
2236 if (compat20) {
2237 c->remote_window = packet_get_int();
2238 c->remote_maxpacket = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002239 if (c->open_confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002240 debug2("callback start");
Damien Millerb84886b2008-05-19 15:05:07 +10002241 c->open_confirm(c->self, c->open_confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002242 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002243 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002244 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002245 c->remote_window, c->remote_maxpacket);
2246 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002247 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002248}
2249
Ben Lindstrombba81212001-06-25 05:01:22 +00002250static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002251reason2txt(int reason)
2252{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002253 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002254 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2255 return "administratively prohibited";
2256 case SSH2_OPEN_CONNECT_FAILED:
2257 return "connect failed";
2258 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2259 return "unknown channel type";
2260 case SSH2_OPEN_RESOURCE_SHORTAGE:
2261 return "resource shortage";
2262 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002263 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002264}
2265
Damien Millerd79b4242006-03-31 23:11:44 +11002266/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002267void
Damien Miller630d6f42002-01-22 23:17:30 +11002268channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002269{
Kevin Steves12057502001-02-05 14:54:34 +00002270 int id, reason;
2271 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002272 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002273
Damien Millerb38eff82000-04-01 11:09:21 +10002274 id = packet_get_int();
2275 c = channel_lookup(id);
2276
2277 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2278 packet_disconnect("Received open failure for "
2279 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002280 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002281 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002282 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002283 msg = packet_get_string(NULL);
2284 lang = packet_get_string(NULL);
2285 }
Damien Miller996acd22003-04-09 20:59:48 +10002286 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002287 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002288 if (msg != NULL)
2289 xfree(msg);
2290 if (lang != NULL)
2291 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002292 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002293 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002294 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002295 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296}
2297
Damien Millerd79b4242006-03-31 23:11:44 +11002298/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002299void
Damien Miller630d6f42002-01-22 23:17:30 +11002300channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002301{
2302 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002303 int id;
2304 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002305
2306 if (!compat20)
2307 return;
2308
2309 /* Get the channel number and verify it. */
2310 id = packet_get_int();
2311 c = channel_lookup(id);
2312
Damien Millerd47c62a2005-12-13 19:33:57 +11002313 if (c == NULL) {
2314 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002315 return;
2316 }
2317 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002318 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002319 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002320 c->remote_window += adjust;
2321}
2322
Damien Millerd79b4242006-03-31 23:11:44 +11002323/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002324void
Damien Miller630d6f42002-01-22 23:17:30 +11002325channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002326{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002327 Channel *c = NULL;
2328 u_short host_port;
2329 char *host, *originator_string;
2330 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002331
Ben Lindstrome9c99912001-06-09 00:41:05 +00002332 remote_id = packet_get_int();
2333 host = packet_get_string(NULL);
2334 host_port = packet_get_int();
2335
2336 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2337 originator_string = packet_get_string(NULL);
2338 } else {
2339 originator_string = xstrdup("unknown (remote did not supply name)");
2340 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002341 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002342 sock = channel_connect_to(host, host_port);
2343 if (sock != -1) {
2344 c = channel_new("connected socket",
2345 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2346 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002347 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002348 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002349 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002350 if (c == NULL) {
2351 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2352 packet_put_int(remote_id);
2353 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002354 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002355 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002356}
2357
Damien Millerb84886b2008-05-19 15:05:07 +10002358/* ARGSUSED */
2359void
2360channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2361{
2362 Channel *c;
2363 struct channel_confirm *cc;
2364 int remote_id;
2365
2366 /* Reset keepalive timeout */
2367 keep_alive_timeouts = 0;
2368
2369 remote_id = packet_get_int();
2370 packet_check_eom();
2371
2372 debug2("channel_input_confirm: type %d id %d", type, remote_id);
2373
2374 if ((c = channel_lookup(remote_id)) == NULL) {
2375 logit("channel_input_success_failure: %d: unknown", remote_id);
2376 return;
2377 }
2378 ;
2379 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
2380 return;
2381 cc->cb(type, c, cc->ctx);
2382 TAILQ_REMOVE(&c->status_confirms, cc, entry);
2383 bzero(cc, sizeof(*cc));
2384 xfree(cc);
2385}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002386
Ben Lindstrome9c99912001-06-09 00:41:05 +00002387/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002388
Ben Lindstrom908afed2001-10-03 17:34:59 +00002389void
2390channel_set_af(int af)
2391{
2392 IPv4or6 = af;
2393}
2394
Damien Millerb16461c2002-01-22 23:29:22 +11002395static int
2396channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2397 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002398{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002399 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002400 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002401 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002402 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002403 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002404
Damien Millerb16461c2002-01-22 23:29:22 +11002405 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2406 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002407 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002408
Damien Millerb16461c2002-01-22 23:29:22 +11002409 if (host == NULL) {
2410 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002411 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002412 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002413 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002414 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002415 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002416 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002417
Damien Miller5428f641999-11-25 11:54:57 +11002418 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002419 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002420 * specified address or wildcard. On the client, a specified bind
2421 * address will always override gateway_ports. On the server, a
2422 * gateway_ports of 1 (``yes'') will override the client's
2423 * specification and force a wildcard bind, whereas a value of 2
2424 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002425 * asked for.
2426 *
2427 * Special-case listen_addrs are:
2428 *
2429 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2430 * "" (empty string), "*" -> wildcard v4/v6
2431 * "localhost" -> loopback v4/v6
2432 */
2433 addr = NULL;
2434 if (listen_addr == NULL) {
2435 /* No address specified: default to gateway_ports setting */
2436 if (gateway_ports)
2437 wildcard = 1;
2438 } else if (gateway_ports || is_client) {
2439 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
Damien Miller3de49f82008-02-10 22:25:24 +11002440 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
Damien Millerf91ee4c2005-03-01 21:24:33 +11002441 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2442 (!is_client && gateway_ports == 1))
2443 wildcard = 1;
2444 else if (strcmp(listen_addr, "localhost") != 0)
2445 addr = listen_addr;
2446 }
2447
2448 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2449 type, wildcard, (addr == NULL) ? "NULL" : addr);
2450
2451 /*
Damien Miller34132e52000-01-14 15:45:46 +11002452 * getaddrinfo returns a loopback address if the hostname is
2453 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002454 */
Damien Miller34132e52000-01-14 15:45:46 +11002455 memset(&hints, 0, sizeof(hints));
2456 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002457 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002458 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002459 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002460 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2461 if (addr == NULL) {
2462 /* This really shouldn't happen */
2463 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11002464 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002465 } else {
Damien Millera7270302005-07-06 09:36:05 +10002466 error("channel_setup_fwd_listener: "
Darren Tucker4abde772007-12-29 02:43:51 +11002467 "getaddrinfo(%.64s): %s", addr,
2468 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11002469 }
Damien Millera7270302005-07-06 09:36:05 +10002470 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002471 }
Damien Miller5428f641999-11-25 11:54:57 +11002472
Damien Miller34132e52000-01-14 15:45:46 +11002473 for (ai = aitop; ai; ai = ai->ai_next) {
2474 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2475 continue;
2476 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2477 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002478 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002479 continue;
2480 }
2481 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002482 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002483 if (sock < 0) {
2484 /* this is no error since kernel may not support ipv6 */
2485 verbose("socket: %.100s", strerror(errno));
2486 continue;
2487 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002488
2489 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002490
Damien Miller34132e52000-01-14 15:45:46 +11002491 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002492
Damien Miller34132e52000-01-14 15:45:46 +11002493 /* Bind the socket to the address. */
2494 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2495 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002496 if (!ai->ai_next)
2497 error("bind: %.100s", strerror(errno));
2498 else
2499 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002500
Damien Miller34132e52000-01-14 15:45:46 +11002501 close(sock);
2502 continue;
2503 }
2504 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002505 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002506 error("listen: %.100s", strerror(errno));
2507 close(sock);
2508 continue;
2509 }
2510 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002511 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002512 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002513 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002514 strlcpy(c->path, host, sizeof(c->path));
2515 c->host_port = port_to_connect;
2516 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002517 success = 1;
2518 }
2519 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002520 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002521 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002522 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002523 return success;
Damien Miller95def091999-11-25 00:26:21 +11002524}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002525
Darren Tuckere7066df2004-05-24 10:18:05 +10002526int
2527channel_cancel_rport_listener(const char *host, u_short port)
2528{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002529 u_int i;
2530 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002531
Darren Tucker47eede72005-03-14 23:08:12 +11002532 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002533 Channel *c = channels[i];
2534
2535 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2536 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002537 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002538 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002539 channel_free(c);
2540 found = 1;
2541 }
2542 }
2543
2544 return (found);
2545}
2546
Damien Millerb16461c2002-01-22 23:29:22 +11002547/* protocol local port fwd, used by ssh (and sshd in v1) */
2548int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002549channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002550 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2551{
2552 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002553 listen_host, listen_port, host_to_connect, port_to_connect,
2554 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002555}
2556
2557/* protocol v2 remote port fwd, used by sshd */
2558int
2559channel_setup_remote_fwd_listener(const char *listen_address,
2560 u_short listen_port, int gateway_ports)
2561{
2562 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2563 listen_address, listen_port, NULL, 0, gateway_ports);
2564}
2565
Damien Miller5428f641999-11-25 11:54:57 +11002566/*
2567 * Initiate forwarding of connections to port "port" on remote host through
2568 * the secure channel to host:port from local side.
2569 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002570
Darren Tuckere7d4b192006-07-12 22:17:10 +10002571int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002572channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002573 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002574{
Damien Millerdff50992002-01-22 23:16:32 +11002575 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002576
Damien Miller95def091999-11-25 00:26:21 +11002577 /* Record locally that connection to this host/port is permitted. */
2578 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2579 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002580
Damien Miller95def091999-11-25 00:26:21 +11002581 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002582 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002583 const char *address_to_bind;
Damien Miller1ec46262007-01-05 16:26:45 +11002584 if (listen_host == NULL) {
2585 if (datafellows & SSH_BUG_RFWD_ADDR)
2586 address_to_bind = "127.0.0.1";
2587 else
2588 address_to_bind = "localhost";
2589 } else if (*listen_host == '\0' ||
2590 strcmp(listen_host, "*") == 0) {
2591 if (datafellows & SSH_BUG_RFWD_ADDR)
2592 address_to_bind = "0.0.0.0";
2593 else
2594 address_to_bind = "";
2595 } else
Damien Millerf91ee4c2005-03-01 21:24:33 +11002596 address_to_bind = listen_host;
2597
Damien Miller33b13562000-04-04 14:38:59 +10002598 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2599 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002600 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002601 packet_put_cstring(address_to_bind);
2602 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002603 packet_send();
2604 packet_write_wait();
2605 /* Assume that server accepts the request */
2606 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002607 } else {
2608 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002609 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002610 packet_put_cstring(host_to_connect);
2611 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002612 packet_send();
2613 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002614
2615 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002616 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002617 switch (type) {
2618 case SSH_SMSG_SUCCESS:
2619 success = 1;
2620 break;
2621 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002622 break;
2623 default:
2624 /* Unknown packet */
2625 packet_disconnect("Protocol error for port forward request:"
2626 "received packet type %d.", type);
2627 }
2628 }
2629 if (success) {
2630 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2631 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2632 permitted_opens[num_permitted_opens].listen_port = listen_port;
2633 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002634 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002635 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002636}
2637
Damien Miller5428f641999-11-25 11:54:57 +11002638/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002639 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002640 * local side.
2641 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002642void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002643channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002644{
2645 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002646
2647 if (!compat20)
2648 return;
2649
2650 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002651 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002652 permitted_opens[i].listen_port == port)
2653 break;
2654 }
2655 if (i >= num_permitted_opens) {
2656 debug("%s: requested forward not found", __func__);
2657 return;
2658 }
2659 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2660 packet_put_cstring("cancel-tcpip-forward");
2661 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002662 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002663 packet_put_int(port);
2664 packet_send();
2665
2666 permitted_opens[i].listen_port = 0;
2667 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002668 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002669 permitted_opens[i].host_to_connect = NULL;
2670}
2671
2672/*
Damien Miller5428f641999-11-25 11:54:57 +11002673 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2674 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002675 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002676 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002677int
Damien Millere247cc42000-05-07 12:03:14 +10002678channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002679{
Damien Milleraae6c611999-12-06 11:47:28 +11002680 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002681 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002682 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002683
Damien Miller95def091999-11-25 00:26:21 +11002684 /* Get arguments from the packet. */
2685 port = packet_get_int();
2686 hostname = packet_get_string(NULL);
2687 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002688
Damien Millerbac2d8a2000-09-05 16:13:06 +11002689#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002690 /*
2691 * Check that an unprivileged user is not trying to forward a
2692 * privileged port.
2693 */
Damien Miller95def091999-11-25 00:26:21 +11002694 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002695 packet_disconnect(
2696 "Requested forwarding of port %d but user is not root.",
2697 port);
2698 if (host_port == 0)
2699 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002700#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002701
Damien Miller0bc1bd82000-11-13 22:57:25 +11002702 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002703 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002704 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002705
2706 /* Free the argument string. */
2707 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002708
2709 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002710}
2711
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002712/*
2713 * Permits opening to any host/port if permitted_opens[] is empty. This is
2714 * usually called by the server, because the user could connect to any port
2715 * anyway, and the server has no way to know but to trust the client anyway.
2716 */
2717void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002718channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002719{
2720 if (num_permitted_opens == 0)
2721 all_opens_permitted = 1;
2722}
2723
Ben Lindstroma3700052001-04-05 23:26:32 +00002724void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002725channel_add_permitted_opens(char *host, int port)
2726{
2727 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002728 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002729 debug("allow port forwarding to host %s port %d", host, port);
2730
2731 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2732 permitted_opens[num_permitted_opens].port_to_connect = port;
2733 num_permitted_opens++;
2734
2735 all_opens_permitted = 0;
2736}
2737
Damien Millera765cf42006-07-24 14:08:13 +10002738int
Damien Miller9b439df2006-07-24 14:04:00 +10002739channel_add_adm_permitted_opens(char *host, int port)
2740{
2741 if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2742 fatal("channel_add_adm_permitted_opens: too many forwards");
Damien Millera765cf42006-07-24 14:08:13 +10002743 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10002744
2745 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2746 = xstrdup(host);
2747 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Millera765cf42006-07-24 14:08:13 +10002748 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10002749}
2750
2751void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002752channel_clear_permitted_opens(void)
2753{
2754 int i;
2755
2756 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002757 if (permitted_opens[i].host_to_connect != NULL)
2758 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002759 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10002760}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002761
Damien Miller9b439df2006-07-24 14:04:00 +10002762void
2763channel_clear_adm_permitted_opens(void)
2764{
2765 int i;
2766
2767 for (i = 0; i < num_adm_permitted_opens; i++)
2768 if (permitted_adm_opens[i].host_to_connect != NULL)
2769 xfree(permitted_adm_opens[i].host_to_connect);
2770 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002771}
2772
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002773/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002774static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002775connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002776{
Damien Miller34132e52000-01-14 15:45:46 +11002777 struct addrinfo hints, *ai, *aitop;
2778 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2779 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002780 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002781
Damien Miller34132e52000-01-14 15:45:46 +11002782 memset(&hints, 0, sizeof(hints));
2783 hints.ai_family = IPv4or6;
2784 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002785 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002786 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002787 error("connect_to %.100s: unknown host (%s)", host,
Darren Tucker4abde772007-12-29 02:43:51 +11002788 ssh_gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002789 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002790 }
Damien Miller34132e52000-01-14 15:45:46 +11002791 for (ai = aitop; ai; ai = ai->ai_next) {
2792 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2793 continue;
2794 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2795 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002796 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002797 continue;
2798 }
Damien Miller2372ace2003-05-14 13:42:23 +10002799 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002800 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002801 if (ai->ai_next == NULL)
2802 error("socket: %.100s", strerror(errno));
2803 else
2804 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002805 continue;
2806 }
Damien Miller232711f2004-06-15 10:35:30 +10002807 if (set_nonblock(sock) == -1)
2808 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002809 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2810 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002811 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002812 strerror(errno));
2813 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002814 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002815 }
2816 break; /* success */
2817
2818 }
2819 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002820 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002821 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002822 return -1;
2823 }
2824 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002825 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002826 return sock;
2827}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002828
Damien Miller0bc1bd82000-11-13 22:57:25 +11002829int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002830channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002831{
2832 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002833
Damien Miller0bc1bd82000-11-13 22:57:25 +11002834 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002835 if (permitted_opens[i].host_to_connect != NULL &&
2836 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002837 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002838 permitted_opens[i].host_to_connect,
2839 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002840 error("WARNING: Server requests forwarding for unknown listen_port %d",
2841 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002842 return -1;
2843}
Damien Millerb38eff82000-04-01 11:09:21 +10002844
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002845/* Check if connecting to that port is permitted and connect. */
2846int
2847channel_connect_to(const char *host, u_short port)
2848{
Damien Miller9b439df2006-07-24 14:04:00 +10002849 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002850
2851 permit = all_opens_permitted;
2852 if (!permit) {
2853 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002854 if (permitted_opens[i].host_to_connect != NULL &&
2855 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002856 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2857 permit = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002858 }
Damien Miller9b439df2006-07-24 14:04:00 +10002859
2860 if (num_adm_permitted_opens > 0) {
2861 permit_adm = 0;
2862 for (i = 0; i < num_adm_permitted_opens; i++)
2863 if (permitted_adm_opens[i].host_to_connect != NULL &&
2864 permitted_adm_opens[i].port_to_connect == port &&
2865 strcmp(permitted_adm_opens[i].host_to_connect, host)
2866 == 0)
2867 permit_adm = 1;
2868 }
2869
2870 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10002871 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002872 "but the request was denied.", host, port);
2873 return -1;
2874 }
2875 return connect_to(host, port);
2876}
2877
Damien Miller0e220db2004-06-15 10:34:08 +10002878void
2879channel_send_window_changes(void)
2880{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002881 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002882 struct winsize ws;
2883
2884 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002885 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002886 channels[i]->type != SSH_CHANNEL_OPEN)
2887 continue;
2888 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2889 continue;
2890 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002891 packet_put_int((u_int)ws.ws_col);
2892 packet_put_int((u_int)ws.ws_row);
2893 packet_put_int((u_int)ws.ws_xpixel);
2894 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002895 packet_send();
2896 }
2897}
2898
Ben Lindstrome9c99912001-06-09 00:41:05 +00002899/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002900
Damien Miller5428f641999-11-25 11:54:57 +11002901/*
2902 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002903 * Returns 0 and a suitable display number for the DISPLAY variable
2904 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002905 */
Kevin Steves366298c2001-12-19 17:58:01 +00002906int
Damien Miller95c249f2002-02-05 12:11:34 +11002907x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002908 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002909{
Damien Millere7378562001-12-21 14:58:35 +11002910 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002911 int display_number, sock;
2912 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002913 struct addrinfo hints, *ai, *aitop;
2914 char strport[NI_MAXSERV];
2915 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002916
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002917 if (chanids == NULL)
2918 return -1;
2919
Damien Millera34a28b1999-12-14 10:47:15 +11002920 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002921 display_number < MAX_DISPLAYS;
2922 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002923 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002924 memset(&hints, 0, sizeof(hints));
2925 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002926 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002927 hints.ai_socktype = SOCK_STREAM;
2928 snprintf(strport, sizeof strport, "%d", port);
2929 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11002930 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002931 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002932 }
Damien Miller34132e52000-01-14 15:45:46 +11002933 for (ai = aitop; ai; ai = ai->ai_next) {
2934 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2935 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002936 sock = socket(ai->ai_family, ai->ai_socktype,
2937 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002938 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002939 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002940 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002941 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002942 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002943 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002944 debug("x11_create_display_inet: Socket family %d not supported",
2945 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002946 continue;
2947 }
Damien Miller34132e52000-01-14 15:45:46 +11002948 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002949#ifdef IPV6_V6ONLY
2950 if (ai->ai_family == AF_INET6) {
2951 int on = 1;
2952 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2953 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2954 }
2955#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002956 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002957 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002958 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002959 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002960
Damien Miller34132e52000-01-14 15:45:46 +11002961 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002962 close(socks[n]);
2963 }
2964 num_socks = 0;
2965 break;
2966 }
2967 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002968#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002969 if (num_socks == NUM_SOCKS)
2970 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002971#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002972 if (x11_use_localhost) {
2973 if (num_socks == NUM_SOCKS)
2974 break;
2975 } else {
2976 break;
2977 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002978#endif
Damien Miller95def091999-11-25 00:26:21 +11002979 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002980 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002981 if (num_socks > 0)
2982 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002983 }
Damien Miller95def091999-11-25 00:26:21 +11002984 if (display_number >= MAX_DISPLAYS) {
2985 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002986 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002987 }
Damien Miller95def091999-11-25 00:26:21 +11002988 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002989 for (n = 0; n < num_socks; n++) {
2990 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002991 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002992 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002993 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002994 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002995 }
Damien Miller95def091999-11-25 00:26:21 +11002996 }
Damien Miller34132e52000-01-14 15:45:46 +11002997
Damien Miller34132e52000-01-14 15:45:46 +11002998 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002999 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11003000 for (n = 0; n < num_socks; n++) {
3001 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11003002 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10003003 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3004 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003005 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11003006 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003007 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11003008 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10003009 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003010
Kevin Steves366298c2001-12-19 17:58:01 +00003011 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003012 *display_numberp = display_number;
3013 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003014}
3015
Ben Lindstrombba81212001-06-25 05:01:22 +00003016static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00003017connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003018{
Damien Miller95def091999-11-25 00:26:21 +11003019 int sock;
3020 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003021
Damien Miller3afe3752001-12-21 12:39:51 +11003022 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3023 if (sock < 0)
3024 error("socket: %.100s", strerror(errno));
3025 memset(&addr, 0, sizeof(addr));
3026 addr.sun_family = AF_UNIX;
3027 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11003028 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11003029 return sock;
3030 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11003031 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
3032 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003033}
3034
Damien Millerbd483e72000-04-30 10:00:53 +10003035int
3036x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003037{
Damien Miller57c4e872006-03-31 23:11:07 +11003038 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11003039 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10003040 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11003041 struct addrinfo hints, *ai, *aitop;
3042 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11003043 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003044
Damien Miller95def091999-11-25 00:26:21 +11003045 /* Try to open a socket for the local X server. */
3046 display = getenv("DISPLAY");
3047 if (!display) {
3048 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10003049 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003050 }
Damien Miller5428f641999-11-25 11:54:57 +11003051 /*
3052 * Now we decode the value of the DISPLAY variable and make a
3053 * connection to the real X server.
3054 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003055
Damien Miller5428f641999-11-25 11:54:57 +11003056 /*
3057 * Check if it is a unix domain socket. Unix domain displays are in
3058 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
3059 */
Damien Miller95def091999-11-25 00:26:21 +11003060 if (strncmp(display, "unix:", 5) == 0 ||
3061 display[0] == ':') {
3062 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11003063 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003064 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003065 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003066 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003067 }
3068 /* Create a socket. */
3069 sock = connect_local_xsocket(display_number);
3070 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10003071 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003072
3073 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10003074 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003075 }
Damien Miller5428f641999-11-25 11:54:57 +11003076 /*
3077 * Connect to an inet socket. The DISPLAY value is supposedly
3078 * hostname:d[.s], where hostname may also be numeric IP address.
3079 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00003080 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11003081 cp = strchr(buf, ':');
3082 if (!cp) {
3083 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10003084 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003085 }
Damien Miller95def091999-11-25 00:26:21 +11003086 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11003087 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11003088 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003089 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003090 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003091 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003092 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003093
Damien Miller34132e52000-01-14 15:45:46 +11003094 /* Look up the host address */
3095 memset(&hints, 0, sizeof(hints));
3096 hints.ai_family = IPv4or6;
3097 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11003098 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11003099 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11003100 error("%.100s: unknown host. (%s)", buf,
3101 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10003102 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003103 }
Damien Miller34132e52000-01-14 15:45:46 +11003104 for (ai = aitop; ai; ai = ai->ai_next) {
3105 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10003106 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003107 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003108 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10003109 continue;
3110 }
3111 /* Connect it to the display. */
3112 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11003113 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10003114 6000 + display_number, strerror(errno));
3115 close(sock);
3116 continue;
3117 }
3118 /* Success */
3119 break;
Damien Miller34132e52000-01-14 15:45:46 +11003120 }
Damien Miller34132e52000-01-14 15:45:46 +11003121 freeaddrinfo(aitop);
3122 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11003123 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11003124 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003125 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003126 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003127 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003128 return sock;
3129}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003130
Damien Millerbd483e72000-04-30 10:00:53 +10003131/*
3132 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3133 * the remote channel number. We should do whatever we want, and respond
3134 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3135 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003136
Damien Miller8473dd82006-07-24 14:08:32 +10003137/* ARGSUSED */
Damien Millerbd483e72000-04-30 10:00:53 +10003138void
Damien Miller630d6f42002-01-22 23:17:30 +11003139x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003140{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003141 Channel *c = NULL;
3142 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003143 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003144
3145 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003146
3147 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003148
3149 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003150 remote_host = packet_get_string(NULL);
3151 } else {
3152 remote_host = xstrdup("unknown (remote did not supply name)");
3153 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003154 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003155
3156 /* Obtain a connection to the real X display. */
3157 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003158 if (sock != -1) {
3159 /* Allocate a channel for this connection. */
3160 c = channel_new("connected x11 socket",
3161 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3162 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003163 c->remote_id = remote_id;
3164 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003165 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003166 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003167 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003168 /* Send refusal to the remote host. */
3169 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003170 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003171 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003172 /* Send a confirmation to the remote host. */
3173 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003174 packet_put_int(remote_id);
3175 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003176 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003177 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003178}
3179
Damien Miller69b69aa2000-10-28 14:19:58 +11003180/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10003181/* ARGSUSED */
Damien Miller69b69aa2000-10-28 14:19:58 +11003182void
Damien Miller630d6f42002-01-22 23:17:30 +11003183deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003184{
3185 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003186
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003187 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003188 case SSH_SMSG_AGENT_OPEN:
3189 error("Warning: ssh server tried agent forwarding.");
3190 break;
3191 case SSH_SMSG_X11_OPEN:
3192 error("Warning: ssh server tried X11 forwarding.");
3193 break;
3194 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003195 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003196 break;
3197 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003198 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003199 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3200 packet_put_int(rchan);
3201 packet_send();
3202}
3203
Damien Miller5428f641999-11-25 11:54:57 +11003204/*
3205 * Requests forwarding of X11 connections, generates fake authentication
3206 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003207 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003208 */
Damien Miller4af51302000-04-16 11:18:38 +10003209void
Damien Miller17e7ed02005-06-17 12:54:33 +10003210x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003211 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003212{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003213 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003214 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003215 char *new_data;
3216 int screen_number;
3217 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003218 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003219
Damien Millerf92c0792005-07-06 09:45:26 +10003220 if (x11_saved_display == NULL)
3221 x11_saved_display = xstrdup(disp);
3222 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003223 error("x11_request_forwarding_with_spoofing: different "
3224 "$DISPLAY already forwarded");
3225 return;
3226 }
3227
Damien Millerd5fe0ba2006-08-30 11:07:39 +10003228 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003229 if (cp)
3230 cp = strchr(cp, '.');
3231 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003232 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003233 else
3234 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003235
Damien Miller13390022005-07-06 09:44:19 +10003236 if (x11_saved_proto == NULL) {
3237 /* Save protocol name. */
3238 x11_saved_proto = xstrdup(proto);
3239 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003240 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003241 * of the same length.
3242 */
3243 x11_saved_data = xmalloc(data_len);
3244 x11_fake_data = xmalloc(data_len);
3245 for (i = 0; i < data_len; i++) {
3246 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3247 fatal("x11_request_forwarding: bad "
3248 "authentication data: %.100s", data);
3249 if (i % 4 == 0)
3250 rnd = arc4random();
3251 x11_saved_data[i] = value;
3252 x11_fake_data[i] = rnd & 0xff;
3253 rnd >>= 8;
3254 }
3255 x11_saved_data_len = data_len;
3256 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003257 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003258
Damien Miller95def091999-11-25 00:26:21 +11003259 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003260 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003261
Damien Miller95def091999-11-25 00:26:21 +11003262 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003263 if (compat20) {
3264 channel_request_start(client_session_id, "x11-req", 0);
3265 packet_put_char(0); /* XXX bool single connection */
3266 } else {
3267 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3268 }
3269 packet_put_cstring(proto);
3270 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003271 packet_put_int(screen_number);
3272 packet_send();
3273 packet_write_wait();
3274 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003275}
3276
Ben Lindstrome9c99912001-06-09 00:41:05 +00003277
3278/* -- agent forwarding */
3279
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003280/* Sends a message to the server to request authentication fd forwarding. */
3281
Damien Miller4af51302000-04-16 11:18:38 +10003282void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003283auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003284{
Damien Miller95def091999-11-25 00:26:21 +11003285 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3286 packet_send();
3287 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003288}