2006-04-23 18:26:03 +00:00
|
|
|
/*
|
|
|
|
* SSH port forwarding.
|
|
|
|
*/
|
|
|
|
|
2018-05-30 21:36:20 +00:00
|
|
|
#include <assert.h>
|
2001-08-08 20:53:27 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
|
2018-05-30 21:36:20 +00:00
|
|
|
/*
|
|
|
|
* Enumeration of values that live in the 'socks_state' field of
|
|
|
|
* struct PortForwarding.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
SOCKS_NONE, /* direct connection (no SOCKS, or SOCKS already done) */
|
|
|
|
SOCKS_INITIAL, /* don't know if we're SOCKS 4 or 5 yet */
|
|
|
|
SOCKS_4, /* expect a SOCKS 4 (or 4A) connection message */
|
|
|
|
SOCKS_5_INITIAL, /* expect a SOCKS 5 preliminary message */
|
|
|
|
SOCKS_5_CONNECT /* expect a SOCKS 5 connection message */
|
|
|
|
} SocksState;
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortForwarding {
|
|
|
|
struct ssh_channel *c; /* channel structure held by ssh.c */
|
2018-09-11 14:33:10 +00:00
|
|
|
Ssh ssh; /* instance of SSH backend itself */
|
|
|
|
/* Note that ssh need not be filled in if c is non-NULL */
|
2001-08-08 20:53:27 +00:00
|
|
|
Socket s;
|
2001-08-25 17:09:23 +00:00
|
|
|
int throttled, throttle_override;
|
2001-08-08 20:53:27 +00:00
|
|
|
int ready;
|
2018-05-30 21:36:20 +00:00
|
|
|
SocksState socks_state;
|
2003-04-05 11:45:21 +00:00
|
|
|
/*
|
|
|
|
* `hostname' and `port' are the real hostname and port, once
|
2013-07-11 17:23:56 +00:00
|
|
|
* we know what we're connecting to.
|
2003-04-05 11:45:21 +00:00
|
|
|
*/
|
2013-07-11 17:23:56 +00:00
|
|
|
char *hostname;
|
2003-04-05 11:45:21 +00:00
|
|
|
int port;
|
2013-07-11 17:23:56 +00:00
|
|
|
/*
|
2018-05-30 21:36:20 +00:00
|
|
|
* `socksbuf' is the buffer we use to accumulate the initial SOCKS
|
|
|
|
* segment of the incoming data, plus anything after that that we
|
|
|
|
* receive before we're ready to send data to the SSH server.
|
2013-07-11 17:23:56 +00:00
|
|
|
*/
|
2018-05-30 21:36:20 +00:00
|
|
|
strbuf *socksbuf;
|
|
|
|
size_t socksbuf_consumed;
|
2018-05-27 08:29:33 +00:00
|
|
|
|
|
|
|
const Plug_vtable *plugvt;
|
2001-08-08 20:53:27 +00:00
|
|
|
};
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortListener {
|
2018-09-11 14:33:10 +00:00
|
|
|
Ssh ssh; /* instance of SSH backend itself */
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
Socket s;
|
2018-05-30 21:36:20 +00:00
|
|
|
int is_dynamic;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
/*
|
|
|
|
* `hostname' and `port' are the real hostname and port, for
|
|
|
|
* ordinary forwardings.
|
|
|
|
*/
|
|
|
|
char *hostname;
|
|
|
|
int port;
|
2018-05-27 08:29:33 +00:00
|
|
|
|
|
|
|
const Plug_vtable *plugvt;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct PortForwarding *new_portfwd_state(void)
|
2013-07-11 17:23:56 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortForwarding *pf = snew(struct PortForwarding);
|
|
|
|
pf->hostname = NULL;
|
|
|
|
pf->socksbuf = NULL;
|
|
|
|
return pf;
|
2013-07-11 17:23:56 +00:00
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
static void free_portfwd_state(struct PortForwarding *pf)
|
2013-07-11 17:23:56 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (!pf)
|
2013-07-11 17:23:56 +00:00
|
|
|
return;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
sfree(pf->hostname);
|
2018-05-30 21:36:20 +00:00
|
|
|
if (pf->socksbuf)
|
|
|
|
strbuf_free(pf->socksbuf);
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
sfree(pf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct PortListener *new_portlistener_state(void)
|
|
|
|
{
|
|
|
|
struct PortListener *pl = snew(struct PortListener);
|
|
|
|
pl->hostname = NULL;
|
|
|
|
return pl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_portlistener_state(struct PortListener *pl)
|
|
|
|
{
|
|
|
|
if (!pl)
|
|
|
|
return;
|
|
|
|
sfree(pl->hostname);
|
|
|
|
sfree(pl);
|
2013-07-11 17:23:56 +00:00
|
|
|
}
|
|
|
|
|
2005-01-16 14:29:34 +00:00
|
|
|
static void pfd_log(Plug plug, int type, SockAddr addr, int port,
|
|
|
|
const char *error_msg, int error_code)
|
|
|
|
{
|
|
|
|
/* we have to dump these since we have no interface to logging.c */
|
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
static void pfl_log(Plug plug, int type, SockAddr addr, int port,
|
|
|
|
const char *error_msg, int error_code)
|
|
|
|
{
|
|
|
|
/* we have to dump these since we have no interface to logging.c */
|
|
|
|
}
|
|
|
|
|
2016-06-02 22:03:24 +00:00
|
|
|
static void pfd_closing(Plug plug, const char *error_msg, int error_code,
|
|
|
|
int calling_back)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
struct PortForwarding *pf = FROMFIELD(plug, struct PortForwarding, plugvt);
|
2001-08-08 20:53:27 +00:00
|
|
|
|
2011-12-08 19:15:58 +00:00
|
|
|
if (error_msg) {
|
|
|
|
/*
|
|
|
|
* Socket error. Slam the connection instantly shut.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf->c) {
|
|
|
|
sshfwd_unclean_close(pf->c, error_msg);
|
2013-08-15 06:42:36 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We might not have an SSH channel, if a socket error
|
|
|
|
* occurred during SOCKS negotiation. If not, we must
|
|
|
|
* clean ourself up without sshfwd_unclean_close's call
|
|
|
|
* back to pfd_close.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pfd_close(pf);
|
2013-08-15 06:42:36 +00:00
|
|
|
}
|
2011-12-08 19:15:58 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Ordinary EOF received on socket. Send an EOF on the SSH
|
|
|
|
* channel.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf->c)
|
|
|
|
sshfwd_write_eof(pf->c);
|
2011-12-08 19:15:58 +00:00
|
|
|
}
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 22:03:24 +00:00
|
|
|
static void pfl_closing(Plug plug, const char *error_msg, int error_code,
|
|
|
|
int calling_back)
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
{
|
|
|
|
struct PortListener *pl = (struct PortListener *) plug;
|
|
|
|
pfl_terminate(pl);
|
|
|
|
}
|
|
|
|
|
2015-05-18 12:57:45 +00:00
|
|
|
static void wrap_send_port_open(void *channel, const char *hostname, int port,
|
|
|
|
Socket s)
|
|
|
|
{
|
|
|
|
char *peerinfo, *description;
|
|
|
|
peerinfo = sk_peer_info(s);
|
|
|
|
if (peerinfo) {
|
|
|
|
description = dupprintf("forwarding from %s", peerinfo);
|
|
|
|
sfree(peerinfo);
|
|
|
|
} else {
|
|
|
|
description = dupstr("forwarding");
|
|
|
|
}
|
|
|
|
ssh_send_port_open(channel, hostname, port, description);
|
|
|
|
sfree(description);
|
|
|
|
}
|
|
|
|
|
2018-05-30 21:36:20 +00:00
|
|
|
static char *ipv4_to_string(unsigned ipv4)
|
|
|
|
{
|
|
|
|
return dupprintf("%u.%u.%u.%u",
|
|
|
|
(ipv4 >> 24) & 0xFF, (ipv4 >> 16) & 0xFF,
|
|
|
|
(ipv4 >> 8) & 0xFF, (ipv4 ) & 0xFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ipv6_to_string(ptrlen ipv6)
|
|
|
|
{
|
|
|
|
const unsigned char *addr = ipv6.ptr;
|
|
|
|
assert(ipv6.len == 16);
|
|
|
|
return dupprintf("%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 0),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 2),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 4),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 6),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 8),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 10),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 12),
|
|
|
|
(unsigned)GET_16BIT_MSB_FIRST(addr + 14));
|
|
|
|
}
|
|
|
|
|
2016-06-02 22:03:24 +00:00
|
|
|
static void pfd_receive(Plug plug, int urgent, char *data, int len)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
struct PortForwarding *pf = FROMFIELD(plug, struct PortForwarding, plugvt);
|
2018-05-30 21:36:20 +00:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pf->socks_state != SOCKS_NONE) {
|
|
|
|
BinarySource src[1];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store all the data we've got in socksbuf.
|
|
|
|
*/
|
|
|
|
put_data(pf->socksbuf, data, len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the start of socksbuf to see if it's a valid and
|
|
|
|
* complete message in the SOCKS exchange.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (pf->socks_state == SOCKS_INITIAL) {
|
|
|
|
/* Preliminary: check the first byte of the data (which we
|
|
|
|
* _must_ have by now) to find out which SOCKS major
|
|
|
|
* version we're speaking. */
|
|
|
|
switch (pf->socksbuf->u[0]) {
|
|
|
|
case 4:
|
|
|
|
pf->socks_state = SOCKS_4;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
pf->socks_state = SOCKS_5_INITIAL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pfd_close(pf); /* unrecognised version */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BinarySource_BARE_INIT(src, pf->socksbuf->u, pf->socksbuf->len);
|
|
|
|
get_data(src, pf->socksbuf_consumed);
|
|
|
|
|
|
|
|
while (pf->socks_state != SOCKS_NONE) {
|
|
|
|
unsigned socks_version, message_type, reserved_byte;
|
|
|
|
unsigned reply_code, port, ipv4, method;
|
|
|
|
ptrlen methods;
|
|
|
|
const char *socks4_hostname;
|
|
|
|
strbuf *output;
|
|
|
|
|
|
|
|
switch (pf->socks_state) {
|
|
|
|
case SOCKS_INITIAL:
|
|
|
|
case SOCKS_NONE:
|
|
|
|
assert(0 && "These case values cannot appear");
|
|
|
|
|
|
|
|
case SOCKS_4:
|
|
|
|
/* SOCKS 4/4A connect message */
|
|
|
|
socks_version = get_byte(src);
|
|
|
|
message_type = get_byte(src);
|
|
|
|
|
|
|
|
if (get_err(src) == BSE_OUT_OF_DATA)
|
|
|
|
return;
|
|
|
|
if (socks_version == 4 && message_type == 1) {
|
|
|
|
/* CONNECT message */
|
|
|
|
int name_based = FALSE;
|
|
|
|
|
|
|
|
port = get_uint16(src);
|
|
|
|
ipv4 = get_uint32(src);
|
|
|
|
if (ipv4 > 0x00000000 && ipv4 < 0x00000100) {
|
|
|
|
/*
|
|
|
|
* Addresses in this range indicate the SOCKS 4A
|
|
|
|
* extension to specify a hostname, which comes
|
|
|
|
* after the username.
|
|
|
|
*/
|
|
|
|
name_based = TRUE;
|
|
|
|
}
|
|
|
|
get_asciz(src); /* skip username */
|
|
|
|
socks4_hostname = name_based ? get_asciz(src) : NULL;
|
|
|
|
|
|
|
|
if (get_err(src) == BSE_OUT_OF_DATA)
|
|
|
|
return;
|
|
|
|
if (get_err(src))
|
|
|
|
goto socks4_reject;
|
|
|
|
|
|
|
|
pf->port = port;
|
|
|
|
if (name_based) {
|
|
|
|
pf->hostname = dupstr(socks4_hostname);
|
|
|
|
} else {
|
|
|
|
pf->hostname = ipv4_to_string(ipv4);
|
|
|
|
}
|
|
|
|
|
|
|
|
output = strbuf_new();
|
|
|
|
put_byte(output, 0); /* reply version */
|
|
|
|
put_byte(output, 90); /* SOCKS 4 'request granted' */
|
|
|
|
put_uint16(output, 0); /* null port field */
|
|
|
|
put_uint32(output, 0); /* null address field */
|
|
|
|
sk_write(pf->s, output->u, output->len);
|
|
|
|
strbuf_free(output);
|
|
|
|
|
|
|
|
pf->socks_state = SOCKS_NONE;
|
|
|
|
pf->socksbuf_consumed = src->pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
socks4_reject:
|
|
|
|
output = strbuf_new();
|
|
|
|
put_byte(output, 0); /* reply version */
|
|
|
|
put_byte(output, 91); /* SOCKS 4 'request rejected' */
|
|
|
|
put_uint16(output, 0); /* null port field */
|
|
|
|
put_uint32(output, 0); /* null address field */
|
|
|
|
sk_write(pf->s, output->u, output->len);
|
|
|
|
strbuf_free(output);
|
|
|
|
pfd_close(pf);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case SOCKS_5_INITIAL:
|
|
|
|
/* SOCKS 5 initial method list */
|
|
|
|
socks_version = get_byte(src);
|
|
|
|
methods = get_pstring(src);
|
|
|
|
|
|
|
|
method = 0xFF; /* means 'no usable method found' */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < methods.len; i++) {
|
|
|
|
if (((const unsigned char *)methods.ptr)[i] == 0 ) {
|
|
|
|
method = 0; /* no auth */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_err(src) == BSE_OUT_OF_DATA)
|
|
|
|
return;
|
|
|
|
if (get_err(src))
|
|
|
|
method = 0xFF;
|
|
|
|
|
|
|
|
output = strbuf_new();
|
|
|
|
put_byte(output, 5); /* SOCKS version */
|
|
|
|
put_byte(output, method); /* selected auth method */
|
|
|
|
sk_write(pf->s, output->u, output->len);
|
|
|
|
strbuf_free(output);
|
|
|
|
|
|
|
|
if (method == 0xFF) {
|
|
|
|
pfd_close(pf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pf->socks_state = SOCKS_5_CONNECT;
|
|
|
|
pf->socksbuf_consumed = src->pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SOCKS_5_CONNECT:
|
|
|
|
/* SOCKS 5 connect message */
|
|
|
|
socks_version = get_byte(src);
|
|
|
|
message_type = get_byte(src);
|
|
|
|
reserved_byte = get_byte(src);
|
|
|
|
|
|
|
|
if (socks_version == 5 && message_type == 1 &&
|
|
|
|
reserved_byte == 0) {
|
|
|
|
|
|
|
|
reply_code = 0; /* success */
|
|
|
|
|
|
|
|
switch (get_byte(src)) {
|
|
|
|
case 1: /* IPv4 */
|
|
|
|
pf->hostname = ipv4_to_string(get_uint32(src));
|
|
|
|
break;
|
|
|
|
case 4: /* IPv6 */
|
|
|
|
pf->hostname = ipv6_to_string(get_data(src, 16));
|
|
|
|
break;
|
|
|
|
case 3: /* unresolved domain name */
|
|
|
|
pf->hostname = mkstr(get_pstring(src));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pf->hostname = NULL;
|
|
|
|
reply_code = 8; /* address type not supported */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pf->port = get_uint16(src);
|
|
|
|
} else {
|
|
|
|
reply_code = 7; /* command not supported */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_err(src) == BSE_OUT_OF_DATA)
|
|
|
|
return;
|
|
|
|
if (get_err(src))
|
|
|
|
reply_code = 1; /* general server failure */
|
|
|
|
|
|
|
|
output = strbuf_new();
|
|
|
|
put_byte(output, 5); /* SOCKS version */
|
|
|
|
put_byte(output, reply_code);
|
|
|
|
put_byte(output, 0); /* reserved */
|
|
|
|
put_byte(output, 1); /* IPv4 address follows */
|
|
|
|
put_uint32(output, 0); /* bound IPv4 address (unused) */
|
|
|
|
put_uint16(output, 0); /* bound port number (unused) */
|
|
|
|
sk_write(pf->s, output->u, output->len);
|
|
|
|
strbuf_free(output);
|
|
|
|
|
|
|
|
if (reply_code != 0) {
|
|
|
|
pfd_close(pf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pf->socks_state = SOCKS_NONE;
|
|
|
|
pf->socksbuf_consumed = src->pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-04-05 11:45:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We come here when we're ready to make an actual
|
|
|
|
* connection.
|
|
|
|
*/
|
|
|
|
|
2009-04-23 17:33:42 +00:00
|
|
|
/*
|
|
|
|
* Freeze the socket until the SSH server confirms the
|
|
|
|
* connection.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
sk_set_frozen(pf->s, 1);
|
2009-04-23 17:33:42 +00:00
|
|
|
|
2018-09-11 14:33:10 +00:00
|
|
|
pf->c = new_sock_channel(pf->ssh, pf);
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf->c == NULL) {
|
|
|
|
pfd_close(pf);
|
2016-06-02 22:03:24 +00:00
|
|
|
return;
|
2003-04-05 11:45:21 +00:00
|
|
|
} else {
|
|
|
|
/* asks to forward to the specified host/port for this */
|
2015-05-18 12:57:45 +00:00
|
|
|
wrap_send_port_open(pf->c, pf->hostname, pf->port, pf->s);
|
2003-04-05 11:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf->ready) {
|
|
|
|
if (sshfwd_write(pf->c, data, len) > 0) {
|
|
|
|
pf->throttled = 1;
|
|
|
|
sk_set_frozen(pf->s, 1);
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
2001-08-25 17:09:23 +00:00
|
|
|
static void pfd_sent(Plug plug, int bufsize)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
struct PortForwarding *pf = FROMFIELD(plug, struct PortForwarding, plugvt);
|
2001-08-25 17:09:23 +00:00
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf->c)
|
|
|
|
sshfwd_unthrottle(pf->c, bufsize);
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
static const Plug_vtable PortForwarding_plugvt = {
|
|
|
|
pfd_log,
|
|
|
|
pfd_closing,
|
|
|
|
pfd_receive,
|
|
|
|
pfd_sent,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-08-08 20:53:27 +00:00
|
|
|
/*
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
* Called when receiving a PORT OPEN from the server to make a
|
|
|
|
* connection to a destination host.
|
|
|
|
*
|
|
|
|
* On success, returns NULL and fills in *pf_ret. On error, returns a
|
|
|
|
* dynamically allocated error message string.
|
2001-08-08 20:53:27 +00:00
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
char *pfd_connect(struct PortForwarding **pf_ret, char *hostname,int port,
|
|
|
|
void *c, Conf *conf, int addressfamily)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
|
|
|
SockAddr addr;
|
2003-05-04 14:18:18 +00:00
|
|
|
const char *err;
|
2018-05-25 13:03:13 +00:00
|
|
|
char *dummy_realhost = NULL;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortForwarding *pf;
|
2001-08-08 20:53:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to find host.
|
|
|
|
*/
|
2015-11-22 09:58:14 +00:00
|
|
|
addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily,
|
|
|
|
NULL, NULL);
|
2003-08-07 16:04:33 +00:00
|
|
|
if ((err = sk_addr_error(addr)) != NULL) {
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
char *err_ret = dupstr(err);
|
2003-08-07 16:04:33 +00:00
|
|
|
sk_addr_free(addr);
|
2013-07-21 07:40:26 +00:00
|
|
|
sfree(dummy_realhost);
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
return err_ret;
|
2003-08-07 16:04:33 +00:00
|
|
|
}
|
2001-08-08 20:53:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf = *pf_ret = new_portfwd_state();
|
2018-05-27 08:29:33 +00:00
|
|
|
pf->plugvt = &PortForwarding_plugvt;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->throttled = pf->throttle_override = 0;
|
|
|
|
pf->ready = 1;
|
|
|
|
pf->c = c;
|
2018-09-11 14:33:10 +00:00
|
|
|
pf->ssh = NULL; /* we shouldn't need this */
|
2018-05-30 21:36:20 +00:00
|
|
|
pf->socks_state = SOCKS_NONE;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
|
|
|
|
pf->s = new_connection(addr, dummy_realhost, port,
|
2018-05-27 08:29:33 +00:00
|
|
|
0, 1, 0, 0, &pf->plugvt, conf);
|
2013-07-14 10:46:07 +00:00
|
|
|
sfree(dummy_realhost);
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if ((err = sk_socket_error(pf->s)) != NULL) {
|
|
|
|
char *err_ret = dupstr(err);
|
|
|
|
sk_close(pf->s);
|
|
|
|
free_portfwd_state(pf);
|
|
|
|
*pf_ret = NULL;
|
|
|
|
return err_ret;
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
called when someone connects to the local port
|
|
|
|
*/
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
static int pfl_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortForwarding *pf;
|
|
|
|
struct PortListener *pl;
|
2001-08-08 20:53:27 +00:00
|
|
|
Socket s;
|
2003-05-04 14:18:18 +00:00
|
|
|
const char *err;
|
2001-08-08 20:53:27 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
pl = FROMFIELD(p, struct PortListener, plugvt);
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf = new_portfwd_state();
|
2018-05-27 08:29:33 +00:00
|
|
|
pf->plugvt = &PortForwarding_plugvt;
|
2001-08-08 20:53:27 +00:00
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->c = NULL;
|
2018-09-11 14:33:10 +00:00
|
|
|
pf->ssh = pl->ssh;
|
2001-08-08 20:53:27 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
pf->s = s = constructor(ctx, &pf->plugvt);
|
2003-01-05 13:04:04 +00:00
|
|
|
if ((err = sk_socket_error(s)) != NULL) {
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
free_portfwd_state(pf);
|
2001-08-08 20:53:27 +00:00
|
|
|
return err != NULL;
|
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->throttled = pf->throttle_override = 0;
|
|
|
|
pf->ready = 0;
|
2001-08-08 20:53:27 +00:00
|
|
|
|
2018-05-30 21:36:20 +00:00
|
|
|
if (pl->is_dynamic) {
|
|
|
|
pf->socks_state = SOCKS_INITIAL;
|
|
|
|
pf->socksbuf = strbuf_new();
|
|
|
|
pf->socksbuf_consumed = 0;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->port = 0; /* "hostname" buffer is so far empty */
|
2003-04-05 11:45:21 +00:00
|
|
|
sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
|
2001-08-08 20:53:27 +00:00
|
|
|
} else {
|
2018-05-30 21:36:20 +00:00
|
|
|
pf->socks_state = SOCKS_NONE;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->hostname = dupstr(pl->hostname);
|
|
|
|
pf->port = pl->port;
|
2018-09-11 14:33:10 +00:00
|
|
|
pf->c = new_sock_channel(pl->ssh, pf);
|
2003-04-05 11:45:21 +00:00
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf->c == NULL) {
|
|
|
|
free_portfwd_state(pf);
|
2003-04-05 11:45:21 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* asks to forward to the specified host/port for this */
|
2015-05-18 12:57:45 +00:00
|
|
|
wrap_send_port_open(pf->c, pf->hostname, pf->port, s);
|
2003-04-05 11:45:21 +00:00
|
|
|
}
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
static const Plug_vtable PortListener_plugvt = {
|
|
|
|
pfl_log,
|
|
|
|
pfl_closing,
|
|
|
|
NULL, /* recv */
|
|
|
|
NULL, /* send */
|
|
|
|
pfl_accepting
|
|
|
|
};
|
2001-08-08 20:53:27 +00:00
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
/*
|
|
|
|
* Add a new port-forwarding listener from srcaddr:port -> desthost:destport.
|
|
|
|
*
|
|
|
|
* On success, returns NULL and fills in *pl_ret. On error, returns a
|
|
|
|
* dynamically allocated error message string.
|
2001-08-08 20:53:27 +00:00
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
char *pfl_listen(char *desthost, int destport, char *srcaddr,
|
2018-09-11 14:33:10 +00:00
|
|
|
int port, Ssh ssh, Conf *conf,
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortListener **pl_ret, int address_family)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
2003-05-04 14:18:18 +00:00
|
|
|
const char *err;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
struct PortListener *pl;
|
2001-08-08 20:53:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open socket.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pl = *pl_ret = new_portlistener_state();
|
2018-05-27 08:29:33 +00:00
|
|
|
pl->plugvt = &PortListener_plugvt;
|
2003-04-05 11:45:21 +00:00
|
|
|
if (desthost) {
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pl->hostname = dupstr(desthost);
|
|
|
|
pl->port = destport;
|
2018-05-30 21:36:20 +00:00
|
|
|
pl->is_dynamic = FALSE;
|
2003-04-05 11:45:21 +00:00
|
|
|
} else
|
2018-05-30 21:36:20 +00:00
|
|
|
pl->is_dynamic = TRUE;
|
2018-09-11 14:33:10 +00:00
|
|
|
pl->ssh = ssh;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
pl->s = new_listener(srcaddr, port, &pl->plugvt,
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
!conf_get_int(conf, CONF_lport_acceptall),
|
|
|
|
conf, address_family);
|
|
|
|
if ((err = sk_socket_error(pl->s)) != NULL) {
|
|
|
|
char *err_ret = dupstr(err);
|
|
|
|
sk_close(pl->s);
|
|
|
|
free_portlistener_state(pl);
|
|
|
|
*pl_ret = NULL;
|
|
|
|
return err_ret;
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
void pfd_close(struct PortForwarding *pf)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (!pf)
|
2001-08-08 20:53:27 +00:00
|
|
|
return;
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
sk_close(pf->s);
|
|
|
|
free_portfwd_state(pf);
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 14:07:05 +00:00
|
|
|
/*
|
|
|
|
* Terminate a listener.
|
|
|
|
*/
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
void pfl_terminate(struct PortListener *pl)
|
2004-12-28 14:07:05 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (!pl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sk_close(pl->s);
|
|
|
|
free_portlistener_state(pl);
|
2004-12-28 14:07:05 +00:00
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
void pfd_unthrottle(struct PortForwarding *pf)
|
2001-08-25 17:09:23 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (!pf)
|
2001-08-25 17:09:23 +00:00
|
|
|
return;
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->throttled = 0;
|
|
|
|
sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
void pfd_override_throttle(struct PortForwarding *pf, int enable)
|
2001-08-25 17:09:23 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (!pf)
|
2001-08-25 17:09:23 +00:00
|
|
|
return;
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->throttle_override = enable;
|
|
|
|
sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
|
2001-08-25 17:09:23 +00:00
|
|
|
}
|
|
|
|
|
2001-08-08 20:53:27 +00:00
|
|
|
/*
|
|
|
|
* Called to send data down the raw connection.
|
|
|
|
*/
|
2018-06-02 06:52:26 +00:00
|
|
|
int pfd_send(struct PortForwarding *pf, const void *data, int len)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf == NULL)
|
2001-08-25 17:09:23 +00:00
|
|
|
return 0;
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
return sk_write(pf->s, data, len);
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
void pfd_send_eof(struct PortForwarding *pf)
|
2011-09-13 11:44:03 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
sk_write_eof(pf->s);
|
2011-09-13 11:44:03 +00:00
|
|
|
}
|
2001-08-08 20:53:27 +00:00
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
void pfd_confirm(struct PortForwarding *pf)
|
2001-08-08 20:53:27 +00:00
|
|
|
{
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
if (pf == NULL)
|
2001-08-08 20:53:27 +00:00
|
|
|
return;
|
|
|
|
|
Refactor ssh.c's APIs to x11fwd.c and portfwd.c.
The most important change is that, where previously ssh.c held the
Socket pointer for each X11 and port forwarding, and the support
modules would find their internal state structure by calling
sk_get_private_ptr on that Socket, it's now the other way round. ssh.c
now directly holds the internal state structure pointer for each
forwarding, and when the support module needs the Socket it looks it
up in a field of that. This will come in handy when I decouple socket
creation from logical forwarding setup, so that X forwardings can
delay actually opening a connection to an X server until they look at
the authentication data and see which server it has to be.
However, while I'm here, I've also taken the opportunity to clean up a
few other points, notably error message handling, and also the fact
that the same kind of state structure was used for both
connection-type and listening-type port forwardings. Now there are
separate PortForwarding and PortListener structure types, which seems
far more sensible.
[originally from svn r10074]
2013-11-17 14:04:41 +00:00
|
|
|
pf->ready = 1;
|
|
|
|
sk_set_frozen(pf->s, 0);
|
|
|
|
sk_write(pf->s, NULL, 0);
|
2018-05-30 21:36:20 +00:00
|
|
|
if (pf->socksbuf) {
|
|
|
|
sshfwd_write(pf->c, pf->socksbuf->u + pf->socksbuf_consumed,
|
|
|
|
pf->socksbuf->len - pf->socksbuf_consumed);
|
|
|
|
strbuf_free(pf->socksbuf);
|
|
|
|
pf->socksbuf = NULL;
|
2003-04-05 11:45:21 +00:00
|
|
|
}
|
2001-08-08 20:53:27 +00:00
|
|
|
}
|