1 /* $OpenBSD: channels.c,v 1.286 2008/07/16 11:52:19 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
16 * SSH2 support added by Markus Friedl.
17 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
18 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
47 #include <sys/socket.h>
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
64 #include "openbsd-compat/sys-queue.h"
78 #include "pathnames.h"
83 * Pointer to an array containing all allocated channels. The array is
84 * dynamically extended as needed.
86 static Channel **channels = NULL;
89 * Size of the channel array. All slots of the array must always be
90 * initialized (at least the type field); unused slots set to NULL
92 static u_int channels_alloc = 0;
95 * Maximum file descriptor value used in any of the channels. This is
96 * updated in channel_new.
98 static int channel_max_fd = 0;
101 /* -- tcp forwarding */
104 * Data structure for storing which hosts are permitted for forward requests.
105 * The local sides of any remote forwards are stored in this array to prevent
106 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
107 * network (which might be behind a firewall).
110 char *host_to_connect; /* Connect to 'host'. */
111 u_short port_to_connect; /* Connect to 'port'. */
112 u_short listen_port; /* Remote side should listen port number. */
115 /* List of all permitted host/port pairs to connect by the user. */
116 static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
118 /* List of all permitted host/port pairs to connect by the admin. */
119 static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
121 /* Number of permitted host/port pairs in the array permitted by the user. */
122 static int num_permitted_opens = 0;
124 /* Number of permitted host/port pair in the array permitted by the admin. */
125 static int num_adm_permitted_opens = 0;
128 * If this is true, all opens are permitted. This is the case on the server
129 * on which we have to trust the client anyway, and the user could do
130 * anything after logging in anyway.
132 static int all_opens_permitted = 0;
135 /* -- X11 forwarding */
137 /* Maximum number of fake X11 displays to try. */
138 #define MAX_DISPLAYS 1000
140 /* Saved X11 local (client) display. */
141 static char *x11_saved_display = NULL;
143 /* Saved X11 authentication protocol name. */
144 static char *x11_saved_proto = NULL;
146 /* Saved X11 authentication data. This is the real data. */
147 static char *x11_saved_data = NULL;
148 static u_int x11_saved_data_len = 0;
151 * Fake X11 authentication data. This is what the server will be sending us;
152 * we should replace any occurrences of this by the real data.
154 static u_char *x11_fake_data = NULL;
155 static u_int x11_fake_data_len;
158 /* -- agent forwarding */
162 /* AF_UNSPEC or AF_INET or AF_INET6 */
163 static int IPv4or6 = AF_UNSPEC;
166 static void port_open_helper(Channel *c, char *rtype);
168 /* non-blocking connect helpers */
169 static int connect_next(struct channel_connect *);
170 static void channel_connect_ctx_free(struct channel_connect *);
173 static int hpn_disabled = 0;
174 static int hpn_buffer_size = 2 * 1024 * 1024;
176 /* -- channel core */
181 channel_by_id(int id)
185 if (id < 0 || (u_int)id >= channels_alloc) {
186 logit("channel_by_id: %d: bad id", id);
191 logit("channel_by_id: %d: bad id: channel free", id);
198 * Returns the channel if it is allowed to receive protocol messages.
199 * Private channels, like listening sockets, may not receive messages.
202 channel_lookup(int id)
206 if ((c = channel_by_id(id)) == NULL)
210 case SSH_CHANNEL_X11_OPEN:
211 case SSH_CHANNEL_LARVAL:
212 case SSH_CHANNEL_CONNECTING:
213 case SSH_CHANNEL_DYNAMIC:
214 case SSH_CHANNEL_OPENING:
215 case SSH_CHANNEL_OPEN:
216 case SSH_CHANNEL_INPUT_DRAINING:
217 case SSH_CHANNEL_OUTPUT_DRAINING:
220 logit("Non-public channel %d, type %d.", id, c->type);
225 * Register filedescriptors for a channel, used when allocating a channel or
226 * when the channel consumer/producer is ready, e.g. shell exec'd
229 channel_register_fds(Channel *c, int rfd, int wfd, int efd,
230 int extusage, int nonblock, int is_tty)
232 /* Update the maximum file descriptor value. */
233 channel_max_fd = MAX(channel_max_fd, rfd);
234 channel_max_fd = MAX(channel_max_fd, wfd);
235 channel_max_fd = MAX(channel_max_fd, efd);
237 /* XXX set close-on-exec -markus */
241 c->sock = (rfd == wfd) ? rfd : -1;
242 c->ctl_fd = -1; /* XXX: set elsewhere */
244 c->extended_usage = extusage;
246 if ((c->isatty = is_tty) != 0)
247 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
248 c->wfd_isatty = is_tty || isatty(c->wfd);
250 /* enable nonblocking mode */
262 * Allocate a new channel object and set its type and socket. This will cause
263 * remote_name to be freed.
266 channel_new(char *ctype, int type, int rfd, int wfd, int efd,
267 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
273 /* Do initial allocation if this is the first call. */
274 if (channels_alloc == 0) {
276 channels = xcalloc(channels_alloc, sizeof(Channel *));
277 for (i = 0; i < channels_alloc; i++)
280 /* Try to find a free slot where to put the new channel. */
281 for (found = -1, i = 0; i < channels_alloc; i++)
282 if (channels[i] == NULL) {
283 /* Found a free slot. */
288 /* There are no free slots. Take last+1 slot and expand the array. */
289 found = channels_alloc;
290 if (channels_alloc > 10000)
291 fatal("channel_new: internal error: channels_alloc %d "
292 "too big.", channels_alloc);
293 channels = xrealloc(channels, channels_alloc + 10,
295 channels_alloc += 10;
296 debug2("channel: expanding %d", channels_alloc);
297 for (i = found; i < channels_alloc; i++)
300 /* Initialize and return new channel. */
301 c = channels[found] = xcalloc(1, sizeof(Channel));
302 buffer_init(&c->input);
303 buffer_init(&c->output);
304 buffer_init(&c->extended);
305 c->ostate = CHAN_OUTPUT_OPEN;
306 c->istate = CHAN_INPUT_OPEN;
308 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
312 c->local_window = window;
313 c->local_window_max = window;
314 c->local_consumed = 0;
315 c->local_maxpacket = maxpack;
316 c->dynamic_window = 0;
318 c->remote_name = xstrdup(remote_name);
319 c->remote_window = 0;
320 c->remote_maxpacket = 0;
322 c->single_connection = 0;
323 c->detach_user = NULL;
325 c->open_confirm = NULL;
326 c->open_confirm_ctx = NULL;
327 c->input_filter = NULL;
328 c->output_filter = NULL;
329 c->filter_ctx = NULL;
330 c->filter_cleanup = NULL;
331 TAILQ_INIT(&c->status_confirms);
332 debug("channel %d: new [%s]", found, remote_name);
337 channel_find_maxfd(void)
343 for (i = 0; i < channels_alloc; i++) {
346 max = MAX(max, c->rfd);
347 max = MAX(max, c->wfd);
348 max = MAX(max, c->efd);
355 channel_close_fd(int *fdp)
357 int ret = 0, fd = *fdp;
362 if (fd == channel_max_fd)
363 channel_max_fd = channel_find_maxfd();
368 /* Close all channel fd/socket. */
370 channel_close_fds(Channel *c)
372 debug3("channel %d: close_fds r %d w %d e %d c %d",
373 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
375 channel_close_fd(&c->sock);
376 channel_close_fd(&c->ctl_fd);
377 channel_close_fd(&c->rfd);
378 channel_close_fd(&c->wfd);
379 channel_close_fd(&c->efd);
382 /* Free the channel and close its fd/socket. */
384 channel_free(Channel *c)
388 struct channel_confirm *cc;
390 for (n = 0, i = 0; i < channels_alloc; i++)
393 debug("channel %d: free: %s, nchannels %u", c->self,
394 c->remote_name ? c->remote_name : "???", n);
396 s = channel_open_message();
397 debug3("channel %d: status: %s", c->self, s);
401 shutdown(c->sock, SHUT_RDWR);
403 shutdown(c->ctl_fd, SHUT_RDWR);
404 channel_close_fds(c);
405 buffer_free(&c->input);
406 buffer_free(&c->output);
407 buffer_free(&c->extended);
408 if (c->remote_name) {
409 xfree(c->remote_name);
410 c->remote_name = NULL;
412 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
413 if (cc->abandon_cb != NULL)
414 cc->abandon_cb(c, cc->ctx);
415 TAILQ_REMOVE(&c->status_confirms, cc, entry);
416 bzero(cc, sizeof(*cc));
419 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
420 c->filter_cleanup(c->self, c->filter_ctx);
421 channels[c->self] = NULL;
426 channel_free_all(void)
430 for (i = 0; i < channels_alloc; i++)
431 if (channels[i] != NULL)
432 channel_free(channels[i]);
436 * Closes the sockets/fds of all channels. This is used to close extra file
437 * descriptors after a fork.
440 channel_close_all(void)
444 for (i = 0; i < channels_alloc; i++)
445 if (channels[i] != NULL)
446 channel_close_fds(channels[i]);
450 * Stop listening to channels.
453 channel_stop_listening(void)
458 for (i = 0; i < channels_alloc; i++) {
462 case SSH_CHANNEL_AUTH_SOCKET:
463 case SSH_CHANNEL_PORT_LISTENER:
464 case SSH_CHANNEL_RPORT_LISTENER:
465 case SSH_CHANNEL_X11_LISTENER:
466 channel_close_fd(&c->sock);
475 * Returns true if no channel has too much buffered data, and false if one or
476 * more channel is overfull.
479 channel_not_very_much_buffered_data(void)
484 for (i = 0; i < channels_alloc; i++) {
486 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
489 buffer_len(&c->input) > packet_get_maxsize()) {
490 debug2("channel %d: big input buffer %d",
491 c->self, buffer_len(&c->input));
495 if (buffer_len(&c->output) > packet_get_maxsize()) {
496 debug2("channel %d: big output buffer %u > %u",
497 c->self, buffer_len(&c->output),
498 packet_get_maxsize());
506 /* Returns true if any channel is still open. */
508 channel_still_open(void)
513 for (i = 0; i < channels_alloc; i++) {
518 case SSH_CHANNEL_X11_LISTENER:
519 case SSH_CHANNEL_PORT_LISTENER:
520 case SSH_CHANNEL_RPORT_LISTENER:
521 case SSH_CHANNEL_CLOSED:
522 case SSH_CHANNEL_AUTH_SOCKET:
523 case SSH_CHANNEL_DYNAMIC:
524 case SSH_CHANNEL_CONNECTING:
525 case SSH_CHANNEL_ZOMBIE:
527 case SSH_CHANNEL_LARVAL:
529 fatal("cannot happen: SSH_CHANNEL_LARVAL");
531 case SSH_CHANNEL_OPENING:
532 case SSH_CHANNEL_OPEN:
533 case SSH_CHANNEL_X11_OPEN:
535 case SSH_CHANNEL_INPUT_DRAINING:
536 case SSH_CHANNEL_OUTPUT_DRAINING:
538 fatal("cannot happen: OUT_DRAIN");
541 fatal("channel_still_open: bad channel type %d", c->type);
548 /* Returns the id of an open channel suitable for keepaliving */
550 channel_find_open(void)
555 for (i = 0; i < channels_alloc; i++) {
557 if (c == NULL || c->remote_id < 0)
560 case SSH_CHANNEL_CLOSED:
561 case SSH_CHANNEL_DYNAMIC:
562 case SSH_CHANNEL_X11_LISTENER:
563 case SSH_CHANNEL_PORT_LISTENER:
564 case SSH_CHANNEL_RPORT_LISTENER:
565 case SSH_CHANNEL_OPENING:
566 case SSH_CHANNEL_CONNECTING:
567 case SSH_CHANNEL_ZOMBIE:
569 case SSH_CHANNEL_LARVAL:
570 case SSH_CHANNEL_AUTH_SOCKET:
571 case SSH_CHANNEL_OPEN:
572 case SSH_CHANNEL_X11_OPEN:
574 case SSH_CHANNEL_INPUT_DRAINING:
575 case SSH_CHANNEL_OUTPUT_DRAINING:
577 fatal("cannot happen: OUT_DRAIN");
580 fatal("channel_find_open: bad channel type %d", c->type);
589 * Returns a message describing the currently open forwarded connections,
590 * suitable for sending to the client. The message contains crlf pairs for
594 channel_open_message(void)
601 buffer_init(&buffer);
602 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
603 buffer_append(&buffer, buf, strlen(buf));
604 for (i = 0; i < channels_alloc; i++) {
609 case SSH_CHANNEL_X11_LISTENER:
610 case SSH_CHANNEL_PORT_LISTENER:
611 case SSH_CHANNEL_RPORT_LISTENER:
612 case SSH_CHANNEL_CLOSED:
613 case SSH_CHANNEL_AUTH_SOCKET:
614 case SSH_CHANNEL_ZOMBIE:
616 case SSH_CHANNEL_LARVAL:
617 case SSH_CHANNEL_OPENING:
618 case SSH_CHANNEL_CONNECTING:
619 case SSH_CHANNEL_DYNAMIC:
620 case SSH_CHANNEL_OPEN:
621 case SSH_CHANNEL_X11_OPEN:
622 case SSH_CHANNEL_INPUT_DRAINING:
623 case SSH_CHANNEL_OUTPUT_DRAINING:
624 snprintf(buf, sizeof buf,
625 " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
626 c->self, c->remote_name,
627 c->type, c->remote_id,
628 c->istate, buffer_len(&c->input),
629 c->ostate, buffer_len(&c->output),
630 c->rfd, c->wfd, c->ctl_fd);
631 buffer_append(&buffer, buf, strlen(buf));
634 fatal("channel_open_message: bad channel type %d", c->type);
638 buffer_append(&buffer, "\0", 1);
639 cp = xstrdup(buffer_ptr(&buffer));
640 buffer_free(&buffer);
645 channel_send_open(int id)
647 Channel *c = channel_lookup(id);
650 logit("channel_send_open: %d: bad id", id);
653 debug2("channel %d: send open", id);
654 packet_start(SSH2_MSG_CHANNEL_OPEN);
655 packet_put_cstring(c->ctype);
656 packet_put_int(c->self);
657 packet_put_int(c->local_window);
658 packet_put_int(c->local_maxpacket);
663 channel_request_start(int id, char *service, int wantconfirm)
665 Channel *c = channel_lookup(id);
668 logit("channel_request_start: %d: unknown channel id", id);
671 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
672 packet_start(SSH2_MSG_CHANNEL_REQUEST);
673 packet_put_int(c->remote_id);
674 packet_put_cstring(service);
675 packet_put_char(wantconfirm);
679 channel_register_status_confirm(int id, channel_confirm_cb *cb,
680 channel_confirm_abandon_cb *abandon_cb, void *ctx)
682 struct channel_confirm *cc;
685 if ((c = channel_lookup(id)) == NULL)
686 fatal("channel_register_expect: %d: bad id", id);
688 cc = xmalloc(sizeof(*cc));
690 cc->abandon_cb = abandon_cb;
692 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
696 channel_register_open_confirm(int id, channel_callback_fn *fn, void *ctx)
698 Channel *c = channel_lookup(id);
701 logit("channel_register_open_comfirm: %d: bad id", id);
704 c->open_confirm = fn;
705 c->open_confirm_ctx = ctx;
709 channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
711 Channel *c = channel_by_id(id);
714 logit("channel_register_cleanup: %d: bad id", id);
718 c->detach_close = do_close;
722 channel_cancel_cleanup(int id)
724 Channel *c = channel_by_id(id);
727 logit("channel_cancel_cleanup: %d: bad id", id);
730 c->detach_user = NULL;
735 channel_register_filter(int id, channel_infilter_fn *ifn,
736 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
738 Channel *c = channel_lookup(id);
741 logit("channel_register_filter: %d: bad id", id);
744 c->input_filter = ifn;
745 c->output_filter = ofn;
747 c->filter_cleanup = cfn;
751 channel_set_fds(int id, int rfd, int wfd, int efd,
752 int extusage, int nonblock, int is_tty, u_int window_max)
754 Channel *c = channel_lookup(id);
756 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
757 fatal("channel_activate for non-larval channel %d.", id);
758 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
759 c->type = SSH_CHANNEL_OPEN;
760 c->local_window = c->local_window_max = window_max;
761 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
762 packet_put_int(c->remote_id);
763 packet_put_int(c->local_window);
768 * 'channel_pre*' are called just before select() to add any bits relevant to
769 * channels in the select bitmasks.
772 * 'channel_post*': perform any appropriate operations for channels which
773 * have events pending.
775 typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
776 chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
777 chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
781 channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
783 FD_SET(c->sock, readset);
788 channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
790 debug3("channel %d: waiting for connection", c->self);
791 FD_SET(c->sock, writeset);
795 channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
797 if (buffer_len(&c->input) < packet_get_maxsize())
798 FD_SET(c->sock, readset);
799 if (buffer_len(&c->output) > 0)
800 FD_SET(c->sock, writeset);
803 int channel_tcpwinsz () {
804 u_int32_t tcpwinsz = 0;
805 socklen_t optsz = sizeof(tcpwinsz);
808 /* if we aren't on a socket return 128KB*/
809 if(!packet_connection_is_on_socket())
811 ret = getsockopt(packet_get_connection_in(),
812 SOL_SOCKET, SO_RCVBUF, &tcpwinsz, &optsz);
813 /* return no more than 64MB */
814 if ((ret == 0) && tcpwinsz > BUFFER_MAX_LEN_HPN)
815 tcpwinsz = BUFFER_MAX_LEN_HPN;
816 debug2("tcpwinsz: %d for connection: %d", tcpwinsz,
817 packet_get_connection_in());
822 channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
824 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
826 /* check buffer limits */
827 if ((!c->tcpwinsz) || (c->dynamic_window > 0))
828 c->tcpwinsz = channel_tcpwinsz();
830 limit = MIN(limit, 2 * c->tcpwinsz);
832 if (c->istate == CHAN_INPUT_OPEN &&
834 buffer_len(&c->input) < limit &&
835 buffer_check_alloc(&c->input, CHAN_RBUF))
836 FD_SET(c->rfd, readset);
837 if (c->ostate == CHAN_OUTPUT_OPEN ||
838 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
839 if (buffer_len(&c->output) > 0) {
840 FD_SET(c->wfd, writeset);
841 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
842 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
843 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
844 c->self, c->efd, buffer_len(&c->extended));
849 /** XXX check close conditions, too */
850 if (compat20 && c->efd != -1 &&
851 !(c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED)) {
852 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
853 buffer_len(&c->extended) > 0)
854 FD_SET(c->efd, writeset);
855 else if (!(c->flags & CHAN_EOF_SENT) &&
856 c->extended_usage == CHAN_EXTENDED_READ &&
857 buffer_len(&c->extended) < c->remote_window)
858 FD_SET(c->efd, readset);
860 /* XXX: What about efd? races? */
861 if (compat20 && c->ctl_fd != -1 &&
862 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
863 FD_SET(c->ctl_fd, readset);
868 channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
870 if (buffer_len(&c->input) == 0) {
871 packet_start(SSH_MSG_CHANNEL_CLOSE);
872 packet_put_int(c->remote_id);
874 c->type = SSH_CHANNEL_CLOSED;
875 debug2("channel %d: closing after input drain.", c->self);
881 channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
883 if (buffer_len(&c->output) == 0)
886 FD_SET(c->sock, writeset);
890 * This is a special state for X11 authentication spoofing. An opened X11
891 * connection (when authentication spoofing is being done) remains in this
892 * state until the first packet has been completely read. The authentication
893 * data in that packet is then substituted by the real data if it matches the
894 * fake data, and the channel is put into normal mode.
895 * XXX All this happens at the client side.
896 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
899 x11_open_helper(Buffer *b)
902 u_int proto_len, data_len;
904 /* Check if the fixed size part of the packet is in buffer. */
905 if (buffer_len(b) < 12)
908 /* Parse the lengths of variable-length fields. */
910 if (ucp[0] == 0x42) { /* Byte order MSB first. */
911 proto_len = 256 * ucp[6] + ucp[7];
912 data_len = 256 * ucp[8] + ucp[9];
913 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
914 proto_len = ucp[6] + 256 * ucp[7];
915 data_len = ucp[8] + 256 * ucp[9];
917 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
922 /* Check if the whole packet is in buffer. */
924 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
927 /* Check if authentication protocol matches. */
928 if (proto_len != strlen(x11_saved_proto) ||
929 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
930 debug2("X11 connection uses different authentication protocol.");
933 /* Check if authentication data matches our fake data. */
934 if (data_len != x11_fake_data_len ||
935 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
936 x11_fake_data, x11_fake_data_len) != 0) {
937 debug2("X11 auth data does not match fake data.");
940 /* Check fake data length */
941 if (x11_fake_data_len != x11_saved_data_len) {
942 error("X11 fake_data_len %d != saved_data_len %d",
943 x11_fake_data_len, x11_saved_data_len);
947 * Received authentication protocol and data match
948 * our fake data. Substitute the fake data with real
951 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
952 x11_saved_data, x11_saved_data_len);
957 channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
959 int ret = x11_open_helper(&c->output);
962 /* Start normal processing for the channel. */
963 c->type = SSH_CHANNEL_OPEN;
964 channel_pre_open_13(c, readset, writeset);
965 } else if (ret == -1) {
967 * We have received an X11 connection that has bad
968 * authentication information.
970 logit("X11 connection rejected because of wrong authentication.");
971 buffer_clear(&c->input);
972 buffer_clear(&c->output);
973 channel_close_fd(&c->sock);
975 c->type = SSH_CHANNEL_CLOSED;
976 packet_start(SSH_MSG_CHANNEL_CLOSE);
977 packet_put_int(c->remote_id);
983 channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
985 int ret = x11_open_helper(&c->output);
987 /* c->force_drain = 1; */
990 c->type = SSH_CHANNEL_OPEN;
991 channel_pre_open(c, readset, writeset);
992 } else if (ret == -1) {
993 logit("X11 connection rejected because of wrong authentication.");
994 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
996 buffer_clear(&c->input);
998 buffer_clear(&c->output);
999 /* for proto v1, the peer will send an IEOF */
1001 chan_write_failed(c);
1003 c->type = SSH_CHANNEL_OPEN;
1004 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
1008 /* try to decode a socks4 header */
1011 channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
1014 u_int len, have, i, found;
1019 u_int16_t dest_port;
1020 struct in_addr dest_addr;
1023 debug2("channel %d: decode socks4", c->self);
1025 have = buffer_len(&c->input);
1026 len = sizeof(s4_req);
1029 p = buffer_ptr(&c->input);
1030 for (found = 0, i = len; i < have; i++) {
1036 /* the peer is probably sending garbage */
1037 debug("channel %d: decode socks4: too long",
1044 buffer_get(&c->input, (char *)&s4_req.version, 1);
1045 buffer_get(&c->input, (char *)&s4_req.command, 1);
1046 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
1047 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
1048 have = buffer_len(&c->input);
1049 p = buffer_ptr(&c->input);
1051 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
1053 fatal("channel %d: decode socks4: len %d > have %d",
1054 c->self, len, have);
1055 strlcpy(username, p, sizeof(username));
1056 buffer_consume(&c->input, len);
1057 buffer_consume(&c->input, 1); /* trailing '\0' */
1059 host = inet_ntoa(s4_req.dest_addr);
1060 strlcpy(c->path, host, sizeof(c->path));
1061 c->host_port = ntohs(s4_req.dest_port);
1063 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
1064 c->self, host, c->host_port, s4_req.command);
1066 if (s4_req.command != 1) {
1067 debug("channel %d: cannot handle: socks4 cn %d",
1068 c->self, s4_req.command);
1071 s4_rsp.version = 0; /* vn: 0 for reply */
1072 s4_rsp.command = 90; /* cd: req granted */
1073 s4_rsp.dest_port = 0; /* ignored */
1074 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
1075 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
1079 /* try to decode a socks5 header */
1080 #define SSH_SOCKS5_AUTHDONE 0x1000
1081 #define SSH_SOCKS5_NOAUTH 0x00
1082 #define SSH_SOCKS5_IPV4 0x01
1083 #define SSH_SOCKS5_DOMAIN 0x03
1084 #define SSH_SOCKS5_IPV6 0x04
1085 #define SSH_SOCKS5_CONNECT 0x01
1086 #define SSH_SOCKS5_SUCCESS 0x00
1090 channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
1098 u_int16_t dest_port;
1099 u_char *p, dest_addr[255+1];
1100 u_int have, need, i, found, nmethods, addrlen, af;
1102 debug2("channel %d: decode socks5", c->self);
1103 p = buffer_ptr(&c->input);
1106 have = buffer_len(&c->input);
1107 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1108 /* format: ver | nmethods | methods */
1112 if (have < nmethods + 2)
1114 /* look for method: "NO AUTHENTICATION REQUIRED" */
1115 for (found = 0, i = 2; i < nmethods + 2; i++) {
1116 if (p[i] == SSH_SOCKS5_NOAUTH) {
1122 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1126 buffer_consume(&c->input, nmethods + 2);
1127 buffer_put_char(&c->output, 0x05); /* version */
1128 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1129 FD_SET(c->sock, writeset);
1130 c->flags |= SSH_SOCKS5_AUTHDONE;
1131 debug2("channel %d: socks5 auth done", c->self);
1132 return 0; /* need more */
1134 debug2("channel %d: socks5 post auth", c->self);
1135 if (have < sizeof(s5_req)+1)
1136 return 0; /* need more */
1137 memcpy(&s5_req, p, sizeof(s5_req));
1138 if (s5_req.version != 0x05 ||
1139 s5_req.command != SSH_SOCKS5_CONNECT ||
1140 s5_req.reserved != 0x00) {
1141 debug2("channel %d: only socks5 connect supported", c->self);
1144 switch (s5_req.atyp){
1145 case SSH_SOCKS5_IPV4:
1149 case SSH_SOCKS5_DOMAIN:
1150 addrlen = p[sizeof(s5_req)];
1153 case SSH_SOCKS5_IPV6:
1158 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1161 need = sizeof(s5_req) + addrlen + 2;
1162 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1166 buffer_consume(&c->input, sizeof(s5_req));
1167 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1168 buffer_consume(&c->input, 1); /* host string length */
1169 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1170 buffer_get(&c->input, (char *)&dest_port, 2);
1171 dest_addr[addrlen] = '\0';
1172 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1173 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
1174 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1176 c->host_port = ntohs(dest_port);
1178 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1179 c->self, c->path, c->host_port, s5_req.command);
1181 s5_rsp.version = 0x05;
1182 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1183 s5_rsp.reserved = 0; /* ignored */
1184 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1185 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1186 dest_port = 0; /* ignored */
1188 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1189 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1190 buffer_append(&c->output, &dest_port, sizeof(dest_port));
1194 /* dynamic port forwarding */
1196 channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
1202 have = buffer_len(&c->input);
1204 debug2("channel %d: pre_dynamic: have %d", c->self, have);
1205 /* buffer_dump(&c->input); */
1206 /* check if the fixed size part of the packet is in buffer. */
1209 FD_SET(c->sock, readset);
1212 /* try to guess the protocol */
1213 p = buffer_ptr(&c->input);
1216 ret = channel_decode_socks4(c, readset, writeset);
1219 ret = channel_decode_socks5(c, readset, writeset);
1227 } else if (ret == 0) {
1228 debug2("channel %d: pre_dynamic: need more", c->self);
1230 FD_SET(c->sock, readset);
1232 /* switch to the next state */
1233 c->type = SSH_CHANNEL_OPENING;
1234 port_open_helper(c, "direct-tcpip");
1238 /* This is our fake X11 server socket. */
1241 channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
1244 struct sockaddr_storage addr;
1247 char buf[16384], *remote_ipaddr;
1250 if (FD_ISSET(c->sock, readset)) {
1251 debug("X11 connection requested.");
1252 addrlen = sizeof(addr);
1253 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1254 if (c->single_connection) {
1255 debug2("single_connection: closing X11 listener.");
1256 channel_close_fd(&c->sock);
1260 error("accept: %.100s", strerror(errno));
1263 set_nodelay(newsock);
1264 remote_ipaddr = get_peer_ipaddr(newsock);
1265 remote_port = get_peer_port(newsock);
1266 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
1267 remote_ipaddr, remote_port);
1269 nc = channel_new("accepted x11 socket",
1270 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1271 c->local_window_max, c->local_maxpacket, 0, buf, 1);
1273 packet_start(SSH2_MSG_CHANNEL_OPEN);
1274 packet_put_cstring("x11");
1275 packet_put_int(nc->self);
1276 packet_put_int(nc->local_window_max);
1277 packet_put_int(nc->local_maxpacket);
1278 /* originator ipaddr and port */
1279 packet_put_cstring(remote_ipaddr);
1280 if (datafellows & SSH_BUG_X11FWD) {
1281 debug2("ssh2 x11 bug compat mode");
1283 packet_put_int(remote_port);
1287 packet_start(SSH_SMSG_X11_OPEN);
1288 packet_put_int(nc->self);
1289 if (packet_get_protocol_flags() &
1290 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1291 packet_put_cstring(buf);
1294 xfree(remote_ipaddr);
1299 port_open_helper(Channel *c, char *rtype)
1303 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1304 int remote_port = get_peer_port(c->sock);
1306 direct = (strcmp(rtype, "direct-tcpip") == 0);
1308 snprintf(buf, sizeof buf,
1309 "%s: listening port %d for %.100s port %d, "
1310 "connect from %.200s port %d",
1311 rtype, c->listening_port, c->path, c->host_port,
1312 remote_ipaddr, remote_port);
1314 xfree(c->remote_name);
1315 c->remote_name = xstrdup(buf);
1318 packet_start(SSH2_MSG_CHANNEL_OPEN);
1319 packet_put_cstring(rtype);
1320 packet_put_int(c->self);
1321 packet_put_int(c->local_window_max);
1322 packet_put_int(c->local_maxpacket);
1324 /* target host, port */
1325 packet_put_cstring(c->path);
1326 packet_put_int(c->host_port);
1328 /* listen address, port */
1329 packet_put_cstring(c->path);
1330 packet_put_int(c->listening_port);
1332 /* originator host and port */
1333 packet_put_cstring(remote_ipaddr);
1334 packet_put_int((u_int)remote_port);
1337 packet_start(SSH_MSG_PORT_OPEN);
1338 packet_put_int(c->self);
1339 packet_put_cstring(c->path);
1340 packet_put_int(c->host_port);
1341 if (packet_get_protocol_flags() &
1342 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
1343 packet_put_cstring(c->remote_name);
1346 xfree(remote_ipaddr);
1350 channel_set_reuseaddr(int fd)
1355 * Set socket options.
1356 * Allow local port reuse in TIME_WAIT.
1358 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1359 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1363 * This socket is listening for connections to a forwarded TCP/IP port.
1367 channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
1370 struct sockaddr_storage addr;
1371 int newsock, nextstate;
1375 if (FD_ISSET(c->sock, readset)) {
1376 debug("Connection to port %d forwarding "
1377 "to %.100s port %d requested.",
1378 c->listening_port, c->path, c->host_port);
1380 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1381 nextstate = SSH_CHANNEL_OPENING;
1382 rtype = "forwarded-tcpip";
1384 if (c->host_port == 0) {
1385 nextstate = SSH_CHANNEL_DYNAMIC;
1386 rtype = "dynamic-tcpip";
1388 nextstate = SSH_CHANNEL_OPENING;
1389 rtype = "direct-tcpip";
1393 addrlen = sizeof(addr);
1394 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1396 error("accept: %.100s", strerror(errno));
1399 set_nodelay(newsock);
1400 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1401 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
1402 nc->listening_port = c->listening_port;
1403 nc->host_port = c->host_port;
1404 strlcpy(nc->path, c->path, sizeof(nc->path));
1406 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1408 * do not call the channel_post handler until
1409 * this flag has been reset by a pre-handler.
1410 * otherwise the FD_ISSET calls might overflow
1414 port_open_helper(nc, rtype);
1420 * This is the authentication agent socket listening for connections from
1425 channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
1429 struct sockaddr_storage addr;
1432 if (FD_ISSET(c->sock, readset)) {
1433 addrlen = sizeof(addr);
1434 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
1436 error("accept from auth socket: %.100s", strerror(errno));
1439 nc = channel_new("accepted auth socket",
1440 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1441 c->local_window_max, c->local_maxpacket,
1442 0, "accepted auth socket", 1);
1444 packet_start(SSH2_MSG_CHANNEL_OPEN);
1445 packet_put_cstring("auth-agent@openssh.com");
1446 packet_put_int(nc->self);
1447 packet_put_int(c->local_window_max);
1448 packet_put_int(c->local_maxpacket);
1450 packet_start(SSH_SMSG_AGENT_OPEN);
1451 packet_put_int(nc->self);
1459 channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
1462 socklen_t sz = sizeof(err);
1464 if (FD_ISSET(c->sock, writeset)) {
1465 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1467 error("getsockopt SO_ERROR failed");
1470 debug("channel %d: connected to %s port %d",
1471 c->self, c->connect_ctx.host, c->connect_ctx.port);
1472 channel_connect_ctx_free(&c->connect_ctx);
1473 c->type = SSH_CHANNEL_OPEN;
1475 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1476 packet_put_int(c->remote_id);
1477 packet_put_int(c->self);
1478 packet_put_int(c->local_window);
1479 packet_put_int(c->local_maxpacket);
1481 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1482 packet_put_int(c->remote_id);
1483 packet_put_int(c->self);
1486 debug("channel %d: connection failed: %s",
1487 c->self, strerror(err));
1488 /* Try next address, if any */
1489 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1491 c->sock = c->rfd = c->wfd = sock;
1492 channel_max_fd = channel_find_maxfd();
1495 /* Exhausted all addresses */
1496 error("connect_to %.100s port %d: failed.",
1497 c->connect_ctx.host, c->connect_ctx.port);
1498 channel_connect_ctx_free(&c->connect_ctx);
1500 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1501 packet_put_int(c->remote_id);
1502 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1503 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1504 packet_put_cstring(strerror(err));
1505 packet_put_cstring("");
1508 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1509 packet_put_int(c->remote_id);
1519 channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
1521 char buf[CHAN_RBUF];
1524 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1525 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
1527 len = read(c->rfd, buf, sizeof(buf));
1528 if (len < 0 && (errno == EINTR ||
1529 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
1531 #ifndef PTY_ZEROREAD
1534 if ((!c->isatty && len <= 0) ||
1535 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
1537 debug2("channel %d: read<=0 rfd %d len %d",
1538 c->self, c->rfd, len);
1539 if (c->type != SSH_CHANNEL_OPEN) {
1540 debug2("channel %d: not open", c->self);
1543 } else if (compat13) {
1544 buffer_clear(&c->output);
1545 c->type = SSH_CHANNEL_INPUT_DRAINING;
1546 debug2("channel %d: input draining.", c->self);
1548 chan_read_failed(c);
1552 if (c->input_filter != NULL) {
1553 if (c->input_filter(c, buf, len) == -1) {
1554 debug2("channel %d: filter stops", c->self);
1555 chan_read_failed(c);
1557 } else if (c->datagram) {
1558 buffer_put_string(&c->input, buf, len);
1560 buffer_append(&c->input, buf, len);
1568 channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
1571 u_char *data = NULL, *buf;
1575 /* Send buffered output data to the socket. */
1577 FD_ISSET(c->wfd, writeset) &&
1578 buffer_len(&c->output) > 0) {
1579 if (c->output_filter != NULL) {
1580 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1581 debug2("channel %d: filter stops", c->self);
1582 if (c->type != SSH_CHANNEL_OPEN)
1585 chan_write_failed(c);
1588 } else if (c->datagram) {
1589 buf = data = buffer_get_string(&c->output, &dlen);
1591 buf = data = buffer_ptr(&c->output);
1592 dlen = buffer_len(&c->output);
1596 /* ignore truncated writes, datagrams might get lost */
1597 c->local_consumed += dlen + 4;
1598 len = write(c->wfd, buf, dlen);
1600 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1601 errno == EWOULDBLOCK))
1604 if (c->type != SSH_CHANNEL_OPEN)
1607 chan_write_failed(c);
1613 /* XXX: Later AIX versions can't push as much data to tty */
1614 if (compat20 && c->wfd_isatty)
1615 dlen = MIN(dlen, 8*1024);
1618 len = write(c->wfd, buf, dlen);
1620 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
1623 if (c->type != SSH_CHANNEL_OPEN) {
1624 debug2("channel %d: not open", c->self);
1627 } else if (compat13) {
1628 buffer_clear(&c->output);
1629 debug2("channel %d: input draining.", c->self);
1630 c->type = SSH_CHANNEL_INPUT_DRAINING;
1632 chan_write_failed(c);
1636 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
1637 if (tcgetattr(c->wfd, &tio) == 0 &&
1638 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1640 * Simulate echo to reduce the impact of
1641 * traffic analysis. We need to match the
1642 * size of a SSH2_MSG_CHANNEL_DATA message
1643 * (4 byte channel id + buf)
1645 packet_send_ignore(4 + len);
1649 buffer_consume(&c->output, len);
1650 if (compat20 && len > 0) {
1651 c->local_consumed += len;
1658 channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
1660 char buf[CHAN_RBUF];
1663 /** XXX handle drain efd, too */
1665 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1666 FD_ISSET(c->efd, writeset) &&
1667 buffer_len(&c->extended) > 0) {
1668 len = write(c->efd, buffer_ptr(&c->extended),
1669 buffer_len(&c->extended));
1670 debug2("channel %d: written %d to efd %d",
1671 c->self, len, c->efd);
1672 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1673 errno == EWOULDBLOCK))
1676 debug2("channel %d: closing write-efd %d",
1678 channel_close_fd(&c->efd);
1680 buffer_consume(&c->extended, len);
1681 c->local_consumed += len;
1683 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1684 (c->detach_close || FD_ISSET(c->efd, readset))) {
1685 len = read(c->efd, buf, sizeof(buf));
1686 debug2("channel %d: read %d from efd %d",
1687 c->self, len, c->efd);
1688 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1689 errno == EWOULDBLOCK) && !c->detach_close)))
1692 debug2("channel %d: closing read-efd %d",
1694 channel_close_fd(&c->efd);
1696 buffer_append(&c->extended, buf, len);
1705 channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
1710 /* Monitor control fd to detect if the slave client exits */
1711 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1712 len = read(c->ctl_fd, buf, sizeof(buf));
1714 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
1717 debug2("channel %d: ctl read<=0", c->self);
1718 if (c->type != SSH_CHANNEL_OPEN) {
1719 debug2("channel %d: not open", c->self);
1723 chan_read_failed(c);
1724 chan_write_failed(c);
1728 fatal("%s: unexpected data on ctl fd", __func__);
1734 channel_check_window(Channel *c)
1736 if (c->type == SSH_CHANNEL_OPEN &&
1737 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
1738 ((c->local_window_max - c->local_window >
1739 c->local_maxpacket*3) ||
1740 c->local_window < c->local_window_max/2) &&
1741 c->local_consumed > 0) {
1743 /* adjust max window size if we are in a dynamic environment */
1744 if (c->dynamic_window && (c->tcpwinsz > c->local_window_max)) {
1745 /* grow the window somewhat aggressively to maintain pressure */
1746 addition = 1.5*(c->tcpwinsz - c->local_window_max);
1747 c->local_window_max += addition;
1749 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1750 packet_put_int(c->remote_id);
1751 packet_put_int(c->local_consumed + addition);
1753 debug2("channel %d: window %d sent adjust %d",
1754 c->self, c->local_window,
1756 c->local_window += c->local_consumed + addition;
1757 c->local_consumed = 0;
1763 channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
1767 channel_handle_rfd(c, readset, writeset);
1768 channel_handle_wfd(c, readset, writeset);
1771 channel_handle_efd(c, readset, writeset);
1772 channel_handle_ctl(c, readset, writeset);
1773 channel_check_window(c);
1778 channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
1782 /* Send buffered output data to the socket. */
1783 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1784 len = write(c->sock, buffer_ptr(&c->output),
1785 buffer_len(&c->output));
1787 buffer_clear(&c->output);
1789 buffer_consume(&c->output, len);
1794 channel_handler_init_20(void)
1796 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1797 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1798 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1799 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
1800 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1801 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1802 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1803 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1805 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1806 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1807 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
1808 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1809 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1810 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1811 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1815 channel_handler_init_13(void)
1817 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1818 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1819 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1820 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1821 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1822 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1823 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
1824 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1825 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1827 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1828 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1829 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1830 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1831 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
1832 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1833 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1837 channel_handler_init_15(void)
1839 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
1840 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
1841 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1842 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1843 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1844 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
1845 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
1847 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1848 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1849 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1850 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
1851 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
1852 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
1856 channel_handler_init(void)
1860 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1861 channel_pre[i] = NULL;
1862 channel_post[i] = NULL;
1865 channel_handler_init_20();
1867 channel_handler_init_13();
1869 channel_handler_init_15();
1872 /* gc dead channels */
1874 channel_garbage_collect(Channel *c)
1878 if (c->detach_user != NULL) {
1879 if (!chan_is_dead(c, c->detach_close))
1881 debug2("channel %d: gc: notify user", c->self);
1882 c->detach_user(c->self, NULL);
1883 /* if we still have a callback */
1884 if (c->detach_user != NULL)
1886 debug2("channel %d: gc: user detached", c->self);
1888 if (!chan_is_dead(c, 1))
1890 debug2("channel %d: garbage collecting", c->self);
1895 channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
1897 static int did_init = 0;
1902 channel_handler_init();
1905 for (i = 0; i < channels_alloc; i++) {
1909 if (ftab[c->type] != NULL)
1910 (*ftab[c->type])(c, readset, writeset);
1911 channel_garbage_collect(c);
1916 * Allocate/update select bitmasks and add any bits relevant to channels in
1920 channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1921 u_int *nallocp, int rekeying)
1923 u_int n, sz, nfdset;
1925 n = MAX(*maxfdp, channel_max_fd);
1927 nfdset = howmany(n+1, NFDBITS);
1928 /* Explicitly test here, because xrealloc isn't always called */
1929 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1930 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1931 sz = nfdset * sizeof(fd_mask);
1933 /* perhaps check sz < nalloc/2 and shrink? */
1934 if (*readsetp == NULL || sz > *nallocp) {
1935 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1936 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
1940 memset(*readsetp, 0, sz);
1941 memset(*writesetp, 0, sz);
1944 channel_handler(channel_pre, *readsetp, *writesetp);
1948 * After select, perform any appropriate operations for channels which have
1952 channel_after_select(fd_set *readset, fd_set *writeset)
1954 channel_handler(channel_post, readset, writeset);
1958 /* If there is data to send to the connection, enqueue some of it now. */
1960 channel_output_poll(void)
1964 int packet_length = 0;
1966 for (i = 0; i < channels_alloc; i++) {
1972 * We are only interested in channels that can have buffered
1976 if (c->type != SSH_CHANNEL_OPEN &&
1977 c->type != SSH_CHANNEL_INPUT_DRAINING)
1980 if (c->type != SSH_CHANNEL_OPEN)
1984 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
1985 /* XXX is this true? */
1986 debug3("channel %d: will not send data after close", c->self);
1990 /* Get the amount of buffered data for this channel. */
1991 if ((c->istate == CHAN_INPUT_OPEN ||
1992 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1993 (len = buffer_len(&c->input)) > 0) {
1999 data = buffer_get_string(&c->input,
2001 packet_start(SSH2_MSG_CHANNEL_DATA);
2002 packet_put_int(c->remote_id);
2003 packet_put_string(data, dlen);
2004 packet_length = packet_send();
2005 c->remote_window -= dlen + 4;
2011 * Send some data for the other side over the secure
2015 if (len > c->remote_window)
2016 len = c->remote_window;
2017 if (len > c->remote_maxpacket)
2018 len = c->remote_maxpacket;
2020 if (packet_is_interactive()) {
2024 /* Keep the packets at reasonable size. */
2025 if (len > packet_get_maxsize()/2)
2026 len = packet_get_maxsize()/2;
2030 packet_start(compat20 ?
2031 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
2032 packet_put_int(c->remote_id);
2033 packet_put_string(buffer_ptr(&c->input), len);
2034 packet_length = packet_send();
2035 buffer_consume(&c->input, len);
2036 c->remote_window -= len;
2038 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
2040 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
2042 * input-buffer is empty and read-socket shutdown:
2043 * tell peer, that we will not send more data: send IEOF.
2044 * hack for extended data: delay EOF if EFD still in use.
2046 if (CHANNEL_EFD_INPUT_ACTIVE(c))
2047 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2048 c->self, c->efd, buffer_len(&c->extended));
2052 /* Send extended data, i.e. stderr */
2054 !(c->flags & CHAN_EOF_SENT) &&
2055 c->remote_window > 0 &&
2056 (len = buffer_len(&c->extended)) > 0 &&
2057 c->extended_usage == CHAN_EXTENDED_READ) {
2058 debug2("channel %d: rwin %u elen %u euse %d",
2059 c->self, c->remote_window, buffer_len(&c->extended),
2061 if (len > c->remote_window)
2062 len = c->remote_window;
2063 if (len > c->remote_maxpacket)
2064 len = c->remote_maxpacket;
2065 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2066 packet_put_int(c->remote_id);
2067 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2068 packet_put_string(buffer_ptr(&c->extended), len);
2069 packet_length = packet_send();
2070 buffer_consume(&c->extended, len);
2071 c->remote_window -= len;
2072 debug2("channel %d: sent ext data %d", c->self, len);
2075 return (packet_length);
2079 /* -- protocol input */
2083 channel_input_data(int type, u_int32_t seq, void *ctxt)
2090 /* Get the channel number and verify it. */
2091 id = packet_get_int();
2092 c = channel_lookup(id);
2094 packet_disconnect("Received data for nonexistent channel %d.", id);
2096 /* Ignore any data for non-open channels (might happen on close) */
2097 if (c->type != SSH_CHANNEL_OPEN &&
2098 c->type != SSH_CHANNEL_X11_OPEN)
2102 data = packet_get_string_ptr(&data_len);
2105 * Ignore data for protocol > 1.3 if output end is no longer open.
2106 * For protocol 2 the sending side is reducing its window as it sends
2107 * data, so we must 'fake' consumption of the data in order to ensure
2108 * that window updates are sent back. Otherwise the connection might
2111 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2113 c->local_window -= data_len;
2114 c->local_consumed += data_len;
2120 if (data_len > c->local_maxpacket) {
2121 logit("channel %d: rcvd big packet %d, maxpack %d",
2122 c->self, data_len, c->local_maxpacket);
2124 if (data_len > c->local_window) {
2125 logit("channel %d: rcvd too much data %d, win %d",
2126 c->self, data_len, c->local_window);
2129 c->local_window -= data_len;
2132 buffer_put_string(&c->output, data, data_len);
2134 buffer_append(&c->output, data, data_len);
2140 channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
2144 u_int data_len, tcode;
2147 /* Get the channel number and verify it. */
2148 id = packet_get_int();
2149 c = channel_lookup(id);
2152 packet_disconnect("Received extended_data for bad channel %d.", id);
2153 if (c->type != SSH_CHANNEL_OPEN) {
2154 logit("channel %d: ext data for non open", id);
2157 if (c->flags & CHAN_EOF_RCVD) {
2158 if (datafellows & SSH_BUG_EXTEOF)
2159 debug("channel %d: accepting ext data after eof", id);
2161 packet_disconnect("Received extended_data after EOF "
2162 "on channel %d.", id);
2164 tcode = packet_get_int();
2166 c->extended_usage != CHAN_EXTENDED_WRITE ||
2167 tcode != SSH2_EXTENDED_DATA_STDERR) {
2168 logit("channel %d: bad ext data", c->self);
2171 data = packet_get_string(&data_len);
2173 if (data_len > c->local_window) {
2174 logit("channel %d: rcvd too much extended_data %d, win %d",
2175 c->self, data_len, c->local_window);
2179 debug2("channel %d: rcvd ext data %d", c->self, data_len);
2180 c->local_window -= data_len;
2181 buffer_append(&c->extended, data, data_len);
2187 channel_input_ieof(int type, u_int32_t seq, void *ctxt)
2192 id = packet_get_int();
2194 c = channel_lookup(id);
2196 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2199 /* XXX force input close */
2200 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
2201 debug("channel %d: FORCE input drain", c->self);
2202 c->istate = CHAN_INPUT_WAIT_DRAIN;
2203 if (buffer_len(&c->input) == 0)
2211 channel_input_close(int type, u_int32_t seq, void *ctxt)
2216 id = packet_get_int();
2218 c = channel_lookup(id);
2220 packet_disconnect("Received close for nonexistent channel %d.", id);
2223 * Send a confirmation that we have closed the channel and no more
2224 * data is coming for it.
2226 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
2227 packet_put_int(c->remote_id);
2231 * If the channel is in closed state, we have sent a close request,
2232 * and the other side will eventually respond with a confirmation.
2233 * Thus, we cannot free the channel here, because then there would be
2234 * no-one to receive the confirmation. The channel gets freed when
2235 * the confirmation arrives.
2237 if (c->type != SSH_CHANNEL_CLOSED) {
2239 * Not a closed channel - mark it as draining, which will
2240 * cause it to be freed later.
2242 buffer_clear(&c->input);
2243 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
2247 /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
2250 channel_input_oclose(int type, u_int32_t seq, void *ctxt)
2252 int id = packet_get_int();
2253 Channel *c = channel_lookup(id);
2257 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2258 chan_rcvd_oclose(c);
2263 channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
2265 int id = packet_get_int();
2266 Channel *c = channel_lookup(id);
2270 packet_disconnect("Received close confirmation for "
2271 "out-of-range channel %d.", id);
2272 if (c->type != SSH_CHANNEL_CLOSED)
2273 packet_disconnect("Received close confirmation for "
2274 "non-closed channel %d (type %d).", id, c->type);
2280 channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
2285 id = packet_get_int();
2286 c = channel_lookup(id);
2288 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2289 packet_disconnect("Received open confirmation for "
2290 "non-opening channel %d.", id);
2291 remote_id = packet_get_int();
2292 /* Record the remote channel number and mark that the channel is now open. */
2293 c->remote_id = remote_id;
2294 c->type = SSH_CHANNEL_OPEN;
2297 c->remote_window = packet_get_int();
2298 c->remote_maxpacket = packet_get_int();
2299 if (c->open_confirm) {
2300 debug2("callback start");
2301 c->open_confirm(c->self, c->open_confirm_ctx);
2302 debug2("callback done");
2304 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2305 c->remote_window, c->remote_maxpacket);
2311 reason2txt(int reason)
2314 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2315 return "administratively prohibited";
2316 case SSH2_OPEN_CONNECT_FAILED:
2317 return "connect failed";
2318 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2319 return "unknown channel type";
2320 case SSH2_OPEN_RESOURCE_SHORTAGE:
2321 return "resource shortage";
2323 return "unknown reason";
2328 channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
2331 char *msg = NULL, *lang = NULL;
2334 id = packet_get_int();
2335 c = channel_lookup(id);
2337 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2338 packet_disconnect("Received open failure for "
2339 "non-opening channel %d.", id);
2341 reason = packet_get_int();
2342 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2343 msg = packet_get_string(NULL);
2344 lang = packet_get_string(NULL);
2346 logit("channel %d: open failed: %s%s%s", id,
2347 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2354 /* Free the channel. This will also close the socket. */
2360 channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
2369 /* Get the channel number and verify it. */
2370 id = packet_get_int();
2371 c = channel_lookup(id);
2374 logit("Received window adjust for non-open channel %d.", id);
2377 adjust = packet_get_int();
2379 debug2("channel %d: rcvd adjust %u", id, adjust);
2380 c->remote_window += adjust;
2385 channel_input_port_open(int type, u_int32_t seq, void *ctxt)
2389 char *host, *originator_string;
2392 remote_id = packet_get_int();
2393 host = packet_get_string(NULL);
2394 host_port = packet_get_int();
2396 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2397 originator_string = packet_get_string(NULL);
2399 originator_string = xstrdup("unknown (remote did not supply name)");
2402 c = channel_connect_to(host, host_port,
2403 "connected socket", originator_string);
2404 xfree(originator_string);
2407 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2408 packet_put_int(remote_id);
2411 c->remote_id = remote_id;
2416 channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2419 struct channel_confirm *cc;
2422 /* Reset keepalive timeout */
2423 keep_alive_timeouts = 0;
2425 remote_id = packet_get_int();
2428 debug2("channel_input_confirm: type %d id %d", type, remote_id);
2430 if ((c = channel_lookup(remote_id)) == NULL) {
2431 logit("channel_input_success_failure: %d: unknown", remote_id);
2435 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
2437 cc->cb(type, c, cc->ctx);
2438 TAILQ_REMOVE(&c->status_confirms, cc, entry);
2439 bzero(cc, sizeof(*cc));
2443 /* -- tcp forwarding */
2446 channel_set_af(int af)
2453 channel_set_hpn(int external_hpn_disabled, int external_hpn_buffer_size)
2455 hpn_disabled = external_hpn_disabled;
2456 hpn_buffer_size = external_hpn_buffer_size;
2457 debug("HPN Disabled: %d, HPN Buffer Size: %d", hpn_disabled, hpn_buffer_size);
2461 channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2462 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2465 int sock, r, success = 0, wildcard = 0, is_client;
2466 struct addrinfo hints, *ai, *aitop;
2467 const char *host, *addr;
2468 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2470 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2471 listen_addr : host_to_connect;
2472 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
2475 error("No forward host name.");
2478 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
2479 error("Forward host name too long.");
2484 * Determine whether or not a port forward listens to loopback,
2485 * specified address or wildcard. On the client, a specified bind
2486 * address will always override gateway_ports. On the server, a
2487 * gateway_ports of 1 (``yes'') will override the client's
2488 * specification and force a wildcard bind, whereas a value of 2
2489 * (``clientspecified'') will bind to whatever address the client
2492 * Special-case listen_addrs are:
2494 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2495 * "" (empty string), "*" -> wildcard v4/v6
2496 * "localhost" -> loopback v4/v6
2499 if (listen_addr == NULL) {
2500 /* No address specified: default to gateway_ports setting */
2503 } else if (gateway_ports || is_client) {
2504 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2505 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2506 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2507 (!is_client && gateway_ports == 1))
2509 else if (strcmp(listen_addr, "localhost") != 0)
2513 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2514 type, wildcard, (addr == NULL) ? "NULL" : addr);
2517 * getaddrinfo returns a loopback address if the hostname is
2518 * set to NULL and hints.ai_flags is not AI_PASSIVE
2520 memset(&hints, 0, sizeof(hints));
2521 hints.ai_family = IPv4or6;
2522 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
2523 hints.ai_socktype = SOCK_STREAM;
2524 snprintf(strport, sizeof strport, "%d", listen_port);
2525 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2527 /* This really shouldn't happen */
2528 packet_disconnect("getaddrinfo: fatal error: %s",
2529 ssh_gai_strerror(r));
2531 error("channel_setup_fwd_listener: "
2532 "getaddrinfo(%.64s): %s", addr,
2533 ssh_gai_strerror(r));
2538 for (ai = aitop; ai; ai = ai->ai_next) {
2539 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2541 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2542 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2543 error("channel_setup_fwd_listener: getnameinfo failed");
2546 /* Create a port to listen for the host. */
2547 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2549 /* this is no error since kernel may not support ipv6 */
2550 verbose("socket: %.100s", strerror(errno));
2554 channel_set_reuseaddr(sock);
2556 debug("Local forwarding listening on %s port %s.", ntop, strport);
2558 /* Bind the socket to the address. */
2559 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2560 /* address can be in use ipv6 address is already bound */
2562 error("bind: %.100s", strerror(errno));
2564 verbose("bind: %.100s", strerror(errno));
2569 /* Start listening for connections on the socket. */
2570 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
2571 error("listen: %.100s", strerror(errno));
2575 /* Allocate a channel number for the socket. */
2576 /* explicitly test for hpn disabled option. if true use smaller window size */
2578 c = channel_new("port listener", type, sock, sock, -1,
2579 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
2580 0, "port listener", 1);
2582 c = channel_new("port listener", type, sock, sock, -1,
2583 hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT,
2584 0, "port listener", 1);
2585 strlcpy(c->path, host, sizeof(c->path));
2586 c->host_port = port_to_connect;
2587 c->listening_port = listen_port;
2591 error("channel_setup_fwd_listener: cannot listen to port: %d",
2593 freeaddrinfo(aitop);
2598 channel_cancel_rport_listener(const char *host, u_short port)
2603 for (i = 0; i < channels_alloc; i++) {
2604 Channel *c = channels[i];
2606 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2607 strncmp(c->path, host, sizeof(c->path)) == 0 &&
2608 c->listening_port == port) {
2609 debug2("%s: close channel %d", __func__, i);
2618 /* protocol local port fwd, used by ssh (and sshd in v1) */
2620 channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
2621 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2623 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
2624 listen_host, listen_port, host_to_connect, port_to_connect,
2628 /* protocol v2 remote port fwd, used by sshd */
2630 channel_setup_remote_fwd_listener(const char *listen_address,
2631 u_short listen_port, int gateway_ports)
2633 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2634 listen_address, listen_port, NULL, 0, gateway_ports);
2638 * Initiate forwarding of connections to port "port" on remote host through
2639 * the secure channel to host:port from local side.
2643 channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
2644 const char *host_to_connect, u_short port_to_connect)
2646 int type, success = 0;
2648 /* Record locally that connection to this host/port is permitted. */
2649 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2650 fatal("channel_request_remote_forwarding: too many forwards");
2652 /* Send the forward request to the remote side. */
2654 const char *address_to_bind;
2655 if (listen_host == NULL) {
2656 if (datafellows & SSH_BUG_RFWD_ADDR)
2657 address_to_bind = "127.0.0.1";
2659 address_to_bind = "localhost";
2660 } else if (*listen_host == '\0' ||
2661 strcmp(listen_host, "*") == 0) {
2662 if (datafellows & SSH_BUG_RFWD_ADDR)
2663 address_to_bind = "0.0.0.0";
2665 address_to_bind = "";
2667 address_to_bind = listen_host;
2669 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2670 packet_put_cstring("tcpip-forward");
2671 packet_put_char(1); /* boolean: want reply */
2672 packet_put_cstring(address_to_bind);
2673 packet_put_int(listen_port);
2675 packet_write_wait();
2676 /* Assume that server accepts the request */
2679 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
2680 packet_put_int(listen_port);
2681 packet_put_cstring(host_to_connect);
2682 packet_put_int(port_to_connect);
2684 packet_write_wait();
2686 /* Wait for response from the remote side. */
2687 type = packet_read();
2689 case SSH_SMSG_SUCCESS:
2692 case SSH_SMSG_FAILURE:
2695 /* Unknown packet */
2696 packet_disconnect("Protocol error for port forward request:"
2697 "received packet type %d.", type);
2701 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2702 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2703 permitted_opens[num_permitted_opens].listen_port = listen_port;
2704 num_permitted_opens++;
2706 return (success ? 0 : -1);
2710 * Request cancellation of remote forwarding of connection host:port from
2714 channel_request_rforward_cancel(const char *host, u_short port)
2721 for (i = 0; i < num_permitted_opens; i++) {
2722 if (permitted_opens[i].host_to_connect != NULL &&
2723 permitted_opens[i].listen_port == port)
2726 if (i >= num_permitted_opens) {
2727 debug("%s: requested forward not found", __func__);
2730 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2731 packet_put_cstring("cancel-tcpip-forward");
2733 packet_put_cstring(host == NULL ? "" : host);
2734 packet_put_int(port);
2737 permitted_opens[i].listen_port = 0;
2738 permitted_opens[i].port_to_connect = 0;
2739 xfree(permitted_opens[i].host_to_connect);
2740 permitted_opens[i].host_to_connect = NULL;
2744 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2745 * listening for the port, and sends back a success reply (or disconnect
2746 * message if there was an error).
2749 channel_input_port_forward_request(int is_root, int gateway_ports)
2751 u_short port, host_port;
2755 /* Get arguments from the packet. */
2756 port = packet_get_int();
2757 hostname = packet_get_string(NULL);
2758 host_port = packet_get_int();
2762 * Check that an unprivileged user is not trying to forward a
2765 if (port < IPPORT_RESERVED && !is_root)
2767 "Requested forwarding of port %d but user is not root.",
2770 packet_disconnect("Dynamic forwarding denied.");
2773 /* Initiate forwarding */
2774 success = channel_setup_local_fwd_listener(NULL, port, hostname,
2775 host_port, gateway_ports);
2777 /* Free the argument string. */
2780 return (success ? 0 : -1);
2784 * Permits opening to any host/port if permitted_opens[] is empty. This is
2785 * usually called by the server, because the user could connect to any port
2786 * anyway, and the server has no way to know but to trust the client anyway.
2789 channel_permit_all_opens(void)
2791 if (num_permitted_opens == 0)
2792 all_opens_permitted = 1;
2796 channel_add_permitted_opens(char *host, int port)
2798 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2799 fatal("channel_add_permitted_opens: too many forwards");
2800 debug("allow port forwarding to host %s port %d", host, port);
2802 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2803 permitted_opens[num_permitted_opens].port_to_connect = port;
2804 num_permitted_opens++;
2806 all_opens_permitted = 0;
2810 channel_add_adm_permitted_opens(char *host, int port)
2812 if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2813 fatal("channel_add_adm_permitted_opens: too many forwards");
2814 debug("config allows port forwarding to host %s port %d", host, port);
2816 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2818 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
2819 return ++num_adm_permitted_opens;
2823 channel_clear_permitted_opens(void)
2827 for (i = 0; i < num_permitted_opens; i++)
2828 if (permitted_opens[i].host_to_connect != NULL)
2829 xfree(permitted_opens[i].host_to_connect);
2830 num_permitted_opens = 0;
2834 channel_clear_adm_permitted_opens(void)
2838 for (i = 0; i < num_adm_permitted_opens; i++)
2839 if (permitted_adm_opens[i].host_to_connect != NULL)
2840 xfree(permitted_adm_opens[i].host_to_connect);
2841 num_adm_permitted_opens = 0;
2845 channel_print_adm_permitted_opens(void)
2849 for (i = 0; i < num_adm_permitted_opens; i++)
2850 if (permitted_adm_opens[i].host_to_connect != NULL)
2851 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
2852 permitted_adm_opens[i].port_to_connect);
2855 /* Try to start non-blocking connect to next host in cctx list */
2857 connect_next(struct channel_connect *cctx)
2859 int sock, saved_errno;
2860 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2862 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
2863 if (cctx->ai->ai_family != AF_INET &&
2864 cctx->ai->ai_family != AF_INET6)
2866 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
2867 ntop, sizeof(ntop), strport, sizeof(strport),
2868 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
2869 error("connect_next: getnameinfo failed");
2872 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
2873 cctx->ai->ai_protocol)) == -1) {
2874 if (cctx->ai->ai_next == NULL)
2875 error("socket: %.100s", strerror(errno));
2877 verbose("socket: %.100s", strerror(errno));
2880 if (set_nonblock(sock) == -1)
2881 fatal("%s: set_nonblock(%d)", __func__, sock);
2882 if (connect(sock, cctx->ai->ai_addr,
2883 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
2884 debug("connect_next: host %.100s ([%.100s]:%s): "
2885 "%.100s", cctx->host, ntop, strport,
2887 saved_errno = errno;
2889 errno = saved_errno;
2890 continue; /* fail -- try next */
2892 debug("connect_next: host %.100s ([%.100s]:%s) "
2893 "in progress, fd=%d", cctx->host, ntop, strport, sock);
2894 cctx->ai = cctx->ai->ai_next;
2902 channel_connect_ctx_free(struct channel_connect *cctx)
2906 freeaddrinfo(cctx->aitop);
2907 bzero(cctx, sizeof(*cctx));
2909 cctx->ai = cctx->aitop = NULL;
2912 /* Return CONNECTING channel to remote host, port */
2914 connect_to(const char *host, u_short port, char *ctype, char *rname)
2916 struct addrinfo hints;
2919 char strport[NI_MAXSERV];
2920 struct channel_connect cctx;
2923 memset(&cctx, 0, sizeof(cctx));
2924 memset(&hints, 0, sizeof(hints));
2925 hints.ai_family = IPv4or6;
2926 hints.ai_socktype = SOCK_STREAM;
2927 snprintf(strport, sizeof strport, "%d", port);
2928 if ((gaierr = getaddrinfo(host, strport, &hints, &cctx.aitop)) != 0) {
2929 error("connect_to %.100s: unknown host (%s)", host,
2930 ssh_gai_strerror(gaierr));
2934 cctx.host = xstrdup(host);
2936 cctx.ai = cctx.aitop;
2938 if ((sock = connect_next(&cctx)) == -1) {
2939 error("connect to %.100s port %d failed: %s",
2940 host, port, strerror(errno));
2941 channel_connect_ctx_free(&cctx);
2944 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
2945 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
2946 c->connect_ctx = cctx;
2951 channel_connect_by_listen_address(u_short listen_port, char *ctype, char *rname)
2955 for (i = 0; i < num_permitted_opens; i++) {
2956 if (permitted_opens[i].host_to_connect != NULL &&
2957 permitted_opens[i].listen_port == listen_port) {
2959 permitted_opens[i].host_to_connect,
2960 permitted_opens[i].port_to_connect, ctype, rname);
2963 error("WARNING: Server requests forwarding for unknown listen_port %d",
2968 /* Check if connecting to that port is permitted and connect. */
2970 channel_connect_to(const char *host, u_short port, char *ctype, char *rname)
2972 int i, permit, permit_adm = 1;
2974 permit = all_opens_permitted;
2976 for (i = 0; i < num_permitted_opens; i++)
2977 if (permitted_opens[i].host_to_connect != NULL &&
2978 permitted_opens[i].port_to_connect == port &&
2979 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2983 if (num_adm_permitted_opens > 0) {
2985 for (i = 0; i < num_adm_permitted_opens; i++)
2986 if (permitted_adm_opens[i].host_to_connect != NULL &&
2987 permitted_adm_opens[i].port_to_connect == port &&
2988 strcmp(permitted_adm_opens[i].host_to_connect, host)
2993 if (!permit || !permit_adm) {
2994 logit("Received request to connect to host %.100s port %d, "
2995 "but the request was denied.", host, port);
2998 return connect_to(host, port, ctype, rname);
3002 channel_send_window_changes(void)
3007 for (i = 0; i < channels_alloc; i++) {
3008 if (channels[i] == NULL || !channels[i]->client_tty ||
3009 channels[i]->type != SSH_CHANNEL_OPEN)
3011 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3013 channel_request_start(i, "window-change", 0);
3014 packet_put_int((u_int)ws.ws_col);
3015 packet_put_int((u_int)ws.ws_row);
3016 packet_put_int((u_int)ws.ws_xpixel);
3017 packet_put_int((u_int)ws.ws_ypixel);
3022 /* -- X11 forwarding */
3025 * Creates an internet domain socket for listening for X11 connections.
3026 * Returns 0 and a suitable display number for the DISPLAY variable
3027 * stored in display_numberp , or -1 if an error occurs.
3030 x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
3031 int single_connection, u_int *display_numberp, int **chanids)
3034 int display_number, sock;
3036 struct addrinfo hints, *ai, *aitop;
3037 char strport[NI_MAXSERV];
3038 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
3040 if (chanids == NULL)
3043 for (display_number = x11_display_offset;
3044 display_number < MAX_DISPLAYS;
3046 port = 6000 + display_number;
3047 memset(&hints, 0, sizeof(hints));
3048 hints.ai_family = IPv4or6;
3049 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
3050 hints.ai_socktype = SOCK_STREAM;
3051 snprintf(strport, sizeof strport, "%d", port);
3052 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
3053 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
3056 for (ai = aitop; ai; ai = ai->ai_next) {
3057 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
3059 sock = socket(ai->ai_family, ai->ai_socktype,
3062 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
3063 error("socket: %.100s", strerror(errno));
3064 freeaddrinfo(aitop);
3067 debug("x11_create_display_inet: Socket family %d not supported",
3073 if (ai->ai_family == AF_INET6) {
3075 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
3076 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
3079 if (x11_use_localhost)
3080 channel_set_reuseaddr(sock);
3081 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
3082 debug2("bind port %d: %.100s", port, strerror(errno));
3085 for (n = 0; n < num_socks; n++) {
3091 socks[num_socks++] = sock;
3092 if (num_socks == NUM_SOCKS)
3095 freeaddrinfo(aitop);
3099 if (display_number >= MAX_DISPLAYS) {
3100 error("Failed to allocate internet-domain X11 display socket.");
3103 /* Start listening for connections on the socket. */
3104 for (n = 0; n < num_socks; n++) {
3106 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
3107 error("listen: %.100s", strerror(errno));
3113 /* Allocate a channel for each socket. */
3114 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
3115 for (n = 0; n < num_socks; n++) {
3117 /* Is this really necassary? */
3119 nc = channel_new("x11 listener",
3120 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3121 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
3122 0, "X11 inet listener", 1);
3124 nc = channel_new("x11 listener",
3125 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
3126 hpn_buffer_size, CHAN_X11_PACKET_DEFAULT,
3127 0, "X11 inet listener", 1);
3128 nc->single_connection = single_connection;
3129 (*chanids)[n] = nc->self;
3133 /* Return the display number for the DISPLAY environment variable. */
3134 *display_numberp = display_number;
3139 connect_local_xsocket(u_int dnr)
3142 struct sockaddr_un addr;
3144 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3146 error("socket: %.100s", strerror(errno));
3147 memset(&addr, 0, sizeof(addr));
3148 addr.sun_family = AF_UNIX;
3149 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
3150 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
3153 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
3158 x11_connect_display(void)
3160 u_int display_number;
3161 const char *display;
3162 char buf[1024], *cp;
3163 struct addrinfo hints, *ai, *aitop;
3164 char strport[NI_MAXSERV];
3165 int gaierr, sock = 0;
3167 /* Try to open a socket for the local X server. */
3168 display = getenv("DISPLAY");
3170 error("DISPLAY not set.");
3174 * Now we decode the value of the DISPLAY variable and make a
3175 * connection to the real X server.
3179 * Check if it is a unix domain socket. Unix domain displays are in
3180 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
3182 if (strncmp(display, "unix:", 5) == 0 ||
3183 display[0] == ':') {
3184 /* Connect to the unix domain socket. */
3185 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
3186 error("Could not parse display number from DISPLAY: %.100s",
3190 /* Create a socket. */
3191 sock = connect_local_xsocket(display_number);
3195 /* OK, we now have a connection to the display. */
3199 * Connect to an inet socket. The DISPLAY value is supposedly
3200 * hostname:d[.s], where hostname may also be numeric IP address.
3202 strlcpy(buf, display, sizeof(buf));
3203 cp = strchr(buf, ':');
3205 error("Could not find ':' in DISPLAY: %.100s", display);
3209 /* buf now contains the host name. But first we parse the display number. */
3210 if (sscanf(cp + 1, "%u", &display_number) != 1) {
3211 error("Could not parse display number from DISPLAY: %.100s",
3216 /* Look up the host address */
3217 memset(&hints, 0, sizeof(hints));
3218 hints.ai_family = IPv4or6;
3219 hints.ai_socktype = SOCK_STREAM;
3220 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
3221 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
3222 error("%.100s: unknown host. (%s)", buf,
3223 ssh_gai_strerror(gaierr));
3226 for (ai = aitop; ai; ai = ai->ai_next) {
3227 /* Create a socket. */
3228 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
3230 debug2("socket: %.100s", strerror(errno));
3233 /* Connect it to the display. */
3234 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
3235 debug2("connect %.100s port %u: %.100s", buf,
3236 6000 + display_number, strerror(errno));
3243 freeaddrinfo(aitop);
3245 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
3254 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3255 * the remote channel number. We should do whatever we want, and respond
3256 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3261 x11_input_open(int type, u_int32_t seq, void *ctxt)
3264 int remote_id, sock = 0;
3267 debug("Received X11 open request.");
3269 remote_id = packet_get_int();
3271 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
3272 remote_host = packet_get_string(NULL);
3274 remote_host = xstrdup("unknown (remote did not supply name)");
3278 /* Obtain a connection to the real X display. */
3279 sock = x11_connect_display();
3281 /* Allocate a channel for this connection. */
3282 c = channel_new("connected x11 socket",
3283 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3285 c->remote_id = remote_id;
3290 /* Send refusal to the remote host. */
3291 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3292 packet_put_int(remote_id);
3294 /* Send a confirmation to the remote host. */
3295 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
3296 packet_put_int(remote_id);
3297 packet_put_int(c->self);
3302 /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
3305 deny_input_open(int type, u_int32_t seq, void *ctxt)
3307 int rchan = packet_get_int();
3310 case SSH_SMSG_AGENT_OPEN:
3311 error("Warning: ssh server tried agent forwarding.");
3313 case SSH_SMSG_X11_OPEN:
3314 error("Warning: ssh server tried X11 forwarding.");
3317 error("deny_input_open: type %d", type);
3320 error("Warning: this is probably a break-in attempt by a malicious server.");
3321 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3322 packet_put_int(rchan);
3327 * Requests forwarding of X11 connections, generates fake authentication
3328 * data, and enables authentication spoofing.
3329 * This should be called in the client only.
3332 x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
3333 const char *proto, const char *data)
3335 u_int data_len = (u_int) strlen(data) / 2;
3342 if (x11_saved_display == NULL)
3343 x11_saved_display = xstrdup(disp);
3344 else if (strcmp(disp, x11_saved_display) != 0) {
3345 error("x11_request_forwarding_with_spoofing: different "
3346 "$DISPLAY already forwarded");
3350 cp = strchr(disp, ':');
3352 cp = strchr(cp, '.');
3354 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
3358 if (x11_saved_proto == NULL) {
3359 /* Save protocol name. */
3360 x11_saved_proto = xstrdup(proto);
3362 * Extract real authentication data and generate fake data
3363 * of the same length.
3365 x11_saved_data = xmalloc(data_len);
3366 x11_fake_data = xmalloc(data_len);
3367 for (i = 0; i < data_len; i++) {
3368 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3369 fatal("x11_request_forwarding: bad "
3370 "authentication data: %.100s", data);
3373 x11_saved_data[i] = value;
3374 x11_fake_data[i] = rnd & 0xff;
3377 x11_saved_data_len = data_len;
3378 x11_fake_data_len = data_len;
3381 /* Convert the fake data into hex. */
3382 new_data = tohex(x11_fake_data, data_len);
3384 /* Send the request packet. */
3386 channel_request_start(client_session_id, "x11-req", 0);
3387 packet_put_char(0); /* XXX bool single connection */
3389 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3391 packet_put_cstring(proto);
3392 packet_put_cstring(new_data);
3393 packet_put_int(screen_number);
3395 packet_write_wait();
3400 /* -- agent forwarding */
3402 /* Sends a message to the server to request authentication fd forwarding. */
3405 auth_request_forwarding(void)
3407 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3409 packet_write_wait();