2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * This file contains functions for generic socket connection forwarding.
6 * There is also code for initiating connection forwarding for X11 connections,
7 * arbitrary tcp/ip connections, and the authentication agent connection.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
15 * SSH2 support added by Markus Friedl.
16 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
17 * Copyright (c) 1999 Dug Song. All rights reserved.
18 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 RCSID("$OpenBSD: channels.c,v 1.183 2002/09/17 07:47:02 itojun Exp $");
43 RCSID("$FreeBSD: src/crypto/openssh/channels.c,v 1.1.1.1.2.8 2003/02/03 17:31:06 des Exp $");
44 RCSID("$DragonFly: src/crypto/openssh/Attic/channels.c,v 1.3 2003/09/17 02:01:05 dillon Exp $");
58 #include "pathnames.h"
64 * Pointer to an array containing all allocated channels. The array is
65 * dynamically extended as needed.
67 static Channel **channels = NULL;
70 * Size of the channel array. All slots of the array must always be
71 * initialized (at least the type field); unused slots set to NULL
73 static int channels_alloc = 0;
76 * Maximum file descriptor value used in any of the channels. This is
77 * updated in channel_new.
79 static int channel_max_fd = 0;
82 /* -- tcp forwarding */
85 * Data structure for storing which hosts are permitted for forward requests.
86 * The local sides of any remote forwards are stored in this array to prevent
87 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
88 * network (which might be behind a firewall).
91 char *host_to_connect; /* Connect to 'host'. */
92 u_short port_to_connect; /* Connect to 'port'. */
93 u_short listen_port; /* Remote side should listen port number. */
96 /* List of all permitted host/port pairs to connect. */
97 static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
99 /* Number of permitted host/port pairs in the array. */
100 static int num_permitted_opens = 0;
102 * If this is true, all opens are permitted. This is the case on the server
103 * on which we have to trust the client anyway, and the user could do
104 * anything after logging in anyway.
106 static int all_opens_permitted = 0;
109 /* -- X11 forwarding */
111 /* Maximum number of fake X11 displays to try. */
112 #define MAX_DISPLAYS 1000
114 /* Saved X11 authentication protocol name. */
115 static char *x11_saved_proto = NULL;
117 /* Saved X11 authentication data. This is the real data. */
118 static char *x11_saved_data = NULL;
119 static u_int x11_saved_data_len = 0;
122 * Fake X11 authentication data. This is what the server will be sending us;
123 * we should replace any occurrences of this by the real data.
125 static char *x11_fake_data = NULL;
126 static u_int x11_fake_data_len;
129 /* -- agent forwarding */
133 /* AF_UNSPEC or AF_INET or AF_INET6 */
134 static int IPv4or6 = AF_UNSPEC;
137 static void port_open_helper(Channel *c, char *rtype);
139 /* -- channel core */
142 channel_lookup(int id)
146 if (id < 0 || id >= channels_alloc) {
147 log("channel_lookup: %d: bad id", id);
152 log("channel_lookup: %d: bad id: channel free", id);
159 * Register filedescriptors for a channel, used when allocating a channel or
160 * when the channel consumer/producer is ready, e.g. shell exec'd
164 channel_register_fds(Channel *c, int rfd, int wfd, int efd,
165 int extusage, int nonblock)
167 /* Update the maximum file descriptor value. */
168 channel_max_fd = MAX(channel_max_fd, rfd);
169 channel_max_fd = MAX(channel_max_fd, wfd);
170 channel_max_fd = MAX(channel_max_fd, efd);
172 /* XXX set close-on-exec -markus */
176 c->sock = (rfd == wfd) ? rfd : -1;
178 c->extended_usage = extusage;
180 /* XXX ugly hack: nonblock is only set by the server */
181 if (nonblock && isatty(c->rfd)) {
182 debug("channel %d: rfd %d isatty", c->self, c->rfd);
184 if (!isatty(c->wfd)) {
185 error("channel %d: wfd %d is not a tty?",
191 c->wfd_isatty = isatty(c->wfd);
193 /* enable nonblocking mode */
205 * Allocate a new channel object and set its type and socket. This will cause
206 * remote_name to be freed.
210 channel_new(char *ctype, int type, int rfd, int wfd, int efd,
211 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
216 /* Do initial allocation if this is the first call. */
217 if (channels_alloc == 0) {
219 channels = xmalloc(channels_alloc * sizeof(Channel *));
220 for (i = 0; i < channels_alloc; i++)
222 fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
224 /* Try to find a free slot where to put the new channel. */
225 for (found = -1, i = 0; i < channels_alloc; i++)
226 if (channels[i] == NULL) {
227 /* Found a free slot. */
232 /* There are no free slots. Take last+1 slot and expand the array. */
233 found = channels_alloc;
234 if (channels_alloc > 10000)
235 fatal("channel_new: internal error: channels_alloc %d "
236 "too big.", channels_alloc);
237 channels = xrealloc(channels,
238 (channels_alloc + 10) * sizeof(Channel *));
239 channels_alloc += 10;
240 debug2("channel: expanding %d", channels_alloc);
241 for (i = found; i < channels_alloc; i++)
244 /* Initialize and return new channel. */
245 c = channels[found] = xmalloc(sizeof(Channel));
246 memset(c, 0, sizeof(Channel));
247 buffer_init(&c->input);
248 buffer_init(&c->output);
249 buffer_init(&c->extended);
250 c->ostate = CHAN_OUTPUT_OPEN;
251 c->istate = CHAN_INPUT_OPEN;
253 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
257 c->local_window = window;
258 c->local_window_max = window;
259 c->local_consumed = 0;
260 c->local_maxpacket = maxpack;
262 c->remote_name = remote_name;
263 c->remote_window = 0;
264 c->remote_maxpacket = 0;
266 c->single_connection = 0;
267 c->detach_user = NULL;
269 c->input_filter = NULL;
270 debug("channel %d: new [%s]", found, remote_name);
275 channel_find_maxfd(void)
280 for (i = 0; i < channels_alloc; i++) {
283 max = MAX(max, c->rfd);
284 max = MAX(max, c->wfd);
285 max = MAX(max, c->efd);
292 channel_close_fd(int *fdp)
294 int ret = 0, fd = *fdp;
299 if (fd == channel_max_fd)
300 channel_max_fd = channel_find_maxfd();
305 /* Close all channel fd/socket. */
308 channel_close_fds(Channel *c)
310 debug3("channel_close_fds: channel %d: r %d w %d e %d",
311 c->self, c->rfd, c->wfd, c->efd);
313 channel_close_fd(&c->sock);
314 channel_close_fd(&c->rfd);
315 channel_close_fd(&c->wfd);
316 channel_close_fd(&c->efd);
319 /* Free the channel and close its fd/socket. */
322 channel_free(Channel *c)
327 for (n = 0, i = 0; i < channels_alloc; i++)
330 debug("channel_free: channel %d: %s, nchannels %d", c->self,
331 c->remote_name ? c->remote_name : "???", n);
333 s = channel_open_message();
334 debug3("channel_free: status: %s", s);
338 shutdown(c->sock, SHUT_RDWR);
339 channel_close_fds(c);
340 buffer_free(&c->input);
341 buffer_free(&c->output);
342 buffer_free(&c->extended);
343 if (c->remote_name) {
344 xfree(c->remote_name);
345 c->remote_name = NULL;
347 channels[c->self] = NULL;
352 channel_free_all(void)
356 for (i = 0; i < channels_alloc; i++)
357 if (channels[i] != NULL)
358 channel_free(channels[i]);
362 * Closes the sockets/fds of all channels. This is used to close extra file
363 * descriptors after a fork.
367 channel_close_all(void)
371 for (i = 0; i < channels_alloc; i++)
372 if (channels[i] != NULL)
373 channel_close_fds(channels[i]);
377 * Stop listening to channels.
381 channel_stop_listening(void)
386 for (i = 0; i < channels_alloc; i++) {
390 case SSH_CHANNEL_AUTH_SOCKET:
391 case SSH_CHANNEL_PORT_LISTENER:
392 case SSH_CHANNEL_RPORT_LISTENER:
393 case SSH_CHANNEL_X11_LISTENER:
394 channel_close_fd(&c->sock);
403 * Returns true if no channel has too much buffered data, and false if one or
404 * more channel is overfull.
408 channel_not_very_much_buffered_data(void)
413 for (i = 0; i < channels_alloc; i++) {
415 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
418 buffer_len(&c->input) > packet_get_maxsize()) {
419 debug("channel %d: big input buffer %d",
420 c->self, buffer_len(&c->input));
424 if (buffer_len(&c->output) > packet_get_maxsize()) {
425 debug("channel %d: big output buffer %d > %d",
426 c->self, buffer_len(&c->output),
427 packet_get_maxsize());
435 /* Returns true if any channel is still open. */
438 channel_still_open(void)
443 for (i = 0; i < channels_alloc; i++) {
448 case SSH_CHANNEL_X11_LISTENER:
449 case SSH_CHANNEL_PORT_LISTENER:
450 case SSH_CHANNEL_RPORT_LISTENER:
451 case SSH_CHANNEL_CLOSED:
452 case SSH_CHANNEL_AUTH_SOCKET:
453 case SSH_CHANNEL_DYNAMIC:
454 case SSH_CHANNEL_CONNECTING:
455 case SSH_CHANNEL_ZOMBIE:
457 case SSH_CHANNEL_LARVAL:
459 fatal("cannot happen: SSH_CHANNEL_LARVAL");
461 case SSH_CHANNEL_OPENING:
462 case SSH_CHANNEL_OPEN:
463 case SSH_CHANNEL_X11_OPEN:
465 case SSH_CHANNEL_INPUT_DRAINING:
466 case SSH_CHANNEL_OUTPUT_DRAINING:
468 fatal("cannot happen: OUT_DRAIN");
471 fatal("channel_still_open: bad channel type %d", c->type);
478 /* Returns the id of an open channel suitable for keepaliving */
481 channel_find_open(void)
486 for (i = 0; i < channels_alloc; i++) {
491 case SSH_CHANNEL_CLOSED:
492 case SSH_CHANNEL_DYNAMIC:
493 case SSH_CHANNEL_X11_LISTENER:
494 case SSH_CHANNEL_PORT_LISTENER:
495 case SSH_CHANNEL_RPORT_LISTENER:
496 case SSH_CHANNEL_OPENING:
497 case SSH_CHANNEL_CONNECTING:
498 case SSH_CHANNEL_ZOMBIE:
500 case SSH_CHANNEL_LARVAL:
501 case SSH_CHANNEL_AUTH_SOCKET:
502 case SSH_CHANNEL_OPEN:
503 case SSH_CHANNEL_X11_OPEN:
505 case SSH_CHANNEL_INPUT_DRAINING:
506 case SSH_CHANNEL_OUTPUT_DRAINING:
508 fatal("cannot happen: OUT_DRAIN");
511 fatal("channel_find_open: bad channel type %d", c->type);
520 * Returns a message describing the currently open forwarded connections,
521 * suitable for sending to the client. The message contains crlf pairs for
526 channel_open_message(void)
533 buffer_init(&buffer);
534 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
535 buffer_append(&buffer, buf, strlen(buf));
536 for (i = 0; i < channels_alloc; i++) {
541 case SSH_CHANNEL_X11_LISTENER:
542 case SSH_CHANNEL_PORT_LISTENER:
543 case SSH_CHANNEL_RPORT_LISTENER:
544 case SSH_CHANNEL_CLOSED:
545 case SSH_CHANNEL_AUTH_SOCKET:
546 case SSH_CHANNEL_ZOMBIE:
548 case SSH_CHANNEL_LARVAL:
549 case SSH_CHANNEL_OPENING:
550 case SSH_CHANNEL_CONNECTING:
551 case SSH_CHANNEL_DYNAMIC:
552 case SSH_CHANNEL_OPEN:
553 case SSH_CHANNEL_X11_OPEN:
554 case SSH_CHANNEL_INPUT_DRAINING:
555 case SSH_CHANNEL_OUTPUT_DRAINING:
556 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
557 c->self, c->remote_name,
558 c->type, c->remote_id,
559 c->istate, buffer_len(&c->input),
560 c->ostate, buffer_len(&c->output),
562 buffer_append(&buffer, buf, strlen(buf));
565 fatal("channel_open_message: bad channel type %d", c->type);
569 buffer_append(&buffer, "\0", 1);
570 cp = xstrdup(buffer_ptr(&buffer));
571 buffer_free(&buffer);
576 channel_send_open(int id)
578 Channel *c = channel_lookup(id);
581 log("channel_send_open: %d: bad id", id);
584 debug("send channel open %d", id);
585 packet_start(SSH2_MSG_CHANNEL_OPEN);
586 packet_put_cstring(c->ctype);
587 packet_put_int(c->self);
588 packet_put_int(c->local_window);
589 packet_put_int(c->local_maxpacket);
594 channel_request_start(int local_id, char *service, int wantconfirm)
596 Channel *c = channel_lookup(local_id);
599 log("channel_request_start: %d: unknown channel id", local_id);
602 debug("channel request %d: %s", local_id, service) ;
603 packet_start(SSH2_MSG_CHANNEL_REQUEST);
604 packet_put_int(c->remote_id);
605 packet_put_cstring(service);
606 packet_put_char(wantconfirm);
609 channel_register_confirm(int id, channel_callback_fn *fn)
611 Channel *c = channel_lookup(id);
614 log("channel_register_comfirm: %d: bad id", id);
620 channel_register_cleanup(int id, channel_callback_fn *fn)
622 Channel *c = channel_lookup(id);
625 log("channel_register_cleanup: %d: bad id", id);
631 channel_cancel_cleanup(int id)
633 Channel *c = channel_lookup(id);
636 log("channel_cancel_cleanup: %d: bad id", id);
639 c->detach_user = NULL;
642 channel_register_filter(int id, channel_filter_fn *fn)
644 Channel *c = channel_lookup(id);
647 log("channel_register_filter: %d: bad id", id);
650 c->input_filter = fn;
654 channel_set_fds(int id, int rfd, int wfd, int efd,
655 int extusage, int nonblock, u_int window_max)
657 Channel *c = channel_lookup(id);
659 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
660 fatal("channel_activate for non-larval channel %d.", id);
661 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
662 c->type = SSH_CHANNEL_OPEN;
663 c->local_window = c->local_window_max = window_max;
664 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
665 packet_put_int(c->remote_id);
666 packet_put_int(c->local_window);
671 * 'channel_pre*' are called just before select() to add any bits relevant to
672 * channels in the select bitmasks.
675 * 'channel_post*': perform any appropriate operations for channels which
676 * have events pending.
678 typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
679 chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
680 chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
683 channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
685 FD_SET(c->sock, readset);
689 channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
691 debug3("channel %d: waiting for connection", c->self);
692 FD_SET(c->sock, writeset);
696 channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
698 if (buffer_len(&c->input) < packet_get_maxsize())
699 FD_SET(c->sock, readset);
700 if (buffer_len(&c->output) > 0)
701 FD_SET(c->sock, writeset);
705 channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset)
707 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
709 if (c->istate == CHAN_INPUT_OPEN &&
711 buffer_len(&c->input) < limit)
712 FD_SET(c->rfd, readset);
713 if (c->ostate == CHAN_OUTPUT_OPEN ||
714 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
715 if (buffer_len(&c->output) > 0) {
716 FD_SET(c->wfd, writeset);
717 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
718 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
719 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
720 c->self, c->efd, buffer_len(&c->extended));
725 /** XXX check close conditions, too */
726 if (compat20 && c->efd != -1) {
727 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
728 buffer_len(&c->extended) > 0)
729 FD_SET(c->efd, writeset);
730 else if (!(c->flags & CHAN_EOF_SENT) &&
731 c->extended_usage == CHAN_EXTENDED_READ &&
732 buffer_len(&c->extended) < c->remote_window)
733 FD_SET(c->efd, readset);
738 channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
740 if (buffer_len(&c->input) == 0) {
741 packet_start(SSH_MSG_CHANNEL_CLOSE);
742 packet_put_int(c->remote_id);
744 c->type = SSH_CHANNEL_CLOSED;
745 debug("channel %d: closing after input drain.", c->self);
750 channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
752 if (buffer_len(&c->output) == 0)
755 FD_SET(c->sock, writeset);
759 * This is a special state for X11 authentication spoofing. An opened X11
760 * connection (when authentication spoofing is being done) remains in this
761 * state until the first packet has been completely read. The authentication
762 * data in that packet is then substituted by the real data if it matches the
763 * fake data, and the channel is put into normal mode.
764 * XXX All this happens at the client side.
765 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
768 x11_open_helper(Buffer *b)
771 u_int proto_len, data_len;
773 /* Check if the fixed size part of the packet is in buffer. */
774 if (buffer_len(b) < 12)
777 /* Parse the lengths of variable-length fields. */
779 if (ucp[0] == 0x42) { /* Byte order MSB first. */
780 proto_len = 256 * ucp[6] + ucp[7];
781 data_len = 256 * ucp[8] + ucp[9];
782 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
783 proto_len = ucp[6] + 256 * ucp[7];
784 data_len = ucp[8] + 256 * ucp[9];
786 debug("Initial X11 packet contains bad byte order byte: 0x%x",
791 /* Check if the whole packet is in buffer. */
793 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
796 /* Check if authentication protocol matches. */
797 if (proto_len != strlen(x11_saved_proto) ||
798 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
799 debug("X11 connection uses different authentication protocol.");
802 /* Check if authentication data matches our fake data. */
803 if (data_len != x11_fake_data_len ||
804 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
805 x11_fake_data, x11_fake_data_len) != 0) {
806 debug("X11 auth data does not match fake data.");
809 /* Check fake data length */
810 if (x11_fake_data_len != x11_saved_data_len) {
811 error("X11 fake_data_len %d != saved_data_len %d",
812 x11_fake_data_len, x11_saved_data_len);
816 * Received authentication protocol and data match
817 * our fake data. Substitute the fake data with real
820 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
821 x11_saved_data, x11_saved_data_len);
826 channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
828 int ret = x11_open_helper(&c->output);
831 /* Start normal processing for the channel. */
832 c->type = SSH_CHANNEL_OPEN;
833 channel_pre_open_13(c, readset, writeset);
834 } else if (ret == -1) {
836 * We have received an X11 connection that has bad
837 * authentication information.
839 log("X11 connection rejected because of wrong authentication.");
840 buffer_clear(&c->input);
841 buffer_clear(&c->output);
842 channel_close_fd(&c->sock);
844 c->type = SSH_CHANNEL_CLOSED;
845 packet_start(SSH_MSG_CHANNEL_CLOSE);
846 packet_put_int(c->remote_id);
852 channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
854 int ret = x11_open_helper(&c->output);
856 /* c->force_drain = 1; */
859 c->type = SSH_CHANNEL_OPEN;
860 channel_pre_open(c, readset, writeset);
861 } else if (ret == -1) {
862 log("X11 connection rejected because of wrong authentication.");
863 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
865 buffer_clear(&c->input);
867 buffer_clear(&c->output);
868 /* for proto v1, the peer will send an IEOF */
870 chan_write_failed(c);
872 c->type = SSH_CHANNEL_OPEN;
873 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
877 /* try to decode a socks4 header */
879 channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
882 int len, have, i, found;
888 struct in_addr dest_addr;
891 debug2("channel %d: decode socks4", c->self);
893 have = buffer_len(&c->input);
894 len = sizeof(s4_req);
897 p = buffer_ptr(&c->input);
898 for (found = 0, i = len; i < have; i++) {
904 /* the peer is probably sending garbage */
905 debug("channel %d: decode socks4: too long",
912 buffer_get(&c->input, (char *)&s4_req.version, 1);
913 buffer_get(&c->input, (char *)&s4_req.command, 1);
914 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
915 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
916 have = buffer_len(&c->input);
917 p = buffer_ptr(&c->input);
919 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
921 fatal("channel %d: decode socks4: len %d > have %d",
923 strlcpy(username, p, sizeof(username));
924 buffer_consume(&c->input, len);
925 buffer_consume(&c->input, 1); /* trailing '\0' */
927 host = inet_ntoa(s4_req.dest_addr);
928 strlcpy(c->path, host, sizeof(c->path));
929 c->host_port = ntohs(s4_req.dest_port);
931 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
932 c->self, host, c->host_port, s4_req.command);
934 if (s4_req.command != 1) {
935 debug("channel %d: cannot handle: socks4 cn %d",
936 c->self, s4_req.command);
939 s4_rsp.version = 0; /* vn: 0 for reply */
940 s4_rsp.command = 90; /* cd: req granted */
941 s4_rsp.dest_port = 0; /* ignored */
942 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
943 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
947 /* dynamic port forwarding */
949 channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
954 have = buffer_len(&c->input);
956 debug2("channel %d: pre_dynamic: have %d", c->self, have);
957 /* buffer_dump(&c->input); */
958 /* check if the fixed size part of the packet is in buffer. */
961 FD_SET(c->sock, readset);
964 /* try to guess the protocol */
965 p = buffer_ptr(&c->input);
968 ret = channel_decode_socks4(c, readset, writeset);
976 } else if (ret == 0) {
977 debug2("channel %d: pre_dynamic: need more", c->self);
979 FD_SET(c->sock, readset);
981 /* switch to the next state */
982 c->type = SSH_CHANNEL_OPENING;
983 port_open_helper(c, "direct-tcpip");
987 /* This is our fake X11 server socket. */
989 channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
992 struct sockaddr addr;
995 char buf[16384], *remote_ipaddr;
998 if (FD_ISSET(c->sock, readset)) {
999 debug("X11 connection requested.");
1000 addrlen = sizeof(addr);
1001 newsock = accept(c->sock, &addr, &addrlen);
1002 if (c->single_connection) {
1003 debug("single_connection: closing X11 listener.");
1004 channel_close_fd(&c->sock);
1008 error("accept: %.100s", strerror(errno));
1011 set_nodelay(newsock);
1012 remote_ipaddr = get_peer_ipaddr(newsock);
1013 remote_port = get_peer_port(newsock);
1014 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1015 remote_ipaddr, remote_port);
1017 nc = channel_new("accepted x11 socket",
1018 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1019 c->local_window_max, c->local_maxpacket,
1020 0, xstrdup(buf), 1);
1022 packet_start(SSH2_MSG_CHANNEL_OPEN);
1023 packet_put_cstring("x11");
1024 packet_put_int(nc->self);
1025 packet_put_int(nc->local_window_max);
1026 packet_put_int(nc->local_maxpacket);
1027 /* originator ipaddr and port */
1028 packet_put_cstring(remote_ipaddr);
1029 if (datafellows & SSH_BUG_X11FWD) {
1030 debug("ssh2 x11 bug compat mode");
1032 packet_put_int(remote_port);
1036 packet_start(SSH_SMSG_X11_OPEN);
1037 packet_put_int(nc->self);
1038 if (packet_get_protocol_flags() &
1039 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1040 packet_put_cstring(buf);
1043 xfree(remote_ipaddr);
1048 port_open_helper(Channel *c, char *rtype)
1052 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1053 u_short remote_port = get_peer_port(c->sock);
1055 direct = (strcmp(rtype, "direct-tcpip") == 0);
1057 snprintf(buf, sizeof buf,
1058 "%s: listening port %d for %.100s port %d, "
1059 "connect from %.200s port %d",
1060 rtype, c->listening_port, c->path, c->host_port,
1061 remote_ipaddr, remote_port);
1063 xfree(c->remote_name);
1064 c->remote_name = xstrdup(buf);
1067 packet_start(SSH2_MSG_CHANNEL_OPEN);
1068 packet_put_cstring(rtype);
1069 packet_put_int(c->self);
1070 packet_put_int(c->local_window_max);
1071 packet_put_int(c->local_maxpacket);
1073 /* target host, port */
1074 packet_put_cstring(c->path);
1075 packet_put_int(c->host_port);
1077 /* listen address, port */
1078 packet_put_cstring(c->path);
1079 packet_put_int(c->listening_port);
1081 /* originator host and port */
1082 packet_put_cstring(remote_ipaddr);
1083 packet_put_int(remote_port);
1086 packet_start(SSH_MSG_PORT_OPEN);
1087 packet_put_int(c->self);
1088 packet_put_cstring(c->path);
1089 packet_put_int(c->host_port);
1090 if (packet_get_protocol_flags() &
1091 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1092 packet_put_cstring(c->remote_name);
1095 xfree(remote_ipaddr);
1099 * This socket is listening for connections to a forwarded TCP/IP port.
1102 channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1105 struct sockaddr addr;
1106 int newsock, nextstate;
1110 if (FD_ISSET(c->sock, readset)) {
1111 debug("Connection to port %d forwarding "
1112 "to %.100s port %d requested.",
1113 c->listening_port, c->path, c->host_port);
1115 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1116 nextstate = SSH_CHANNEL_OPENING;
1117 rtype = "forwarded-tcpip";
1119 if (c->host_port == 0) {
1120 nextstate = SSH_CHANNEL_DYNAMIC;
1121 rtype = "dynamic-tcpip";
1123 nextstate = SSH_CHANNEL_OPENING;
1124 rtype = "direct-tcpip";
1128 addrlen = sizeof(addr);
1129 newsock = accept(c->sock, &addr, &addrlen);
1131 error("accept: %.100s", strerror(errno));
1134 set_nodelay(newsock);
1135 nc = channel_new(rtype,
1136 nextstate, newsock, newsock, -1,
1137 c->local_window_max, c->local_maxpacket,
1138 0, xstrdup(rtype), 1);
1139 nc->listening_port = c->listening_port;
1140 nc->host_port = c->host_port;
1141 strlcpy(nc->path, c->path, sizeof(nc->path));
1143 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1145 * do not call the channel_post handler until
1146 * this flag has been reset by a pre-handler.
1147 * otherwise the FD_ISSET calls might overflow
1151 port_open_helper(nc, rtype);
1157 * This is the authentication agent socket listening for connections from
1161 channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1166 struct sockaddr addr;
1169 if (FD_ISSET(c->sock, readset)) {
1170 addrlen = sizeof(addr);
1171 newsock = accept(c->sock, &addr, &addrlen);
1173 error("accept from auth socket: %.100s", strerror(errno));
1176 name = xstrdup("accepted auth socket");
1177 nc = channel_new("accepted auth socket",
1178 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1179 c->local_window_max, c->local_maxpacket,
1182 packet_start(SSH2_MSG_CHANNEL_OPEN);
1183 packet_put_cstring("auth-agent@openssh.com");
1184 packet_put_int(nc->self);
1185 packet_put_int(c->local_window_max);
1186 packet_put_int(c->local_maxpacket);
1188 packet_start(SSH_SMSG_AGENT_OPEN);
1189 packet_put_int(nc->self);
1196 channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1199 socklen_t sz = sizeof(err);
1201 if (FD_ISSET(c->sock, writeset)) {
1202 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1204 error("getsockopt SO_ERROR failed");
1207 debug("channel %d: connected", c->self);
1208 c->type = SSH_CHANNEL_OPEN;
1210 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1211 packet_put_int(c->remote_id);
1212 packet_put_int(c->self);
1213 packet_put_int(c->local_window);
1214 packet_put_int(c->local_maxpacket);
1216 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1217 packet_put_int(c->remote_id);
1218 packet_put_int(c->self);
1221 debug("channel %d: not connected: %s",
1222 c->self, strerror(err));
1224 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1225 packet_put_int(c->remote_id);
1226 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1227 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1228 packet_put_cstring(strerror(err));
1229 packet_put_cstring("");
1232 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1233 packet_put_int(c->remote_id);
1242 channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1248 FD_ISSET(c->rfd, readset)) {
1249 len = read(c->rfd, buf, sizeof(buf));
1250 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1253 debug("channel %d: read<=0 rfd %d len %d",
1254 c->self, c->rfd, len);
1255 if (c->type != SSH_CHANNEL_OPEN) {
1256 debug("channel %d: not open", c->self);
1259 } else if (compat13) {
1260 buffer_clear(&c->output);
1261 c->type = SSH_CHANNEL_INPUT_DRAINING;
1262 debug("channel %d: input draining.", c->self);
1264 chan_read_failed(c);
1268 if (c->input_filter != NULL) {
1269 if (c->input_filter(c, buf, len) == -1) {
1270 debug("channel %d: filter stops", c->self);
1271 chan_read_failed(c);
1274 buffer_append(&c->input, buf, len);
1280 channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1287 /* Send buffered output data to the socket. */
1289 FD_ISSET(c->wfd, writeset) &&
1290 buffer_len(&c->output) > 0) {
1291 data = buffer_ptr(&c->output);
1292 dlen = buffer_len(&c->output);
1294 /* XXX: Later AIX versions can't push as much data to tty */
1295 if (compat20 && c->wfd_isatty && dlen > 8*1024)
1298 len = write(c->wfd, data, dlen);
1299 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1302 if (c->type != SSH_CHANNEL_OPEN) {
1303 debug("channel %d: not open", c->self);
1306 } else if (compat13) {
1307 buffer_clear(&c->output);
1308 debug("channel %d: input draining.", c->self);
1309 c->type = SSH_CHANNEL_INPUT_DRAINING;
1311 chan_write_failed(c);
1315 if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
1316 if (tcgetattr(c->wfd, &tio) == 0 &&
1317 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1319 * Simulate echo to reduce the impact of
1320 * traffic analysis. We need to match the
1321 * size of a SSH2_MSG_CHANNEL_DATA message
1322 * (4 byte channel id + data)
1324 packet_send_ignore(4 + len);
1328 buffer_consume(&c->output, len);
1329 if (compat20 && len > 0) {
1330 c->local_consumed += len;
1336 channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1341 /** XXX handle drain efd, too */
1343 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1344 FD_ISSET(c->efd, writeset) &&
1345 buffer_len(&c->extended) > 0) {
1346 len = write(c->efd, buffer_ptr(&c->extended),
1347 buffer_len(&c->extended));
1348 debug2("channel %d: written %d to efd %d",
1349 c->self, len, c->efd);
1350 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1353 debug2("channel %d: closing write-efd %d",
1355 channel_close_fd(&c->efd);
1357 buffer_consume(&c->extended, len);
1358 c->local_consumed += len;
1360 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1361 FD_ISSET(c->efd, readset)) {
1362 len = read(c->efd, buf, sizeof(buf));
1363 debug2("channel %d: read %d from efd %d",
1364 c->self, len, c->efd);
1365 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1368 debug2("channel %d: closing read-efd %d",
1370 channel_close_fd(&c->efd);
1372 buffer_append(&c->extended, buf, len);
1379 channel_check_window(Channel *c)
1381 if (c->type == SSH_CHANNEL_OPEN &&
1382 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1383 c->local_window < c->local_window_max/2 &&
1384 c->local_consumed > 0) {
1385 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1386 packet_put_int(c->remote_id);
1387 packet_put_int(c->local_consumed);
1389 debug2("channel %d: window %d sent adjust %d",
1390 c->self, c->local_window,
1392 c->local_window += c->local_consumed;
1393 c->local_consumed = 0;
1399 channel_post_open(Channel *c, fd_set * readset, fd_set * writeset)
1403 channel_handle_rfd(c, readset, writeset);
1404 channel_handle_wfd(c, readset, writeset);
1407 channel_handle_efd(c, readset, writeset);
1408 channel_check_window(c);
1412 channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1416 /* Send buffered output data to the socket. */
1417 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1418 len = write(c->sock, buffer_ptr(&c->output),
1419 buffer_len(&c->output));
1421 buffer_clear(&c->output);
1423 buffer_consume(&c->output, len);
1428 channel_handler_init_20(void)
1430 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1431 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1432 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1433 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1434 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1435 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1436 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1437 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1439 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1440 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1441 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1442 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1443 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1444 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1445 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1449 channel_handler_init_13(void)
1451 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1452 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1453 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1454 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1455 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1456 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1457 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1458 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1459 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1461 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1462 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1463 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1464 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1465 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1466 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1467 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1471 channel_handler_init_15(void)
1473 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1474 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1475 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1476 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1477 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1478 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1479 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1481 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1482 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1483 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1484 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1485 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1486 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1490 channel_handler_init(void)
1494 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1495 channel_pre[i] = NULL;
1496 channel_post[i] = NULL;
1499 channel_handler_init_20();
1501 channel_handler_init_13();
1503 channel_handler_init_15();
1506 /* gc dead channels */
1508 channel_garbage_collect(Channel *c)
1512 if (c->detach_user != NULL) {
1513 if (!chan_is_dead(c, 0))
1515 debug("channel %d: gc: notify user", c->self);
1516 c->detach_user(c->self, NULL);
1517 /* if we still have a callback */
1518 if (c->detach_user != NULL)
1520 debug("channel %d: gc: user detached", c->self);
1522 if (!chan_is_dead(c, 1))
1524 debug("channel %d: garbage collecting", c->self);
1529 channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1531 static int did_init = 0;
1536 channel_handler_init();
1539 for (i = 0; i < channels_alloc; i++) {
1543 if (ftab[c->type] != NULL)
1544 (*ftab[c->type])(c, readset, writeset);
1545 channel_garbage_collect(c);
1550 * Allocate/update select bitmasks and add any bits relevant to channels in
1554 channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1555 int *nallocp, int rekeying)
1560 n = MAX(*maxfdp, channel_max_fd);
1562 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1563 /* perhaps check sz < nalloc/2 and shrink? */
1564 if (*readsetp == NULL || sz > *nallocp) {
1565 *readsetp = xrealloc(*readsetp, sz);
1566 *writesetp = xrealloc(*writesetp, sz);
1570 memset(*readsetp, 0, sz);
1571 memset(*writesetp, 0, sz);
1574 channel_handler(channel_pre, *readsetp, *writesetp);
1578 * After select, perform any appropriate operations for channels which have
1582 channel_after_select(fd_set * readset, fd_set * writeset)
1584 channel_handler(channel_post, readset, writeset);
1588 /* If there is data to send to the connection, enqueue some of it now. */
1591 channel_output_poll(void)
1597 for (i = 0; i < channels_alloc; i++) {
1603 * We are only interested in channels that can have buffered
1607 if (c->type != SSH_CHANNEL_OPEN &&
1608 c->type != SSH_CHANNEL_INPUT_DRAINING)
1611 if (c->type != SSH_CHANNEL_OPEN)
1615 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1616 /* XXX is this true? */
1617 debug3("channel %d: will not send data after close", c->self);
1621 /* Get the amount of buffered data for this channel. */
1622 if ((c->istate == CHAN_INPUT_OPEN ||
1623 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1624 (len = buffer_len(&c->input)) > 0) {
1626 * Send some data for the other side over the secure
1630 if (len > c->remote_window)
1631 len = c->remote_window;
1632 if (len > c->remote_maxpacket)
1633 len = c->remote_maxpacket;
1635 if (packet_is_interactive()) {
1639 /* Keep the packets at reasonable size. */
1640 if (len > packet_get_maxsize()/2)
1641 len = packet_get_maxsize()/2;
1645 packet_start(compat20 ?
1646 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
1647 packet_put_int(c->remote_id);
1648 packet_put_string(buffer_ptr(&c->input), len);
1650 buffer_consume(&c->input, len);
1651 c->remote_window -= len;
1653 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1655 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
1657 * input-buffer is empty and read-socket shutdown:
1658 * tell peer, that we will not send more data: send IEOF.
1659 * hack for extended data: delay EOF if EFD still in use.
1661 if (CHANNEL_EFD_INPUT_ACTIVE(c))
1662 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1663 c->self, c->efd, buffer_len(&c->extended));
1667 /* Send extended data, i.e. stderr */
1669 !(c->flags & CHAN_EOF_SENT) &&
1670 c->remote_window > 0 &&
1671 (len = buffer_len(&c->extended)) > 0 &&
1672 c->extended_usage == CHAN_EXTENDED_READ) {
1673 debug2("channel %d: rwin %u elen %u euse %d",
1674 c->self, c->remote_window, buffer_len(&c->extended),
1676 if (len > c->remote_window)
1677 len = c->remote_window;
1678 if (len > c->remote_maxpacket)
1679 len = c->remote_maxpacket;
1680 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1681 packet_put_int(c->remote_id);
1682 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1683 packet_put_string(buffer_ptr(&c->extended), len);
1685 buffer_consume(&c->extended, len);
1686 c->remote_window -= len;
1687 debug2("channel %d: sent ext data %d", c->self, len);
1693 /* -- protocol input */
1696 channel_input_data(int type, u_int32_t seq, void *ctxt)
1703 /* Get the channel number and verify it. */
1704 id = packet_get_int();
1705 c = channel_lookup(id);
1707 packet_disconnect("Received data for nonexistent channel %d.", id);
1709 /* Ignore any data for non-open channels (might happen on close) */
1710 if (c->type != SSH_CHANNEL_OPEN &&
1711 c->type != SSH_CHANNEL_X11_OPEN)
1714 /* same for protocol 1.5 if output end is no longer open */
1715 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
1719 data = packet_get_string(&data_len);
1722 if (data_len > c->local_maxpacket) {
1723 log("channel %d: rcvd big packet %d, maxpack %d",
1724 c->self, data_len, c->local_maxpacket);
1726 if (data_len > c->local_window) {
1727 log("channel %d: rcvd too much data %d, win %d",
1728 c->self, data_len, c->local_window);
1732 c->local_window -= data_len;
1735 buffer_append(&c->output, data, data_len);
1740 channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1744 u_int data_len, tcode;
1747 /* Get the channel number and verify it. */
1748 id = packet_get_int();
1749 c = channel_lookup(id);
1752 packet_disconnect("Received extended_data for bad channel %d.", id);
1753 if (c->type != SSH_CHANNEL_OPEN) {
1754 log("channel %d: ext data for non open", id);
1757 if (c->flags & CHAN_EOF_RCVD) {
1758 if (datafellows & SSH_BUG_EXTEOF)
1759 debug("channel %d: accepting ext data after eof", id);
1761 packet_disconnect("Received extended_data after EOF "
1762 "on channel %d.", id);
1764 tcode = packet_get_int();
1766 c->extended_usage != CHAN_EXTENDED_WRITE ||
1767 tcode != SSH2_EXTENDED_DATA_STDERR) {
1768 log("channel %d: bad ext data", c->self);
1771 data = packet_get_string(&data_len);
1773 if (data_len > c->local_window) {
1774 log("channel %d: rcvd too much extended_data %d, win %d",
1775 c->self, data_len, c->local_window);
1779 debug2("channel %d: rcvd ext data %d", c->self, data_len);
1780 c->local_window -= data_len;
1781 buffer_append(&c->extended, data, data_len);
1786 channel_input_ieof(int type, u_int32_t seq, void *ctxt)
1791 id = packet_get_int();
1793 c = channel_lookup(id);
1795 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1798 /* XXX force input close */
1799 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
1800 debug("channel %d: FORCE input drain", c->self);
1801 c->istate = CHAN_INPUT_WAIT_DRAIN;
1802 if (buffer_len(&c->input) == 0)
1809 channel_input_close(int type, u_int32_t seq, void *ctxt)
1814 id = packet_get_int();
1816 c = channel_lookup(id);
1818 packet_disconnect("Received close for nonexistent channel %d.", id);
1821 * Send a confirmation that we have closed the channel and no more
1822 * data is coming for it.
1824 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
1825 packet_put_int(c->remote_id);
1829 * If the channel is in closed state, we have sent a close request,
1830 * and the other side will eventually respond with a confirmation.
1831 * Thus, we cannot free the channel here, because then there would be
1832 * no-one to receive the confirmation. The channel gets freed when
1833 * the confirmation arrives.
1835 if (c->type != SSH_CHANNEL_CLOSED) {
1837 * Not a closed channel - mark it as draining, which will
1838 * cause it to be freed later.
1840 buffer_clear(&c->input);
1841 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
1845 /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1847 channel_input_oclose(int type, u_int32_t seq, void *ctxt)
1849 int id = packet_get_int();
1850 Channel *c = channel_lookup(id);
1854 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1855 chan_rcvd_oclose(c);
1859 channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
1861 int id = packet_get_int();
1862 Channel *c = channel_lookup(id);
1866 packet_disconnect("Received close confirmation for "
1867 "out-of-range channel %d.", id);
1868 if (c->type != SSH_CHANNEL_CLOSED)
1869 packet_disconnect("Received close confirmation for "
1870 "non-closed channel %d (type %d).", id, c->type);
1875 channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
1880 id = packet_get_int();
1881 c = channel_lookup(id);
1883 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1884 packet_disconnect("Received open confirmation for "
1885 "non-opening channel %d.", id);
1886 remote_id = packet_get_int();
1887 /* Record the remote channel number and mark that the channel is now open. */
1888 c->remote_id = remote_id;
1889 c->type = SSH_CHANNEL_OPEN;
1892 c->remote_window = packet_get_int();
1893 c->remote_maxpacket = packet_get_int();
1895 debug2("callback start");
1896 c->confirm(c->self, NULL);
1897 debug2("callback done");
1899 debug("channel %d: open confirm rwindow %u rmax %u", c->self,
1900 c->remote_window, c->remote_maxpacket);
1906 reason2txt(int reason)
1909 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1910 return "administratively prohibited";
1911 case SSH2_OPEN_CONNECT_FAILED:
1912 return "connect failed";
1913 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1914 return "unknown channel type";
1915 case SSH2_OPEN_RESOURCE_SHORTAGE:
1916 return "resource shortage";
1918 return "unknown reason";
1922 channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
1925 char *msg = NULL, *lang = NULL;
1928 id = packet_get_int();
1929 c = channel_lookup(id);
1931 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1932 packet_disconnect("Received open failure for "
1933 "non-opening channel %d.", id);
1935 reason = packet_get_int();
1936 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1937 msg = packet_get_string(NULL);
1938 lang = packet_get_string(NULL);
1940 log("channel %d: open failed: %s%s%s", id,
1941 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
1948 /* Free the channel. This will also close the socket. */
1953 channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
1962 /* Get the channel number and verify it. */
1963 id = packet_get_int();
1964 c = channel_lookup(id);
1966 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1967 log("Received window adjust for "
1968 "non-open channel %d.", id);
1971 adjust = packet_get_int();
1973 debug2("channel %d: rcvd adjust %u", id, adjust);
1974 c->remote_window += adjust;
1978 channel_input_port_open(int type, u_int32_t seq, void *ctxt)
1982 char *host, *originator_string;
1983 int remote_id, sock = -1;
1985 remote_id = packet_get_int();
1986 host = packet_get_string(NULL);
1987 host_port = packet_get_int();
1989 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1990 originator_string = packet_get_string(NULL);
1992 originator_string = xstrdup("unknown (remote did not supply name)");
1995 sock = channel_connect_to(host, host_port);
1997 c = channel_new("connected socket",
1998 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
1999 originator_string, 1);
2000 c->remote_id = remote_id;
2003 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2004 packet_put_int(remote_id);
2011 /* -- tcp forwarding */
2014 channel_set_af(int af)
2020 channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2021 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2024 int success, sock, on = 1;
2025 struct addrinfo hints, *ai, *aitop;
2027 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2030 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2031 listen_addr : host_to_connect;
2034 error("No forward host name.");
2037 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
2038 error("Forward host name too long.");
2043 * getaddrinfo returns a loopback address if the hostname is
2044 * set to NULL and hints.ai_flags is not AI_PASSIVE
2046 memset(&hints, 0, sizeof(hints));
2047 hints.ai_family = IPv4or6;
2048 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2049 hints.ai_socktype = SOCK_STREAM;
2050 snprintf(strport, sizeof strport, "%d", listen_port);
2051 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2052 packet_disconnect("getaddrinfo: fatal error");
2054 for (ai = aitop; ai; ai = ai->ai_next) {
2055 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2057 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2058 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2059 error("channel_setup_fwd_listener: getnameinfo failed");
2062 /* Create a port to listen for the host. */
2063 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2065 /* this is no error since kernel may not support ipv6 */
2066 verbose("socket: %.100s", strerror(errno));
2070 * Set socket options.
2071 * Allow local port reuse in TIME_WAIT.
2073 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
2075 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
2077 debug("Local forwarding listening on %s port %s.", ntop, strport);
2079 /* Bind the socket to the address. */
2080 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2081 /* address can be in use ipv6 address is already bound */
2083 error("bind: %.100s", strerror(errno));
2085 verbose("bind: %.100s", strerror(errno));
2090 /* Start listening for connections on the socket. */
2091 if (listen(sock, 5) < 0) {
2092 error("listen: %.100s", strerror(errno));
2096 /* Allocate a channel number for the socket. */
2097 c = channel_new("port listener", type, sock, sock, -1,
2098 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
2099 0, xstrdup("port listener"), 1);
2100 strlcpy(c->path, host, sizeof(c->path));
2101 c->host_port = port_to_connect;
2102 c->listening_port = listen_port;
2106 error("channel_setup_fwd_listener: cannot listen to port: %d",
2108 freeaddrinfo(aitop);
2112 /* protocol local port fwd, used by ssh (and sshd in v1) */
2114 channel_setup_local_fwd_listener(u_short listen_port,
2115 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2117 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
2118 NULL, listen_port, host_to_connect, port_to_connect, gateway_ports);
2121 /* protocol v2 remote port fwd, used by sshd */
2123 channel_setup_remote_fwd_listener(const char *listen_address,
2124 u_short listen_port, int gateway_ports)
2126 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2127 listen_address, listen_port, NULL, 0, gateway_ports);
2131 * Initiate forwarding of connections to port "port" on remote host through
2132 * the secure channel to host:port from local side.
2136 channel_request_remote_forwarding(u_short listen_port,
2137 const char *host_to_connect, u_short port_to_connect)
2139 int type, success = 0;
2141 /* Record locally that connection to this host/port is permitted. */
2142 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2143 fatal("channel_request_remote_forwarding: too many forwards");
2145 /* Send the forward request to the remote side. */
2147 const char *address_to_bind = "0.0.0.0";
2148 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2149 packet_put_cstring("tcpip-forward");
2150 packet_put_char(1); /* boolean: want reply */
2151 packet_put_cstring(address_to_bind);
2152 packet_put_int(listen_port);
2154 packet_write_wait();
2155 /* Assume that server accepts the request */
2158 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
2159 packet_put_int(listen_port);
2160 packet_put_cstring(host_to_connect);
2161 packet_put_int(port_to_connect);
2163 packet_write_wait();
2165 /* Wait for response from the remote side. */
2166 type = packet_read();
2168 case SSH_SMSG_SUCCESS:
2171 case SSH_SMSG_FAILURE:
2172 log("Warning: Server denied remote port forwarding.");
2175 /* Unknown packet */
2176 packet_disconnect("Protocol error for port forward request:"
2177 "received packet type %d.", type);
2181 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2182 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2183 permitted_opens[num_permitted_opens].listen_port = listen_port;
2184 num_permitted_opens++;
2189 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2190 * listening for the port, and sends back a success reply (or disconnect
2191 * message if there was an error). This never returns if there was an error.
2195 channel_input_port_forward_request(int is_root, int gateway_ports)
2197 u_short port, host_port;
2200 /* Get arguments from the packet. */
2201 port = packet_get_int();
2202 hostname = packet_get_string(NULL);
2203 host_port = packet_get_int();
2207 * Check that an unprivileged user is not trying to forward a
2210 if (port < IPPORT_RESERVED && !is_root)
2211 packet_disconnect("Requested forwarding of port %d but user is not root.",
2214 /* Initiate forwarding */
2215 channel_setup_local_fwd_listener(port, hostname, host_port, gateway_ports);
2217 /* Free the argument string. */
2222 * Permits opening to any host/port if permitted_opens[] is empty. This is
2223 * usually called by the server, because the user could connect to any port
2224 * anyway, and the server has no way to know but to trust the client anyway.
2227 channel_permit_all_opens(void)
2229 if (num_permitted_opens == 0)
2230 all_opens_permitted = 1;
2234 channel_add_permitted_opens(char *host, int port)
2236 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2237 fatal("channel_request_remote_forwarding: too many forwards");
2238 debug("allow port forwarding to host %s port %d", host, port);
2240 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2241 permitted_opens[num_permitted_opens].port_to_connect = port;
2242 num_permitted_opens++;
2244 all_opens_permitted = 0;
2248 channel_clear_permitted_opens(void)
2252 for (i = 0; i < num_permitted_opens; i++)
2253 xfree(permitted_opens[i].host_to_connect);
2254 num_permitted_opens = 0;
2259 /* return socket to remote host, port */
2261 connect_to(const char *host, u_short port)
2263 struct addrinfo hints, *ai, *aitop;
2264 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2268 memset(&hints, 0, sizeof(hints));
2269 hints.ai_family = IPv4or6;
2270 hints.ai_socktype = SOCK_STREAM;
2271 snprintf(strport, sizeof strport, "%d", port);
2272 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
2273 error("connect_to %.100s: unknown host (%s)", host,
2274 gai_strerror(gaierr));
2277 for (ai = aitop; ai; ai = ai->ai_next) {
2278 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2280 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2281 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2282 error("connect_to: getnameinfo failed");
2285 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2287 error("socket: %.100s", strerror(errno));
2290 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
2291 fatal("connect_to: F_SETFL: %s", strerror(errno));
2292 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2293 errno != EINPROGRESS) {
2294 error("connect_to %.100s port %s: %.100s", ntop, strport,
2297 continue; /* fail -- try next */
2299 break; /* success */
2302 freeaddrinfo(aitop);
2304 error("connect_to %.100s port %d: failed.", host, port);
2313 channel_connect_by_listen_address(u_short listen_port)
2317 for (i = 0; i < num_permitted_opens; i++)
2318 if (permitted_opens[i].listen_port == listen_port)
2320 permitted_opens[i].host_to_connect,
2321 permitted_opens[i].port_to_connect);
2322 error("WARNING: Server requests forwarding for unknown listen_port %d",
2327 /* Check if connecting to that port is permitted and connect. */
2329 channel_connect_to(const char *host, u_short port)
2333 permit = all_opens_permitted;
2335 for (i = 0; i < num_permitted_opens; i++)
2336 if (permitted_opens[i].port_to_connect == port &&
2337 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2342 log("Received request to connect to host %.100s port %d, "
2343 "but the request was denied.", host, port);
2346 return connect_to(host, port);
2349 /* -- X11 forwarding */
2352 * Creates an internet domain socket for listening for X11 connections.
2353 * Returns 0 and a suitable display number for the DISPLAY variable
2354 * stored in display_numberp , or -1 if an error occurs.
2357 x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
2358 int single_connection, u_int *display_numberp)
2361 int display_number, sock;
2363 struct addrinfo hints, *ai, *aitop;
2364 char strport[NI_MAXSERV];
2365 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2367 for (display_number = x11_display_offset;
2368 display_number < MAX_DISPLAYS;
2370 port = 6000 + display_number;
2371 memset(&hints, 0, sizeof(hints));
2372 hints.ai_family = IPv4or6;
2373 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
2374 hints.ai_socktype = SOCK_STREAM;
2375 snprintf(strport, sizeof strport, "%d", port);
2376 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2377 error("getaddrinfo: %.100s", gai_strerror(gaierr));
2380 for (ai = aitop; ai; ai = ai->ai_next) {
2381 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2383 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2385 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
2386 error("socket: %.100s", strerror(errno));
2389 debug("x11_create_display_inet: Socket family %d not supported",
2395 if (ai->ai_family == AF_INET6) {
2397 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2398 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2401 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2402 debug("bind port %d: %.100s", port, strerror(errno));
2408 for (n = 0; n < num_socks; n++) {
2414 socks[num_socks++] = sock;
2415 #ifndef DONT_TRY_OTHER_AF
2416 if (num_socks == NUM_SOCKS)
2419 if (x11_use_localhost) {
2420 if (num_socks == NUM_SOCKS)
2427 freeaddrinfo(aitop);
2431 if (display_number >= MAX_DISPLAYS) {
2432 error("Failed to allocate internet-domain X11 display socket.");
2435 /* Start listening for connections on the socket. */
2436 for (n = 0; n < num_socks; n++) {
2438 if (listen(sock, 5) < 0) {
2439 error("listen: %.100s", strerror(errno));
2445 /* Allocate a channel for each socket. */
2446 for (n = 0; n < num_socks; n++) {
2448 nc = channel_new("x11 listener",
2449 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2450 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2451 0, xstrdup("X11 inet listener"), 1);
2452 nc->single_connection = single_connection;
2455 /* Return the display number for the DISPLAY environment variable. */
2456 *display_numberp = display_number;
2461 connect_local_xsocket(u_int dnr)
2464 struct sockaddr_un addr;
2466 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2468 error("socket: %.100s", strerror(errno));
2469 memset(&addr, 0, sizeof(addr));
2470 addr.sun_family = AF_UNIX;
2471 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
2472 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2475 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2480 x11_connect_display(void)
2482 int display_number, sock = 0;
2483 const char *display;
2484 char buf[1024], *cp;
2485 struct addrinfo hints, *ai, *aitop;
2486 char strport[NI_MAXSERV];
2489 /* Try to open a socket for the local X server. */
2490 display = getenv("DISPLAY");
2492 error("DISPLAY not set.");
2496 * Now we decode the value of the DISPLAY variable and make a
2497 * connection to the real X server.
2501 * Check if it is a unix domain socket. Unix domain displays are in
2502 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2504 if (strncmp(display, "unix:", 5) == 0 ||
2505 display[0] == ':') {
2506 /* Connect to the unix domain socket. */
2507 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2508 error("Could not parse display number from DISPLAY: %.100s",
2512 /* Create a socket. */
2513 sock = connect_local_xsocket(display_number);
2517 /* OK, we now have a connection to the display. */
2521 * Connect to an inet socket. The DISPLAY value is supposedly
2522 * hostname:d[.s], where hostname may also be numeric IP address.
2524 strlcpy(buf, display, sizeof(buf));
2525 cp = strchr(buf, ':');
2527 error("Could not find ':' in DISPLAY: %.100s", display);
2531 /* buf now contains the host name. But first we parse the display number. */
2532 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2533 error("Could not parse display number from DISPLAY: %.100s",
2538 /* Look up the host address */
2539 memset(&hints, 0, sizeof(hints));
2540 hints.ai_family = IPv4or6;
2541 hints.ai_socktype = SOCK_STREAM;
2542 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2543 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2544 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
2547 for (ai = aitop; ai; ai = ai->ai_next) {
2548 /* Create a socket. */
2549 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2551 debug("socket: %.100s", strerror(errno));
2554 /* Connect it to the display. */
2555 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2556 debug("connect %.100s port %d: %.100s", buf,
2557 6000 + display_number, strerror(errno));
2564 freeaddrinfo(aitop);
2566 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
2575 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2576 * the remote channel number. We should do whatever we want, and respond
2577 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2581 x11_input_open(int type, u_int32_t seq, void *ctxt)
2584 int remote_id, sock = 0;
2587 debug("Received X11 open request.");
2589 remote_id = packet_get_int();
2591 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2592 remote_host = packet_get_string(NULL);
2594 remote_host = xstrdup("unknown (remote did not supply name)");
2598 /* Obtain a connection to the real X display. */
2599 sock = x11_connect_display();
2601 /* Allocate a channel for this connection. */
2602 c = channel_new("connected x11 socket",
2603 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2605 c->remote_id = remote_id;
2609 /* Send refusal to the remote host. */
2610 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2611 packet_put_int(remote_id);
2613 /* Send a confirmation to the remote host. */
2614 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2615 packet_put_int(remote_id);
2616 packet_put_int(c->self);
2621 /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2623 deny_input_open(int type, u_int32_t seq, void *ctxt)
2625 int rchan = packet_get_int();
2628 case SSH_SMSG_AGENT_OPEN:
2629 error("Warning: ssh server tried agent forwarding.");
2631 case SSH_SMSG_X11_OPEN:
2632 error("Warning: ssh server tried X11 forwarding.");
2635 error("deny_input_open: type %d", type);
2638 error("Warning: this is probably a break in attempt by a malicious server.");
2639 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2640 packet_put_int(rchan);
2645 * Requests forwarding of X11 connections, generates fake authentication
2646 * data, and enables authentication spoofing.
2647 * This should be called in the client only.
2650 x11_request_forwarding_with_spoofing(int client_session_id,
2651 const char *proto, const char *data)
2653 u_int data_len = (u_int) strlen(data) / 2;
2654 u_int i, value, len;
2660 cp = getenv("DISPLAY");
2662 cp = strchr(cp, ':');
2664 cp = strchr(cp, '.');
2666 screen_number = atoi(cp + 1);
2670 /* Save protocol name. */
2671 x11_saved_proto = xstrdup(proto);
2674 * Extract real authentication data and generate fake data of the
2677 x11_saved_data = xmalloc(data_len);
2678 x11_fake_data = xmalloc(data_len);
2679 for (i = 0; i < data_len; i++) {
2680 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2681 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2683 rand = arc4random();
2684 x11_saved_data[i] = value;
2685 x11_fake_data[i] = rand & 0xff;
2688 x11_saved_data_len = data_len;
2689 x11_fake_data_len = data_len;
2691 /* Convert the fake data into hex. */
2692 len = 2 * data_len + 1;
2693 new_data = xmalloc(len);
2694 for (i = 0; i < data_len; i++)
2695 snprintf(new_data + 2 * i, len - 2 * i,
2696 "%02x", (u_char) x11_fake_data[i]);
2698 /* Send the request packet. */
2700 channel_request_start(client_session_id, "x11-req", 0);
2701 packet_put_char(0); /* XXX bool single connection */
2703 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2705 packet_put_cstring(proto);
2706 packet_put_cstring(new_data);
2707 packet_put_int(screen_number);
2709 packet_write_wait();
2714 /* -- agent forwarding */
2716 /* Sends a message to the server to request authentication fd forwarding. */
2719 auth_request_forwarding(void)
2721 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2723 packet_write_wait();
2726 /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2729 auth_input_open_request(int type, u_int32_t seq, void *ctxt)
2732 int remote_id, sock;
2735 /* Read the remote channel number from the message. */
2736 remote_id = packet_get_int();
2740 * Get a connection to the local authentication agent (this may again
2743 sock = ssh_get_authentication_socket();
2746 * If we could not connect the agent, send an error message back to
2747 * the server. This should never happen unless the agent dies,
2748 * because authentication forwarding is only enabled if we have an
2752 name = xstrdup("authentication agent connection");
2753 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2754 -1, 0, 0, 0, name, 1);
2755 c->remote_id = remote_id;
2759 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2760 packet_put_int(remote_id);
2762 /* Send a confirmation to the remote host. */
2763 debug("Forwarding authentication connection.");
2764 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2765 packet_put_int(remote_id);
2766 packet_put_int(c->self);