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>
|
|
|
|
|
|
|
|
#define DEFINE_PLUG_METHOD_MACROS
|
|
|
|
#include "tree234.h"
|
|
|
|
#include "putty.h"
|
|
|
|
#include "network.h"
|
|
|
|
#include "proxy.h"
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
typedef struct LocalProxySocket {
|
2015-11-22 11:50:37 +00:00
|
|
|
int to_cmd, from_cmd, cmd_err; /* fds */
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
char *error;
|
|
|
|
|
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
|
|
|
Plug *plug;
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
bufchain pending_output_data;
|
|
|
|
bufchain pending_input_data;
|
2015-11-22 11:50:37 +00:00
|
|
|
bufchain pending_error_data;
|
2011-09-13 11:44:03 +00:00
|
|
|
enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
|
2018-05-27 08:29:33 +00:00
|
|
|
|
2018-09-28 18:06:07 +00:00
|
|
|
int pending_error;
|
|
|
|
|
2018-10-05 06:03:46 +00:00
|
|
|
const SocketVtable *sockvt;
|
2018-05-27 08:29:33 +00:00
|
|
|
} LocalProxySocket;
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2016-05-30 21:52:30 +00:00
|
|
|
static void localproxy_select_result(int fd, int event);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Trees to look up the pipe fds in.
|
|
|
|
*/
|
2015-11-22 11:50:37 +00:00
|
|
|
static tree234 *localproxy_by_fromfd;
|
|
|
|
static tree234 *localproxy_by_tofd;
|
|
|
|
static tree234 *localproxy_by_errfd;
|
2003-05-06 19:52:31 +00:00
|
|
|
static int localproxy_fromfd_cmp(void *av, void *bv)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *a = (LocalProxySocket *)av;
|
|
|
|
LocalProxySocket *b = (LocalProxySocket *)bv;
|
2003-05-06 19:52:31 +00:00
|
|
|
if (a->from_cmd < b->from_cmd)
|
|
|
|
return -1;
|
|
|
|
if (a->from_cmd > b->from_cmd)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int localproxy_fromfd_find(void *av, void *bv)
|
|
|
|
{
|
|
|
|
int a = *(int *)av;
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *b = (LocalProxySocket *)bv;
|
2003-05-06 19:52:31 +00:00
|
|
|
if (a < b->from_cmd)
|
|
|
|
return -1;
|
|
|
|
if (a > b->from_cmd)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int localproxy_tofd_cmp(void *av, void *bv)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *a = (LocalProxySocket *)av;
|
|
|
|
LocalProxySocket *b = (LocalProxySocket *)bv;
|
2003-05-06 19:52:31 +00:00
|
|
|
if (a->to_cmd < b->to_cmd)
|
|
|
|
return -1;
|
|
|
|
if (a->to_cmd > b->to_cmd)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int localproxy_tofd_find(void *av, void *bv)
|
|
|
|
{
|
|
|
|
int a = *(int *)av;
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *b = (LocalProxySocket *)bv;
|
2003-05-06 19:52:31 +00:00
|
|
|
if (a < b->to_cmd)
|
|
|
|
return -1;
|
|
|
|
if (a > b->to_cmd)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-22 11:50:37 +00:00
|
|
|
static int localproxy_errfd_cmp(void *av, void *bv)
|
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *a = (LocalProxySocket *)av;
|
|
|
|
LocalProxySocket *b = (LocalProxySocket *)bv;
|
2015-11-22 11:50:37 +00:00
|
|
|
if (a->cmd_err < b->cmd_err)
|
|
|
|
return -1;
|
|
|
|
if (a->cmd_err > b->cmd_err)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
static int localproxy_errfd_find(void *av, void *bv)
|
|
|
|
{
|
|
|
|
int a = *(int *)av;
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *b = (LocalProxySocket *)bv;
|
2015-11-22 11:50:37 +00:00
|
|
|
if (a < b->cmd_err)
|
|
|
|
return -1;
|
|
|
|
if (a > b->cmd_err)
|
|
|
|
return +1;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
/* basic proxy socket functions */
|
|
|
|
|
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
|
|
|
static Plug *sk_localproxy_plug (Socket *s, Plug *p)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt);
|
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
|
|
|
Plug *ret = ps->plug;
|
2003-05-06 19:52:31 +00:00
|
|
|
if (p)
|
|
|
|
ps->plug = p;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_localproxy_close (Socket *s)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2011-09-13 11:44:03 +00:00
|
|
|
if (ps->to_cmd >= 0) {
|
|
|
|
del234(localproxy_by_tofd, ps);
|
|
|
|
uxsel_del(ps->to_cmd);
|
|
|
|
close(ps->to_cmd);
|
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2018-09-28 18:21:37 +00:00
|
|
|
if (ps->from_cmd >= 0) {
|
|
|
|
del234(localproxy_by_fromfd, ps);
|
|
|
|
uxsel_del(ps->from_cmd);
|
|
|
|
close(ps->from_cmd);
|
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2018-09-28 18:21:37 +00:00
|
|
|
if (ps->cmd_err >= 0) {
|
|
|
|
del234(localproxy_by_errfd, ps);
|
|
|
|
uxsel_del(ps->cmd_err);
|
|
|
|
close(ps->cmd_err);
|
|
|
|
}
|
2015-11-22 11:50:37 +00:00
|
|
|
|
2015-11-22 15:02:14 +00:00
|
|
|
bufchain_clear(&ps->pending_input_data);
|
|
|
|
bufchain_clear(&ps->pending_output_data);
|
2015-11-22 11:50:37 +00:00
|
|
|
bufchain_clear(&ps->pending_error_data);
|
|
|
|
|
2018-09-28 18:06:07 +00:00
|
|
|
delete_callbacks_for_context(ps);
|
|
|
|
|
2003-05-06 19:52:31 +00:00
|
|
|
sfree(ps);
|
|
|
|
}
|
|
|
|
|
2018-09-28 18:06:07 +00:00
|
|
|
static void localproxy_error_callback(void *vs)
|
|
|
|
{
|
|
|
|
LocalProxySocket *ps = (LocalProxySocket *)vs;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just in case other socket work has caused this socket to vanish
|
|
|
|
* or become somehow non-erroneous before this callback arrived...
|
|
|
|
*/
|
|
|
|
if (!ps->pending_error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An error has occurred on this socket. Pass it to the plug.
|
|
|
|
*/
|
|
|
|
plug_closing(ps->plug, strerror(ps->pending_error), ps->pending_error, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
static int localproxy_try_send(LocalProxySocket *ps)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
|
|
|
int sent = 0;
|
|
|
|
|
|
|
|
while (bufchain_size(&ps->pending_output_data) > 0) {
|
|
|
|
void *data;
|
|
|
|
int len, ret;
|
|
|
|
|
|
|
|
bufchain_prefix(&ps->pending_output_data, &data, &len);
|
|
|
|
ret = write(ps->to_cmd, data, len);
|
|
|
|
if (ret < 0 && errno != EWOULDBLOCK) {
|
2018-09-28 18:06:07 +00:00
|
|
|
if (!ps->pending_error) {
|
|
|
|
ps->pending_error = errno;
|
|
|
|
queue_toplevel_callback(localproxy_error_callback, ps);
|
|
|
|
}
|
2017-02-22 21:51:03 +00:00
|
|
|
return 0;
|
2003-05-06 19:52:31 +00:00
|
|
|
} else if (ret <= 0) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
bufchain_consume(&ps->pending_output_data, ret);
|
|
|
|
sent += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-13 11:44:03 +00:00
|
|
|
if (ps->outgoingeof == EOF_PENDING) {
|
|
|
|
del234(localproxy_by_tofd, ps);
|
|
|
|
close(ps->to_cmd);
|
|
|
|
uxsel_del(ps->to_cmd);
|
|
|
|
ps->to_cmd = -1;
|
|
|
|
ps->outgoingeof = EOF_SENT;
|
|
|
|
}
|
|
|
|
|
2003-05-06 19:52:31 +00:00
|
|
|
if (bufchain_size(&ps->pending_output_data) == 0)
|
|
|
|
uxsel_del(ps->to_cmd);
|
|
|
|
else
|
|
|
|
uxsel_set(ps->to_cmd, 2, localproxy_select_result);
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static int sk_localproxy_write (Socket *s, const void *data, int len)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2011-09-13 11:44:03 +00:00
|
|
|
assert(ps->outgoingeof == EOF_NO);
|
|
|
|
|
2003-05-06 19:52:31 +00:00
|
|
|
bufchain_add(&ps->pending_output_data, data, len);
|
|
|
|
|
|
|
|
localproxy_try_send(ps);
|
|
|
|
|
|
|
|
return bufchain_size(&ps->pending_output_data);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static int sk_localproxy_write_oob (Socket *s, const void *data, int len)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* oob data is treated as inband; nasty, but nothing really
|
|
|
|
* better we can do
|
|
|
|
*/
|
|
|
|
return sk_localproxy_write(s, data, len);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_localproxy_write_eof (Socket *s)
|
2011-09-13 11:44:03 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt);
|
2011-09-13 11:44:03 +00:00
|
|
|
|
|
|
|
assert(ps->outgoingeof == EOF_NO);
|
|
|
|
ps->outgoingeof = EOF_PENDING;
|
|
|
|
|
|
|
|
localproxy_try_send(ps);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_localproxy_flush (Socket *s)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
/* LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt); */
|
2003-05-06 19:52:31 +00:00
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void sk_localproxy_set_frozen (Socket *s, int is_frozen)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
2018-09-28 18:21:37 +00:00
|
|
|
if (ps->from_cmd < 0)
|
|
|
|
return;
|
|
|
|
|
2003-05-06 19:52:31 +00:00
|
|
|
if (is_frozen)
|
|
|
|
uxsel_del(ps->from_cmd);
|
|
|
|
else
|
|
|
|
uxsel_set(ps->from_cmd, 1, localproxy_select_result);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static const char * sk_localproxy_socket_error (Socket *s)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ps = FROMFIELD(s, LocalProxySocket, sockvt);
|
2003-05-06 19:52:31 +00:00
|
|
|
return ps->error;
|
|
|
|
}
|
|
|
|
|
2016-05-30 21:52:30 +00:00
|
|
|
static void localproxy_select_result(int fd, int event)
|
2003-05-06 19:52:31 +00:00
|
|
|
{
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *s;
|
2003-05-06 19:52:31 +00:00
|
|
|
char buf[20480];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!(s = find234(localproxy_by_fromfd, &fd, localproxy_fromfd_find)) &&
|
2018-09-28 18:21:37 +00:00
|
|
|
!(s = find234(localproxy_by_errfd, &fd, localproxy_errfd_find)) &&
|
2003-05-06 19:52:31 +00:00
|
|
|
!(s = find234(localproxy_by_tofd, &fd, localproxy_tofd_find)) )
|
2016-05-30 21:52:30 +00:00
|
|
|
return; /* boggle */
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
if (event == 1) {
|
2015-11-22 11:50:37 +00:00
|
|
|
if (fd == s->cmd_err) {
|
|
|
|
ret = read(fd, buf, sizeof(buf));
|
2018-09-28 18:21:37 +00:00
|
|
|
if (ret > 0) {
|
2015-11-22 11:50:37 +00:00
|
|
|
log_proxy_stderr(s->plug, &s->pending_error_data, buf, ret);
|
2018-09-28 18:21:37 +00:00
|
|
|
} else {
|
|
|
|
del234(localproxy_by_errfd, s);
|
|
|
|
uxsel_del(s->cmd_err);
|
|
|
|
close(s->cmd_err);
|
|
|
|
s->cmd_err = -1;
|
|
|
|
}
|
2015-11-22 11:50:37 +00:00
|
|
|
} else {
|
|
|
|
assert(fd == s->from_cmd);
|
|
|
|
ret = read(fd, buf, sizeof(buf));
|
2018-09-28 18:21:37 +00:00
|
|
|
if (ret > 0) {
|
2016-05-30 21:52:30 +00:00
|
|
|
plug_receive(s->plug, 0, buf, ret);
|
2018-09-28 18:21:37 +00:00
|
|
|
} else {
|
|
|
|
if (ret < 0) {
|
|
|
|
plug_closing(s->plug, strerror(errno), errno, 0);
|
|
|
|
} else {
|
|
|
|
plug_closing(s->plug, NULL, 0, 0);
|
|
|
|
}
|
|
|
|
del234(localproxy_by_fromfd, s);
|
|
|
|
uxsel_del(s->from_cmd);
|
|
|
|
close(s->from_cmd);
|
|
|
|
s->from_cmd = -1;
|
2015-11-22 11:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
} else if (event == 2) {
|
|
|
|
assert(fd == s->to_cmd);
|
|
|
|
if (localproxy_try_send(s))
|
|
|
|
plug_sent(s->plug, bufchain_size(&s->pending_output_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 06:03:46 +00:00
|
|
|
static const SocketVtable LocalProxySocket_sockvt = {
|
2018-05-27 08:29:33 +00:00
|
|
|
sk_localproxy_plug,
|
|
|
|
sk_localproxy_close,
|
|
|
|
sk_localproxy_write,
|
|
|
|
sk_localproxy_write_oob,
|
|
|
|
sk_localproxy_write_eof,
|
|
|
|
sk_localproxy_flush,
|
|
|
|
sk_localproxy_set_frozen,
|
|
|
|
sk_localproxy_socket_error,
|
|
|
|
NULL, /* peer_info */
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
LocalProxySocket *ret;
|
2015-11-22 11:50:37 +00:00
|
|
|
int to_cmd_pipe[2], from_cmd_pipe[2], cmd_err_pipe[2], pid, proxytype;
|
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;
|
|
|
|
|
2018-05-27 08:29:33 +00:00
|
|
|
ret = snew(LocalProxySocket);
|
|
|
|
ret->sockvt = &LocalProxySocket_sockvt;
|
2003-05-06 19:52:31 +00:00
|
|
|
ret->plug = plug;
|
|
|
|
ret->error = NULL;
|
2011-09-13 11:44:03 +00:00
|
|
|
ret->outgoingeof = EOF_NO;
|
2018-09-28 18:06:07 +00:00
|
|
|
ret->pending_error = 0;
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
bufchain_init(&ret->pending_input_data);
|
|
|
|
bufchain_init(&ret->pending_output_data);
|
2015-11-22 11:50:37 +00:00
|
|
|
bufchain_init(&ret->pending_error_data);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
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
|
|
|
ret->error = dupprintf("pipe: %s", strerror(errno));
|
|
|
|
sfree(cmd);
|
2018-05-27 08:29:33 +00:00
|
|
|
return &ret->sockvt;
|
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();
|
|
|
|
|
|
|
|
if (pid < 0) {
|
|
|
|
ret->error = dupprintf("fork: %s", strerror(errno));
|
|
|
|
sfree(cmd);
|
2018-05-27 08:29:33 +00:00
|
|
|
return &ret->sockvt;
|
2015-10-17 13:06:06 +00:00
|
|
|
} else if (pid == 0) {
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2015-10-17 13:06:06 +00:00
|
|
|
ret->to_cmd = to_cmd_pipe[1];
|
|
|
|
ret->from_cmd = from_cmd_pipe[0];
|
2015-11-22 11:50:37 +00:00
|
|
|
ret->cmd_err = cmd_err_pipe[0];
|
2015-10-17 13:06:06 +00:00
|
|
|
} else {
|
|
|
|
cmd = format_telnet_command(addr, port, conf);
|
|
|
|
ret->to_cmd = open("/dev/null", O_WRONLY);
|
|
|
|
if (ret->to_cmd == -1) {
|
|
|
|
ret->error = dupprintf("/dev/null: %s", strerror(errno));
|
|
|
|
sfree(cmd);
|
2018-05-27 08:29:33 +00:00
|
|
|
return &ret->sockvt;
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
|
|
|
ret->from_cmd = open(cmd, O_RDONLY);
|
|
|
|
if (ret->from_cmd == -1) {
|
|
|
|
ret->error = dupprintf("%s: %s", cmd, strerror(errno));
|
|
|
|
sfree(cmd);
|
2018-05-27 08:29:33 +00:00
|
|
|
return &ret->sockvt;
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
|
|
|
sfree(cmd);
|
2015-11-22 11:50:37 +00:00
|
|
|
ret->cmd_err = -1;
|
2015-10-17 13:06:06 +00:00
|
|
|
}
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
if (!localproxy_by_fromfd)
|
|
|
|
localproxy_by_fromfd = newtree234(localproxy_fromfd_cmp);
|
|
|
|
if (!localproxy_by_tofd)
|
|
|
|
localproxy_by_tofd = newtree234(localproxy_tofd_cmp);
|
2015-11-22 11:50:37 +00:00
|
|
|
if (!localproxy_by_errfd)
|
|
|
|
localproxy_by_errfd = newtree234(localproxy_errfd_cmp);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
add234(localproxy_by_fromfd, ret);
|
|
|
|
add234(localproxy_by_tofd, ret);
|
2015-11-22 11:50:37 +00:00
|
|
|
if (ret->cmd_err >= 0)
|
|
|
|
add234(localproxy_by_errfd, ret);
|
2003-05-06 19:52:31 +00:00
|
|
|
|
|
|
|
uxsel_set(ret->from_cmd, 1, localproxy_select_result);
|
2015-11-25 18:18:45 +00:00
|
|
|
if (ret->cmd_err >= 0)
|
|
|
|
uxsel_set(ret->cmd_err, 1, localproxy_select_result);
|
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-05-27 08:29:33 +00:00
|
|
|
return &ret->sockvt;
|
2003-05-06 19:52:31 +00:00
|
|
|
}
|