Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Remove FLAG_VERBOSE.
Browse files Browse the repository at this point in the history
The global 'int flags' has always been an ugly feature of this code
base, and I suddenly thought that perhaps it's time to start throwing
it out, one flag at a time, until it's totally unused.

My first target is FLAG_VERBOSE. This was usually set by cmdline.c
when it saw a -v option on the program's command line, except that GUI
PuTTY itself sets it unconditionally on startup. And then various bits
of the code would check it in order to decide whether to print a given
message.

In the current system of front-end abstraction traits, there's no
_one_ place that I can move it to. But there are two: every place that
checked FLAG_VERBOSE has access to either a Seat or a LogPolicy. So
now each of those traits has a query method for 'do I want verbose
messages?'.

A good effect of this is that subsidiary Seats, like the ones used in
Uppity for the main SSH server module itself and the server end of
shell channels, now get to have their own verbosity setting instead of
inheriting the one global one. In fact I don't expect any code using
those Seats to be generating any messages at all, but if that changes
later, we'll have a way to control it. (Who knows, perhaps logging in
Uppity might become a thing.)

As part of this cleanup, I've added a new flag to cmdline_tooltype,
called TOOLTYPE_NO_VERBOSE_OPTION. The unconditionally-verbose tools
now set that, and it has the effect of making cmdline.c disallow -v
completely. So where 'putty -v' would previously have been silently
ignored ("I was already verbose"), it's now an error, reminding you
that that option doesn't actually do anything.

Finally, the 'default_logpolicy' provided by uxcons.c and wincons.c
(with identical definitions) has had to move into a new file of its
own, because now it has to ask cmdline.c for the verbosity setting as
well as asking console.c for the rest of its methods. So there's a new
file clicons.c which can only be included by programs that link
against both cmdline.c _and_ one of the *cons.c, and I've renamed the
logpolicy to reflect that.
  • Loading branch information
sgtatham committed Jan 30, 2020
1 parent 06e9f71 commit d20d3b2
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 66 deletions.
11 changes: 7 additions & 4 deletions Recipe
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,12 @@ KEYGEN = sshrsag sshdssg sshecdsag
putty : [G] GUITERM NONSSH WINSSH W_BE_ALL WINMISC winx11 putty.res LIBS
puttytel : [G] GUITERM NONSSH W_BE_NOSSH WINMISC puttytel.res nogss LIBS
plink : [C] winplink wincons NONSSH WINSSH W_BE_ALL logging WINMISC
+ winx11 plink.res winnojmp sessprep noterm winnohlp winselcli LIBS
+ winx11 plink.res winnojmp sessprep noterm winnohlp winselcli
+ clicons LIBS
pscp : [C] pscp winsftp wincons WINSSH BE_SSH SFTP wildcard WINMISC
+ pscp.res winnojmp winnohlp winselcli LIBS
+ pscp.res winnojmp winnohlp winselcli clicons LIBS
psftp : [C] psftp winsftp wincons WINSSH BE_SSH SFTP wildcard WINMISC
+ psftp.res winnojmp winnohlp winselcli LIBS
+ psftp.res winnojmp winnohlp winselcli clicons LIBS

pageant : [G] winpgnt pageant sshrsa sshpubk sshdes ARITH sshmd5 version
+ tree234 MISC sshaes sshsha winsecur winpgntc aqsync sshdss sshsh256
Expand All @@ -363,7 +364,7 @@ puttytel : [X] GTKTERM uxmisc misc ldisc settings uxsel U_BE_NOSSH
+ nogss utils memory GTKMAIN

plink : [U] uxplink uxcons NONSSH UXSSH U_BE_ALL logging UXMISC uxsignal
+ ux_x11 noterm uxnogtk sessprep cmdline
+ ux_x11 noterm uxnogtk sessprep cmdline clicons

PUTTYGEN_UNIX = KEYGEN sshprime sshdes ARITH sshmd5 version sshprng
+ sshrand uxnoise sshsha MISC sshrsa sshdss uxcons uxstore uxmisc
Expand All @@ -374,7 +375,9 @@ puttygen : [U] cmdgen PUTTYGEN_UNIX
cgtest : [UT] cgtest PUTTYGEN_UNIX

pscp : [U] pscp uxsftp uxcons UXSSH BE_SSH SFTP wildcard UXMISC uxnogtk
+ clicons
psftp : [U] psftp uxsftp uxcons UXSSH BE_SSH SFTP wildcard UXMISC uxnogtk
+ clicons

pageant : [X] uxpgnt uxagentc aqsync pageant sshrsa sshpubk sshdes ARITH
+ sshmd5 version tree234 misc sshaes sshsha sshdss sshsh256 sshsh512
Expand Down
14 changes: 14 additions & 0 deletions clicons.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* clicons.c: definitions limited to tools that link against both
* console.c and cmdline.c.
*/

#include "putty.h"

static const LogPolicyVtable console_cli_logpolicy_vt = {
console_eventlog,
console_askappend,
console_logging_error,
cmdline_lp_verbose,
};
LogPolicy console_cli_logpolicy[1] = {{ &console_cli_logpolicy_vt }};
7 changes: 6 additions & 1 deletion cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ static bool cmdline_check_unavailable(int flag, const char *p)

static bool seen_hostname_argument = false;
static bool seen_port_argument = false;
static bool seen_verbose_option = false;
bool cmdline_verbose(void) { return seen_verbose_option; }
bool cmdline_seat_verbose(Seat *seat) { return cmdline_verbose(); }
bool cmdline_lp_verbose(LogPolicy *lp) { return cmdline_verbose(); }

int cmdline_process_param(const char *p, char *value,
int need_save, Conf *conf)
Expand Down Expand Up @@ -452,7 +456,8 @@ int cmdline_process_param(const char *p, char *value,
}
if (!strcmp(p, "-v")) {
RETURN(1);
flags |= FLAG_VERBOSE;
UNAVAILABLE_IN(TOOLTYPE_NO_VERBOSE_OPTION);
seen_verbose_option = true;
}
if (!strcmp(p, "-l")) {
RETURN(2);
Expand Down
5 changes: 5 additions & 0 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ StripCtrlChars *nullseat_stripctrl_new(
Seat *seat, BinarySink *bs_out, SeatInteractionContext sic) {return NULL;}
bool nullseat_set_trust_status(Seat *seat, bool tr) { return false; }
bool nullseat_set_trust_status_vacuously(Seat *seat, bool tr) { return true; }
bool nullseat_verbose_no(Seat *seat) { return false; }
bool nullseat_verbose_yes(Seat *seat) { return true; }

bool null_lp_verbose_no(LogPolicy *lp) { return false; }
bool null_lp_verbose_yes(LogPolicy *lp) { return true; }

void sk_free_peer_info(SocketPeerInfo *pi)
{
Expand Down
7 changes: 4 additions & 3 deletions pscp.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static const SeatVtable pscp_seat_vt = {
nullseat_get_window_pixel_size,
console_stripctrl_new,
nullseat_set_trust_status_vacuously,
cmdline_seat_verbose,
};
static Seat pscp_seat[1] = {{ &pscp_seat_vt }};

Expand Down Expand Up @@ -450,9 +451,9 @@ static void do_cmd(char *host, char *user, char *cmd)
}
conf_set_bool(conf, CONF_nopty, true);

logctx = log_init(default_logpolicy, conf);
logctx = log_init(console_cli_logpolicy, conf);

platform_psftp_pre_conn_setup();
platform_psftp_pre_conn_setup(console_cli_logpolicy);

err = backend_init(&ssh_backend, pscp_seat, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
Expand Down Expand Up @@ -2257,7 +2258,7 @@ int psftp_main(int argc, char *argv[])
i++; /* skip next argument */
} else if (ret == 1) {
/* We have our own verbosity in addition to `flags'. */
if (flags & FLAG_VERBOSE)
if (cmdline_verbose())
verbose = true;
} else if (strcmp(argv[i], "-pgpfp") == 0) {
pgp_fingerprints();
Expand Down
7 changes: 4 additions & 3 deletions psftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static const SeatVtable psftp_seat_vt = {
nullseat_get_window_pixel_size,
console_stripctrl_new,
nullseat_set_trust_status_vacuously,
cmdline_seat_verbose,
};
static Seat psftp_seat[1] = {{ &psftp_seat_vt }};

Expand Down Expand Up @@ -2709,9 +2710,9 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
"exec sftp-server");
conf_set_bool(conf, CONF_ssh_subsys2, false);

psftp_logctx = log_init(default_logpolicy, conf);
psftp_logctx = log_init(console_cli_logpolicy, conf);

platform_psftp_pre_conn_setup();
platform_psftp_pre_conn_setup(console_cli_logpolicy);

err = backend_init(&ssh_backend, psftp_seat, &backend, psftp_logctx, conf,
conf_get_str(conf, CONF_host),
Expand Down Expand Up @@ -2798,7 +2799,7 @@ int psftp_main(int argc, char *argv[])
i++; /* skip next argument */
} else if (ret == 1) {
/* We have our own verbosity in addition to `flags'. */
if (flags & FLAG_VERBOSE)
if (cmdline_verbose())
verbose = true;
} else if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "-?") == 0 ||
Expand Down
2 changes: 1 addition & 1 deletion psftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ char *ssh_sftp_get_cmdline(const char *prompt, bool backend_required);
* Platform-specific function called when we're about to make a
* network connection.
*/
void platform_psftp_pre_conn_setup(void);
void platform_psftp_pre_conn_setup(LogPolicy *lp);

/*
* The main program in psftp.c. Called from main() in the platform-
Expand Down
37 changes: 30 additions & 7 deletions putty.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,6 @@ extern const char *const appname;
/*
* Some global flags denoting the type of application.
*
* FLAG_VERBOSE is set when the user requests verbose details.
*
* FLAG_INTERACTIVE is set when a full interactive shell session is
* being run, _either_ because no remote command has been provided
* _or_ because the application is GUI and can't run non-
Expand All @@ -596,7 +594,6 @@ extern const char *const appname;
* headers. It's probably best if those ones start from 0x1000, to
* avoid collision.
*/
#define FLAG_VERBOSE 0x0001
#define FLAG_INTERACTIVE 0x0002
GLOBAL int flags;

Expand Down Expand Up @@ -954,6 +951,11 @@ struct SeatVtable {
* prompts by malicious servers.
*/
bool (*set_trust_status)(Seat *seat, bool trusted);

/*
* Ask the seat whether it would like verbose messages.
*/
bool (*verbose)(Seat *seat);
};

static inline size_t seat_output(
Expand Down Expand Up @@ -999,6 +1001,8 @@ static inline StripCtrlChars *seat_stripctrl_new(
{ return seat->vt->stripctrl_new(seat, bs, sic); }
static inline bool seat_set_trust_status(Seat *seat, bool trusted)
{ return seat->vt->set_trust_status(seat, trusted); }
static inline bool seat_verbose(Seat *seat)
{ return seat->vt->verbose(seat); }

/* Unlike the seat's actual method, the public entry point
* seat_connection_fatal is a wrapper function with a printf-like API,
Expand Down Expand Up @@ -1051,6 +1055,8 @@ StripCtrlChars *nullseat_stripctrl_new(
Seat *seat, BinarySink *bs_out, SeatInteractionContext sic);
bool nullseat_set_trust_status(Seat *seat, bool trusted);
bool nullseat_set_trust_status_vacuously(Seat *seat, bool trusted);
bool nullseat_verbose_no(Seat *seat);
bool nullseat_verbose_yes(Seat *seat);

/*
* Seat functions provided by the platform's console-application
Expand All @@ -1076,6 +1082,7 @@ bool console_set_trust_status(Seat *seat, bool trusted);
* Other centralised seat functions.
*/
int filexfer_get_userpass_input(Seat *seat, prompts_t *p, bufchain *input);
bool cmdline_seat_verbose(Seat *seat);

/*
* Data type 'TermWin', which is a vtable encapsulating all the
Expand Down Expand Up @@ -1681,6 +1688,11 @@ struct LogPolicyVtable {
* file :-)
*/
void (*logging_error)(LogPolicy *lp, const char *event);

/*
* Ask whether extra verbose log messages are required.
*/
bool (*verbose)(LogPolicy *lp);
};
struct LogPolicy {
const LogPolicyVtable *vt;
Expand All @@ -1694,6 +1706,19 @@ static inline int lp_askappend(
{ return lp->vt->askappend(lp, filename, callback, ctx); }
static inline void lp_logging_error(LogPolicy *lp, const char *event)
{ lp->vt->logging_error(lp, event); }
static inline bool lp_verbose(LogPolicy *lp)
{ return lp->vt->verbose(lp); }

/* Defined in conscli.c, used in several console command-line tools */
extern LogPolicy console_cli_logpolicy[];

int console_askappend(LogPolicy *lp, Filename *filename,
void (*callback)(void *ctx, int result), void *ctx);
void console_logging_error(LogPolicy *lp, const char *string);
void console_eventlog(LogPolicy *lp, const char *string);
bool null_lp_verbose_yes(LogPolicy *lp);
bool null_lp_verbose_no(LogPolicy *lp);
bool cmdline_lp_verbose(LogPolicy *lp);

LogContext *log_init(LogPolicy *lp, Conf *conf);
void log_free(LogContext *logctx);
Expand Down Expand Up @@ -1725,10 +1750,6 @@ void log_packet(LogContext *logctx, int direction, int type,
const unsigned long *sequence,
unsigned downstream_id, const char *additional_log_text);

/* This is defined by applications that have an obvious logging
* destination like standard error or the GUI. */
extern LogPolicy default_logpolicy[1];

/*
* Exports from testback.c
*/
Expand Down Expand Up @@ -1950,13 +1971,15 @@ void cmdline_run_saved(Conf *);
void cmdline_cleanup(void);
int cmdline_get_passwd_input(prompts_t *p);
bool cmdline_host_ok(Conf *);
bool cmdline_verbose(void);
#define TOOLTYPE_FILETRANSFER 1
#define TOOLTYPE_NONNETWORK 2
#define TOOLTYPE_HOST_ARG 4
#define TOOLTYPE_HOST_ARG_CAN_BE_SESSION 8
#define TOOLTYPE_HOST_ARG_PROTOCOL_PREFIX 16
#define TOOLTYPE_HOST_ARG_FROM_LAUNCHABLE_LOAD 32
#define TOOLTYPE_PORT_ARG 64
#define TOOLTYPE_NO_VERBOSE_OPTION 128
extern int cmdline_tooltype;

void cmdline_error(const char *, ...) PRINTF_LIKE(1, 2);
Expand Down
2 changes: 2 additions & 0 deletions sesschan.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ static const LogPolicyVtable sesschan_logpolicy_vt = {
sesschan_eventlog,
sesschan_askappend,
sesschan_logging_error,
null_lp_verbose_no,
};

static size_t sesschan_seat_output(
Expand Down Expand Up @@ -196,6 +197,7 @@ static const SeatVtable sesschan_seat_vt = {
sesschan_get_window_pixel_size,
nullseat_stripctrl_new,
nullseat_set_trust_status,
nullseat_verbose_no,
};

Channel *sesschan_new(SshChannel *c, LogContext *logctx,
Expand Down
2 changes: 1 addition & 1 deletion ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ static const char *connect_to_host(
ssh->fullhostname = NULL;
*realhost = dupstr(host); /* best we can do */

if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE)) {
if (seat_verbose(ssh->seat) || (flags & FLAG_INTERACTIVE)) {
/* In an interactive session, or in verbose mode, announce
* in the console window that we're a sharing downstream,
* to avoid confusing users as to why this session doesn't
Expand Down
12 changes: 6 additions & 6 deletions ssh1login.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
pq_push(s->ppl.out_pq, pkt);

ppl_logevent("Sent username \"%s\"", s->username);
if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE))
if (seat_verbose(s->ppl.seat) || (flags & FLAG_INTERACTIVE))
ppl_printf("Sent username \"%s\"\r\n", s->username);

crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
Expand Down Expand Up @@ -586,7 +586,7 @@ static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
if (pktin->type == SSH1_SMSG_SUCCESS) {
ppl_logevent("Pageant's response "
"accepted");
if (flags & FLAG_VERBOSE) {
if (seat_verbose(s->ppl.seat)) {
ptrlen comment = ptrlen_from_strbuf(
s->agent_comment);
ppl_printf("Authenticated using RSA "
Expand Down Expand Up @@ -630,7 +630,7 @@ static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
* key file.
*/
bool got_passphrase; /* need not be kept over crReturn */
if (flags & FLAG_VERBOSE)
if (seat_verbose(s->ppl.seat))
ppl_printf("Trying public key authentication.\r\n");
ppl_logevent("Trying public key \"%s\"",
filename_to_str(s->keyfile));
Expand All @@ -644,7 +644,7 @@ static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
char *passphrase = NULL; /* only written after crReturn */
const char *error;
if (!s->privatekey_encrypted) {
if (flags & FLAG_VERBOSE)
if (seat_verbose(s->ppl.seat))
ppl_printf("No passphrase required.\r\n");
passphrase = NULL;
} else {
Expand Down Expand Up @@ -766,7 +766,7 @@ static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
crMaybeWaitUntilV((pktin = ssh1_login_pop(s))
!= NULL);
if (pktin->type == SSH1_SMSG_FAILURE) {
if (flags & FLAG_VERBOSE)
if (seat_verbose(s->ppl.seat))
ppl_printf("Failed to authenticate with"
" our public key.\r\n");
continue; /* go and try something else */
Expand Down Expand Up @@ -1054,7 +1054,7 @@ static void ssh1_login_process_queue(PacketProtocolLayer *ppl)
s->cur_prompt = NULL;
crMaybeWaitUntilV((pktin = ssh1_login_pop(s)) != NULL);
if (pktin->type == SSH1_SMSG_FAILURE) {
if (flags & FLAG_VERBOSE)
if (seat_verbose(s->ppl.seat))
ppl_printf("Access denied\r\n");
ppl_logevent("Authentication refused");
} else if (pktin->type != SSH1_SMSG_SUCCESS) {
Expand Down
8 changes: 4 additions & 4 deletions ssh2userauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
prompt_get_result(s->cur_prompt->prompts[0]);
free_prompts(s->cur_prompt);
} else {
if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE))
if (seat_verbose(s->ppl.seat) || (flags & FLAG_INTERACTIVE))
ppl_printf("Using username \"%s\".\r\n", s->username);
}
s->got_username = true;
Expand Down Expand Up @@ -496,7 +496,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
* anti-spoofing header lines.
*/
if (bufchain_size(&s->banner) &&
(flags & (FLAG_VERBOSE | FLAG_INTERACTIVE))) {
(seat_verbose(s->ppl.seat) || (flags & FLAG_INTERACTIVE))) {
if (s->banner_scc) {
ssh2_userauth_antispoof_msg(
s, "Pre-authentication banner message from server:");
Expand Down Expand Up @@ -727,7 +727,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
} else {
strbuf *agentreq, *sigdata;

if (flags & FLAG_VERBOSE)
if (seat_verbose(s->ppl.seat))
ppl_printf("Authenticating with public key "
"\"%.*s\" from agent\r\n",
PTRLEN_PRINTF(s->comment));
Expand Down Expand Up @@ -836,7 +836,7 @@ static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
* Actually attempt a serious authentication using
* the key.
*/
if (flags & FLAG_VERBOSE)
if (seat_verbose(s->ppl.seat))
ppl_printf("Authenticating with public key \"%s\"\r\n",
s->publickey_comment);

Expand Down
1 change: 1 addition & 0 deletions sshserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ static const SeatVtable server_seat_vt = {
nullseat_get_window_pixel_size,
nullseat_stripctrl_new,
nullseat_set_trust_status,
nullseat_verbose_no,
};

static void server_socket_log(Plug *plug, int type, SockAddr *addr, int port,
Expand Down
Loading

0 comments on commit d20d3b2

Please sign in to comment.