blob: bd8c337ee95b792ed2ecca62cb627349e904e753 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * channels.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Fri Mar 24 16:35:24 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * This file contains functions for generic socket connection forwarding.
13 * There is also code for initiating connection forwarding for X11 connections,
14 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +100015 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Miller95def091999-11-25 00:26:21 +110017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "includes.h"
Damien Miller6f83b8e2000-05-02 09:23:45 +100020RCSID("$Id: channels.c,v 1.28 2000/05/01 23:23:45 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22#include "ssh.h"
23#include "packet.h"
24#include "xmalloc.h"
25#include "buffer.h"
26#include "authfd.h"
27#include "uidswap.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110028#include "readconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#include "servconf.h"
30
31#include "channels.h"
32#include "nchan.h"
33#include "compat.h"
34
Damien Miller33b13562000-04-04 14:38:59 +100035#include "ssh2.h"
36
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037/* Maximum number of fake X11 displays to try. */
38#define MAX_DISPLAYS 1000
39
40/* Max len of agent socket */
41#define MAX_SOCKET_NAME 100
42
Damien Millerbd483e72000-04-30 10:00:53 +100043/* default window/packet sizes for tcp/x11-fwd-channel */
44#define CHAN_TCP_WINDOW_DEFAULT (8*1024)
45#define CHAN_TCP_PACKET_DEFAULT (CHAN_TCP_WINDOW_DEFAULT/2)
46#define CHAN_X11_WINDOW_DEFAULT (4*1024)
47#define CHAN_X11_PACKET_DEFAULT (CHAN_X11_WINDOW_DEFAULT/2)
Damien Millerb38eff82000-04-01 11:09:21 +100048
Damien Miller5428f641999-11-25 11:54:57 +110049/*
50 * Pointer to an array containing all allocated channels. The array is
51 * dynamically extended as needed.
52 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053static Channel *channels = NULL;
54
Damien Miller5428f641999-11-25 11:54:57 +110055/*
56 * Size of the channel array. All slots of the array must always be
57 * initialized (at least the type field); unused slots are marked with type
58 * SSH_CHANNEL_FREE.
59 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060static int channels_alloc = 0;
61
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Maximum file descriptor value used in any of the channels. This is
64 * updated in channel_allocate.
65 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066static int channel_max_fd_value = 0;
67
68/* Name and directory of socket for authentication agent forwarding. */
69static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110070static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
72/* Saved X11 authentication protocol name. */
73char *x11_saved_proto = NULL;
74
75/* Saved X11 authentication data. This is the real data. */
76char *x11_saved_data = NULL;
77unsigned int x11_saved_data_len = 0;
78
Damien Miller5428f641999-11-25 11:54:57 +110079/*
80 * Fake X11 authentication data. This is what the server will be sending us;
81 * we should replace any occurrences of this by the real data.
82 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083char *x11_fake_data = NULL;
84unsigned int x11_fake_data_len;
85
Damien Miller5428f641999-11-25 11:54:57 +110086/*
87 * Data structure for storing which hosts are permitted for forward requests.
88 * The local sides of any remote forwards are stored in this array to prevent
89 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
90 * network (which might be behind a firewall).
91 */
Damien Miller95def091999-11-25 00:26:21 +110092typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100093 char *host_to_connect; /* Connect to 'host'. */
94 u_short port_to_connect; /* Connect to 'port'. */
95 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096} ForwardPermission;
97
98/* List of all permitted host/port pairs to connect. */
99static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
100/* Number of permitted host/port pairs in the array. */
101static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100102/*
103 * If this is true, all opens are permitted. This is the case on the server
104 * on which we have to trust the client anyway, and the user could do
105 * anything after logging in anyway.
106 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107static int all_opens_permitted = 0;
108
109/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
110static int have_hostname_in_open = 0;
111
112/* Sets specific protocol options. */
113
Damien Miller4af51302000-04-16 11:18:38 +1000114void
Damien Miller95def091999-11-25 00:26:21 +1100115channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116{
Damien Miller95def091999-11-25 00:26:21 +1100117 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118}
119
Damien Miller5428f641999-11-25 11:54:57 +1100120/*
121 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
122 * called by the server, because the user could connect to any port anyway,
123 * and the server has no way to know but to trust the client anyway.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller4af51302000-04-16 11:18:38 +1000126void
Damien Miller95def091999-11-25 00:26:21 +1100127channel_permit_all_opens()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128{
Damien Miller95def091999-11-25 00:26:21 +1100129 all_opens_permitted = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130}
131
Damien Millerb38eff82000-04-01 11:09:21 +1000132/* lookup channel by id */
133
134Channel *
135channel_lookup(int id)
136{
137 Channel *c;
138 if (id < 0 && id > channels_alloc) {
139 log("channel_lookup: %d: bad id", id);
140 return NULL;
141 }
142 c = &channels[id];
143 if (c->type == SSH_CHANNEL_FREE) {
144 log("channel_lookup: %d: bad id: channel free", id);
145 return NULL;
146 }
147 return c;
148}
149
Damien Miller5428f641999-11-25 11:54:57 +1100150/*
Damien Miller6f83b8e2000-05-02 09:23:45 +1000151 * register filedescriptors for a channel, used when allocating a channel or
152 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100153 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller6f83b8e2000-05-02 09:23:45 +1000155void
156channel_register_fds(Channel *c, int rfd, int wfd, int efd, int extusage)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157{
Damien Miller95def091999-11-25 00:26:21 +1100158 /* Update the maximum file descriptor value. */
Damien Millerb38eff82000-04-01 11:09:21 +1000159 if (rfd > channel_max_fd_value)
160 channel_max_fd_value = rfd;
161 if (wfd > channel_max_fd_value)
162 channel_max_fd_value = wfd;
163 if (efd > channel_max_fd_value)
164 channel_max_fd_value = efd;
Damien Miller5428f641999-11-25 11:54:57 +1100165 /* XXX set close-on-exec -markus */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000166 c->rfd = rfd;
167 c->wfd = wfd;
168 c->sock = (rfd == wfd) ? rfd : -1;
169 c->efd = efd;
170 c->extended_usage = extusage;
171}
172
173/*
174 * Allocate a new channel object and set its type and socket. This will cause
175 * remote_name to be freed.
176 */
177
178int
179channel_new(char *ctype, int type, int rfd, int wfd, int efd,
180 int window, int maxpack, int extusage, char *remote_name)
181{
182 int i, found;
183 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000184
Damien Miller95def091999-11-25 00:26:21 +1100185 /* Do initial allocation if this is the first call. */
186 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000187 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100188 channels_alloc = 10;
189 channels = xmalloc(channels_alloc * sizeof(Channel));
190 for (i = 0; i < channels_alloc; i++)
191 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100192 /*
193 * Kludge: arrange a call to channel_stop_listening if we
194 * terminate with fatal().
195 */
Damien Miller95def091999-11-25 00:26:21 +1100196 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
197 }
198 /* Try to find a free slot where to put the new channel. */
199 for (found = -1, i = 0; i < channels_alloc; i++)
200 if (channels[i].type == SSH_CHANNEL_FREE) {
201 /* Found a free slot. */
202 found = i;
203 break;
204 }
205 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100206 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100207 found = channels_alloc;
208 channels_alloc += 10;
209 debug("channel: expanding %d", channels_alloc);
210 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
211 for (i = found; i < channels_alloc; i++)
212 channels[i].type = SSH_CHANNEL_FREE;
213 }
214 /* Initialize and return new channel number. */
215 c = &channels[found];
216 buffer_init(&c->input);
217 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000218 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100219 chan_init_iostates(c);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000220 channel_register_fds(c, rfd, wfd, efd, extusage);
Damien Miller95def091999-11-25 00:26:21 +1100221 c->self = found;
222 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000223 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000224 c->local_window = window;
225 c->local_window_max = window;
226 c->local_consumed = 0;
227 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100228 c->remote_id = -1;
229 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000230 c->remote_window = 0;
231 c->remote_maxpacket = 0;
232 c->cb_fn = NULL;
233 c->cb_arg = NULL;
234 c->cb_event = 0;
235 c->dettach_user = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100236 debug("channel %d: new [%s]", found, remote_name);
237 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000239/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000240int
Damien Millerb38eff82000-04-01 11:09:21 +1000241channel_allocate(int type, int sock, char *remote_name)
242{
243 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
244}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Miller6f83b8e2000-05-02 09:23:45 +1000246
247/* Close all channel fd/socket. */
248
249void
250channel_close_fds(Channel *c)
251{
252 if (c->sock != -1) {
253 close(c->sock);
254 c->sock = -1;
255 }
256 if (c->rfd != -1) {
257 close(c->rfd);
258 c->rfd = -1;
259 }
260 if (c->wfd != -1) {
261 close(c->wfd);
262 c->wfd = -1;
263 }
264 if (c->efd != -1) {
265 close(c->efd);
266 c->efd = -1;
267 }
268}
269
270/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller4af51302000-04-16 11:18:38 +1000272void
Damien Millerb38eff82000-04-01 11:09:21 +1000273channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000274{
Damien Millerb38eff82000-04-01 11:09:21 +1000275 Channel *c = channel_lookup(id);
276 if (c == NULL)
277 packet_disconnect("channel free: bad local channel %d", id);
278 debug("channel_free: channel %d: status: %s", id, channel_open_message());
Damien Miller33b13562000-04-04 14:38:59 +1000279 if (c->dettach_user != NULL) {
280 debug("channel_free: channel %d: dettaching channel user", id);
281 c->dettach_user(c->self, NULL);
282 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000283 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000284 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000285 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000286 buffer_free(&c->input);
287 buffer_free(&c->output);
288 buffer_free(&c->extended);
289 c->type = SSH_CHANNEL_FREE;
290 if (c->remote_name) {
291 xfree(c->remote_name);
292 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100293 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294}
295
Damien Miller5428f641999-11-25 11:54:57 +1100296/*
Damien Millerb38eff82000-04-01 11:09:21 +1000297 * 'channel_pre*' are called just before select() to add any bits relevant to
298 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100299 */
Damien Millerb38eff82000-04-01 11:09:21 +1000300/*
301 * 'channel_post*': perform any appropriate operations for channels which
302 * have events pending.
303 */
304typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
305chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
306chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
307
308void
309channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
310{
311 FD_SET(c->sock, readset);
312}
313
314void
315channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
316{
317 if (buffer_len(&c->input) < packet_get_maxsize())
318 FD_SET(c->sock, readset);
319 if (buffer_len(&c->output) > 0)
320 FD_SET(c->sock, writeset);
321}
322
323void
324channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
325{
326 /* test whether sockets are 'alive' for read/write */
327 if (c->istate == CHAN_INPUT_OPEN)
328 if (buffer_len(&c->input) < packet_get_maxsize())
329 FD_SET(c->sock, readset);
330 if (c->ostate == CHAN_OUTPUT_OPEN ||
331 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
332 if (buffer_len(&c->output) > 0) {
333 FD_SET(c->sock, writeset);
334 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
335 chan_obuf_empty(c);
336 }
337 }
338}
339
340void
Damien Miller33b13562000-04-04 14:38:59 +1000341channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
342{
343 if (c->istate == CHAN_INPUT_OPEN &&
344 c->remote_window > 0 &&
345 buffer_len(&c->input) < c->remote_window)
346 FD_SET(c->rfd, readset);
347 if (c->ostate == CHAN_OUTPUT_OPEN ||
348 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
349 if (buffer_len(&c->output) > 0) {
350 FD_SET(c->wfd, writeset);
351 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
352 chan_obuf_empty(c);
353 }
354 }
355 /** XXX check close conditions, too */
356 if (c->efd != -1) {
357 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
358 buffer_len(&c->extended) > 0)
359 FD_SET(c->efd, writeset);
360 else if (c->extended_usage == CHAN_EXTENDED_READ &&
361 buffer_len(&c->extended) < c->remote_window)
362 FD_SET(c->efd, readset);
363 }
364}
365
366void
Damien Millerb38eff82000-04-01 11:09:21 +1000367channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
368{
369 if (buffer_len(&c->input) == 0) {
370 packet_start(SSH_MSG_CHANNEL_CLOSE);
371 packet_put_int(c->remote_id);
372 packet_send();
373 c->type = SSH_CHANNEL_CLOSED;
374 debug("Closing channel %d after input drain.", c->self);
375 }
376}
377
378void
379channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
380{
381 if (buffer_len(&c->output) == 0)
382 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000383 else
Damien Millerb38eff82000-04-01 11:09:21 +1000384 FD_SET(c->sock, writeset);
385}
386
387/*
388 * This is a special state for X11 authentication spoofing. An opened X11
389 * connection (when authentication spoofing is being done) remains in this
390 * state until the first packet has been completely read. The authentication
391 * data in that packet is then substituted by the real data if it matches the
392 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000393 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000394 */
395int
396x11_open_helper(Channel *c)
397{
398 unsigned char *ucp;
399 unsigned int proto_len, data_len;
400
401 /* Check if the fixed size part of the packet is in buffer. */
402 if (buffer_len(&c->output) < 12)
403 return 0;
404
405 /* Parse the lengths of variable-length fields. */
406 ucp = (unsigned char *) buffer_ptr(&c->output);
407 if (ucp[0] == 0x42) { /* Byte order MSB first. */
408 proto_len = 256 * ucp[6] + ucp[7];
409 data_len = 256 * ucp[8] + ucp[9];
410 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
411 proto_len = ucp[6] + 256 * ucp[7];
412 data_len = ucp[8] + 256 * ucp[9];
413 } else {
414 debug("Initial X11 packet contains bad byte order byte: 0x%x",
415 ucp[0]);
416 return -1;
417 }
418
419 /* Check if the whole packet is in buffer. */
420 if (buffer_len(&c->output) <
421 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
422 return 0;
423
424 /* Check if authentication protocol matches. */
425 if (proto_len != strlen(x11_saved_proto) ||
426 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
427 debug("X11 connection uses different authentication protocol.");
428 return -1;
429 }
430 /* Check if authentication data matches our fake data. */
431 if (data_len != x11_fake_data_len ||
432 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
433 x11_fake_data, x11_fake_data_len) != 0) {
434 debug("X11 auth data does not match fake data.");
435 return -1;
436 }
437 /* Check fake data length */
438 if (x11_fake_data_len != x11_saved_data_len) {
439 error("X11 fake_data_len %d != saved_data_len %d",
440 x11_fake_data_len, x11_saved_data_len);
441 return -1;
442 }
443 /*
444 * Received authentication protocol and data match
445 * our fake data. Substitute the fake data with real
446 * data.
447 */
448 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
449 x11_saved_data, x11_saved_data_len);
450 return 1;
451}
452
453void
454channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
455{
456 int ret = x11_open_helper(c);
457 if (ret == 1) {
458 /* Start normal processing for the channel. */
459 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000460 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000461 } else if (ret == -1) {
462 /*
463 * We have received an X11 connection that has bad
464 * authentication information.
465 */
466 log("X11 connection rejected because of wrong authentication.\r\n");
467 buffer_clear(&c->input);
468 buffer_clear(&c->output);
469 close(c->sock);
470 c->sock = -1;
471 c->type = SSH_CHANNEL_CLOSED;
472 packet_start(SSH_MSG_CHANNEL_CLOSE);
473 packet_put_int(c->remote_id);
474 packet_send();
475 }
476}
477
478void
Damien Millerbd483e72000-04-30 10:00:53 +1000479channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000480{
481 int ret = x11_open_helper(c);
482 if (ret == 1) {
483 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000484 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000485 } else if (ret == -1) {
486 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000487 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000488 chan_write_failed(c);
489 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
490 }
491}
492
493/* This is our fake X11 server socket. */
494void
495channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
496{
497 struct sockaddr addr;
498 int newsock, newch;
499 socklen_t addrlen;
500 char buf[16384], *remote_hostname;
Damien Millerbd483e72000-04-30 10:00:53 +1000501 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000502
503 if (FD_ISSET(c->sock, readset)) {
504 debug("X11 connection requested.");
505 addrlen = sizeof(addr);
506 newsock = accept(c->sock, &addr, &addrlen);
507 if (newsock < 0) {
508 error("accept: %.100s", strerror(errno));
509 return;
510 }
511 remote_hostname = get_remote_hostname(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000512 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000513 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerbd483e72000-04-30 10:00:53 +1000514 remote_hostname, remote_port);
515
516 newch = channel_new("x11",
517 SSH_CHANNEL_OPENING, newsock, newsock, -1,
518 c->local_window_max, c->local_maxpacket,
519 0, xstrdup(buf));
520 if (compat20) {
521 packet_start(SSH2_MSG_CHANNEL_OPEN);
522 packet_put_cstring("x11");
523 packet_put_int(newch);
524 packet_put_int(c->local_window_max);
525 packet_put_int(c->local_maxpacket);
526 /* originator host and port */
527 packet_put_cstring(remote_hostname);
528 packet_put_int(remote_port);
529 packet_send();
530 } else {
531 packet_start(SSH_SMSG_X11_OPEN);
532 packet_put_int(newch);
533 if (have_hostname_in_open)
534 packet_put_string(buf, strlen(buf));
535 packet_send();
536 }
Damien Millerb38eff82000-04-01 11:09:21 +1000537 xfree(remote_hostname);
Damien Millerb38eff82000-04-01 11:09:21 +1000538 }
539}
540
541/*
542 * This socket is listening for connections to a forwarded TCP/IP port.
543 */
544void
545channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
546{
547 struct sockaddr addr;
548 int newsock, newch;
549 socklen_t addrlen;
550 char buf[1024], *remote_hostname;
551 int remote_port;
552
553 if (FD_ISSET(c->sock, readset)) {
554 debug("Connection to port %d forwarding "
555 "to %.100s port %d requested.",
556 c->listening_port, c->path, c->host_port);
557 addrlen = sizeof(addr);
558 newsock = accept(c->sock, &addr, &addrlen);
559 if (newsock < 0) {
560 error("accept: %.100s", strerror(errno));
561 return;
562 }
563 remote_hostname = get_remote_hostname(newsock);
564 remote_port = get_peer_port(newsock);
565 snprintf(buf, sizeof buf,
566 "listen port %d for %.100s port %d, "
567 "connect from %.200s port %d",
568 c->listening_port, c->path, c->host_port,
569 remote_hostname, remote_port);
570 newch = channel_new("direct-tcpip",
571 SSH_CHANNEL_OPENING, newsock, newsock, -1,
572 c->local_window_max, c->local_maxpacket,
573 0, xstrdup(buf));
Damien Miller33b13562000-04-04 14:38:59 +1000574 if (compat20) {
575 packet_start(SSH2_MSG_CHANNEL_OPEN);
576 packet_put_cstring("direct-tcpip");
577 packet_put_int(newch);
578 packet_put_int(c->local_window_max);
579 packet_put_int(c->local_maxpacket);
Damien Miller4af51302000-04-16 11:18:38 +1000580 /* target host and port */
Damien Miller33b13562000-04-04 14:38:59 +1000581 packet_put_string(c->path, strlen(c->path));
582 packet_put_int(c->host_port);
Damien Miller4af51302000-04-16 11:18:38 +1000583 /* originator host and port */
Damien Miller33b13562000-04-04 14:38:59 +1000584 packet_put_cstring(remote_hostname);
585 packet_put_int(remote_port);
586 packet_send();
587 } else {
588 packet_start(SSH_MSG_PORT_OPEN);
589 packet_put_int(newch);
590 packet_put_string(c->path, strlen(c->path));
591 packet_put_int(c->host_port);
592 if (have_hostname_in_open) {
593 packet_put_string(buf, strlen(buf));
594 }
595 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000596 }
Damien Millerb38eff82000-04-01 11:09:21 +1000597 xfree(remote_hostname);
598 }
599}
600
601/*
602 * This is the authentication agent socket listening for connections from
603 * clients.
604 */
605void
606channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
607{
608 struct sockaddr addr;
609 int newsock, newch;
610 socklen_t addrlen;
611
612 if (FD_ISSET(c->sock, readset)) {
613 addrlen = sizeof(addr);
614 newsock = accept(c->sock, &addr, &addrlen);
615 if (newsock < 0) {
616 error("accept from auth socket: %.100s", strerror(errno));
617 return;
618 }
619 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
620 xstrdup("accepted auth socket"));
621 packet_start(SSH_SMSG_AGENT_OPEN);
622 packet_put_int(newch);
623 packet_send();
624 }
625}
626
627int
628channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
629{
630 char buf[16*1024];
631 int len;
632
633 if (c->rfd != -1 &&
634 FD_ISSET(c->rfd, readset)) {
635 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000636 if (len < 0 && (errno == EINTR || errno == EAGAIN))
637 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000638 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000639 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000640 c->self, c->rfd, len);
641 if (compat13) {
642 buffer_consume(&c->output, buffer_len(&c->output));
643 c->type = SSH_CHANNEL_INPUT_DRAINING;
644 debug("Channel %d status set to input draining.", c->self);
645 } else {
646 chan_read_failed(c);
647 }
648 return -1;
649 }
650 buffer_append(&c->input, buf, len);
651 }
652 return 1;
653}
654int
655channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
656{
657 int len;
658
659 /* Send buffered output data to the socket. */
660 if (c->wfd != -1 &&
661 FD_ISSET(c->wfd, writeset) &&
662 buffer_len(&c->output) > 0) {
663 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000664 buffer_len(&c->output));
665 if (len < 0 && (errno == EINTR || errno == EAGAIN))
666 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000667 if (len <= 0) {
668 if (compat13) {
669 buffer_consume(&c->output, buffer_len(&c->output));
670 debug("Channel %d status set to input draining.", c->self);
671 c->type = SSH_CHANNEL_INPUT_DRAINING;
672 } else {
673 chan_write_failed(c);
674 }
675 return -1;
676 }
677 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000678 if (compat20 && len > 0) {
679 c->local_consumed += len;
680 }
681 }
682 return 1;
683}
684int
685channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
686{
687 char buf[16*1024];
688 int len;
689
Damien Miller1383bd82000-04-06 12:32:37 +1000690/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000691 if (c->efd != -1) {
692 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
693 FD_ISSET(c->efd, writeset) &&
694 buffer_len(&c->extended) > 0) {
695 len = write(c->efd, buffer_ptr(&c->extended),
696 buffer_len(&c->extended));
697 debug("channel %d: written %d to efd %d",
698 c->self, len, c->efd);
699 if (len > 0) {
700 buffer_consume(&c->extended, len);
701 c->local_consumed += len;
702 }
703 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
704 FD_ISSET(c->efd, readset)) {
705 len = read(c->efd, buf, sizeof(buf));
706 debug("channel %d: read %d from efd %d",
707 c->self, len, c->efd);
Damien Miller1383bd82000-04-06 12:32:37 +1000708 if (len == 0) {
709 debug("channel %d: closing efd %d",
710 c->self, c->efd);
711 close(c->efd);
712 c->efd = -1;
713 } else if (len > 0)
Damien Miller33b13562000-04-04 14:38:59 +1000714 buffer_append(&c->extended, buf, len);
715 }
716 }
717 return 1;
718}
719int
720channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
721{
Damien Millerefb4afe2000-04-12 18:45:05 +1000722 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000723 c->local_window < c->local_window_max/2 &&
724 c->local_consumed > 0) {
725 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
726 packet_put_int(c->remote_id);
727 packet_put_int(c->local_consumed);
728 packet_send();
729 debug("channel %d: window %d sent adjust %d",
730 c->self, c->local_window,
731 c->local_consumed);
732 c->local_window += c->local_consumed;
733 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000734 }
735 return 1;
736}
737
738void
739channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
740{
741 channel_handle_rfd(c, readset, writeset);
742 channel_handle_wfd(c, readset, writeset);
743}
744
745void
Damien Miller33b13562000-04-04 14:38:59 +1000746channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
747{
748 channel_handle_rfd(c, readset, writeset);
749 channel_handle_wfd(c, readset, writeset);
750 channel_handle_efd(c, readset, writeset);
751 channel_check_window(c, readset, writeset);
752}
753
754void
Damien Millerb38eff82000-04-01 11:09:21 +1000755channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
756{
757 int len;
758 /* Send buffered output data to the socket. */
759 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
760 len = write(c->sock, buffer_ptr(&c->output),
761 buffer_len(&c->output));
762 if (len <= 0)
763 buffer_consume(&c->output, buffer_len(&c->output));
764 else
765 buffer_consume(&c->output, len);
766 }
767}
768
769void
Damien Miller33b13562000-04-04 14:38:59 +1000770channel_handler_init_20(void)
771{
772 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +1000773 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +1000774 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000775 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller33b13562000-04-04 14:38:59 +1000776
777 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
778 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000779 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller33b13562000-04-04 14:38:59 +1000780}
781
782void
Damien Millerb38eff82000-04-01 11:09:21 +1000783channel_handler_init_13(void)
784{
785 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
786 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
787 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
788 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
789 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
790 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
791 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
792
793 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
794 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
795 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
796 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
797 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
798}
799
800void
801channel_handler_init_15(void)
802{
803 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +1000804 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +1000805 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
806 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
807 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
808
809 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
810 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
811 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
812 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
813}
814
815void
816channel_handler_init(void)
817{
818 int i;
819 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
820 channel_pre[i] = NULL;
821 channel_post[i] = NULL;
822 }
Damien Miller33b13562000-04-04 14:38:59 +1000823 if (compat20)
824 channel_handler_init_20();
825 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000826 channel_handler_init_13();
827 else
828 channel_handler_init_15();
829}
830
Damien Miller4af51302000-04-16 11:18:38 +1000831void
Damien Millerb38eff82000-04-01 11:09:21 +1000832channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
833{
834 static int did_init = 0;
835 int i;
836 Channel *c;
837
838 if (!did_init) {
839 channel_handler_init();
840 did_init = 1;
841 }
842 for (i = 0; i < channels_alloc; i++) {
843 c = &channels[i];
844 if (c->type == SSH_CHANNEL_FREE)
845 continue;
846 if (ftab[c->type] == NULL)
847 continue;
848 (*ftab[c->type])(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +1000849 chan_delete_if_full_closed(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000850 }
851}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852
Damien Miller4af51302000-04-16 11:18:38 +1000853void
Damien Miller95def091999-11-25 00:26:21 +1100854channel_prepare_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000855{
Damien Millerb38eff82000-04-01 11:09:21 +1000856 channel_handler(channel_pre, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000857}
858
Damien Miller4af51302000-04-16 11:18:38 +1000859void
Damien Miller95def091999-11-25 00:26:21 +1100860channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861{
Damien Millerb38eff82000-04-01 11:09:21 +1000862 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863}
864
865/* If there is data to send to the connection, send some of it now. */
866
Damien Miller4af51302000-04-16 11:18:38 +1000867void
Damien Miller95def091999-11-25 00:26:21 +1100868channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869{
Damien Miller95def091999-11-25 00:26:21 +1100870 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +1000871 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872
Damien Miller95def091999-11-25 00:26:21 +1100873 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +1000874 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +1100875
Damien Miller5428f641999-11-25 11:54:57 +1100876 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +1100877 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000878 if (c->type != SSH_CHANNEL_OPEN &&
879 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +1100880 continue;
881 } else {
Damien Millerb38eff82000-04-01 11:09:21 +1000882 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100883 continue;
Damien Millerb38eff82000-04-01 11:09:21 +1000884 if (c->istate != CHAN_INPUT_OPEN &&
885 c->istate != CHAN_INPUT_WAIT_DRAIN)
Damien Miller34132e52000-01-14 15:45:46 +1100886 continue;
887 }
Damien Millerefb4afe2000-04-12 18:45:05 +1000888 if (compat20 &&
889 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Damien Miller33b13562000-04-04 14:38:59 +1000890 debug("channel: %d: no data after CLOSE", c->self);
891 continue;
892 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893
Damien Miller95def091999-11-25 00:26:21 +1100894 /* Get the amount of buffered data for this channel. */
Damien Millerb38eff82000-04-01 11:09:21 +1000895 len = buffer_len(&c->input);
Damien Miller95def091999-11-25 00:26:21 +1100896 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100897 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +1000898 if (compat20) {
899 if (len > c->remote_window)
900 len = c->remote_window;
901 if (len > c->remote_maxpacket)
902 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +1100903 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000904 if (packet_is_interactive()) {
905 if (len > 1024)
906 len = 512;
907 } else {
908 /* Keep the packets at reasonable size. */
909 if (len > packet_get_maxsize()/2)
910 len = packet_get_maxsize()/2;
911 }
Damien Miller95def091999-11-25 00:26:21 +1100912 }
Damien Millerb38eff82000-04-01 11:09:21 +1000913 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000914 packet_start(compat20 ?
915 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +1000916 packet_put_int(c->remote_id);
917 packet_put_string(buffer_ptr(&c->input), len);
918 packet_send();
919 buffer_consume(&c->input, len);
920 c->remote_window -= len;
Damien Miller33b13562000-04-04 14:38:59 +1000921 debug("channel %d: send data len %d", c->self, len);
Damien Millerb38eff82000-04-01 11:09:21 +1000922 }
923 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +1100924 if (compat13)
925 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +1100926 /*
927 * input-buffer is empty and read-socket shutdown:
928 * tell peer, that we will not send more data: send IEOF
929 */
Damien Millerb38eff82000-04-01 11:09:21 +1000930 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +1100931 }
Damien Miller33b13562000-04-04 14:38:59 +1000932 /* Send extended data, i.e. stderr */
933 if (compat20 &&
934 c->remote_window > 0 &&
935 (len = buffer_len(&c->extended)) > 0 &&
936 c->extended_usage == CHAN_EXTENDED_READ) {
937 if (len > c->remote_window)
938 len = c->remote_window;
939 if (len > c->remote_maxpacket)
940 len = c->remote_maxpacket;
941 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
942 packet_put_int(c->remote_id);
943 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
944 packet_put_string(buffer_ptr(&c->extended), len);
945 packet_send();
946 buffer_consume(&c->extended, len);
947 c->remote_window -= len;
948 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000950}
951
Damien Miller5428f641999-11-25 11:54:57 +1100952/*
953 * This is called when a packet of type CHANNEL_DATA has just been received.
954 * The message type has already been consumed, but channel number and data is
955 * still there.
956 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957
Damien Miller4af51302000-04-16 11:18:38 +1000958void
Damien Millerb38eff82000-04-01 11:09:21 +1000959channel_input_data(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960{
Damien Miller34132e52000-01-14 15:45:46 +1100961 int id;
Damien Miller95def091999-11-25 00:26:21 +1100962 char *data;
963 unsigned int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000964 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965
Damien Miller95def091999-11-25 00:26:21 +1100966 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +1100967 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +1000968 c = channel_lookup(id);
969 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +1100970 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000971
Damien Miller95def091999-11-25 00:26:21 +1100972 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +1000973 if (c->type != SSH_CHANNEL_OPEN &&
974 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +1100975 return;
976
977 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +1000978 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +1100979 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 /* Get the data. */
982 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +1000983 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +1000984
Damien Miller33b13562000-04-04 14:38:59 +1000985 if (compat20){
986 if (data_len > c->local_maxpacket) {
987 log("channel %d: rcvd big packet %d, maxpack %d",
988 c->self, data_len, c->local_maxpacket);
989 }
990 if (data_len > c->local_window) {
991 log("channel %d: rcvd too much data %d, win %d",
992 c->self, data_len, c->local_window);
993 xfree(data);
994 return;
995 }
996 c->local_window -= data_len;
997 }else{
998 packet_integrity_check(plen, 4 + 4 + data_len, type);
999 }
Damien Millerb38eff82000-04-01 11:09:21 +10001000 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001001 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002}
Damien Miller4af51302000-04-16 11:18:38 +10001003void
Damien Miller33b13562000-04-04 14:38:59 +10001004channel_input_extended_data(int type, int plen)
1005{
1006 int id;
1007 int tcode;
1008 char *data;
1009 unsigned int data_len;
1010 Channel *c;
1011
1012 /* Get the channel number and verify it. */
1013 id = packet_get_int();
1014 c = channel_lookup(id);
1015
1016 if (c == NULL)
1017 packet_disconnect("Received extended_data for bad channel %d.", id);
1018 if (c->type != SSH_CHANNEL_OPEN) {
1019 log("channel %d: ext data for non open", id);
1020 return;
1021 }
1022 tcode = packet_get_int();
1023 if (c->efd == -1 ||
1024 c->extended_usage != CHAN_EXTENDED_WRITE ||
1025 tcode != SSH2_EXTENDED_DATA_STDERR) {
1026 log("channel %d: bad ext data", c->self);
1027 return;
1028 }
1029 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001030 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001031 if (data_len > c->local_window) {
1032 log("channel %d: rcvd too much extended_data %d, win %d",
1033 c->self, data_len, c->local_window);
1034 xfree(data);
1035 return;
1036 }
1037 debug("channel %d: rcvd ext data %d", c->self, data_len);
1038 c->local_window -= data_len;
1039 buffer_append(&c->extended, data, data_len);
1040 xfree(data);
1041}
1042
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043
Damien Miller5428f641999-11-25 11:54:57 +11001044/*
1045 * Returns true if no channel has too much buffered data, and false if one or
1046 * more channel is overfull.
1047 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048
Damien Miller4af51302000-04-16 11:18:38 +10001049int
Damien Miller95def091999-11-25 00:26:21 +11001050channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051{
Damien Miller95def091999-11-25 00:26:21 +11001052 unsigned int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001053 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001054
1055 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001056 c = &channels[i];
1057 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001058 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001059 debug("channel %d: big input buffer %d",
1060 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001061 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001062 }
1063 if (buffer_len(&c->output) > packet_get_maxsize()) {
1064 debug("channel %d: big output buffer %d",
1065 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001066 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001067 }
Damien Miller95def091999-11-25 00:26:21 +11001068 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001069 }
Damien Miller95def091999-11-25 00:26:21 +11001070 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001071}
1072
Damien Miller4af51302000-04-16 11:18:38 +10001073void
Damien Millerb38eff82000-04-01 11:09:21 +10001074channel_input_ieof(int type, int plen)
1075{
1076 int id;
1077 Channel *c;
1078
1079 packet_integrity_check(plen, 4, type);
1080
1081 id = packet_get_int();
1082 c = channel_lookup(id);
1083 if (c == NULL)
1084 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1085 chan_rcvd_ieof(c);
1086}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087
Damien Miller4af51302000-04-16 11:18:38 +10001088void
Damien Millerb38eff82000-04-01 11:09:21 +10001089channel_input_close(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090{
Damien Millerb38eff82000-04-01 11:09:21 +10001091 int id;
1092 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001093
Damien Millerb38eff82000-04-01 11:09:21 +10001094 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001095
Damien Millerb38eff82000-04-01 11:09:21 +10001096 id = packet_get_int();
1097 c = channel_lookup(id);
1098 if (c == NULL)
1099 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001100
1101 /*
1102 * Send a confirmation that we have closed the channel and no more
1103 * data is coming for it.
1104 */
Damien Miller95def091999-11-25 00:26:21 +11001105 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001106 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001107 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108
Damien Miller5428f641999-11-25 11:54:57 +11001109 /*
1110 * If the channel is in closed state, we have sent a close request,
1111 * and the other side will eventually respond with a confirmation.
1112 * Thus, we cannot free the channel here, because then there would be
1113 * no-one to receive the confirmation. The channel gets freed when
1114 * the confirmation arrives.
1115 */
Damien Millerb38eff82000-04-01 11:09:21 +10001116 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001117 /*
1118 * Not a closed channel - mark it as draining, which will
1119 * cause it to be freed later.
1120 */
Damien Millerb38eff82000-04-01 11:09:21 +10001121 buffer_consume(&c->input, buffer_len(&c->input));
1122 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001123 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001124}
1125
Damien Millerb38eff82000-04-01 11:09:21 +10001126/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001127void
Damien Millerb38eff82000-04-01 11:09:21 +10001128channel_input_oclose(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001129{
Damien Millerb38eff82000-04-01 11:09:21 +10001130 int id = packet_get_int();
1131 Channel *c = channel_lookup(id);
1132 packet_integrity_check(plen, 4, type);
1133 if (c == NULL)
1134 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1135 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136}
1137
Damien Miller4af51302000-04-16 11:18:38 +10001138void
Damien Millerb38eff82000-04-01 11:09:21 +10001139channel_input_close_confirmation(int type, int plen)
1140{
1141 int id = packet_get_int();
1142 Channel *c = channel_lookup(id);
1143
Damien Miller4af51302000-04-16 11:18:38 +10001144 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001145 if (c == NULL)
1146 packet_disconnect("Received close confirmation for "
1147 "out-of-range channel %d.", id);
1148 if (c->type != SSH_CHANNEL_CLOSED)
1149 packet_disconnect("Received close confirmation for "
1150 "non-closed channel %d (type %d).", id, c->type);
1151 channel_free(c->self);
1152}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001153
Damien Miller4af51302000-04-16 11:18:38 +10001154void
Damien Millerb38eff82000-04-01 11:09:21 +10001155channel_input_open_confirmation(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156{
Damien Millerb38eff82000-04-01 11:09:21 +10001157 int id, remote_id;
1158 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159
Damien Miller33b13562000-04-04 14:38:59 +10001160 if (!compat20)
1161 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162
Damien Millerb38eff82000-04-01 11:09:21 +10001163 id = packet_get_int();
1164 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165
Damien Millerb38eff82000-04-01 11:09:21 +10001166 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1167 packet_disconnect("Received open confirmation for "
1168 "non-opening channel %d.", id);
1169 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001170 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001171 c->remote_id = remote_id;
1172 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001173
1174 if (compat20) {
1175 c->remote_window = packet_get_int();
1176 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001177 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001178 if (c->cb_fn != NULL && c->cb_event == type) {
1179 debug("callback start");
1180 c->cb_fn(c->self, c->cb_arg);
1181 debug("callback done");
1182 }
1183 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1184 c->remote_window, c->remote_maxpacket);
1185 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186}
1187
Damien Miller4af51302000-04-16 11:18:38 +10001188void
Damien Millerb38eff82000-04-01 11:09:21 +10001189channel_input_open_failure(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190{
Damien Millerb38eff82000-04-01 11:09:21 +10001191 int id;
1192 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193
Damien Miller33b13562000-04-04 14:38:59 +10001194 if (!compat20)
1195 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001196
1197 id = packet_get_int();
1198 c = channel_lookup(id);
1199
1200 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1201 packet_disconnect("Received open failure for "
1202 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001203 if (compat20) {
1204 int reason = packet_get_int();
1205 char *msg = packet_get_string(NULL);
Damien Miller4af51302000-04-16 11:18:38 +10001206 char *lang = packet_get_string(NULL);
Damien Miller33b13562000-04-04 14:38:59 +10001207 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
Damien Miller4af51302000-04-16 11:18:38 +10001208 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001209 xfree(msg);
Damien Miller4af51302000-04-16 11:18:38 +10001210 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001211 }
Damien Miller95def091999-11-25 00:26:21 +11001212 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001213 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214}
1215
Damien Miller33b13562000-04-04 14:38:59 +10001216void
1217channel_input_channel_request(int type, int plen)
1218{
1219 int id;
1220 Channel *c;
1221
1222 id = packet_get_int();
1223 c = channel_lookup(id);
1224
1225 if (c == NULL ||
1226 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1227 packet_disconnect("Received request for "
1228 "non-open channel %d.", id);
1229 if (c->cb_fn != NULL && c->cb_event == type) {
1230 debug("callback start");
1231 c->cb_fn(c->self, c->cb_arg);
1232 debug("callback done");
1233 } else {
1234 char *service = packet_get_string(NULL);
1235 debug("channel: %d rcvd request for %s", c->self, service);
1236debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
1237 xfree(service);
1238 }
1239}
1240
Damien Miller4af51302000-04-16 11:18:38 +10001241void
Damien Miller33b13562000-04-04 14:38:59 +10001242channel_input_window_adjust(int type, int plen)
1243{
1244 Channel *c;
1245 int id, adjust;
1246
1247 if (!compat20)
1248 return;
1249
1250 /* Get the channel number and verify it. */
1251 id = packet_get_int();
1252 c = channel_lookup(id);
1253
1254 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1255 log("Received window adjust for "
1256 "non-open channel %d.", id);
1257 return;
1258 }
1259 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001260 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001261 debug("channel %d: rcvd adjust %d", id, adjust);
1262 c->remote_window += adjust;
1263}
1264
Damien Miller5428f641999-11-25 11:54:57 +11001265/*
1266 * Stops listening for channels, and removes any unix domain sockets that we
1267 * might have.
1268 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269
Damien Miller4af51302000-04-16 11:18:38 +10001270void
Damien Miller95def091999-11-25 00:26:21 +11001271channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001272{
Damien Miller95def091999-11-25 00:26:21 +11001273 int i;
1274 for (i = 0; i < channels_alloc; i++) {
1275 switch (channels[i].type) {
1276 case SSH_CHANNEL_AUTH_SOCKET:
1277 close(channels[i].sock);
1278 remove(channels[i].path);
1279 channel_free(i);
1280 break;
1281 case SSH_CHANNEL_PORT_LISTENER:
1282 case SSH_CHANNEL_X11_LISTENER:
1283 close(channels[i].sock);
1284 channel_free(i);
1285 break;
1286 default:
1287 break;
1288 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001289 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001290}
1291
Damien Miller5428f641999-11-25 11:54:57 +11001292/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001293 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001294 * descriptors after a fork.
1295 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
Damien Miller4af51302000-04-16 11:18:38 +10001297void
Damien Miller95def091999-11-25 00:26:21 +11001298channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001299{
Damien Miller95def091999-11-25 00:26:21 +11001300 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001301 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001302 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001303 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001304}
1305
1306/* Returns the maximum file descriptor number used by the channels. */
1307
Damien Miller4af51302000-04-16 11:18:38 +10001308int
Damien Miller95def091999-11-25 00:26:21 +11001309channel_max_fd()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310{
Damien Miller95def091999-11-25 00:26:21 +11001311 return channel_max_fd_value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312}
1313
1314/* Returns true if any channel is still open. */
1315
Damien Miller4af51302000-04-16 11:18:38 +10001316int
Damien Miller95def091999-11-25 00:26:21 +11001317channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318{
Damien Miller95def091999-11-25 00:26:21 +11001319 unsigned int i;
1320 for (i = 0; i < channels_alloc; i++)
1321 switch (channels[i].type) {
1322 case SSH_CHANNEL_FREE:
1323 case SSH_CHANNEL_X11_LISTENER:
1324 case SSH_CHANNEL_PORT_LISTENER:
1325 case SSH_CHANNEL_CLOSED:
1326 case SSH_CHANNEL_AUTH_SOCKET:
1327 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001328 case SSH_CHANNEL_LARVAL:
1329 if (!compat20)
1330 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1331 continue;
Damien Miller95def091999-11-25 00:26:21 +11001332 case SSH_CHANNEL_OPENING:
1333 case SSH_CHANNEL_OPEN:
1334 case SSH_CHANNEL_X11_OPEN:
1335 return 1;
1336 case SSH_CHANNEL_INPUT_DRAINING:
1337 case SSH_CHANNEL_OUTPUT_DRAINING:
1338 if (!compat13)
1339 fatal("cannot happen: OUT_DRAIN");
1340 return 1;
1341 default:
1342 fatal("channel_still_open: bad channel type %d", channels[i].type);
1343 /* NOTREACHED */
1344 }
1345 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346}
1347
Damien Miller5428f641999-11-25 11:54:57 +11001348/*
1349 * Returns a message describing the currently open forwarded connections,
1350 * suitable for sending to the client. The message contains crlf pairs for
1351 * newlines.
1352 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353
Damien Miller95def091999-11-25 00:26:21 +11001354char *
1355channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001356{
Damien Miller95def091999-11-25 00:26:21 +11001357 Buffer buffer;
1358 int i;
1359 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360
Damien Miller95def091999-11-25 00:26:21 +11001361 buffer_init(&buffer);
1362 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001364 for (i = 0; i < channels_alloc; i++) {
1365 Channel *c = &channels[i];
1366 switch (c->type) {
1367 case SSH_CHANNEL_FREE:
1368 case SSH_CHANNEL_X11_LISTENER:
1369 case SSH_CHANNEL_PORT_LISTENER:
1370 case SSH_CHANNEL_CLOSED:
1371 case SSH_CHANNEL_AUTH_SOCKET:
1372 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001373 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001374 case SSH_CHANNEL_OPENING:
1375 case SSH_CHANNEL_OPEN:
1376 case SSH_CHANNEL_X11_OPEN:
1377 case SSH_CHANNEL_INPUT_DRAINING:
1378 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001379 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 +11001380 c->self, c->remote_name,
1381 c->type, c->remote_id,
1382 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001383 c->ostate, buffer_len(&c->output),
1384 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001385 buffer_append(&buffer, buf, strlen(buf));
1386 continue;
1387 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001388 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001389 /* NOTREACHED */
1390 }
1391 }
1392 buffer_append(&buffer, "\0", 1);
1393 cp = xstrdup(buffer_ptr(&buffer));
1394 buffer_free(&buffer);
1395 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396}
1397
Damien Miller5428f641999-11-25 11:54:57 +11001398/*
1399 * Initiate forwarding of connections to local port "port" through the secure
1400 * channel to host:port from remote side.
1401 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402
Damien Miller4af51302000-04-16 11:18:38 +10001403void
Damien Milleraae6c611999-12-06 11:47:28 +11001404channel_request_local_forwarding(u_short port, const char *host,
Damien Millera34a28b1999-12-14 10:47:15 +11001405 u_short host_port, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001406{
Damien Miller34132e52000-01-14 15:45:46 +11001407 int success, ch, sock, on = 1;
1408 struct addrinfo hints, *ai, *aitop;
1409 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller5428f641999-11-25 11:54:57 +11001410 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001411
Damien Miller95def091999-11-25 00:26:21 +11001412 if (strlen(host) > sizeof(channels[0].path) - 1)
1413 packet_disconnect("Forward host name too long.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001414
Damien Miller5428f641999-11-25 11:54:57 +11001415 /*
Damien Miller34132e52000-01-14 15:45:46 +11001416 * getaddrinfo returns a loopback address if the hostname is
1417 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001418 */
Damien Miller34132e52000-01-14 15:45:46 +11001419 memset(&hints, 0, sizeof(hints));
1420 hints.ai_family = IPv4or6;
1421 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1422 hints.ai_socktype = SOCK_STREAM;
1423 snprintf(strport, sizeof strport, "%d", port);
1424 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1425 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001426
Damien Miller34132e52000-01-14 15:45:46 +11001427 success = 0;
1428 for (ai = aitop; ai; ai = ai->ai_next) {
1429 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1430 continue;
1431 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1432 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1433 error("channel_request_local_forwarding: getnameinfo failed");
1434 continue;
1435 }
1436 /* Create a port to listen for the host. */
1437 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1438 if (sock < 0) {
1439 /* this is no error since kernel may not support ipv6 */
1440 verbose("socket: %.100s", strerror(errno));
1441 continue;
1442 }
1443 /*
1444 * Set socket options. We would like the socket to disappear
1445 * as soon as it has been closed for whatever reason.
1446 */
1447 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1448 linger.l_onoff = 1;
1449 linger.l_linger = 5;
1450 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1451 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001452
Damien Miller34132e52000-01-14 15:45:46 +11001453 /* Bind the socket to the address. */
1454 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1455 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001456 if (!ai->ai_next)
1457 error("bind: %.100s", strerror(errno));
1458 else
1459 verbose("bind: %.100s", strerror(errno));
1460
Damien Miller34132e52000-01-14 15:45:46 +11001461 close(sock);
1462 continue;
1463 }
1464 /* Start listening for connections on the socket. */
1465 if (listen(sock, 5) < 0) {
1466 error("listen: %.100s", strerror(errno));
1467 close(sock);
1468 continue;
1469 }
1470 /* Allocate a channel number for the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001471 ch = channel_new(
1472 "port listener", SSH_CHANNEL_PORT_LISTENER,
1473 sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001474 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb38eff82000-04-01 11:09:21 +10001475 0, xstrdup("port listener"));
Damien Miller34132e52000-01-14 15:45:46 +11001476 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
1477 channels[ch].host_port = host_port;
1478 channels[ch].listening_port = port;
1479 success = 1;
1480 }
1481 if (success == 0)
1482 packet_disconnect("cannot listen port: %d", port);
1483 freeaddrinfo(aitop);
Damien Miller95def091999-11-25 00:26:21 +11001484}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485
Damien Miller5428f641999-11-25 11:54:57 +11001486/*
1487 * Initiate forwarding of connections to port "port" on remote host through
1488 * the secure channel to host:port from local side.
1489 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
Damien Miller4af51302000-04-16 11:18:38 +10001491void
Damien Millerb38eff82000-04-01 11:09:21 +10001492channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1493 u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494{
Damien Miller95def091999-11-25 00:26:21 +11001495 int payload_len;
1496 /* Record locally that connection to this host/port is permitted. */
1497 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1498 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001499
Damien Millerb38eff82000-04-01 11:09:21 +10001500 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1501 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1502 permitted_opens[num_permitted_opens].listen_port = listen_port;
Damien Miller95def091999-11-25 00:26:21 +11001503 num_permitted_opens++;
1504
1505 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001506 if (compat20) {
1507 const char *address_to_bind = "0.0.0.0";
1508 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1509 packet_put_cstring("tcpip-forward");
1510 packet_put_char(0); /* boolean: want reply */
1511 packet_put_cstring(address_to_bind);
1512 packet_put_int(listen_port);
1513 } else {
1514 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001515 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001516 packet_put_cstring(host_to_connect);
1517 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001518 packet_send();
1519 packet_write_wait();
1520 /*
1521 * Wait for response from the remote side. It will send a disconnect
1522 * message on failure, and we will never see it here.
1523 */
1524 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1525 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526}
1527
Damien Miller5428f641999-11-25 11:54:57 +11001528/*
1529 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1530 * listening for the port, and sends back a success reply (or disconnect
1531 * message if there was an error). This never returns if there was an error.
1532 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533
Damien Miller4af51302000-04-16 11:18:38 +10001534void
Damien Miller95def091999-11-25 00:26:21 +11001535channel_input_port_forward_request(int is_root)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536{
Damien Milleraae6c611999-12-06 11:47:28 +11001537 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001538 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539
Damien Miller95def091999-11-25 00:26:21 +11001540 /* Get arguments from the packet. */
1541 port = packet_get_int();
1542 hostname = packet_get_string(NULL);
1543 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544
Damien Miller5428f641999-11-25 11:54:57 +11001545 /*
1546 * Check that an unprivileged user is not trying to forward a
1547 * privileged port.
1548 */
Damien Miller95def091999-11-25 00:26:21 +11001549 if (port < IPPORT_RESERVED && !is_root)
1550 packet_disconnect("Requested forwarding of port %d but user is not root.",
1551 port);
Damien Millera34a28b1999-12-14 10:47:15 +11001552 /*
1553 * Initiate forwarding,
1554 * bind port to localhost only (gateway ports == 0).
1555 */
1556 channel_request_local_forwarding(port, hostname, host_port, 0);
Damien Miller95def091999-11-25 00:26:21 +11001557
1558 /* Free the argument string. */
1559 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001560}
1561
Damien Millerb38eff82000-04-01 11:09:21 +10001562/* XXX move to aux.c */
1563int
1564channel_connect_to(const char *host, u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565{
Damien Miller34132e52000-01-14 15:45:46 +11001566 struct addrinfo hints, *ai, *aitop;
1567 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1568 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001569 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001570
Damien Miller34132e52000-01-14 15:45:46 +11001571 memset(&hints, 0, sizeof(hints));
1572 hints.ai_family = IPv4or6;
1573 hints.ai_socktype = SOCK_STREAM;
1574 snprintf(strport, sizeof strport, "%d", host_port);
1575 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1576 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001577 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001578 }
Damien Miller34132e52000-01-14 15:45:46 +11001579 for (ai = aitop; ai; ai = ai->ai_next) {
1580 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1581 continue;
1582 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1583 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001584 error("channel_connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001585 continue;
1586 }
1587 /* Create the socket. */
1588 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1589 if (sock < 0) {
1590 error("socket: %.100s", strerror(errno));
1591 continue;
1592 }
1593 /* Connect to the host/port. */
1594 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1595 error("connect %.100s port %s: %.100s", ntop, strport,
1596 strerror(errno));
1597 close(sock);
1598 continue; /* fail -- try next */
1599 }
1600 break; /* success */
1601
1602 }
1603 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001604 if (!ai) {
1605 error("connect %.100s port %d: failed.", host, host_port);
Damien Millerb38eff82000-04-01 11:09:21 +10001606 return -1;
1607 }
1608 /* success */
1609 return sock;
1610}
1611
1612/*
1613 * This is called after receiving PORT_OPEN message. This attempts to
1614 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1615 * or CHANNEL_OPEN_FAILURE.
1616 */
1617
Damien Miller4af51302000-04-16 11:18:38 +10001618void
Damien Millerb38eff82000-04-01 11:09:21 +10001619channel_input_port_open(int type, int plen)
1620{
1621 u_short host_port;
1622 char *host, *originator_string;
1623 int remote_channel, sock = -1, newch, i, denied;
1624 unsigned int host_len, originator_len;
1625
1626 /* Get remote channel number. */
1627 remote_channel = packet_get_int();
1628
1629 /* Get host name to connect to. */
1630 host = packet_get_string(&host_len);
1631
1632 /* Get port to connect to. */
1633 host_port = packet_get_int();
1634
1635 /* Get remote originator name. */
1636 if (have_hostname_in_open) {
1637 originator_string = packet_get_string(&originator_len);
1638 originator_len += 4; /* size of packet_int */
1639 } else {
1640 originator_string = xstrdup("unknown (remote did not supply name)");
1641 originator_len = 0; /* no originator supplied */
Damien Miller95def091999-11-25 00:26:21 +11001642 }
Damien Miller34132e52000-01-14 15:45:46 +11001643
Damien Millerb38eff82000-04-01 11:09:21 +10001644 packet_integrity_check(plen,
1645 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001646
Damien Millerb38eff82000-04-01 11:09:21 +10001647 /* Check if opening that port is permitted. */
1648 denied = 0;
1649 if (!all_opens_permitted) {
1650 /* Go trough all permitted ports. */
1651 for (i = 0; i < num_permitted_opens; i++)
1652 if (permitted_opens[i].port_to_connect == host_port &&
1653 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1654 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655
Damien Millerb38eff82000-04-01 11:09:21 +10001656 /* Check if we found the requested port among those permitted. */
1657 if (i >= num_permitted_opens) {
1658 /* The port is not permitted. */
1659 log("Received request to connect to %.100s:%d, but the request was denied.",
1660 host, host_port);
1661 denied = 1;
1662 }
1663 }
1664 sock = denied ? -1 : channel_connect_to(host, host_port);
1665 if (sock > 0) {
1666 /* Allocate a channel for this connection. */
1667 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1668 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001669
Damien Millerb38eff82000-04-01 11:09:21 +10001670 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1671 packet_put_int(remote_channel);
1672 packet_put_int(newch);
1673 packet_send();
1674 } else {
1675 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1676 packet_put_int(remote_channel);
1677 packet_send();
1678 }
Damien Miller95def091999-11-25 00:26:21 +11001679 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001680}
1681
Damien Miller5428f641999-11-25 11:54:57 +11001682/*
1683 * Creates an internet domain socket for listening for X11 connections.
1684 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1685 * occurs.
1686 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001687
Damien Miller34132e52000-01-14 15:45:46 +11001688#define NUM_SOCKS 10
1689
Damien Miller95def091999-11-25 00:26:21 +11001690char *
Damien Millera34a28b1999-12-14 10:47:15 +11001691x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692{
Damien Milleraae6c611999-12-06 11:47:28 +11001693 int display_number, sock;
1694 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001695 struct addrinfo hints, *ai, *aitop;
1696 char strport[NI_MAXSERV];
1697 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1698 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001699 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001700
Damien Millera34a28b1999-12-14 10:47:15 +11001701 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001702 display_number < MAX_DISPLAYS;
1703 display_number++) {
1704 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001705 memset(&hints, 0, sizeof(hints));
1706 hints.ai_family = IPv4or6;
1707 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1708 hints.ai_socktype = SOCK_STREAM;
1709 snprintf(strport, sizeof strport, "%d", port);
1710 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1711 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001712 return NULL;
1713 }
Damien Miller34132e52000-01-14 15:45:46 +11001714 for (ai = aitop; ai; ai = ai->ai_next) {
1715 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1716 continue;
1717 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1718 if (sock < 0) {
Damien Millere2192732000-01-17 13:22:55 +11001719 if (errno != EINVAL) {
1720 error("socket: %.100s", strerror(errno));
1721 return NULL;
1722 } else {
1723 debug("Socket family %d not supported [X11 disp create]", ai->ai_family);
1724 continue;
1725 }
Damien Miller34132e52000-01-14 15:45:46 +11001726 }
1727 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1728 debug("bind port %d: %.100s", port, strerror(errno));
1729 shutdown(sock, SHUT_RDWR);
1730 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11001731
1732 if (ai->ai_next)
1733 continue;
1734
Damien Miller34132e52000-01-14 15:45:46 +11001735 for (n = 0; n < num_socks; n++) {
1736 shutdown(socks[n], SHUT_RDWR);
1737 close(socks[n]);
1738 }
1739 num_socks = 0;
1740 break;
1741 }
1742 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11001743#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11001744 if (num_socks == NUM_SOCKS)
1745 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11001746#else
1747 break;
1748#endif
Damien Miller95def091999-11-25 00:26:21 +11001749 }
Damien Miller34132e52000-01-14 15:45:46 +11001750 if (num_socks > 0)
1751 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001752 }
Damien Miller95def091999-11-25 00:26:21 +11001753 if (display_number >= MAX_DISPLAYS) {
1754 error("Failed to allocate internet-domain X11 display socket.");
1755 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001756 }
Damien Miller95def091999-11-25 00:26:21 +11001757 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11001758 for (n = 0; n < num_socks; n++) {
1759 sock = socks[n];
1760 if (listen(sock, 5) < 0) {
1761 error("listen: %.100s", strerror(errno));
1762 shutdown(sock, SHUT_RDWR);
1763 close(sock);
1764 return NULL;
1765 }
Damien Miller95def091999-11-25 00:26:21 +11001766 }
Damien Miller34132e52000-01-14 15:45:46 +11001767
Damien Miller95def091999-11-25 00:26:21 +11001768 /* Set up a suitable value for the DISPLAY variable. */
Damien Miller76112de1999-12-21 11:18:08 +11001769
Damien Miller95def091999-11-25 00:26:21 +11001770 if (gethostname(hostname, sizeof(hostname)) < 0)
1771 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11001772
1773#ifdef IPADDR_IN_DISPLAY
1774 /*
1775 * HPUX detects the local hostname in the DISPLAY variable and tries
1776 * to set up a shared memory connection to the server, which it
1777 * incorrectly supposes to be local.
1778 *
1779 * The workaround - as used in later $$H and other programs - is
1780 * is to set display to the host's IP address.
1781 */
1782 {
1783 struct hostent *he;
1784 struct in_addr my_addr;
1785
1786 he = gethostbyname(hostname);
1787 if (he == NULL) {
1788 error("[X11-broken-fwd-hostname-workaround] Could not get "
1789 "IP address for hostname %s.", hostname);
1790
1791 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1792 "Could not get IP address for hostname %s.", hostname);
1793
1794 shutdown(sock, SHUT_RDWR);
1795 close(sock);
1796
1797 return NULL;
1798 }
1799
1800 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1801
1802 /* Set DISPLAY to <ip address>:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001803 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Damien Miller76112de1999-12-21 11:18:08 +11001804 display_number, screen_number);
1805 }
1806#else /* IPADDR_IN_DISPLAY */
1807 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11001808 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Damien Miller76112de1999-12-21 11:18:08 +11001809 display_number, screen_number);
1810#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Damien Miller34132e52000-01-14 15:45:46 +11001812 /* Allocate a channel for each socket. */
1813 for (n = 0; n < num_socks; n++) {
1814 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10001815 (void) channel_new("x11 listener",
1816 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
1817 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1818 0, xstrdup("X11 inet listener"));
Damien Miller34132e52000-01-14 15:45:46 +11001819 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001820
Damien Miller95def091999-11-25 00:26:21 +11001821 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11001822 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823}
1824
1825#ifndef X_UNIX_PATH
1826#define X_UNIX_PATH "/tmp/.X11-unix/X"
1827#endif
1828
1829static
1830int
Damien Miller81b25cf1999-12-07 16:47:28 +11001831connect_local_xsocket(unsigned int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001832{
Damien Miller95def091999-11-25 00:26:21 +11001833 static const char *const x_sockets[] = {
1834 X_UNIX_PATH "%u",
1835 "/var/X/.X11-unix/X" "%u",
1836 "/usr/spool/sockets/X11/" "%u",
1837 NULL
1838 };
1839 int sock;
1840 struct sockaddr_un addr;
1841 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001842
Damien Miller95def091999-11-25 00:26:21 +11001843 for (path = x_sockets; *path; ++path) {
1844 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1845 if (sock < 0)
1846 error("socket: %.100s", strerror(errno));
1847 memset(&addr, 0, sizeof(addr));
1848 addr.sun_family = AF_UNIX;
1849 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1850 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1851 return sock;
1852 close(sock);
1853 }
1854 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1855 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001856}
1857
Damien Millerbd483e72000-04-30 10:00:53 +10001858int
1859x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860{
Damien Millerbd483e72000-04-30 10:00:53 +10001861 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11001862 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10001863 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11001864 struct addrinfo hints, *ai, *aitop;
1865 char strport[NI_MAXSERV];
1866 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001867
Damien Miller95def091999-11-25 00:26:21 +11001868 /* Try to open a socket for the local X server. */
1869 display = getenv("DISPLAY");
1870 if (!display) {
1871 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10001872 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873 }
Damien Miller5428f641999-11-25 11:54:57 +11001874 /*
1875 * Now we decode the value of the DISPLAY variable and make a
1876 * connection to the real X server.
1877 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878
Damien Miller5428f641999-11-25 11:54:57 +11001879 /*
1880 * Check if it is a unix domain socket. Unix domain displays are in
1881 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1882 */
Damien Miller95def091999-11-25 00:26:21 +11001883 if (strncmp(display, "unix:", 5) == 0 ||
1884 display[0] == ':') {
1885 /* Connect to the unix domain socket. */
1886 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1887 error("Could not parse display number from DISPLAY: %.100s",
1888 display);
Damien Millerbd483e72000-04-30 10:00:53 +10001889 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001890 }
1891 /* Create a socket. */
1892 sock = connect_local_xsocket(display_number);
1893 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10001894 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001895
1896 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10001897 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001898 }
Damien Miller5428f641999-11-25 11:54:57 +11001899 /*
1900 * Connect to an inet socket. The DISPLAY value is supposedly
1901 * hostname:d[.s], where hostname may also be numeric IP address.
1902 */
Damien Miller95def091999-11-25 00:26:21 +11001903 strncpy(buf, display, sizeof(buf));
1904 buf[sizeof(buf) - 1] = 0;
1905 cp = strchr(buf, ':');
1906 if (!cp) {
1907 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10001908 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001909 }
Damien Miller95def091999-11-25 00:26:21 +11001910 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11001911 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11001912 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1913 error("Could not parse display number from DISPLAY: %.100s",
1914 display);
Damien Millerbd483e72000-04-30 10:00:53 +10001915 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001916 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001917
Damien Miller34132e52000-01-14 15:45:46 +11001918 /* Look up the host address */
1919 memset(&hints, 0, sizeof(hints));
1920 hints.ai_family = IPv4or6;
1921 hints.ai_socktype = SOCK_STREAM;
1922 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
1923 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1924 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10001925 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001926 }
Damien Miller34132e52000-01-14 15:45:46 +11001927 for (ai = aitop; ai; ai = ai->ai_next) {
1928 /* Create a socket. */
1929 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1930 if (sock < 0) {
1931 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10001932 continue;
1933 }
1934 /* Connect it to the display. */
1935 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1936 debug("connect %.100s port %d: %.100s", buf,
1937 6000 + display_number, strerror(errno));
1938 close(sock);
1939 continue;
1940 }
1941 /* Success */
1942 break;
Damien Miller34132e52000-01-14 15:45:46 +11001943 }
Damien Miller34132e52000-01-14 15:45:46 +11001944 freeaddrinfo(aitop);
1945 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10001946 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11001947 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10001948 return -1;
Damien Miller95def091999-11-25 00:26:21 +11001949 }
Damien Millerbd483e72000-04-30 10:00:53 +10001950 return sock;
1951}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001952
Damien Millerbd483e72000-04-30 10:00:53 +10001953/*
1954 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1955 * the remote channel number. We should do whatever we want, and respond
1956 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1957 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958
Damien Millerbd483e72000-04-30 10:00:53 +10001959void
1960x11_input_open(int type, int plen)
1961{
1962 int remote_channel, sock = 0, newch;
1963 char *remote_host;
1964 unsigned int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11001965
Damien Millerbd483e72000-04-30 10:00:53 +10001966 /* Get remote channel number. */
1967 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11001968
Damien Millerbd483e72000-04-30 10:00:53 +10001969 /* Get remote originator name. */
1970 if (have_hostname_in_open) {
1971 remote_host = packet_get_string(&remote_len);
1972 remote_len += 4;
1973 } else {
1974 remote_host = xstrdup("unknown (remote did not supply name)");
1975 remote_len = 0;
1976 }
1977
1978 debug("Received X11 open request.");
1979 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
1980
1981 /* Obtain a connection to the real X display. */
1982 sock = x11_connect_display();
1983 if (sock == -1) {
1984 /* Send refusal to the remote host. */
1985 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1986 packet_put_int(remote_channel);
1987 packet_send();
1988 } else {
1989 /* Allocate a channel for this connection. */
1990 newch = channel_allocate(
1991 (x11_saved_proto == NULL) ?
1992 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
1993 sock, remote_host);
1994 channels[newch].remote_id = remote_channel;
1995
1996 /* Send a confirmation to the remote host. */
1997 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1998 packet_put_int(remote_channel);
1999 packet_put_int(newch);
2000 packet_send();
2001 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002002}
2003
Damien Miller5428f641999-11-25 11:54:57 +11002004/*
2005 * Requests forwarding of X11 connections, generates fake authentication
2006 * data, and enables authentication spoofing.
2007 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002008
Damien Miller4af51302000-04-16 11:18:38 +10002009void
Damien Millerbd483e72000-04-30 10:00:53 +10002010x11_request_forwarding_with_spoofing(int client_session_id,
2011 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012{
Damien Miller95def091999-11-25 00:26:21 +11002013 unsigned int data_len = (unsigned int) strlen(data) / 2;
2014 unsigned int i, value;
2015 char *new_data;
2016 int screen_number;
2017 const char *cp;
2018 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002019
Damien Miller95def091999-11-25 00:26:21 +11002020 cp = getenv("DISPLAY");
2021 if (cp)
2022 cp = strchr(cp, ':');
2023 if (cp)
2024 cp = strchr(cp, '.');
2025 if (cp)
2026 screen_number = atoi(cp + 1);
2027 else
2028 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002029
Damien Miller95def091999-11-25 00:26:21 +11002030 /* Save protocol name. */
2031 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002032
Damien Miller5428f641999-11-25 11:54:57 +11002033 /*
2034 * Extract real authentication data and generate fake data of the
2035 * same length.
2036 */
Damien Miller95def091999-11-25 00:26:21 +11002037 x11_saved_data = xmalloc(data_len);
2038 x11_fake_data = xmalloc(data_len);
2039 for (i = 0; i < data_len; i++) {
2040 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2041 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2042 if (i % 4 == 0)
2043 rand = arc4random();
2044 x11_saved_data[i] = value;
2045 x11_fake_data[i] = rand & 0xff;
2046 rand >>= 8;
2047 }
2048 x11_saved_data_len = data_len;
2049 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002050
Damien Miller95def091999-11-25 00:26:21 +11002051 /* Convert the fake data into hex. */
2052 new_data = xmalloc(2 * data_len + 1);
2053 for (i = 0; i < data_len; i++)
2054 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002055
Damien Miller95def091999-11-25 00:26:21 +11002056 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002057 if (compat20) {
2058 channel_request_start(client_session_id, "x11-req", 0);
2059 packet_put_char(0); /* XXX bool single connection */
2060 } else {
2061 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2062 }
2063 packet_put_cstring(proto);
2064 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002065 packet_put_int(screen_number);
2066 packet_send();
2067 packet_write_wait();
2068 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002069}
2070
2071/* Sends a message to the server to request authentication fd forwarding. */
2072
Damien Miller4af51302000-04-16 11:18:38 +10002073void
Damien Miller95def091999-11-25 00:26:21 +11002074auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002075{
Damien Miller95def091999-11-25 00:26:21 +11002076 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2077 packet_send();
2078 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002079}
2080
Damien Miller5428f641999-11-25 11:54:57 +11002081/*
2082 * Returns the name of the forwarded authentication socket. Returns NULL if
2083 * there is no forwarded authentication socket. The returned value points to
2084 * a static buffer.
2085 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002086
Damien Miller95def091999-11-25 00:26:21 +11002087char *
2088auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002089{
Damien Miller95def091999-11-25 00:26:21 +11002090 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091}
2092
2093/* removes the agent forwarding socket */
2094
Damien Miller4af51302000-04-16 11:18:38 +10002095void
Damien Miller95def091999-11-25 00:26:21 +11002096cleanup_socket(void)
2097{
2098 remove(channel_forwarded_auth_socket_name);
2099 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100}
2101
Damien Miller5428f641999-11-25 11:54:57 +11002102/*
2103 * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
2104 * This starts forwarding authentication requests.
2105 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002106
Damien Miller4af51302000-04-16 11:18:38 +10002107void
Damien Miller95def091999-11-25 00:26:21 +11002108auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002109{
Damien Miller95def091999-11-25 00:26:21 +11002110 int sock, newch;
2111 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002112
Damien Miller95def091999-11-25 00:26:21 +11002113 if (auth_get_socket_name() != NULL)
2114 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002115
Damien Miller95def091999-11-25 00:26:21 +11002116 /* Temporarily drop privileged uid for mkdir/bind. */
2117 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002118
Damien Miller95def091999-11-25 00:26:21 +11002119 /* Allocate a buffer for the socket name, and format the name. */
2120 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2121 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2122 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002123
Damien Miller95def091999-11-25 00:26:21 +11002124 /* Create private directory for socket */
2125 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
2126 packet_disconnect("mkdtemp: %.100s", strerror(errno));
2127 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2128 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002129
Damien Miller95def091999-11-25 00:26:21 +11002130 if (atexit(cleanup_socket) < 0) {
2131 int saved = errno;
2132 cleanup_socket();
2133 packet_disconnect("socket: %.100s", strerror(saved));
2134 }
2135 /* Create the socket. */
2136 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2137 if (sock < 0)
2138 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139
Damien Miller95def091999-11-25 00:26:21 +11002140 /* Bind it to the name. */
2141 memset(&sunaddr, 0, sizeof(sunaddr));
2142 sunaddr.sun_family = AF_UNIX;
2143 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2144 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002145
Damien Miller95def091999-11-25 00:26:21 +11002146 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2147 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002148
Damien Miller95def091999-11-25 00:26:21 +11002149 /* Restore the privileged uid. */
2150 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002151
Damien Miller95def091999-11-25 00:26:21 +11002152 /* Start listening on the socket. */
2153 if (listen(sock, 5) < 0)
2154 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155
Damien Miller95def091999-11-25 00:26:21 +11002156 /* Allocate a channel for the authentication agent socket. */
2157 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
2158 xstrdup("auth socket"));
Damien Miller037a0dc1999-12-07 15:38:31 +11002159 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2160 sizeof(channels[newch].path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161}
2162
2163/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2164
Damien Miller4af51302000-04-16 11:18:38 +10002165void
Damien Millerb38eff82000-04-01 11:09:21 +10002166auth_input_open_request(int type, int plen)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002167{
Damien Miller95def091999-11-25 00:26:21 +11002168 int remch, sock, newch;
2169 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002170
Damien Millerb38eff82000-04-01 11:09:21 +10002171 packet_integrity_check(plen, 4, type);
2172
Damien Miller95def091999-11-25 00:26:21 +11002173 /* Read the remote channel number from the message. */
2174 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002175
Damien Miller5428f641999-11-25 11:54:57 +11002176 /*
2177 * Get a connection to the local authentication agent (this may again
2178 * get forwarded).
2179 */
Damien Miller95def091999-11-25 00:26:21 +11002180 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002181
Damien Miller5428f641999-11-25 11:54:57 +11002182 /*
2183 * If we could not connect the agent, send an error message back to
2184 * the server. This should never happen unless the agent dies,
2185 * because authentication forwarding is only enabled if we have an
2186 * agent.
2187 */
Damien Miller95def091999-11-25 00:26:21 +11002188 if (sock < 0) {
2189 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2190 packet_put_int(remch);
2191 packet_send();
2192 return;
2193 }
2194 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002195
Damien Miller5428f641999-11-25 11:54:57 +11002196 /*
2197 * Dummy host name. This will be freed when the channel is freed; it
2198 * will still be valid in the packet_put_string below since the
2199 * channel cannot yet be freed at that point.
2200 */
Damien Miller95def091999-11-25 00:26:21 +11002201 dummyname = xstrdup("authentication agent connection");
2202
2203 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2204 channels[newch].remote_id = remch;
2205
2206 /* Send a confirmation to the remote host. */
2207 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2208 packet_put_int(remch);
2209 packet_put_int(newch);
2210 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002211}
Damien Miller33b13562000-04-04 14:38:59 +10002212
2213void
Damien Millerbd483e72000-04-30 10:00:53 +10002214channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002215{
2216 Channel *c = channel_lookup(id);
2217 if (c == NULL) {
2218 log("channel_open: %d: bad id", id);
2219 return;
2220 }
Damien Millerbd483e72000-04-30 10:00:53 +10002221 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002222 packet_start(SSH2_MSG_CHANNEL_OPEN);
2223 packet_put_cstring(c->ctype);
2224 packet_put_int(c->self);
2225 packet_put_int(c->local_window);
2226 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002227}
2228void
2229channel_open(int id)
2230{
2231 /* XXX REMOVE ME */
2232 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002233 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002234}
2235void
2236channel_request(int id, char *service, int wantconfirm)
2237{
2238 channel_request_start(id, service, wantconfirm);
2239 packet_send();
2240 debug("channel request %d: %s", id, service) ;
2241}
2242void
2243channel_request_start(int id, char *service, int wantconfirm)
2244{
2245 Channel *c = channel_lookup(id);
2246 if (c == NULL) {
2247 log("channel_request: %d: bad id", id);
2248 return;
2249 }
2250 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2251 packet_put_int(c->remote_id);
2252 packet_put_cstring(service);
2253 packet_put_char(wantconfirm);
2254}
2255void
2256channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2257{
2258 Channel *c = channel_lookup(id);
2259 if (c == NULL) {
2260 log("channel_register_callback: %d: bad id", id);
2261 return;
2262 }
2263 c->cb_event = mtype;
2264 c->cb_fn = fn;
2265 c->cb_arg = arg;
2266}
2267void
2268channel_register_cleanup(int id, channel_callback_fn *fn)
2269{
2270 Channel *c = channel_lookup(id);
2271 if (c == NULL) {
2272 log("channel_register_cleanup: %d: bad id", id);
2273 return;
2274 }
2275 c->dettach_user = fn;
2276}
2277void
2278channel_cancel_cleanup(int id)
2279{
2280 Channel *c = channel_lookup(id);
2281 if (c == NULL) {
2282 log("channel_cancel_cleanup: %d: bad id", id);
2283 return;
2284 }
2285 c->dettach_user = NULL;
2286}
2287
2288void
2289channel_set_fds(int id, int rfd, int wfd, int efd, int extusage)
2290{
2291 Channel *c = channel_lookup(id);
2292 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2293 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller6f83b8e2000-05-02 09:23:45 +10002294
2295 channel_register_fds(c, rfd, wfd, efd, extusage);
Damien Miller33b13562000-04-04 14:38:59 +10002296 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002297 /* XXX window size? */
2298 c->local_window = c->local_window_max = c->local_maxpacket/2;
2299 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2300 packet_put_int(c->remote_id);
2301 packet_put_int(c->local_window);
2302 packet_send();
2303}