version 1.72, 2000/10/27 07:48:22 |
version 1.73, 2000/11/06 23:04:55 |
|
|
struct sockaddr addr; |
struct sockaddr addr; |
int newsock, newch; |
int newsock, newch; |
socklen_t addrlen; |
socklen_t addrlen; |
char buf[1024], *remote_hostname; |
char buf[1024], *remote_hostname, *rtype; |
int remote_port; |
int remote_port; |
|
|
|
rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ? |
|
"forwarded-tcpip" : "direct-tcpip"; |
|
|
if (FD_ISSET(c->sock, readset)) { |
if (FD_ISSET(c->sock, readset)) { |
debug("Connection to port %d forwarding " |
debug("Connection to port %d forwarding " |
"to %.100s port %d requested.", |
"to %.100s port %d requested.", |
|
|
"connect from %.200s port %d", |
"connect from %.200s port %d", |
c->listening_port, c->path, c->host_port, |
c->listening_port, c->path, c->host_port, |
remote_hostname, remote_port); |
remote_hostname, remote_port); |
newch = channel_new("direct-tcpip", |
|
|
newch = channel_new(rtype, |
SSH_CHANNEL_OPENING, newsock, newsock, -1, |
SSH_CHANNEL_OPENING, newsock, newsock, -1, |
c->local_window_max, c->local_maxpacket, |
c->local_window_max, c->local_maxpacket, |
0, xstrdup(buf), 1); |
0, xstrdup(buf), 1); |
if (compat20) { |
if (compat20) { |
packet_start(SSH2_MSG_CHANNEL_OPEN); |
packet_start(SSH2_MSG_CHANNEL_OPEN); |
packet_put_cstring("direct-tcpip"); |
packet_put_cstring(rtype); |
packet_put_int(newch); |
packet_put_int(newch); |
packet_put_int(c->local_window_max); |
packet_put_int(c->local_window_max); |
packet_put_int(c->local_maxpacket); |
packet_put_int(c->local_maxpacket); |
/* target host and port */ |
if (c->type == SSH_CHANNEL_RPORT_LISTENER) { |
packet_put_string(c->path, strlen(c->path)); |
/* listen address, port */ |
packet_put_int(c->host_port); |
packet_put_string(c->path, strlen(c->path)); |
|
packet_put_int(c->listening_port); |
|
} else { |
|
/* target host, port */ |
|
packet_put_string(c->path, strlen(c->path)); |
|
packet_put_int(c->host_port); |
|
} |
/* originator host and port */ |
/* originator host and port */ |
packet_put_cstring(remote_hostname); |
packet_put_cstring(remote_hostname); |
packet_put_int(remote_port); |
packet_put_int(remote_port); |
|
|
error("accept from auth socket: %.100s", strerror(errno)); |
error("accept from auth socket: %.100s", strerror(errno)); |
return; |
return; |
} |
} |
newch = channel_allocate(SSH_CHANNEL_OPENING, newsock, |
newch = channel_new("accepted auth socket", |
xstrdup("accepted auth socket")); |
SSH_CHANNEL_OPENING, newsock, newsock, -1, |
packet_start(SSH_SMSG_AGENT_OPEN); |
c->local_window_max, c->local_maxpacket, |
packet_put_int(newch); |
0, xstrdup("accepted auth socket"), 1); |
|
if (compat20) { |
|
packet_start(SSH2_MSG_CHANNEL_OPEN); |
|
packet_put_cstring("auth-agent@openssh.com"); |
|
packet_put_int(newch); |
|
packet_put_int(c->local_window_max); |
|
packet_put_int(c->local_maxpacket); |
|
} else { |
|
packet_start(SSH_SMSG_AGENT_OPEN); |
|
packet_put_int(newch); |
|
} |
packet_send(); |
packet_send(); |
} |
} |
} |
} |
|
|
channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20; |
channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20; |
channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open; |
channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener; |
|
channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener; |
channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener; |
|
channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener; |
|
|
channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2; |
channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2; |
channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener; |
|
channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener; |
channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener; |
|
channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener; |
} |
} |
|
|
void |
void |
|
|
channel_free(i); |
channel_free(i); |
break; |
break; |
case SSH_CHANNEL_PORT_LISTENER: |
case SSH_CHANNEL_PORT_LISTENER: |
|
case SSH_CHANNEL_RPORT_LISTENER: |
case SSH_CHANNEL_X11_LISTENER: |
case SSH_CHANNEL_X11_LISTENER: |
close(channels[i].sock); |
close(channels[i].sock); |
channel_free(i); |
channel_free(i); |
|
|
case SSH_CHANNEL_FREE: |
case SSH_CHANNEL_FREE: |
case SSH_CHANNEL_X11_LISTENER: |
case SSH_CHANNEL_X11_LISTENER: |
case SSH_CHANNEL_PORT_LISTENER: |
case SSH_CHANNEL_PORT_LISTENER: |
|
case SSH_CHANNEL_RPORT_LISTENER: |
case SSH_CHANNEL_CLOSED: |
case SSH_CHANNEL_CLOSED: |
case SSH_CHANNEL_AUTH_SOCKET: |
case SSH_CHANNEL_AUTH_SOCKET: |
continue; |
continue; |
|
|
case SSH_CHANNEL_FREE: |
case SSH_CHANNEL_FREE: |
case SSH_CHANNEL_X11_LISTENER: |
case SSH_CHANNEL_X11_LISTENER: |
case SSH_CHANNEL_PORT_LISTENER: |
case SSH_CHANNEL_PORT_LISTENER: |
|
case SSH_CHANNEL_RPORT_LISTENER: |
case SSH_CHANNEL_CLOSED: |
case SSH_CHANNEL_CLOSED: |
case SSH_CHANNEL_AUTH_SOCKET: |
case SSH_CHANNEL_AUTH_SOCKET: |
continue; |
continue; |
|
|
* Initiate forwarding of connections to local port "port" through the secure |
* Initiate forwarding of connections to local port "port" through the secure |
* channel to host:port from remote side. |
* channel to host:port from remote side. |
*/ |
*/ |
|
void |
|
channel_request_local_forwarding(u_short listen_port, const char *host_to_connect, |
|
u_short port_to_connect, int gateway_ports) |
|
{ |
|
channel_request_forwarding( |
|
NULL, listen_port, |
|
host_to_connect, port_to_connect, |
|
gateway_ports, /*remote_fwd*/ 0); |
|
} |
|
|
|
/* |
|
* If 'remote_fwd' is true we have a '-R style' listener for protocol 2 |
|
* (SSH_CHANNEL_RPORT_LISTENER). |
|
*/ |
void |
void |
channel_request_local_forwarding(u_short port, const char *host, |
channel_request_forwarding( |
u_short host_port, int gateway_ports) |
const char *listen_address, u_short listen_port, |
|
const char *host_to_connect, u_short port_to_connect, |
|
int gateway_ports, int remote_fwd) |
{ |
{ |
int success, ch, sock, on = 1; |
int success, ch, sock, on = 1, ctype; |
struct addrinfo hints, *ai, *aitop; |
struct addrinfo hints, *ai, *aitop; |
char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
char ntop[NI_MAXHOST], strport[NI_MAXSERV]; |
|
const char *host; |
struct linger linger; |
struct linger linger; |
|
|
|
if (remote_fwd) { |
|
host = listen_address; |
|
ctype = SSH_CHANNEL_RPORT_LISTENER; |
|
} else { |
|
host = host_to_connect; |
|
ctype =SSH_CHANNEL_PORT_LISTENER; |
|
} |
|
|
if (strlen(host) > sizeof(channels[0].path) - 1) |
if (strlen(host) > sizeof(channels[0].path) - 1) |
packet_disconnect("Forward host name too long."); |
packet_disconnect("Forward host name too long."); |
|
|
|
/* XXX listen_address is currently ignored */ |
/* |
/* |
* getaddrinfo returns a loopback address if the hostname is |
* getaddrinfo returns a loopback address if the hostname is |
* set to NULL and hints.ai_flags is not AI_PASSIVE |
* set to NULL and hints.ai_flags is not AI_PASSIVE |
|
|
hints.ai_family = IPv4or6; |
hints.ai_family = IPv4or6; |
hints.ai_flags = gateway_ports ? AI_PASSIVE : 0; |
hints.ai_flags = gateway_ports ? AI_PASSIVE : 0; |
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = SOCK_STREAM; |
snprintf(strport, sizeof strport, "%d", port); |
snprintf(strport, sizeof strport, "%d", listen_port); |
if (getaddrinfo(NULL, strport, &hints, &aitop) != 0) |
if (getaddrinfo(NULL, strport, &hints, &aitop) != 0) |
packet_disconnect("getaddrinfo: fatal error"); |
packet_disconnect("getaddrinfo: fatal error"); |
|
|
|
|
continue; |
continue; |
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), |
strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { |
error("channel_request_local_forwarding: getnameinfo failed"); |
error("channel_request_forwarding: getnameinfo failed"); |
continue; |
continue; |
} |
} |
/* Create a port to listen for the host. */ |
/* Create a port to listen for the host. */ |
|
|
continue; |
continue; |
} |
} |
/* Allocate a channel number for the socket. */ |
/* Allocate a channel number for the socket. */ |
ch = channel_new( |
ch = channel_new("port listener", ctype, sock, sock, -1, |
"port listener", SSH_CHANNEL_PORT_LISTENER, |
|
sock, sock, -1, |
|
CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, |
0, xstrdup("port listener"), 1); |
0, xstrdup("port listener"), 1); |
strlcpy(channels[ch].path, host, sizeof(channels[ch].path)); |
strlcpy(channels[ch].path, host, sizeof(channels[ch].path)); |
channels[ch].host_port = host_port; |
channels[ch].host_port = port_to_connect; |
channels[ch].listening_port = port; |
channels[ch].listening_port = listen_port; |
success = 1; |
success = 1; |
} |
} |
if (success == 0) |
if (success == 0) |
packet_disconnect("cannot listen port: %d", port); |
packet_disconnect("cannot listen port: %d", listen_port); /*XXX ?disconnect? */ |
freeaddrinfo(aitop); |
freeaddrinfo(aitop); |
} |
} |
|
|
|
|
*/ |
*/ |
|
|
void |
void |
channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect, |
channel_request_remote_forwarding(u_short listen_port, |
u_short port_to_connect) |
const char *host_to_connect, u_short port_to_connect) |
{ |
{ |
int payload_len; |
int payload_len, type, success = 0; |
|
|
/* Record locally that connection to this host/port is permitted. */ |
/* Record locally that connection to this host/port is permitted. */ |
if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION) |
fatal("channel_request_remote_forwarding: too many forwards"); |
fatal("channel_request_remote_forwarding: too many forwards"); |
|
|
permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect); |
|
permitted_opens[num_permitted_opens].port_to_connect = port_to_connect; |
|
permitted_opens[num_permitted_opens].listen_port = listen_port; |
|
num_permitted_opens++; |
|
|
|
/* Send the forward request to the remote side. */ |
/* Send the forward request to the remote side. */ |
if (compat20) { |
if (compat20) { |
const char *address_to_bind = "0.0.0.0"; |
const char *address_to_bind = "0.0.0.0"; |
|
|
packet_put_char(0); /* boolean: want reply */ |
packet_put_char(0); /* boolean: want reply */ |
packet_put_cstring(address_to_bind); |
packet_put_cstring(address_to_bind); |
packet_put_int(listen_port); |
packet_put_int(listen_port); |
|
packet_send(); |
|
packet_write_wait(); |
|
/* Assume that server accepts the request */ |
|
success = 1; |
} else { |
} else { |
packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
packet_start(SSH_CMSG_PORT_FORWARD_REQUEST); |
packet_put_int(listen_port); |
packet_put_int(listen_port); |
|
|
packet_put_int(port_to_connect); |
packet_put_int(port_to_connect); |
packet_send(); |
packet_send(); |
packet_write_wait(); |
packet_write_wait(); |
/* |
|
* Wait for response from the remote side. It will send a disconnect |
/* Wait for response from the remote side. */ |
* message on failure, and we will never see it here. |
type = packet_read(&payload_len); |
*/ |
switch (type) { |
packet_read_expect(&payload_len, SSH_SMSG_SUCCESS); |
case SSH_SMSG_SUCCESS: |
|
success = 1; |
|
break; |
|
case SSH_SMSG_FAILURE: |
|
log("Warning: Server denied remote port forwarding."); |
|
break; |
|
default: |
|
/* Unknown packet */ |
|
packet_disconnect("Protocol error for port forward request:" |
|
"received packet type %d.", type); |
|
} |
} |
} |
|
if (success) { |
|
permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect); |
|
permitted_opens[num_permitted_opens].port_to_connect = port_to_connect; |
|
permitted_opens[num_permitted_opens].listen_port = listen_port; |
|
num_permitted_opens++; |
|
} |
} |
} |
|
|
/* |
/* |
|
|
if (port < IPPORT_RESERVED && !is_root) |
if (port < IPPORT_RESERVED && !is_root) |
packet_disconnect("Requested forwarding of port %d but user is not root.", |
packet_disconnect("Requested forwarding of port %d but user is not root.", |
port); |
port); |
/* |
/* Initiate forwarding */ |
* Initiate forwarding, |
|
*/ |
|
channel_request_local_forwarding(port, hostname, host_port, gateway_ports); |
channel_request_local_forwarding(port, hostname, host_port, gateway_ports); |
|
|
/* Free the argument string. */ |
/* Free the argument string. */ |
|
|
/* success */ |
/* success */ |
return sock; |
return sock; |
} |
} |
|
int |
|
channel_connect_by_listen_adress(u_short listen_port) |
|
{ |
|
int i; |
|
for (i = 0; i < num_permitted_opens; i++) |
|
if (permitted_opens[i].listen_port == listen_port) |
|
return channel_connect_to( |
|
permitted_opens[i].host_to_connect, |
|
permitted_opens[i].port_to_connect); |
|
debug("channel_connect_by_listen_adress: unknown listen_port %d", listen_port); |
|
return -1; |
|
} |
|
|
/* |
/* |
* This is called after receiving PORT_OPEN message. This attempts to |
* This is called after receiving PORT_OPEN message. This attempts to |
* connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION |
* connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION |
|
|
packet_disconnect("listen: %.100s", strerror(errno)); |
packet_disconnect("listen: %.100s", strerror(errno)); |
|
|
/* Allocate a channel for the authentication agent socket. */ |
/* Allocate a channel for the authentication agent socket. */ |
newch = channel_allocate(SSH_CHANNEL_AUTH_SOCKET, sock, |
newch = channel_new("auth socket", |
xstrdup("auth socket")); |
SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1, |
|
CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, |
|
0, xstrdup("auth socket"), 1); |
|
|
strlcpy(channels[newch].path, channel_forwarded_auth_socket_name, |
strlcpy(channels[newch].path, channel_forwarded_auth_socket_name, |
sizeof(channels[newch].path)); |
sizeof(channels[newch].path)); |
return 1; |
return 1; |