Skip to content

Commit

Permalink
Option to log proxy setup diagnostics to the terminal.
Browse files Browse the repository at this point in the history
It has three settings: on, off, and 'only until session starts'. The
idea of the last one is that if you use something like 'ssh -v' as
your proxy command, you probably wanted to see the initial SSH
connection-setup messages while you were waiting to see if the
connection would be set up successfully at all, but probably _didn't_
want a slew of diagnostics from rekeys disrupting your terminal in
mid-emacs once the session had got properly under way.

Default is off, to avoid startling people used to the old behaviour. I
wonder if I should have set it more aggressively, though.
  • Loading branch information
sgtatham committed Nov 22, 2015
1 parent 297efff commit 7c65b9c
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 11 deletions.
23 changes: 21 additions & 2 deletions be_misc.c
Expand Up @@ -2,12 +2,16 @@
* be_misc.c: helper functions shared between main network backends.
*/

#include <assert.h>
#include <string.h>

#define DEFINE_PLUG_METHOD_MACROS
#include "putty.h"
#include "network.h"

void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
const char *error_msg, int error_code)
const char *error_msg, int error_code, Conf *conf,
int session_started)
{
char addrbuf[256], *msg;

Expand All @@ -27,7 +31,22 @@ void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
case 2:
/* Proxy-related log messages have their own identifying
* prefix already, put on by our caller. */
msg = dupstr(error_msg);
{
int len, log_to_term;

/* Suffix \r\n temporarily, so we can log to the terminal. */
msg = dupprintf("%s\r\n", error_msg);
len = strlen(msg);
assert(len >= 2);

log_to_term = conf_get_int(conf, CONF_proxy_log_to_term);
if (log_to_term == AUTO)
log_to_term = session_started ? FORCE_OFF : FORCE_ON;
if (log_to_term == FORCE_ON)
from_backend(frontend, TRUE, msg, len);

msg[len-2] = '\0'; /* remove the \r\n again */
}
break;
default:
msg = NULL; /* shouldn't happen, but placate optimiser */
Expand Down
9 changes: 9 additions & 0 deletions config.c
Expand Up @@ -2072,6 +2072,15 @@ void setup_config_box(struct controlbox *b, int midsession,
HELPCTX(proxy_command),
conf_editbox_handler,
I(CONF_proxy_telnet_command), I(1));

ctrl_radiobuttons(s, "Print proxy diagnostics "
"in the terminal window", 'r', 5,
HELPCTX(proxy_main),
conf_radiobutton_handler,
I(CONF_proxy_log_to_term),
"No", I(FORCE_OFF),
"Yes", I(FORCE_ON),
"Only until session starts", I(AUTO), NULL);
}

/*
Expand Down
3 changes: 2 additions & 1 deletion network.h
Expand Up @@ -227,7 +227,8 @@ Socket new_error_socket(const char *errmsg, Plug plug);
* Exports from be_misc.c.
*/
void backend_socket_log(void *frontend, int type, SockAddr addr, int port,
const char *error_msg, int error_code);
const char *error_msg, int error_code, Conf *conf,
int session_started);
typedef struct bufchain_tag bufchain; /* rest of declaration in misc.c */
void log_proxy_stderr(Plug plug, bufchain *buf, const void *vdata, int len);

Expand Down
7 changes: 4 additions & 3 deletions putty.h
Expand Up @@ -279,9 +279,9 @@ enum {
* three-way settings whose values are `always yes', `always
* no', and `decide by some more complex automated means'. This
* is true of line discipline options (local echo and line
* editing), proxy DNS, Close On Exit, and SSH server bug
* workarounds. Accordingly I supply a single enum here to deal
* with them all.
* editing), proxy DNS, proxy terminal logging, Close On Exit, and
* SSH server bug workarounds. Accordingly I supply a single enum
* here to deal with them all.
*/
FORCE_ON, FORCE_OFF, AUTO
};
Expand Down Expand Up @@ -681,6 +681,7 @@ void cleanup_exit(int);
X(STR, NONE, proxy_username) \
X(STR, NONE, proxy_password) \
X(STR, NONE, proxy_telnet_command) \
X(INT, NONE, proxy_log_to_term) \
/* SSH options */ \
X(STR, NONE, remote_cmd) \
X(STR, NONE, remote_cmd2) /* fallback if remote_cmd fails; never loaded or saved */ \
Expand Down
12 changes: 10 additions & 2 deletions raw.c
Expand Up @@ -25,7 +25,9 @@ typedef struct raw_backend_data {
int closed_on_socket_error;
int bufsize;
void *frontend;
int sent_console_eof, sent_socket_eof;
int sent_console_eof, sent_socket_eof, session_started;

Conf *conf;
} *Raw;

static void raw_size(void *handle, int width, int height);
Expand All @@ -41,7 +43,7 @@ static void raw_log(Plug plug, int type, SockAddr addr, int port,
{
Raw raw = (Raw) plug;
backend_socket_log(raw->frontend, type, addr, port,
error_msg, error_code);
error_msg, error_code, raw->conf, raw->session_started);
}

static void raw_check_close(Raw raw)
Expand Down Expand Up @@ -97,6 +99,9 @@ static int raw_receive(Plug plug, int urgent, char *data, int len)
{
Raw raw = (Raw) plug;
c_write(raw, data, len);
/* We count 'session start', for proxy logging purposes, as being
* when data is received from the network and printed. */
raw->session_started = TRUE;
return 1;
}

Expand Down Expand Up @@ -138,6 +143,8 @@ static const char *raw_init(void *frontend_handle, void **backend_handle,
*backend_handle = raw;
raw->sent_console_eof = raw->sent_socket_eof = FALSE;
raw->bufsize = 0;
raw->session_started = FALSE;
raw->conf = conf_copy(conf);

raw->frontend = frontend_handle;

Expand Down Expand Up @@ -184,6 +191,7 @@ static void raw_free(void *handle)

if (raw->s)
sk_close(raw->s);
conf_free(raw->conf);
sfree(raw);
}

Expand Down
3 changes: 2 additions & 1 deletion rlogin.c
Expand Up @@ -49,7 +49,8 @@ static void rlogin_log(Plug plug, int type, SockAddr addr, int port,
{
Rlogin rlogin = (Rlogin) plug;
backend_socket_log(rlogin->frontend, type, addr, port,
error_msg, error_code);
error_msg, error_code,
rlogin->conf, !rlogin->firstbyte);
}

static int rlogin_closing(Plug plug, const char *error_msg, int error_code,
Expand Down
2 changes: 2 additions & 0 deletions settings.c
Expand Up @@ -480,6 +480,7 @@ void save_open_settings(void *sesskey, Conf *conf)
write_setting_s(sesskey, "ProxyUsername", conf_get_str(conf, CONF_proxy_username));
write_setting_s(sesskey, "ProxyPassword", conf_get_str(conf, CONF_proxy_password));
write_setting_s(sesskey, "ProxyTelnetCommand", conf_get_str(conf, CONF_proxy_telnet_command));
write_setting_i(sesskey, "ProxyLogToTerm", conf_get_int(conf, CONF_proxy_log_to_term));
wmap(sesskey, "Environment", conf, CONF_environmt, TRUE);
write_setting_s(sesskey, "UserName", conf_get_str(conf, CONF_username));
write_setting_i(sesskey, "UserNameFromEnvironment", conf_get_int(conf, CONF_username_from_env));
Expand Down Expand Up @@ -759,6 +760,7 @@ void load_open_settings(void *sesskey, Conf *conf)
gpps(sesskey, "ProxyPassword", "", conf, CONF_proxy_password);
gpps(sesskey, "ProxyTelnetCommand", "connect %host %port\\n",
conf, CONF_proxy_telnet_command);
gppi(sesskey, "ProxyLogToTerm", FORCE_OFF, conf, CONF_proxy_log_to_term);
gppmap(sesskey, "Environment", conf, CONF_environmt);
gpps(sesskey, "UserName", "", conf, CONF_username);
gppi(sesskey, "UserNameFromEnvironment", 0, conf, CONF_username_from_env);
Expand Down
7 changes: 6 additions & 1 deletion ssh.c
Expand Up @@ -792,6 +792,7 @@ struct ssh_tag {
int send_ok;
int echoing, editing;

int session_started;
void *frontend;

int ospeed, ispeed; /* temporaries */
Expand Down Expand Up @@ -3070,6 +3071,8 @@ static int do_ssh_init(Ssh ssh, unsigned char c)
crReturn(1);
}

ssh->session_started = TRUE;

s->vstrsize = sizeof(protoname) + 16;
s->vstring = snewn(s->vstrsize, char);
strcpy(s->vstring, protoname);
Expand Down Expand Up @@ -3464,7 +3467,8 @@ static void ssh_socket_log(Plug plug, int type, SockAddr addr, int port,

if (!ssh->attempting_connshare)
backend_socket_log(ssh->frontend, type, addr, port,
error_msg, error_code);
error_msg, error_code, ssh->conf,
ssh->session_started);
}

void ssh_connshare_log(Ssh ssh, int event, const char *logtext,
Expand Down Expand Up @@ -10998,6 +11002,7 @@ static const char *ssh_init(void *frontend_handle, void **backend_handle,
ssh->X11_fwd_enabled = FALSE;
ssh->connshare = NULL;
ssh->attempting_connshare = FALSE;
ssh->session_started = FALSE;

*backend_handle = ssh;

Expand Down
6 changes: 5 additions & 1 deletion telnet.c
Expand Up @@ -197,6 +197,7 @@ typedef struct telnet_tag {
int sb_opt, sb_len;
unsigned char *sb_buf;
int sb_size;
int session_started;

enum {
TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
Expand Down Expand Up @@ -653,7 +654,8 @@ static void telnet_log(Plug plug, int type, SockAddr addr, int port,
{
Telnet telnet = (Telnet) plug;
backend_socket_log(telnet->frontend, type, addr, port,
error_msg, error_code);
error_msg, error_code, telnet->conf,
telnet->session_started);
}

static int telnet_closing(Plug plug, const char *error_msg, int error_code,
Expand Down Expand Up @@ -687,6 +689,7 @@ static int telnet_receive(Plug plug, int urgent, char *data, int len)
Telnet telnet = (Telnet) plug;
if (urgent)
telnet->in_synch = TRUE;
telnet->session_started = TRUE;
do_telnet_read(telnet, data, len);
return 1;
}
Expand Down Expand Up @@ -737,6 +740,7 @@ static const char *telnet_init(void *frontend_handle, void **backend_handle,
telnet->state = TOP_LEVEL;
telnet->ldisc = NULL;
telnet->pinger = NULL;
telnet->session_started = TRUE;
*backend_handle = telnet;

/*
Expand Down
2 changes: 2 additions & 0 deletions unix/uxpgnt.c
Expand Up @@ -103,6 +103,8 @@ FontSpec *platform_default_fontspec(const char *name) { return fontspec_new("");
Filename *platform_default_filename(const char *name) { return filename_from_str(""); }
char *x_get_default(const char *key) { return NULL; }
void log_eventlog(void *handle, const char *event) {}
int from_backend(void *frontend, int is_stderr, const char *data, int datalen)
{ assert(!"only here to satisfy notional call from backend_socket_log"); }

/*
* Short description of parameters.
Expand Down

0 comments on commit 7c65b9c

Please sign in to comment.