summaryrefslogtreecommitdiff
path: root/channels.c
diff options
context:
space:
mode:
Diffstat (limited to 'channels.c')
-rw-r--r--channels.c94
1 files changed, 52 insertions, 42 deletions
diff --git a/channels.c b/channels.c
index 957b4a428..5f3b0d113 100644
--- a/channels.c
+++ b/channels.c
@@ -1,23 +1,23 @@
1/* 1/*
2 * 2 *
3 * channels.c 3 * channels.c
4 * 4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 * 6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved 8 * All rights reserved
9 * 9 *
10 * Created: Fri Mar 24 16:35:24 1995 ylo 10 * Created: Fri Mar 24 16:35:24 1995 ylo
11 * 11 *
12 * This file contains functions for generic socket connection forwarding. 12 * This file contains functions for generic socket connection forwarding.
13 * There is also code for initiating connection forwarding for X11 connections, 13 * There is also code for initiating connection forwarding for X11 connections,
14 * arbitrary tcp/ip connections, and the authentication agent connection. 14 * arbitrary tcp/ip connections, and the authentication agent connection.
15 * 15 *
16 * SSH2 support added by Markus Friedl. 16 * SSH2 support added by Markus Friedl.
17 */ 17 */
18 18
19#include "includes.h" 19#include "includes.h"
20RCSID("$Id: channels.c,v 1.24 2000/04/12 10:17:38 damien Exp $"); 20RCSID("$Id: channels.c,v 1.25 2000/04/16 01:18:41 damien Exp $");
21 21
22#include "ssh.h" 22#include "ssh.h"
23#include "packet.h" 23#include "packet.h"
@@ -109,7 +109,7 @@ static int have_hostname_in_open = 0;
109 109
110/* Sets specific protocol options. */ 110/* Sets specific protocol options. */
111 111
112void 112void
113channel_set_options(int hostname_in_open) 113channel_set_options(int hostname_in_open)
114{ 114{
115 have_hostname_in_open = hostname_in_open; 115 have_hostname_in_open = hostname_in_open;
@@ -121,7 +121,7 @@ channel_set_options(int hostname_in_open)
121 * and the server has no way to know but to trust the client anyway. 121 * and the server has no way to know but to trust the client anyway.
122 */ 122 */
123 123
124void 124void
125channel_permit_all_opens() 125channel_permit_all_opens()
126{ 126{
127 all_opens_permitted = 1; 127 all_opens_permitted = 1;
@@ -150,7 +150,7 @@ channel_lookup(int id)
150 * remote_name to be freed. 150 * remote_name to be freed.
151 */ 151 */
152 152
153int 153int
154channel_new(char *ctype, int type, int rfd, int wfd, int efd, 154channel_new(char *ctype, int type, int rfd, int wfd, int efd,
155 int window, int maxpack, int extended_usage, char *remote_name) 155 int window, int maxpack, int extended_usage, char *remote_name)
156{ 156{
@@ -226,7 +226,7 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
226 debug("channel %d: new [%s]", found, remote_name); 226 debug("channel %d: new [%s]", found, remote_name);
227 return found; 227 return found;
228} 228}
229int 229int
230channel_allocate(int type, int sock, char *remote_name) 230channel_allocate(int type, int sock, char *remote_name)
231{ 231{
232 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name); 232 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
@@ -234,7 +234,7 @@ channel_allocate(int type, int sock, char *remote_name)
234 234
235/* Free the channel and close its socket. */ 235/* Free the channel and close its socket. */
236 236
237void 237void
238channel_free(int id) 238channel_free(int id)
239{ 239{
240 Channel *c = channel_lookup(id); 240 Channel *c = channel_lookup(id);
@@ -361,7 +361,7 @@ channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
361{ 361{
362 if (buffer_len(&c->output) == 0) 362 if (buffer_len(&c->output) == 0)
363 channel_free(c->self); 363 channel_free(c->self);
364 else 364 else
365 FD_SET(c->sock, writeset); 365 FD_SET(c->sock, writeset);
366} 366}
367 367
@@ -540,8 +540,10 @@ channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
540 packet_put_int(newch); 540 packet_put_int(newch);
541 packet_put_int(c->local_window_max); 541 packet_put_int(c->local_window_max);
542 packet_put_int(c->local_maxpacket); 542 packet_put_int(c->local_maxpacket);
543 /* target host and port */
543 packet_put_string(c->path, strlen(c->path)); 544 packet_put_string(c->path, strlen(c->path));
544 packet_put_int(c->host_port); 545 packet_put_int(c->host_port);
546 /* originator host and port */
545 packet_put_cstring(remote_hostname); 547 packet_put_cstring(remote_hostname);
546 packet_put_int(remote_port); 548 packet_put_int(remote_port);
547 packet_send(); 549 packet_send();
@@ -782,7 +784,7 @@ channel_handler_init(void)
782 channel_handler_init_15(); 784 channel_handler_init_15();
783} 785}
784 786
785void 787void
786channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset) 788channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
787{ 789{
788 static int did_init = 0; 790 static int did_init = 0;
@@ -804,13 +806,13 @@ channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
804 } 806 }
805} 807}
806 808
807void 809void
808channel_prepare_select(fd_set * readset, fd_set * writeset) 810channel_prepare_select(fd_set * readset, fd_set * writeset)
809{ 811{
810 channel_handler(channel_pre, readset, writeset); 812 channel_handler(channel_pre, readset, writeset);
811} 813}
812 814
813void 815void
814channel_after_select(fd_set * readset, fd_set * writeset) 816channel_after_select(fd_set * readset, fd_set * writeset)
815{ 817{
816 channel_handler(channel_post, readset, writeset); 818 channel_handler(channel_post, readset, writeset);
@@ -818,7 +820,7 @@ channel_after_select(fd_set * readset, fd_set * writeset)
818 820
819/* If there is data to send to the connection, send some of it now. */ 821/* If there is data to send to the connection, send some of it now. */
820 822
821void 823void
822channel_output_poll() 824channel_output_poll()
823{ 825{
824 int len, i; 826 int len, i;
@@ -909,7 +911,7 @@ channel_output_poll()
909 * still there. 911 * still there.
910 */ 912 */
911 913
912void 914void
913channel_input_data(int type, int plen) 915channel_input_data(int type, int plen)
914{ 916{
915 int id; 917 int id;
@@ -934,6 +936,7 @@ channel_input_data(int type, int plen)
934 936
935 /* Get the data. */ 937 /* Get the data. */
936 data = packet_get_string(&data_len); 938 data = packet_get_string(&data_len);
939 packet_done();
937 940
938 if (compat20){ 941 if (compat20){
939 if (data_len > c->local_maxpacket) { 942 if (data_len > c->local_maxpacket) {
@@ -953,7 +956,7 @@ channel_input_data(int type, int plen)
953 buffer_append(&c->output, data, data_len); 956 buffer_append(&c->output, data, data_len);
954 xfree(data); 957 xfree(data);
955} 958}
956void 959void
957channel_input_extended_data(int type, int plen) 960channel_input_extended_data(int type, int plen)
958{ 961{
959 int id; 962 int id;
@@ -980,6 +983,7 @@ channel_input_extended_data(int type, int plen)
980 return; 983 return;
981 } 984 }
982 data = packet_get_string(&data_len); 985 data = packet_get_string(&data_len);
986 packet_done();
983 if (data_len > c->local_window) { 987 if (data_len > c->local_window) {
984 log("channel %d: rcvd too much extended_data %d, win %d", 988 log("channel %d: rcvd too much extended_data %d, win %d",
985 c->self, data_len, c->local_window); 989 c->self, data_len, c->local_window);
@@ -998,7 +1002,7 @@ channel_input_extended_data(int type, int plen)
998 * more channel is overfull. 1002 * more channel is overfull.
999 */ 1003 */
1000 1004
1001int 1005int
1002channel_not_very_much_buffered_data() 1006channel_not_very_much_buffered_data()
1003{ 1007{
1004 unsigned int i; 1008 unsigned int i;
@@ -1022,7 +1026,7 @@ channel_not_very_much_buffered_data()
1022 return 1; 1026 return 1;
1023} 1027}
1024 1028
1025void 1029void
1026channel_input_ieof(int type, int plen) 1030channel_input_ieof(int type, int plen)
1027{ 1031{
1028 int id; 1032 int id;
@@ -1037,7 +1041,7 @@ channel_input_ieof(int type, int plen)
1037 chan_rcvd_ieof(c); 1041 chan_rcvd_ieof(c);
1038} 1042}
1039 1043
1040void 1044void
1041channel_input_close(int type, int plen) 1045channel_input_close(int type, int plen)
1042{ 1046{
1043 int id; 1047 int id;
@@ -1076,7 +1080,7 @@ channel_input_close(int type, int plen)
1076} 1080}
1077 1081
1078/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */ 1082/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
1079void 1083void
1080channel_input_oclose(int type, int plen) 1084channel_input_oclose(int type, int plen)
1081{ 1085{
1082 int id = packet_get_int(); 1086 int id = packet_get_int();
@@ -1087,12 +1091,13 @@ channel_input_oclose(int type, int plen)
1087 chan_rcvd_oclose(c); 1091 chan_rcvd_oclose(c);
1088} 1092}
1089 1093
1090void 1094void
1091channel_input_close_confirmation(int type, int plen) 1095channel_input_close_confirmation(int type, int plen)
1092{ 1096{
1093 int id = packet_get_int(); 1097 int id = packet_get_int();
1094 Channel *c = channel_lookup(id); 1098 Channel *c = channel_lookup(id);
1095 1099
1100 packet_done();
1096 if (c == NULL) 1101 if (c == NULL)
1097 packet_disconnect("Received close confirmation for " 1102 packet_disconnect("Received close confirmation for "
1098 "out-of-range channel %d.", id); 1103 "out-of-range channel %d.", id);
@@ -1102,7 +1107,7 @@ channel_input_close_confirmation(int type, int plen)
1102 channel_free(c->self); 1107 channel_free(c->self);
1103} 1108}
1104 1109
1105void 1110void
1106channel_input_open_confirmation(int type, int plen) 1111channel_input_open_confirmation(int type, int plen)
1107{ 1112{
1108 int id, remote_id; 1113 int id, remote_id;
@@ -1125,6 +1130,7 @@ channel_input_open_confirmation(int type, int plen)
1125 if (compat20) { 1130 if (compat20) {
1126 c->remote_window = packet_get_int(); 1131 c->remote_window = packet_get_int();
1127 c->remote_maxpacket = packet_get_int(); 1132 c->remote_maxpacket = packet_get_int();
1133 packet_done();
1128 if (c->cb_fn != NULL && c->cb_event == type) { 1134 if (c->cb_fn != NULL && c->cb_event == type) {
1129 debug("callback start"); 1135 debug("callback start");
1130 c->cb_fn(c->self, c->cb_arg); 1136 c->cb_fn(c->self, c->cb_arg);
@@ -1135,7 +1141,7 @@ channel_input_open_confirmation(int type, int plen)
1135 } 1141 }
1136} 1142}
1137 1143
1138void 1144void
1139channel_input_open_failure(int type, int plen) 1145channel_input_open_failure(int type, int plen)
1140{ 1146{
1141 int id; 1147 int id;
@@ -1153,8 +1159,11 @@ channel_input_open_failure(int type, int plen)
1153 if (compat20) { 1159 if (compat20) {
1154 int reason = packet_get_int(); 1160 int reason = packet_get_int();
1155 char *msg = packet_get_string(NULL); 1161 char *msg = packet_get_string(NULL);
1162 char *lang = packet_get_string(NULL);
1156 log("channel_open_failure: %d: reason %d: %s", id, reason, msg); 1163 log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
1164 packet_done();
1157 xfree(msg); 1165 xfree(msg);
1166 xfree(lang);
1158 } 1167 }
1159 /* Free the channel. This will also close the socket. */ 1168 /* Free the channel. This will also close the socket. */
1160 channel_free(id); 1169 channel_free(id);
@@ -1185,7 +1194,7 @@ debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
1185 } 1194 }
1186} 1195}
1187 1196
1188void 1197void
1189channel_input_window_adjust(int type, int plen) 1198channel_input_window_adjust(int type, int plen)
1190{ 1199{
1191 Channel *c; 1200 Channel *c;
@@ -1204,6 +1213,7 @@ channel_input_window_adjust(int type, int plen)
1204 return; 1213 return;
1205 } 1214 }
1206 adjust = packet_get_int(); 1215 adjust = packet_get_int();
1216 packet_done();
1207 debug("channel %d: rcvd adjust %d", id, adjust); 1217 debug("channel %d: rcvd adjust %d", id, adjust);
1208 c->remote_window += adjust; 1218 c->remote_window += adjust;
1209} 1219}
@@ -1213,7 +1223,7 @@ channel_input_window_adjust(int type, int plen)
1213 * might have. 1223 * might have.
1214 */ 1224 */
1215 1225
1216void 1226void
1217channel_stop_listening() 1227channel_stop_listening()
1218{ 1228{
1219 int i; 1229 int i;
@@ -1240,7 +1250,7 @@ channel_stop_listening()
1240 * descriptors after a fork. 1250 * descriptors after a fork.
1241 */ 1251 */
1242 1252
1243void 1253void
1244channel_close_all() 1254channel_close_all()
1245{ 1255{
1246 int i; 1256 int i;
@@ -1252,7 +1262,7 @@ channel_close_all()
1252 1262
1253/* Returns the maximum file descriptor number used by the channels. */ 1263/* Returns the maximum file descriptor number used by the channels. */
1254 1264
1255int 1265int
1256channel_max_fd() 1266channel_max_fd()
1257{ 1267{
1258 return channel_max_fd_value; 1268 return channel_max_fd_value;
@@ -1260,7 +1270,7 @@ channel_max_fd()
1260 1270
1261/* Returns true if any channel is still open. */ 1271/* Returns true if any channel is still open. */
1262 1272
1263int 1273int
1264channel_still_open() 1274channel_still_open()
1265{ 1275{
1266 unsigned int i; 1276 unsigned int i;
@@ -1347,7 +1357,7 @@ channel_open_message()
1347 * channel to host:port from remote side. 1357 * channel to host:port from remote side.
1348 */ 1358 */
1349 1359
1350void 1360void
1351channel_request_local_forwarding(u_short port, const char *host, 1361channel_request_local_forwarding(u_short port, const char *host,
1352 u_short host_port, int gateway_ports) 1362 u_short host_port, int gateway_ports)
1353{ 1363{
@@ -1435,7 +1445,7 @@ channel_request_local_forwarding(u_short port, const char *host,
1435 * the secure channel to host:port from local side. 1445 * the secure channel to host:port from local side.
1436 */ 1446 */
1437 1447
1438void 1448void
1439channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect, 1449channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
1440 u_short port_to_connect) 1450 u_short port_to_connect)
1441{ 1451{
@@ -1478,7 +1488,7 @@ channel_request_remote_forwarding(u_short listen_port, const char *host_to_conne
1478 * message if there was an error). This never returns if there was an error. 1488 * message if there was an error). This never returns if there was an error.
1479 */ 1489 */
1480 1490
1481void 1491void
1482channel_input_port_forward_request(int is_root) 1492channel_input_port_forward_request(int is_root)
1483{ 1493{
1484 u_short port, host_port; 1494 u_short port, host_port;
@@ -1562,7 +1572,7 @@ channel_connect_to(const char *host, u_short host_port)
1562 * or CHANNEL_OPEN_FAILURE. 1572 * or CHANNEL_OPEN_FAILURE.
1563 */ 1573 */
1564 1574
1565void 1575void
1566channel_input_port_open(int type, int plen) 1576channel_input_port_open(int type, int plen)
1567{ 1577{
1568 u_short host_port; 1578 u_short host_port;
@@ -1807,7 +1817,7 @@ connect_local_xsocket(unsigned int dnr)
1807 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. 1817 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
1808 */ 1818 */
1809 1819
1810void 1820void
1811x11_input_open(int type, int plen) 1821x11_input_open(int type, int plen)
1812{ 1822{
1813 int remote_channel, display_number, sock = 0, newch; 1823 int remote_channel, display_number, sock = 0, newch;
@@ -1911,7 +1921,7 @@ x11_input_open(int type, int plen)
1911 } 1921 }
1912 freeaddrinfo(aitop); 1922 freeaddrinfo(aitop);
1913 if (!ai) { 1923 if (!ai) {
1914 error("connect %.100s port %d: %.100s", buf, 6000 + display_number, 1924 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
1915 strerror(errno)); 1925 strerror(errno));
1916 goto fail; 1926 goto fail;
1917 } 1927 }
@@ -1945,7 +1955,7 @@ fail:
1945 * data, and enables authentication spoofing. 1955 * data, and enables authentication spoofing.
1946 */ 1956 */
1947 1957
1948void 1958void
1949x11_request_forwarding_with_spoofing(const char *proto, const char *data) 1959x11_request_forwarding_with_spoofing(const char *proto, const char *data)
1950{ 1960{
1951 unsigned int data_len = (unsigned int) strlen(data) / 2; 1961 unsigned int data_len = (unsigned int) strlen(data) / 2;
@@ -2003,7 +2013,7 @@ x11_request_forwarding_with_spoofing(const char *proto, const char *data)
2003 2013
2004/* Sends a message to the server to request authentication fd forwarding. */ 2014/* Sends a message to the server to request authentication fd forwarding. */
2005 2015
2006void 2016void
2007auth_request_forwarding() 2017auth_request_forwarding()
2008{ 2018{
2009 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING); 2019 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
@@ -2025,7 +2035,7 @@ auth_get_socket_name()
2025 2035
2026/* removes the agent forwarding socket */ 2036/* removes the agent forwarding socket */
2027 2037
2028void 2038void
2029cleanup_socket(void) 2039cleanup_socket(void)
2030{ 2040{
2031 remove(channel_forwarded_auth_socket_name); 2041 remove(channel_forwarded_auth_socket_name);
@@ -2037,7 +2047,7 @@ cleanup_socket(void)
2037 * This starts forwarding authentication requests. 2047 * This starts forwarding authentication requests.
2038 */ 2048 */
2039 2049
2040void 2050void
2041auth_input_request_forwarding(struct passwd * pw) 2051auth_input_request_forwarding(struct passwd * pw)
2042{ 2052{
2043 int sock, newch; 2053 int sock, newch;
@@ -2095,7 +2105,7 @@ auth_input_request_forwarding(struct passwd * pw)
2095 2105
2096/* This is called to process an SSH_SMSG_AGENT_OPEN message. */ 2106/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2097 2107
2098void 2108void
2099auth_input_open_request(int type, int plen) 2109auth_input_open_request(int type, int plen)
2100{ 2110{
2101 int remch, sock, newch; 2111 int remch, sock, newch;