2006-08-28 11:13:56 +00:00
|
|
|
/*
|
|
|
|
* winproxy.c: Windows implementation of platform_new_connection(),
|
|
|
|
* supporting an OpenSSH-like proxy command via the winhandl.c
|
|
|
|
* mechanism.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define DEFINE_PLUG_METHOD_MACROS
|
|
|
|
#include "tree234.h"
|
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
|
|
|
|
typedef struct Socket_localproxy_tag *Local_Proxy_Socket;
|
|
|
|
|
|
|
|
struct Socket_localproxy_tag {
|
|
|
|
const struct socket_function_table *fn;
|
|
|
|
/* the above variable absolutely *must* be the first in this structure */
|
|
|
|
|
|
|
|
HANDLE to_cmd_H, from_cmd_H;
|
|
|
|
struct handle *to_cmd_h, *from_cmd_h;
|
|
|
|
|
|
|
|
char *error;
|
|
|
|
|
|
|
|
Plug plug;
|
|
|
|
|
|
|
|
void *privptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
int localproxy_gotdata(struct handle *h, void *data, int len)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
|
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
return plug_closing(ps->plug, "Read error from local proxy command",
|
|
|
|
0, 0);
|
|
|
|
} else if (len == 0) {
|
|
|
|
return plug_closing(ps->plug, NULL, 0, 0);
|
|
|
|
} else {
|
2008-08-31 21:45:39 +00:00
|
|
|
return plug_receive(ps->plug, 0, data, len);
|
2006-08-28 11:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void localproxy_sentdata(struct handle *h, int new_backlog)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) handle_get_privdata(h);
|
|
|
|
|
|
|
|
plug_sent(ps->plug, new_backlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Plug sk_localproxy_plug (Socket s, Plug p)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
Plug ret = ps->plug;
|
|
|
|
if (p)
|
|
|
|
ps->plug = p;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sk_localproxy_close (Socket s)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
|
|
|
|
handle_free(ps->to_cmd_h);
|
|
|
|
handle_free(ps->from_cmd_h);
|
|
|
|
CloseHandle(ps->to_cmd_H);
|
|
|
|
CloseHandle(ps->from_cmd_H);
|
|
|
|
|
|
|
|
sfree(ps);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sk_localproxy_write (Socket s, const char *data, int len)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
|
|
|
|
return handle_write(ps->to_cmd_h, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sk_localproxy_write_oob(Socket s, const char *data, int len)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* oob data is treated as inband; nasty, but nothing really
|
|
|
|
* better we can do
|
|
|
|
*/
|
|
|
|
return sk_localproxy_write(s, data, len);
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:44:03 +00:00
|
|
|
static void sk_localproxy_write_eof(Socket s)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
|
|
|
|
handle_write_eof(ps->to_cmd_h);
|
|
|
|
}
|
|
|
|
|
2006-08-28 11:13:56 +00:00
|
|
|
static void sk_localproxy_flush(Socket s)
|
|
|
|
{
|
|
|
|
/* Local_Proxy_Socket ps = (Local_Proxy_Socket) s; */
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sk_localproxy_set_private_ptr(Socket s, void *ptr)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
ps->privptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *sk_localproxy_get_private_ptr(Socket s)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
return ps->privptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sk_localproxy_set_frozen(Socket s, int is_frozen)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *sk_localproxy_socket_error(Socket s)
|
|
|
|
{
|
|
|
|
Local_Proxy_Socket ps = (Local_Proxy_Socket) s;
|
|
|
|
return ps->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket platform_new_connection(SockAddr addr, char *hostname,
|
|
|
|
int port, int privport,
|
|
|
|
int oobinline, int nodelay, int keepalive,
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
Plug plug, Conf *conf)
|
2006-08-28 11:13:56 +00:00
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
static const struct socket_function_table socket_fn_table = {
|
|
|
|
sk_localproxy_plug,
|
|
|
|
sk_localproxy_close,
|
|
|
|
sk_localproxy_write,
|
|
|
|
sk_localproxy_write_oob,
|
2011-09-13 11:44:03 +00:00
|
|
|
sk_localproxy_write_eof,
|
2006-08-28 11:13:56 +00:00
|
|
|
sk_localproxy_flush,
|
|
|
|
sk_localproxy_set_private_ptr,
|
|
|
|
sk_localproxy_get_private_ptr,
|
|
|
|
sk_localproxy_set_frozen,
|
|
|
|
sk_localproxy_socket_error
|
|
|
|
};
|
|
|
|
|
|
|
|
Local_Proxy_Socket ret;
|
|
|
|
HANDLE us_to_cmd, us_from_cmd, cmd_to_us, cmd_from_us;
|
|
|
|
SECURITY_ATTRIBUTES sa;
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
if (conf_get_int(conf, CONF_proxy_type) != PROXY_CMD)
|
2006-08-28 11:13:56 +00:00
|
|
|
return NULL;
|
|
|
|
|
Post-release destabilisation! Completely remove the struct type
'Config' in putty.h, which stores all PuTTY's settings and includes an
arbitrary length limit on every single one of those settings which is
stored in string form. In place of it is 'Conf', an opaque data type
everywhere outside the new file conf.c, which stores a list of (key,
value) pairs in which every key contains an integer identifying a
configuration setting, and for some of those integers the key also
contains extra parts (so that, for instance, CONF_environmt is a
string-to-string mapping). Everywhere that a Config was previously
used, a Conf is now; everywhere there was a Config structure copy,
conf_copy() is called; every lookup, adjustment, load and save
operation on a Config has been rewritten; and there's a mechanism for
serialising a Conf into a binary blob and back for use with Duplicate
Session.
User-visible effects of this change _should_ be minimal, though I
don't doubt I've introduced one or two bugs here and there which will
eventually be found. The _intended_ visible effects of this change are
that all arbitrary limits on configuration strings and lists (e.g.
limit on number of port forwardings) should now disappear; that list
boxes in the configuration will now be displayed in a sorted order
rather than the arbitrary order in which they were added to the list
(since the underlying data structure is now a sorted tree234 rather
than an ad-hoc comma-separated string); and one more specific change,
which is that local and dynamic port forwardings on the same port
number are now mutually exclusive in the configuration (putting 'D' in
the key rather than the value was a mistake in the first place).
One other reorganisation as a result of this is that I've moved all
the dialog.c standard handlers (dlg_stdeditbox_handler and friends)
out into config.c, because I can't really justify calling them generic
any more. When they took a pointer to an arbitrary structure type and
the offset of a field within that structure, they were independent of
whether that structure was a Config or something completely different,
but now they really do expect to talk to a Conf, which can _only_ be
used for PuTTY configuration, so I've renamed them all things like
conf_editbox_handler and moved them out of the nominally independent
dialog-box management module into the PuTTY-specific config.c.
[originally from svn r9214]
2011-07-14 18:52:21 +00:00
|
|
|
cmd = format_telnet_command(addr, port, conf);
|
2006-08-28 11:13:56 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
char *msg = dupprintf("Starting local proxy command: %s", cmd);
|
|
|
|
/* We're allowed to pass NULL here, because we're part of the Windows
|
|
|
|
* front end so we know logevent doesn't expect any data. */
|
|
|
|
logevent(NULL, msg);
|
|
|
|
sfree(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = snew(struct Socket_localproxy_tag);
|
|
|
|
ret->fn = &socket_fn_table;
|
|
|
|
ret->plug = plug;
|
|
|
|
ret->error = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the pipes to the proxy command, and spawn the proxy
|
|
|
|
* command process.
|
|
|
|
*/
|
|
|
|
sa.nLength = sizeof(sa);
|
|
|
|
sa.lpSecurityDescriptor = NULL; /* default */
|
|
|
|
sa.bInheritHandle = TRUE;
|
|
|
|
if (!CreatePipe(&us_from_cmd, &cmd_to_us, &sa, 0)) {
|
|
|
|
ret->error = dupprintf("Unable to create pipes for proxy command");
|
2013-07-22 07:11:54 +00:00
|
|
|
sfree(cmd);
|
2006-08-28 11:13:56 +00:00
|
|
|
return (Socket)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
|
|
|
|
CloseHandle(us_from_cmd);
|
|
|
|
CloseHandle(cmd_to_us);
|
|
|
|
ret->error = dupprintf("Unable to create pipes for proxy command");
|
2013-07-22 07:11:54 +00:00
|
|
|
sfree(cmd);
|
2006-08-28 11:13:56 +00:00
|
|
|
return (Socket)ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
si.lpReserved = NULL;
|
|
|
|
si.lpDesktop = NULL;
|
|
|
|
si.lpTitle = NULL;
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
|
|
|
si.cbReserved2 = 0;
|
|
|
|
si.lpReserved2 = NULL;
|
|
|
|
si.hStdInput = cmd_from_us;
|
|
|
|
si.hStdOutput = cmd_to_us;
|
|
|
|
si.hStdError = NULL;
|
|
|
|
CreateProcess(NULL, cmd, NULL, NULL, TRUE,
|
|
|
|
CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS,
|
|
|
|
NULL, NULL, &si, &pi);
|
2012-10-10 18:29:16 +00:00
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
2006-08-28 11:13:56 +00:00
|
|
|
|
2009-08-21 21:16:22 +00:00
|
|
|
sfree(cmd);
|
|
|
|
|
2006-08-28 11:13:56 +00:00
|
|
|
CloseHandle(cmd_from_us);
|
|
|
|
CloseHandle(cmd_to_us);
|
|
|
|
|
|
|
|
ret->to_cmd_H = us_to_cmd;
|
|
|
|
ret->from_cmd_H = us_from_cmd;
|
|
|
|
|
|
|
|
ret->from_cmd_h = handle_input_new(ret->from_cmd_H, localproxy_gotdata,
|
|
|
|
ret, 0);
|
|
|
|
ret->to_cmd_h = handle_output_new(ret->to_cmd_H, localproxy_sentdata,
|
|
|
|
ret, 0);
|
|
|
|
|
|
|
|
/* We are responsible for this and don't need it any more */
|
|
|
|
sk_addr_free(addr);
|
|
|
|
|
|
|
|
return (Socket) ret;
|
|
|
|
}
|