summaryrefslogtreecommitdiff
path: root/channels.c
diff options
context:
space:
mode:
Diffstat (limited to 'channels.c')
-rw-r--r--channels.c263
1 files changed, 76 insertions, 187 deletions
diff --git a/channels.c b/channels.c
index 65a6a7f00..218744d1a 100644
--- a/channels.c
+++ b/channels.c
@@ -39,7 +39,7 @@
39 */ 39 */
40 40
41#include "includes.h" 41#include "includes.h"
42RCSID("$OpenBSD: channels.c,v 1.194 2003/08/29 10:04:36 markus Exp $"); 42RCSID("$OpenBSD: channels.c,v 1.187 2003/03/05 22:33:43 markus Exp $");
43 43
44#include "ssh.h" 44#include "ssh.h"
45#include "ssh1.h" 45#include "ssh1.h"
@@ -54,7 +54,7 @@ RCSID("$OpenBSD: channels.c,v 1.194 2003/08/29 10:04:36 markus Exp $");
54#include "key.h" 54#include "key.h"
55#include "authfd.h" 55#include "authfd.h"
56#include "pathnames.h" 56#include "pathnames.h"
57#include "bufaux.h" 57
58 58
59/* -- channel core */ 59/* -- channel core */
60 60
@@ -142,12 +142,12 @@ channel_lookup(int id)
142 Channel *c; 142 Channel *c;
143 143
144 if (id < 0 || id >= channels_alloc) { 144 if (id < 0 || id >= channels_alloc) {
145 logit("channel_lookup: %d: bad id", id); 145 log("channel_lookup: %d: bad id", id);
146 return NULL; 146 return NULL;
147 } 147 }
148 c = channels[id]; 148 c = channels[id];
149 if (c == NULL) { 149 if (c == NULL) {
150 logit("channel_lookup: %d: bad id: channel free", id); 150 log("channel_lookup: %d: bad id: channel free", id);
151 return NULL; 151 return NULL;
152 } 152 }
153 return c; 153 return c;
@@ -177,7 +177,7 @@ channel_register_fds(Channel *c, int rfd, int wfd, int efd,
177 177
178 /* XXX ugly hack: nonblock is only set by the server */ 178 /* XXX ugly hack: nonblock is only set by the server */
179 if (nonblock && isatty(c->rfd)) { 179 if (nonblock && isatty(c->rfd)) {
180 debug2("channel %d: rfd %d isatty", c->self, c->rfd); 180 debug("channel %d: rfd %d isatty", c->self, c->rfd);
181 c->isatty = 1; 181 c->isatty = 1;
182 if (!isatty(c->wfd)) { 182 if (!isatty(c->wfd)) {
183 error("channel %d: wfd %d is not a tty?", 183 error("channel %d: wfd %d is not a tty?",
@@ -229,12 +229,13 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
229 if (found == -1) { 229 if (found == -1) {
230 /* There are no free slots. Take last+1 slot and expand the array. */ 230 /* There are no free slots. Take last+1 slot and expand the array. */
231 found = channels_alloc; 231 found = channels_alloc;
232 channels_alloc += 10;
233 if (channels_alloc > 10000) 232 if (channels_alloc > 10000)
234 fatal("channel_new: internal error: channels_alloc %d " 233 fatal("channel_new: internal error: channels_alloc %d "
235 "too big.", channels_alloc); 234 "too big.", channels_alloc);
235 channels = xrealloc(channels,
236 (channels_alloc + 10) * sizeof(Channel *));
237 channels_alloc += 10;
236 debug2("channel: expanding %d", channels_alloc); 238 debug2("channel: expanding %d", channels_alloc);
237 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
238 for (i = found; i < channels_alloc; i++) 239 for (i = found; i < channels_alloc; i++)
239 channels[i] = NULL; 240 channels[i] = NULL;
240 } 241 }
@@ -256,7 +257,7 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
256 c->local_consumed = 0; 257 c->local_consumed = 0;
257 c->local_maxpacket = maxpack; 258 c->local_maxpacket = maxpack;
258 c->remote_id = -1; 259 c->remote_id = -1;
259 c->remote_name = xstrdup(remote_name); 260 c->remote_name = remote_name;
260 c->remote_window = 0; 261 c->remote_window = 0;
261 c->remote_maxpacket = 0; 262 c->remote_maxpacket = 0;
262 c->force_drain = 0; 263 c->force_drain = 0;
@@ -304,7 +305,7 @@ channel_close_fd(int *fdp)
304static void 305static void
305channel_close_fds(Channel *c) 306channel_close_fds(Channel *c)
306{ 307{
307 debug3("channel %d: close_fds r %d w %d e %d", 308 debug3("channel_close_fds: channel %d: r %d w %d e %d",
308 c->self, c->rfd, c->wfd, c->efd); 309 c->self, c->rfd, c->wfd, c->efd);
309 310
310 channel_close_fd(&c->sock); 311 channel_close_fd(&c->sock);
@@ -324,11 +325,11 @@ channel_free(Channel *c)
324 for (n = 0, i = 0; i < channels_alloc; i++) 325 for (n = 0, i = 0; i < channels_alloc; i++)
325 if (channels[i]) 326 if (channels[i])
326 n++; 327 n++;
327 debug("channel %d: free: %s, nchannels %d", c->self, 328 debug("channel_free: channel %d: %s, nchannels %d", c->self,
328 c->remote_name ? c->remote_name : "???", n); 329 c->remote_name ? c->remote_name : "???", n);
329 330
330 s = channel_open_message(); 331 s = channel_open_message();
331 debug3("channel %d: status: %s", c->self, s); 332 debug3("channel_free: status: %s", s);
332 xfree(s); 333 xfree(s);
333 334
334 if (c->sock != -1) 335 if (c->sock != -1)
@@ -419,7 +420,7 @@ channel_not_very_much_buffered_data(void)
419 } 420 }
420#endif 421#endif
421 if (buffer_len(&c->output) > packet_get_maxsize()) { 422 if (buffer_len(&c->output) > packet_get_maxsize()) {
422 debug2("channel %d: big output buffer %u > %u", 423 debug2("channel %d: big output buffer %d > %d",
423 c->self, buffer_len(&c->output), 424 c->self, buffer_len(&c->output),
424 packet_get_maxsize()); 425 packet_get_maxsize());
425 return 0; 426 return 0;
@@ -575,7 +576,7 @@ channel_send_open(int id)
575 Channel *c = channel_lookup(id); 576 Channel *c = channel_lookup(id);
576 577
577 if (c == NULL) { 578 if (c == NULL) {
578 logit("channel_send_open: %d: bad id", id); 579 log("channel_send_open: %d: bad id", id);
579 return; 580 return;
580 } 581 }
581 debug2("channel %d: send open", id); 582 debug2("channel %d: send open", id);
@@ -593,10 +594,10 @@ channel_request_start(int id, char *service, int wantconfirm)
593 Channel *c = channel_lookup(id); 594 Channel *c = channel_lookup(id);
594 595
595 if (c == NULL) { 596 if (c == NULL) {
596 logit("channel_request_start: %d: unknown channel id", id); 597 log("channel_request_start: %d: unknown channel id", id);
597 return; 598 return;
598 } 599 }
599 debug2("channel %d: request %s", id, service) ; 600 debug("channel %d: request %s", id, service) ;
600 packet_start(SSH2_MSG_CHANNEL_REQUEST); 601 packet_start(SSH2_MSG_CHANNEL_REQUEST);
601 packet_put_int(c->remote_id); 602 packet_put_int(c->remote_id);
602 packet_put_cstring(service); 603 packet_put_cstring(service);
@@ -608,7 +609,7 @@ channel_register_confirm(int id, channel_callback_fn *fn)
608 Channel *c = channel_lookup(id); 609 Channel *c = channel_lookup(id);
609 610
610 if (c == NULL) { 611 if (c == NULL) {
611 logit("channel_register_comfirm: %d: bad id", id); 612 log("channel_register_comfirm: %d: bad id", id);
612 return; 613 return;
613 } 614 }
614 c->confirm = fn; 615 c->confirm = fn;
@@ -619,7 +620,7 @@ channel_register_cleanup(int id, channel_callback_fn *fn)
619 Channel *c = channel_lookup(id); 620 Channel *c = channel_lookup(id);
620 621
621 if (c == NULL) { 622 if (c == NULL) {
622 logit("channel_register_cleanup: %d: bad id", id); 623 log("channel_register_cleanup: %d: bad id", id);
623 return; 624 return;
624 } 625 }
625 c->detach_user = fn; 626 c->detach_user = fn;
@@ -630,7 +631,7 @@ channel_cancel_cleanup(int id)
630 Channel *c = channel_lookup(id); 631 Channel *c = channel_lookup(id);
631 632
632 if (c == NULL) { 633 if (c == NULL) {
633 logit("channel_cancel_cleanup: %d: bad id", id); 634 log("channel_cancel_cleanup: %d: bad id", id);
634 return; 635 return;
635 } 636 }
636 c->detach_user = NULL; 637 c->detach_user = NULL;
@@ -641,7 +642,7 @@ channel_register_filter(int id, channel_filter_fn *fn)
641 Channel *c = channel_lookup(id); 642 Channel *c = channel_lookup(id);
642 643
643 if (c == NULL) { 644 if (c == NULL) {
644 logit("channel_register_filter: %d: bad id", id); 645 log("channel_register_filter: %d: bad id", id);
645 return; 646 return;
646 } 647 }
647 c->input_filter = fn; 648 c->input_filter = fn;
@@ -739,7 +740,7 @@ channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
739 packet_put_int(c->remote_id); 740 packet_put_int(c->remote_id);
740 packet_send(); 741 packet_send();
741 c->type = SSH_CHANNEL_CLOSED; 742 c->type = SSH_CHANNEL_CLOSED;
742 debug2("channel %d: closing after input drain.", c->self); 743 debug("channel %d: closing after input drain.", c->self);
743 } 744 }
744} 745}
745 746
@@ -780,7 +781,7 @@ x11_open_helper(Buffer *b)
780 proto_len = ucp[6] + 256 * ucp[7]; 781 proto_len = ucp[6] + 256 * ucp[7];
781 data_len = ucp[8] + 256 * ucp[9]; 782 data_len = ucp[8] + 256 * ucp[9];
782 } else { 783 } else {
783 debug2("Initial X11 packet contains bad byte order byte: 0x%x", 784 debug("Initial X11 packet contains bad byte order byte: 0x%x",
784 ucp[0]); 785 ucp[0]);
785 return -1; 786 return -1;
786 } 787 }
@@ -793,14 +794,14 @@ x11_open_helper(Buffer *b)
793 /* Check if authentication protocol matches. */ 794 /* Check if authentication protocol matches. */
794 if (proto_len != strlen(x11_saved_proto) || 795 if (proto_len != strlen(x11_saved_proto) ||
795 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) { 796 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
796 debug2("X11 connection uses different authentication protocol."); 797 debug("X11 connection uses different authentication protocol.");
797 return -1; 798 return -1;
798 } 799 }
799 /* Check if authentication data matches our fake data. */ 800 /* Check if authentication data matches our fake data. */
800 if (data_len != x11_fake_data_len || 801 if (data_len != x11_fake_data_len ||
801 memcmp(ucp + 12 + ((proto_len + 3) & ~3), 802 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
802 x11_fake_data, x11_fake_data_len) != 0) { 803 x11_fake_data, x11_fake_data_len) != 0) {
803 debug2("X11 auth data does not match fake data."); 804 debug("X11 auth data does not match fake data.");
804 return -1; 805 return -1;
805 } 806 }
806 /* Check fake data length */ 807 /* Check fake data length */
@@ -833,7 +834,7 @@ channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
833 * We have received an X11 connection that has bad 834 * We have received an X11 connection that has bad
834 * authentication information. 835 * authentication information.
835 */ 836 */
836 logit("X11 connection rejected because of wrong authentication."); 837 log("X11 connection rejected because of wrong authentication.");
837 buffer_clear(&c->input); 838 buffer_clear(&c->input);
838 buffer_clear(&c->output); 839 buffer_clear(&c->output);
839 channel_close_fd(&c->sock); 840 channel_close_fd(&c->sock);
@@ -856,8 +857,8 @@ channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
856 c->type = SSH_CHANNEL_OPEN; 857 c->type = SSH_CHANNEL_OPEN;
857 channel_pre_open(c, readset, writeset); 858 channel_pre_open(c, readset, writeset);
858 } else if (ret == -1) { 859 } else if (ret == -1) {
859 logit("X11 connection rejected because of wrong authentication."); 860 log("X11 connection rejected because of wrong authentication.");
860 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate); 861 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
861 chan_read_failed(c); 862 chan_read_failed(c);
862 buffer_clear(&c->input); 863 buffer_clear(&c->input);
863 chan_ibuf_empty(c); 864 chan_ibuf_empty(c);
@@ -867,7 +868,7 @@ channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
867 chan_write_failed(c); 868 chan_write_failed(c);
868 else 869 else
869 c->type = SSH_CHANNEL_OPEN; 870 c->type = SSH_CHANNEL_OPEN;
870 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate); 871 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
871 } 872 }
872} 873}
873 874
@@ -925,7 +926,7 @@ channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
925 strlcpy(c->path, host, sizeof(c->path)); 926 strlcpy(c->path, host, sizeof(c->path));
926 c->host_port = ntohs(s4_req.dest_port); 927 c->host_port = ntohs(s4_req.dest_port);
927 928
928 debug2("channel %d: dynamic request: socks4 host %s port %u command %u", 929 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
929 c->self, host, c->host_port, s4_req.command); 930 c->self, host, c->host_port, s4_req.command);
930 931
931 if (s4_req.command != 1) { 932 if (s4_req.command != 1) {
@@ -941,117 +942,6 @@ channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
941 return 1; 942 return 1;
942} 943}
943 944
944/* try to decode a socks5 header */
945#define SSH_SOCKS5_AUTHDONE 0x1000
946#define SSH_SOCKS5_NOAUTH 0x00
947#define SSH_SOCKS5_IPV4 0x01
948#define SSH_SOCKS5_DOMAIN 0x03
949#define SSH_SOCKS5_IPV6 0x04
950#define SSH_SOCKS5_CONNECT 0x01
951#define SSH_SOCKS5_SUCCESS 0x00
952
953static int
954channel_decode_socks5(Channel *c, fd_set * readset, fd_set * writeset)
955{
956 struct {
957 u_int8_t version;
958 u_int8_t command;
959 u_int8_t reserved;
960 u_int8_t atyp;
961 } s5_req, s5_rsp;
962 u_int16_t dest_port;
963 u_char *p, dest_addr[255+1];
964 int i, have, found, nmethods, addrlen, af;
965
966 debug2("channel %d: decode socks5", c->self);
967 p = buffer_ptr(&c->input);
968 if (p[0] != 0x05)
969 return -1;
970 have = buffer_len(&c->input);
971 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
972 /* format: ver | nmethods | methods */
973 if (have < 2)
974 return 0;
975 nmethods = p[1];
976 if (have < nmethods + 2)
977 return 0;
978 /* look for method: "NO AUTHENTICATION REQUIRED" */
979 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
980 if (p[i] == SSH_SOCKS5_NOAUTH ) {
981 found = 1;
982 break;
983 }
984 }
985 if (!found) {
986 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
987 c->self);
988 return -1;
989 }
990 buffer_consume(&c->input, nmethods + 2);
991 buffer_put_char(&c->output, 0x05); /* version */
992 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
993 FD_SET(c->sock, writeset);
994 c->flags |= SSH_SOCKS5_AUTHDONE;
995 debug2("channel %d: socks5 auth done", c->self);
996 return 0; /* need more */
997 }
998 debug2("channel %d: socks5 post auth", c->self);
999 if (have < sizeof(s5_req)+1)
1000 return 0; /* need more */
1001 memcpy((char *)&s5_req, p, sizeof(s5_req));
1002 if (s5_req.version != 0x05 ||
1003 s5_req.command != SSH_SOCKS5_CONNECT ||
1004 s5_req.reserved != 0x00) {
1005 debug2("channel %d: only socks5 connect supported", c->self);
1006 return -1;
1007 }
1008 switch(s5_req.atyp){
1009 case SSH_SOCKS5_IPV4:
1010 addrlen = 4;
1011 af = AF_INET;
1012 break;
1013 case SSH_SOCKS5_DOMAIN:
1014 addrlen = p[sizeof(s5_req)];
1015 af = -1;
1016 break;
1017 case SSH_SOCKS5_IPV6:
1018 addrlen = 16;
1019 af = AF_INET6;
1020 break;
1021 default:
1022 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
1023 return -1;
1024 }
1025 if (have < 4 + addrlen + 2)
1026 return 0;
1027 buffer_consume(&c->input, sizeof(s5_req));
1028 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1029 buffer_consume(&c->input, 1); /* host string length */
1030 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1031 buffer_get(&c->input, (char *)&dest_port, 2);
1032 dest_addr[addrlen] = '\0';
1033 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1034 strlcpy(c->path, dest_addr, sizeof(c->path));
1035 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1036 return -1;
1037 c->host_port = ntohs(dest_port);
1038
1039 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1040 c->self, c->path, c->host_port, s5_req.command);
1041
1042 s5_rsp.version = 0x05;
1043 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1044 s5_rsp.reserved = 0; /* ignored */
1045 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1046 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1047 dest_port = 0; /* ignored */
1048
1049 buffer_append(&c->output, (char *)&s5_rsp, sizeof(s5_rsp));
1050 buffer_append(&c->output, (char *)&dest_addr, sizeof(struct in_addr));
1051 buffer_append(&c->output, (char *)&dest_port, sizeof(dest_port));
1052 return 1;
1053}
1054
1055/* dynamic port forwarding */ 945/* dynamic port forwarding */
1056static void 946static void
1057channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset) 947channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
@@ -1064,7 +954,7 @@ channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
1064 debug2("channel %d: pre_dynamic: have %d", c->self, have); 954 debug2("channel %d: pre_dynamic: have %d", c->self, have);
1065 /* buffer_dump(&c->input); */ 955 /* buffer_dump(&c->input); */
1066 /* check if the fixed size part of the packet is in buffer. */ 956 /* check if the fixed size part of the packet is in buffer. */
1067 if (have < 3) { 957 if (have < 4) {
1068 /* need more */ 958 /* need more */
1069 FD_SET(c->sock, readset); 959 FD_SET(c->sock, readset);
1070 return; 960 return;
@@ -1075,9 +965,6 @@ channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
1075 case 0x04: 965 case 0x04:
1076 ret = channel_decode_socks4(c, readset, writeset); 966 ret = channel_decode_socks4(c, readset, writeset);
1077 break; 967 break;
1078 case 0x05:
1079 ret = channel_decode_socks5(c, readset, writeset);
1080 break;
1081 default: 968 default:
1082 ret = -1; 969 ret = -1;
1083 break; 970 break;
@@ -1111,7 +998,7 @@ channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1111 addrlen = sizeof(addr); 998 addrlen = sizeof(addr);
1112 newsock = accept(c->sock, &addr, &addrlen); 999 newsock = accept(c->sock, &addr, &addrlen);
1113 if (c->single_connection) { 1000 if (c->single_connection) {
1114 debug2("single_connection: closing X11 listener."); 1001 debug("single_connection: closing X11 listener.");
1115 channel_close_fd(&c->sock); 1002 channel_close_fd(&c->sock);
1116 chan_mark_dead(c); 1003 chan_mark_dead(c);
1117 } 1004 }
@@ -1127,7 +1014,8 @@ channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1127 1014
1128 nc = channel_new("accepted x11 socket", 1015 nc = channel_new("accepted x11 socket",
1129 SSH_CHANNEL_OPENING, newsock, newsock, -1, 1016 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1130 c->local_window_max, c->local_maxpacket, 0, buf, 1); 1017 c->local_window_max, c->local_maxpacket,
1018 0, xstrdup(buf), 1);
1131 if (compat20) { 1019 if (compat20) {
1132 packet_start(SSH2_MSG_CHANNEL_OPEN); 1020 packet_start(SSH2_MSG_CHANNEL_OPEN);
1133 packet_put_cstring("x11"); 1021 packet_put_cstring("x11");
@@ -1137,7 +1025,7 @@ channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1137 /* originator ipaddr and port */ 1025 /* originator ipaddr and port */
1138 packet_put_cstring(remote_ipaddr); 1026 packet_put_cstring(remote_ipaddr);
1139 if (datafellows & SSH_BUG_X11FWD) { 1027 if (datafellows & SSH_BUG_X11FWD) {
1140 debug2("ssh2 x11 bug compat mode"); 1028 debug("ssh2 x11 bug compat mode");
1141 } else { 1029 } else {
1142 packet_put_int(remote_port); 1030 packet_put_int(remote_port);
1143 } 1031 }
@@ -1242,8 +1130,10 @@ channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1242 return; 1130 return;
1243 } 1131 }
1244 set_nodelay(newsock); 1132 set_nodelay(newsock);
1245 nc = channel_new(rtype, nextstate, newsock, newsock, -1, 1133 nc = channel_new(rtype,
1246 c->local_window_max, c->local_maxpacket, 0, rtype, 1); 1134 nextstate, newsock, newsock, -1,
1135 c->local_window_max, c->local_maxpacket,
1136 0, xstrdup(rtype), 1);
1247 nc->listening_port = c->listening_port; 1137 nc->listening_port = c->listening_port;
1248 nc->host_port = c->host_port; 1138 nc->host_port = c->host_port;
1249 strlcpy(nc->path, c->path, sizeof(nc->path)); 1139 strlcpy(nc->path, c->path, sizeof(nc->path));
@@ -1269,6 +1159,7 @@ static void
1269channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset) 1159channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1270{ 1160{
1271 Channel *nc; 1161 Channel *nc;
1162 char *name;
1272 int newsock; 1163 int newsock;
1273 struct sockaddr addr; 1164 struct sockaddr addr;
1274 socklen_t addrlen; 1165 socklen_t addrlen;
@@ -1280,10 +1171,11 @@ channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1280 error("accept from auth socket: %.100s", strerror(errno)); 1171 error("accept from auth socket: %.100s", strerror(errno));
1281 return; 1172 return;
1282 } 1173 }
1174 name = xstrdup("accepted auth socket");
1283 nc = channel_new("accepted auth socket", 1175 nc = channel_new("accepted auth socket",
1284 SSH_CHANNEL_OPENING, newsock, newsock, -1, 1176 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1285 c->local_window_max, c->local_maxpacket, 1177 c->local_window_max, c->local_maxpacket,
1286 0, "accepted auth socket", 1); 1178 0, name, 1);
1287 if (compat20) { 1179 if (compat20) {
1288 packet_start(SSH2_MSG_CHANNEL_OPEN); 1180 packet_start(SSH2_MSG_CHANNEL_OPEN);
1289 packet_put_cstring("auth-agent@openssh.com"); 1181 packet_put_cstring("auth-agent@openssh.com");
@@ -1356,16 +1248,16 @@ channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1356 if (len < 0 && (errno == EINTR || errno == EAGAIN)) 1248 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1357 return 1; 1249 return 1;
1358 if (len <= 0) { 1250 if (len <= 0) {
1359 debug2("channel %d: read<=0 rfd %d len %d", 1251 debug("channel %d: read<=0 rfd %d len %d",
1360 c->self, c->rfd, len); 1252 c->self, c->rfd, len);
1361 if (c->type != SSH_CHANNEL_OPEN) { 1253 if (c->type != SSH_CHANNEL_OPEN) {
1362 debug2("channel %d: not open", c->self); 1254 debug("channel %d: not open", c->self);
1363 chan_mark_dead(c); 1255 chan_mark_dead(c);
1364 return -1; 1256 return -1;
1365 } else if (compat13) { 1257 } else if (compat13) {
1366 buffer_clear(&c->output); 1258 buffer_clear(&c->output);
1367 c->type = SSH_CHANNEL_INPUT_DRAINING; 1259 c->type = SSH_CHANNEL_INPUT_DRAINING;
1368 debug2("channel %d: input draining.", c->self); 1260 debug("channel %d: input draining.", c->self);
1369 } else { 1261 } else {
1370 chan_read_failed(c); 1262 chan_read_failed(c);
1371 } 1263 }
@@ -1373,7 +1265,7 @@ channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1373 } 1265 }
1374 if (c->input_filter != NULL) { 1266 if (c->input_filter != NULL) {
1375 if (c->input_filter(c, buf, len) == -1) { 1267 if (c->input_filter(c, buf, len) == -1) {
1376 debug2("channel %d: filter stops", c->self); 1268 debug("channel %d: filter stops", c->self);
1377 chan_read_failed(c); 1269 chan_read_failed(c);
1378 } 1270 }
1379 } else { 1271 } else {
@@ -1406,12 +1298,12 @@ channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1406 return 1; 1298 return 1;
1407 if (len <= 0) { 1299 if (len <= 0) {
1408 if (c->type != SSH_CHANNEL_OPEN) { 1300 if (c->type != SSH_CHANNEL_OPEN) {
1409 debug2("channel %d: not open", c->self); 1301 debug("channel %d: not open", c->self);
1410 chan_mark_dead(c); 1302 chan_mark_dead(c);
1411 return -1; 1303 return -1;
1412 } else if (compat13) { 1304 } else if (compat13) {
1413 buffer_clear(&c->output); 1305 buffer_clear(&c->output);
1414 debug2("channel %d: input draining.", c->self); 1306 debug("channel %d: input draining.", c->self);
1415 c->type = SSH_CHANNEL_INPUT_DRAINING; 1307 c->type = SSH_CHANNEL_INPUT_DRAINING;
1416 } else { 1308 } else {
1417 chan_write_failed(c); 1309 chan_write_failed(c);
@@ -1618,16 +1510,16 @@ channel_garbage_collect(Channel *c)
1618 if (c->detach_user != NULL) { 1510 if (c->detach_user != NULL) {
1619 if (!chan_is_dead(c, 0)) 1511 if (!chan_is_dead(c, 0))
1620 return; 1512 return;
1621 debug2("channel %d: gc: notify user", c->self); 1513 debug("channel %d: gc: notify user", c->self);
1622 c->detach_user(c->self, NULL); 1514 c->detach_user(c->self, NULL);
1623 /* if we still have a callback */ 1515 /* if we still have a callback */
1624 if (c->detach_user != NULL) 1516 if (c->detach_user != NULL)
1625 return; 1517 return;
1626 debug2("channel %d: gc: user detached", c->self); 1518 debug("channel %d: gc: user detached", c->self);
1627 } 1519 }
1628 if (!chan_is_dead(c, 1)) 1520 if (!chan_is_dead(c, 1))
1629 return; 1521 return;
1630 debug2("channel %d: garbage collecting", c->self); 1522 debug("channel %d: garbage collecting", c->self);
1631 channel_free(c); 1523 channel_free(c);
1632} 1524}
1633 1525
@@ -1826,11 +1718,11 @@ channel_input_data(int type, u_int32_t seq, void *ctxt)
1826 1718
1827 if (compat20) { 1719 if (compat20) {
1828 if (data_len > c->local_maxpacket) { 1720 if (data_len > c->local_maxpacket) {
1829 logit("channel %d: rcvd big packet %d, maxpack %d", 1721 log("channel %d: rcvd big packet %d, maxpack %d",
1830 c->self, data_len, c->local_maxpacket); 1722 c->self, data_len, c->local_maxpacket);
1831 } 1723 }
1832 if (data_len > c->local_window) { 1724 if (data_len > c->local_window) {
1833 logit("channel %d: rcvd too much data %d, win %d", 1725 log("channel %d: rcvd too much data %d, win %d",
1834 c->self, data_len, c->local_window); 1726 c->self, data_len, c->local_window);
1835 xfree(data); 1727 xfree(data);
1836 return; 1728 return;
@@ -1857,7 +1749,7 @@ channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1857 if (c == NULL) 1749 if (c == NULL)
1858 packet_disconnect("Received extended_data for bad channel %d.", id); 1750 packet_disconnect("Received extended_data for bad channel %d.", id);
1859 if (c->type != SSH_CHANNEL_OPEN) { 1751 if (c->type != SSH_CHANNEL_OPEN) {
1860 logit("channel %d: ext data for non open", id); 1752 log("channel %d: ext data for non open", id);
1861 return; 1753 return;
1862 } 1754 }
1863 if (c->flags & CHAN_EOF_RCVD) { 1755 if (c->flags & CHAN_EOF_RCVD) {
@@ -1871,13 +1763,13 @@ channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
1871 if (c->efd == -1 || 1763 if (c->efd == -1 ||
1872 c->extended_usage != CHAN_EXTENDED_WRITE || 1764 c->extended_usage != CHAN_EXTENDED_WRITE ||
1873 tcode != SSH2_EXTENDED_DATA_STDERR) { 1765 tcode != SSH2_EXTENDED_DATA_STDERR) {
1874 logit("channel %d: bad ext data", c->self); 1766 log("channel %d: bad ext data", c->self);
1875 return; 1767 return;
1876 } 1768 }
1877 data = packet_get_string(&data_len); 1769 data = packet_get_string(&data_len);
1878 packet_check_eom(); 1770 packet_check_eom();
1879 if (data_len > c->local_window) { 1771 if (data_len > c->local_window) {
1880 logit("channel %d: rcvd too much extended_data %d, win %d", 1772 log("channel %d: rcvd too much extended_data %d, win %d",
1881 c->self, data_len, c->local_window); 1773 c->self, data_len, c->local_window);
1882 xfree(data); 1774 xfree(data);
1883 return; 1775 return;
@@ -2002,7 +1894,7 @@ channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
2002 c->confirm(c->self, NULL); 1894 c->confirm(c->self, NULL);
2003 debug2("callback done"); 1895 debug2("callback done");
2004 } 1896 }
2005 debug2("channel %d: open confirm rwindow %u rmax %u", c->self, 1897 debug("channel %d: open confirm rwindow %u rmax %u", c->self,
2006 c->remote_window, c->remote_maxpacket); 1898 c->remote_window, c->remote_maxpacket);
2007 } 1899 }
2008 packet_check_eom(); 1900 packet_check_eom();
@@ -2043,7 +1935,7 @@ channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
2043 msg = packet_get_string(NULL); 1935 msg = packet_get_string(NULL);
2044 lang = packet_get_string(NULL); 1936 lang = packet_get_string(NULL);
2045 } 1937 }
2046 logit("channel %d: open failed: %s%s%s", id, 1938 log("channel %d: open failed: %s%s%s", id,
2047 reason2txt(reason), msg ? ": ": "", msg ? msg : ""); 1939 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2048 if (msg != NULL) 1940 if (msg != NULL)
2049 xfree(msg); 1941 xfree(msg);
@@ -2070,7 +1962,7 @@ channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
2070 c = channel_lookup(id); 1962 c = channel_lookup(id);
2071 1963
2072 if (c == NULL || c->type != SSH_CHANNEL_OPEN) { 1964 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
2073 logit("Received window adjust for " 1965 log("Received window adjust for "
2074 "non-open channel %d.", id); 1966 "non-open channel %d.", id);
2075 return; 1967 return;
2076 } 1968 }
@@ -2105,8 +1997,8 @@ channel_input_port_open(int type, u_int32_t seq, void *ctxt)
2105 originator_string, 1); 1997 originator_string, 1);
2106 c->remote_id = remote_id; 1998 c->remote_id = remote_id;
2107 } 1999 }
2108 xfree(originator_string);
2109 if (c == NULL) { 2000 if (c == NULL) {
2001 xfree(originator_string);
2110 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); 2002 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2111 packet_put_int(remote_id); 2003 packet_put_int(remote_id);
2112 packet_send(); 2004 packet_send();
@@ -2167,7 +2059,7 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
2167 continue; 2059 continue;
2168 } 2060 }
2169 /* Create a port to listen for the host. */ 2061 /* Create a port to listen for the host. */
2170 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 2062 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2171 if (sock < 0) { 2063 if (sock < 0) {
2172 /* this is no error since kernel may not support ipv6 */ 2064 /* this is no error since kernel may not support ipv6 */
2173 verbose("socket: %.100s", strerror(errno)); 2065 verbose("socket: %.100s", strerror(errno));
@@ -2203,7 +2095,7 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
2203 /* Allocate a channel number for the socket. */ 2095 /* Allocate a channel number for the socket. */
2204 c = channel_new("port listener", type, sock, sock, -1, 2096 c = channel_new("port listener", type, sock, sock, -1,
2205 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 2097 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
2206 0, "port listener", 1); 2098 0, xstrdup("port listener"), 1);
2207 strlcpy(c->path, host, sizeof(c->path)); 2099 strlcpy(c->path, host, sizeof(c->path));
2208 c->host_port = port_to_connect; 2100 c->host_port = port_to_connect;
2209 c->listening_port = listen_port; 2101 c->listening_port = listen_port;
@@ -2276,7 +2168,7 @@ channel_request_remote_forwarding(u_short listen_port,
2276 success = 1; 2168 success = 1;
2277 break; 2169 break;
2278 case SSH_SMSG_FAILURE: 2170 case SSH_SMSG_FAILURE:
2279 logit("Warning: Server denied remote port forwarding."); 2171 log("Warning: Server denied remote port forwarding.");
2280 break; 2172 break;
2281 default: 2173 default:
2282 /* Unknown packet */ 2174 /* Unknown packet */
@@ -2315,13 +2207,9 @@ channel_input_port_forward_request(int is_root, int gateway_ports)
2315 * privileged port. 2207 * privileged port.
2316 */ 2208 */
2317 if (port < IPPORT_RESERVED && !is_root) 2209 if (port < IPPORT_RESERVED && !is_root)
2318 packet_disconnect( 2210 packet_disconnect("Requested forwarding of port %d but user is not root.",
2319 "Requested forwarding of port %d but user is not root.", 2211 port);
2320 port);
2321 if (host_port == 0)
2322 packet_disconnect("Dynamic forwarding denied.");
2323#endif 2212#endif
2324
2325 /* Initiate forwarding */ 2213 /* Initiate forwarding */
2326 channel_setup_local_fwd_listener(port, hostname, host_port, gateway_ports); 2214 channel_setup_local_fwd_listener(port, hostname, host_port, gateway_ports);
2327 2215
@@ -2393,7 +2281,7 @@ connect_to(const char *host, u_short port)
2393 error("connect_to: getnameinfo failed"); 2281 error("connect_to: getnameinfo failed");
2394 continue; 2282 continue;
2395 } 2283 }
2396 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 2284 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2397 if (sock < 0) { 2285 if (sock < 0) {
2398 if (ai->ai_next == NULL) 2286 if (ai->ai_next == NULL)
2399 error("socket: %.100s", strerror(errno)); 2287 error("socket: %.100s", strerror(errno));
@@ -2453,7 +2341,7 @@ channel_connect_to(const char *host, u_short port)
2453 2341
2454 } 2342 }
2455 if (!permit) { 2343 if (!permit) {
2456 logit("Received request to connect to host %.100s port %d, " 2344 log("Received request to connect to host %.100s port %d, "
2457 "but the request was denied.", host, port); 2345 "but the request was denied.", host, port);
2458 return -1; 2346 return -1;
2459 } 2347 }
@@ -2494,8 +2382,7 @@ x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
2494 for (ai = aitop; ai; ai = ai->ai_next) { 2382 for (ai = aitop; ai; ai = ai->ai_next) {
2495 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) 2383 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2496 continue; 2384 continue;
2497 sock = socket(ai->ai_family, ai->ai_socktype, 2385 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2498 ai->ai_protocol);
2499 if (sock < 0) { 2386 if (sock < 0) {
2500 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) { 2387 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
2501 error("socket: %.100s", strerror(errno)); 2388 error("socket: %.100s", strerror(errno));
@@ -2514,7 +2401,7 @@ x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
2514 } 2401 }
2515#endif 2402#endif
2516 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { 2403 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2517 debug2("bind port %d: %.100s", port, strerror(errno)); 2404 debug("bind port %d: %.100s", port, strerror(errno));
2518 close(sock); 2405 close(sock);
2519 2406
2520 if (ai->ai_next) 2407 if (ai->ai_next)
@@ -2563,7 +2450,7 @@ x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
2563 nc = channel_new("x11 listener", 2450 nc = channel_new("x11 listener",
2564 SSH_CHANNEL_X11_LISTENER, sock, sock, -1, 2451 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2565 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 2452 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2566 0, "X11 inet listener", 1); 2453 0, xstrdup("X11 inet listener"), 1);
2567 nc->single_connection = single_connection; 2454 nc->single_connection = single_connection;
2568 } 2455 }
2569 2456
@@ -2661,14 +2548,14 @@ x11_connect_display(void)
2661 } 2548 }
2662 for (ai = aitop; ai; ai = ai->ai_next) { 2549 for (ai = aitop; ai; ai = ai->ai_next) {
2663 /* Create a socket. */ 2550 /* Create a socket. */
2664 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); 2551 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2665 if (sock < 0) { 2552 if (sock < 0) {
2666 debug2("socket: %.100s", strerror(errno)); 2553 debug("socket: %.100s", strerror(errno));
2667 continue; 2554 continue;
2668 } 2555 }
2669 /* Connect it to the display. */ 2556 /* Connect it to the display. */
2670 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { 2557 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2671 debug2("connect %.100s port %d: %.100s", buf, 2558 debug("connect %.100s port %d: %.100s", buf,
2672 6000 + display_number, strerror(errno)); 2559 6000 + display_number, strerror(errno));
2673 close(sock); 2560 close(sock);
2674 continue; 2561 continue;
@@ -2720,11 +2607,11 @@ x11_input_open(int type, u_int32_t seq, void *ctxt)
2720 c->remote_id = remote_id; 2607 c->remote_id = remote_id;
2721 c->force_drain = 1; 2608 c->force_drain = 1;
2722 } 2609 }
2723 xfree(remote_host);
2724 if (c == NULL) { 2610 if (c == NULL) {
2725 /* Send refusal to the remote host. */ 2611 /* Send refusal to the remote host. */
2726 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); 2612 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2727 packet_put_int(remote_id); 2613 packet_put_int(remote_id);
2614 xfree(remote_host);
2728 } else { 2615 } else {
2729 /* Send a confirmation to the remote host. */ 2616 /* Send a confirmation to the remote host. */
2730 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 2617 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
@@ -2846,6 +2733,7 @@ auth_input_open_request(int type, u_int32_t seq, void *ctxt)
2846{ 2733{
2847 Channel *c = NULL; 2734 Channel *c = NULL;
2848 int remote_id, sock; 2735 int remote_id, sock;
2736 char *name;
2849 2737
2850 /* Read the remote channel number from the message. */ 2738 /* Read the remote channel number from the message. */
2851 remote_id = packet_get_int(); 2739 remote_id = packet_get_int();
@@ -2864,8 +2752,9 @@ auth_input_open_request(int type, u_int32_t seq, void *ctxt)
2864 * agent. 2752 * agent.
2865 */ 2753 */
2866 if (sock >= 0) { 2754 if (sock >= 0) {
2755 name = xstrdup("authentication agent connection");
2867 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, 2756 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2868 -1, 0, 0, 0, "authentication agent connection", 1); 2757 -1, 0, 0, 0, name, 1);
2869 c->remote_id = remote_id; 2758 c->remote_id = remote_id;
2870 c->force_drain = 1; 2759 c->force_drain = 1;
2871 } 2760 }