summaryrefslogtreecommitdiff
path: root/channels.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-11-25 11:54:57 +1100
committerDamien Miller <djm@mindrot.org>1999-11-25 11:54:57 +1100
commit5428f646ad32da88ddd04a8c287d595524674fbf (patch)
treecc1f1e5d7852e1f44d41077f776abf7dab7ac06d /channels.c
parent9072e1889648988da38b7b81bce95291c1dc3a23 (diff)
- More reformatting merged from OpenBSD CVS
- Merged OpenBSD CVS changes: - [channels.c] report from mrwizard@psu.edu via djm@ibs.com.au - [channels.c] set SO_REUSEADDR and SO_LINGER for forwarded ports. chip@valinux.com via damien@ibs.com.au - [nchan.c] it's not an error() if shutdown_write failes in nchan. - [readconf.c] remove dead #ifdef-0-code - [readconf.c servconf.c] strcasecmp instead of tolower - [scp.c] progress meter overflow fix from damien@ibs.com.au - [ssh-add.1 ssh-add.c] SSH_ASKPASS support - [ssh.1 ssh.c] postpone fork_after_authentication until command execution, request/patch from jahakala@cc.jyu.fi via damien@ibs.com.au plus: use daemon() for backgrounding
Diffstat (limited to 'channels.c')
-rw-r--r--channels.c380
1 files changed, 239 insertions, 141 deletions
diff --git a/channels.c b/channels.c
index 0a37d1abd..61ba76dec 100644
--- a/channels.c
+++ b/channels.c
@@ -16,7 +16,7 @@
16 */ 16 */
17 17
18#include "includes.h" 18#include "includes.h"
19RCSID("$Id: channels.c,v 1.7 1999/11/24 13:26:22 damien Exp $"); 19RCSID("$Id: channels.c,v 1.8 1999/11/25 00:54:58 damien Exp $");
20 20
21#include "ssh.h" 21#include "ssh.h"
22#include "packet.h" 22#include "packet.h"
@@ -37,17 +37,23 @@ RCSID("$Id: channels.c,v 1.7 1999/11/24 13:26:22 damien Exp $");
37/* Max len of agent socket */ 37/* Max len of agent socket */
38#define MAX_SOCKET_NAME 100 38#define MAX_SOCKET_NAME 100
39 39
40/* Pointer to an array containing all allocated channels. The array is 40/*
41 dynamically extended as needed. */ 41 * Pointer to an array containing all allocated channels. The array is
42 * dynamically extended as needed.
43 */
42static Channel *channels = NULL; 44static Channel *channels = NULL;
43 45
44/* Size of the channel array. All slots of the array must always be 46/*
45 initialized (at least the type field); unused slots are marked with 47 * Size of the channel array. All slots of the array must always be
46 type SSH_CHANNEL_FREE. */ 48 * initialized (at least the type field); unused slots are marked with type
49 * SSH_CHANNEL_FREE.
50 */
47static int channels_alloc = 0; 51static int channels_alloc = 0;
48 52
49/* Maximum file descriptor value used in any of the channels. This is updated 53/*
50 in channel_allocate. */ 54 * Maximum file descriptor value used in any of the channels. This is
55 * updated in channel_allocate.
56 */
51static int channel_max_fd_value = 0; 57static int channel_max_fd_value = 0;
52 58
53/* Name and directory of socket for authentication agent forwarding. */ 59/* Name and directory of socket for authentication agent forwarding. */
@@ -61,15 +67,19 @@ char *x11_saved_proto = NULL;
61char *x11_saved_data = NULL; 67char *x11_saved_data = NULL;
62unsigned int x11_saved_data_len = 0; 68unsigned int x11_saved_data_len = 0;
63 69
64/* Fake X11 authentication data. This is what the server will be sending 70/*
65 us; we should replace any occurrences of this by the real data. */ 71 * Fake X11 authentication data. This is what the server will be sending us;
72 * we should replace any occurrences of this by the real data.
73 */
66char *x11_fake_data = NULL; 74char *x11_fake_data = NULL;
67unsigned int x11_fake_data_len; 75unsigned int x11_fake_data_len;
68 76
69/* Data structure for storing which hosts are permitted for forward requests. 77/*
70 The local sides of any remote forwards are stored in this array to prevent 78 * Data structure for storing which hosts are permitted for forward requests.
71 a corrupt remote server from accessing arbitrary TCP/IP ports on our 79 * The local sides of any remote forwards are stored in this array to prevent
72 local network (which might be behind a firewall). */ 80 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
81 * network (which might be behind a firewall).
82 */
73typedef struct { 83typedef struct {
74 char *host; /* Host name. */ 84 char *host; /* Host name. */
75 int port; /* Port number. */ 85 int port; /* Port number. */
@@ -79,9 +89,11 @@ typedef struct {
79static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION]; 89static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
80/* Number of permitted host/port pairs in the array. */ 90/* Number of permitted host/port pairs in the array. */
81static int num_permitted_opens = 0; 91static int num_permitted_opens = 0;
82/* If this is true, all opens are permitted. This is the case on the 92/*
83 server on which we have to trust the client anyway, and the user could 93 * If this is true, all opens are permitted. This is the case on the server
84 do anything after logging in anyway. */ 94 * on which we have to trust the client anyway, and the user could do
95 * anything after logging in anyway.
96 */
85static int all_opens_permitted = 0; 97static int all_opens_permitted = 0;
86 98
87/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */ 99/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
@@ -95,9 +107,11 @@ channel_set_options(int hostname_in_open)
95 have_hostname_in_open = hostname_in_open; 107 have_hostname_in_open = hostname_in_open;
96} 108}
97 109
98/* Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually 110/*
99 called by the server, because the user could connect to any port anyway, 111 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
100 and the server has no way to know but to trust the client anyway. */ 112 * called by the server, because the user could connect to any port anyway,
113 * and the server has no way to know but to trust the client anyway.
114 */
101 115
102void 116void
103channel_permit_all_opens() 117channel_permit_all_opens()
@@ -105,8 +119,10 @@ channel_permit_all_opens()
105 all_opens_permitted = 1; 119 all_opens_permitted = 1;
106} 120}
107 121
108/* Allocate a new channel object and set its type and socket. 122/*
109 This will cause remote_name to be freed. */ 123 * Allocate a new channel object and set its type and socket. This will cause
124 * remote_name to be freed.
125 */
110 126
111int 127int
112channel_allocate(int type, int sock, char *remote_name) 128channel_allocate(int type, int sock, char *remote_name)
@@ -117,6 +133,7 @@ channel_allocate(int type, int sock, char *remote_name)
117 /* Update the maximum file descriptor value. */ 133 /* Update the maximum file descriptor value. */
118 if (sock > channel_max_fd_value) 134 if (sock > channel_max_fd_value)
119 channel_max_fd_value = sock; 135 channel_max_fd_value = sock;
136 /* XXX set close-on-exec -markus */
120 137
121 /* Do initial allocation if this is the first call. */ 138 /* Do initial allocation if this is the first call. */
122 if (channels_alloc == 0) { 139 if (channels_alloc == 0) {
@@ -124,9 +141,10 @@ channel_allocate(int type, int sock, char *remote_name)
124 channels = xmalloc(channels_alloc * sizeof(Channel)); 141 channels = xmalloc(channels_alloc * sizeof(Channel));
125 for (i = 0; i < channels_alloc; i++) 142 for (i = 0; i < channels_alloc; i++)
126 channels[i].type = SSH_CHANNEL_FREE; 143 channels[i].type = SSH_CHANNEL_FREE;
127 144 /*
128 /* Kludge: arrange a call to channel_stop_listening if we 145 * Kludge: arrange a call to channel_stop_listening if we
129 terminate with fatal(). */ 146 * terminate with fatal().
147 */
130 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL); 148 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
131 } 149 }
132 /* Try to find a free slot where to put the new channel. */ 150 /* Try to find a free slot where to put the new channel. */
@@ -137,8 +155,7 @@ channel_allocate(int type, int sock, char *remote_name)
137 break; 155 break;
138 } 156 }
139 if (found == -1) { 157 if (found == -1) {
140 /* There are no free slots. Take last+1 slot and expand 158 /* There are no free slots. Take last+1 slot and expand the array. */
141 the array. */
142 found = channels_alloc; 159 found = channels_alloc;
143 channels_alloc += 10; 160 channels_alloc += 10;
144 debug("channel: expanding %d", channels_alloc); 161 debug("channel: expanding %d", channels_alloc);
@@ -181,8 +198,10 @@ channel_free(int channel)
181 } 198 }
182} 199}
183 200
184/* This is called just before select() to add any bits relevant to 201/*
185 channels in the select bitmasks. */ 202 * This is called just before select() to add any bits relevant to channels
203 * in the select bitmasks.
204 */
186 205
187void 206void
188channel_prepare_select(fd_set * readset, fd_set * writeset) 207channel_prepare_select(fd_set * readset, fd_set * writeset)
@@ -248,15 +267,16 @@ redo:
248 break; 267 break;
249 268
250 case SSH_CHANNEL_X11_OPEN: 269 case SSH_CHANNEL_X11_OPEN:
251 /* This is a special state for X11 authentication 270 /*
252 spoofing. An opened X11 connection (when 271 * This is a special state for X11 authentication
253 authentication spoofing is being done) remains 272 * spoofing. An opened X11 connection (when
254 in this state until the first packet has been 273 * authentication spoofing is being done) remains in
255 completely read. The authentication data in 274 * this state until the first packet has been
256 that packet is then substituted by the real 275 * completely read. The authentication data in that
257 data if it matches the fake data, and the 276 * packet is then substituted by the real data if it
258 channel is put into normal mode. */ 277 * matches the fake data, and the channel is put into
259 278 * normal mode.
279 */
260 /* Check if the fixed size part of the packet is in buffer. */ 280 /* Check if the fixed size part of the packet is in buffer. */
261 if (buffer_len(&ch->output) < 12) 281 if (buffer_len(&ch->output) < 12)
262 break; 282 break;
@@ -303,9 +323,11 @@ redo:
303 ch->type = SSH_CHANNEL_OPEN; 323 ch->type = SSH_CHANNEL_OPEN;
304 goto reject; 324 goto reject;
305 } 325 }
306 /* Received authentication protocol and data match 326 /*
307 our fake data. Substitute the fake data with 327 * Received authentication protocol and data match
308 real data. */ 328 * our fake data. Substitute the fake data with real
329 * data.
330 */
309 memcpy(ucp + 12 + ((proto_len + 3) & ~3), 331 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
310 x11_saved_data, x11_saved_data_len); 332 x11_saved_data, x11_saved_data_len);
311 333
@@ -314,8 +336,10 @@ redo:
314 goto redo; 336 goto redo;
315 337
316 reject: 338 reject:
317 /* We have received an X11 connection that has bad 339 /*
318 authentication information. */ 340 * We have received an X11 connection that has bad
341 * authentication information.
342 */
319 log("X11 connection rejected because of wrong authentication.\r\n"); 343 log("X11 connection rejected because of wrong authentication.\r\n");
320 buffer_clear(&ch->input); 344 buffer_clear(&ch->input);
321 buffer_clear(&ch->output); 345 buffer_clear(&ch->output);
@@ -341,8 +365,10 @@ redo:
341 } 365 }
342} 366}
343 367
344/* After select, perform any appropriate operations for channels which 368/*
345 have events pending. */ 369 * After select, perform any appropriate operations for channels which have
370 * events pending.
371 */
346 372
347void 373void
348channel_after_select(fd_set * readset, fd_set * writeset) 374channel_after_select(fd_set * readset, fd_set * writeset)
@@ -381,8 +407,10 @@ channel_after_select(fd_set * readset, fd_set * writeset)
381 break; 407 break;
382 408
383 case SSH_CHANNEL_PORT_LISTENER: 409 case SSH_CHANNEL_PORT_LISTENER:
384 /* This socket is listening for connections to a 410 /*
385 forwarded TCP/IP port. */ 411 * This socket is listening for connections to a
412 * forwarded TCP/IP port.
413 */
386 if (FD_ISSET(ch->sock, readset)) { 414 if (FD_ISSET(ch->sock, readset)) {
387 debug("Connection to port %d forwarding to %.100s:%d requested.", 415 debug("Connection to port %d forwarding to %.100s:%d requested.",
388 ch->listening_port, ch->path, ch->host_port); 416 ch->listening_port, ch->path, ch->host_port);
@@ -410,8 +438,10 @@ channel_after_select(fd_set * readset, fd_set * writeset)
410 break; 438 break;
411 439
412 case SSH_CHANNEL_AUTH_SOCKET: 440 case SSH_CHANNEL_AUTH_SOCKET:
413 /* This is the authentication agent socket 441 /*
414 listening for connections from clients. */ 442 * This is the authentication agent socket listening
443 * for connections from clients.
444 */
415 if (FD_ISSET(ch->sock, readset)) { 445 if (FD_ISSET(ch->sock, readset)) {
416 int nchan; 446 int nchan;
417 len = sizeof(addr); 447 len = sizeof(addr);
@@ -429,13 +459,16 @@ channel_after_select(fd_set * readset, fd_set * writeset)
429 break; 459 break;
430 460
431 case SSH_CHANNEL_OPEN: 461 case SSH_CHANNEL_OPEN:
432 /* This is an open two-way communication channel. 462 /*
433 It is not of interest to us at this point what 463 * This is an open two-way communication channel. It
434 kind of data is being transmitted. */ 464 * is not of interest to us at this point what kind
435 465 * of data is being transmitted.
436 /* Read available incoming data and append it to 466 */
437 buffer; shutdown socket, if read or write 467
438 failes */ 468 /*
469 * Read available incoming data and append it to
470 * buffer; shutdown socket, if read or write failes
471 */
439 if (FD_ISSET(ch->sock, readset)) { 472 if (FD_ISSET(ch->sock, readset)) {
440 len = read(ch->sock, buf, sizeof(buf)); 473 len = read(ch->sock, buf, sizeof(buf));
441 if (len <= 0) { 474 if (len <= 0) {
@@ -500,8 +533,7 @@ channel_output_poll()
500 533
501 for (i = 0; i < channels_alloc; i++) { 534 for (i = 0; i < channels_alloc; i++) {
502 ch = &channels[i]; 535 ch = &channels[i];
503 /* We are only interested in channels that can have 536 /* We are only interested in channels that can have buffered incoming data. */
504 buffered incoming data. */
505 if (ch->type != SSH_CHANNEL_OPEN && 537 if (ch->type != SSH_CHANNEL_OPEN &&
506 ch->type != SSH_CHANNEL_INPUT_DRAINING) 538 ch->type != SSH_CHANNEL_INPUT_DRAINING)
507 continue; 539 continue;
@@ -509,8 +541,7 @@ channel_output_poll()
509 /* Get the amount of buffered data for this channel. */ 541 /* Get the amount of buffered data for this channel. */
510 len = buffer_len(&ch->input); 542 len = buffer_len(&ch->input);
511 if (len > 0) { 543 if (len > 0) {
512 /* Send some data for the other side over the 544 /* Send some data for the other side over the secure connection. */
513 secure connection. */
514 if (packet_is_interactive()) { 545 if (packet_is_interactive()) {
515 if (len > 1024) 546 if (len > 1024)
516 len = 512; 547 len = 512;
@@ -527,17 +558,20 @@ channel_output_poll()
527 } else if (ch->istate == CHAN_INPUT_WAIT_DRAIN) { 558 } else if (ch->istate == CHAN_INPUT_WAIT_DRAIN) {
528 if (compat13) 559 if (compat13)
529 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3"); 560 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
530 /* input-buffer is empty and read-socket shutdown: 561 /*
531 tell peer, that we will not send more data: 562 * input-buffer is empty and read-socket shutdown:
532 send IEOF */ 563 * tell peer, that we will not send more data: send IEOF
564 */
533 chan_ibuf_empty(ch); 565 chan_ibuf_empty(ch);
534 } 566 }
535 } 567 }
536} 568}
537 569
538/* This is called when a packet of type CHANNEL_DATA has just been received. 570/*
539 The message type has already been consumed, but channel number and data 571 * This is called when a packet of type CHANNEL_DATA has just been received.
540 is still there. */ 572 * The message type has already been consumed, but channel number and data is
573 * still there.
574 */
541 575
542void 576void
543channel_input_data(int payload_len) 577channel_input_data(int payload_len)
@@ -564,8 +598,10 @@ channel_input_data(int payload_len)
564 xfree(data); 598 xfree(data);
565} 599}
566 600
567/* Returns true if no channel has too much buffered data, and false if 601/*
568 one or more channel is overfull. */ 602 * Returns true if no channel has too much buffered data, and false if one or
603 * more channel is overfull.
604 */
569 605
570int 606int
571channel_not_very_much_buffered_data() 607channel_not_very_much_buffered_data()
@@ -615,20 +651,27 @@ channel_input_close()
615 chan_rcvd_ieof(&channels[channel]); 651 chan_rcvd_ieof(&channels[channel]);
616 return; 652 return;
617 } 653 }
618 /* Send a confirmation that we have closed the channel and no more 654
619 data is coming for it. */ 655 /*
656 * Send a confirmation that we have closed the channel and no more
657 * data is coming for it.
658 */
620 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION); 659 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
621 packet_put_int(channels[channel].remote_id); 660 packet_put_int(channels[channel].remote_id);
622 packet_send(); 661 packet_send();
623 662
624 /* If the channel is in closed state, we have sent a close 663 /*
625 request, and the other side will eventually respond with a 664 * If the channel is in closed state, we have sent a close request,
626 confirmation. Thus, we cannot free the channel here, because 665 * and the other side will eventually respond with a confirmation.
627 then there would be no-one to receive the confirmation. The 666 * Thus, we cannot free the channel here, because then there would be
628 channel gets freed when the confirmation arrives. */ 667 * no-one to receive the confirmation. The channel gets freed when
668 * the confirmation arrives.
669 */
629 if (channels[channel].type != SSH_CHANNEL_CLOSED) { 670 if (channels[channel].type != SSH_CHANNEL_CLOSED) {
630 /* Not a closed channel - mark it as draining, which will 671 /*
631 cause it to be freed later. */ 672 * Not a closed channel - mark it as draining, which will
673 * cause it to be freed later.
674 */
632 buffer_consume(&channels[channel].input, 675 buffer_consume(&channels[channel].input,
633 buffer_len(&channels[channel].input)); 676 buffer_len(&channels[channel].input));
634 channels[channel].type = SSH_CHANNEL_OUTPUT_DRAINING; 677 channels[channel].type = SSH_CHANNEL_OUTPUT_DRAINING;
@@ -678,8 +721,7 @@ channel_input_open_confirmation()
678 /* Get remote side's id for this channel. */ 721 /* Get remote side's id for this channel. */
679 remote_channel = packet_get_int(); 722 remote_channel = packet_get_int();
680 723
681 /* Record the remote channel number and mark that the channel is 724 /* Record the remote channel number and mark that the channel is now open. */
682 now open. */
683 channels[channel].remote_id = remote_channel; 725 channels[channel].remote_id = remote_channel;
684 channels[channel].type = SSH_CHANNEL_OPEN; 726 channels[channel].type = SSH_CHANNEL_OPEN;
685} 727}
@@ -702,8 +744,10 @@ channel_input_open_failure()
702 channel_free(channel); 744 channel_free(channel);
703} 745}
704 746
705/* Stops listening for channels, and removes any unix domain sockets that 747/*
706 we might have. */ 748 * Stops listening for channels, and removes any unix domain sockets that we
749 * might have.
750 */
707 751
708void 752void
709channel_stop_listening() 753channel_stop_listening()
@@ -727,8 +771,10 @@ channel_stop_listening()
727 } 771 }
728} 772}
729 773
730/* Closes the sockets of all channels. This is used to close extra file 774/*
731 descriptors after a fork. */ 775 * Closes the sockets of all channels. This is used to close extra file
776 * descriptors after a fork.
777 */
732 778
733void 779void
734channel_close_all() 780channel_close_all()
@@ -778,9 +824,11 @@ channel_still_open()
778 return 0; 824 return 0;
779} 825}
780 826
781/* Returns a message describing the currently open forwarded 827/*
782 connections, suitable for sending to the client. The message 828 * Returns a message describing the currently open forwarded connections,
783 contains crlf pairs for newlines. */ 829 * suitable for sending to the client. The message contains crlf pairs for
830 * newlines.
831 */
784 832
785char * 833char *
786channel_open_message() 834channel_open_message()
@@ -822,16 +870,19 @@ channel_open_message()
822 return cp; 870 return cp;
823} 871}
824 872
825/* Initiate forwarding of connections to local port "port" through the secure 873/*
826 channel to host:port from remote side. */ 874 * Initiate forwarding of connections to local port "port" through the secure
875 * channel to host:port from remote side.
876 */
827 877
828void 878void
829channel_request_local_forwarding(int port, const char *host, 879channel_request_local_forwarding(int port, const char *host,
830 int host_port) 880 int host_port)
831{ 881{
832 int ch, sock; 882 int ch, sock, on = 1;
833 struct sockaddr_in sin; 883 struct sockaddr_in sin;
834 extern Options options; 884 extern Options options;
885 struct linger linger;
835 886
836 if (strlen(host) > sizeof(channels[0].path) - 1) 887 if (strlen(host) > sizeof(channels[0].path) - 1)
837 packet_disconnect("Forward host name too long."); 888 packet_disconnect("Forward host name too long.");
@@ -850,6 +901,15 @@ channel_request_local_forwarding(int port, const char *host,
850 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 901 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
851 sin.sin_port = htons(port); 902 sin.sin_port = htons(port);
852 903
904 /*
905 * Set socket options. We would like the socket to disappear as soon
906 * as it has been closed for whatever reason.
907 */
908 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
909 linger.l_onoff = 1;
910 linger.l_linger = 5;
911 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
912
853 /* Bind the socket to the address. */ 913 /* Bind the socket to the address. */
854 if (bind(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0) 914 if (bind(sock, (struct sockaddr *) & sin, sizeof(sin)) < 0)
855 packet_disconnect("bind: %.100s", strerror(errno)); 915 packet_disconnect("bind: %.100s", strerror(errno));
@@ -866,8 +926,10 @@ channel_request_local_forwarding(int port, const char *host,
866 channels[ch].listening_port = port; 926 channels[ch].listening_port = port;
867} 927}
868 928
869/* Initiate forwarding of connections to port "port" on remote host through 929/*
870 the secure channel to host:port from local side. */ 930 * Initiate forwarding of connections to port "port" on remote host through
931 * the secure channel to host:port from local side.
932 */
871 933
872void 934void
873channel_request_remote_forwarding(int port, const char *host, 935channel_request_remote_forwarding(int port, const char *host,
@@ -890,15 +952,18 @@ channel_request_remote_forwarding(int port, const char *host,
890 packet_send(); 952 packet_send();
891 packet_write_wait(); 953 packet_write_wait();
892 954
893 /* Wait for response from the remote side. It will send a 955 /*
894 disconnect message on failure, and we will never see it here. */ 956 * Wait for response from the remote side. It will send a disconnect
957 * message on failure, and we will never see it here.
958 */
895 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); 959 packet_read_expect(&payload_len, SSH_SMSG_SUCCESS);
896} 960}
897 961
898/* This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates 962/*
899 listening for the port, and sends back a success reply (or disconnect 963 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
900 message if there was an error). This never returns if there was an 964 * listening for the port, and sends back a success reply (or disconnect
901 error. */ 965 * message if there was an error). This never returns if there was an error.
966 */
902 967
903void 968void
904channel_input_port_forward_request(int is_root) 969channel_input_port_forward_request(int is_root)
@@ -915,8 +980,10 @@ channel_input_port_forward_request(int is_root)
915 if ((port & 0xffff) != port) 980 if ((port & 0xffff) != port)
916 packet_disconnect("Requested forwarding of nonexistent port %d.", port); 981 packet_disconnect("Requested forwarding of nonexistent port %d.", port);
917 982
918 /* Check that an unprivileged user is not trying to forward a 983 /*
919 privileged port. */ 984 * Check that an unprivileged user is not trying to forward a
985 * privileged port.
986 */
920 if (port < IPPORT_RESERVED && !is_root) 987 if (port < IPPORT_RESERVED && !is_root)
921 packet_disconnect("Requested forwarding of port %d but user is not root.", 988 packet_disconnect("Requested forwarding of port %d but user is not root.",
922 port); 989 port);
@@ -928,9 +995,11 @@ channel_input_port_forward_request(int is_root)
928 xfree(hostname); 995 xfree(hostname);
929} 996}
930 997
931/* This is called after receiving PORT_OPEN message. This attempts to connect 998/*
932 to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION or 999 * This is called after receiving PORT_OPEN message. This attempts to
933 CHANNEL_OPEN_FAILURE. */ 1000 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1001 * or CHANNEL_OPEN_FAILURE.
1002 */
934 1003
935void 1004void
936channel_input_port_open(int payload_len) 1005channel_input_port_open(int payload_len)
@@ -951,13 +1020,16 @@ channel_input_port_open(int payload_len)
951 host_port = packet_get_int(); 1020 host_port = packet_get_int();
952 1021
953 /* Get remote originator name. */ 1022 /* Get remote originator name. */
954 if (have_hostname_in_open) 1023 if (have_hostname_in_open) {
955 originator_string = packet_get_string(&originator_len); 1024 originator_string = packet_get_string(&originator_len);
956 else 1025 originator_len += 4; /* size of packet_int */
1026 } else {
957 originator_string = xstrdup("unknown (remote did not supply name)"); 1027 originator_string = xstrdup("unknown (remote did not supply name)");
1028 originator_len = 0; /* no originator supplied */
1029 }
958 1030
959 packet_integrity_check(payload_len, 1031 packet_integrity_check(payload_len,
960 4 + 4 + host_len + 4 + 4 + originator_len, 1032 4 + 4 + host_len + 4 + originator_len,
961 SSH_MSG_PORT_OPEN); 1033 SSH_MSG_PORT_OPEN);
962 1034
963 /* Check if opening that port is permitted. */ 1035 /* Check if opening that port is permitted. */
@@ -1040,9 +1112,11 @@ fail:
1040 packet_send(); 1112 packet_send();
1041} 1113}
1042 1114
1043/* Creates an internet domain socket for listening for X11 connections. 1115/*
1044 Returns a suitable value for the DISPLAY variable, or NULL if an error 1116 * Creates an internet domain socket for listening for X11 connections.
1045 occurs. */ 1117 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1118 * occurs.
1119 */
1046 1120
1047char * 1121char *
1048x11_create_display_inet(int screen_number) 1122x11_create_display_inet(int screen_number)
@@ -1134,9 +1208,11 @@ connect_local_xsocket(unsigned dnr)
1134} 1208}
1135 1209
1136 1210
1137/* This is called when SSH_SMSG_X11_OPEN is received. The packet contains 1211/*
1138 the remote channel number. We should do whatever we want, and respond 1212 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
1139 with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. */ 1213 * the remote channel number. We should do whatever we want, and respond
1214 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1215 */
1140 1216
1141void 1217void
1142x11_input_open(int payload_len) 1218x11_input_open(int payload_len)
@@ -1152,13 +1228,16 @@ x11_input_open(int payload_len)
1152 remote_channel = packet_get_int(); 1228 remote_channel = packet_get_int();
1153 1229
1154 /* Get remote originator name. */ 1230 /* Get remote originator name. */
1155 if (have_hostname_in_open) 1231 if (have_hostname_in_open) {
1156 remote_host = packet_get_string(&remote_len); 1232 remote_host = packet_get_string(&remote_len);
1157 else 1233 remote_len += 4;
1234 } else {
1158 remote_host = xstrdup("unknown (remote did not supply name)"); 1235 remote_host = xstrdup("unknown (remote did not supply name)");
1236 remote_len = 0;
1237 }
1159 1238
1160 debug("Received X11 open request."); 1239 debug("Received X11 open request.");
1161 packet_integrity_check(payload_len, 4 + 4 + remote_len, SSH_SMSG_X11_OPEN); 1240 packet_integrity_check(payload_len, 4 + remote_len, SSH_SMSG_X11_OPEN);
1162 1241
1163 /* Try to open a socket for the local X server. */ 1242 /* Try to open a socket for the local X server. */
1164 display = getenv("DISPLAY"); 1243 display = getenv("DISPLAY");
@@ -1166,11 +1245,15 @@ x11_input_open(int payload_len)
1166 error("DISPLAY not set."); 1245 error("DISPLAY not set.");
1167 goto fail; 1246 goto fail;
1168 } 1247 }
1169 /* Now we decode the value of the DISPLAY variable and make a 1248 /*
1170 connection to the real X server. */ 1249 * Now we decode the value of the DISPLAY variable and make a
1171 1250 * connection to the real X server.
1172 /* Check if it is a unix domain socket. Unix domain displays are 1251 */
1173 in one of the following formats: unix:d[.s], :d[.s], ::d[.s] */ 1252
1253 /*
1254 * Check if it is a unix domain socket. Unix domain displays are in
1255 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
1256 */
1174 if (strncmp(display, "unix:", 5) == 0 || 1257 if (strncmp(display, "unix:", 5) == 0 ||
1175 display[0] == ':') { 1258 display[0] == ':') {
1176 /* Connect to the unix domain socket. */ 1259 /* Connect to the unix domain socket. */
@@ -1187,8 +1270,10 @@ x11_input_open(int payload_len)
1187 /* OK, we now have a connection to the display. */ 1270 /* OK, we now have a connection to the display. */
1188 goto success; 1271 goto success;
1189 } 1272 }
1190 /* Connect to an inet socket. The DISPLAY value is supposedly 1273 /*
1191 hostname:d[.s], where hostname may also be numeric IP address. */ 1274 * Connect to an inet socket. The DISPLAY value is supposedly
1275 * hostname:d[.s], where hostname may also be numeric IP address.
1276 */
1192 strncpy(buf, display, sizeof(buf)); 1277 strncpy(buf, display, sizeof(buf));
1193 buf[sizeof(buf) - 1] = 0; 1278 buf[sizeof(buf) - 1] = 0;
1194 cp = strchr(buf, ':'); 1279 cp = strchr(buf, ':');
@@ -1197,8 +1282,7 @@ x11_input_open(int payload_len)
1197 goto fail; 1282 goto fail;
1198 } 1283 }
1199 *cp = 0; 1284 *cp = 0;
1200 /* buf now contains the host name. But first we parse the display 1285 /* buf now contains the host name. But first we parse the display number. */
1201 number. */
1202 if (sscanf(cp + 1, "%d", &display_number) != 1) { 1286 if (sscanf(cp + 1, "%d", &display_number) != 1) {
1203 error("Could not parse display number from DISPLAY: %.100s", 1287 error("Could not parse display number from DISPLAY: %.100s",
1204 display); 1288 display);
@@ -1267,8 +1351,10 @@ fail:
1267 packet_send(); 1351 packet_send();
1268} 1352}
1269 1353
1270/* Requests forwarding of X11 connections, generates fake authentication 1354/*
1271 data, and enables authentication spoofing. */ 1355 * Requests forwarding of X11 connections, generates fake authentication
1356 * data, and enables authentication spoofing.
1357 */
1272 1358
1273void 1359void
1274x11_request_forwarding_with_spoofing(const char *proto, const char *data) 1360x11_request_forwarding_with_spoofing(const char *proto, const char *data)
@@ -1293,8 +1379,10 @@ x11_request_forwarding_with_spoofing(const char *proto, const char *data)
1293 /* Save protocol name. */ 1379 /* Save protocol name. */
1294 x11_saved_proto = xstrdup(proto); 1380 x11_saved_proto = xstrdup(proto);
1295 1381
1296 /* Extract real authentication data and generate fake data of the 1382 /*
1297 same length. */ 1383 * Extract real authentication data and generate fake data of the
1384 * same length.
1385 */
1298 x11_saved_data = xmalloc(data_len); 1386 x11_saved_data = xmalloc(data_len);
1299 x11_fake_data = xmalloc(data_len); 1387 x11_fake_data = xmalloc(data_len);
1300 for (i = 0; i < data_len; i++) { 1388 for (i = 0; i < data_len; i++) {
@@ -1334,9 +1422,11 @@ auth_request_forwarding()
1334 packet_write_wait(); 1422 packet_write_wait();
1335} 1423}
1336 1424
1337/* Returns the name of the forwarded authentication socket. Returns NULL 1425/*
1338 if there is no forwarded authentication socket. The returned value 1426 * Returns the name of the forwarded authentication socket. Returns NULL if
1339 points to a static buffer. */ 1427 * there is no forwarded authentication socket. The returned value points to
1428 * a static buffer.
1429 */
1340 1430
1341char * 1431char *
1342auth_get_socket_name() 1432auth_get_socket_name()
@@ -1353,8 +1443,10 @@ cleanup_socket(void)
1353 rmdir(channel_forwarded_auth_socket_dir); 1443 rmdir(channel_forwarded_auth_socket_dir);
1354} 1444}
1355 1445
1356/* This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server. 1446/*
1357 This starts forwarding authentication requests. */ 1447 * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
1448 * This starts forwarding authentication requests.
1449 */
1358 1450
1359void 1451void
1360auth_input_request_forwarding(struct passwd * pw) 1452auth_input_request_forwarding(struct passwd * pw)
@@ -1422,14 +1514,18 @@ auth_input_open_request()
1422 /* Read the remote channel number from the message. */ 1514 /* Read the remote channel number from the message. */
1423 remch = packet_get_int(); 1515 remch = packet_get_int();
1424 1516
1425 /* Get a connection to the local authentication agent (this may 1517 /*
1426 again get forwarded). */ 1518 * Get a connection to the local authentication agent (this may again
1519 * get forwarded).
1520 */
1427 sock = ssh_get_authentication_socket(); 1521 sock = ssh_get_authentication_socket();
1428 1522
1429 /* If we could not connect the agent, send an error message back 1523 /*
1430 to the server. This should never happen unless the agent dies, 1524 * If we could not connect the agent, send an error message back to
1431 because authentication forwarding is only enabled if we have an 1525 * the server. This should never happen unless the agent dies,
1432 agent. */ 1526 * because authentication forwarding is only enabled if we have an
1527 * agent.
1528 */
1433 if (sock < 0) { 1529 if (sock < 0) {
1434 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); 1530 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1435 packet_put_int(remch); 1531 packet_put_int(remch);
@@ -1438,9 +1534,11 @@ auth_input_open_request()
1438 } 1534 }
1439 debug("Forwarding authentication connection."); 1535 debug("Forwarding authentication connection.");
1440 1536
1441 /* Dummy host name. This will be freed when the channel is freed; 1537 /*
1442 it will still be valid in the packet_put_string below since the 1538 * Dummy host name. This will be freed when the channel is freed; it
1443 channel cannot yet be freed at that point. */ 1539 * will still be valid in the packet_put_string below since the
1540 * channel cannot yet be freed at that point.
1541 */
1444 dummyname = xstrdup("authentication agent connection"); 1542 dummyname = xstrdup("authentication agent connection");
1445 1543
1446 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname); 1544 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);