]> andersk Git - openssh.git/blame - channels.c
- (djm) Fix rsh path in RPMs. Report from Jason L Tibbitts III
[openssh.git] / channels.c
CommitLineData
8efc0c15 1/*
6ae2364d 2 *
5260325f 3 * channels.c
6ae2364d 4 *
5260325f 5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6ae2364d 6 *
5260325f 7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
6ae2364d 9 *
5260325f 10 * Created: Fri Mar 24 16:35:24 1995 ylo
6ae2364d 11 *
5260325f 12 * 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.
6ae2364d 15 *
7e7327a1 16 * SSH2 support added by Markus Friedl.
5260325f 17 */
8efc0c15 18
19#include "includes.h"
20RCSID("$Id$");
21
22#include "ssh.h"
23#include "packet.h"
24#include "xmalloc.h"
25#include "buffer.h"
26#include "authfd.h"
27#include "uidswap.h"
6fa724bc 28#include "readconf.h"
8efc0c15 29#include "servconf.h"
30
31#include "channels.h"
32#include "nchan.h"
33#include "compat.h"
34
7e7327a1 35#include "ssh2.h"
36
8efc0c15 37/* 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
0b242b12 43/* 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)
7368a6c8 48
aa3378df 49/*
50 * Pointer to an array containing all allocated channels. The array is
51 * dynamically extended as needed.
52 */
8efc0c15 53static Channel *channels = NULL;
54
aa3378df 55/*
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 */
8efc0c15 60static int channels_alloc = 0;
61
aa3378df 62/*
63 * Maximum file descriptor value used in any of the channels. This is
64 * updated in channel_allocate.
65 */
8efc0c15 66static 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;
5260325f 70static char *channel_forwarded_auth_socket_dir = NULL;
8efc0c15 71
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
aa3378df 79/*
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 */
8efc0c15 83char *x11_fake_data = NULL;
84unsigned int x11_fake_data_len;
85
aa3378df 86/*
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 */
5260325f 92typedef struct {
7368a6c8 93 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. */
8efc0c15 96} 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;
aa3378df 102/*
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 */
8efc0c15 107static 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
6ae2364d 114void
5260325f 115channel_set_options(int hostname_in_open)
8efc0c15 116{
5260325f 117 have_hostname_in_open = hostname_in_open;
8efc0c15 118}
119
aa3378df 120/*
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 */
8efc0c15 125
6ae2364d 126void
5260325f 127channel_permit_all_opens()
8efc0c15 128{
5260325f 129 all_opens_permitted = 1;
8efc0c15 130}
131
7368a6c8 132/* 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
aa3378df 150/*
1d1ffb87 151 * Register filedescriptors for a channel, used when allocating a channel or
0fbe8c74 152 * when the channel consumer/producer is ready, e.g. shell exec'd
aa3378df 153 */
8efc0c15 154
0fbe8c74 155void
156channel_register_fds(Channel *c, int rfd, int wfd, int efd, int extusage)
8efc0c15 157{
5260325f 158 /* Update the maximum file descriptor value. */
7368a6c8 159 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;
aa3378df 165 /* XXX set close-on-exec -markus */
1d1ffb87 166
0fbe8c74 167 c->rfd = rfd;
168 c->wfd = wfd;
169 c->sock = (rfd == wfd) ? rfd : -1;
170 c->efd = efd;
171 c->extended_usage = extusage;
1d1ffb87 172 if (rfd != -1)
173 set_nonblock(rfd);
174 if (wfd != -1)
175 set_nonblock(wfd);
176 if (efd != -1)
177 set_nonblock(efd);
0fbe8c74 178}
179
180/*
181 * Allocate a new channel object and set its type and socket. This will cause
182 * remote_name to be freed.
183 */
184
185int
186channel_new(char *ctype, int type, int rfd, int wfd, int efd,
187 int window, int maxpack, int extusage, char *remote_name)
188{
189 int i, found;
190 Channel *c;
5260325f 191
192 /* Do initial allocation if this is the first call. */
193 if (channels_alloc == 0) {
7e7327a1 194 chan_init();
5260325f 195 channels_alloc = 10;
196 channels = xmalloc(channels_alloc * sizeof(Channel));
197 for (i = 0; i < channels_alloc; i++)
198 channels[i].type = SSH_CHANNEL_FREE;
aa3378df 199 /*
200 * Kludge: arrange a call to channel_stop_listening if we
201 * terminate with fatal().
202 */
5260325f 203 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
204 }
205 /* Try to find a free slot where to put the new channel. */
206 for (found = -1, i = 0; i < channels_alloc; i++)
207 if (channels[i].type == SSH_CHANNEL_FREE) {
208 /* Found a free slot. */
209 found = i;
210 break;
211 }
212 if (found == -1) {
aa3378df 213 /* There are no free slots. Take last+1 slot and expand the array. */
5260325f 214 found = channels_alloc;
215 channels_alloc += 10;
216 debug("channel: expanding %d", channels_alloc);
217 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
218 for (i = found; i < channels_alloc; i++)
219 channels[i].type = SSH_CHANNEL_FREE;
220 }
221 /* Initialize and return new channel number. */
222 c = &channels[found];
223 buffer_init(&c->input);
224 buffer_init(&c->output);
7368a6c8 225 buffer_init(&c->extended);
5260325f 226 chan_init_iostates(c);
0fbe8c74 227 channel_register_fds(c, rfd, wfd, efd, extusage);
5260325f 228 c->self = found;
229 c->type = type;
7368a6c8 230 c->ctype = ctype;
0b242b12 231 c->local_window = window;
232 c->local_window_max = window;
233 c->local_consumed = 0;
234 c->local_maxpacket = maxpack;
5260325f 235 c->remote_id = -1;
236 c->remote_name = remote_name;
7368a6c8 237 c->remote_window = 0;
238 c->remote_maxpacket = 0;
239 c->cb_fn = NULL;
240 c->cb_arg = NULL;
241 c->cb_event = 0;
242 c->dettach_user = NULL;
5260325f 243 debug("channel %d: new [%s]", found, remote_name);
244 return found;
8efc0c15 245}
0fbe8c74 246/* old interface XXX */
6ae2364d 247int
7368a6c8 248channel_allocate(int type, int sock, char *remote_name)
249{
250 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
251}
8efc0c15 252
0fbe8c74 253
254/* Close all channel fd/socket. */
255
256void
257channel_close_fds(Channel *c)
258{
259 if (c->sock != -1) {
260 close(c->sock);
261 c->sock = -1;
262 }
263 if (c->rfd != -1) {
264 close(c->rfd);
265 c->rfd = -1;
266 }
267 if (c->wfd != -1) {
268 close(c->wfd);
269 c->wfd = -1;
270 }
271 if (c->efd != -1) {
272 close(c->efd);
273 c->efd = -1;
274 }
275}
276
277/* Free the channel and close its fd/socket. */
8efc0c15 278
6ae2364d 279void
7368a6c8 280channel_free(int id)
8efc0c15 281{
7368a6c8 282 Channel *c = channel_lookup(id);
283 if (c == NULL)
284 packet_disconnect("channel free: bad local channel %d", id);
285 debug("channel_free: channel %d: status: %s", id, channel_open_message());
7e7327a1 286 if (c->dettach_user != NULL) {
287 debug("channel_free: channel %d: dettaching channel user", id);
288 c->dettach_user(c->self, NULL);
289 }
0fbe8c74 290 if (c->sock != -1)
7368a6c8 291 shutdown(c->sock, SHUT_RDWR);
0fbe8c74 292 channel_close_fds(c);
7368a6c8 293 buffer_free(&c->input);
294 buffer_free(&c->output);
295 buffer_free(&c->extended);
296 c->type = SSH_CHANNEL_FREE;
297 if (c->remote_name) {
298 xfree(c->remote_name);
299 c->remote_name = NULL;
5260325f 300 }
8efc0c15 301}
302
aa3378df 303/*
7368a6c8 304 * 'channel_pre*' are called just before select() to add any bits relevant to
305 * channels in the select bitmasks.
aa3378df 306 */
7368a6c8 307/*
308 * 'channel_post*': perform any appropriate operations for channels which
309 * have events pending.
310 */
311typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
312chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
313chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
8efc0c15 314
7368a6c8 315void
316channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
8efc0c15 317{
7368a6c8 318 FD_SET(c->sock, readset);
319}
5260325f 320
7368a6c8 321void
322channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
323{
324 if (buffer_len(&c->input) < packet_get_maxsize())
325 FD_SET(c->sock, readset);
326 if (buffer_len(&c->output) > 0)
327 FD_SET(c->sock, writeset);
328}
5260325f 329
7368a6c8 330void
331channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
332{
333 /* test whether sockets are 'alive' for read/write */
334 if (c->istate == CHAN_INPUT_OPEN)
335 if (buffer_len(&c->input) < packet_get_maxsize())
336 FD_SET(c->sock, readset);
337 if (c->ostate == CHAN_OUTPUT_OPEN ||
338 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
339 if (buffer_len(&c->output) > 0) {
340 FD_SET(c->sock, writeset);
341 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
342 chan_obuf_empty(c);
343 }
344 }
345}
5260325f 346
7e7327a1 347void
348channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
349{
350 if (c->istate == CHAN_INPUT_OPEN &&
351 c->remote_window > 0 &&
352 buffer_len(&c->input) < c->remote_window)
353 FD_SET(c->rfd, readset);
354 if (c->ostate == CHAN_OUTPUT_OPEN ||
355 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
356 if (buffer_len(&c->output) > 0) {
357 FD_SET(c->wfd, writeset);
358 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
359 chan_obuf_empty(c);
360 }
361 }
362 /** XXX check close conditions, too */
363 if (c->efd != -1) {
364 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
365 buffer_len(&c->extended) > 0)
366 FD_SET(c->efd, writeset);
367 else if (c->extended_usage == CHAN_EXTENDED_READ &&
368 buffer_len(&c->extended) < c->remote_window)
369 FD_SET(c->efd, readset);
370 }
371}
372
7368a6c8 373void
374channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
375{
376 if (buffer_len(&c->input) == 0) {
377 packet_start(SSH_MSG_CHANNEL_CLOSE);
378 packet_put_int(c->remote_id);
379 packet_send();
380 c->type = SSH_CHANNEL_CLOSED;
381 debug("Closing channel %d after input drain.", c->self);
382 }
383}
5260325f 384
7368a6c8 385void
386channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
387{
388 if (buffer_len(&c->output) == 0)
389 channel_free(c->self);
6ae2364d 390 else
7368a6c8 391 FD_SET(c->sock, writeset);
392}
5260325f 393
7368a6c8 394/*
395 * This is a special state for X11 authentication spoofing. An opened X11
396 * connection (when authentication spoofing is being done) remains in this
397 * state until the first packet has been completely read. The authentication
398 * data in that packet is then substituted by the real data if it matches the
399 * fake data, and the channel is put into normal mode.
0b242b12 400 * XXX All this happens at the client side.
7368a6c8 401 */
402int
403x11_open_helper(Channel *c)
404{
405 unsigned char *ucp;
406 unsigned int proto_len, data_len;
5260325f 407
7368a6c8 408 /* Check if the fixed size part of the packet is in buffer. */
409 if (buffer_len(&c->output) < 12)
410 return 0;
411
412 /* Parse the lengths of variable-length fields. */
413 ucp = (unsigned char *) buffer_ptr(&c->output);
414 if (ucp[0] == 0x42) { /* Byte order MSB first. */
415 proto_len = 256 * ucp[6] + ucp[7];
416 data_len = 256 * ucp[8] + ucp[9];
417 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
418 proto_len = ucp[6] + 256 * ucp[7];
419 data_len = ucp[8] + 256 * ucp[9];
420 } else {
421 debug("Initial X11 packet contains bad byte order byte: 0x%x",
422 ucp[0]);
423 return -1;
424 }
5260325f 425
7368a6c8 426 /* Check if the whole packet is in buffer. */
427 if (buffer_len(&c->output) <
428 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
429 return 0;
5260325f 430
7368a6c8 431 /* Check if authentication protocol matches. */
432 if (proto_len != strlen(x11_saved_proto) ||
433 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
434 debug("X11 connection uses different authentication protocol.");
435 return -1;
436 }
437 /* Check if authentication data matches our fake data. */
438 if (data_len != x11_fake_data_len ||
439 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
440 x11_fake_data, x11_fake_data_len) != 0) {
441 debug("X11 auth data does not match fake data.");
442 return -1;
443 }
444 /* Check fake data length */
445 if (x11_fake_data_len != x11_saved_data_len) {
446 error("X11 fake_data_len %d != saved_data_len %d",
447 x11_fake_data_len, x11_saved_data_len);
448 return -1;
449 }
450 /*
451 * Received authentication protocol and data match
452 * our fake data. Substitute the fake data with real
453 * data.
454 */
455 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
456 x11_saved_data, x11_saved_data_len);
457 return 1;
458}
5260325f 459
7368a6c8 460void
461channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
462{
463 int ret = x11_open_helper(c);
464 if (ret == 1) {
465 /* Start normal processing for the channel. */
466 c->type = SSH_CHANNEL_OPEN;
a8be9f80 467 channel_pre_open_13(c, readset, writeset);
7368a6c8 468 } else if (ret == -1) {
469 /*
470 * We have received an X11 connection that has bad
471 * authentication information.
472 */
473 log("X11 connection rejected because of wrong authentication.\r\n");
474 buffer_clear(&c->input);
475 buffer_clear(&c->output);
476 close(c->sock);
477 c->sock = -1;
478 c->type = SSH_CHANNEL_CLOSED;
479 packet_start(SSH_MSG_CHANNEL_CLOSE);
480 packet_put_int(c->remote_id);
481 packet_send();
482 }
483}
5260325f 484
7368a6c8 485void
0b242b12 486channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
7368a6c8 487{
488 int ret = x11_open_helper(c);
489 if (ret == 1) {
490 c->type = SSH_CHANNEL_OPEN;
d0c832f3 491 if (compat20)
492 channel_pre_open_20(c, readset, writeset);
493 else
494 channel_pre_open_15(c, readset, writeset);
7368a6c8 495 } else if (ret == -1) {
496 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
0b242b12 497 chan_read_failed(c); /** force close? */
7368a6c8 498 chan_write_failed(c);
499 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
500 }
501}
5260325f 502
7368a6c8 503/* This is our fake X11 server socket. */
504void
505channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
506{
507 struct sockaddr addr;
508 int newsock, newch;
509 socklen_t addrlen;
510 char buf[16384], *remote_hostname;
0b242b12 511 int remote_port;
7368a6c8 512
513 if (FD_ISSET(c->sock, readset)) {
514 debug("X11 connection requested.");
515 addrlen = sizeof(addr);
516 newsock = accept(c->sock, &addr, &addrlen);
517 if (newsock < 0) {
518 error("accept: %.100s", strerror(errno));
519 return;
5260325f 520 }
7368a6c8 521 remote_hostname = get_remote_hostname(newsock);
0b242b12 522 remote_port = get_peer_port(newsock);
7368a6c8 523 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
0b242b12 524 remote_hostname, remote_port);
525
526 newch = channel_new("x11",
527 SSH_CHANNEL_OPENING, newsock, newsock, -1,
528 c->local_window_max, c->local_maxpacket,
529 0, xstrdup(buf));
530 if (compat20) {
531 packet_start(SSH2_MSG_CHANNEL_OPEN);
532 packet_put_cstring("x11");
533 packet_put_int(newch);
534 packet_put_int(c->local_window_max);
535 packet_put_int(c->local_maxpacket);
536 /* originator host and port */
537 packet_put_cstring(remote_hostname);
d0c832f3 538 if (datafellows & SSH_BUG_X11FWD) {
539 debug("ssh2 x11 bug compat mode");
540 } else {
541 packet_put_int(remote_port);
542 }
0b242b12 543 packet_send();
544 } else {
545 packet_start(SSH_SMSG_X11_OPEN);
546 packet_put_int(newch);
547 if (have_hostname_in_open)
548 packet_put_string(buf, strlen(buf));
549 packet_send();
550 }
7368a6c8 551 xfree(remote_hostname);
8efc0c15 552 }
8efc0c15 553}
554
aa3378df 555/*
7368a6c8 556 * This socket is listening for connections to a forwarded TCP/IP port.
aa3378df 557 */
7368a6c8 558void
559channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
560{
561 struct sockaddr addr;
562 int newsock, newch;
563 socklen_t addrlen;
564 char buf[1024], *remote_hostname;
565 int remote_port;
566
567 if (FD_ISSET(c->sock, readset)) {
568 debug("Connection to port %d forwarding "
569 "to %.100s port %d requested.",
570 c->listening_port, c->path, c->host_port);
571 addrlen = sizeof(addr);
572 newsock = accept(c->sock, &addr, &addrlen);
573 if (newsock < 0) {
574 error("accept: %.100s", strerror(errno));
575 return;
576 }
577 remote_hostname = get_remote_hostname(newsock);
578 remote_port = get_peer_port(newsock);
579 snprintf(buf, sizeof buf,
580 "listen port %d for %.100s port %d, "
581 "connect from %.200s port %d",
582 c->listening_port, c->path, c->host_port,
583 remote_hostname, remote_port);
584 newch = channel_new("direct-tcpip",
585 SSH_CHANNEL_OPENING, newsock, newsock, -1,
586 c->local_window_max, c->local_maxpacket,
587 0, xstrdup(buf));
7e7327a1 588 if (compat20) {
589 packet_start(SSH2_MSG_CHANNEL_OPEN);
590 packet_put_cstring("direct-tcpip");
591 packet_put_int(newch);
592 packet_put_int(c->local_window_max);
593 packet_put_int(c->local_maxpacket);
6ae2364d 594 /* target host and port */
7e7327a1 595 packet_put_string(c->path, strlen(c->path));
596 packet_put_int(c->host_port);
6ae2364d 597 /* originator host and port */
7e7327a1 598 packet_put_cstring(remote_hostname);
599 packet_put_int(remote_port);
600 packet_send();
601 } else {
602 packet_start(SSH_MSG_PORT_OPEN);
603 packet_put_int(newch);
604 packet_put_string(c->path, strlen(c->path));
605 packet_put_int(c->host_port);
606 if (have_hostname_in_open) {
607 packet_put_string(buf, strlen(buf));
608 }
609 packet_send();
7368a6c8 610 }
7368a6c8 611 xfree(remote_hostname);
612 }
613}
8efc0c15 614
7368a6c8 615/*
616 * This is the authentication agent socket listening for connections from
617 * clients.
618 */
619void
620channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
8efc0c15 621{
5260325f 622 struct sockaddr addr;
7368a6c8 623 int newsock, newch;
48e671d5 624 socklen_t addrlen;
5260325f 625
7368a6c8 626 if (FD_ISSET(c->sock, readset)) {
627 addrlen = sizeof(addr);
628 newsock = accept(c->sock, &addr, &addrlen);
629 if (newsock < 0) {
630 error("accept from auth socket: %.100s", strerror(errno));
631 return;
632 }
633 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
634 xstrdup("accepted auth socket"));
635 packet_start(SSH_SMSG_AGENT_OPEN);
636 packet_put_int(newch);
637 packet_send();
638 }
639}
5260325f 640
7368a6c8 641int
642channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
643{
644 char buf[16*1024];
645 int len;
646
647 if (c->rfd != -1 &&
648 FD_ISSET(c->rfd, readset)) {
649 len = read(c->rfd, buf, sizeof(buf));
0fbe8c74 650 if (len < 0 && (errno == EINTR || errno == EAGAIN))
651 return 1;
7368a6c8 652 if (len <= 0) {
0b242b12 653 debug("channel %d: read<=0 rfd %d len %d",
7368a6c8 654 c->self, c->rfd, len);
655 if (compat13) {
656 buffer_consume(&c->output, buffer_len(&c->output));
657 c->type = SSH_CHANNEL_INPUT_DRAINING;
658 debug("Channel %d status set to input draining.", c->self);
659 } else {
660 chan_read_failed(c);
5260325f 661 }
7368a6c8 662 return -1;
663 }
664 buffer_append(&c->input, buf, len);
665 }
666 return 1;
667}
668int
669channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
670{
671 int len;
672
673 /* Send buffered output data to the socket. */
674 if (c->wfd != -1 &&
675 FD_ISSET(c->wfd, writeset) &&
676 buffer_len(&c->output) > 0) {
677 len = write(c->wfd, buffer_ptr(&c->output),
0fbe8c74 678 buffer_len(&c->output));
679 if (len < 0 && (errno == EINTR || errno == EAGAIN))
680 return 1;
7368a6c8 681 if (len <= 0) {
682 if (compat13) {
683 buffer_consume(&c->output, buffer_len(&c->output));
684 debug("Channel %d status set to input draining.", c->self);
685 c->type = SSH_CHANNEL_INPUT_DRAINING;
686 } else {
687 chan_write_failed(c);
5260325f 688 }
7368a6c8 689 return -1;
690 }
691 buffer_consume(&c->output, len);
7e7327a1 692 if (compat20 && len > 0) {
693 c->local_consumed += len;
694 }
695 }
696 return 1;
697}
698int
699channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
700{
701 char buf[16*1024];
702 int len;
703
8ce64345 704/** XXX handle drain efd, too */
7e7327a1 705 if (c->efd != -1) {
706 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
707 FD_ISSET(c->efd, writeset) &&
708 buffer_len(&c->extended) > 0) {
709 len = write(c->efd, buffer_ptr(&c->extended),
710 buffer_len(&c->extended));
711 debug("channel %d: written %d to efd %d",
712 c->self, len, c->efd);
713 if (len > 0) {
714 buffer_consume(&c->extended, len);
715 c->local_consumed += len;
716 }
717 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
718 FD_ISSET(c->efd, readset)) {
719 len = read(c->efd, buf, sizeof(buf));
720 debug("channel %d: read %d from efd %d",
721 c->self, len, c->efd);
8ce64345 722 if (len == 0) {
723 debug("channel %d: closing efd %d",
724 c->self, c->efd);
725 close(c->efd);
726 c->efd = -1;
727 } else if (len > 0)
7e7327a1 728 buffer_append(&c->extended, buf, len);
729 }
730 }
731 return 1;
732}
733int
734channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
735{
e78a59f5 736 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
7e7327a1 737 c->local_window < c->local_window_max/2 &&
738 c->local_consumed > 0) {
739 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
740 packet_put_int(c->remote_id);
741 packet_put_int(c->local_consumed);
742 packet_send();
743 debug("channel %d: window %d sent adjust %d",
744 c->self, c->local_window,
745 c->local_consumed);
746 c->local_window += c->local_consumed;
747 c->local_consumed = 0;
7368a6c8 748 }
749 return 1;
750}
5260325f 751
7368a6c8 752void
753channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
754{
755 channel_handle_rfd(c, readset, writeset);
756 channel_handle_wfd(c, readset, writeset);
757}
aa3378df 758
7e7327a1 759void
760channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
761{
762 channel_handle_rfd(c, readset, writeset);
763 channel_handle_wfd(c, readset, writeset);
764 channel_handle_efd(c, readset, writeset);
765 channel_check_window(c, readset, writeset);
766}
767
7368a6c8 768void
769channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
770{
771 int len;
772 /* Send buffered output data to the socket. */
773 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
774 len = write(c->sock, buffer_ptr(&c->output),
775 buffer_len(&c->output));
776 if (len <= 0)
777 buffer_consume(&c->output, buffer_len(&c->output));
778 else
779 buffer_consume(&c->output, len);
780 }
781}
5260325f 782
7e7327a1 783void
784channel_handler_init_20(void)
785{
786 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
0b242b12 787 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
7e7327a1 788 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
0b242b12 789 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
7e7327a1 790
791 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
792 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
0b242b12 793 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
7e7327a1 794}
795
7368a6c8 796void
797channel_handler_init_13(void)
798{
799 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
800 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
801 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
802 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
803 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
804 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
805 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
806
807 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
808 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
809 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
810 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
811 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
812}
5260325f 813
7368a6c8 814void
815channel_handler_init_15(void)
816{
817 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
0b242b12 818 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
7368a6c8 819 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
820 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
821 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
822
823 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
824 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
825 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
826 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
827}
828
829void
830channel_handler_init(void)
831{
832 int i;
833 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
834 channel_pre[i] = NULL;
835 channel_post[i] = NULL;
836 }
7e7327a1 837 if (compat20)
838 channel_handler_init_20();
839 else if (compat13)
7368a6c8 840 channel_handler_init_13();
841 else
842 channel_handler_init_15();
843}
844
6ae2364d 845void
7368a6c8 846channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
847{
848 static int did_init = 0;
849 int i;
850 Channel *c;
851
852 if (!did_init) {
853 channel_handler_init();
854 did_init = 1;
855 }
856 for (i = 0; i < channels_alloc; i++) {
857 c = &channels[i];
858 if (c->type == SSH_CHANNEL_FREE)
5260325f 859 continue;
7368a6c8 860 if (ftab[c->type] == NULL)
861 continue;
862 (*ftab[c->type])(c, readset, writeset);
7e7327a1 863 chan_delete_if_full_closed(c);
8efc0c15 864 }
8efc0c15 865}
866
6ae2364d 867void
7368a6c8 868channel_prepare_select(fd_set * readset, fd_set * writeset)
869{
870 channel_handler(channel_pre, readset, writeset);
871}
872
6ae2364d 873void
7368a6c8 874channel_after_select(fd_set * readset, fd_set * writeset)
875{
876 channel_handler(channel_post, readset, writeset);
877}
878
8efc0c15 879/* If there is data to send to the connection, send some of it now. */
880
6ae2364d 881void
5260325f 882channel_output_poll()
8efc0c15 883{
5260325f 884 int len, i;
7368a6c8 885 Channel *c;
5260325f 886
887 for (i = 0; i < channels_alloc; i++) {
7368a6c8 888 c = &channels[i];
48e671d5 889
aa3378df 890 /* We are only interested in channels that can have buffered incoming data. */
48e671d5 891 if (compat13) {
7368a6c8 892 if (c->type != SSH_CHANNEL_OPEN &&
893 c->type != SSH_CHANNEL_INPUT_DRAINING)
48e671d5 894 continue;
895 } else {
7368a6c8 896 if (c->type != SSH_CHANNEL_OPEN)
48e671d5 897 continue;
7368a6c8 898 if (c->istate != CHAN_INPUT_OPEN &&
899 c->istate != CHAN_INPUT_WAIT_DRAIN)
48e671d5 900 continue;
901 }
e78a59f5 902 if (compat20 &&
903 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
7e7327a1 904 debug("channel: %d: no data after CLOSE", c->self);
905 continue;
906 }
5260325f 907
908 /* Get the amount of buffered data for this channel. */
7368a6c8 909 len = buffer_len(&c->input);
5260325f 910 if (len > 0) {
aa3378df 911 /* Send some data for the other side over the secure connection. */
7e7327a1 912 if (compat20) {
913 if (len > c->remote_window)
914 len = c->remote_window;
915 if (len > c->remote_maxpacket)
916 len = c->remote_maxpacket;
5260325f 917 } else {
7e7327a1 918 if (packet_is_interactive()) {
919 if (len > 1024)
920 len = 512;
921 } else {
922 /* Keep the packets at reasonable size. */
923 if (len > packet_get_maxsize()/2)
924 len = packet_get_maxsize()/2;
925 }
5260325f 926 }
7368a6c8 927 if (len > 0) {
7e7327a1 928 packet_start(compat20 ?
929 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
7368a6c8 930 packet_put_int(c->remote_id);
931 packet_put_string(buffer_ptr(&c->input), len);
932 packet_send();
933 buffer_consume(&c->input, len);
934 c->remote_window -= len;
7e7327a1 935 debug("channel %d: send data len %d", c->self, len);
7368a6c8 936 }
937 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
5260325f 938 if (compat13)
939 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
aa3378df 940 /*
941 * input-buffer is empty and read-socket shutdown:
942 * tell peer, that we will not send more data: send IEOF
943 */
7368a6c8 944 chan_ibuf_empty(c);
5260325f 945 }
7e7327a1 946 /* Send extended data, i.e. stderr */
947 if (compat20 &&
948 c->remote_window > 0 &&
949 (len = buffer_len(&c->extended)) > 0 &&
950 c->extended_usage == CHAN_EXTENDED_READ) {
951 if (len > c->remote_window)
952 len = c->remote_window;
953 if (len > c->remote_maxpacket)
954 len = c->remote_maxpacket;
955 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
956 packet_put_int(c->remote_id);
957 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
958 packet_put_string(buffer_ptr(&c->extended), len);
959 packet_send();
960 buffer_consume(&c->extended, len);
961 c->remote_window -= len;
962 }
8efc0c15 963 }
8efc0c15 964}
965
aa3378df 966/*
967 * This is called when a packet of type CHANNEL_DATA has just been received.
968 * The message type has already been consumed, but channel number and data is
969 * still there.
970 */
8efc0c15 971
6ae2364d 972void
7368a6c8 973channel_input_data(int type, int plen)
8efc0c15 974{
48e671d5 975 int id;
5260325f 976 char *data;
977 unsigned int data_len;
7368a6c8 978 Channel *c;
5260325f 979
980 /* Get the channel number and verify it. */
48e671d5 981 id = packet_get_int();
7368a6c8 982 c = channel_lookup(id);
983 if (c == NULL)
48e671d5 984 packet_disconnect("Received data for nonexistent channel %d.", id);
5260325f 985
986 /* Ignore any data for non-open channels (might happen on close) */
7368a6c8 987 if (c->type != SSH_CHANNEL_OPEN &&
988 c->type != SSH_CHANNEL_X11_OPEN)
48e671d5 989 return;
990
991 /* same for protocol 1.5 if output end is no longer open */
7368a6c8 992 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
5260325f 993 return;
994
995 /* Get the data. */
996 data = packet_get_string(&data_len);
6ae2364d 997 packet_done();
7368a6c8 998
7e7327a1 999 if (compat20){
1000 if (data_len > c->local_maxpacket) {
1001 log("channel %d: rcvd big packet %d, maxpack %d",
1002 c->self, data_len, c->local_maxpacket);
1003 }
1004 if (data_len > c->local_window) {
1005 log("channel %d: rcvd too much data %d, win %d",
1006 c->self, data_len, c->local_window);
1007 xfree(data);
1008 return;
1009 }
1010 c->local_window -= data_len;
1011 }else{
1012 packet_integrity_check(plen, 4 + 4 + data_len, type);
1013 }
7368a6c8 1014 buffer_append(&c->output, data, data_len);
5260325f 1015 xfree(data);
8efc0c15 1016}
6ae2364d 1017void
7e7327a1 1018channel_input_extended_data(int type, int plen)
1019{
1020 int id;
1021 int tcode;
1022 char *data;
1023 unsigned int data_len;
1024 Channel *c;
1025
1026 /* Get the channel number and verify it. */
1027 id = packet_get_int();
1028 c = channel_lookup(id);
1029
1030 if (c == NULL)
1031 packet_disconnect("Received extended_data for bad channel %d.", id);
1032 if (c->type != SSH_CHANNEL_OPEN) {
1033 log("channel %d: ext data for non open", id);
1034 return;
1035 }
1036 tcode = packet_get_int();
1037 if (c->efd == -1 ||
1038 c->extended_usage != CHAN_EXTENDED_WRITE ||
1039 tcode != SSH2_EXTENDED_DATA_STDERR) {
1040 log("channel %d: bad ext data", c->self);
1041 return;
1042 }
1043 data = packet_get_string(&data_len);
6ae2364d 1044 packet_done();
7e7327a1 1045 if (data_len > c->local_window) {
1046 log("channel %d: rcvd too much extended_data %d, win %d",
1047 c->self, data_len, c->local_window);
1048 xfree(data);
1049 return;
1050 }
1051 debug("channel %d: rcvd ext data %d", c->self, data_len);
1052 c->local_window -= data_len;
1053 buffer_append(&c->extended, data, data_len);
1054 xfree(data);
1055}
1056
8efc0c15 1057
aa3378df 1058/*
1059 * Returns true if no channel has too much buffered data, and false if one or
1060 * more channel is overfull.
1061 */
8efc0c15 1062
6ae2364d 1063int
5260325f 1064channel_not_very_much_buffered_data()
8efc0c15 1065{
5260325f 1066 unsigned int i;
7368a6c8 1067 Channel *c;
5260325f 1068
1069 for (i = 0; i < channels_alloc; i++) {
7368a6c8 1070 c = &channels[i];
1071 if (c->type == SSH_CHANNEL_OPEN) {
7e7327a1 1072 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
7368a6c8 1073 debug("channel %d: big input buffer %d",
1074 c->self, buffer_len(&c->input));
5260325f 1075 return 0;
7368a6c8 1076 }
1077 if (buffer_len(&c->output) > packet_get_maxsize()) {
1078 debug("channel %d: big output buffer %d",
1079 c->self, buffer_len(&c->output));
5260325f 1080 return 0;
7368a6c8 1081 }
5260325f 1082 }
8efc0c15 1083 }
5260325f 1084 return 1;
8efc0c15 1085}
1086
6ae2364d 1087void
7368a6c8 1088channel_input_ieof(int type, int plen)
1089{
1090 int id;
1091 Channel *c;
1092
1093 packet_integrity_check(plen, 4, type);
1094
1095 id = packet_get_int();
1096 c = channel_lookup(id);
1097 if (c == NULL)
1098 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1099 chan_rcvd_ieof(c);
1100}
8efc0c15 1101
6ae2364d 1102void
7368a6c8 1103channel_input_close(int type, int plen)
8efc0c15 1104{
7368a6c8 1105 int id;
1106 Channel *c;
5260325f 1107
7368a6c8 1108 packet_integrity_check(plen, 4, type);
1109
1110 id = packet_get_int();
1111 c = channel_lookup(id);
1112 if (c == NULL)
1113 packet_disconnect("Received close for nonexistent channel %d.", id);
aa3378df 1114
1115 /*
1116 * Send a confirmation that we have closed the channel and no more
1117 * data is coming for it.
1118 */
5260325f 1119 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
7368a6c8 1120 packet_put_int(c->remote_id);
5260325f 1121 packet_send();
1122
aa3378df 1123 /*
1124 * If the channel is in closed state, we have sent a close request,
1125 * and the other side will eventually respond with a confirmation.
1126 * Thus, we cannot free the channel here, because then there would be
1127 * no-one to receive the confirmation. The channel gets freed when
1128 * the confirmation arrives.
1129 */
7368a6c8 1130 if (c->type != SSH_CHANNEL_CLOSED) {
aa3378df 1131 /*
1132 * Not a closed channel - mark it as draining, which will
1133 * cause it to be freed later.
1134 */
7368a6c8 1135 buffer_consume(&c->input, buffer_len(&c->input));
1136 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
5260325f 1137 }
8efc0c15 1138}
1139
7368a6c8 1140/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
6ae2364d 1141void
7368a6c8 1142channel_input_oclose(int type, int plen)
8efc0c15 1143{
7368a6c8 1144 int id = packet_get_int();
1145 Channel *c = channel_lookup(id);
1146 packet_integrity_check(plen, 4, type);
1147 if (c == NULL)
1148 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1149 chan_rcvd_oclose(c);
8efc0c15 1150}
1151
6ae2364d 1152void
7368a6c8 1153channel_input_close_confirmation(int type, int plen)
1154{
1155 int id = packet_get_int();
1156 Channel *c = channel_lookup(id);
1157
6ae2364d 1158 packet_done();
7368a6c8 1159 if (c == NULL)
1160 packet_disconnect("Received close confirmation for "
1161 "out-of-range channel %d.", id);
1162 if (c->type != SSH_CHANNEL_CLOSED)
1163 packet_disconnect("Received close confirmation for "
1164 "non-closed channel %d (type %d).", id, c->type);
1165 channel_free(c->self);
1166}
8efc0c15 1167
6ae2364d 1168void
7368a6c8 1169channel_input_open_confirmation(int type, int plen)
8efc0c15 1170{
7368a6c8 1171 int id, remote_id;
1172 Channel *c;
5260325f 1173
7e7327a1 1174 if (!compat20)
1175 packet_integrity_check(plen, 4 + 4, type);
5260325f 1176
7368a6c8 1177 id = packet_get_int();
1178 c = channel_lookup(id);
5260325f 1179
7368a6c8 1180 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1181 packet_disconnect("Received open confirmation for "
1182 "non-opening channel %d.", id);
1183 remote_id = packet_get_int();
aa3378df 1184 /* Record the remote channel number and mark that the channel is now open. */
7368a6c8 1185 c->remote_id = remote_id;
1186 c->type = SSH_CHANNEL_OPEN;
7e7327a1 1187
1188 if (compat20) {
1189 c->remote_window = packet_get_int();
1190 c->remote_maxpacket = packet_get_int();
6ae2364d 1191 packet_done();
7e7327a1 1192 if (c->cb_fn != NULL && c->cb_event == type) {
1193 debug("callback start");
1194 c->cb_fn(c->self, c->cb_arg);
1195 debug("callback done");
1196 }
1197 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1198 c->remote_window, c->remote_maxpacket);
1199 }
8efc0c15 1200}
1201
6ae2364d 1202void
7368a6c8 1203channel_input_open_failure(int type, int plen)
8efc0c15 1204{
7368a6c8 1205 int id;
1206 Channel *c;
5260325f 1207
7e7327a1 1208 if (!compat20)
1209 packet_integrity_check(plen, 4, type);
7368a6c8 1210
1211 id = packet_get_int();
1212 c = channel_lookup(id);
1213
1214 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1215 packet_disconnect("Received open failure for "
1216 "non-opening channel %d.", id);
7e7327a1 1217 if (compat20) {
1218 int reason = packet_get_int();
1219 char *msg = packet_get_string(NULL);
6ae2364d 1220 char *lang = packet_get_string(NULL);
7e7327a1 1221 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
6ae2364d 1222 packet_done();
7e7327a1 1223 xfree(msg);
6ae2364d 1224 xfree(lang);
7e7327a1 1225 }
5260325f 1226 /* Free the channel. This will also close the socket. */
7368a6c8 1227 channel_free(id);
8efc0c15 1228}
1229
7e7327a1 1230void
1231channel_input_channel_request(int type, int plen)
1232{
1233 int id;
1234 Channel *c;
1235
1236 id = packet_get_int();
1237 c = channel_lookup(id);
1238
1239 if (c == NULL ||
1240 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1241 packet_disconnect("Received request for "
1242 "non-open channel %d.", id);
1243 if (c->cb_fn != NULL && c->cb_event == type) {
1244 debug("callback start");
1245 c->cb_fn(c->self, c->cb_arg);
1246 debug("callback done");
1247 } else {
1248 char *service = packet_get_string(NULL);
1249 debug("channel: %d rcvd request for %s", c->self, service);
1250debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
1251 xfree(service);
1252 }
1253}
1254
6ae2364d 1255void
7e7327a1 1256channel_input_window_adjust(int type, int plen)
1257{
1258 Channel *c;
1259 int id, adjust;
1260
1261 if (!compat20)
1262 return;
1263
1264 /* Get the channel number and verify it. */
1265 id = packet_get_int();
1266 c = channel_lookup(id);
1267
1268 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1269 log("Received window adjust for "
1270 "non-open channel %d.", id);
1271 return;
1272 }
1273 adjust = packet_get_int();
6ae2364d 1274 packet_done();
7e7327a1 1275 debug("channel %d: rcvd adjust %d", id, adjust);
1276 c->remote_window += adjust;
1277}
1278
aa3378df 1279/*
1280 * Stops listening for channels, and removes any unix domain sockets that we
1281 * might have.
1282 */
8efc0c15 1283
6ae2364d 1284void
5260325f 1285channel_stop_listening()
8efc0c15 1286{
5260325f 1287 int i;
1288 for (i = 0; i < channels_alloc; i++) {
1289 switch (channels[i].type) {
1290 case SSH_CHANNEL_AUTH_SOCKET:
1291 close(channels[i].sock);
1292 remove(channels[i].path);
1293 channel_free(i);
1294 break;
1295 case SSH_CHANNEL_PORT_LISTENER:
1296 case SSH_CHANNEL_X11_LISTENER:
1297 close(channels[i].sock);
1298 channel_free(i);
1299 break;
1300 default:
1301 break;
1302 }
8efc0c15 1303 }
8efc0c15 1304}
1305
aa3378df 1306/*
0fbe8c74 1307 * Closes the sockets/fds of all channels. This is used to close extra file
aa3378df 1308 * descriptors after a fork.
1309 */
8efc0c15 1310
6ae2364d 1311void
5260325f 1312channel_close_all()
8efc0c15 1313{
5260325f 1314 int i;
0fbe8c74 1315 for (i = 0; i < channels_alloc; i++)
5260325f 1316 if (channels[i].type != SSH_CHANNEL_FREE)
0fbe8c74 1317 channel_close_fds(&channels[i]);
8efc0c15 1318}
1319
1320/* Returns the maximum file descriptor number used by the channels. */
1321
6ae2364d 1322int
5260325f 1323channel_max_fd()
8efc0c15 1324{
5260325f 1325 return channel_max_fd_value;
8efc0c15 1326}
1327
1328/* Returns true if any channel is still open. */
1329
6ae2364d 1330int
5260325f 1331channel_still_open()
8efc0c15 1332{
5260325f 1333 unsigned int i;
1334 for (i = 0; i < channels_alloc; i++)
1335 switch (channels[i].type) {
1336 case SSH_CHANNEL_FREE:
1337 case SSH_CHANNEL_X11_LISTENER:
1338 case SSH_CHANNEL_PORT_LISTENER:
1339 case SSH_CHANNEL_CLOSED:
1340 case SSH_CHANNEL_AUTH_SOCKET:
1341 continue;
7e7327a1 1342 case SSH_CHANNEL_LARVAL:
1343 if (!compat20)
1344 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1345 continue;
5260325f 1346 case SSH_CHANNEL_OPENING:
1347 case SSH_CHANNEL_OPEN:
1348 case SSH_CHANNEL_X11_OPEN:
1349 return 1;
1350 case SSH_CHANNEL_INPUT_DRAINING:
1351 case SSH_CHANNEL_OUTPUT_DRAINING:
1352 if (!compat13)
1353 fatal("cannot happen: OUT_DRAIN");
1354 return 1;
1355 default:
1356 fatal("channel_still_open: bad channel type %d", channels[i].type);
1357 /* NOTREACHED */
1358 }
1359 return 0;
8efc0c15 1360}
1361
aa3378df 1362/*
1363 * Returns a message describing the currently open forwarded connections,
1364 * suitable for sending to the client. The message contains crlf pairs for
1365 * newlines.
1366 */
8efc0c15 1367
5260325f 1368char *
1369channel_open_message()
8efc0c15 1370{
5260325f 1371 Buffer buffer;
1372 int i;
1373 char buf[512], *cp;
1374
1375 buffer_init(&buffer);
1376 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
8efc0c15 1377 buffer_append(&buffer, buf, strlen(buf));
5260325f 1378 for (i = 0; i < channels_alloc; i++) {
1379 Channel *c = &channels[i];
1380 switch (c->type) {
1381 case SSH_CHANNEL_FREE:
1382 case SSH_CHANNEL_X11_LISTENER:
1383 case SSH_CHANNEL_PORT_LISTENER:
1384 case SSH_CHANNEL_CLOSED:
1385 case SSH_CHANNEL_AUTH_SOCKET:
1386 continue;
7e7327a1 1387 case SSH_CHANNEL_LARVAL:
5260325f 1388 case SSH_CHANNEL_OPENING:
1389 case SSH_CHANNEL_OPEN:
1390 case SSH_CHANNEL_X11_OPEN:
1391 case SSH_CHANNEL_INPUT_DRAINING:
1392 case SSH_CHANNEL_OUTPUT_DRAINING:
7368a6c8 1393 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
48e671d5 1394 c->self, c->remote_name,
1395 c->type, c->remote_id,
1396 c->istate, buffer_len(&c->input),
7368a6c8 1397 c->ostate, buffer_len(&c->output),
1398 c->rfd, c->wfd);
5260325f 1399 buffer_append(&buffer, buf, strlen(buf));
1400 continue;
1401 default:
7368a6c8 1402 fatal("channel_open_message: bad channel type %d", c->type);
5260325f 1403 /* NOTREACHED */
1404 }
1405 }
1406 buffer_append(&buffer, "\0", 1);
1407 cp = xstrdup(buffer_ptr(&buffer));
1408 buffer_free(&buffer);
1409 return cp;
8efc0c15 1410}
1411
aa3378df 1412/*
1413 * Initiate forwarding of connections to local port "port" through the secure
1414 * channel to host:port from remote side.
1415 */
8efc0c15 1416
6ae2364d 1417void
57112b5a 1418channel_request_local_forwarding(u_short port, const char *host,
95f1eccc 1419 u_short host_port, int gateway_ports)
8efc0c15 1420{
48e671d5 1421 int success, ch, sock, on = 1;
1422 struct addrinfo hints, *ai, *aitop;
1423 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
aa3378df 1424 struct linger linger;
5260325f 1425
1426 if (strlen(host) > sizeof(channels[0].path) - 1)
1427 packet_disconnect("Forward host name too long.");
1428
aa3378df 1429 /*
48e671d5 1430 * getaddrinfo returns a loopback address if the hostname is
1431 * set to NULL and hints.ai_flags is not AI_PASSIVE
aa3378df 1432 */
48e671d5 1433 memset(&hints, 0, sizeof(hints));
1434 hints.ai_family = IPv4or6;
1435 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1436 hints.ai_socktype = SOCK_STREAM;
1437 snprintf(strport, sizeof strport, "%d", port);
1438 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1439 packet_disconnect("getaddrinfo: fatal error");
1440
1441 success = 0;
1442 for (ai = aitop; ai; ai = ai->ai_next) {
1443 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1444 continue;
1445 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1446 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1447 error("channel_request_local_forwarding: getnameinfo failed");
1448 continue;
1449 }
1450 /* Create a port to listen for the host. */
1451 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1452 if (sock < 0) {
1453 /* this is no error since kernel may not support ipv6 */
1454 verbose("socket: %.100s", strerror(errno));
1455 continue;
1456 }
1457 /*
1458 * Set socket options. We would like the socket to disappear
1459 * as soon as it has been closed for whatever reason.
1460 */
1461 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1462 linger.l_onoff = 1;
1463 linger.l_linger = 5;
1464 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1465 debug("Local forwarding listening on %s port %s.", ntop, strport);
1466
1467 /* Bind the socket to the address. */
1468 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1469 /* address can be in use ipv6 address is already bound */
16218745 1470 if (!ai->ai_next)
1471 error("bind: %.100s", strerror(errno));
1472 else
1473 verbose("bind: %.100s", strerror(errno));
1474
48e671d5 1475 close(sock);
1476 continue;
1477 }
1478 /* Start listening for connections on the socket. */
1479 if (listen(sock, 5) < 0) {
1480 error("listen: %.100s", strerror(errno));
1481 close(sock);
1482 continue;
1483 }
1484 /* Allocate a channel number for the socket. */
7368a6c8 1485 ch = channel_new(
1486 "port listener", SSH_CHANNEL_PORT_LISTENER,
1487 sock, sock, -1,
0b242b12 1488 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
7368a6c8 1489 0, xstrdup("port listener"));
48e671d5 1490 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
1491 channels[ch].host_port = host_port;
1492 channels[ch].listening_port = port;
1493 success = 1;
1494 }
1495 if (success == 0)
1496 packet_disconnect("cannot listen port: %d", port);
1497 freeaddrinfo(aitop);
5260325f 1498}
8efc0c15 1499
aa3378df 1500/*
1501 * Initiate forwarding of connections to port "port" on remote host through
1502 * the secure channel to host:port from local side.
1503 */
8efc0c15 1504
6ae2364d 1505void
7368a6c8 1506channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1507 u_short port_to_connect)
8efc0c15 1508{
5260325f 1509 int payload_len;
1510 /* Record locally that connection to this host/port is permitted. */
1511 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1512 fatal("channel_request_remote_forwarding: too many forwards");
1513
7368a6c8 1514 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1515 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1516 permitted_opens[num_permitted_opens].listen_port = listen_port;
5260325f 1517 num_permitted_opens++;
1518
1519 /* Send the forward request to the remote side. */
7e7327a1 1520 if (compat20) {
1521 const char *address_to_bind = "0.0.0.0";
1522 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1523 packet_put_cstring("tcpip-forward");
1524 packet_put_char(0); /* boolean: want reply */
1525 packet_put_cstring(address_to_bind);
1526 packet_put_int(listen_port);
1527 } else {
1528 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
7e7327a1 1529 packet_put_int(listen_port);
d6f24e45 1530 packet_put_cstring(host_to_connect);
1531 packet_put_int(port_to_connect);
7e7327a1 1532 packet_send();
1533 packet_write_wait();
1534 /*
1535 * Wait for response from the remote side. It will send a disconnect
1536 * message on failure, and we will never see it here.
1537 */
1538 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1539 }
8efc0c15 1540}
1541
aa3378df 1542/*
1543 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1544 * listening for the port, and sends back a success reply (or disconnect
1545 * message if there was an error). This never returns if there was an error.
1546 */
8efc0c15 1547
6ae2364d 1548void
1d1ffb87 1549channel_input_port_forward_request(int is_root, int gateway_ports)
8efc0c15 1550{
57112b5a 1551 u_short port, host_port;
5260325f 1552 char *hostname;
1553
1554 /* Get arguments from the packet. */
1555 port = packet_get_int();
1556 hostname = packet_get_string(NULL);
1557 host_port = packet_get_int();
1558
aa3378df 1559 /*
1560 * Check that an unprivileged user is not trying to forward a
1561 * privileged port.
1562 */
5260325f 1563 if (port < IPPORT_RESERVED && !is_root)
1564 packet_disconnect("Requested forwarding of port %d but user is not root.",
1565 port);
95f1eccc 1566 /*
1567 * Initiate forwarding,
95f1eccc 1568 */
1d1ffb87 1569 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
5260325f 1570
1571 /* Free the argument string. */
1572 xfree(hostname);
8efc0c15 1573}
1574
7368a6c8 1575/* XXX move to aux.c */
1576int
1577channel_connect_to(const char *host, u_short host_port)
8efc0c15 1578{
48e671d5 1579 struct addrinfo hints, *ai, *aitop;
1580 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1581 int gaierr;
7368a6c8 1582 int sock = -1;
48e671d5 1583
1584 memset(&hints, 0, sizeof(hints));
1585 hints.ai_family = IPv4or6;
1586 hints.ai_socktype = SOCK_STREAM;
1587 snprintf(strport, sizeof strport, "%d", host_port);
1588 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1589 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
7368a6c8 1590 return -1;
48e671d5 1591 }
48e671d5 1592 for (ai = aitop; ai; ai = ai->ai_next) {
1593 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1594 continue;
1595 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1596 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
7368a6c8 1597 error("channel_connect_to: getnameinfo failed");
48e671d5 1598 continue;
5260325f 1599 }
48e671d5 1600 /* Create the socket. */
1601 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1602 if (sock < 0) {
1603 error("socket: %.100s", strerror(errno));
1604 continue;
5260325f 1605 }
48e671d5 1606 /* Connect to the host/port. */
1607 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1608 error("connect %.100s port %s: %.100s", ntop, strport,
1609 strerror(errno));
1610 close(sock);
1611 continue; /* fail -- try next */
1612 }
1613 break; /* success */
5260325f 1614
5260325f 1615 }
48e671d5 1616 freeaddrinfo(aitop);
48e671d5 1617 if (!ai) {
1618 error("connect %.100s port %d: failed.", host, host_port);
7368a6c8 1619 return -1;
8efc0c15 1620 }
7368a6c8 1621 /* success */
1622 return sock;
1623}
48e671d5 1624
7368a6c8 1625/*
1626 * This is called after receiving PORT_OPEN message. This attempts to
1627 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1628 * or CHANNEL_OPEN_FAILURE.
1629 */
5260325f 1630
6ae2364d 1631void
7368a6c8 1632channel_input_port_open(int type, int plen)
1633{
1634 u_short host_port;
1635 char *host, *originator_string;
1636 int remote_channel, sock = -1, newch, i, denied;
1637 unsigned int host_len, originator_len;
5260325f 1638
7368a6c8 1639 /* Get remote channel number. */
1640 remote_channel = packet_get_int();
5260325f 1641
7368a6c8 1642 /* Get host name to connect to. */
1643 host = packet_get_string(&host_len);
5260325f 1644
7368a6c8 1645 /* Get port to connect to. */
1646 host_port = packet_get_int();
5260325f 1647
7368a6c8 1648 /* Get remote originator name. */
1649 if (have_hostname_in_open) {
1650 originator_string = packet_get_string(&originator_len);
1651 originator_len += 4; /* size of packet_int */
1652 } else {
1653 originator_string = xstrdup("unknown (remote did not supply name)");
1654 originator_len = 0; /* no originator supplied */
1655 }
5260325f 1656
7368a6c8 1657 packet_integrity_check(plen,
1658 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
1659
1660 /* Check if opening that port is permitted. */
1661 denied = 0;
1662 if (!all_opens_permitted) {
1663 /* Go trough all permitted ports. */
1664 for (i = 0; i < num_permitted_opens; i++)
1665 if (permitted_opens[i].port_to_connect == host_port &&
1666 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1667 break;
1668
1669 /* Check if we found the requested port among those permitted. */
1670 if (i >= num_permitted_opens) {
1671 /* The port is not permitted. */
1672 log("Received request to connect to %.100s:%d, but the request was denied.",
1673 host, host_port);
1674 denied = 1;
1675 }
1676 }
1677 sock = denied ? -1 : channel_connect_to(host, host_port);
1678 if (sock > 0) {
1679 /* Allocate a channel for this connection. */
1680 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1681 channels[newch].remote_id = remote_channel;
1682
1683 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1684 packet_put_int(remote_channel);
1685 packet_put_int(newch);
1686 packet_send();
1687 } else {
1688 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1689 packet_put_int(remote_channel);
1690 packet_send();
1691 }
1692 xfree(host);
8efc0c15 1693}
1694
aa3378df 1695/*
1696 * Creates an internet domain socket for listening for X11 connections.
1697 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1698 * occurs.
1699 */
8efc0c15 1700
48e671d5 1701#define NUM_SOCKS 10
1702
5260325f 1703char *
95f1eccc 1704x11_create_display_inet(int screen_number, int x11_display_offset)
8efc0c15 1705{
57112b5a 1706 int display_number, sock;
1707 u_short port;
48e671d5 1708 struct addrinfo hints, *ai, *aitop;
1709 char strport[NI_MAXSERV];
1710 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1711 char display[512];
5260325f 1712 char hostname[MAXHOSTNAMELEN];
1713
95f1eccc 1714 for (display_number = x11_display_offset;
5260325f 1715 display_number < MAX_DISPLAYS;
1716 display_number++) {
1717 port = 6000 + display_number;
48e671d5 1718 memset(&hints, 0, sizeof(hints));
1719 hints.ai_family = IPv4or6;
1720 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1721 hints.ai_socktype = SOCK_STREAM;
1722 snprintf(strport, sizeof strport, "%d", port);
1723 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1724 error("getaddrinfo: %.100s", gai_strerror(gaierr));
5260325f 1725 return NULL;
1726 }
48e671d5 1727 for (ai = aitop; ai; ai = ai->ai_next) {
1728 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1729 continue;
1730 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1731 if (sock < 0) {
80a44451 1732 if (errno != EINVAL) {
1733 error("socket: %.100s", strerror(errno));
1734 return NULL;
1735 } else {
1736 debug("Socket family %d not supported [X11 disp create]", ai->ai_family);
1737 continue;
1738 }
48e671d5 1739 }
1740 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1741 debug("bind port %d: %.100s", port, strerror(errno));
1742 shutdown(sock, SHUT_RDWR);
1743 close(sock);
16218745 1744
1745 if (ai->ai_next)
1746 continue;
1747
48e671d5 1748 for (n = 0; n < num_socks; n++) {
1749 shutdown(socks[n], SHUT_RDWR);
1750 close(socks[n]);
1751 }
1752 num_socks = 0;
1753 break;
1754 }
1755 socks[num_socks++] = sock;
80faa19f 1756#ifndef DONT_TRY_OTHER_AF
48e671d5 1757 if (num_socks == NUM_SOCKS)
1758 break;
80faa19f 1759#else
1760 break;
1761#endif
5260325f 1762 }
48e671d5 1763 if (num_socks > 0)
1764 break;
5260325f 1765 }
1766 if (display_number >= MAX_DISPLAYS) {
1767 error("Failed to allocate internet-domain X11 display socket.");
1768 return NULL;
8efc0c15 1769 }
5260325f 1770 /* Start listening for connections on the socket. */
48e671d5 1771 for (n = 0; n < num_socks; n++) {
1772 sock = socks[n];
1773 if (listen(sock, 5) < 0) {
1774 error("listen: %.100s", strerror(errno));
1775 shutdown(sock, SHUT_RDWR);
1776 close(sock);
1777 return NULL;
1778 }
8efc0c15 1779 }
48e671d5 1780
5260325f 1781 /* Set up a suitable value for the DISPLAY variable. */
a7effaac 1782
5260325f 1783 if (gethostname(hostname, sizeof(hostname)) < 0)
1784 fatal("gethostname: %.100s", strerror(errno));
a7effaac 1785
1786#ifdef IPADDR_IN_DISPLAY
1787 /*
1788 * HPUX detects the local hostname in the DISPLAY variable and tries
1789 * to set up a shared memory connection to the server, which it
1790 * incorrectly supposes to be local.
1791 *
1792 * The workaround - as used in later $$H and other programs - is
1793 * is to set display to the host's IP address.
1794 */
1795 {
1796 struct hostent *he;
1797 struct in_addr my_addr;
1798
1799 he = gethostbyname(hostname);
1800 if (he == NULL) {
1801 error("[X11-broken-fwd-hostname-workaround] Could not get "
1802 "IP address for hostname %s.", hostname);
1803
1804 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1805 "Could not get IP address for hostname %s.", hostname);
1806
1807 shutdown(sock, SHUT_RDWR);
1808 close(sock);
1809
1810 return NULL;
1811 }
1812
1813 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1814
1815 /* Set DISPLAY to <ip address>:screen.display */
48e671d5 1816 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
a7effaac 1817 display_number, screen_number);
1818 }
1819#else /* IPADDR_IN_DISPLAY */
1820 /* Just set DISPLAY to hostname:screen.display */
48e671d5 1821 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
a7effaac 1822 display_number, screen_number);
1823#endif /* IPADDR_IN_DISPLAY */
5260325f 1824
48e671d5 1825 /* Allocate a channel for each socket. */
1826 for (n = 0; n < num_socks; n++) {
1827 sock = socks[n];
0b242b12 1828 (void) channel_new("x11 listener",
1829 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
1830 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
1831 0, xstrdup("X11 inet listener"));
48e671d5 1832 }
5260325f 1833
1834 /* Return a suitable value for the DISPLAY environment variable. */
48e671d5 1835 return xstrdup(display);
8efc0c15 1836}
1837
1838#ifndef X_UNIX_PATH
1839#define X_UNIX_PATH "/tmp/.X11-unix/X"
1840#endif
1841
1842static
1843int
2f450969 1844connect_local_xsocket(unsigned int dnr)
8efc0c15 1845{
5260325f 1846 static const char *const x_sockets[] = {
1847 X_UNIX_PATH "%u",
1848 "/var/X/.X11-unix/X" "%u",
1849 "/usr/spool/sockets/X11/" "%u",
1850 NULL
1851 };
1852 int sock;
1853 struct sockaddr_un addr;
1854 const char *const * path;
1855
1856 for (path = x_sockets; *path; ++path) {
1857 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1858 if (sock < 0)
1859 error("socket: %.100s", strerror(errno));
1860 memset(&addr, 0, sizeof(addr));
1861 addr.sun_family = AF_UNIX;
1862 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1863 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1864 return sock;
1865 close(sock);
1866 }
1867 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1868 return -1;
8efc0c15 1869}
1870
0b242b12 1871int
1872x11_connect_display(void)
8efc0c15 1873{
0b242b12 1874 int display_number, sock = 0;
5260325f 1875 const char *display;
0b242b12 1876 char buf[1024], *cp;
48e671d5 1877 struct addrinfo hints, *ai, *aitop;
1878 char strport[NI_MAXSERV];
1879 int gaierr;
5260325f 1880
5260325f 1881 /* Try to open a socket for the local X server. */
1882 display = getenv("DISPLAY");
1883 if (!display) {
1884 error("DISPLAY not set.");
0b242b12 1885 return -1;
5260325f 1886 }
aa3378df 1887 /*
1888 * Now we decode the value of the DISPLAY variable and make a
1889 * connection to the real X server.
1890 */
1891
1892 /*
1893 * Check if it is a unix domain socket. Unix domain displays are in
1894 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1895 */
5260325f 1896 if (strncmp(display, "unix:", 5) == 0 ||
1897 display[0] == ':') {
1898 /* Connect to the unix domain socket. */
1899 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1900 error("Could not parse display number from DISPLAY: %.100s",
1901 display);
0b242b12 1902 return -1;
5260325f 1903 }
1904 /* Create a socket. */
1905 sock = connect_local_xsocket(display_number);
1906 if (sock < 0)
0b242b12 1907 return -1;
5260325f 1908
1909 /* OK, we now have a connection to the display. */
0b242b12 1910 return sock;
5260325f 1911 }
aa3378df 1912 /*
1913 * Connect to an inet socket. The DISPLAY value is supposedly
1914 * hostname:d[.s], where hostname may also be numeric IP address.
1915 */
5260325f 1916 strncpy(buf, display, sizeof(buf));
1917 buf[sizeof(buf) - 1] = 0;
1918 cp = strchr(buf, ':');
1919 if (!cp) {
1920 error("Could not find ':' in DISPLAY: %.100s", display);
0b242b12 1921 return -1;
5260325f 1922 }
1923 *cp = 0;
aa3378df 1924 /* buf now contains the host name. But first we parse the display number. */
5260325f 1925 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1926 error("Could not parse display number from DISPLAY: %.100s",
1927 display);
0b242b12 1928 return -1;
5260325f 1929 }
5260325f 1930
48e671d5 1931 /* Look up the host address */
1932 memset(&hints, 0, sizeof(hints));
1933 hints.ai_family = IPv4or6;
1934 hints.ai_socktype = SOCK_STREAM;
1935 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
1936 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1937 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
0b242b12 1938 return -1;
8efc0c15 1939 }
48e671d5 1940 for (ai = aitop; ai; ai = ai->ai_next) {
1941 /* Create a socket. */
1942 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1943 if (sock < 0) {
1944 debug("socket: %.100s", strerror(errno));
7368a6c8 1945 continue;
1946 }
1947 /* Connect it to the display. */
1948 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1949 debug("connect %.100s port %d: %.100s", buf,
1950 6000 + display_number, strerror(errno));
1951 close(sock);
1952 continue;
1953 }
1954 /* Success */
1955 break;
48e671d5 1956 }
48e671d5 1957 freeaddrinfo(aitop);
1958 if (!ai) {
6ae2364d 1959 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
48e671d5 1960 strerror(errno));
0b242b12 1961 return -1;
8efc0c15 1962 }
0b242b12 1963 return sock;
1964}
5260325f 1965
0b242b12 1966/*
1967 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1968 * the remote channel number. We should do whatever we want, and respond
1969 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1970 */
5260325f 1971
0b242b12 1972void
1973x11_input_open(int type, int plen)
1974{
1975 int remote_channel, sock = 0, newch;
1976 char *remote_host;
1977 unsigned int remote_len;
5260325f 1978
0b242b12 1979 /* Get remote channel number. */
1980 remote_channel = packet_get_int();
5260325f 1981
0b242b12 1982 /* Get remote originator name. */
1983 if (have_hostname_in_open) {
1984 remote_host = packet_get_string(&remote_len);
1985 remote_len += 4;
1986 } else {
1987 remote_host = xstrdup("unknown (remote did not supply name)");
1988 remote_len = 0;
1989 }
1990
1991 debug("Received X11 open request.");
1992 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
1993
1994 /* Obtain a connection to the real X display. */
1995 sock = x11_connect_display();
1996 if (sock == -1) {
1997 /* Send refusal to the remote host. */
1998 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1999 packet_put_int(remote_channel);
2000 packet_send();
2001 } else {
2002 /* Allocate a channel for this connection. */
2003 newch = channel_allocate(
2004 (x11_saved_proto == NULL) ?
2005 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2006 sock, remote_host);
2007 channels[newch].remote_id = remote_channel;
2008
2009 /* Send a confirmation to the remote host. */
2010 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2011 packet_put_int(remote_channel);
2012 packet_put_int(newch);
2013 packet_send();
2014 }
8efc0c15 2015}
2016
aa3378df 2017/*
2018 * Requests forwarding of X11 connections, generates fake authentication
2019 * data, and enables authentication spoofing.
2020 */
8efc0c15 2021
6ae2364d 2022void
0b242b12 2023x11_request_forwarding_with_spoofing(int client_session_id,
2024 const char *proto, const char *data)
8efc0c15 2025{
5260325f 2026 unsigned int data_len = (unsigned int) strlen(data) / 2;
2027 unsigned int i, value;
2028 char *new_data;
2029 int screen_number;
2030 const char *cp;
2031 u_int32_t rand = 0;
2032
2033 cp = getenv("DISPLAY");
2034 if (cp)
2035 cp = strchr(cp, ':');
2036 if (cp)
2037 cp = strchr(cp, '.');
2038 if (cp)
2039 screen_number = atoi(cp + 1);
2040 else
2041 screen_number = 0;
2042
2043 /* Save protocol name. */
2044 x11_saved_proto = xstrdup(proto);
2045
aa3378df 2046 /*
2047 * Extract real authentication data and generate fake data of the
2048 * same length.
2049 */
5260325f 2050 x11_saved_data = xmalloc(data_len);
2051 x11_fake_data = xmalloc(data_len);
2052 for (i = 0; i < data_len; i++) {
2053 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2054 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2055 if (i % 4 == 0)
2056 rand = arc4random();
2057 x11_saved_data[i] = value;
2058 x11_fake_data[i] = rand & 0xff;
2059 rand >>= 8;
2060 }
2061 x11_saved_data_len = data_len;
2062 x11_fake_data_len = data_len;
2063
2064 /* Convert the fake data into hex. */
2065 new_data = xmalloc(2 * data_len + 1);
2066 for (i = 0; i < data_len; i++)
2067 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
2068
2069 /* Send the request packet. */
0b242b12 2070 if (compat20) {
2071 channel_request_start(client_session_id, "x11-req", 0);
2072 packet_put_char(0); /* XXX bool single connection */
2073 } else {
2074 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2075 }
2076 packet_put_cstring(proto);
2077 packet_put_cstring(new_data);
5260325f 2078 packet_put_int(screen_number);
2079 packet_send();
2080 packet_write_wait();
2081 xfree(new_data);
8efc0c15 2082}
2083
2084/* Sends a message to the server to request authentication fd forwarding. */
2085
6ae2364d 2086void
5260325f 2087auth_request_forwarding()
8efc0c15 2088{
5260325f 2089 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2090 packet_send();
2091 packet_write_wait();
8efc0c15 2092}
2093
aa3378df 2094/*
2095 * Returns the name of the forwarded authentication socket. Returns NULL if
2096 * there is no forwarded authentication socket. The returned value points to
2097 * a static buffer.
2098 */
8efc0c15 2099
5260325f 2100char *
2101auth_get_socket_name()
8efc0c15 2102{
5260325f 2103 return channel_forwarded_auth_socket_name;
8efc0c15 2104}
2105
2106/* removes the agent forwarding socket */
2107
6ae2364d 2108void
5260325f 2109cleanup_socket(void)
2110{
2111 remove(channel_forwarded_auth_socket_name);
2112 rmdir(channel_forwarded_auth_socket_dir);
8efc0c15 2113}
2114
aa3378df 2115/*
fa649821 2116 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
aa3378df 2117 * This starts forwarding authentication requests.
2118 */
8efc0c15 2119
fa649821 2120int
5260325f 2121auth_input_request_forwarding(struct passwd * pw)
8efc0c15 2122{
5260325f 2123 int sock, newch;
2124 struct sockaddr_un sunaddr;
2125
2126 if (auth_get_socket_name() != NULL)
2127 fatal("Protocol error: authentication forwarding requested twice.");
2128
2129 /* Temporarily drop privileged uid for mkdir/bind. */
2130 temporarily_use_uid(pw->pw_uid);
2131
2132 /* Allocate a buffer for the socket name, and format the name. */
2133 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2134 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2135 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
2136
2137 /* Create private directory for socket */
fa649821 2138 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2139 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2140 strerror(errno));
2141 restore_uid();
2142 xfree(channel_forwarded_auth_socket_name);
2143 xfree(channel_forwarded_auth_socket_dir);
2144 channel_forwarded_auth_socket_name = NULL;
2145 channel_forwarded_auth_socket_dir = NULL;
2146 return 0;
2147 }
5260325f 2148 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2149 channel_forwarded_auth_socket_dir, (int) getpid());
2150
2151 if (atexit(cleanup_socket) < 0) {
2152 int saved = errno;
2153 cleanup_socket();
2154 packet_disconnect("socket: %.100s", strerror(saved));
2155 }
2156 /* Create the socket. */
2157 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2158 if (sock < 0)
2159 packet_disconnect("socket: %.100s", strerror(errno));
2160
2161 /* Bind it to the name. */
2162 memset(&sunaddr, 0, sizeof(sunaddr));
2163 sunaddr.sun_family = AF_UNIX;
2164 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2165 sizeof(sunaddr.sun_path));
2166
2167 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2168 packet_disconnect("bind: %.100s", strerror(errno));
2169
2170 /* Restore the privileged uid. */
2171 restore_uid();
2172
2173 /* Start listening on the socket. */
2174 if (listen(sock, 5) < 0)
2175 packet_disconnect("listen: %.100s", strerror(errno));
2176
2177 /* Allocate a channel for the authentication agent socket. */
2178 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
2179 xstrdup("auth socket"));
a408af76 2180 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2181 sizeof(channels[newch].path));
fa649821 2182 return 1;
8efc0c15 2183}
2184
2185/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2186
6ae2364d 2187void
7368a6c8 2188auth_input_open_request(int type, int plen)
8efc0c15 2189{
5260325f 2190 int remch, sock, newch;
2191 char *dummyname;
2192
7368a6c8 2193 packet_integrity_check(plen, 4, type);
2194
5260325f 2195 /* Read the remote channel number from the message. */
2196 remch = packet_get_int();
2197
aa3378df 2198 /*
2199 * Get a connection to the local authentication agent (this may again
2200 * get forwarded).
2201 */
5260325f 2202 sock = ssh_get_authentication_socket();
2203
aa3378df 2204 /*
2205 * If we could not connect the agent, send an error message back to
2206 * the server. This should never happen unless the agent dies,
2207 * because authentication forwarding is only enabled if we have an
2208 * agent.
2209 */
5260325f 2210 if (sock < 0) {
2211 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2212 packet_put_int(remch);
2213 packet_send();
2214 return;
2215 }
2216 debug("Forwarding authentication connection.");
2217
aa3378df 2218 /*
2219 * Dummy host name. This will be freed when the channel is freed; it
2220 * will still be valid in the packet_put_string below since the
2221 * channel cannot yet be freed at that point.
2222 */
5260325f 2223 dummyname = xstrdup("authentication agent connection");
2224
2225 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2226 channels[newch].remote_id = remch;
2227
2228 /* Send a confirmation to the remote host. */
2229 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2230 packet_put_int(remch);
2231 packet_put_int(newch);
2232 packet_send();
8efc0c15 2233}
7e7327a1 2234
2235void
0b242b12 2236channel_start_open(int id)
7e7327a1 2237{
2238 Channel *c = channel_lookup(id);
2239 if (c == NULL) {
2240 log("channel_open: %d: bad id", id);
2241 return;
2242 }
0b242b12 2243 debug("send channel open %d", id);
7e7327a1 2244 packet_start(SSH2_MSG_CHANNEL_OPEN);
2245 packet_put_cstring(c->ctype);
2246 packet_put_int(c->self);
2247 packet_put_int(c->local_window);
2248 packet_put_int(c->local_maxpacket);
0b242b12 2249}
2250void
2251channel_open(int id)
2252{
2253 /* XXX REMOVE ME */
2254 channel_start_open(id);
7e7327a1 2255 packet_send();
7e7327a1 2256}
2257void
2258channel_request(int id, char *service, int wantconfirm)
2259{
2260 channel_request_start(id, service, wantconfirm);
2261 packet_send();
2262 debug("channel request %d: %s", id, service) ;
2263}
2264void
2265channel_request_start(int id, char *service, int wantconfirm)
2266{
2267 Channel *c = channel_lookup(id);
2268 if (c == NULL) {
2269 log("channel_request: %d: bad id", id);
2270 return;
2271 }
2272 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2273 packet_put_int(c->remote_id);
2274 packet_put_cstring(service);
2275 packet_put_char(wantconfirm);
2276}
2277void
2278channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2279{
2280 Channel *c = channel_lookup(id);
2281 if (c == NULL) {
2282 log("channel_register_callback: %d: bad id", id);
2283 return;
2284 }
2285 c->cb_event = mtype;
2286 c->cb_fn = fn;
2287 c->cb_arg = arg;
2288}
2289void
2290channel_register_cleanup(int id, channel_callback_fn *fn)
2291{
2292 Channel *c = channel_lookup(id);
2293 if (c == NULL) {
2294 log("channel_register_cleanup: %d: bad id", id);
2295 return;
2296 }
2297 c->dettach_user = fn;
2298}
2299void
2300channel_cancel_cleanup(int id)
2301{
2302 Channel *c = channel_lookup(id);
2303 if (c == NULL) {
2304 log("channel_cancel_cleanup: %d: bad id", id);
2305 return;
2306 }
2307 c->dettach_user = NULL;
2308}
2309
2310void
2311channel_set_fds(int id, int rfd, int wfd, int efd, int extusage)
2312{
2313 Channel *c = channel_lookup(id);
2314 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2315 fatal("channel_activate for non-larval channel %d.", id);
0fbe8c74 2316
2317 channel_register_fds(c, rfd, wfd, efd, extusage);
7e7327a1 2318 c->type = SSH_CHANNEL_OPEN;
7e7327a1 2319 /* XXX window size? */
2320 c->local_window = c->local_window_max = c->local_maxpacket/2;
2321 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2322 packet_put_int(c->remote_id);
2323 packet_put_int(c->local_window);
2324 packet_send();
2325}
This page took 0.437218 seconds and 5 git commands to generate.