blob: 11cdf1bb7a56ec3156c4f3cfe198087689bdd8d4 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains functions for generic socket connection forwarding.
6 * There is also code for initiating connection forwarding for X11 connections,
7 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millere4340be2000-09-16 13:29:08 +110017 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
18 * 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"
Ben Lindstrom2941f112000-12-29 16:50:13 +000043RCSID("$OpenBSD: channels.c,v 1.78 2000/12/29 11:05:55 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "ssh.h"
46#include "packet.h"
47#include "xmalloc.h"
48#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "uidswap.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110050#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "servconf.h"
52
53#include "channels.h"
54#include "nchan.h"
55#include "compat.h"
56
Damien Miller33b13562000-04-04 14:38:59 +100057#include "ssh2.h"
58
Damien Miller994cf142000-07-21 10:19:44 +100059#include <openssl/rsa.h>
60#include <openssl/dsa.h>
61#include "key.h"
62#include "authfd.h"
63
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064/* Maximum number of fake X11 displays to try. */
65#define MAX_DISPLAYS 1000
66
67/* Max len of agent socket */
68#define MAX_SOCKET_NAME 100
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * Pointer to an array containing all allocated channels. The array is
72 * dynamically extended as needed.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074static Channel *channels = NULL;
75
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Size of the channel array. All slots of the array must always be
78 * initialized (at least the type field); unused slots are marked with type
79 * SSH_CHANNEL_FREE.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int channels_alloc = 0;
82
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Maximum file descriptor value used in any of the channels. This is
85 * updated in channel_allocate.
86 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087static int channel_max_fd_value = 0;
88
89/* Name and directory of socket for authentication agent forwarding. */
90static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110091static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Saved X11 authentication protocol name. */
94char *x11_saved_proto = NULL;
95
96/* Saved X11 authentication data. This is the real data. */
97char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000098u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Fake X11 authentication data. This is what the server will be sending us;
102 * we should replace any occurrences of this by the real data.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Data structure for storing which hosts are permitted for forward requests.
109 * The local sides of any remote forwards are stored in this array to prevent
110 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
111 * network (which might be behind a firewall).
112 */
Damien Miller95def091999-11-25 00:26:21 +1100113typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000114 char *host_to_connect; /* Connect to 'host'. */
115 u_short port_to_connect; /* Connect to 'port'. */
116 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117} ForwardPermission;
118
119/* List of all permitted host/port pairs to connect. */
120static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
121/* Number of permitted host/port pairs in the array. */
122static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * If this is true, all opens are permitted. This is the case on the server
125 * on which we have to trust the client anyway, and the user could do
126 * anything after logging in anyway.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128static int all_opens_permitted = 0;
129
130/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
131static int have_hostname_in_open = 0;
132
133/* Sets specific protocol options. */
134
Damien Miller4af51302000-04-16 11:18:38 +1000135void
Damien Miller95def091999-11-25 00:26:21 +1100136channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137{
Damien Miller95def091999-11-25 00:26:21 +1100138 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139}
140
Damien Miller5428f641999-11-25 11:54:57 +1100141/*
142 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
143 * called by the server, because the user could connect to any port anyway,
144 * and the server has no way to know but to trust the client anyway.
145 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Miller4af51302000-04-16 11:18:38 +1000147void
Damien Miller95def091999-11-25 00:26:21 +1100148channel_permit_all_opens()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149{
Damien Miller95def091999-11-25 00:26:21 +1100150 all_opens_permitted = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151}
152
Damien Millerb38eff82000-04-01 11:09:21 +1000153/* lookup channel by id */
154
155Channel *
156channel_lookup(int id)
157{
158 Channel *c;
Damien Millerc0fd17f2000-06-26 10:22:53 +1000159 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000160 log("channel_lookup: %d: bad id", id);
161 return NULL;
162 }
163 c = &channels[id];
164 if (c->type == SSH_CHANNEL_FREE) {
165 log("channel_lookup: %d: bad id: channel free", id);
166 return NULL;
167 }
168 return c;
169}
170
Damien Miller5428f641999-11-25 11:54:57 +1100171/*
Damien Millere247cc42000-05-07 12:03:14 +1000172 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000173 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100174 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller6f83b8e2000-05-02 09:23:45 +1000176void
Damien Miller69b69aa2000-10-28 14:19:58 +1100177channel_register_fds(Channel *c, int rfd, int wfd, int efd,
178 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179{
Damien Miller95def091999-11-25 00:26:21 +1100180 /* Update the maximum file descriptor value. */
Damien Millerb38eff82000-04-01 11:09:21 +1000181 if (rfd > channel_max_fd_value)
182 channel_max_fd_value = rfd;
183 if (wfd > channel_max_fd_value)
184 channel_max_fd_value = wfd;
185 if (efd > channel_max_fd_value)
186 channel_max_fd_value = efd;
Damien Miller5428f641999-11-25 11:54:57 +1100187 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000188
Damien Miller6f83b8e2000-05-02 09:23:45 +1000189 c->rfd = rfd;
190 c->wfd = wfd;
191 c->sock = (rfd == wfd) ? rfd : -1;
192 c->efd = efd;
193 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100194
195 /* enable nonblocking mode */
196 if (nonblock) {
197 if (rfd != -1)
198 set_nonblock(rfd);
199 if (wfd != -1)
200 set_nonblock(wfd);
201 if (efd != -1)
202 set_nonblock(efd);
203 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000204}
205
206/*
207 * Allocate a new channel object and set its type and socket. This will cause
208 * remote_name to be freed.
209 */
210
211int
212channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100213 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000214{
215 int i, found;
216 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* Do initial allocation if this is the first call. */
219 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000220 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100221 channels_alloc = 10;
222 channels = xmalloc(channels_alloc * sizeof(Channel));
223 for (i = 0; i < channels_alloc; i++)
224 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100225 /*
226 * Kludge: arrange a call to channel_stop_listening if we
227 * terminate with fatal().
228 */
Damien Miller95def091999-11-25 00:26:21 +1100229 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
230 }
231 /* Try to find a free slot where to put the new channel. */
232 for (found = -1, i = 0; i < channels_alloc; i++)
233 if (channels[i].type == SSH_CHANNEL_FREE) {
234 /* Found a free slot. */
235 found = i;
236 break;
237 }
238 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100239 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100240 found = channels_alloc;
241 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100242 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100243 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
244 for (i = found; i < channels_alloc; i++)
245 channels[i].type = SSH_CHANNEL_FREE;
246 }
247 /* Initialize and return new channel number. */
248 c = &channels[found];
249 buffer_init(&c->input);
250 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000251 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100252 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100253 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100254 c->self = found;
255 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000256 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000257 c->local_window = window;
258 c->local_window_max = window;
259 c->local_consumed = 0;
260 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100261 c->remote_id = -1;
262 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000263 c->remote_window = 0;
264 c->remote_maxpacket = 0;
265 c->cb_fn = NULL;
266 c->cb_arg = NULL;
267 c->cb_event = 0;
268 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000269 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100270 debug("channel %d: new [%s]", found, remote_name);
271 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000273/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000274int
Damien Millerb38eff82000-04-01 11:09:21 +1000275channel_allocate(int type, int sock, char *remote_name)
276{
Damien Miller69b69aa2000-10-28 14:19:58 +1100277 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1);
Damien Millerb38eff82000-04-01 11:09:21 +1000278}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller6f83b8e2000-05-02 09:23:45 +1000280
281/* Close all channel fd/socket. */
282
283void
284channel_close_fds(Channel *c)
285{
286 if (c->sock != -1) {
287 close(c->sock);
288 c->sock = -1;
289 }
290 if (c->rfd != -1) {
291 close(c->rfd);
292 c->rfd = -1;
293 }
294 if (c->wfd != -1) {
295 close(c->wfd);
296 c->wfd = -1;
297 }
298 if (c->efd != -1) {
299 close(c->efd);
300 c->efd = -1;
301 }
302}
303
304/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
Damien Miller4af51302000-04-16 11:18:38 +1000306void
Damien Millerb38eff82000-04-01 11:09:21 +1000307channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308{
Damien Millerb38eff82000-04-01 11:09:21 +1000309 Channel *c = channel_lookup(id);
310 if (c == NULL)
311 packet_disconnect("channel free: bad local channel %d", id);
312 debug("channel_free: channel %d: status: %s", id, channel_open_message());
Damien Miller33b13562000-04-04 14:38:59 +1000313 if (c->dettach_user != NULL) {
314 debug("channel_free: channel %d: dettaching channel user", id);
315 c->dettach_user(c->self, NULL);
316 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000317 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000318 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000319 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000320 buffer_free(&c->input);
321 buffer_free(&c->output);
322 buffer_free(&c->extended);
323 c->type = SSH_CHANNEL_FREE;
324 if (c->remote_name) {
325 xfree(c->remote_name);
326 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100327 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
329
Damien Miller5428f641999-11-25 11:54:57 +1100330/*
Damien Millerb38eff82000-04-01 11:09:21 +1000331 * 'channel_pre*' are called just before select() to add any bits relevant to
332 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100333 */
Damien Millerb38eff82000-04-01 11:09:21 +1000334/*
335 * 'channel_post*': perform any appropriate operations for channels which
336 * have events pending.
337 */
338typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
339chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
340chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
341
342void
343channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
344{
345 FD_SET(c->sock, readset);
346}
347
348void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000349channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
350{
351 debug3("channel %d: waiting for connection", c->self);
352 FD_SET(c->sock, writeset);
353}
354
355void
Damien Millerb38eff82000-04-01 11:09:21 +1000356channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
357{
358 if (buffer_len(&c->input) < packet_get_maxsize())
359 FD_SET(c->sock, readset);
360 if (buffer_len(&c->output) > 0)
361 FD_SET(c->sock, writeset);
362}
363
364void
365channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
366{
367 /* test whether sockets are 'alive' for read/write */
368 if (c->istate == CHAN_INPUT_OPEN)
369 if (buffer_len(&c->input) < packet_get_maxsize())
370 FD_SET(c->sock, readset);
371 if (c->ostate == CHAN_OUTPUT_OPEN ||
372 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
373 if (buffer_len(&c->output) > 0) {
374 FD_SET(c->sock, writeset);
375 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
376 chan_obuf_empty(c);
377 }
378 }
379}
380
381void
Damien Miller33b13562000-04-04 14:38:59 +1000382channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
383{
384 if (c->istate == CHAN_INPUT_OPEN &&
385 c->remote_window > 0 &&
386 buffer_len(&c->input) < c->remote_window)
387 FD_SET(c->rfd, readset);
388 if (c->ostate == CHAN_OUTPUT_OPEN ||
389 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
390 if (buffer_len(&c->output) > 0) {
391 FD_SET(c->wfd, writeset);
392 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
393 chan_obuf_empty(c);
394 }
395 }
396 /** XXX check close conditions, too */
397 if (c->efd != -1) {
398 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
399 buffer_len(&c->extended) > 0)
400 FD_SET(c->efd, writeset);
401 else if (c->extended_usage == CHAN_EXTENDED_READ &&
402 buffer_len(&c->extended) < c->remote_window)
403 FD_SET(c->efd, readset);
404 }
405}
406
407void
Damien Millerb38eff82000-04-01 11:09:21 +1000408channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
409{
410 if (buffer_len(&c->input) == 0) {
411 packet_start(SSH_MSG_CHANNEL_CLOSE);
412 packet_put_int(c->remote_id);
413 packet_send();
414 c->type = SSH_CHANNEL_CLOSED;
415 debug("Closing channel %d after input drain.", c->self);
416 }
417}
418
419void
420channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
421{
422 if (buffer_len(&c->output) == 0)
423 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000424 else
Damien Millerb38eff82000-04-01 11:09:21 +1000425 FD_SET(c->sock, writeset);
426}
427
428/*
429 * This is a special state for X11 authentication spoofing. An opened X11
430 * connection (when authentication spoofing is being done) remains in this
431 * state until the first packet has been completely read. The authentication
432 * data in that packet is then substituted by the real data if it matches the
433 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000434 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000435 */
436int
437x11_open_helper(Channel *c)
438{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000439 u_char *ucp;
440 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000441
442 /* Check if the fixed size part of the packet is in buffer. */
443 if (buffer_len(&c->output) < 12)
444 return 0;
445
446 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000448 if (ucp[0] == 0x42) { /* Byte order MSB first. */
449 proto_len = 256 * ucp[6] + ucp[7];
450 data_len = 256 * ucp[8] + ucp[9];
451 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
452 proto_len = ucp[6] + 256 * ucp[7];
453 data_len = ucp[8] + 256 * ucp[9];
454 } else {
455 debug("Initial X11 packet contains bad byte order byte: 0x%x",
456 ucp[0]);
457 return -1;
458 }
459
460 /* Check if the whole packet is in buffer. */
461 if (buffer_len(&c->output) <
462 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
463 return 0;
464
465 /* Check if authentication protocol matches. */
466 if (proto_len != strlen(x11_saved_proto) ||
467 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
468 debug("X11 connection uses different authentication protocol.");
469 return -1;
470 }
471 /* Check if authentication data matches our fake data. */
472 if (data_len != x11_fake_data_len ||
473 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
474 x11_fake_data, x11_fake_data_len) != 0) {
475 debug("X11 auth data does not match fake data.");
476 return -1;
477 }
478 /* Check fake data length */
479 if (x11_fake_data_len != x11_saved_data_len) {
480 error("X11 fake_data_len %d != saved_data_len %d",
481 x11_fake_data_len, x11_saved_data_len);
482 return -1;
483 }
484 /*
485 * Received authentication protocol and data match
486 * our fake data. Substitute the fake data with real
487 * data.
488 */
489 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
490 x11_saved_data, x11_saved_data_len);
491 return 1;
492}
493
494void
495channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
496{
497 int ret = x11_open_helper(c);
498 if (ret == 1) {
499 /* Start normal processing for the channel. */
500 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000501 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000502 } else if (ret == -1) {
503 /*
504 * We have received an X11 connection that has bad
505 * authentication information.
506 */
507 log("X11 connection rejected because of wrong authentication.\r\n");
508 buffer_clear(&c->input);
509 buffer_clear(&c->output);
510 close(c->sock);
511 c->sock = -1;
512 c->type = SSH_CHANNEL_CLOSED;
513 packet_start(SSH_MSG_CHANNEL_CLOSE);
514 packet_put_int(c->remote_id);
515 packet_send();
516 }
517}
518
519void
Damien Millerbd483e72000-04-30 10:00:53 +1000520channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000521{
522 int ret = x11_open_helper(c);
523 if (ret == 1) {
524 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000525 if (compat20)
526 channel_pre_open_20(c, readset, writeset);
527 else
528 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000529 } else if (ret == -1) {
530 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000531 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000532 chan_write_failed(c);
533 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
534 }
535}
536
537/* This is our fake X11 server socket. */
538void
539channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
540{
541 struct sockaddr addr;
542 int newsock, newch;
543 socklen_t addrlen;
544 char buf[16384], *remote_hostname;
Damien Millerbd483e72000-04-30 10:00:53 +1000545 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000546
547 if (FD_ISSET(c->sock, readset)) {
548 debug("X11 connection requested.");
549 addrlen = sizeof(addr);
550 newsock = accept(c->sock, &addr, &addrlen);
551 if (newsock < 0) {
552 error("accept: %.100s", strerror(errno));
553 return;
554 }
555 remote_hostname = get_remote_hostname(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000556 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000557 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerbd483e72000-04-30 10:00:53 +1000558 remote_hostname, remote_port);
559
560 newch = channel_new("x11",
561 SSH_CHANNEL_OPENING, newsock, newsock, -1,
562 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100563 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000564 if (compat20) {
565 packet_start(SSH2_MSG_CHANNEL_OPEN);
566 packet_put_cstring("x11");
567 packet_put_int(newch);
568 packet_put_int(c->local_window_max);
569 packet_put_int(c->local_maxpacket);
570 /* originator host and port */
571 packet_put_cstring(remote_hostname);
Damien Miller30c3d422000-05-09 11:02:59 +1000572 if (datafellows & SSH_BUG_X11FWD) {
573 debug("ssh2 x11 bug compat mode");
574 } else {
575 packet_put_int(remote_port);
576 }
Damien Millerbd483e72000-04-30 10:00:53 +1000577 packet_send();
578 } else {
579 packet_start(SSH_SMSG_X11_OPEN);
580 packet_put_int(newch);
581 if (have_hostname_in_open)
582 packet_put_string(buf, strlen(buf));
583 packet_send();
584 }
Damien Millerb38eff82000-04-01 11:09:21 +1000585 xfree(remote_hostname);
Damien Millerb38eff82000-04-01 11:09:21 +1000586 }
587}
588
589/*
590 * This socket is listening for connections to a forwarded TCP/IP port.
591 */
592void
593channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
594{
595 struct sockaddr addr;
596 int newsock, newch;
597 socklen_t addrlen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100598 char buf[1024], *remote_hostname, *rtype;
Damien Millerb38eff82000-04-01 11:09:21 +1000599 int remote_port;
600
Damien Miller0bc1bd82000-11-13 22:57:25 +1100601 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
602 "forwarded-tcpip" : "direct-tcpip";
603
Damien Millerb38eff82000-04-01 11:09:21 +1000604 if (FD_ISSET(c->sock, readset)) {
605 debug("Connection to port %d forwarding "
606 "to %.100s port %d requested.",
607 c->listening_port, c->path, c->host_port);
608 addrlen = sizeof(addr);
609 newsock = accept(c->sock, &addr, &addrlen);
610 if (newsock < 0) {
611 error("accept: %.100s", strerror(errno));
612 return;
613 }
614 remote_hostname = get_remote_hostname(newsock);
615 remote_port = get_peer_port(newsock);
616 snprintf(buf, sizeof buf,
617 "listen port %d for %.100s port %d, "
618 "connect from %.200s port %d",
619 c->listening_port, c->path, c->host_port,
620 remote_hostname, remote_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100621
622 newch = channel_new(rtype,
Damien Millerb38eff82000-04-01 11:09:21 +1000623 SSH_CHANNEL_OPENING, newsock, newsock, -1,
624 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100625 0, xstrdup(buf), 1);
Damien Miller33b13562000-04-04 14:38:59 +1000626 if (compat20) {
627 packet_start(SSH2_MSG_CHANNEL_OPEN);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100628 packet_put_cstring(rtype);
Damien Miller33b13562000-04-04 14:38:59 +1000629 packet_put_int(newch);
630 packet_put_int(c->local_window_max);
631 packet_put_int(c->local_maxpacket);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100632 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
633 /* listen address, port */
634 packet_put_string(c->path, strlen(c->path));
635 packet_put_int(c->listening_port);
636 } else {
637 /* target host, port */
638 packet_put_string(c->path, strlen(c->path));
639 packet_put_int(c->host_port);
640 }
Damien Miller4af51302000-04-16 11:18:38 +1000641 /* originator host and port */
Damien Miller33b13562000-04-04 14:38:59 +1000642 packet_put_cstring(remote_hostname);
643 packet_put_int(remote_port);
644 packet_send();
645 } else {
646 packet_start(SSH_MSG_PORT_OPEN);
647 packet_put_int(newch);
648 packet_put_string(c->path, strlen(c->path));
649 packet_put_int(c->host_port);
650 if (have_hostname_in_open) {
651 packet_put_string(buf, strlen(buf));
652 }
653 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000654 }
Damien Millerb38eff82000-04-01 11:09:21 +1000655 xfree(remote_hostname);
656 }
657}
658
659/*
660 * This is the authentication agent socket listening for connections from
661 * clients.
662 */
663void
664channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
665{
666 struct sockaddr addr;
667 int newsock, newch;
668 socklen_t addrlen;
669
670 if (FD_ISSET(c->sock, readset)) {
671 addrlen = sizeof(addr);
672 newsock = accept(c->sock, &addr, &addrlen);
673 if (newsock < 0) {
674 error("accept from auth socket: %.100s", strerror(errno));
675 return;
676 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677 newch = channel_new("accepted auth socket",
678 SSH_CHANNEL_OPENING, newsock, newsock, -1,
679 c->local_window_max, c->local_maxpacket,
680 0, xstrdup("accepted auth socket"), 1);
681 if (compat20) {
682 packet_start(SSH2_MSG_CHANNEL_OPEN);
683 packet_put_cstring("auth-agent@openssh.com");
684 packet_put_int(newch);
685 packet_put_int(c->local_window_max);
686 packet_put_int(c->local_maxpacket);
687 } else {
688 packet_start(SSH_SMSG_AGENT_OPEN);
689 packet_put_int(newch);
690 }
Damien Millerb38eff82000-04-01 11:09:21 +1000691 packet_send();
692 }
693}
694
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000695void
696channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
697{
698 if (FD_ISSET(c->sock, writeset)) {
699 int err = 0;
700 int sz = sizeof(err);
701 c->type = SSH_CHANNEL_OPEN;
702 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
703 debug("getsockopt SO_ERROR failed");
704 } else {
705 if (err == 0) {
706 debug("channel %d: connected)", c->self);
707 } else {
708 debug("channel %d: not connected: %s",
709 c->self, strerror(err));
710 chan_read_failed(c);
711 chan_write_failed(c);
712 }
713 }
714 }
715}
716
Damien Millerb38eff82000-04-01 11:09:21 +1000717int
718channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
719{
720 char buf[16*1024];
721 int len;
722
723 if (c->rfd != -1 &&
724 FD_ISSET(c->rfd, readset)) {
725 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000726 if (len < 0 && (errno == EINTR || errno == EAGAIN))
727 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000728 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000729 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000730 c->self, c->rfd, len);
731 if (compat13) {
732 buffer_consume(&c->output, buffer_len(&c->output));
733 c->type = SSH_CHANNEL_INPUT_DRAINING;
734 debug("Channel %d status set to input draining.", c->self);
735 } else {
736 chan_read_failed(c);
737 }
738 return -1;
739 }
Damien Millerad833b32000-08-23 10:46:23 +1000740 if(c->input_filter != NULL) {
741 if (c->input_filter(c, buf, len) == -1) {
742 debug("filter stops channel %d", c->self);
743 chan_read_failed(c);
744 }
745 } else {
746 buffer_append(&c->input, buf, len);
747 }
Damien Millerb38eff82000-04-01 11:09:21 +1000748 }
749 return 1;
750}
751int
752channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
753{
754 int len;
755
756 /* Send buffered output data to the socket. */
757 if (c->wfd != -1 &&
758 FD_ISSET(c->wfd, writeset) &&
759 buffer_len(&c->output) > 0) {
760 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000761 buffer_len(&c->output));
762 if (len < 0 && (errno == EINTR || errno == EAGAIN))
763 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000764 if (len <= 0) {
765 if (compat13) {
766 buffer_consume(&c->output, buffer_len(&c->output));
767 debug("Channel %d status set to input draining.", c->self);
768 c->type = SSH_CHANNEL_INPUT_DRAINING;
769 } else {
770 chan_write_failed(c);
771 }
772 return -1;
773 }
774 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000775 if (compat20 && len > 0) {
776 c->local_consumed += len;
777 }
778 }
779 return 1;
780}
781int
782channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
783{
784 char buf[16*1024];
785 int len;
786
Damien Miller1383bd82000-04-06 12:32:37 +1000787/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000788 if (c->efd != -1) {
789 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
790 FD_ISSET(c->efd, writeset) &&
791 buffer_len(&c->extended) > 0) {
792 len = write(c->efd, buffer_ptr(&c->extended),
793 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100794 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000795 c->self, len, c->efd);
796 if (len > 0) {
797 buffer_consume(&c->extended, len);
798 c->local_consumed += len;
799 }
800 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
801 FD_ISSET(c->efd, readset)) {
802 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100803 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000804 c->self, len, c->efd);
Damien Miller1383bd82000-04-06 12:32:37 +1000805 if (len == 0) {
806 debug("channel %d: closing efd %d",
807 c->self, c->efd);
808 close(c->efd);
809 c->efd = -1;
810 } else if (len > 0)
Damien Miller33b13562000-04-04 14:38:59 +1000811 buffer_append(&c->extended, buf, len);
812 }
813 }
814 return 1;
815}
816int
817channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
818{
Damien Millerefb4afe2000-04-12 18:45:05 +1000819 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000820 c->local_window < c->local_window_max/2 &&
821 c->local_consumed > 0) {
822 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
823 packet_put_int(c->remote_id);
824 packet_put_int(c->local_consumed);
825 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +1100826 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +1000827 c->self, c->local_window,
828 c->local_consumed);
829 c->local_window += c->local_consumed;
830 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000831 }
832 return 1;
833}
834
835void
836channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
837{
838 channel_handle_rfd(c, readset, writeset);
839 channel_handle_wfd(c, readset, writeset);
840}
841
842void
Damien Miller33b13562000-04-04 14:38:59 +1000843channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
844{
845 channel_handle_rfd(c, readset, writeset);
846 channel_handle_wfd(c, readset, writeset);
847 channel_handle_efd(c, readset, writeset);
848 channel_check_window(c, readset, writeset);
849}
850
851void
Damien Millerb38eff82000-04-01 11:09:21 +1000852channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
853{
854 int len;
855 /* Send buffered output data to the socket. */
856 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
857 len = write(c->sock, buffer_ptr(&c->output),
858 buffer_len(&c->output));
859 if (len <= 0)
860 buffer_consume(&c->output, buffer_len(&c->output));
861 else
862 buffer_consume(&c->output, len);
863 }
864}
865
866void
Damien Miller33b13562000-04-04 14:38:59 +1000867channel_handler_init_20(void)
868{
869 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +1000870 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +1000871 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100872 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000873 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100874 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000875 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Miller33b13562000-04-04 14:38:59 +1000876
877 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
878 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100879 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000880 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100881 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000882 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Miller33b13562000-04-04 14:38:59 +1000883}
884
885void
Damien Millerb38eff82000-04-01 11:09:21 +1000886channel_handler_init_13(void)
887{
888 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
889 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
890 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
891 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
892 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
893 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
894 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000895 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000896
897 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
898 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
899 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
900 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
901 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000902 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000903}
904
905void
906channel_handler_init_15(void)
907{
908 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +1000909 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +1000910 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
911 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
912 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000913 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000914
915 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
916 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
917 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
918 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000919 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000920}
921
922void
923channel_handler_init(void)
924{
925 int i;
926 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
927 channel_pre[i] = NULL;
928 channel_post[i] = NULL;
929 }
Damien Miller33b13562000-04-04 14:38:59 +1000930 if (compat20)
931 channel_handler_init_20();
932 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000933 channel_handler_init_13();
934 else
935 channel_handler_init_15();
936}
937
Damien Miller4af51302000-04-16 11:18:38 +1000938void
Damien Millerb38eff82000-04-01 11:09:21 +1000939channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
940{
941 static int did_init = 0;
942 int i;
943 Channel *c;
944
945 if (!did_init) {
946 channel_handler_init();
947 did_init = 1;
948 }
949 for (i = 0; i < channels_alloc; i++) {
950 c = &channels[i];
951 if (c->type == SSH_CHANNEL_FREE)
952 continue;
953 if (ftab[c->type] == NULL)
954 continue;
955 (*ftab[c->type])(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +1000956 chan_delete_if_full_closed(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000957 }
958}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959
Damien Miller4af51302000-04-16 11:18:38 +1000960void
Damien Miller95def091999-11-25 00:26:21 +1100961channel_prepare_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962{
Damien Millerb38eff82000-04-01 11:09:21 +1000963 channel_handler(channel_pre, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964}
965
Damien Miller4af51302000-04-16 11:18:38 +1000966void
Damien Miller95def091999-11-25 00:26:21 +1100967channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968{
Damien Millerb38eff82000-04-01 11:09:21 +1000969 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970}
971
972/* If there is data to send to the connection, send some of it now. */
973
Damien Miller4af51302000-04-16 11:18:38 +1000974void
Damien Miller95def091999-11-25 00:26:21 +1100975channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976{
Damien Miller95def091999-11-25 00:26:21 +1100977 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000978 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000979
Damien Miller95def091999-11-25 00:26:21 +1100980 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +1000981 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +1100982
Damien Miller5428f641999-11-25 11:54:57 +1100983 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +1100984 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000985 if (c->type != SSH_CHANNEL_OPEN &&
986 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +1100987 continue;
988 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000989 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100990 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000991 if (c->istate != CHAN_INPUT_OPEN &&
992 c->istate != CHAN_INPUT_WAIT_DRAIN)
Damien Miller34132e52000-01-14 15:45:46 +1100993 continue;
994 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000995 if (compat20 &&
996 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Damien Miller33b13562000-04-04 14:38:59 +1000997 debug("channel: %d: no data after CLOSE", c->self);
998 continue;
999 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Damien Miller95def091999-11-25 00:26:21 +11001001 /* Get the amount of buffered data for this channel. */
Damien Millerb38eff82000-04-01 11:09:21 +10001002 len = buffer_len(&c->input);
Damien Miller95def091999-11-25 00:26:21 +11001003 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001004 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001005 if (compat20) {
1006 if (len > c->remote_window)
1007 len = c->remote_window;
1008 if (len > c->remote_maxpacket)
1009 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001010 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001011 if (packet_is_interactive()) {
1012 if (len > 1024)
1013 len = 512;
1014 } else {
1015 /* Keep the packets at reasonable size. */
1016 if (len > packet_get_maxsize()/2)
1017 len = packet_get_maxsize()/2;
1018 }
Damien Miller95def091999-11-25 00:26:21 +11001019 }
Damien Millerb38eff82000-04-01 11:09:21 +10001020 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001021 packet_start(compat20 ?
1022 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001023 packet_put_int(c->remote_id);
1024 packet_put_string(buffer_ptr(&c->input), len);
1025 packet_send();
1026 buffer_consume(&c->input, len);
1027 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001028 }
1029 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001030 if (compat13)
1031 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001032 /*
1033 * input-buffer is empty and read-socket shutdown:
1034 * tell peer, that we will not send more data: send IEOF
1035 */
Damien Millerb38eff82000-04-01 11:09:21 +10001036 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001037 }
Damien Miller33b13562000-04-04 14:38:59 +10001038 /* Send extended data, i.e. stderr */
1039 if (compat20 &&
1040 c->remote_window > 0 &&
1041 (len = buffer_len(&c->extended)) > 0 &&
1042 c->extended_usage == CHAN_EXTENDED_READ) {
1043 if (len > c->remote_window)
1044 len = c->remote_window;
1045 if (len > c->remote_maxpacket)
1046 len = c->remote_maxpacket;
1047 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1048 packet_put_int(c->remote_id);
1049 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1050 packet_put_string(buffer_ptr(&c->extended), len);
1051 packet_send();
1052 buffer_consume(&c->extended, len);
1053 c->remote_window -= len;
1054 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001055 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056}
1057
Damien Miller5428f641999-11-25 11:54:57 +11001058/*
1059 * This is called when a packet of type CHANNEL_DATA has just been received.
1060 * The message type has already been consumed, but channel number and data is
1061 * still there.
1062 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063
Damien Miller4af51302000-04-16 11:18:38 +10001064void
Damien Miller62cee002000-09-23 17:15:56 +11001065channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066{
Damien Miller34132e52000-01-14 15:45:46 +11001067 int id;
Damien Miller95def091999-11-25 00:26:21 +11001068 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001069 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001070 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071
Damien Miller95def091999-11-25 00:26:21 +11001072 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001073 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001074 c = channel_lookup(id);
1075 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001076 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077
Damien Miller95def091999-11-25 00:26:21 +11001078 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001079 if (c->type != SSH_CHANNEL_OPEN &&
1080 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001081 return;
1082
1083 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001084 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001085 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086
Damien Miller95def091999-11-25 00:26:21 +11001087 /* Get the data. */
1088 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001089 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001090
Damien Miller33b13562000-04-04 14:38:59 +10001091 if (compat20){
1092 if (data_len > c->local_maxpacket) {
1093 log("channel %d: rcvd big packet %d, maxpack %d",
1094 c->self, data_len, c->local_maxpacket);
1095 }
1096 if (data_len > c->local_window) {
1097 log("channel %d: rcvd too much data %d, win %d",
1098 c->self, data_len, c->local_window);
1099 xfree(data);
1100 return;
1101 }
1102 c->local_window -= data_len;
1103 }else{
1104 packet_integrity_check(plen, 4 + 4 + data_len, type);
1105 }
Damien Millerb38eff82000-04-01 11:09:21 +10001106 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001107 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108}
Damien Miller4af51302000-04-16 11:18:38 +10001109void
Damien Miller62cee002000-09-23 17:15:56 +11001110channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001111{
1112 int id;
1113 int tcode;
1114 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001115 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001116 Channel *c;
1117
1118 /* Get the channel number and verify it. */
1119 id = packet_get_int();
1120 c = channel_lookup(id);
1121
1122 if (c == NULL)
1123 packet_disconnect("Received extended_data for bad channel %d.", id);
1124 if (c->type != SSH_CHANNEL_OPEN) {
1125 log("channel %d: ext data for non open", id);
1126 return;
1127 }
1128 tcode = packet_get_int();
1129 if (c->efd == -1 ||
1130 c->extended_usage != CHAN_EXTENDED_WRITE ||
1131 tcode != SSH2_EXTENDED_DATA_STDERR) {
1132 log("channel %d: bad ext data", c->self);
1133 return;
1134 }
1135 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001136 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001137 if (data_len > c->local_window) {
1138 log("channel %d: rcvd too much extended_data %d, win %d",
1139 c->self, data_len, c->local_window);
1140 xfree(data);
1141 return;
1142 }
Damien Millerd3444942000-09-30 14:20:03 +11001143 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001144 c->local_window -= data_len;
1145 buffer_append(&c->extended, data, data_len);
1146 xfree(data);
1147}
1148
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001149
Damien Miller5428f641999-11-25 11:54:57 +11001150/*
1151 * Returns true if no channel has too much buffered data, and false if one or
1152 * more channel is overfull.
1153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001154
Damien Miller4af51302000-04-16 11:18:38 +10001155int
Damien Miller95def091999-11-25 00:26:21 +11001156channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001158 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001159 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001160
1161 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001162 c = &channels[i];
1163 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001164 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001165 debug("channel %d: big input buffer %d",
1166 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001167 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001168 }
1169 if (buffer_len(&c->output) > packet_get_maxsize()) {
1170 debug("channel %d: big output buffer %d",
1171 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001172 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001173 }
Damien Miller95def091999-11-25 00:26:21 +11001174 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175 }
Damien Miller95def091999-11-25 00:26:21 +11001176 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177}
1178
Damien Miller4af51302000-04-16 11:18:38 +10001179void
Damien Miller62cee002000-09-23 17:15:56 +11001180channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001181{
1182 int id;
1183 Channel *c;
1184
1185 packet_integrity_check(plen, 4, type);
1186
1187 id = packet_get_int();
1188 c = channel_lookup(id);
1189 if (c == NULL)
1190 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1191 chan_rcvd_ieof(c);
1192}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
Damien Miller4af51302000-04-16 11:18:38 +10001194void
Damien Miller62cee002000-09-23 17:15:56 +11001195channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001196{
Damien Millerb38eff82000-04-01 11:09:21 +10001197 int id;
1198 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199
Damien Millerb38eff82000-04-01 11:09:21 +10001200 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201
Damien Millerb38eff82000-04-01 11:09:21 +10001202 id = packet_get_int();
1203 c = channel_lookup(id);
1204 if (c == NULL)
1205 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001206
1207 /*
1208 * Send a confirmation that we have closed the channel and no more
1209 * data is coming for it.
1210 */
Damien Miller95def091999-11-25 00:26:21 +11001211 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001212 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001213 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214
Damien Miller5428f641999-11-25 11:54:57 +11001215 /*
1216 * If the channel is in closed state, we have sent a close request,
1217 * and the other side will eventually respond with a confirmation.
1218 * Thus, we cannot free the channel here, because then there would be
1219 * no-one to receive the confirmation. The channel gets freed when
1220 * the confirmation arrives.
1221 */
Damien Millerb38eff82000-04-01 11:09:21 +10001222 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001223 /*
1224 * Not a closed channel - mark it as draining, which will
1225 * cause it to be freed later.
1226 */
Damien Millerb38eff82000-04-01 11:09:21 +10001227 buffer_consume(&c->input, buffer_len(&c->input));
1228 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001229 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001230}
1231
Damien Millerb38eff82000-04-01 11:09:21 +10001232/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001233void
Damien Miller62cee002000-09-23 17:15:56 +11001234channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235{
Damien Millerb38eff82000-04-01 11:09:21 +10001236 int id = packet_get_int();
1237 Channel *c = channel_lookup(id);
1238 packet_integrity_check(plen, 4, type);
1239 if (c == NULL)
1240 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1241 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242}
1243
Damien Miller4af51302000-04-16 11:18:38 +10001244void
Damien Miller62cee002000-09-23 17:15:56 +11001245channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001246{
1247 int id = packet_get_int();
1248 Channel *c = channel_lookup(id);
1249
Damien Miller4af51302000-04-16 11:18:38 +10001250 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001251 if (c == NULL)
1252 packet_disconnect("Received close confirmation for "
1253 "out-of-range channel %d.", id);
1254 if (c->type != SSH_CHANNEL_CLOSED)
1255 packet_disconnect("Received close confirmation for "
1256 "non-closed channel %d (type %d).", id, c->type);
1257 channel_free(c->self);
1258}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259
Damien Miller4af51302000-04-16 11:18:38 +10001260void
Damien Miller62cee002000-09-23 17:15:56 +11001261channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001262{
Damien Millerb38eff82000-04-01 11:09:21 +10001263 int id, remote_id;
1264 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001265
Damien Miller33b13562000-04-04 14:38:59 +10001266 if (!compat20)
1267 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001268
Damien Millerb38eff82000-04-01 11:09:21 +10001269 id = packet_get_int();
1270 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271
Damien Millerb38eff82000-04-01 11:09:21 +10001272 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1273 packet_disconnect("Received open confirmation for "
1274 "non-opening channel %d.", id);
1275 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001276 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001277 c->remote_id = remote_id;
1278 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001279
1280 if (compat20) {
1281 c->remote_window = packet_get_int();
1282 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001283 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001284 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001285 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001286 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001287 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001288 }
1289 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1290 c->remote_window, c->remote_maxpacket);
1291 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292}
1293
Damien Miller4af51302000-04-16 11:18:38 +10001294void
Damien Miller62cee002000-09-23 17:15:56 +11001295channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296{
Damien Millerb38eff82000-04-01 11:09:21 +10001297 int id;
1298 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299
Damien Miller33b13562000-04-04 14:38:59 +10001300 if (!compat20)
1301 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001302
1303 id = packet_get_int();
1304 c = channel_lookup(id);
1305
1306 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1307 packet_disconnect("Received open failure for "
1308 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001309 if (compat20) {
1310 int reason = packet_get_int();
1311 char *msg = packet_get_string(NULL);
Damien Miller4af51302000-04-16 11:18:38 +10001312 char *lang = packet_get_string(NULL);
Damien Miller33b13562000-04-04 14:38:59 +10001313 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
Damien Miller4af51302000-04-16 11:18:38 +10001314 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001315 xfree(msg);
Damien Miller4af51302000-04-16 11:18:38 +10001316 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001317 }
Damien Miller95def091999-11-25 00:26:21 +11001318 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001319 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320}
1321
Damien Miller33b13562000-04-04 14:38:59 +10001322void
Damien Miller62cee002000-09-23 17:15:56 +11001323channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001324{
1325 int id;
1326 Channel *c;
1327
1328 id = packet_get_int();
1329 c = channel_lookup(id);
1330
1331 if (c == NULL ||
1332 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1333 packet_disconnect("Received request for "
1334 "non-open channel %d.", id);
1335 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001336 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001337 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001338 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001339 } else {
1340 char *service = packet_get_string(NULL);
1341 debug("channel: %d rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001342 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001343 xfree(service);
1344 }
1345}
1346
Damien Miller4af51302000-04-16 11:18:38 +10001347void
Damien Miller62cee002000-09-23 17:15:56 +11001348channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001349{
1350 Channel *c;
1351 int id, adjust;
1352
1353 if (!compat20)
1354 return;
1355
1356 /* Get the channel number and verify it. */
1357 id = packet_get_int();
1358 c = channel_lookup(id);
1359
1360 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1361 log("Received window adjust for "
1362 "non-open channel %d.", id);
1363 return;
1364 }
1365 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001366 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001367 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001368 c->remote_window += adjust;
1369}
1370
Damien Miller5428f641999-11-25 11:54:57 +11001371/*
1372 * Stops listening for channels, and removes any unix domain sockets that we
1373 * might have.
1374 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001375
Damien Miller4af51302000-04-16 11:18:38 +10001376void
Damien Miller95def091999-11-25 00:26:21 +11001377channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378{
Damien Miller95def091999-11-25 00:26:21 +11001379 int i;
1380 for (i = 0; i < channels_alloc; i++) {
1381 switch (channels[i].type) {
1382 case SSH_CHANNEL_AUTH_SOCKET:
1383 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001384 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001385 channel_free(i);
1386 break;
1387 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001388 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001389 case SSH_CHANNEL_X11_LISTENER:
1390 close(channels[i].sock);
1391 channel_free(i);
1392 break;
1393 default:
1394 break;
1395 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397}
1398
Damien Miller5428f641999-11-25 11:54:57 +11001399/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001400 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001401 * descriptors after a fork.
1402 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Miller4af51302000-04-16 11:18:38 +10001404void
Damien Miller95def091999-11-25 00:26:21 +11001405channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406{
Damien Miller95def091999-11-25 00:26:21 +11001407 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001408 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001409 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001410 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411}
1412
1413/* Returns the maximum file descriptor number used by the channels. */
1414
Damien Miller4af51302000-04-16 11:18:38 +10001415int
Damien Miller95def091999-11-25 00:26:21 +11001416channel_max_fd()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417{
Damien Miller95def091999-11-25 00:26:21 +11001418 return channel_max_fd_value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001419}
1420
1421/* Returns true if any channel is still open. */
1422
Damien Miller4af51302000-04-16 11:18:38 +10001423int
Damien Miller95def091999-11-25 00:26:21 +11001424channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001426 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001427 for (i = 0; i < channels_alloc; i++)
1428 switch (channels[i].type) {
1429 case SSH_CHANNEL_FREE:
1430 case SSH_CHANNEL_X11_LISTENER:
1431 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001432 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001433 case SSH_CHANNEL_CLOSED:
1434 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001435 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001436 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001437 case SSH_CHANNEL_LARVAL:
1438 if (!compat20)
1439 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1440 continue;
Damien Miller95def091999-11-25 00:26:21 +11001441 case SSH_CHANNEL_OPENING:
1442 case SSH_CHANNEL_OPEN:
1443 case SSH_CHANNEL_X11_OPEN:
1444 return 1;
1445 case SSH_CHANNEL_INPUT_DRAINING:
1446 case SSH_CHANNEL_OUTPUT_DRAINING:
1447 if (!compat13)
1448 fatal("cannot happen: OUT_DRAIN");
1449 return 1;
1450 default:
1451 fatal("channel_still_open: bad channel type %d", channels[i].type);
1452 /* NOTREACHED */
1453 }
1454 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455}
1456
Damien Miller5428f641999-11-25 11:54:57 +11001457/*
1458 * Returns a message describing the currently open forwarded connections,
1459 * suitable for sending to the client. The message contains crlf pairs for
1460 * newlines.
1461 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462
Damien Miller95def091999-11-25 00:26:21 +11001463char *
1464channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465{
Damien Miller95def091999-11-25 00:26:21 +11001466 Buffer buffer;
1467 int i;
1468 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
Damien Miller95def091999-11-25 00:26:21 +11001470 buffer_init(&buffer);
1471 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001473 for (i = 0; i < channels_alloc; i++) {
1474 Channel *c = &channels[i];
1475 switch (c->type) {
1476 case SSH_CHANNEL_FREE:
1477 case SSH_CHANNEL_X11_LISTENER:
1478 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001479 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001480 case SSH_CHANNEL_CLOSED:
1481 case SSH_CHANNEL_AUTH_SOCKET:
1482 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001483 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001484 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001485 case SSH_CHANNEL_CONNECTING:
Damien Miller95def091999-11-25 00:26:21 +11001486 case SSH_CHANNEL_OPEN:
1487 case SSH_CHANNEL_X11_OPEN:
1488 case SSH_CHANNEL_INPUT_DRAINING:
1489 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001490 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
Damien Miller34132e52000-01-14 15:45:46 +11001491 c->self, c->remote_name,
1492 c->type, c->remote_id,
1493 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001494 c->ostate, buffer_len(&c->output),
1495 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001496 buffer_append(&buffer, buf, strlen(buf));
1497 continue;
1498 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001499 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001500 /* NOTREACHED */
1501 }
1502 }
1503 buffer_append(&buffer, "\0", 1);
1504 cp = xstrdup(buffer_ptr(&buffer));
1505 buffer_free(&buffer);
1506 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001507}
1508
Damien Miller5428f641999-11-25 11:54:57 +11001509/*
1510 * Initiate forwarding of connections to local port "port" through the secure
1511 * channel to host:port from remote side.
1512 */
Damien Miller4af51302000-04-16 11:18:38 +10001513void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001514channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1515 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001517 channel_request_forwarding(
1518 NULL, listen_port,
1519 host_to_connect, port_to_connect,
1520 gateway_ports, /*remote_fwd*/ 0);
1521}
1522
1523/*
1524 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1525 * (SSH_CHANNEL_RPORT_LISTENER).
1526 */
1527void
1528channel_request_forwarding(
1529 const char *listen_address, u_short listen_port,
1530 const char *host_to_connect, u_short port_to_connect,
1531 int gateway_ports, int remote_fwd)
1532{
1533 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001534 struct addrinfo hints, *ai, *aitop;
1535 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001536 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001537 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001538
Damien Miller0bc1bd82000-11-13 22:57:25 +11001539 if (remote_fwd) {
1540 host = listen_address;
1541 ctype = SSH_CHANNEL_RPORT_LISTENER;
1542 } else {
1543 host = host_to_connect;
1544 ctype =SSH_CHANNEL_PORT_LISTENER;
1545 }
1546
Damien Miller95def091999-11-25 00:26:21 +11001547 if (strlen(host) > sizeof(channels[0].path) - 1)
1548 packet_disconnect("Forward host name too long.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001549
Damien Miller0bc1bd82000-11-13 22:57:25 +11001550 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001551 /*
Damien Miller34132e52000-01-14 15:45:46 +11001552 * getaddrinfo returns a loopback address if the hostname is
1553 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001554 */
Damien Miller34132e52000-01-14 15:45:46 +11001555 memset(&hints, 0, sizeof(hints));
1556 hints.ai_family = IPv4or6;
1557 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1558 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001559 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001560 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1561 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001562
Damien Miller34132e52000-01-14 15:45:46 +11001563 success = 0;
1564 for (ai = aitop; ai; ai = ai->ai_next) {
1565 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1566 continue;
1567 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1568 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001569 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001570 continue;
1571 }
1572 /* Create a port to listen for the host. */
1573 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1574 if (sock < 0) {
1575 /* this is no error since kernel may not support ipv6 */
1576 verbose("socket: %.100s", strerror(errno));
1577 continue;
1578 }
1579 /*
1580 * Set socket options. We would like the socket to disappear
1581 * as soon as it has been closed for whatever reason.
1582 */
1583 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1584 linger.l_onoff = 1;
1585 linger.l_linger = 5;
1586 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1587 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001588
Damien Miller34132e52000-01-14 15:45:46 +11001589 /* Bind the socket to the address. */
1590 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1591 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001592 if (!ai->ai_next)
1593 error("bind: %.100s", strerror(errno));
1594 else
1595 verbose("bind: %.100s", strerror(errno));
1596
Damien Miller34132e52000-01-14 15:45:46 +11001597 close(sock);
1598 continue;
1599 }
1600 /* Start listening for connections on the socket. */
1601 if (listen(sock, 5) < 0) {
1602 error("listen: %.100s", strerror(errno));
1603 close(sock);
1604 continue;
1605 }
1606 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001607 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001608 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001609 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001610 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001611 channels[ch].host_port = port_to_connect;
1612 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001613 success = 1;
1614 }
1615 if (success == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +11001616 packet_disconnect("cannot listen port: %d", listen_port); /*XXX ?disconnect? */
Damien Miller34132e52000-01-14 15:45:46 +11001617 freeaddrinfo(aitop);
Damien Miller95def091999-11-25 00:26:21 +11001618}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001619
Damien Miller5428f641999-11-25 11:54:57 +11001620/*
1621 * Initiate forwarding of connections to port "port" on remote host through
1622 * the secure channel to host:port from local side.
1623 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624
Damien Miller4af51302000-04-16 11:18:38 +10001625void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001626channel_request_remote_forwarding(u_short listen_port,
1627 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001628{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001629 int payload_len, type, success = 0;
1630
Damien Miller95def091999-11-25 00:26:21 +11001631 /* Record locally that connection to this host/port is permitted. */
1632 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1633 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001634
Damien Miller95def091999-11-25 00:26:21 +11001635 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001636 if (compat20) {
1637 const char *address_to_bind = "0.0.0.0";
1638 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1639 packet_put_cstring("tcpip-forward");
1640 packet_put_char(0); /* boolean: want reply */
1641 packet_put_cstring(address_to_bind);
1642 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001643 packet_send();
1644 packet_write_wait();
1645 /* Assume that server accepts the request */
1646 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001647 } else {
1648 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001649 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001650 packet_put_cstring(host_to_connect);
1651 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001652 packet_send();
1653 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001654
1655 /* Wait for response from the remote side. */
1656 type = packet_read(&payload_len);
1657 switch (type) {
1658 case SSH_SMSG_SUCCESS:
1659 success = 1;
1660 break;
1661 case SSH_SMSG_FAILURE:
1662 log("Warning: Server denied remote port forwarding.");
1663 break;
1664 default:
1665 /* Unknown packet */
1666 packet_disconnect("Protocol error for port forward request:"
1667 "received packet type %d.", type);
1668 }
1669 }
1670 if (success) {
1671 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1672 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1673 permitted_opens[num_permitted_opens].listen_port = listen_port;
1674 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001675 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001676}
1677
Damien Miller5428f641999-11-25 11:54:57 +11001678/*
1679 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1680 * listening for the port, and sends back a success reply (or disconnect
1681 * message if there was an error). This never returns if there was an error.
1682 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001683
Damien Miller4af51302000-04-16 11:18:38 +10001684void
Damien Millere247cc42000-05-07 12:03:14 +10001685channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001686{
Damien Milleraae6c611999-12-06 11:47:28 +11001687 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001688 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001689
Damien Miller95def091999-11-25 00:26:21 +11001690 /* Get arguments from the packet. */
1691 port = packet_get_int();
1692 hostname = packet_get_string(NULL);
1693 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001694
Damien Millerbac2d8a2000-09-05 16:13:06 +11001695#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001696 /*
1697 * Check that an unprivileged user is not trying to forward a
1698 * privileged port.
1699 */
Damien Miller95def091999-11-25 00:26:21 +11001700 if (port < IPPORT_RESERVED && !is_root)
1701 packet_disconnect("Requested forwarding of port %d but user is not root.",
1702 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001703#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001704 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001705 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001706
1707 /* Free the argument string. */
1708 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709}
1710
Damien Millerb38eff82000-04-01 11:09:21 +10001711/* XXX move to aux.c */
1712int
1713channel_connect_to(const char *host, u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714{
Damien Miller34132e52000-01-14 15:45:46 +11001715 struct addrinfo hints, *ai, *aitop;
1716 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1717 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001718 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001719
Damien Miller34132e52000-01-14 15:45:46 +11001720 memset(&hints, 0, sizeof(hints));
1721 hints.ai_family = IPv4or6;
1722 hints.ai_socktype = SOCK_STREAM;
1723 snprintf(strport, sizeof strport, "%d", host_port);
1724 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1725 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001726 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727 }
Damien Miller34132e52000-01-14 15:45:46 +11001728 for (ai = aitop; ai; ai = ai->ai_next) {
1729 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1730 continue;
1731 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1732 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001733 error("channel_connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001734 continue;
1735 }
1736 /* Create the socket. */
1737 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1738 if (sock < 0) {
1739 error("socket: %.100s", strerror(errno));
1740 continue;
1741 }
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001742 if (fcntl(sock, F_SETFL, O_NDELAY) < 0)
1743 fatal("connect_to: F_SETFL: %s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001744 /* Connect to the host/port. */
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001745 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
1746 errno != EINPROGRESS) {
Damien Miller34132e52000-01-14 15:45:46 +11001747 error("connect %.100s port %s: %.100s", ntop, strport,
1748 strerror(errno));
1749 close(sock);
1750 continue; /* fail -- try next */
1751 }
1752 break; /* success */
1753
1754 }
1755 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001756 if (!ai) {
1757 error("connect %.100s port %d: failed.", host, host_port);
Damien Millerb38eff82000-04-01 11:09:21 +10001758 return -1;
1759 }
1760 /* success */
1761 return sock;
1762}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001763int
1764channel_connect_by_listen_adress(u_short listen_port)
1765{
1766 int i;
1767 for (i = 0; i < num_permitted_opens; i++)
1768 if (permitted_opens[i].listen_port == listen_port)
1769 return channel_connect_to(
1770 permitted_opens[i].host_to_connect,
1771 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00001772 error("WARNING: Server requests forwarding for unknown listen_port %d",
1773 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774 return -1;
1775}
Damien Millerb38eff82000-04-01 11:09:21 +10001776
1777/*
1778 * This is called after receiving PORT_OPEN message. This attempts to
1779 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1780 * or CHANNEL_OPEN_FAILURE.
1781 */
1782
Damien Miller4af51302000-04-16 11:18:38 +10001783void
Damien Miller62cee002000-09-23 17:15:56 +11001784channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001785{
1786 u_short host_port;
1787 char *host, *originator_string;
1788 int remote_channel, sock = -1, newch, i, denied;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001789 u_int host_len, originator_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001790
1791 /* Get remote channel number. */
1792 remote_channel = packet_get_int();
1793
1794 /* Get host name to connect to. */
1795 host = packet_get_string(&host_len);
1796
1797 /* Get port to connect to. */
1798 host_port = packet_get_int();
1799
1800 /* Get remote originator name. */
1801 if (have_hostname_in_open) {
1802 originator_string = packet_get_string(&originator_len);
1803 originator_len += 4; /* size of packet_int */
1804 } else {
1805 originator_string = xstrdup("unknown (remote did not supply name)");
1806 originator_len = 0; /* no originator supplied */
Damien Miller95def091999-11-25 00:26:21 +11001807 }
Damien Miller34132e52000-01-14 15:45:46 +11001808
Damien Millerb38eff82000-04-01 11:09:21 +10001809 packet_integrity_check(plen,
1810 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Damien Millerb38eff82000-04-01 11:09:21 +10001812 /* Check if opening that port is permitted. */
1813 denied = 0;
1814 if (!all_opens_permitted) {
1815 /* Go trough all permitted ports. */
1816 for (i = 0; i < num_permitted_opens; i++)
1817 if (permitted_opens[i].port_to_connect == host_port &&
1818 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1819 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001820
Damien Millerb38eff82000-04-01 11:09:21 +10001821 /* Check if we found the requested port among those permitted. */
1822 if (i >= num_permitted_opens) {
1823 /* The port is not permitted. */
1824 log("Received request to connect to %.100s:%d, but the request was denied.",
1825 host, host_port);
1826 denied = 1;
1827 }
1828 }
1829 sock = denied ? -1 : channel_connect_to(host, host_port);
1830 if (sock > 0) {
1831 /* Allocate a channel for this connection. */
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001832 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
1833 sock, originator_string);
1834/*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10001835 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001836
Damien Millerb38eff82000-04-01 11:09:21 +10001837 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1838 packet_put_int(remote_channel);
1839 packet_put_int(newch);
1840 packet_send();
1841 } else {
1842 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1843 packet_put_int(remote_channel);
1844 packet_send();
1845 }
Damien Miller95def091999-11-25 00:26:21 +11001846 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001847}
1848
Damien Miller5428f641999-11-25 11:54:57 +11001849/*
1850 * Creates an internet domain socket for listening for X11 connections.
1851 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1852 * occurs.
1853 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001854
Damien Miller34132e52000-01-14 15:45:46 +11001855#define NUM_SOCKS 10
1856
Damien Miller95def091999-11-25 00:26:21 +11001857char *
Damien Millera34a28b1999-12-14 10:47:15 +11001858x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001859{
Damien Milleraae6c611999-12-06 11:47:28 +11001860 int display_number, sock;
1861 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001862 struct addrinfo hints, *ai, *aitop;
1863 char strport[NI_MAXSERV];
1864 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1865 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001866 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001867
Damien Millera34a28b1999-12-14 10:47:15 +11001868 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001869 display_number < MAX_DISPLAYS;
1870 display_number++) {
1871 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001872 memset(&hints, 0, sizeof(hints));
1873 hints.ai_family = IPv4or6;
1874 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1875 hints.ai_socktype = SOCK_STREAM;
1876 snprintf(strport, sizeof strport, "%d", port);
1877 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1878 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001879 return NULL;
1880 }
Damien Miller34132e52000-01-14 15:45:46 +11001881 for (ai = aitop; ai; ai = ai->ai_next) {
1882 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1883 continue;
1884 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1885 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11001886 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11001887 error("socket: %.100s", strerror(errno));
1888 return NULL;
1889 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11001890 debug("x11_create_display_inet: Socket family %d not supported",
1891 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11001892 continue;
1893 }
Damien Miller34132e52000-01-14 15:45:46 +11001894 }
1895 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1896 debug("bind port %d: %.100s", port, strerror(errno));
1897 shutdown(sock, SHUT_RDWR);
1898 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11001899
1900 if (ai->ai_next)
1901 continue;
1902
Damien Miller34132e52000-01-14 15:45:46 +11001903 for (n = 0; n < num_socks; n++) {
1904 shutdown(socks[n], SHUT_RDWR);
1905 close(socks[n]);
1906 }
1907 num_socks = 0;
1908 break;
1909 }
1910 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11001911#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11001912 if (num_socks == NUM_SOCKS)
1913 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11001914#else
1915 break;
1916#endif
Damien Miller95def091999-11-25 00:26:21 +11001917 }
Damien Miller34132e52000-01-14 15:45:46 +11001918 if (num_socks > 0)
1919 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920 }
Damien Miller95def091999-11-25 00:26:21 +11001921 if (display_number >= MAX_DISPLAYS) {
1922 error("Failed to allocate internet-domain X11 display socket.");
1923 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001924 }
Damien Miller95def091999-11-25 00:26:21 +11001925 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11001926 for (n = 0; n < num_socks; n++) {
1927 sock = socks[n];
1928 if (listen(sock, 5) < 0) {
1929 error("listen: %.100s", strerror(errno));
1930 shutdown(sock, SHUT_RDWR);
1931 close(sock);
1932 return NULL;
1933 }
Damien Miller95def091999-11-25 00:26:21 +11001934 }
Damien Miller34132e52000-01-14 15:45:46 +11001935
Damien Miller95def091999-11-25 00:26:21 +11001936 /* Set up a suitable value for the DISPLAY variable. */
1937 if (gethostname(hostname, sizeof(hostname)) < 0)
1938 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11001939
1940#ifdef IPADDR_IN_DISPLAY
1941 /*
1942 * HPUX detects the local hostname in the DISPLAY variable and tries
1943 * to set up a shared memory connection to the server, which it
1944 * incorrectly supposes to be local.
1945 *
1946 * The workaround - as used in later $$H and other programs - is
1947 * is to set display to the host's IP address.
1948 */
1949 {
1950 struct hostent *he;
1951 struct in_addr my_addr;
1952
1953 he = gethostbyname(hostname);
1954 if (he == NULL) {
1955 error("[X11-broken-fwd-hostname-workaround] Could not get "
1956 "IP address for hostname %s.", hostname);
1957
1958 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1959 "Could not get IP address for hostname %s.", hostname);
1960
1961 shutdown(sock, SHUT_RDWR);
1962 close(sock);
1963
1964 return NULL;
1965 }
1966
1967 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1968
1969 /* Set DISPLAY to <ip address>:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001970 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00001971 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11001972 }
1973#else /* IPADDR_IN_DISPLAY */
1974 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001975 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00001976 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11001977#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001978
Damien Miller34132e52000-01-14 15:45:46 +11001979 /* Allocate a channel for each socket. */
1980 for (n = 0; n < num_socks; n++) {
1981 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10001982 (void) channel_new("x11 listener",
1983 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
1984 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001985 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001986 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001987
Damien Miller95def091999-11-25 00:26:21 +11001988 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11001989 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001990}
1991
1992#ifndef X_UNIX_PATH
1993#define X_UNIX_PATH "/tmp/.X11-unix/X"
1994#endif
1995
1996static
1997int
Ben Lindstrom46c16222000-12-22 01:43:59 +00001998connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001999{
Damien Miller95def091999-11-25 00:26:21 +11002000 static const char *const x_sockets[] = {
2001 X_UNIX_PATH "%u",
2002 "/var/X/.X11-unix/X" "%u",
2003 "/usr/spool/sockets/X11/" "%u",
2004 NULL
2005 };
2006 int sock;
2007 struct sockaddr_un addr;
2008 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002009
Damien Miller95def091999-11-25 00:26:21 +11002010 for (path = x_sockets; *path; ++path) {
2011 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2012 if (sock < 0)
2013 error("socket: %.100s", strerror(errno));
2014 memset(&addr, 0, sizeof(addr));
2015 addr.sun_family = AF_UNIX;
2016 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2017 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2018 return sock;
2019 close(sock);
2020 }
2021 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2022 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002023}
2024
Damien Millerbd483e72000-04-30 10:00:53 +10002025int
2026x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002027{
Damien Millerbd483e72000-04-30 10:00:53 +10002028 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002029 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002030 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002031 struct addrinfo hints, *ai, *aitop;
2032 char strport[NI_MAXSERV];
2033 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002034
Damien Miller95def091999-11-25 00:26:21 +11002035 /* Try to open a socket for the local X server. */
2036 display = getenv("DISPLAY");
2037 if (!display) {
2038 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002039 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002040 }
Damien Miller5428f641999-11-25 11:54:57 +11002041 /*
2042 * Now we decode the value of the DISPLAY variable and make a
2043 * connection to the real X server.
2044 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002045
Damien Miller5428f641999-11-25 11:54:57 +11002046 /*
2047 * Check if it is a unix domain socket. Unix domain displays are in
2048 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2049 */
Damien Miller95def091999-11-25 00:26:21 +11002050 if (strncmp(display, "unix:", 5) == 0 ||
2051 display[0] == ':') {
2052 /* Connect to the unix domain socket. */
2053 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2054 error("Could not parse display number from DISPLAY: %.100s",
2055 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002056 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002057 }
2058 /* Create a socket. */
2059 sock = connect_local_xsocket(display_number);
2060 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002061 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002062
2063 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002064 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002065 }
Damien Miller5428f641999-11-25 11:54:57 +11002066 /*
2067 * Connect to an inet socket. The DISPLAY value is supposedly
2068 * hostname:d[.s], where hostname may also be numeric IP address.
2069 */
Damien Miller95def091999-11-25 00:26:21 +11002070 strncpy(buf, display, sizeof(buf));
2071 buf[sizeof(buf) - 1] = 0;
2072 cp = strchr(buf, ':');
2073 if (!cp) {
2074 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002075 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002076 }
Damien Miller95def091999-11-25 00:26:21 +11002077 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002078 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002079 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2080 error("Could not parse display number from DISPLAY: %.100s",
2081 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002082 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002083 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002084
Damien Miller34132e52000-01-14 15:45:46 +11002085 /* Look up the host address */
2086 memset(&hints, 0, sizeof(hints));
2087 hints.ai_family = IPv4or6;
2088 hints.ai_socktype = SOCK_STREAM;
2089 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2090 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2091 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002092 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002093 }
Damien Miller34132e52000-01-14 15:45:46 +11002094 for (ai = aitop; ai; ai = ai->ai_next) {
2095 /* Create a socket. */
2096 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2097 if (sock < 0) {
2098 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002099 continue;
2100 }
2101 /* Connect it to the display. */
2102 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2103 debug("connect %.100s port %d: %.100s", buf,
2104 6000 + display_number, strerror(errno));
2105 close(sock);
2106 continue;
2107 }
2108 /* Success */
2109 break;
Damien Miller34132e52000-01-14 15:45:46 +11002110 }
Damien Miller34132e52000-01-14 15:45:46 +11002111 freeaddrinfo(aitop);
2112 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002113 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002114 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002115 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002116 }
Damien Millerbd483e72000-04-30 10:00:53 +10002117 return sock;
2118}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119
Damien Millerbd483e72000-04-30 10:00:53 +10002120/*
2121 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2122 * the remote channel number. We should do whatever we want, and respond
2123 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125
Damien Millerbd483e72000-04-30 10:00:53 +10002126void
Damien Miller62cee002000-09-23 17:15:56 +11002127x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002128{
2129 int remote_channel, sock = 0, newch;
2130 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002131 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002132
Damien Millerbd483e72000-04-30 10:00:53 +10002133 /* Get remote channel number. */
2134 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002135
Damien Millerbd483e72000-04-30 10:00:53 +10002136 /* Get remote originator name. */
2137 if (have_hostname_in_open) {
2138 remote_host = packet_get_string(&remote_len);
2139 remote_len += 4;
2140 } else {
2141 remote_host = xstrdup("unknown (remote did not supply name)");
2142 remote_len = 0;
2143 }
2144
2145 debug("Received X11 open request.");
2146 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2147
2148 /* Obtain a connection to the real X display. */
2149 sock = x11_connect_display();
2150 if (sock == -1) {
2151 /* Send refusal to the remote host. */
2152 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2153 packet_put_int(remote_channel);
2154 packet_send();
2155 } else {
2156 /* Allocate a channel for this connection. */
2157 newch = channel_allocate(
2158 (x11_saved_proto == NULL) ?
2159 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2160 sock, remote_host);
2161 channels[newch].remote_id = remote_channel;
2162
2163 /* Send a confirmation to the remote host. */
2164 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2165 packet_put_int(remote_channel);
2166 packet_put_int(newch);
2167 packet_send();
2168 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002169}
2170
Damien Miller69b69aa2000-10-28 14:19:58 +11002171/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2172void
2173deny_input_open(int type, int plen, void *ctxt)
2174{
2175 int rchan = packet_get_int();
2176 switch(type){
2177 case SSH_SMSG_AGENT_OPEN:
2178 error("Warning: ssh server tried agent forwarding.");
2179 break;
2180 case SSH_SMSG_X11_OPEN:
2181 error("Warning: ssh server tried X11 forwarding.");
2182 break;
2183 default:
2184 error("deny_input_open: type %d plen %d", type, plen);
2185 break;
2186 }
2187 error("Warning: this is probably a break in attempt by a malicious server.");
2188 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2189 packet_put_int(rchan);
2190 packet_send();
2191}
2192
Damien Miller5428f641999-11-25 11:54:57 +11002193/*
2194 * Requests forwarding of X11 connections, generates fake authentication
2195 * data, and enables authentication spoofing.
2196 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002197
Damien Miller4af51302000-04-16 11:18:38 +10002198void
Damien Millerbd483e72000-04-30 10:00:53 +10002199x11_request_forwarding_with_spoofing(int client_session_id,
2200 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002201{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002202 u_int data_len = (u_int) strlen(data) / 2;
2203 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11002204 char *new_data;
2205 int screen_number;
2206 const char *cp;
2207 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002208
Damien Miller95def091999-11-25 00:26:21 +11002209 cp = getenv("DISPLAY");
2210 if (cp)
2211 cp = strchr(cp, ':');
2212 if (cp)
2213 cp = strchr(cp, '.');
2214 if (cp)
2215 screen_number = atoi(cp + 1);
2216 else
2217 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002218
Damien Miller95def091999-11-25 00:26:21 +11002219 /* Save protocol name. */
2220 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002221
Damien Miller5428f641999-11-25 11:54:57 +11002222 /*
2223 * Extract real authentication data and generate fake data of the
2224 * same length.
2225 */
Damien Miller95def091999-11-25 00:26:21 +11002226 x11_saved_data = xmalloc(data_len);
2227 x11_fake_data = xmalloc(data_len);
2228 for (i = 0; i < data_len; i++) {
2229 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2230 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2231 if (i % 4 == 0)
2232 rand = arc4random();
2233 x11_saved_data[i] = value;
2234 x11_fake_data[i] = rand & 0xff;
2235 rand >>= 8;
2236 }
2237 x11_saved_data_len = data_len;
2238 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002239
Damien Miller95def091999-11-25 00:26:21 +11002240 /* Convert the fake data into hex. */
2241 new_data = xmalloc(2 * data_len + 1);
2242 for (i = 0; i < data_len; i++)
Ben Lindstrom46c16222000-12-22 01:43:59 +00002243 sprintf(new_data + 2 * i, "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002244
Damien Miller95def091999-11-25 00:26:21 +11002245 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002246 if (compat20) {
2247 channel_request_start(client_session_id, "x11-req", 0);
2248 packet_put_char(0); /* XXX bool single connection */
2249 } else {
2250 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2251 }
2252 packet_put_cstring(proto);
2253 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002254 packet_put_int(screen_number);
2255 packet_send();
2256 packet_write_wait();
2257 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002258}
2259
2260/* Sends a message to the server to request authentication fd forwarding. */
2261
Damien Miller4af51302000-04-16 11:18:38 +10002262void
Damien Miller95def091999-11-25 00:26:21 +11002263auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002264{
Damien Miller95def091999-11-25 00:26:21 +11002265 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2266 packet_send();
2267 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268}
2269
Damien Miller5428f641999-11-25 11:54:57 +11002270/*
2271 * Returns the name of the forwarded authentication socket. Returns NULL if
2272 * there is no forwarded authentication socket. The returned value points to
2273 * a static buffer.
2274 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002275
Damien Miller95def091999-11-25 00:26:21 +11002276char *
2277auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002278{
Damien Miller95def091999-11-25 00:26:21 +11002279 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002280}
2281
2282/* removes the agent forwarding socket */
2283
Damien Miller4af51302000-04-16 11:18:38 +10002284void
Damien Miller95def091999-11-25 00:26:21 +11002285cleanup_socket(void)
2286{
Damien Miller78315eb2000-09-29 23:01:36 +11002287 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002288 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002289}
2290
Damien Miller5428f641999-11-25 11:54:57 +11002291/*
Damien Millerd3a18572000-06-07 19:55:44 +10002292 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002293 * This starts forwarding authentication requests.
2294 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002295
Damien Millerd3a18572000-06-07 19:55:44 +10002296int
Damien Miller95def091999-11-25 00:26:21 +11002297auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002298{
Damien Miller95def091999-11-25 00:26:21 +11002299 int sock, newch;
2300 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002301
Damien Miller95def091999-11-25 00:26:21 +11002302 if (auth_get_socket_name() != NULL)
2303 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002304
Damien Miller95def091999-11-25 00:26:21 +11002305 /* Temporarily drop privileged uid for mkdir/bind. */
2306 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002307
Damien Miller95def091999-11-25 00:26:21 +11002308 /* Allocate a buffer for the socket name, and format the name. */
2309 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2310 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2311 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002312
Damien Miller95def091999-11-25 00:26:21 +11002313 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002314 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2315 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2316 strerror(errno));
2317 restore_uid();
2318 xfree(channel_forwarded_auth_socket_name);
2319 xfree(channel_forwarded_auth_socket_dir);
2320 channel_forwarded_auth_socket_name = NULL;
2321 channel_forwarded_auth_socket_dir = NULL;
2322 return 0;
2323 }
Damien Miller95def091999-11-25 00:26:21 +11002324 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2325 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002326
Damien Miller95def091999-11-25 00:26:21 +11002327 if (atexit(cleanup_socket) < 0) {
2328 int saved = errno;
2329 cleanup_socket();
2330 packet_disconnect("socket: %.100s", strerror(saved));
2331 }
2332 /* Create the socket. */
2333 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2334 if (sock < 0)
2335 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002336
Damien Miller95def091999-11-25 00:26:21 +11002337 /* Bind it to the name. */
2338 memset(&sunaddr, 0, sizeof(sunaddr));
2339 sunaddr.sun_family = AF_UNIX;
2340 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2341 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002342
Damien Miller95def091999-11-25 00:26:21 +11002343 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2344 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002345
Damien Miller95def091999-11-25 00:26:21 +11002346 /* Restore the privileged uid. */
2347 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002348
Damien Miller95def091999-11-25 00:26:21 +11002349 /* Start listening on the socket. */
2350 if (listen(sock, 5) < 0)
2351 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002352
Damien Miller95def091999-11-25 00:26:21 +11002353 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002354 newch = channel_new("auth socket",
2355 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2356 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2357 0, xstrdup("auth socket"), 1);
2358
Damien Miller037a0dc1999-12-07 15:38:31 +11002359 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2360 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002361 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002362}
2363
2364/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2365
Damien Miller4af51302000-04-16 11:18:38 +10002366void
Damien Miller62cee002000-09-23 17:15:56 +11002367auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002368{
Damien Miller95def091999-11-25 00:26:21 +11002369 int remch, sock, newch;
2370 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002371
Damien Millerb38eff82000-04-01 11:09:21 +10002372 packet_integrity_check(plen, 4, type);
2373
Damien Miller95def091999-11-25 00:26:21 +11002374 /* Read the remote channel number from the message. */
2375 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002376
Damien Miller5428f641999-11-25 11:54:57 +11002377 /*
2378 * Get a connection to the local authentication agent (this may again
2379 * get forwarded).
2380 */
Damien Miller95def091999-11-25 00:26:21 +11002381 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002382
Damien Miller5428f641999-11-25 11:54:57 +11002383 /*
2384 * If we could not connect the agent, send an error message back to
2385 * the server. This should never happen unless the agent dies,
2386 * because authentication forwarding is only enabled if we have an
2387 * agent.
2388 */
Damien Miller95def091999-11-25 00:26:21 +11002389 if (sock < 0) {
2390 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2391 packet_put_int(remch);
2392 packet_send();
2393 return;
2394 }
2395 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002396
Damien Miller5428f641999-11-25 11:54:57 +11002397 /*
2398 * Dummy host name. This will be freed when the channel is freed; it
2399 * will still be valid in the packet_put_string below since the
2400 * channel cannot yet be freed at that point.
2401 */
Damien Miller95def091999-11-25 00:26:21 +11002402 dummyname = xstrdup("authentication agent connection");
2403
2404 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2405 channels[newch].remote_id = remch;
2406
2407 /* Send a confirmation to the remote host. */
2408 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2409 packet_put_int(remch);
2410 packet_put_int(newch);
2411 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002412}
Damien Miller33b13562000-04-04 14:38:59 +10002413
2414void
Damien Millerbd483e72000-04-30 10:00:53 +10002415channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002416{
2417 Channel *c = channel_lookup(id);
2418 if (c == NULL) {
2419 log("channel_open: %d: bad id", id);
2420 return;
2421 }
Damien Millerbd483e72000-04-30 10:00:53 +10002422 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002423 packet_start(SSH2_MSG_CHANNEL_OPEN);
2424 packet_put_cstring(c->ctype);
2425 packet_put_int(c->self);
2426 packet_put_int(c->local_window);
2427 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002428}
2429void
2430channel_open(int id)
2431{
2432 /* XXX REMOVE ME */
2433 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002434 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002435}
2436void
2437channel_request(int id, char *service, int wantconfirm)
2438{
2439 channel_request_start(id, service, wantconfirm);
2440 packet_send();
2441 debug("channel request %d: %s", id, service) ;
2442}
2443void
2444channel_request_start(int id, char *service, int wantconfirm)
2445{
2446 Channel *c = channel_lookup(id);
2447 if (c == NULL) {
2448 log("channel_request: %d: bad id", id);
2449 return;
2450 }
2451 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2452 packet_put_int(c->remote_id);
2453 packet_put_cstring(service);
2454 packet_put_char(wantconfirm);
2455}
2456void
2457channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2458{
2459 Channel *c = channel_lookup(id);
2460 if (c == NULL) {
2461 log("channel_register_callback: %d: bad id", id);
2462 return;
2463 }
2464 c->cb_event = mtype;
2465 c->cb_fn = fn;
2466 c->cb_arg = arg;
2467}
2468void
2469channel_register_cleanup(int id, channel_callback_fn *fn)
2470{
2471 Channel *c = channel_lookup(id);
2472 if (c == NULL) {
2473 log("channel_register_cleanup: %d: bad id", id);
2474 return;
2475 }
2476 c->dettach_user = fn;
2477}
2478void
2479channel_cancel_cleanup(int id)
2480{
2481 Channel *c = channel_lookup(id);
2482 if (c == NULL) {
2483 log("channel_cancel_cleanup: %d: bad id", id);
2484 return;
2485 }
2486 c->dettach_user = NULL;
2487}
Damien Millerad833b32000-08-23 10:46:23 +10002488void
2489channel_register_filter(int id, channel_filter_fn *fn)
2490{
2491 Channel *c = channel_lookup(id);
2492 if (c == NULL) {
2493 log("channel_register_filter: %d: bad id", id);
2494 return;
2495 }
2496 c->input_filter = fn;
2497}
Damien Miller33b13562000-04-04 14:38:59 +10002498
2499void
Damien Miller69b69aa2000-10-28 14:19:58 +11002500channel_set_fds(int id, int rfd, int wfd, int efd,
2501 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002502{
2503 Channel *c = channel_lookup(id);
2504 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2505 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002506 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002507 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002508 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002509 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002510 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2511 packet_put_int(c->remote_id);
2512 packet_put_int(c->local_window);
2513 packet_send();
2514}