2003-05-06 19:52:31 +00:00
|
|
|
/*
|
|
|
|
* uxproxy.c: Unix implementation of platform_new_connection(),
|
|
|
|
* supporting an OpenSSH-like proxy command.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.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 *platform_new_connection(SockAddr *addr, const char *hostname,
|
|
|
|
int port, int privport,
|
|
|
|
int oobinline, int nodelay, int keepalive,
|
|
|
|
Plug *plug, Conf *conf)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
|
2015-11-22 11:50:37 +00:00
|
|
|
int to_cmd_pipe[2], from_cmd_pipe[2], cmd_err_pipe[2], pid, proxytype;
|
2018-10-07 13:55:32 +00:00
|
|
|
int infd, outfd, inerrfd;
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2015-10-17 13:06:06 +00:00
|
|
|
proxytype = conf_get_int(conf, CONF_proxy_type);
|
|
|
|
if (proxytype != PROXY_CMD && proxytype != PROXY_FUZZ)
|
2003-05-06 19:52:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
2015-10-17 13:06:06 +00:00
|
|
|
if (proxytype == PROXY_CMD) {
|
|
|
|
cmd = format_telnet_command(addr, port, conf);
|
|
|
|
|
2015-11-22 12:15:52 +00:00
|
|
|
{
|
|
|
|
char *logmsg = dupprintf("Starting local proxy command: %s", cmd);
|
|
|
|
plug_log(plug, 2, NULL, 0, logmsg, 0);
|
|
|
|
sfree(logmsg);
|
|
|
|
}
|
|
|
|
|
2015-10-17 13:06:06 +00:00
|
|
|
/*
|
|
|
|
* Create the pipes to the proxy command, and spawn the proxy
|
|
|
|
* command process.
|
|
|
|
*/
|
|
|
|
if (pipe(to_cmd_pipe) < 0 ||
|
2015-11-22 11:50:37 +00:00
|
|
|
pipe(from_cmd_pipe) < 0 ||
|
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
|
|
|
pipe(cmd_err_pipe) < 0) {
|
2015-10-17 13:06:06 +00:00
|
|
|
sfree(cmd);
|
2018-10-07 13:55:32 +00:00
|
|
|
return new_error_socket_fmt(plug, "pipe: %s", strerror(errno));
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
|
|
|
cloexec(to_cmd_pipe[1]);
|
|
|
|
cloexec(from_cmd_pipe[0]);
|
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
|
|
|
cloexec(cmd_err_pipe[0]);
|
2015-10-17 13:06:06 +00:00
|
|
|
|
|
|
|
pid = fork();
|
2018-10-07 13:55:32 +00:00
|
|
|
if (pid == 0) {
|
2015-10-17 13:06:06 +00:00
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
dup2(to_cmd_pipe[0], 0);
|
|
|
|
dup2(from_cmd_pipe[1], 1);
|
|
|
|
close(to_cmd_pipe[0]);
|
|
|
|
close(from_cmd_pipe[1]);
|
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
|
|
|
dup2(cmd_err_pipe[1], 2);
|
2015-10-17 13:06:06 +00:00
|
|
|
noncloexec(0);
|
|
|
|
noncloexec(1);
|
|
|
|
execl("/bin/sh", "sh", "-c", cmd, (void *)NULL);
|
|
|
|
_exit(255);
|
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2015-10-17 13:06:06 +00:00
|
|
|
sfree(cmd);
|
2009-08-21 21:16:22 +00:00
|
|
|
|
2018-10-07 13:55:32 +00:00
|
|
|
if (pid < 0)
|
|
|
|
return new_error_socket_fmt(plug, "fork: %s", strerror(errno));
|
|
|
|
|
2015-10-17 13:06:06 +00:00
|
|
|
close(to_cmd_pipe[0]);
|
|
|
|
close(from_cmd_pipe[1]);
|
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
|
|
|
close(cmd_err_pipe[1]);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2018-10-07 13:55:32 +00:00
|
|
|
outfd = to_cmd_pipe[1];
|
|
|
|
infd = from_cmd_pipe[0];
|
|
|
|
inerrfd = cmd_err_pipe[0];
|
2015-10-17 13:06:06 +00:00
|
|
|
} else {
|
|
|
|
cmd = format_telnet_command(addr, port, conf);
|
2018-10-07 13:55:32 +00:00
|
|
|
outfd = open("/dev/null", O_WRONLY);
|
|
|
|
if (outfd == -1) {
|
2015-10-17 13:06:06 +00:00
|
|
|
sfree(cmd);
|
2018-10-07 13:55:32 +00:00
|
|
|
return new_error_socket_fmt(
|
|
|
|
plug, "/dev/null: %s", strerror(errno));
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
2018-10-07 13:55:32 +00:00
|
|
|
infd = open(cmd, O_RDONLY);
|
|
|
|
if (infd == -1) {
|
2015-10-17 13:06:06 +00:00
|
|
|
sfree(cmd);
|
2018-10-07 13:55:32 +00:00
|
|
|
return new_error_socket_fmt(plug, "%s: %s", cmd, strerror(errno));
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
|
|
|
sfree(cmd);
|
2018-10-07 13:55:32 +00:00
|
|
|
inerrfd = -1;
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2003-08-07 16:04:33 +00:00
|
|
|
/* We are responsible for this and don't need it any more */
|
|
|
|
sk_addr_free(addr);
|
|
|
|
|
2018-10-07 13:55:32 +00:00
|
|
|
return make_fd_socket(infd, outfd, inerrfd, plug);
|
2003-05-06 19:52:31 +00:00
|
|
|
}
|