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>
|
|
|
|
|
|
|
|
#include "tree234.h"
|
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
|
|
|
|
Plug *plug, int overlapped);
|
2006-08-28 11:13:56 +00:00
|
|
|
|
Get rid of lots of implicit pointer types.
All the main backend structures - Ssh, Telnet, Pty, Serial etc - now
describe structure types themselves rather than pointers to them. The
same goes for the codebase-wide trait types Socket and Plug, and the
supporting types SockAddr and Pinger.
All those things that were typedefed as pointers are older types; the
newer ones have the explicit * at the point of use, because that's
what I now seem to be preferring. But whichever one of those is
better, inconsistently using a mixture of the two styles is worse, so
let's make everything consistent.
A few types are still implicitly pointers, such as Bignum and some of
the GSSAPI types; generally this is either because they have to be
void *, or because they're typedefed differently on different
platforms and aren't always pointers at all. Can't be helped. But I've
got rid of the main ones, at least.
2018-10-04 18:10:23 +00:00
|
|
|
Socket *platform_new_connection(SockAddr *addr, const char *hostname,
|
|
|
|
int port, int privport,
|
|
|
|
int oobinline, int nodelay, int keepalive,
|
|
|
|
Plug *plug, Conf *conf)
|
2006-08-28 11:13:56 +00:00
|
|
|
{
|
|
|
|
char *cmd;
|
2015-11-22 11:50:37 +00:00
|
|
|
HANDLE us_to_cmd, cmd_from_us;
|
|
|
|
HANDLE us_from_cmd, cmd_to_us;
|
|
|
|
HANDLE us_from_cmd_err, cmd_err_to_us;
|
2006-08-28 11:13:56 +00:00
|
|
|
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
|
|
|
|
2013-11-17 14:03:44 +00:00
|
|
|
/* We are responsible for this and don't need it any more */
|
|
|
|
sk_addr_free(addr);
|
|
|
|
|
2006-08-28 11:13:56 +00:00
|
|
|
{
|
|
|
|
char *msg = dupprintf("Starting local proxy command: %s", cmd);
|
2015-11-22 12:15:52 +00:00
|
|
|
plug_log(plug, 2, NULL, 0, msg, 0);
|
2006-08-28 11:13:56 +00:00
|
|
|
sfree(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)) {
|
2013-07-22 07:11:54 +00:00
|
|
|
sfree(cmd);
|
2018-10-07 13:47:16 +00:00
|
|
|
return new_error_socket_fmt(
|
|
|
|
plug, "Unable to create pipes for proxy command: %s",
|
|
|
|
win_strerror(GetLastError()));
|
2006-08-28 11:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!CreatePipe(&cmd_from_us, &us_to_cmd, &sa, 0)) {
|
2013-11-17 14:03:44 +00:00
|
|
|
sfree(cmd);
|
2006-08-28 11:13:56 +00:00
|
|
|
CloseHandle(us_from_cmd);
|
|
|
|
CloseHandle(cmd_to_us);
|
2018-10-07 13:47:16 +00:00
|
|
|
return new_error_socket_fmt(
|
|
|
|
plug, "Unable to create pipes for proxy command: %s",
|
|
|
|
win_strerror(GetLastError()));
|
2006-08-28 11:13:56 +00:00
|
|
|
}
|
|
|
|
|
Remove FLAG_STDERR completely.
Originally, it controlled whether ssh.c should send terminal messages
(such as login and password prompts) to terminal.c or to stderr. But
we've had the from_backend() abstraction for ages now, which even has
an existing flag to indicate that the data is stderr rather than
stdout data; applications which set FLAG_STDERR are precisely those
that link against uxcons or wincons, so from_backend will do the
expected thing anyway with data sent to it with that flag set. So
there's no reason ssh.c can't just unconditionally pass everything
through that, and remove the special case.
FLAG_STDERR was also used by winproxy and uxproxy to decide whether to
capture standard error from a local proxy command, or whether to let
the proxy command send its diagnostics directly to the usual standard
error. On reflection, I think it's better to unconditionally capture
the proxy's stderr, for three reasons. Firstly, it means proxy
diagnostics are prefixed with 'proxy:' so that you can tell them apart
from any other stderr spew (which used to be particularly confusing if
both the main application and the proxy command were instances of
Plink); secondly, proxy diagnostics are now reliably copied to packet
log files along with all the other Event Log entries, even by
command-line tools; and thirdly, this means the option to suppress
proxy command diagnostics after the main session starts will actually
_work_ in the command-line tools, which it previously couldn't.
A more minor structure change is that copying of Event Log messages to
stderr in verbose mode is now done by wincons/uxcons, instead of
centrally in logging.c (since logging.c can now no longer check
FLAG_STDERR to decide whether to do it). The total amount of code to
do this is considerably smaller than the defensive-sounding comment in
logevent.c explaining why I did it the other way instead :-)
2018-09-21 15:15:49 +00:00
|
|
|
if (!CreatePipe(&us_from_cmd_err, &cmd_err_to_us, &sa, 0)) {
|
|
|
|
sfree(cmd);
|
|
|
|
CloseHandle(us_from_cmd);
|
|
|
|
CloseHandle(cmd_to_us);
|
|
|
|
CloseHandle(us_to_cmd);
|
|
|
|
CloseHandle(cmd_from_us);
|
2018-10-07 13:47:16 +00:00
|
|
|
return new_error_socket_fmt(
|
|
|
|
plug, "Unable to create pipes for proxy command: %s",
|
|
|
|
win_strerror(GetLastError()));
|
2015-11-22 11:50:37 +00:00
|
|
|
}
|
|
|
|
|
2006-08-28 11:13:56 +00:00
|
|
|
SetHandleInformation(us_to_cmd, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
SetHandleInformation(us_from_cmd, HANDLE_FLAG_INHERIT, 0);
|
2015-11-22 11:50:37 +00:00
|
|
|
if (us_from_cmd_err != NULL)
|
|
|
|
SetHandleInformation(us_from_cmd_err, HANDLE_FLAG_INHERIT, 0);
|
2006-08-28 11:13:56 +00:00
|
|
|
|
|
|
|
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;
|
2015-11-22 11:50:37 +00:00
|
|
|
si.hStdError = cmd_err_to_us;
|
2006-08-28 11:13:56 +00:00
|
|
|
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);
|
|
|
|
|
2015-11-22 11:50:37 +00:00
|
|
|
if (cmd_err_to_us != NULL)
|
|
|
|
CloseHandle(cmd_err_to_us);
|
|
|
|
|
|
|
|
return make_handle_socket(us_to_cmd, us_from_cmd, us_from_cmd_err,
|
|
|
|
plug, FALSE);
|
2006-08-28 11:13:56 +00:00
|
|
|
}
|