5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Fri Mar 24 16:35:24 1995 ylo
12 * This file contains functions for generic socket connection forwarding.
13 * There is also code for initiating connection forwarding for X11 connections,
14 * arbitrary tcp/ip connections, and the authentication agent connection.
34 /* Maximum number of fake X11 displays to try. */
35 #define MAX_DISPLAYS 1000
37 /* Max len of agent socket */
38 #define MAX_SOCKET_NAME 100
40 /* default buffer for tcp-fwd-channel */
41 #define CHAN_WINDOW_DEFAULT (8*1024)
42 #define CHAN_PACKET_DEFAULT (CHAN_WINDOW_DEFAULT/2)
45 * Pointer to an array containing all allocated channels. The array is
46 * dynamically extended as needed.
48 static Channel *channels = NULL;
51 * Size of the channel array. All slots of the array must always be
52 * initialized (at least the type field); unused slots are marked with type
55 static int channels_alloc = 0;
58 * Maximum file descriptor value used in any of the channels. This is
59 * updated in channel_allocate.
61 static int channel_max_fd_value = 0;
63 /* Name and directory of socket for authentication agent forwarding. */
64 static char *channel_forwarded_auth_socket_name = NULL;
65 static char *channel_forwarded_auth_socket_dir = NULL;
67 /* Saved X11 authentication protocol name. */
68 char *x11_saved_proto = NULL;
70 /* Saved X11 authentication data. This is the real data. */
71 char *x11_saved_data = NULL;
72 unsigned int x11_saved_data_len = 0;
75 * Fake X11 authentication data. This is what the server will be sending us;
76 * we should replace any occurrences of this by the real data.
78 char *x11_fake_data = NULL;
79 unsigned int x11_fake_data_len;
82 * Data structure for storing which hosts are permitted for forward requests.
83 * The local sides of any remote forwards are stored in this array to prevent
84 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
85 * network (which might be behind a firewall).
88 char *host_to_connect; /* Connect to 'host'. */
89 u_short port_to_connect; /* Connect to 'port'. */
90 u_short listen_port; /* Remote side should listen port number. */
93 /* List of all permitted host/port pairs to connect. */
94 static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
95 /* Number of permitted host/port pairs in the array. */
96 static int num_permitted_opens = 0;
98 * If this is true, all opens are permitted. This is the case on the server
99 * on which we have to trust the client anyway, and the user could do
100 * anything after logging in anyway.
102 static int all_opens_permitted = 0;
104 /* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
105 static int have_hostname_in_open = 0;
107 /* Sets specific protocol options. */
110 channel_set_options(int hostname_in_open)
112 have_hostname_in_open = hostname_in_open;
116 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
117 * called by the server, because the user could connect to any port anyway,
118 * and the server has no way to know but to trust the client anyway.
122 channel_permit_all_opens()
124 all_opens_permitted = 1;
127 /* lookup channel by id */
130 channel_lookup(int id)
133 if (id < 0 && id > channels_alloc) {
134 log("channel_lookup: %d: bad id", id);
138 if (c->type == SSH_CHANNEL_FREE) {
139 log("channel_lookup: %d: bad id: channel free", id);
146 * Allocate a new channel object and set its type and socket. This will cause
147 * remote_name to be freed.
151 channel_new(char *ctype, int type, int rfd, int wfd, int efd,
152 int window, int maxpack, int extended_usage, char *remote_name)
157 /* Update the maximum file descriptor value. */
158 if (rfd > channel_max_fd_value)
159 channel_max_fd_value = rfd;
160 if (wfd > channel_max_fd_value)
161 channel_max_fd_value = wfd;
162 if (efd > channel_max_fd_value)
163 channel_max_fd_value = efd;
164 /* XXX set close-on-exec -markus */
166 /* Do initial allocation if this is the first call. */
167 if (channels_alloc == 0) {
169 channels = xmalloc(channels_alloc * sizeof(Channel));
170 for (i = 0; i < channels_alloc; i++)
171 channels[i].type = SSH_CHANNEL_FREE;
173 * Kludge: arrange a call to channel_stop_listening if we
174 * terminate with fatal().
176 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
178 /* Try to find a free slot where to put the new channel. */
179 for (found = -1, i = 0; i < channels_alloc; i++)
180 if (channels[i].type == SSH_CHANNEL_FREE) {
181 /* Found a free slot. */
186 /* There are no free slots. Take last+1 slot and expand the array. */
187 found = channels_alloc;
188 channels_alloc += 10;
189 debug("channel: expanding %d", channels_alloc);
190 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
191 for (i = found; i < channels_alloc; i++)
192 channels[i].type = SSH_CHANNEL_FREE;
194 /* Initialize and return new channel number. */
195 c = &channels[found];
196 buffer_init(&c->input);
197 buffer_init(&c->output);
198 buffer_init(&c->extended);
199 chan_init_iostates(c);
203 c->local_window = window;
204 c->local_window_max = window;
205 c->local_consumed = 0;
206 c->local_maxpacket = maxpack;
207 c->remote_window = 0;
208 c->remote_maxpacket = 0;
211 c->sock = (rfd == wfd) ? rfd : -1;
213 c->extended_usage = extended_usage;
215 c->remote_name = remote_name;
216 c->remote_window = 0;
217 c->remote_maxpacket = 0;
221 c->dettach_user = NULL;
222 debug("channel %d: new [%s]", found, remote_name);
226 channel_allocate(int type, int sock, char *remote_name)
228 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
231 /* Free the channel and close its socket. */
236 Channel *c = channel_lookup(id);
238 packet_disconnect("channel free: bad local channel %d", id);
239 debug("channel_free: channel %d: status: %s", id, channel_open_message());
241 shutdown(c->sock, SHUT_RDWR);
244 buffer_free(&c->input);
245 buffer_free(&c->output);
246 buffer_free(&c->extended);
247 c->type = SSH_CHANNEL_FREE;
248 if (c->remote_name) {
249 xfree(c->remote_name);
250 c->remote_name = NULL;
255 * 'channel_pre*' are called just before select() to add any bits relevant to
256 * channels in the select bitmasks.
259 * 'channel_post*': perform any appropriate operations for channels which
260 * have events pending.
262 typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
263 chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
264 chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
267 channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
269 FD_SET(c->sock, readset);
273 channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
275 if (buffer_len(&c->input) < packet_get_maxsize())
276 FD_SET(c->sock, readset);
277 if (buffer_len(&c->output) > 0)
278 FD_SET(c->sock, writeset);
282 channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
284 /* test whether sockets are 'alive' for read/write */
285 if (c->istate == CHAN_INPUT_OPEN)
286 if (buffer_len(&c->input) < packet_get_maxsize())
287 FD_SET(c->sock, readset);
288 if (c->ostate == CHAN_OUTPUT_OPEN ||
289 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
290 if (buffer_len(&c->output) > 0) {
291 FD_SET(c->sock, writeset);
292 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
299 channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
301 if (buffer_len(&c->input) == 0) {
302 packet_start(SSH_MSG_CHANNEL_CLOSE);
303 packet_put_int(c->remote_id);
305 c->type = SSH_CHANNEL_CLOSED;
306 debug("Closing channel %d after input drain.", c->self);
311 channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
313 if (buffer_len(&c->output) == 0)
314 channel_free(c->self);
316 FD_SET(c->sock, writeset);
320 * This is a special state for X11 authentication spoofing. An opened X11
321 * connection (when authentication spoofing is being done) remains in this
322 * state until the first packet has been completely read. The authentication
323 * data in that packet is then substituted by the real data if it matches the
324 * fake data, and the channel is put into normal mode.
327 x11_open_helper(Channel *c)
330 unsigned int proto_len, data_len;
332 /* Check if the fixed size part of the packet is in buffer. */
333 if (buffer_len(&c->output) < 12)
336 /* Parse the lengths of variable-length fields. */
337 ucp = (unsigned char *) buffer_ptr(&c->output);
338 if (ucp[0] == 0x42) { /* Byte order MSB first. */
339 proto_len = 256 * ucp[6] + ucp[7];
340 data_len = 256 * ucp[8] + ucp[9];
341 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
342 proto_len = ucp[6] + 256 * ucp[7];
343 data_len = ucp[8] + 256 * ucp[9];
345 debug("Initial X11 packet contains bad byte order byte: 0x%x",
350 /* Check if the whole packet is in buffer. */
351 if (buffer_len(&c->output) <
352 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
355 /* Check if authentication protocol matches. */
356 if (proto_len != strlen(x11_saved_proto) ||
357 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
358 debug("X11 connection uses different authentication protocol.");
361 /* Check if authentication data matches our fake data. */
362 if (data_len != x11_fake_data_len ||
363 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
364 x11_fake_data, x11_fake_data_len) != 0) {
365 debug("X11 auth data does not match fake data.");
368 /* Check fake data length */
369 if (x11_fake_data_len != x11_saved_data_len) {
370 error("X11 fake_data_len %d != saved_data_len %d",
371 x11_fake_data_len, x11_saved_data_len);
375 * Received authentication protocol and data match
376 * our fake data. Substitute the fake data with real
379 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
380 x11_saved_data, x11_saved_data_len);
385 channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
387 int ret = x11_open_helper(c);
389 /* Start normal processing for the channel. */
390 c->type = SSH_CHANNEL_OPEN;
391 } else if (ret == -1) {
393 * We have received an X11 connection that has bad
394 * authentication information.
396 log("X11 connection rejected because of wrong authentication.\r\n");
397 buffer_clear(&c->input);
398 buffer_clear(&c->output);
401 c->type = SSH_CHANNEL_CLOSED;
402 packet_start(SSH_MSG_CHANNEL_CLOSE);
403 packet_put_int(c->remote_id);
409 channel_pre_x11_open_15(Channel *c, fd_set * readset, fd_set * writeset)
411 int ret = x11_open_helper(c);
413 c->type = SSH_CHANNEL_OPEN;
414 } else if (ret == -1) {
415 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
417 chan_write_failed(c);
418 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
422 /* This is our fake X11 server socket. */
424 channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
426 struct sockaddr addr;
429 char buf[16384], *remote_hostname;
431 if (FD_ISSET(c->sock, readset)) {
432 debug("X11 connection requested.");
433 addrlen = sizeof(addr);
434 newsock = accept(c->sock, &addr, &addrlen);
436 error("accept: %.100s", strerror(errno));
439 remote_hostname = get_remote_hostname(newsock);
440 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
441 remote_hostname, get_peer_port(newsock));
442 xfree(remote_hostname);
443 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
445 packet_start(SSH_SMSG_X11_OPEN);
446 packet_put_int(newch);
447 if (have_hostname_in_open)
448 packet_put_string(buf, strlen(buf));
454 * This socket is listening for connections to a forwarded TCP/IP port.
457 channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
459 struct sockaddr addr;
462 char buf[1024], *remote_hostname;
465 if (FD_ISSET(c->sock, readset)) {
466 debug("Connection to port %d forwarding "
467 "to %.100s port %d requested.",
468 c->listening_port, c->path, c->host_port);
469 addrlen = sizeof(addr);
470 newsock = accept(c->sock, &addr, &addrlen);
472 error("accept: %.100s", strerror(errno));
475 remote_hostname = get_remote_hostname(newsock);
476 remote_port = get_peer_port(newsock);
477 snprintf(buf, sizeof buf,
478 "listen port %d for %.100s port %d, "
479 "connect from %.200s port %d",
480 c->listening_port, c->path, c->host_port,
481 remote_hostname, remote_port);
482 newch = channel_new("direct-tcpip",
483 SSH_CHANNEL_OPENING, newsock, newsock, -1,
484 c->local_window_max, c->local_maxpacket,
487 packet_start(SSH_MSG_PORT_OPEN);
488 packet_put_int(newch);
489 packet_put_string(c->path, strlen(c->path));
490 packet_put_int(c->host_port);
491 if (have_hostname_in_open) {
492 packet_put_string(buf, strlen(buf));
495 xfree(remote_hostname);
500 * This is the authentication agent socket listening for connections from
504 channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
506 struct sockaddr addr;
510 if (FD_ISSET(c->sock, readset)) {
511 addrlen = sizeof(addr);
512 newsock = accept(c->sock, &addr, &addrlen);
514 error("accept from auth socket: %.100s", strerror(errno));
517 newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
518 xstrdup("accepted auth socket"));
519 packet_start(SSH_SMSG_AGENT_OPEN);
520 packet_put_int(newch);
526 channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
532 FD_ISSET(c->rfd, readset)) {
533 len = read(c->rfd, buf, sizeof(buf));
535 debug("channel %d: read<0 rfd %d len %d",
536 c->self, c->rfd, len);
538 buffer_consume(&c->output, buffer_len(&c->output));
539 c->type = SSH_CHANNEL_INPUT_DRAINING;
540 debug("Channel %d status set to input draining.", c->self);
546 buffer_append(&c->input, buf, len);
551 channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
555 /* Send buffered output data to the socket. */
557 FD_ISSET(c->wfd, writeset) &&
558 buffer_len(&c->output) > 0) {
559 len = write(c->wfd, buffer_ptr(&c->output),
560 buffer_len(&c->output));
563 buffer_consume(&c->output, buffer_len(&c->output));
564 debug("Channel %d status set to input draining.", c->self);
565 c->type = SSH_CHANNEL_INPUT_DRAINING;
567 chan_write_failed(c);
571 buffer_consume(&c->output, len);
577 channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
579 channel_handle_rfd(c, readset, writeset);
580 channel_handle_wfd(c, readset, writeset);
584 channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
587 /* Send buffered output data to the socket. */
588 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
589 len = write(c->sock, buffer_ptr(&c->output),
590 buffer_len(&c->output));
592 buffer_consume(&c->output, buffer_len(&c->output));
594 buffer_consume(&c->output, len);
599 channel_handler_init_13(void)
601 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
602 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
603 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
604 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
605 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
606 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
607 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
609 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
610 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
611 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
612 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
613 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
617 channel_handler_init_15(void)
619 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
620 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_15;
621 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
622 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
623 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
625 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
626 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
627 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
628 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
632 channel_handler_init(void)
635 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
636 channel_pre[i] = NULL;
637 channel_post[i] = NULL;
640 channel_handler_init_13();
642 channel_handler_init_15();
646 channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
648 static int did_init = 0;
653 channel_handler_init();
656 for (i = 0; i < channels_alloc; i++) {
658 if (c->type == SSH_CHANNEL_FREE)
660 if (ftab[c->type] == NULL)
662 (*ftab[c->type])(c, readset, writeset);
664 chan_delete_if_full_closed(c);
669 channel_prepare_select(fd_set * readset, fd_set * writeset)
671 channel_handler(channel_pre, readset, writeset);
675 channel_after_select(fd_set * readset, fd_set * writeset)
677 channel_handler(channel_post, readset, writeset);
680 /* If there is data to send to the connection, send some of it now. */
683 channel_output_poll()
688 for (i = 0; i < channels_alloc; i++) {
691 /* We are only interested in channels that can have buffered incoming data. */
693 if (c->type != SSH_CHANNEL_OPEN &&
694 c->type != SSH_CHANNEL_INPUT_DRAINING)
697 if (c->type != SSH_CHANNEL_OPEN)
699 if (c->istate != CHAN_INPUT_OPEN &&
700 c->istate != CHAN_INPUT_WAIT_DRAIN)
704 /* Get the amount of buffered data for this channel. */
705 len = buffer_len(&c->input);
707 /* Send some data for the other side over the secure connection. */
708 if (packet_is_interactive()) {
712 /* Keep the packets at reasonable size. */
713 if (len > packet_get_maxsize())
714 len = packet_get_maxsize()/2;
717 packet_start(SSH_MSG_CHANNEL_DATA);
718 packet_put_int(c->remote_id);
719 packet_put_string(buffer_ptr(&c->input), len);
721 buffer_consume(&c->input, len);
722 c->remote_window -= len;
723 debug("channel %d: send data len %d", c->self, len);
725 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
727 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
729 * input-buffer is empty and read-socket shutdown:
730 * tell peer, that we will not send more data: send IEOF
738 * This is called when a packet of type CHANNEL_DATA has just been received.
739 * The message type has already been consumed, but channel number and data is
744 channel_input_data(int type, int plen)
748 unsigned int data_len;
751 /* Get the channel number and verify it. */
752 id = packet_get_int();
753 c = channel_lookup(id);
755 packet_disconnect("Received data for nonexistent channel %d.", id);
757 /* Ignore any data for non-open channels (might happen on close) */
758 if (c->type != SSH_CHANNEL_OPEN &&
759 c->type != SSH_CHANNEL_X11_OPEN)
762 /* same for protocol 1.5 if output end is no longer open */
763 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
767 data = packet_get_string(&data_len);
769 packet_integrity_check(plen, 4 + 4 + data_len, SSH_MSG_CHANNEL_DATA);
770 buffer_append(&c->output, data, data_len);
775 * Returns true if no channel has too much buffered data, and false if one or
776 * more channel is overfull.
780 channel_not_very_much_buffered_data()
785 for (i = 0; i < channels_alloc; i++) {
787 if (c->type == SSH_CHANNEL_OPEN) {
788 if (buffer_len(&c->input) > packet_get_maxsize()) {
789 debug("channel %d: big input buffer %d",
790 c->self, buffer_len(&c->input));
793 if (buffer_len(&c->output) > packet_get_maxsize()) {
794 debug("channel %d: big output buffer %d",
795 c->self, buffer_len(&c->output));
804 channel_input_ieof(int type, int plen)
809 packet_integrity_check(plen, 4, type);
811 id = packet_get_int();
812 c = channel_lookup(id);
814 packet_disconnect("Received ieof for nonexistent channel %d.", id);
819 channel_input_close(int type, int plen)
824 packet_integrity_check(plen, 4, type);
826 id = packet_get_int();
827 c = channel_lookup(id);
829 packet_disconnect("Received close for nonexistent channel %d.", id);
832 * Send a confirmation that we have closed the channel and no more
833 * data is coming for it.
835 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
836 packet_put_int(c->remote_id);
840 * If the channel is in closed state, we have sent a close request,
841 * and the other side will eventually respond with a confirmation.
842 * Thus, we cannot free the channel here, because then there would be
843 * no-one to receive the confirmation. The channel gets freed when
844 * the confirmation arrives.
846 if (c->type != SSH_CHANNEL_CLOSED) {
848 * Not a closed channel - mark it as draining, which will
849 * cause it to be freed later.
851 buffer_consume(&c->input, buffer_len(&c->input));
852 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
856 /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
858 channel_input_oclose(int type, int plen)
860 int id = packet_get_int();
861 Channel *c = channel_lookup(id);
862 packet_integrity_check(plen, 4, type);
864 packet_disconnect("Received oclose for nonexistent channel %d.", id);
869 channel_input_close_confirmation(int type, int plen)
871 int id = packet_get_int();
872 Channel *c = channel_lookup(id);
875 packet_disconnect("Received close confirmation for "
876 "out-of-range channel %d.", id);
877 if (c->type != SSH_CHANNEL_CLOSED)
878 packet_disconnect("Received close confirmation for "
879 "non-closed channel %d (type %d).", id, c->type);
880 channel_free(c->self);
884 channel_input_open_confirmation(int type, int plen)
889 packet_integrity_check(plen, 4 + 4, type);
891 id = packet_get_int();
892 c = channel_lookup(id);
894 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
895 packet_disconnect("Received open confirmation for "
896 "non-opening channel %d.", id);
897 remote_id = packet_get_int();
898 /* Record the remote channel number and mark that the channel is now open. */
899 c->remote_id = remote_id;
900 c->type = SSH_CHANNEL_OPEN;
904 channel_input_open_failure(int type, int plen)
909 packet_integrity_check(plen, 4, type);
911 id = packet_get_int();
912 c = channel_lookup(id);
914 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
915 packet_disconnect("Received open failure for "
916 "non-opening channel %d.", id);
918 /* Free the channel. This will also close the socket. */
923 * Stops listening for channels, and removes any unix domain sockets that we
928 channel_stop_listening()
931 for (i = 0; i < channels_alloc; i++) {
932 switch (channels[i].type) {
933 case SSH_CHANNEL_AUTH_SOCKET:
934 close(channels[i].sock);
935 remove(channels[i].path);
938 case SSH_CHANNEL_PORT_LISTENER:
939 case SSH_CHANNEL_X11_LISTENER:
940 close(channels[i].sock);
950 * Closes the sockets of all channels. This is used to close extra file
951 * descriptors after a fork.
958 for (i = 0; i < channels_alloc; i++) {
959 if (channels[i].type != SSH_CHANNEL_FREE)
960 close(channels[i].sock);
964 /* Returns the maximum file descriptor number used by the channels. */
969 return channel_max_fd_value;
972 /* Returns true if any channel is still open. */
978 for (i = 0; i < channels_alloc; i++)
979 switch (channels[i].type) {
980 case SSH_CHANNEL_FREE:
981 case SSH_CHANNEL_X11_LISTENER:
982 case SSH_CHANNEL_PORT_LISTENER:
983 case SSH_CHANNEL_CLOSED:
984 case SSH_CHANNEL_AUTH_SOCKET:
986 case SSH_CHANNEL_OPENING:
987 case SSH_CHANNEL_OPEN:
988 case SSH_CHANNEL_X11_OPEN:
990 case SSH_CHANNEL_INPUT_DRAINING:
991 case SSH_CHANNEL_OUTPUT_DRAINING:
993 fatal("cannot happen: OUT_DRAIN");
996 fatal("channel_still_open: bad channel type %d", channels[i].type);
1003 * Returns a message describing the currently open forwarded connections,
1004 * suitable for sending to the client. The message contains crlf pairs for
1009 channel_open_message()
1015 buffer_init(&buffer);
1016 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
1017 buffer_append(&buffer, buf, strlen(buf));
1018 for (i = 0; i < channels_alloc; i++) {
1019 Channel *c = &channels[i];
1021 case SSH_CHANNEL_FREE:
1022 case SSH_CHANNEL_X11_LISTENER:
1023 case SSH_CHANNEL_PORT_LISTENER:
1024 case SSH_CHANNEL_CLOSED:
1025 case SSH_CHANNEL_AUTH_SOCKET:
1027 case SSH_CHANNEL_OPENING:
1028 case SSH_CHANNEL_OPEN:
1029 case SSH_CHANNEL_X11_OPEN:
1030 case SSH_CHANNEL_INPUT_DRAINING:
1031 case SSH_CHANNEL_OUTPUT_DRAINING:
1032 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
1033 c->self, c->remote_name,
1034 c->type, c->remote_id,
1035 c->istate, buffer_len(&c->input),
1036 c->ostate, buffer_len(&c->output),
1038 buffer_append(&buffer, buf, strlen(buf));
1041 fatal("channel_open_message: bad channel type %d", c->type);
1045 buffer_append(&buffer, "\0", 1);
1046 cp = xstrdup(buffer_ptr(&buffer));
1047 buffer_free(&buffer);
1052 * Initiate forwarding of connections to local port "port" through the secure
1053 * channel to host:port from remote side.
1057 channel_request_local_forwarding(u_short port, const char *host,
1058 u_short host_port, int gateway_ports)
1060 int success, ch, sock, on = 1;
1061 struct addrinfo hints, *ai, *aitop;
1062 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1063 struct linger linger;
1065 if (strlen(host) > sizeof(channels[0].path) - 1)
1066 packet_disconnect("Forward host name too long.");
1069 * getaddrinfo returns a loopback address if the hostname is
1070 * set to NULL and hints.ai_flags is not AI_PASSIVE
1072 memset(&hints, 0, sizeof(hints));
1073 hints.ai_family = IPv4or6;
1074 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1075 hints.ai_socktype = SOCK_STREAM;
1076 snprintf(strport, sizeof strport, "%d", port);
1077 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1078 packet_disconnect("getaddrinfo: fatal error");
1081 for (ai = aitop; ai; ai = ai->ai_next) {
1082 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1084 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1085 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1086 error("channel_request_local_forwarding: getnameinfo failed");
1089 /* Create a port to listen for the host. */
1090 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1092 /* this is no error since kernel may not support ipv6 */
1093 verbose("socket: %.100s", strerror(errno));
1097 * Set socket options. We would like the socket to disappear
1098 * as soon as it has been closed for whatever reason.
1100 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1102 linger.l_linger = 5;
1103 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1104 debug("Local forwarding listening on %s port %s.", ntop, strport);
1106 /* Bind the socket to the address. */
1107 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1108 /* address can be in use ipv6 address is already bound */
1110 error("bind: %.100s", strerror(errno));
1112 verbose("bind: %.100s", strerror(errno));
1117 /* Start listening for connections on the socket. */
1118 if (listen(sock, 5) < 0) {
1119 error("listen: %.100s", strerror(errno));
1123 /* Allocate a channel number for the socket. */
1125 "port listener", SSH_CHANNEL_PORT_LISTENER,
1127 CHAN_WINDOW_DEFAULT, CHAN_PACKET_DEFAULT,
1128 0, xstrdup("port listener"));
1129 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
1130 channels[ch].host_port = host_port;
1131 channels[ch].listening_port = port;
1135 packet_disconnect("cannot listen port: %d", port);
1136 freeaddrinfo(aitop);
1140 * Initiate forwarding of connections to port "port" on remote host through
1141 * the secure channel to host:port from local side.
1145 channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1146 u_short port_to_connect)
1149 /* Record locally that connection to this host/port is permitted. */
1150 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1151 fatal("channel_request_remote_forwarding: too many forwards");
1153 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1154 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1155 permitted_opens[num_permitted_opens].listen_port = listen_port;
1156 num_permitted_opens++;
1158 /* Send the forward request to the remote side. */
1159 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
1160 packet_put_int(port_to_connect);
1161 packet_put_string(host_to_connect, strlen(host_to_connect));
1162 packet_put_int(listen_port);
1164 packet_write_wait();
1166 * Wait for response from the remote side. It will send a disconnect
1167 * message on failure, and we will never see it here.
1169 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
1173 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1174 * listening for the port, and sends back a success reply (or disconnect
1175 * message if there was an error). This never returns if there was an error.
1179 channel_input_port_forward_request(int is_root)
1181 u_short port, host_port;
1184 /* Get arguments from the packet. */
1185 port = packet_get_int();
1186 hostname = packet_get_string(NULL);
1187 host_port = packet_get_int();
1190 * Check that an unprivileged user is not trying to forward a
1193 if (port < IPPORT_RESERVED && !is_root)
1194 packet_disconnect("Requested forwarding of port %d but user is not root.",
1197 * Initiate forwarding,
1198 * bind port to localhost only (gateway ports == 0).
1200 channel_request_local_forwarding(port, hostname, host_port, 0);
1202 /* Free the argument string. */
1206 /* XXX move to aux.c */
1208 channel_connect_to(const char *host, u_short host_port)
1210 struct addrinfo hints, *ai, *aitop;
1211 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1215 memset(&hints, 0, sizeof(hints));
1216 hints.ai_family = IPv4or6;
1217 hints.ai_socktype = SOCK_STREAM;
1218 snprintf(strport, sizeof strport, "%d", host_port);
1219 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1220 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
1223 for (ai = aitop; ai; ai = ai->ai_next) {
1224 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1226 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1227 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1228 error("channel_connect_to: getnameinfo failed");
1231 /* Create the socket. */
1232 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1234 error("socket: %.100s", strerror(errno));
1237 /* Connect to the host/port. */
1238 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1239 error("connect %.100s port %s: %.100s", ntop, strport,
1242 continue; /* fail -- try next */
1244 break; /* success */
1247 freeaddrinfo(aitop);
1249 error("connect %.100s port %d: failed.", host, host_port);
1257 * This is called after receiving PORT_OPEN message. This attempts to
1258 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1259 * or CHANNEL_OPEN_FAILURE.
1263 channel_input_port_open(int type, int plen)
1266 char *host, *originator_string;
1267 int remote_channel, sock = -1, newch, i, denied;
1268 unsigned int host_len, originator_len;
1270 /* Get remote channel number. */
1271 remote_channel = packet_get_int();
1273 /* Get host name to connect to. */
1274 host = packet_get_string(&host_len);
1276 /* Get port to connect to. */
1277 host_port = packet_get_int();
1279 /* Get remote originator name. */
1280 if (have_hostname_in_open) {
1281 originator_string = packet_get_string(&originator_len);
1282 originator_len += 4; /* size of packet_int */
1284 originator_string = xstrdup("unknown (remote did not supply name)");
1285 originator_len = 0; /* no originator supplied */
1288 packet_integrity_check(plen,
1289 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
1291 /* Check if opening that port is permitted. */
1293 if (!all_opens_permitted) {
1294 /* Go trough all permitted ports. */
1295 for (i = 0; i < num_permitted_opens; i++)
1296 if (permitted_opens[i].port_to_connect == host_port &&
1297 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1300 /* Check if we found the requested port among those permitted. */
1301 if (i >= num_permitted_opens) {
1302 /* The port is not permitted. */
1303 log("Received request to connect to %.100s:%d, but the request was denied.",
1308 sock = denied ? -1 : channel_connect_to(host, host_port);
1310 /* Allocate a channel for this connection. */
1311 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, originator_string);
1312 channels[newch].remote_id = remote_channel;
1314 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1315 packet_put_int(remote_channel);
1316 packet_put_int(newch);
1319 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1320 packet_put_int(remote_channel);
1327 * Creates an internet domain socket for listening for X11 connections.
1328 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1332 #define NUM_SOCKS 10
1335 x11_create_display_inet(int screen_number, int x11_display_offset)
1337 int display_number, sock;
1339 struct addrinfo hints, *ai, *aitop;
1340 char strport[NI_MAXSERV];
1341 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1343 char hostname[MAXHOSTNAMELEN];
1345 for (display_number = x11_display_offset;
1346 display_number < MAX_DISPLAYS;
1348 port = 6000 + display_number;
1349 memset(&hints, 0, sizeof(hints));
1350 hints.ai_family = IPv4or6;
1351 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1352 hints.ai_socktype = SOCK_STREAM;
1353 snprintf(strport, sizeof strport, "%d", port);
1354 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1355 error("getaddrinfo: %.100s", gai_strerror(gaierr));
1358 for (ai = aitop; ai; ai = ai->ai_next) {
1359 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1361 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1363 if (errno != EINVAL) {
1364 error("socket: %.100s", strerror(errno));
1367 debug("Socket family %d not supported [X11 disp create]", ai->ai_family);
1371 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1372 debug("bind port %d: %.100s", port, strerror(errno));
1373 shutdown(sock, SHUT_RDWR);
1379 for (n = 0; n < num_socks; n++) {
1380 shutdown(socks[n], SHUT_RDWR);
1386 socks[num_socks++] = sock;
1387 #ifndef DONT_TRY_OTHER_AF
1388 if (num_socks == NUM_SOCKS)
1397 if (display_number >= MAX_DISPLAYS) {
1398 error("Failed to allocate internet-domain X11 display socket.");
1401 /* Start listening for connections on the socket. */
1402 for (n = 0; n < num_socks; n++) {
1404 if (listen(sock, 5) < 0) {
1405 error("listen: %.100s", strerror(errno));
1406 shutdown(sock, SHUT_RDWR);
1412 /* Set up a suitable value for the DISPLAY variable. */
1414 if (gethostname(hostname, sizeof(hostname)) < 0)
1415 fatal("gethostname: %.100s", strerror(errno));
1417 #ifdef IPADDR_IN_DISPLAY
1419 * HPUX detects the local hostname in the DISPLAY variable and tries
1420 * to set up a shared memory connection to the server, which it
1421 * incorrectly supposes to be local.
1423 * The workaround - as used in later $$H and other programs - is
1424 * is to set display to the host's IP address.
1428 struct in_addr my_addr;
1430 he = gethostbyname(hostname);
1432 error("[X11-broken-fwd-hostname-workaround] Could not get "
1433 "IP address for hostname %s.", hostname);
1435 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
1436 "Could not get IP address for hostname %s.", hostname);
1438 shutdown(sock, SHUT_RDWR);
1444 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
1446 /* Set DISPLAY to <ip address>:screen.display */
1447 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
1448 display_number, screen_number);
1450 #else /* IPADDR_IN_DISPLAY */
1451 /* Just set DISPLAY to hostname:screen.display */
1452 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
1453 display_number, screen_number);
1454 #endif /* IPADDR_IN_DISPLAY */
1456 /* Allocate a channel for each socket. */
1457 for (n = 0; n < num_socks; n++) {
1459 (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
1460 xstrdup("X11 inet listener"));
1463 /* Return a suitable value for the DISPLAY environment variable. */
1464 return xstrdup(display);
1468 #define X_UNIX_PATH "/tmp/.X11-unix/X"
1473 connect_local_xsocket(unsigned int dnr)
1475 static const char *const x_sockets[] = {
1477 "/var/X/.X11-unix/X" "%u",
1478 "/usr/spool/sockets/X11/" "%u",
1482 struct sockaddr_un addr;
1483 const char *const * path;
1485 for (path = x_sockets; *path; ++path) {
1486 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1488 error("socket: %.100s", strerror(errno));
1489 memset(&addr, 0, sizeof(addr));
1490 addr.sun_family = AF_UNIX;
1491 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
1492 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
1496 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
1502 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1503 * the remote channel number. We should do whatever we want, and respond
1504 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1508 x11_input_open(int type, int plen)
1510 int remote_channel, display_number, sock = 0, newch;
1511 const char *display;
1512 char buf[1024], *cp, *remote_host;
1513 unsigned int remote_len;
1514 struct addrinfo hints, *ai, *aitop;
1515 char strport[NI_MAXSERV];
1518 /* Get remote channel number. */
1519 remote_channel = packet_get_int();
1521 /* Get remote originator name. */
1522 if (have_hostname_in_open) {
1523 remote_host = packet_get_string(&remote_len);
1526 remote_host = xstrdup("unknown (remote did not supply name)");
1530 debug("Received X11 open request.");
1531 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
1533 /* Try to open a socket for the local X server. */
1534 display = getenv("DISPLAY");
1536 error("DISPLAY not set.");
1540 * Now we decode the value of the DISPLAY variable and make a
1541 * connection to the real X server.
1545 * Check if it is a unix domain socket. Unix domain displays are in
1546 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1548 if (strncmp(display, "unix:", 5) == 0 ||
1549 display[0] == ':') {
1550 /* Connect to the unix domain socket. */
1551 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
1552 error("Could not parse display number from DISPLAY: %.100s",
1556 /* Create a socket. */
1557 sock = connect_local_xsocket(display_number);
1561 /* OK, we now have a connection to the display. */
1565 * Connect to an inet socket. The DISPLAY value is supposedly
1566 * hostname:d[.s], where hostname may also be numeric IP address.
1568 strncpy(buf, display, sizeof(buf));
1569 buf[sizeof(buf) - 1] = 0;
1570 cp = strchr(buf, ':');
1572 error("Could not find ':' in DISPLAY: %.100s", display);
1576 /* buf now contains the host name. But first we parse the display number. */
1577 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1578 error("Could not parse display number from DISPLAY: %.100s",
1583 /* Look up the host address */
1584 memset(&hints, 0, sizeof(hints));
1585 hints.ai_family = IPv4or6;
1586 hints.ai_socktype = SOCK_STREAM;
1587 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
1588 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
1589 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
1592 for (ai = aitop; ai; ai = ai->ai_next) {
1593 /* Create a socket. */
1594 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1596 debug("socket: %.100s", strerror(errno));
1599 /* Connect it to the display. */
1600 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1601 debug("connect %.100s port %d: %.100s", buf,
1602 6000 + display_number, strerror(errno));
1609 freeaddrinfo(aitop);
1611 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1616 /* We have successfully obtained a connection to the real X display. */
1618 /* Allocate a channel for this connection. */
1619 if (x11_saved_proto == NULL)
1620 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
1622 newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
1623 channels[newch].remote_id = remote_channel;
1625 /* Send a confirmation to the remote host. */
1626 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1627 packet_put_int(remote_channel);
1628 packet_put_int(newch);
1634 /* Send refusal to the remote host. */
1635 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1636 packet_put_int(remote_channel);
1641 * Requests forwarding of X11 connections, generates fake authentication
1642 * data, and enables authentication spoofing.
1646 x11_request_forwarding_with_spoofing(const char *proto, const char *data)
1648 unsigned int data_len = (unsigned int) strlen(data) / 2;
1649 unsigned int i, value;
1655 cp = getenv("DISPLAY");
1657 cp = strchr(cp, ':');
1659 cp = strchr(cp, '.');
1661 screen_number = atoi(cp + 1);
1665 /* Save protocol name. */
1666 x11_saved_proto = xstrdup(proto);
1669 * Extract real authentication data and generate fake data of the
1672 x11_saved_data = xmalloc(data_len);
1673 x11_fake_data = xmalloc(data_len);
1674 for (i = 0; i < data_len; i++) {
1675 if (sscanf(data + 2 * i, "%2x", &value) != 1)
1676 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
1678 rand = arc4random();
1679 x11_saved_data[i] = value;
1680 x11_fake_data[i] = rand & 0xff;
1683 x11_saved_data_len = data_len;
1684 x11_fake_data_len = data_len;
1686 /* Convert the fake data into hex. */
1687 new_data = xmalloc(2 * data_len + 1);
1688 for (i = 0; i < data_len; i++)
1689 sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
1691 /* Send the request packet. */
1692 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
1693 packet_put_string(proto, strlen(proto));
1694 packet_put_string(new_data, strlen(new_data));
1695 packet_put_int(screen_number);
1697 packet_write_wait();
1701 /* Sends a message to the server to request authentication fd forwarding. */
1704 auth_request_forwarding()
1706 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
1708 packet_write_wait();
1712 * Returns the name of the forwarded authentication socket. Returns NULL if
1713 * there is no forwarded authentication socket. The returned value points to
1718 auth_get_socket_name()
1720 return channel_forwarded_auth_socket_name;
1723 /* removes the agent forwarding socket */
1726 cleanup_socket(void)
1728 remove(channel_forwarded_auth_socket_name);
1729 rmdir(channel_forwarded_auth_socket_dir);
1733 * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
1734 * This starts forwarding authentication requests.
1738 auth_input_request_forwarding(struct passwd * pw)
1741 struct sockaddr_un sunaddr;
1743 if (auth_get_socket_name() != NULL)
1744 fatal("Protocol error: authentication forwarding requested twice.");
1746 /* Temporarily drop privileged uid for mkdir/bind. */
1747 temporarily_use_uid(pw->pw_uid);
1749 /* Allocate a buffer for the socket name, and format the name. */
1750 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
1751 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
1752 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
1754 /* Create private directory for socket */
1755 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
1756 packet_disconnect("mkdtemp: %.100s", strerror(errno));
1757 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
1758 channel_forwarded_auth_socket_dir, (int) getpid());
1760 if (atexit(cleanup_socket) < 0) {
1763 packet_disconnect("socket: %.100s", strerror(saved));
1765 /* Create the socket. */
1766 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1768 packet_disconnect("socket: %.100s", strerror(errno));
1770 /* Bind it to the name. */
1771 memset(&sunaddr, 0, sizeof(sunaddr));
1772 sunaddr.sun_family = AF_UNIX;
1773 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
1774 sizeof(sunaddr.sun_path));
1776 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
1777 packet_disconnect("bind: %.100s", strerror(errno));
1779 /* Restore the privileged uid. */
1782 /* Start listening on the socket. */
1783 if (listen(sock, 5) < 0)
1784 packet_disconnect("listen: %.100s", strerror(errno));
1786 /* Allocate a channel for the authentication agent socket. */
1787 newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock,
1788 xstrdup("auth socket"));
1789 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
1790 sizeof(channels[newch].path));
1793 /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
1796 auth_input_open_request(int type, int plen)
1798 int remch, sock, newch;
1801 packet_integrity_check(plen, 4, type);
1803 /* Read the remote channel number from the message. */
1804 remch = packet_get_int();
1807 * Get a connection to the local authentication agent (this may again
1810 sock = ssh_get_authentication_socket();
1813 * If we could not connect the agent, send an error message back to
1814 * the server. This should never happen unless the agent dies,
1815 * because authentication forwarding is only enabled if we have an
1819 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1820 packet_put_int(remch);
1824 debug("Forwarding authentication connection.");
1827 * Dummy host name. This will be freed when the channel is freed; it
1828 * will still be valid in the packet_put_string below since the
1829 * channel cannot yet be freed at that point.
1831 dummyname = xstrdup("authentication agent connection");
1833 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
1834 channels[newch].remote_id = remch;
1836 /* Send a confirmation to the remote host. */
1837 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1838 packet_put_int(remch);
1839 packet_put_int(newch);