blob: 028c09e6a34516cc8af3af425eeaeb7ac89edbf4 [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"
Damien Miller69b69aa2000-10-28 14:19:58 +110043RCSID("$OpenBSD: channels.c,v 1.72 2000/10/27 07:48:22 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;
98unsigned int x11_saved_data_len = 0;
99
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;
105unsigned int x11_fake_data_len;
106
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
349channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
350{
351 if (buffer_len(&c->input) < packet_get_maxsize())
352 FD_SET(c->sock, readset);
353 if (buffer_len(&c->output) > 0)
354 FD_SET(c->sock, writeset);
355}
356
357void
358channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
359{
360 /* test whether sockets are 'alive' for read/write */
361 if (c->istate == CHAN_INPUT_OPEN)
362 if (buffer_len(&c->input) < packet_get_maxsize())
363 FD_SET(c->sock, readset);
364 if (c->ostate == CHAN_OUTPUT_OPEN ||
365 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
366 if (buffer_len(&c->output) > 0) {
367 FD_SET(c->sock, writeset);
368 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
369 chan_obuf_empty(c);
370 }
371 }
372}
373
374void
Damien Miller33b13562000-04-04 14:38:59 +1000375channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
376{
377 if (c->istate == CHAN_INPUT_OPEN &&
378 c->remote_window > 0 &&
379 buffer_len(&c->input) < c->remote_window)
380 FD_SET(c->rfd, readset);
381 if (c->ostate == CHAN_OUTPUT_OPEN ||
382 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
383 if (buffer_len(&c->output) > 0) {
384 FD_SET(c->wfd, writeset);
385 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
386 chan_obuf_empty(c);
387 }
388 }
389 /** XXX check close conditions, too */
390 if (c->efd != -1) {
391 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
392 buffer_len(&c->extended) > 0)
393 FD_SET(c->efd, writeset);
394 else if (c->extended_usage == CHAN_EXTENDED_READ &&
395 buffer_len(&c->extended) < c->remote_window)
396 FD_SET(c->efd, readset);
397 }
398}
399
400void
Damien Millerb38eff82000-04-01 11:09:21 +1000401channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
402{
403 if (buffer_len(&c->input) == 0) {
404 packet_start(SSH_MSG_CHANNEL_CLOSE);
405 packet_put_int(c->remote_id);
406 packet_send();
407 c->type = SSH_CHANNEL_CLOSED;
408 debug("Closing channel %d after input drain.", c->self);
409 }
410}
411
412void
413channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
414{
415 if (buffer_len(&c->output) == 0)
416 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000417 else
Damien Millerb38eff82000-04-01 11:09:21 +1000418 FD_SET(c->sock, writeset);
419}
420
421/*
422 * This is a special state for X11 authentication spoofing. An opened X11
423 * connection (when authentication spoofing is being done) remains in this
424 * state until the first packet has been completely read. The authentication
425 * data in that packet is then substituted by the real data if it matches the
426 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000427 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000428 */
429int
430x11_open_helper(Channel *c)
431{
432 unsigned char *ucp;
433 unsigned int proto_len, data_len;
434
435 /* Check if the fixed size part of the packet is in buffer. */
436 if (buffer_len(&c->output) < 12)
437 return 0;
438
439 /* Parse the lengths of variable-length fields. */
440 ucp = (unsigned char *) buffer_ptr(&c->output);
441 if (ucp[0] == 0x42) { /* Byte order MSB first. */
442 proto_len = 256 * ucp[6] + ucp[7];
443 data_len = 256 * ucp[8] + ucp[9];
444 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
445 proto_len = ucp[6] + 256 * ucp[7];
446 data_len = ucp[8] + 256 * ucp[9];
447 } else {
448 debug("Initial X11 packet contains bad byte order byte: 0x%x",
449 ucp[0]);
450 return -1;
451 }
452
453 /* Check if the whole packet is in buffer. */
454 if (buffer_len(&c->output) <
455 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
456 return 0;
457
458 /* Check if authentication protocol matches. */
459 if (proto_len != strlen(x11_saved_proto) ||
460 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
461 debug("X11 connection uses different authentication protocol.");
462 return -1;
463 }
464 /* Check if authentication data matches our fake data. */
465 if (data_len != x11_fake_data_len ||
466 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
467 x11_fake_data, x11_fake_data_len) != 0) {
468 debug("X11 auth data does not match fake data.");
469 return -1;
470 }
471 /* Check fake data length */
472 if (x11_fake_data_len != x11_saved_data_len) {
473 error("X11 fake_data_len %d != saved_data_len %d",
474 x11_fake_data_len, x11_saved_data_len);
475 return -1;
476 }
477 /*
478 * Received authentication protocol and data match
479 * our fake data. Substitute the fake data with real
480 * data.
481 */
482 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
483 x11_saved_data, x11_saved_data_len);
484 return 1;
485}
486
487void
488channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
489{
490 int ret = x11_open_helper(c);
491 if (ret == 1) {
492 /* Start normal processing for the channel. */
493 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000494 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000495 } else if (ret == -1) {
496 /*
497 * We have received an X11 connection that has bad
498 * authentication information.
499 */
500 log("X11 connection rejected because of wrong authentication.\r\n");
501 buffer_clear(&c->input);
502 buffer_clear(&c->output);
503 close(c->sock);
504 c->sock = -1;
505 c->type = SSH_CHANNEL_CLOSED;
506 packet_start(SSH_MSG_CHANNEL_CLOSE);
507 packet_put_int(c->remote_id);
508 packet_send();
509 }
510}
511
512void
Damien Millerbd483e72000-04-30 10:00:53 +1000513channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000514{
515 int ret = x11_open_helper(c);
516 if (ret == 1) {
517 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000518 if (compat20)
519 channel_pre_open_20(c, readset, writeset);
520 else
521 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000522 } else if (ret == -1) {
523 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000524 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000525 chan_write_failed(c);
526 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
527 }
528}
529
530/* This is our fake X11 server socket. */
531void
532channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
533{
534 struct sockaddr addr;
535 int newsock, newch;
536 socklen_t addrlen;
537 char buf[16384], *remote_hostname;
Damien Millerbd483e72000-04-30 10:00:53 +1000538 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000539
540 if (FD_ISSET(c->sock, readset)) {
541 debug("X11 connection requested.");
542 addrlen = sizeof(addr);
543 newsock = accept(c->sock, &addr, &addrlen);
544 if (newsock < 0) {
545 error("accept: %.100s", strerror(errno));
546 return;
547 }
548 remote_hostname = get_remote_hostname(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000549 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000550 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerbd483e72000-04-30 10:00:53 +1000551 remote_hostname, remote_port);
552
553 newch = channel_new("x11",
554 SSH_CHANNEL_OPENING, newsock, newsock, -1,
555 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100556 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000557 if (compat20) {
558 packet_start(SSH2_MSG_CHANNEL_OPEN);
559 packet_put_cstring("x11");
560 packet_put_int(newch);
561 packet_put_int(c->local_window_max);
562 packet_put_int(c->local_maxpacket);
563 /* originator host and port */
564 packet_put_cstring(remote_hostname);
Damien Miller30c3d422000-05-09 11:02:59 +1000565 if (datafellows & SSH_BUG_X11FWD) {
566 debug("ssh2 x11 bug compat mode");
567 } else {
568 packet_put_int(remote_port);
569 }
Damien Millerbd483e72000-04-30 10:00:53 +1000570 packet_send();
571 } else {
572 packet_start(SSH_SMSG_X11_OPEN);
573 packet_put_int(newch);
574 if (have_hostname_in_open)
575 packet_put_string(buf, strlen(buf));
576 packet_send();
577 }
Damien Millerb38eff82000-04-01 11:09:21 +1000578 xfree(remote_hostname);
Damien Millerb38eff82000-04-01 11:09:21 +1000579 }
580}
581
582/*
583 * This socket is listening for connections to a forwarded TCP/IP port.
584 */
585void
586channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
587{
588 struct sockaddr addr;
589 int newsock, newch;
590 socklen_t addrlen;
591 char buf[1024], *remote_hostname;
592 int remote_port;
593
594 if (FD_ISSET(c->sock, readset)) {
595 debug("Connection to port %d forwarding "
596 "to %.100s port %d requested.",
597 c->listening_port, c->path, c->host_port);
598 addrlen = sizeof(addr);
599 newsock = accept(c->sock, &addr, &addrlen);
600 if (newsock < 0) {
601 error("accept: %.100s", strerror(errno));
602 return;
603 }
604 remote_hostname = get_remote_hostname(newsock);
605 remote_port = get_peer_port(newsock);
606 snprintf(buf, sizeof buf,
607 "listen port %d for %.100s port %d, "
608 "connect from %.200s port %d",
609 c->listening_port, c->path, c->host_port,
610 remote_hostname, remote_port);
611 newch = channel_new("direct-tcpip",
612 SSH_CHANNEL_OPENING, newsock, newsock, -1,
613 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100614 0, xstrdup(buf), 1);
Damien Miller33b13562000-04-04 14:38:59 +1000615 if (compat20) {
616 packet_start(SSH2_MSG_CHANNEL_OPEN);
617 packet_put_cstring("direct-tcpip");
618 packet_put_int(newch);
619 packet_put_int(c->local_window_max);
620 packet_put_int(c->local_maxpacket);
Damien Miller4af51302000-04-16 11:18:38 +1000621 /* target host and port */
Damien Miller33b13562000-04-04 14:38:59 +1000622 packet_put_string(c->path, strlen(c->path));
623 packet_put_int(c->host_port);
Damien Miller4af51302000-04-16 11:18:38 +1000624 /* originator host and port */
Damien Miller33b13562000-04-04 14:38:59 +1000625 packet_put_cstring(remote_hostname);
626 packet_put_int(remote_port);
627 packet_send();
628 } else {
629 packet_start(SSH_MSG_PORT_OPEN);
630 packet_put_int(newch);
631 packet_put_string(c->path, strlen(c->path));
632 packet_put_int(c->host_port);
633 if (have_hostname_in_open) {
634 packet_put_string(buf, strlen(buf));
635 }
636 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000637 }
Damien Millerb38eff82000-04-01 11:09:21 +1000638 xfree(remote_hostname);
639 }
640}
641
642/*
643 * This is the authentication agent socket listening for connections from
644 * clients.
645 */
646void
647channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
648{
649 struct sockaddr addr;
650 int newsock, newch;
651 socklen_t addrlen;
652
653 if (FD_ISSET(c->sock, readset)) {
654 addrlen = sizeof(addr);
655 newsock = accept(c->sock, &addr, &addrlen);
656 if (newsock < 0) {
657 error("accept from auth socket: %.100s", strerror(errno));
658 return;
659 }
660 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
661 xstrdup("accepted auth socket"));
662 packet_start(SSH_SMSG_AGENT_OPEN);
663 packet_put_int(newch);
664 packet_send();
665 }
666}
667
668int
669channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
670{
671 char buf[16*1024];
672 int len;
673
674 if (c->rfd != -1 &&
675 FD_ISSET(c->rfd, readset)) {
676 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000677 if (len < 0 && (errno == EINTR || errno == EAGAIN))
678 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000679 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000680 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000681 c->self, c->rfd, len);
682 if (compat13) {
683 buffer_consume(&c->output, buffer_len(&c->output));
684 c->type = SSH_CHANNEL_INPUT_DRAINING;
685 debug("Channel %d status set to input draining.", c->self);
686 } else {
687 chan_read_failed(c);
688 }
689 return -1;
690 }
Damien Millerad833b32000-08-23 10:46:23 +1000691 if(c->input_filter != NULL) {
692 if (c->input_filter(c, buf, len) == -1) {
693 debug("filter stops channel %d", c->self);
694 chan_read_failed(c);
695 }
696 } else {
697 buffer_append(&c->input, buf, len);
698 }
Damien Millerb38eff82000-04-01 11:09:21 +1000699 }
700 return 1;
701}
702int
703channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
704{
705 int len;
706
707 /* Send buffered output data to the socket. */
708 if (c->wfd != -1 &&
709 FD_ISSET(c->wfd, writeset) &&
710 buffer_len(&c->output) > 0) {
711 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000712 buffer_len(&c->output));
713 if (len < 0 && (errno == EINTR || errno == EAGAIN))
714 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000715 if (len <= 0) {
716 if (compat13) {
717 buffer_consume(&c->output, buffer_len(&c->output));
718 debug("Channel %d status set to input draining.", c->self);
719 c->type = SSH_CHANNEL_INPUT_DRAINING;
720 } else {
721 chan_write_failed(c);
722 }
723 return -1;
724 }
725 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000726 if (compat20 && len > 0) {
727 c->local_consumed += len;
728 }
729 }
730 return 1;
731}
732int
733channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
734{
735 char buf[16*1024];
736 int len;
737
Damien Miller1383bd82000-04-06 12:32:37 +1000738/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000739 if (c->efd != -1) {
740 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
741 FD_ISSET(c->efd, writeset) &&
742 buffer_len(&c->extended) > 0) {
743 len = write(c->efd, buffer_ptr(&c->extended),
744 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100745 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000746 c->self, len, c->efd);
747 if (len > 0) {
748 buffer_consume(&c->extended, len);
749 c->local_consumed += len;
750 }
751 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
752 FD_ISSET(c->efd, readset)) {
753 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100754 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000755 c->self, len, c->efd);
Damien Miller1383bd82000-04-06 12:32:37 +1000756 if (len == 0) {
757 debug("channel %d: closing efd %d",
758 c->self, c->efd);
759 close(c->efd);
760 c->efd = -1;
761 } else if (len > 0)
Damien Miller33b13562000-04-04 14:38:59 +1000762 buffer_append(&c->extended, buf, len);
763 }
764 }
765 return 1;
766}
767int
768channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
769{
Damien Millerefb4afe2000-04-12 18:45:05 +1000770 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000771 c->local_window < c->local_window_max/2 &&
772 c->local_consumed > 0) {
773 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
774 packet_put_int(c->remote_id);
775 packet_put_int(c->local_consumed);
776 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +1100777 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +1000778 c->self, c->local_window,
779 c->local_consumed);
780 c->local_window += c->local_consumed;
781 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000782 }
783 return 1;
784}
785
786void
787channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
788{
789 channel_handle_rfd(c, readset, writeset);
790 channel_handle_wfd(c, readset, writeset);
791}
792
793void
Damien Miller33b13562000-04-04 14:38:59 +1000794channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
795{
796 channel_handle_rfd(c, readset, writeset);
797 channel_handle_wfd(c, readset, writeset);
798 channel_handle_efd(c, readset, writeset);
799 channel_check_window(c, readset, writeset);
800}
801
802void
Damien Millerb38eff82000-04-01 11:09:21 +1000803channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
804{
805 int len;
806 /* Send buffered output data to the socket. */
807 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
808 len = write(c->sock, buffer_ptr(&c->output),
809 buffer_len(&c->output));
810 if (len <= 0)
811 buffer_consume(&c->output, buffer_len(&c->output));
812 else
813 buffer_consume(&c->output, len);
814 }
815}
816
817void
Damien Miller33b13562000-04-04 14:38:59 +1000818channel_handler_init_20(void)
819{
820 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +1000821 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +1000822 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000823 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller33b13562000-04-04 14:38:59 +1000824
825 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
826 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000827 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller33b13562000-04-04 14:38:59 +1000828}
829
830void
Damien Millerb38eff82000-04-01 11:09:21 +1000831channel_handler_init_13(void)
832{
833 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
834 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
835 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
836 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
837 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
838 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
839 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
840
841 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
842 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
843 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
844 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
845 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
846}
847
848void
849channel_handler_init_15(void)
850{
851 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +1000852 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +1000853 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
854 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
855 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
856
857 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
858 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
859 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
860 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
861}
862
863void
864channel_handler_init(void)
865{
866 int i;
867 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
868 channel_pre[i] = NULL;
869 channel_post[i] = NULL;
870 }
Damien Miller33b13562000-04-04 14:38:59 +1000871 if (compat20)
872 channel_handler_init_20();
873 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000874 channel_handler_init_13();
875 else
876 channel_handler_init_15();
877}
878
Damien Miller4af51302000-04-16 11:18:38 +1000879void
Damien Millerb38eff82000-04-01 11:09:21 +1000880channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
881{
882 static int did_init = 0;
883 int i;
884 Channel *c;
885
886 if (!did_init) {
887 channel_handler_init();
888 did_init = 1;
889 }
890 for (i = 0; i < channels_alloc; i++) {
891 c = &channels[i];
892 if (c->type == SSH_CHANNEL_FREE)
893 continue;
894 if (ftab[c->type] == NULL)
895 continue;
896 (*ftab[c->type])(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +1000897 chan_delete_if_full_closed(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000898 }
899}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900
Damien Miller4af51302000-04-16 11:18:38 +1000901void
Damien Miller95def091999-11-25 00:26:21 +1100902channel_prepare_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903{
Damien Millerb38eff82000-04-01 11:09:21 +1000904 channel_handler(channel_pre, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905}
906
Damien Miller4af51302000-04-16 11:18:38 +1000907void
Damien Miller95def091999-11-25 00:26:21 +1100908channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000909{
Damien Millerb38eff82000-04-01 11:09:21 +1000910 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911}
912
913/* If there is data to send to the connection, send some of it now. */
914
Damien Miller4af51302000-04-16 11:18:38 +1000915void
Damien Miller95def091999-11-25 00:26:21 +1100916channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917{
Damien Miller95def091999-11-25 00:26:21 +1100918 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000919 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920
Damien Miller95def091999-11-25 00:26:21 +1100921 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +1000922 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +1100923
Damien Miller5428f641999-11-25 11:54:57 +1100924 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +1100925 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000926 if (c->type != SSH_CHANNEL_OPEN &&
927 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +1100928 continue;
929 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000930 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100931 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000932 if (c->istate != CHAN_INPUT_OPEN &&
933 c->istate != CHAN_INPUT_WAIT_DRAIN)
Damien Miller34132e52000-01-14 15:45:46 +1100934 continue;
935 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000936 if (compat20 &&
937 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Damien Miller33b13562000-04-04 14:38:59 +1000938 debug("channel: %d: no data after CLOSE", c->self);
939 continue;
940 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941
Damien Miller95def091999-11-25 00:26:21 +1100942 /* Get the amount of buffered data for this channel. */
Damien Millerb38eff82000-04-01 11:09:21 +1000943 len = buffer_len(&c->input);
Damien Miller95def091999-11-25 00:26:21 +1100944 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100945 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +1000946 if (compat20) {
947 if (len > c->remote_window)
948 len = c->remote_window;
949 if (len > c->remote_maxpacket)
950 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +1100951 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000952 if (packet_is_interactive()) {
953 if (len > 1024)
954 len = 512;
955 } else {
956 /* Keep the packets at reasonable size. */
957 if (len > packet_get_maxsize()/2)
958 len = packet_get_maxsize()/2;
959 }
Damien Miller95def091999-11-25 00:26:21 +1100960 }
Damien Millerb38eff82000-04-01 11:09:21 +1000961 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000962 packet_start(compat20 ?
963 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +1000964 packet_put_int(c->remote_id);
965 packet_put_string(buffer_ptr(&c->input), len);
966 packet_send();
967 buffer_consume(&c->input, len);
968 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +1000969 }
970 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +1100971 if (compat13)
972 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +1100973 /*
974 * input-buffer is empty and read-socket shutdown:
975 * tell peer, that we will not send more data: send IEOF
976 */
Damien Millerb38eff82000-04-01 11:09:21 +1000977 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +1100978 }
Damien Miller33b13562000-04-04 14:38:59 +1000979 /* Send extended data, i.e. stderr */
980 if (compat20 &&
981 c->remote_window > 0 &&
982 (len = buffer_len(&c->extended)) > 0 &&
983 c->extended_usage == CHAN_EXTENDED_READ) {
984 if (len > c->remote_window)
985 len = c->remote_window;
986 if (len > c->remote_maxpacket)
987 len = c->remote_maxpacket;
988 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
989 packet_put_int(c->remote_id);
990 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
991 packet_put_string(buffer_ptr(&c->extended), len);
992 packet_send();
993 buffer_consume(&c->extended, len);
994 c->remote_window -= len;
995 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997}
998
Damien Miller5428f641999-11-25 11:54:57 +1100999/*
1000 * This is called when a packet of type CHANNEL_DATA has just been received.
1001 * The message type has already been consumed, but channel number and data is
1002 * still there.
1003 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Damien Miller4af51302000-04-16 11:18:38 +10001005void
Damien Miller62cee002000-09-23 17:15:56 +11001006channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007{
Damien Miller34132e52000-01-14 15:45:46 +11001008 int id;
Damien Miller95def091999-11-25 00:26:21 +11001009 char *data;
1010 unsigned int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001011 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012
Damien Miller95def091999-11-25 00:26:21 +11001013 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001014 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001015 c = channel_lookup(id);
1016 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001017 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018
Damien Miller95def091999-11-25 00:26:21 +11001019 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001020 if (c->type != SSH_CHANNEL_OPEN &&
1021 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001022 return;
1023
1024 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001025 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001026 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Damien Miller95def091999-11-25 00:26:21 +11001028 /* Get the data. */
1029 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001030 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001031
Damien Miller33b13562000-04-04 14:38:59 +10001032 if (compat20){
1033 if (data_len > c->local_maxpacket) {
1034 log("channel %d: rcvd big packet %d, maxpack %d",
1035 c->self, data_len, c->local_maxpacket);
1036 }
1037 if (data_len > c->local_window) {
1038 log("channel %d: rcvd too much data %d, win %d",
1039 c->self, data_len, c->local_window);
1040 xfree(data);
1041 return;
1042 }
1043 c->local_window -= data_len;
1044 }else{
1045 packet_integrity_check(plen, 4 + 4 + data_len, type);
1046 }
Damien Millerb38eff82000-04-01 11:09:21 +10001047 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001048 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049}
Damien Miller4af51302000-04-16 11:18:38 +10001050void
Damien Miller62cee002000-09-23 17:15:56 +11001051channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001052{
1053 int id;
1054 int tcode;
1055 char *data;
1056 unsigned int data_len;
1057 Channel *c;
1058
1059 /* Get the channel number and verify it. */
1060 id = packet_get_int();
1061 c = channel_lookup(id);
1062
1063 if (c == NULL)
1064 packet_disconnect("Received extended_data for bad channel %d.", id);
1065 if (c->type != SSH_CHANNEL_OPEN) {
1066 log("channel %d: ext data for non open", id);
1067 return;
1068 }
1069 tcode = packet_get_int();
1070 if (c->efd == -1 ||
1071 c->extended_usage != CHAN_EXTENDED_WRITE ||
1072 tcode != SSH2_EXTENDED_DATA_STDERR) {
1073 log("channel %d: bad ext data", c->self);
1074 return;
1075 }
1076 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001077 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001078 if (data_len > c->local_window) {
1079 log("channel %d: rcvd too much extended_data %d, win %d",
1080 c->self, data_len, c->local_window);
1081 xfree(data);
1082 return;
1083 }
Damien Millerd3444942000-09-30 14:20:03 +11001084 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001085 c->local_window -= data_len;
1086 buffer_append(&c->extended, data, data_len);
1087 xfree(data);
1088}
1089
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090
Damien Miller5428f641999-11-25 11:54:57 +11001091/*
1092 * Returns true if no channel has too much buffered data, and false if one or
1093 * more channel is overfull.
1094 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095
Damien Miller4af51302000-04-16 11:18:38 +10001096int
Damien Miller95def091999-11-25 00:26:21 +11001097channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001098{
Damien Miller95def091999-11-25 00:26:21 +11001099 unsigned int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001100 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001101
1102 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001103 c = &channels[i];
1104 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001105 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001106 debug("channel %d: big input buffer %d",
1107 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001108 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001109 }
1110 if (buffer_len(&c->output) > packet_get_maxsize()) {
1111 debug("channel %d: big output buffer %d",
1112 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001113 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001114 }
Damien Miller95def091999-11-25 00:26:21 +11001115 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116 }
Damien Miller95def091999-11-25 00:26:21 +11001117 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001118}
1119
Damien Miller4af51302000-04-16 11:18:38 +10001120void
Damien Miller62cee002000-09-23 17:15:56 +11001121channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001122{
1123 int id;
1124 Channel *c;
1125
1126 packet_integrity_check(plen, 4, type);
1127
1128 id = packet_get_int();
1129 c = channel_lookup(id);
1130 if (c == NULL)
1131 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1132 chan_rcvd_ieof(c);
1133}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134
Damien Miller4af51302000-04-16 11:18:38 +10001135void
Damien Miller62cee002000-09-23 17:15:56 +11001136channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137{
Damien Millerb38eff82000-04-01 11:09:21 +10001138 int id;
1139 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140
Damien Millerb38eff82000-04-01 11:09:21 +10001141 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001142
Damien Millerb38eff82000-04-01 11:09:21 +10001143 id = packet_get_int();
1144 c = channel_lookup(id);
1145 if (c == NULL)
1146 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001147
1148 /*
1149 * Send a confirmation that we have closed the channel and no more
1150 * data is coming for it.
1151 */
Damien Miller95def091999-11-25 00:26:21 +11001152 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001153 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001154 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155
Damien Miller5428f641999-11-25 11:54:57 +11001156 /*
1157 * If the channel is in closed state, we have sent a close request,
1158 * and the other side will eventually respond with a confirmation.
1159 * Thus, we cannot free the channel here, because then there would be
1160 * no-one to receive the confirmation. The channel gets freed when
1161 * the confirmation arrives.
1162 */
Damien Millerb38eff82000-04-01 11:09:21 +10001163 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001164 /*
1165 * Not a closed channel - mark it as draining, which will
1166 * cause it to be freed later.
1167 */
Damien Millerb38eff82000-04-01 11:09:21 +10001168 buffer_consume(&c->input, buffer_len(&c->input));
1169 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001170 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171}
1172
Damien Millerb38eff82000-04-01 11:09:21 +10001173/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001174void
Damien Miller62cee002000-09-23 17:15:56 +11001175channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001176{
Damien Millerb38eff82000-04-01 11:09:21 +10001177 int id = packet_get_int();
1178 Channel *c = channel_lookup(id);
1179 packet_integrity_check(plen, 4, type);
1180 if (c == NULL)
1181 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1182 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183}
1184
Damien Miller4af51302000-04-16 11:18:38 +10001185void
Damien Miller62cee002000-09-23 17:15:56 +11001186channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001187{
1188 int id = packet_get_int();
1189 Channel *c = channel_lookup(id);
1190
Damien Miller4af51302000-04-16 11:18:38 +10001191 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001192 if (c == NULL)
1193 packet_disconnect("Received close confirmation for "
1194 "out-of-range channel %d.", id);
1195 if (c->type != SSH_CHANNEL_CLOSED)
1196 packet_disconnect("Received close confirmation for "
1197 "non-closed channel %d (type %d).", id, c->type);
1198 channel_free(c->self);
1199}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200
Damien Miller4af51302000-04-16 11:18:38 +10001201void
Damien Miller62cee002000-09-23 17:15:56 +11001202channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203{
Damien Millerb38eff82000-04-01 11:09:21 +10001204 int id, remote_id;
1205 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001206
Damien Miller33b13562000-04-04 14:38:59 +10001207 if (!compat20)
1208 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001209
Damien Millerb38eff82000-04-01 11:09:21 +10001210 id = packet_get_int();
1211 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212
Damien Millerb38eff82000-04-01 11:09:21 +10001213 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1214 packet_disconnect("Received open confirmation for "
1215 "non-opening channel %d.", id);
1216 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001217 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001218 c->remote_id = remote_id;
1219 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001220
1221 if (compat20) {
1222 c->remote_window = packet_get_int();
1223 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001224 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001225 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001226 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001227 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001228 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001229 }
1230 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1231 c->remote_window, c->remote_maxpacket);
1232 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001233}
1234
Damien Miller4af51302000-04-16 11:18:38 +10001235void
Damien Miller62cee002000-09-23 17:15:56 +11001236channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237{
Damien Millerb38eff82000-04-01 11:09:21 +10001238 int id;
1239 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240
Damien Miller33b13562000-04-04 14:38:59 +10001241 if (!compat20)
1242 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001243
1244 id = packet_get_int();
1245 c = channel_lookup(id);
1246
1247 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1248 packet_disconnect("Received open failure for "
1249 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001250 if (compat20) {
1251 int reason = packet_get_int();
1252 char *msg = packet_get_string(NULL);
Damien Miller4af51302000-04-16 11:18:38 +10001253 char *lang = packet_get_string(NULL);
Damien Miller33b13562000-04-04 14:38:59 +10001254 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
Damien Miller4af51302000-04-16 11:18:38 +10001255 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001256 xfree(msg);
Damien Miller4af51302000-04-16 11:18:38 +10001257 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001258 }
Damien Miller95def091999-11-25 00:26:21 +11001259 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001260 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001261}
1262
Damien Miller33b13562000-04-04 14:38:59 +10001263void
Damien Miller62cee002000-09-23 17:15:56 +11001264channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001265{
1266 int id;
1267 Channel *c;
1268
1269 id = packet_get_int();
1270 c = channel_lookup(id);
1271
1272 if (c == NULL ||
1273 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1274 packet_disconnect("Received request for "
1275 "non-open channel %d.", id);
1276 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001277 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001278 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001279 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001280 } else {
1281 char *service = packet_get_string(NULL);
1282 debug("channel: %d rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001283 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001284 xfree(service);
1285 }
1286}
1287
Damien Miller4af51302000-04-16 11:18:38 +10001288void
Damien Miller62cee002000-09-23 17:15:56 +11001289channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001290{
1291 Channel *c;
1292 int id, adjust;
1293
1294 if (!compat20)
1295 return;
1296
1297 /* Get the channel number and verify it. */
1298 id = packet_get_int();
1299 c = channel_lookup(id);
1300
1301 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1302 log("Received window adjust for "
1303 "non-open channel %d.", id);
1304 return;
1305 }
1306 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001307 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001308 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001309 c->remote_window += adjust;
1310}
1311
Damien Miller5428f641999-11-25 11:54:57 +11001312/*
1313 * Stops listening for channels, and removes any unix domain sockets that we
1314 * might have.
1315 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller4af51302000-04-16 11:18:38 +10001317void
Damien Miller95def091999-11-25 00:26:21 +11001318channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319{
Damien Miller95def091999-11-25 00:26:21 +11001320 int i;
1321 for (i = 0; i < channels_alloc; i++) {
1322 switch (channels[i].type) {
1323 case SSH_CHANNEL_AUTH_SOCKET:
1324 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001325 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001326 channel_free(i);
1327 break;
1328 case SSH_CHANNEL_PORT_LISTENER:
1329 case SSH_CHANNEL_X11_LISTENER:
1330 close(channels[i].sock);
1331 channel_free(i);
1332 break;
1333 default:
1334 break;
1335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337}
1338
Damien Miller5428f641999-11-25 11:54:57 +11001339/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001340 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001341 * descriptors after a fork.
1342 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001343
Damien Miller4af51302000-04-16 11:18:38 +10001344void
Damien Miller95def091999-11-25 00:26:21 +11001345channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346{
Damien Miller95def091999-11-25 00:26:21 +11001347 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001348 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001349 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001350 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351}
1352
1353/* Returns the maximum file descriptor number used by the channels. */
1354
Damien Miller4af51302000-04-16 11:18:38 +10001355int
Damien Miller95def091999-11-25 00:26:21 +11001356channel_max_fd()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357{
Damien Miller95def091999-11-25 00:26:21 +11001358 return channel_max_fd_value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001359}
1360
1361/* Returns true if any channel is still open. */
1362
Damien Miller4af51302000-04-16 11:18:38 +10001363int
Damien Miller95def091999-11-25 00:26:21 +11001364channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001365{
Damien Miller95def091999-11-25 00:26:21 +11001366 unsigned int i;
1367 for (i = 0; i < channels_alloc; i++)
1368 switch (channels[i].type) {
1369 case SSH_CHANNEL_FREE:
1370 case SSH_CHANNEL_X11_LISTENER:
1371 case SSH_CHANNEL_PORT_LISTENER:
1372 case SSH_CHANNEL_CLOSED:
1373 case SSH_CHANNEL_AUTH_SOCKET:
1374 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001375 case SSH_CHANNEL_LARVAL:
1376 if (!compat20)
1377 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1378 continue;
Damien Miller95def091999-11-25 00:26:21 +11001379 case SSH_CHANNEL_OPENING:
1380 case SSH_CHANNEL_OPEN:
1381 case SSH_CHANNEL_X11_OPEN:
1382 return 1;
1383 case SSH_CHANNEL_INPUT_DRAINING:
1384 case SSH_CHANNEL_OUTPUT_DRAINING:
1385 if (!compat13)
1386 fatal("cannot happen: OUT_DRAIN");
1387 return 1;
1388 default:
1389 fatal("channel_still_open: bad channel type %d", channels[i].type);
1390 /* NOTREACHED */
1391 }
1392 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001393}
1394
Damien Miller5428f641999-11-25 11:54:57 +11001395/*
1396 * Returns a message describing the currently open forwarded connections,
1397 * suitable for sending to the client. The message contains crlf pairs for
1398 * newlines.
1399 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400
Damien Miller95def091999-11-25 00:26:21 +11001401char *
1402channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403{
Damien Miller95def091999-11-25 00:26:21 +11001404 Buffer buffer;
1405 int i;
1406 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407
Damien Miller95def091999-11-25 00:26:21 +11001408 buffer_init(&buffer);
1409 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001411 for (i = 0; i < channels_alloc; i++) {
1412 Channel *c = &channels[i];
1413 switch (c->type) {
1414 case SSH_CHANNEL_FREE:
1415 case SSH_CHANNEL_X11_LISTENER:
1416 case SSH_CHANNEL_PORT_LISTENER:
1417 case SSH_CHANNEL_CLOSED:
1418 case SSH_CHANNEL_AUTH_SOCKET:
1419 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001420 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001421 case SSH_CHANNEL_OPENING:
1422 case SSH_CHANNEL_OPEN:
1423 case SSH_CHANNEL_X11_OPEN:
1424 case SSH_CHANNEL_INPUT_DRAINING:
1425 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001426 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 +11001427 c->self, c->remote_name,
1428 c->type, c->remote_id,
1429 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001430 c->ostate, buffer_len(&c->output),
1431 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001432 buffer_append(&buffer, buf, strlen(buf));
1433 continue;
1434 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001435 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001436 /* NOTREACHED */
1437 }
1438 }
1439 buffer_append(&buffer, "\0", 1);
1440 cp = xstrdup(buffer_ptr(&buffer));
1441 buffer_free(&buffer);
1442 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001443}
1444
Damien Miller5428f641999-11-25 11:54:57 +11001445/*
1446 * Initiate forwarding of connections to local port "port" through the secure
1447 * channel to host:port from remote side.
1448 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Damien Miller4af51302000-04-16 11:18:38 +10001450void
Damien Milleraae6c611999-12-06 11:47:28 +11001451channel_request_local_forwarding(u_short port, const char *host,
Damien Millera34a28b1999-12-14 10:47:15 +11001452 u_short host_port, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453{
Damien Miller34132e52000-01-14 15:45:46 +11001454 int success, ch, sock, on = 1;
1455 struct addrinfo hints, *ai, *aitop;
1456 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller5428f641999-11-25 11:54:57 +11001457 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458
Damien Miller95def091999-11-25 00:26:21 +11001459 if (strlen(host) > sizeof(channels[0].path) - 1)
1460 packet_disconnect("Forward host name too long.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
Damien Miller5428f641999-11-25 11:54:57 +11001462 /*
Damien Miller34132e52000-01-14 15:45:46 +11001463 * getaddrinfo returns a loopback address if the hostname is
1464 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001465 */
Damien Miller34132e52000-01-14 15:45:46 +11001466 memset(&hints, 0, sizeof(hints));
1467 hints.ai_family = IPv4or6;
1468 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1469 hints.ai_socktype = SOCK_STREAM;
1470 snprintf(strport, sizeof strport, "%d", port);
1471 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1472 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001473
Damien Miller34132e52000-01-14 15:45:46 +11001474 success = 0;
1475 for (ai = aitop; ai; ai = ai->ai_next) {
1476 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1477 continue;
1478 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1479 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1480 error("channel_request_local_forwarding: getnameinfo failed");
1481 continue;
1482 }
1483 /* Create a port to listen for the host. */
1484 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1485 if (sock < 0) {
1486 /* this is no error since kernel may not support ipv6 */
1487 verbose("socket: %.100s", strerror(errno));
1488 continue;
1489 }
1490 /*
1491 * Set socket options. We would like the socket to disappear
1492 * as soon as it has been closed for whatever reason.
1493 */
1494 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1495 linger.l_onoff = 1;
1496 linger.l_linger = 5;
1497 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1498 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001499
Damien Miller34132e52000-01-14 15:45:46 +11001500 /* Bind the socket to the address. */
1501 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1502 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001503 if (!ai->ai_next)
1504 error("bind: %.100s", strerror(errno));
1505 else
1506 verbose("bind: %.100s", strerror(errno));
1507
Damien Miller34132e52000-01-14 15:45:46 +11001508 close(sock);
1509 continue;
1510 }
1511 /* Start listening for connections on the socket. */
1512 if (listen(sock, 5) < 0) {
1513 error("listen: %.100s", strerror(errno));
1514 close(sock);
1515 continue;
1516 }
1517 /* Allocate a channel number for the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001518 ch = channel_new(
1519 "port listener", SSH_CHANNEL_PORT_LISTENER,
1520 sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001521 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001522 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001523 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
1524 channels[ch].host_port = host_port;
1525 channels[ch].listening_port = port;
1526 success = 1;
1527 }
1528 if (success == 0)
1529 packet_disconnect("cannot listen port: %d", port);
1530 freeaddrinfo(aitop);
Damien Miller95def091999-11-25 00:26:21 +11001531}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001532
Damien Miller5428f641999-11-25 11:54:57 +11001533/*
1534 * Initiate forwarding of connections to port "port" on remote host through
1535 * the secure channel to host:port from local side.
1536 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537
Damien Miller4af51302000-04-16 11:18:38 +10001538void
Damien Millerb38eff82000-04-01 11:09:21 +10001539channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1540 u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001541{
Damien Miller95def091999-11-25 00:26:21 +11001542 int payload_len;
1543 /* Record locally that connection to this host/port is permitted. */
1544 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1545 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001546
Damien Millerb38eff82000-04-01 11:09:21 +10001547 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1548 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1549 permitted_opens[num_permitted_opens].listen_port = listen_port;
Damien Miller95def091999-11-25 00:26:21 +11001550 num_permitted_opens++;
1551
1552 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001553 if (compat20) {
1554 const char *address_to_bind = "0.0.0.0";
1555 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1556 packet_put_cstring("tcpip-forward");
1557 packet_put_char(0); /* boolean: want reply */
1558 packet_put_cstring(address_to_bind);
1559 packet_put_int(listen_port);
1560 } else {
1561 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001562 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001563 packet_put_cstring(host_to_connect);
1564 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001565 packet_send();
1566 packet_write_wait();
1567 /*
1568 * Wait for response from the remote side. It will send a disconnect
1569 * message on failure, and we will never see it here.
1570 */
1571 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1572 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573}
1574
Damien Miller5428f641999-11-25 11:54:57 +11001575/*
1576 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1577 * listening for the port, and sends back a success reply (or disconnect
1578 * message if there was an error). This never returns if there was an error.
1579 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Damien Miller4af51302000-04-16 11:18:38 +10001581void
Damien Millere247cc42000-05-07 12:03:14 +10001582channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583{
Damien Milleraae6c611999-12-06 11:47:28 +11001584 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001585 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001586
Damien Miller95def091999-11-25 00:26:21 +11001587 /* Get arguments from the packet. */
1588 port = packet_get_int();
1589 hostname = packet_get_string(NULL);
1590 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591
Damien Millerbac2d8a2000-09-05 16:13:06 +11001592#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001593 /*
1594 * Check that an unprivileged user is not trying to forward a
1595 * privileged port.
1596 */
Damien Miller95def091999-11-25 00:26:21 +11001597 if (port < IPPORT_RESERVED && !is_root)
1598 packet_disconnect("Requested forwarding of port %d but user is not root.",
1599 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001600#endif
Damien Millera34a28b1999-12-14 10:47:15 +11001601 /*
1602 * Initiate forwarding,
Damien Millera34a28b1999-12-14 10:47:15 +11001603 */
Damien Millere247cc42000-05-07 12:03:14 +10001604 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001605
1606 /* Free the argument string. */
1607 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608}
1609
Damien Millerb38eff82000-04-01 11:09:21 +10001610/* XXX move to aux.c */
1611int
1612channel_connect_to(const char *host, u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613{
Damien Miller34132e52000-01-14 15:45:46 +11001614 struct addrinfo hints, *ai, *aitop;
1615 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1616 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001617 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001618
Damien Miller34132e52000-01-14 15:45:46 +11001619 memset(&hints, 0, sizeof(hints));
1620 hints.ai_family = IPv4or6;
1621 hints.ai_socktype = SOCK_STREAM;
1622 snprintf(strport, sizeof strport, "%d", host_port);
1623 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1624 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001625 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001626 }
Damien Miller34132e52000-01-14 15:45:46 +11001627 for (ai = aitop; ai; ai = ai->ai_next) {
1628 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1629 continue;
1630 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1631 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001632 error("channel_connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001633 continue;
1634 }
1635 /* Create the socket. */
1636 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1637 if (sock < 0) {
1638 error("socket: %.100s", strerror(errno));
1639 continue;
1640 }
1641 /* Connect to the host/port. */
1642 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1643 error("connect %.100s port %s: %.100s", ntop, strport,
1644 strerror(errno));
1645 close(sock);
1646 continue; /* fail -- try next */
1647 }
1648 break; /* success */
1649
1650 }
1651 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001652 if (!ai) {
1653 error("connect %.100s port %d: failed.", host, host_port);
Damien Millerb38eff82000-04-01 11:09:21 +10001654 return -1;
1655 }
1656 /* success */
1657 return sock;
1658}
1659
1660/*
1661 * This is called after receiving PORT_OPEN message. This attempts to
1662 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1663 * or CHANNEL_OPEN_FAILURE.
1664 */
1665
Damien Miller4af51302000-04-16 11:18:38 +10001666void
Damien Miller62cee002000-09-23 17:15:56 +11001667channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001668{
1669 u_short host_port;
1670 char *host, *originator_string;
1671 int remote_channel, sock = -1, newch, i, denied;
1672 unsigned int host_len, originator_len;
1673
1674 /* Get remote channel number. */
1675 remote_channel = packet_get_int();
1676
1677 /* Get host name to connect to. */
1678 host = packet_get_string(&host_len);
1679
1680 /* Get port to connect to. */
1681 host_port = packet_get_int();
1682
1683 /* Get remote originator name. */
1684 if (have_hostname_in_open) {
1685 originator_string = packet_get_string(&originator_len);
1686 originator_len += 4; /* size of packet_int */
1687 } else {
1688 originator_string = xstrdup("unknown (remote did not supply name)");
1689 originator_len = 0; /* no originator supplied */
Damien Miller95def091999-11-25 00:26:21 +11001690 }
Damien Miller34132e52000-01-14 15:45:46 +11001691
Damien Millerb38eff82000-04-01 11:09:21 +10001692 packet_integrity_check(plen,
1693 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001694
Damien Millerb38eff82000-04-01 11:09:21 +10001695 /* Check if opening that port is permitted. */
1696 denied = 0;
1697 if (!all_opens_permitted) {
1698 /* Go trough all permitted ports. */
1699 for (i = 0; i < num_permitted_opens; i++)
1700 if (permitted_opens[i].port_to_connect == host_port &&
1701 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1702 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703
Damien Millerb38eff82000-04-01 11:09:21 +10001704 /* Check if we found the requested port among those permitted. */
1705 if (i >= num_permitted_opens) {
1706 /* The port is not permitted. */
1707 log("Received request to connect to %.100s:%d, but the request was denied.",
1708 host, host_port);
1709 denied = 1;
1710 }
1711 }
1712 sock = denied ? -1 : channel_connect_to(host, host_port);
1713 if (sock > 0) {
1714 /* Allocate a channel for this connection. */
1715 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1716 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001717
Damien Millerb38eff82000-04-01 11:09:21 +10001718 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1719 packet_put_int(remote_channel);
1720 packet_put_int(newch);
1721 packet_send();
1722 } else {
1723 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1724 packet_put_int(remote_channel);
1725 packet_send();
1726 }
Damien Miller95def091999-11-25 00:26:21 +11001727 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728}
1729
Damien Miller5428f641999-11-25 11:54:57 +11001730/*
1731 * Creates an internet domain socket for listening for X11 connections.
1732 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1733 * occurs.
1734 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735
Damien Miller34132e52000-01-14 15:45:46 +11001736#define NUM_SOCKS 10
1737
Damien Miller95def091999-11-25 00:26:21 +11001738char *
Damien Millera34a28b1999-12-14 10:47:15 +11001739x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740{
Damien Milleraae6c611999-12-06 11:47:28 +11001741 int display_number, sock;
1742 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001743 struct addrinfo hints, *ai, *aitop;
1744 char strport[NI_MAXSERV];
1745 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1746 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001747 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001748
Damien Millera34a28b1999-12-14 10:47:15 +11001749 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001750 display_number < MAX_DISPLAYS;
1751 display_number++) {
1752 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001753 memset(&hints, 0, sizeof(hints));
1754 hints.ai_family = IPv4or6;
1755 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1756 hints.ai_socktype = SOCK_STREAM;
1757 snprintf(strport, sizeof strport, "%d", port);
1758 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1759 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001760 return NULL;
1761 }
Damien Miller34132e52000-01-14 15:45:46 +11001762 for (ai = aitop; ai; ai = ai->ai_next) {
1763 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1764 continue;
1765 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1766 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11001767 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11001768 error("socket: %.100s", strerror(errno));
1769 return NULL;
1770 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11001771 debug("x11_create_display_inet: Socket family %d not supported",
1772 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11001773 continue;
1774 }
Damien Miller34132e52000-01-14 15:45:46 +11001775 }
1776 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1777 debug("bind port %d: %.100s", port, strerror(errno));
1778 shutdown(sock, SHUT_RDWR);
1779 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11001780
1781 if (ai->ai_next)
1782 continue;
1783
Damien Miller34132e52000-01-14 15:45:46 +11001784 for (n = 0; n < num_socks; n++) {
1785 shutdown(socks[n], SHUT_RDWR);
1786 close(socks[n]);
1787 }
1788 num_socks = 0;
1789 break;
1790 }
1791 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11001792#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11001793 if (num_socks == NUM_SOCKS)
1794 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11001795#else
1796 break;
1797#endif
Damien Miller95def091999-11-25 00:26:21 +11001798 }
Damien Miller34132e52000-01-14 15:45:46 +11001799 if (num_socks > 0)
1800 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001801 }
Damien Miller95def091999-11-25 00:26:21 +11001802 if (display_number >= MAX_DISPLAYS) {
1803 error("Failed to allocate internet-domain X11 display socket.");
1804 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001805 }
Damien Miller95def091999-11-25 00:26:21 +11001806 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11001807 for (n = 0; n < num_socks; n++) {
1808 sock = socks[n];
1809 if (listen(sock, 5) < 0) {
1810 error("listen: %.100s", strerror(errno));
1811 shutdown(sock, SHUT_RDWR);
1812 close(sock);
1813 return NULL;
1814 }
Damien Miller95def091999-11-25 00:26:21 +11001815 }
Damien Miller34132e52000-01-14 15:45:46 +11001816
Damien Miller95def091999-11-25 00:26:21 +11001817 /* Set up a suitable value for the DISPLAY variable. */
Damien Miller76112de1999-12-21 11:18:08 +11001818
Damien Miller95def091999-11-25 00:26:21 +11001819 if (gethostname(hostname, sizeof(hostname)) < 0)
1820 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11001821
1822#ifdef IPADDR_IN_DISPLAY
1823 /*
1824 * HPUX detects the local hostname in the DISPLAY variable and tries
1825 * to set up a shared memory connection to the server, which it
1826 * incorrectly supposes to be local.
1827 *
1828 * The workaround - as used in later $$H and other programs - is
1829 * is to set display to the host's IP address.
1830 */
1831 {
1832 struct hostent *he;
1833 struct in_addr my_addr;
1834
1835 he = gethostbyname(hostname);
1836 if (he == NULL) {
1837 error("[X11-broken-fwd-hostname-workaround] Could not get "
1838 "IP address for hostname %s.", hostname);
1839
1840 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1841 "Could not get IP address for hostname %s.", hostname);
1842
1843 shutdown(sock, SHUT_RDWR);
1844 close(sock);
1845
1846 return NULL;
1847 }
1848
1849 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1850
1851 /* Set DISPLAY to <ip address>:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001852 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Damien Miller76112de1999-12-21 11:18:08 +11001853 display_number, screen_number);
1854 }
1855#else /* IPADDR_IN_DISPLAY */
1856 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001857 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Damien Miller76112de1999-12-21 11:18:08 +11001858 display_number, screen_number);
1859#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860
Damien Miller34132e52000-01-14 15:45:46 +11001861 /* Allocate a channel for each socket. */
1862 for (n = 0; n < num_socks; n++) {
1863 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10001864 (void) channel_new("x11 listener",
1865 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
1866 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001867 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001868 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001869
Damien Miller95def091999-11-25 00:26:21 +11001870 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11001871 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872}
1873
1874#ifndef X_UNIX_PATH
1875#define X_UNIX_PATH "/tmp/.X11-unix/X"
1876#endif
1877
1878static
1879int
Damien Miller81b25cf1999-12-07 16:47:28 +11001880connect_local_xsocket(unsigned int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881{
Damien Miller95def091999-11-25 00:26:21 +11001882 static const char *const x_sockets[] = {
1883 X_UNIX_PATH "%u",
1884 "/var/X/.X11-unix/X" "%u",
1885 "/usr/spool/sockets/X11/" "%u",
1886 NULL
1887 };
1888 int sock;
1889 struct sockaddr_un addr;
1890 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001891
Damien Miller95def091999-11-25 00:26:21 +11001892 for (path = x_sockets; *path; ++path) {
1893 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1894 if (sock < 0)
1895 error("socket: %.100s", strerror(errno));
1896 memset(&addr, 0, sizeof(addr));
1897 addr.sun_family = AF_UNIX;
1898 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1899 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1900 return sock;
1901 close(sock);
1902 }
1903 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1904 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001905}
1906
Damien Millerbd483e72000-04-30 10:00:53 +10001907int
1908x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001909{
Damien Millerbd483e72000-04-30 10:00:53 +10001910 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11001911 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10001912 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11001913 struct addrinfo hints, *ai, *aitop;
1914 char strport[NI_MAXSERV];
1915 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001916
Damien Miller95def091999-11-25 00:26:21 +11001917 /* Try to open a socket for the local X server. */
1918 display = getenv("DISPLAY");
1919 if (!display) {
1920 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10001921 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001922 }
Damien Miller5428f641999-11-25 11:54:57 +11001923 /*
1924 * Now we decode the value of the DISPLAY variable and make a
1925 * connection to the real X server.
1926 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001927
Damien Miller5428f641999-11-25 11:54:57 +11001928 /*
1929 * Check if it is a unix domain socket. Unix domain displays are in
1930 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1931 */
Damien Miller95def091999-11-25 00:26:21 +11001932 if (strncmp(display, "unix:", 5) == 0 ||
1933 display[0] == ':') {
1934 /* Connect to the unix domain socket. */
1935 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1936 error("Could not parse display number from DISPLAY: %.100s",
1937 display);
Damien Millerbd483e72000-04-30 10:00:53 +10001938 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001939 }
1940 /* Create a socket. */
1941 sock = connect_local_xsocket(display_number);
1942 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10001943 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001944
1945 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10001946 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001947 }
Damien Miller5428f641999-11-25 11:54:57 +11001948 /*
1949 * Connect to an inet socket. The DISPLAY value is supposedly
1950 * hostname:d[.s], where hostname may also be numeric IP address.
1951 */
Damien Miller95def091999-11-25 00:26:21 +11001952 strncpy(buf, display, sizeof(buf));
1953 buf[sizeof(buf) - 1] = 0;
1954 cp = strchr(buf, ':');
1955 if (!cp) {
1956 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10001957 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958 }
Damien Miller95def091999-11-25 00:26:21 +11001959 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001960 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11001961 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1962 error("Could not parse display number from DISPLAY: %.100s",
1963 display);
Damien Millerbd483e72000-04-30 10:00:53 +10001964 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001965 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001966
Damien Miller34132e52000-01-14 15:45:46 +11001967 /* Look up the host address */
1968 memset(&hints, 0, sizeof(hints));
1969 hints.ai_family = IPv4or6;
1970 hints.ai_socktype = SOCK_STREAM;
1971 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
1972 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1973 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10001974 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001975 }
Damien Miller34132e52000-01-14 15:45:46 +11001976 for (ai = aitop; ai; ai = ai->ai_next) {
1977 /* Create a socket. */
1978 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1979 if (sock < 0) {
1980 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001981 continue;
1982 }
1983 /* Connect it to the display. */
1984 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1985 debug("connect %.100s port %d: %.100s", buf,
1986 6000 + display_number, strerror(errno));
1987 close(sock);
1988 continue;
1989 }
1990 /* Success */
1991 break;
Damien Miller34132e52000-01-14 15:45:46 +11001992 }
Damien Miller34132e52000-01-14 15:45:46 +11001993 freeaddrinfo(aitop);
1994 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10001995 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11001996 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10001997 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001998 }
Damien Millerbd483e72000-04-30 10:00:53 +10001999 return sock;
2000}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002001
Damien Millerbd483e72000-04-30 10:00:53 +10002002/*
2003 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2004 * the remote channel number. We should do whatever we want, and respond
2005 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2006 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002007
Damien Millerbd483e72000-04-30 10:00:53 +10002008void
Damien Miller62cee002000-09-23 17:15:56 +11002009x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002010{
2011 int remote_channel, sock = 0, newch;
2012 char *remote_host;
2013 unsigned int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002014
Damien Millerbd483e72000-04-30 10:00:53 +10002015 /* Get remote channel number. */
2016 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002017
Damien Millerbd483e72000-04-30 10:00:53 +10002018 /* Get remote originator name. */
2019 if (have_hostname_in_open) {
2020 remote_host = packet_get_string(&remote_len);
2021 remote_len += 4;
2022 } else {
2023 remote_host = xstrdup("unknown (remote did not supply name)");
2024 remote_len = 0;
2025 }
2026
2027 debug("Received X11 open request.");
2028 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2029
2030 /* Obtain a connection to the real X display. */
2031 sock = x11_connect_display();
2032 if (sock == -1) {
2033 /* Send refusal to the remote host. */
2034 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2035 packet_put_int(remote_channel);
2036 packet_send();
2037 } else {
2038 /* Allocate a channel for this connection. */
2039 newch = channel_allocate(
2040 (x11_saved_proto == NULL) ?
2041 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2042 sock, remote_host);
2043 channels[newch].remote_id = remote_channel;
2044
2045 /* Send a confirmation to the remote host. */
2046 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2047 packet_put_int(remote_channel);
2048 packet_put_int(newch);
2049 packet_send();
2050 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002051}
2052
Damien Miller69b69aa2000-10-28 14:19:58 +11002053/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2054void
2055deny_input_open(int type, int plen, void *ctxt)
2056{
2057 int rchan = packet_get_int();
2058 switch(type){
2059 case SSH_SMSG_AGENT_OPEN:
2060 error("Warning: ssh server tried agent forwarding.");
2061 break;
2062 case SSH_SMSG_X11_OPEN:
2063 error("Warning: ssh server tried X11 forwarding.");
2064 break;
2065 default:
2066 error("deny_input_open: type %d plen %d", type, plen);
2067 break;
2068 }
2069 error("Warning: this is probably a break in attempt by a malicious server.");
2070 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2071 packet_put_int(rchan);
2072 packet_send();
2073}
2074
Damien Miller5428f641999-11-25 11:54:57 +11002075/*
2076 * Requests forwarding of X11 connections, generates fake authentication
2077 * data, and enables authentication spoofing.
2078 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002079
Damien Miller4af51302000-04-16 11:18:38 +10002080void
Damien Millerbd483e72000-04-30 10:00:53 +10002081x11_request_forwarding_with_spoofing(int client_session_id,
2082 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002083{
Damien Miller95def091999-11-25 00:26:21 +11002084 unsigned int data_len = (unsigned int) strlen(data) / 2;
2085 unsigned int i, value;
2086 char *new_data;
2087 int screen_number;
2088 const char *cp;
2089 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002090
Damien Miller95def091999-11-25 00:26:21 +11002091 cp = getenv("DISPLAY");
2092 if (cp)
2093 cp = strchr(cp, ':');
2094 if (cp)
2095 cp = strchr(cp, '.');
2096 if (cp)
2097 screen_number = atoi(cp + 1);
2098 else
2099 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100
Damien Miller95def091999-11-25 00:26:21 +11002101 /* Save protocol name. */
2102 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002103
Damien Miller5428f641999-11-25 11:54:57 +11002104 /*
2105 * Extract real authentication data and generate fake data of the
2106 * same length.
2107 */
Damien Miller95def091999-11-25 00:26:21 +11002108 x11_saved_data = xmalloc(data_len);
2109 x11_fake_data = xmalloc(data_len);
2110 for (i = 0; i < data_len; i++) {
2111 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2112 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2113 if (i % 4 == 0)
2114 rand = arc4random();
2115 x11_saved_data[i] = value;
2116 x11_fake_data[i] = rand & 0xff;
2117 rand >>= 8;
2118 }
2119 x11_saved_data_len = data_len;
2120 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002121
Damien Miller95def091999-11-25 00:26:21 +11002122 /* Convert the fake data into hex. */
2123 new_data = xmalloc(2 * data_len + 1);
2124 for (i = 0; i < data_len; i++)
2125 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002126
Damien Miller95def091999-11-25 00:26:21 +11002127 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002128 if (compat20) {
2129 channel_request_start(client_session_id, "x11-req", 0);
2130 packet_put_char(0); /* XXX bool single connection */
2131 } else {
2132 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2133 }
2134 packet_put_cstring(proto);
2135 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002136 packet_put_int(screen_number);
2137 packet_send();
2138 packet_write_wait();
2139 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002140}
2141
2142/* Sends a message to the server to request authentication fd forwarding. */
2143
Damien Miller4af51302000-04-16 11:18:38 +10002144void
Damien Miller95def091999-11-25 00:26:21 +11002145auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002146{
Damien Miller95def091999-11-25 00:26:21 +11002147 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2148 packet_send();
2149 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002150}
2151
Damien Miller5428f641999-11-25 11:54:57 +11002152/*
2153 * Returns the name of the forwarded authentication socket. Returns NULL if
2154 * there is no forwarded authentication socket. The returned value points to
2155 * a static buffer.
2156 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002157
Damien Miller95def091999-11-25 00:26:21 +11002158char *
2159auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002160{
Damien Miller95def091999-11-25 00:26:21 +11002161 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002162}
2163
2164/* removes the agent forwarding socket */
2165
Damien Miller4af51302000-04-16 11:18:38 +10002166void
Damien Miller95def091999-11-25 00:26:21 +11002167cleanup_socket(void)
2168{
Damien Miller78315eb2000-09-29 23:01:36 +11002169 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002170 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171}
2172
Damien Miller5428f641999-11-25 11:54:57 +11002173/*
Damien Millerd3a18572000-06-07 19:55:44 +10002174 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002175 * This starts forwarding authentication requests.
2176 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002177
Damien Millerd3a18572000-06-07 19:55:44 +10002178int
Damien Miller95def091999-11-25 00:26:21 +11002179auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002180{
Damien Miller95def091999-11-25 00:26:21 +11002181 int sock, newch;
2182 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002183
Damien Miller95def091999-11-25 00:26:21 +11002184 if (auth_get_socket_name() != NULL)
2185 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186
Damien Miller95def091999-11-25 00:26:21 +11002187 /* Temporarily drop privileged uid for mkdir/bind. */
2188 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002189
Damien Miller95def091999-11-25 00:26:21 +11002190 /* Allocate a buffer for the socket name, and format the name. */
2191 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2192 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2193 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002194
Damien Miller95def091999-11-25 00:26:21 +11002195 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002196 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2197 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2198 strerror(errno));
2199 restore_uid();
2200 xfree(channel_forwarded_auth_socket_name);
2201 xfree(channel_forwarded_auth_socket_dir);
2202 channel_forwarded_auth_socket_name = NULL;
2203 channel_forwarded_auth_socket_dir = NULL;
2204 return 0;
2205 }
Damien Miller95def091999-11-25 00:26:21 +11002206 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2207 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002208
Damien Miller95def091999-11-25 00:26:21 +11002209 if (atexit(cleanup_socket) < 0) {
2210 int saved = errno;
2211 cleanup_socket();
2212 packet_disconnect("socket: %.100s", strerror(saved));
2213 }
2214 /* Create the socket. */
2215 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2216 if (sock < 0)
2217 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002218
Damien Miller95def091999-11-25 00:26:21 +11002219 /* Bind it to the name. */
2220 memset(&sunaddr, 0, sizeof(sunaddr));
2221 sunaddr.sun_family = AF_UNIX;
2222 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2223 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002224
Damien Miller95def091999-11-25 00:26:21 +11002225 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2226 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002227
Damien Miller95def091999-11-25 00:26:21 +11002228 /* Restore the privileged uid. */
2229 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002230
Damien Miller95def091999-11-25 00:26:21 +11002231 /* Start listening on the socket. */
2232 if (listen(sock, 5) < 0)
2233 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002234
Damien Miller95def091999-11-25 00:26:21 +11002235 /* Allocate a channel for the authentication agent socket. */
2236 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
2237 xstrdup("auth socket"));
Damien Miller037a0dc1999-12-07 15:38:31 +11002238 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2239 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002240 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002241}
2242
2243/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2244
Damien Miller4af51302000-04-16 11:18:38 +10002245void
Damien Miller62cee002000-09-23 17:15:56 +11002246auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002247{
Damien Miller95def091999-11-25 00:26:21 +11002248 int remch, sock, newch;
2249 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002250
Damien Millerb38eff82000-04-01 11:09:21 +10002251 packet_integrity_check(plen, 4, type);
2252
Damien Miller95def091999-11-25 00:26:21 +11002253 /* Read the remote channel number from the message. */
2254 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002255
Damien Miller5428f641999-11-25 11:54:57 +11002256 /*
2257 * Get a connection to the local authentication agent (this may again
2258 * get forwarded).
2259 */
Damien Miller95def091999-11-25 00:26:21 +11002260 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002261
Damien Miller5428f641999-11-25 11:54:57 +11002262 /*
2263 * If we could not connect the agent, send an error message back to
2264 * the server. This should never happen unless the agent dies,
2265 * because authentication forwarding is only enabled if we have an
2266 * agent.
2267 */
Damien Miller95def091999-11-25 00:26:21 +11002268 if (sock < 0) {
2269 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2270 packet_put_int(remch);
2271 packet_send();
2272 return;
2273 }
2274 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002275
Damien Miller5428f641999-11-25 11:54:57 +11002276 /*
2277 * Dummy host name. This will be freed when the channel is freed; it
2278 * will still be valid in the packet_put_string below since the
2279 * channel cannot yet be freed at that point.
2280 */
Damien Miller95def091999-11-25 00:26:21 +11002281 dummyname = xstrdup("authentication agent connection");
2282
2283 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2284 channels[newch].remote_id = remch;
2285
2286 /* Send a confirmation to the remote host. */
2287 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2288 packet_put_int(remch);
2289 packet_put_int(newch);
2290 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002291}
Damien Miller33b13562000-04-04 14:38:59 +10002292
2293void
Damien Millerbd483e72000-04-30 10:00:53 +10002294channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002295{
2296 Channel *c = channel_lookup(id);
2297 if (c == NULL) {
2298 log("channel_open: %d: bad id", id);
2299 return;
2300 }
Damien Millerbd483e72000-04-30 10:00:53 +10002301 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002302 packet_start(SSH2_MSG_CHANNEL_OPEN);
2303 packet_put_cstring(c->ctype);
2304 packet_put_int(c->self);
2305 packet_put_int(c->local_window);
2306 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002307}
2308void
2309channel_open(int id)
2310{
2311 /* XXX REMOVE ME */
2312 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002313 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002314}
2315void
2316channel_request(int id, char *service, int wantconfirm)
2317{
2318 channel_request_start(id, service, wantconfirm);
2319 packet_send();
2320 debug("channel request %d: %s", id, service) ;
2321}
2322void
2323channel_request_start(int id, char *service, int wantconfirm)
2324{
2325 Channel *c = channel_lookup(id);
2326 if (c == NULL) {
2327 log("channel_request: %d: bad id", id);
2328 return;
2329 }
2330 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2331 packet_put_int(c->remote_id);
2332 packet_put_cstring(service);
2333 packet_put_char(wantconfirm);
2334}
2335void
2336channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2337{
2338 Channel *c = channel_lookup(id);
2339 if (c == NULL) {
2340 log("channel_register_callback: %d: bad id", id);
2341 return;
2342 }
2343 c->cb_event = mtype;
2344 c->cb_fn = fn;
2345 c->cb_arg = arg;
2346}
2347void
2348channel_register_cleanup(int id, channel_callback_fn *fn)
2349{
2350 Channel *c = channel_lookup(id);
2351 if (c == NULL) {
2352 log("channel_register_cleanup: %d: bad id", id);
2353 return;
2354 }
2355 c->dettach_user = fn;
2356}
2357void
2358channel_cancel_cleanup(int id)
2359{
2360 Channel *c = channel_lookup(id);
2361 if (c == NULL) {
2362 log("channel_cancel_cleanup: %d: bad id", id);
2363 return;
2364 }
2365 c->dettach_user = NULL;
2366}
Damien Millerad833b32000-08-23 10:46:23 +10002367void
2368channel_register_filter(int id, channel_filter_fn *fn)
2369{
2370 Channel *c = channel_lookup(id);
2371 if (c == NULL) {
2372 log("channel_register_filter: %d: bad id", id);
2373 return;
2374 }
2375 c->input_filter = fn;
2376}
Damien Miller33b13562000-04-04 14:38:59 +10002377
2378void
Damien Miller69b69aa2000-10-28 14:19:58 +11002379channel_set_fds(int id, int rfd, int wfd, int efd,
2380 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002381{
2382 Channel *c = channel_lookup(id);
2383 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2384 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002385 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002386 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002387 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002388 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002389 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2390 packet_put_int(c->remote_id);
2391 packet_put_int(c->local_window);
2392 packet_send();
2393}