2001-12-31 16:15:19 +00:00
|
|
|
/*
|
2006-04-23 18:26:03 +00:00
|
|
|
* wincons.c - various interactive-prompt routines shared between
|
2004-04-27 12:31:57 +00:00
|
|
|
* the Windows console PuTTY tools
|
2001-12-31 16:15:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "putty.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "ssh.h"
|
|
|
|
|
|
|
|
int console_batch_mode = FALSE;
|
|
|
|
|
2002-03-06 20:13:22 +00:00
|
|
|
/*
|
|
|
|
* Clean up and exit.
|
|
|
|
*/
|
|
|
|
void cleanup_exit(int code)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
|
|
|
sk_cleanup();
|
|
|
|
|
2003-01-12 13:44:35 +00:00
|
|
|
random_save_seed();
|
2002-03-06 20:13:22 +00:00
|
|
|
|
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:10:51 +00:00
|
|
|
void set_busy_status(Frontend *frontend, int status)
|
2005-02-15 17:05:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:10:51 +00:00
|
|
|
void notify_remote_exit(Frontend *frontend)
|
2004-11-27 13:20:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-18 21:42:48 +00:00
|
|
|
void timer_change_notify(unsigned long next)
|
2004-11-27 13:20:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:10:51 +00:00
|
|
|
int verify_ssh_host_key(Frontend *frontend, char *host, int port,
|
2015-05-15 09:12:06 +00:00
|
|
|
const char *keytype, char *keystr, char *fingerprint,
|
2005-02-17 18:34:24 +00:00
|
|
|
void (*callback)(void *ctx, int result), void *ctx)
|
2001-12-31 16:15:19 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
HANDLE hin;
|
|
|
|
DWORD savemode, i;
|
|
|
|
|
|
|
|
static const char absentmsg_batch[] =
|
|
|
|
"The server's host key is not cached in the registry. You\n"
|
|
|
|
"have no guarantee that the server is the computer you\n"
|
|
|
|
"think it is.\n"
|
2003-06-26 14:19:33 +00:00
|
|
|
"The server's %s key fingerprint is:\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"%s\n"
|
|
|
|
"Connection abandoned.\n";
|
|
|
|
static const char absentmsg[] =
|
|
|
|
"The server's host key is not cached in the registry. You\n"
|
|
|
|
"have no guarantee that the server is the computer you\n"
|
|
|
|
"think it is.\n"
|
2003-06-26 14:19:33 +00:00
|
|
|
"The server's %s key fingerprint is:\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"%s\n"
|
|
|
|
"If you trust this host, enter \"y\" to add the key to\n"
|
|
|
|
"PuTTY's cache and carry on connecting.\n"
|
|
|
|
"If you want to carry on connecting just once, without\n"
|
|
|
|
"adding the key to the cache, enter \"n\".\n"
|
|
|
|
"If you do not trust this host, press Return to abandon the\n"
|
|
|
|
"connection.\n"
|
|
|
|
"Store key in cache? (y/n) ";
|
|
|
|
|
|
|
|
static const char wrongmsg_batch[] =
|
|
|
|
"WARNING - POTENTIAL SECURITY BREACH!\n"
|
|
|
|
"The server's host key does not match the one PuTTY has\n"
|
|
|
|
"cached in the registry. This means that either the\n"
|
|
|
|
"server administrator has changed the host key, or you\n"
|
|
|
|
"have actually connected to another computer pretending\n"
|
|
|
|
"to be the server.\n"
|
2003-06-26 14:19:33 +00:00
|
|
|
"The new %s key fingerprint is:\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"%s\n"
|
|
|
|
"Connection abandoned.\n";
|
|
|
|
static const char wrongmsg[] =
|
|
|
|
"WARNING - POTENTIAL SECURITY BREACH!\n"
|
|
|
|
"The server's host key does not match the one PuTTY has\n"
|
|
|
|
"cached in the registry. This means that either the\n"
|
|
|
|
"server administrator has changed the host key, or you\n"
|
|
|
|
"have actually connected to another computer pretending\n"
|
|
|
|
"to be the server.\n"
|
2003-06-26 14:19:33 +00:00
|
|
|
"The new %s key fingerprint is:\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"%s\n"
|
|
|
|
"If you were expecting this change and trust the new key,\n"
|
|
|
|
"enter \"y\" to update PuTTY's cache and continue connecting.\n"
|
|
|
|
"If you want to carry on connecting but without updating\n"
|
|
|
|
"the cache, enter \"n\".\n"
|
|
|
|
"If you want to abandon the connection completely, press\n"
|
|
|
|
"Return to cancel. Pressing Return is the ONLY guaranteed\n"
|
|
|
|
"safe choice.\n"
|
|
|
|
"Update cached key? (y/n, Return cancels connection) ";
|
|
|
|
|
|
|
|
static const char abandoned[] = "Connection abandoned.\n";
|
|
|
|
|
|
|
|
char line[32];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the key against the registry.
|
|
|
|
*/
|
|
|
|
ret = verify_host_key(host, port, keytype, keystr);
|
|
|
|
|
|
|
|
if (ret == 0) /* success - key matched OK */
|
2005-02-17 18:34:24 +00:00
|
|
|
return 1;
|
2001-12-31 16:15:19 +00:00
|
|
|
|
|
|
|
if (ret == 2) { /* key was different */
|
|
|
|
if (console_batch_mode) {
|
2003-06-26 14:19:33 +00:00
|
|
|
fprintf(stderr, wrongmsg_batch, keytype, fingerprint);
|
2005-02-17 18:34:24 +00:00
|
|
|
return 0;
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
2003-06-26 14:19:33 +00:00
|
|
|
fprintf(stderr, wrongmsg, keytype, fingerprint);
|
2001-12-31 16:15:19 +00:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
if (ret == 1) { /* key was absent */
|
|
|
|
if (console_batch_mode) {
|
2003-06-26 14:19:33 +00:00
|
|
|
fprintf(stderr, absentmsg_batch, keytype, fingerprint);
|
2005-02-17 18:34:24 +00:00
|
|
|
return 0;
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
2003-06-26 14:19:33 +00:00
|
|
|
fprintf(stderr, absentmsg, keytype, fingerprint);
|
2001-12-31 16:15:19 +00:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
|
2017-02-15 06:03:50 +00:00
|
|
|
line[0] = '\0'; /* fail safe if ReadFile returns no data */
|
|
|
|
|
2001-12-31 16:15:19 +00:00
|
|
|
hin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode(hin, &savemode);
|
|
|
|
SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
|
|
|
|
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
|
|
|
|
ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
|
|
|
|
SetConsoleMode(hin, savemode);
|
|
|
|
|
|
|
|
if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
|
|
|
|
if (line[0] == 'y' || line[0] == 'Y')
|
|
|
|
store_host_key(host, port, keytype, keystr);
|
2005-02-17 18:34:24 +00:00
|
|
|
return 1;
|
2001-12-31 16:15:19 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, abandoned);
|
2005-02-17 18:34:24 +00:00
|
|
|
return 0;
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:10:51 +00:00
|
|
|
void update_specials_menu(Frontend *frontend)
|
2003-04-04 20:21:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-12-31 16:15:19 +00:00
|
|
|
/*
|
2004-12-23 02:24:07 +00:00
|
|
|
* Ask whether the selected algorithm is acceptable (since it was
|
2001-12-31 16:15:19 +00:00
|
|
|
* below the configured 'warn' threshold).
|
|
|
|
*/
|
2018-09-12 08:10:51 +00:00
|
|
|
int askalg(Frontend *frontend, const char *algtype, const char *algname,
|
2005-02-17 18:34:24 +00:00
|
|
|
void (*callback)(void *ctx, int result), void *ctx)
|
2001-12-31 16:15:19 +00:00
|
|
|
{
|
|
|
|
HANDLE hin;
|
|
|
|
DWORD savemode, i;
|
|
|
|
|
|
|
|
static const char msg[] =
|
2004-12-23 02:24:07 +00:00
|
|
|
"The first %s supported by the server is\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"%s, which is below the configured warning threshold.\n"
|
|
|
|
"Continue with connection? (y/n) ";
|
|
|
|
static const char msg_batch[] =
|
2004-12-23 02:24:07 +00:00
|
|
|
"The first %s supported by the server is\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"%s, which is below the configured warning threshold.\n"
|
|
|
|
"Connection abandoned.\n";
|
|
|
|
static const char abandoned[] = "Connection abandoned.\n";
|
|
|
|
|
|
|
|
char line[32];
|
|
|
|
|
|
|
|
if (console_batch_mode) {
|
2004-12-23 02:24:07 +00:00
|
|
|
fprintf(stderr, msg_batch, algtype, algname);
|
2005-02-17 18:34:24 +00:00
|
|
|
return 0;
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
|
|
|
|
2004-12-23 02:24:07 +00:00
|
|
|
fprintf(stderr, msg, algtype, algname);
|
2001-12-31 16:15:19 +00:00
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
hin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode(hin, &savemode);
|
|
|
|
SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
|
|
|
|
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
|
|
|
|
ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
|
|
|
|
SetConsoleMode(hin, savemode);
|
|
|
|
|
|
|
|
if (line[0] == 'y' || line[0] == 'Y') {
|
2005-02-17 18:34:24 +00:00
|
|
|
return 1;
|
2001-12-31 16:15:19 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, abandoned);
|
2005-02-17 18:34:24 +00:00
|
|
|
return 0;
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:10:51 +00:00
|
|
|
int askhk(Frontend *frontend, const char *algname, const char *betteralgs,
|
2016-03-27 17:08:49 +00:00
|
|
|
void (*callback)(void *ctx, int result), void *ctx)
|
|
|
|
{
|
|
|
|
HANDLE hin;
|
|
|
|
DWORD savemode, i;
|
|
|
|
|
|
|
|
static const char msg[] =
|
|
|
|
"The first host key type we have stored for this server\n"
|
|
|
|
"is %s, which is below the configured warning threshold.\n"
|
|
|
|
"The server also provides the following types of host key\n"
|
|
|
|
"above the threshold, which we do not have stored:\n"
|
|
|
|
"%s\n"
|
|
|
|
"Continue with connection? (y/n) ";
|
|
|
|
static const char msg_batch[] =
|
|
|
|
"The first host key type we have stored for this server\n"
|
|
|
|
"is %s, which is below the configured warning threshold.\n"
|
|
|
|
"The server also provides the following types of host key\n"
|
|
|
|
"above the threshold, which we do not have stored:\n"
|
|
|
|
"%s\n"
|
|
|
|
"Connection abandoned.\n";
|
|
|
|
static const char abandoned[] = "Connection abandoned.\n";
|
|
|
|
|
|
|
|
char line[32];
|
|
|
|
|
|
|
|
if (console_batch_mode) {
|
|
|
|
fprintf(stderr, msg_batch, algname, betteralgs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, msg, algname, betteralgs);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
hin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode(hin, &savemode);
|
|
|
|
SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
|
|
|
|
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
|
|
|
|
ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
|
|
|
|
SetConsoleMode(hin, savemode);
|
|
|
|
|
|
|
|
if (line[0] == 'y' || line[0] == 'Y') {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, abandoned);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-31 16:15:19 +00:00
|
|
|
/*
|
|
|
|
* Ask whether to wipe a session log file before writing to it.
|
|
|
|
* Returns 2 for wipe, 1 for append, 0 for cancel (don't log).
|
|
|
|
*/
|
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.
This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).
LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.
One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.
While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
2018-10-10 18:26:18 +00:00
|
|
|
static int console_askappend(LogPolicy *lp, Filename *filename,
|
|
|
|
void (*callback)(void *ctx, int result),
|
|
|
|
void *ctx)
|
2001-12-31 16:15:19 +00:00
|
|
|
{
|
|
|
|
HANDLE hin;
|
|
|
|
DWORD savemode, i;
|
|
|
|
|
|
|
|
static const char msgtemplate[] =
|
|
|
|
"The session log file \"%.*s\" already exists.\n"
|
|
|
|
"You can overwrite it with a new session log,\n"
|
|
|
|
"append your session log to the end of it,\n"
|
|
|
|
"or disable session logging for this session.\n"
|
|
|
|
"Enter \"y\" to wipe the file, \"n\" to append to it,\n"
|
|
|
|
"or just press Return to disable logging.\n"
|
|
|
|
"Wipe the log file? (y/n, Return cancels logging) ";
|
|
|
|
|
|
|
|
static const char msgtemplate_batch[] =
|
|
|
|
"The session log file \"%.*s\" already exists.\n"
|
|
|
|
"Logging will not be enabled.\n";
|
|
|
|
|
|
|
|
char line[32];
|
|
|
|
|
|
|
|
if (console_batch_mode) {
|
2011-10-02 11:01:57 +00:00
|
|
|
fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path);
|
2001-12-31 16:15:19 +00:00
|
|
|
fflush(stderr);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-02 11:01:57 +00:00
|
|
|
fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path);
|
2001-12-31 16:15:19 +00:00
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
hin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode(hin, &savemode);
|
|
|
|
SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
|
|
|
|
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
|
|
|
|
ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
|
|
|
|
SetConsoleMode(hin, savemode);
|
|
|
|
|
|
|
|
if (line[0] == 'y' || line[0] == 'Y')
|
|
|
|
return 2;
|
|
|
|
else if (line[0] == 'n' || line[0] == 'N')
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Warn about the obsolescent key file format.
|
2002-10-26 12:58:13 +00:00
|
|
|
*
|
|
|
|
* Uniquely among these functions, this one does _not_ expect a
|
|
|
|
* frontend handle. This means that if PuTTY is ported to a
|
|
|
|
* platform which requires frontend handles, this function will be
|
|
|
|
* an anomaly. Fortunately, the problem it addresses will not have
|
|
|
|
* been present on that platform, so it can plausibly be
|
|
|
|
* implemented as an empty function.
|
2001-12-31 16:15:19 +00:00
|
|
|
*/
|
|
|
|
void old_keyfile_warning(void)
|
|
|
|
{
|
|
|
|
static const char message[] =
|
2005-03-10 16:36:05 +00:00
|
|
|
"You are loading an SSH-2 private key which has an\n"
|
2001-12-31 16:15:19 +00:00
|
|
|
"old version of the file format. This means your key\n"
|
|
|
|
"file is not fully tamperproof. Future versions of\n"
|
|
|
|
"PuTTY may stop supporting this private key format,\n"
|
|
|
|
"so we recommend you convert your key to the new\n"
|
|
|
|
"format.\n"
|
|
|
|
"\n"
|
|
|
|
"Once the key is loaded into PuTTYgen, you can perform\n"
|
|
|
|
"this conversion simply by saving it again.\n";
|
|
|
|
|
|
|
|
fputs(message, stderr);
|
|
|
|
}
|
|
|
|
|
2005-03-19 02:26:58 +00:00
|
|
|
/*
|
|
|
|
* Display the fingerprints of the PGP Master Keys to the user.
|
|
|
|
*/
|
|
|
|
void pgp_fingerprints(void)
|
|
|
|
{
|
|
|
|
fputs("These are the fingerprints of the PuTTY PGP Master Keys. They can\n"
|
|
|
|
"be used to establish a trust path from this executable to another\n"
|
|
|
|
"one. See the manual for more information.\n"
|
|
|
|
"(Note: these fingerprints have nothing to do with SSH!)\n"
|
|
|
|
"\n"
|
2018-08-25 13:36:25 +00:00
|
|
|
"PuTTY Master Key as of " PGP_MASTER_KEY_YEAR
|
|
|
|
" (" PGP_MASTER_KEY_DETAILS "):\n"
|
2015-09-02 17:31:24 +00:00
|
|
|
" " PGP_MASTER_KEY_FP "\n\n"
|
2018-08-25 13:36:25 +00:00
|
|
|
"Previous Master Key (" PGP_PREV_MASTER_KEY_YEAR
|
|
|
|
", " PGP_PREV_MASTER_KEY_DETAILS "):\n"
|
|
|
|
" " PGP_PREV_MASTER_KEY_FP "\n", stdout);
|
2005-03-19 02:26:58 +00:00
|
|
|
}
|
|
|
|
|
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.
This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).
LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.
One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.
While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
2018-10-10 18:26:18 +00:00
|
|
|
static void console_logging_error(LogPolicy *lp, const char *string)
|
2003-01-21 19:18:06 +00:00
|
|
|
{
|
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.
This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).
LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.
One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.
While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
2018-10-10 18:26:18 +00:00
|
|
|
/* Ordinary Event Log entries are displayed in the same way as
|
|
|
|
* logging errors, but only in verbose mode */
|
|
|
|
fprintf(stderr, "%s\n", string);
|
|
|
|
fflush(stderr);
|
2003-01-21 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.
This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).
LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.
One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.
While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
2018-10-10 18:26:18 +00:00
|
|
|
static void console_eventlog(LogPolicy *lp, const char *string)
|
2001-12-31 16:15:19 +00:00
|
|
|
{
|
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.
This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).
LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.
One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.
While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
2018-10-10 18:26:18 +00:00
|
|
|
/* Ordinary Event Log entries are displayed in the same way as
|
|
|
|
* logging errors, but only in verbose mode */
|
|
|
|
if (flags & FLAG_VERBOSE)
|
|
|
|
console_logging_error(lp, string);
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
|
|
|
|
2005-10-30 20:24:09 +00:00
|
|
|
static void console_data_untrusted(HANDLE hout, const char *data, int len)
|
|
|
|
{
|
|
|
|
DWORD dummy;
|
2018-09-19 17:22:36 +00:00
|
|
|
bufchain sanitised;
|
|
|
|
void *vdata;
|
|
|
|
|
|
|
|
bufchain_init(&sanitised);
|
|
|
|
sanitise_term_data(&sanitised, data, len);
|
|
|
|
while (bufchain_size(&sanitised) > 0) {
|
|
|
|
bufchain_prefix(&sanitised, &vdata, &len);
|
|
|
|
WriteFile(hout, vdata, len, &dummy, NULL);
|
|
|
|
bufchain_consume(&sanitised, len);
|
|
|
|
}
|
2005-10-30 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 06:22:56 +00:00
|
|
|
int console_get_userpass_input(prompts_t *p)
|
2001-12-31 16:15:19 +00:00
|
|
|
{
|
2018-06-03 20:48:08 +00:00
|
|
|
HANDLE hin = INVALID_HANDLE_VALUE, hout = INVALID_HANDLE_VALUE;
|
2005-10-30 20:24:09 +00:00
|
|
|
size_t curr_prompt;
|
2001-12-31 16:15:19 +00:00
|
|
|
|
2005-10-30 20:24:09 +00:00
|
|
|
/*
|
|
|
|
* Zero all the results, in case we abort half-way through.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
int i;
|
2007-01-09 18:24:07 +00:00
|
|
|
for (i = 0; i < (int)p->n_prompts; i++)
|
2011-10-02 11:50:45 +00:00
|
|
|
prompt_set_result(p->prompts[i], "");
|
2005-10-30 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:35:53 +00:00
|
|
|
/*
|
|
|
|
* The prompts_t might contain a message to be displayed but no
|
|
|
|
* actual prompt. More usually, though, it will contain
|
|
|
|
* questions that the user needs to answer, in which case we
|
|
|
|
* need to ensure that we're able to get the answers.
|
|
|
|
*/
|
|
|
|
if (p->n_prompts) {
|
|
|
|
if (console_batch_mode)
|
|
|
|
return 0;
|
|
|
|
hin = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
if (hin == INVALID_HANDLE_VALUE) {
|
|
|
|
fprintf(stderr, "Cannot get standard input handle\n");
|
|
|
|
cleanup_exit(1);
|
|
|
|
}
|
|
|
|
}
|
2005-10-30 20:24:09 +00:00
|
|
|
|
2009-03-03 18:35:53 +00:00
|
|
|
/*
|
|
|
|
* And if we have anything to print, we need standard output.
|
|
|
|
*/
|
|
|
|
if ((p->name_reqd && p->name) || p->instruction || p->n_prompts) {
|
|
|
|
hout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (hout == INVALID_HANDLE_VALUE) {
|
|
|
|
fprintf(stderr, "Cannot get standard output handle\n");
|
|
|
|
cleanup_exit(1);
|
|
|
|
}
|
2005-10-30 20:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Preamble.
|
|
|
|
*/
|
|
|
|
/* We only print the `name' caption if we have to... */
|
|
|
|
if (p->name_reqd && p->name) {
|
|
|
|
size_t l = strlen(p->name);
|
|
|
|
console_data_untrusted(hout, p->name, l);
|
|
|
|
if (p->name[l-1] != '\n')
|
|
|
|
console_data_untrusted(hout, "\n", 1);
|
|
|
|
}
|
|
|
|
/* ...but we always print any `instruction'. */
|
|
|
|
if (p->instruction) {
|
|
|
|
size_t l = strlen(p->instruction);
|
|
|
|
console_data_untrusted(hout, p->instruction, l);
|
|
|
|
if (p->instruction[l-1] != '\n')
|
|
|
|
console_data_untrusted(hout, "\n", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) {
|
|
|
|
|
2011-10-02 11:50:45 +00:00
|
|
|
DWORD savemode, newmode;
|
|
|
|
int len;
|
2005-10-30 20:24:09 +00:00
|
|
|
prompt_t *pr = p->prompts[curr_prompt];
|
2001-12-31 16:15:19 +00:00
|
|
|
|
|
|
|
GetConsoleMode(hin, &savemode);
|
|
|
|
newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
|
2005-10-30 20:24:09 +00:00
|
|
|
if (!pr->echo)
|
2001-12-31 16:15:19 +00:00
|
|
|
newmode &= ~ENABLE_ECHO_INPUT;
|
|
|
|
else
|
|
|
|
newmode |= ENABLE_ECHO_INPUT;
|
|
|
|
SetConsoleMode(hin, newmode);
|
|
|
|
|
2005-10-30 20:24:09 +00:00
|
|
|
console_data_untrusted(hout, pr->prompt, strlen(pr->prompt));
|
|
|
|
|
2011-10-02 11:50:45 +00:00
|
|
|
len = 0;
|
|
|
|
while (1) {
|
|
|
|
DWORD ret = 0;
|
|
|
|
BOOL r;
|
|
|
|
|
|
|
|
prompt_ensure_result_size(pr, len * 5 / 4 + 512);
|
|
|
|
|
|
|
|
r = ReadFile(hin, pr->result + len, pr->resultsize - len - 1,
|
|
|
|
&ret, NULL);
|
|
|
|
|
|
|
|
if (!r || ret == 0) {
|
|
|
|
len = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len += ret;
|
|
|
|
if (pr->result[len - 1] == '\n') {
|
|
|
|
len--;
|
|
|
|
if (pr->result[len - 1] == '\r')
|
|
|
|
len--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-12-31 16:15:19 +00:00
|
|
|
|
|
|
|
SetConsoleMode(hin, savemode);
|
|
|
|
|
2005-10-30 20:24:09 +00:00
|
|
|
if (!pr->echo) {
|
|
|
|
DWORD dummy;
|
|
|
|
WriteFile(hout, "\r\n", 2, &dummy, NULL);
|
|
|
|
}
|
2001-12-31 16:15:19 +00:00
|
|
|
|
2011-10-02 11:50:45 +00:00
|
|
|
if (len < 0) {
|
|
|
|
return 0; /* failure due to read error */
|
|
|
|
}
|
|
|
|
|
|
|
|
pr->result[len] = '\0';
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
2005-10-30 20:24:09 +00:00
|
|
|
|
|
|
|
return 1; /* success */
|
2001-12-31 16:15:19 +00:00
|
|
|
}
|
2002-10-24 14:48:08 +00:00
|
|
|
|
Refactor the LogContext type.
LogContext is now the owner of the logevent() function that back ends
and so forth are constantly calling. Previously, logevent was owned by
the Frontend, which would store the message into its list for the GUI
Event Log dialog (or print it to standard error, or whatever) and then
pass it _back_ to LogContext to write to the currently open log file.
Now it's the other way round: LogContext gets the message from the
back end first, writes it to its log file if it feels so inclined, and
communicates it back to the front end.
This means that lots of parts of the back end system no longer need to
have a pointer to a full-on Frontend; the only thing they needed it
for was logging, so now they just have a LogContext (which many of
them had to have anyway, e.g. for logging SSH packets or session
traffic).
LogContext itself also doesn't get a full Frontend pointer any more:
it now talks back to the front end via a little vtable of its own
called LogPolicy, which contains the method that passes Event Log
entries through, the old askappend() function that decides whether to
truncate a pre-existing log file, and an emergency function for
printing an especially prominent message if the log file can't be
created. One minor nice effect of this is that console and GUI apps
can implement that last function subtly differently, so that Unix
console apps can write it with a plain \n instead of the \r\n
(harmless but inelegant) that the old centralised implementation
generated.
One other consequence of this is that the LogContext has to be
provided to backend_init() so that it's available to backends from the
instant of creation, rather than being provided via a separate API
call a couple of function calls later, because backends have typically
started doing things that need logging (like making network
connections) before the call to backend_provide_logctx. Fortunately,
there's no case in the whole code base where we don't already have
logctx by the time we make a backend (so I don't actually remember why
I ever delayed providing one). So that shortens the backend API by one
function, which is always nice.
While I'm tidying up, I've also moved the printf-style logeventf() and
the handy logevent_and_free() into logging.c, instead of having copies
of them scattered around other places. This has also let me remove
some stub functions from a couple of outlying applications like
Pageant. Finally, I've removed the pointless "_tag" at the end of
LogContext's official struct name.
2018-10-10 18:26:18 +00:00
|
|
|
static const LogPolicyVtable default_logpolicy_vt = {
|
|
|
|
console_eventlog,
|
|
|
|
console_askappend,
|
|
|
|
console_logging_error,
|
|
|
|
};
|
|
|
|
LogPolicy default_logpolicy[1] = {{ &default_logpolicy_vt }};
|