Windows: the SSH Login dialog (keys and Pageant, the agent alone, a key file) and PuTTY key import

This commit is contained in:
Claude committed 2026-11-12 15:44:04 +00:00
1 parent 089d9e3555
commit 5e1a895245
3 files changed
+423 -8

No files matched your search

+20
View File
@@ -105,6 +105,26 @@ BEGIN
PUSHBUTTON "Cancel", IDCANCEL, 202, 150, 50, 14
END
/* the Remote page's SSH login: by OpenSSH's keys and the agent, the
agent alone, or a key file (and a PuTTY key's import) */
IDD_LOGIN DIALOG 0, 0, 260, 150
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "SSH Login"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Log in &with:", -1, 8, 10, 56, 8
COMBOBOX IDC_K_HOW, 66, 8, 186, 60, CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP
LTEXT "&Key file (OpenSSH's, PEM or PuTTY's .ppk):", -1, 8, 28, 244, 8
EDITTEXT IDC_K_KEY, 8, 40, 186, 12, ES_AUTOHSCROLL
PUSHBUTTON "&Browse...", IDC_K_BROWSE, 200, 39, 52, 14
PUSHBUTTON "&Import PuTTY Key...", IDC_K_IMPORT, 8, 58, 90, 14
LTEXT "&Agent (empty: Pageant, else OpenSSH's; or pageant, or a pipe):", -1, 8, 80, 244, 8
EDITTEXT IDC_K_AGENT, 8, 92, 244, 12, ES_AUTOHSCROLL
LTEXT "", IDC_K_STATE, 8, 108, 244, 8
DEFPUSHBUTTON "OK", IDOK, 146, 128, 50, 14
PUSHBUTTON "Cancel", IDCANCEL, 202, 128, 50, 14
END
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,0
PRODUCTVERSION 1,0,0,0
+393 -8
View File
@@ -9,7 +9,10 @@
* modem's link), the protocol libdmodemctl's dmc_net; every Windows from
* 95 on has them. SSH: libdmodemssh (the apps' own client, on libssh),
* on a thread of its own that posts its events to the main window; it
* reads OpenSSH's config, known_hosts and keys in the profile's .ssh.
* reads OpenSSH's config, known_hosts and keys in the profile's .ssh,
* and asks Pageant (or else OpenSSH's own agent) for its keys; or logs
* in by the agent alone, or by a key file: OpenSSH's, PEM or PuTTY's
* .ppk, which can also be imported into .ssh in OpenSSH's form.
* The Windows 95 build (Open Watcom, no libssh) runs Windows' own
* ssh.exe in a pseudo console (ConPTY) instead, when it finds one
* (Windows 10 1809 and later): the ConPTY calls are looked up at run
@@ -22,8 +25,12 @@
#include "app.h"
#include "resource.h"
#ifdef DMODEM_HAVE_SSH
#include <commdlg.h>
#endif
static HWND page, view, type_box, proto_box, host_ed, port_ed, user_ed,
go_btn, status_txt;
go_btn, status_txt, login_btn;
/* the protocols in the list's order */
static const int protos[] = { DMC_NET_SSH, DMC_NET_TELNET, DMC_NET_RLOGIN };
@@ -149,6 +156,10 @@ static void controls()
EnableWindow(port_ed, idle);
EnableWindow(user_ed, idle);
SetWindowText(go_btn, idle ? "&Connect" : "&Disconnect");
if (login_btn) {
int i = (int)SendMessage(proto_box, CB_GETCURSEL, 0, 0);
EnableWindow(login_btn, idle && (i < 0 || protos[i] == DMC_NET_SSH));
}
}
static void show(const char *s, int n)
@@ -395,6 +406,61 @@ struct SshMsg {
static int tried_keys, asks, exit_status;
static void ini(char *out, int n);
/*
* How it logs in: [remote] login ("auto", "agent", "key"), key (a file)
* and agent ("": Pageant, else OpenSSH's), in dmodem.ini
*/
struct Login {
char how[16], key[MAX_PATH], agent[MAX_PATH];
};
static void login_read(Login *l)
{
char path[MAX_PATH];
ini(path, sizeof(path));
GetPrivateProfileString("remote", "login", "auto", l->how, sizeof(l->how), path);
GetPrivateProfileString("remote", "key", "", l->key, sizeof(l->key), path);
GetPrivateProfileString("remote", "agent", "", l->agent, sizeof(l->agent), path);
}
static void login_write(const Login *l)
{
char path[MAX_PATH];
ini(path, sizeof(path));
WritePrivateProfileString("remote", "login", l->how, path);
WritePrivateProfileString("remote", "key", l->key, path);
WritePrivateProfileString("remote", "agent", l->agent, path);
}
/* a file's text (malloc'd, wiped and freed by text_free), or NULL */
static char *text_read(const char *path)
{
FILE *f = fopen(path, "rb");
if (!f)
return NULL;
char *t = (char *)malloc(65536);
size_t n = t ? fread(t, 1, 65535, f) : 0;
fclose(f);
if (t)
t[n] = '\0';
return t;
}
static void text_free(char *t)
{
if (t) {
SecureZeroMemory(t, strlen(t));
free(t);
}
}
/* the key file's text, while its login goes on; passphrases tried */
static char *key_text;
static int tried_file, key_tries;
/* the connection's dmc_ssh */
static dmc_ssh *ssh_conn()
{
@@ -464,6 +530,8 @@ static void get_answer(HWND d, int id, char *out, int n)
questions (four at most) */
struct Ask {
const char *head, *text;
const char *label; /* the one answer's (prompts 0): "Password:" */
const char *ok; /* its button's: "&Log In" */
int prompts;
char answers[4][256];
};
@@ -476,13 +544,16 @@ static INT_PTR CALLBACK ask_proc(HWND d, UINT msg, WPARAM wp, LPARAM lp)
a = (Ask *)lp;
SetWindowLongPtr(d, DWLP_USER, (LONG_PTR)a);
set_text(d, IDC_A_HEAD, a->head);
if (a->ok)
SetDlgItemText(d, IDOK, a->ok);
set_text(d, IDC_A_TEXT, a->text);
int n = a->prompts ? a->prompts : 1;
HWND first = NULL;
for (int i = 0; i < 4; i++) {
int echo = 0;
const char *q = i >= n ? "" : a->prompts ?
dmc_ssh_prompt(ssh->s, i, &echo) : "Password:";
dmc_ssh_prompt(ssh->s, i, &echo) :
a->label ? a->label : "Password:";
set_text(d, IDC_A_Q0 + i, q);
ShowWindow(GetDlgItem(d, IDC_A_Q0 + i), i < n ? SW_SHOW : SW_HIDE);
ShowWindow(GetDlgItem(d, IDC_A_E0 + i), i < n && echo ? SW_SHOW : SW_HIDE);
@@ -590,15 +661,93 @@ static void ssh_ask(const char *head, const char *text, int prompts)
SecureZeroMemory(&a, sizeof(a));
}
/* what's tried, in OpenSSH's order: its keys, then the server's
questions or a password (three times at most) */
/* a key's passphrase asked (why: a try's failure, or ""): FALSE when
cancelled */
static bool ask_passphrase(HWND owner, const char *file, const char *why,
const char *ok, char *out, int n)
{
Ask a;
memset(&a, 0, sizeof(a));
const char *name = strrchr(file, '\\');
char head[MAX_PATH + 32];
_snprintf(head, sizeof(head), "The passphrase for %s", name ? name + 1 : file);
head[sizeof(head) - 1] = '\0';
a.head = head;
a.text = why;
a.label = "Passphrase:";
a.ok = ok;
INT_PTR r = DialogBoxParam(app.inst, MAKEINTRESOURCE(IDD_ASK), owner,
ask_proc, (LPARAM)&a);
if (r == IDOK)
lstrcpyn(out, a.answers[0], n);
SecureZeroMemory(&a, sizeof(a));
return r == IDOK;
}
/* the login by the key file (its passphrase asked if it has one): FALSE
when there's nothing to try */
static bool key_login(const char *file)
{
Ssh *c = ssh;
text_free(key_text);
key_text = text_read(file);
tried_file = 1;
if (!key_text) {
char m[MAX_PATH + 64];
_snprintf(m, sizeof(m), "\r\n[The key file %s can't be read]\r\n", file);
m[sizeof(m) - 1] = '\0';
show_line(m);
return false;
}
if (!dmc_ssh_key_encrypted(key_text)) {
if (dmc_ssh_auth_key(ssh->s, key_text, NULL) == 0)
return true;
show_line("\r\n[The key file is not a key that can be read]\r\n");
return false;
}
const char *why = "";
for (key_tries = 0; key_tries < 3; key_tries++) {
char pass[256];
bool ok = ask_passphrase(app.main, file, why, NULL, pass, sizeof(pass));
/* (the connection may have gone while it asked) */
if (c != ssh || !key_text) {
SecureZeroMemory(pass, sizeof(pass));
return true;
}
if (!ok)
return false;
int r = dmc_ssh_auth_key(ssh->s, key_text, pass);
SecureZeroMemory(pass, sizeof(pass));
if (r == 0)
return true;
why = "That passphrase does not open the key.";
}
return false;
}
/* what's tried, in OpenSSH's order: its keys and the agent (or what was
chosen: the agent alone, or a key file), then the server's questions
or a password (three times at most) */
static void ssh_auth(int methods, const char *why)
{
if ((methods & DMC_SSH_AUTH_PUBLICKEY) && !tried_keys) {
Login l;
login_read(&l);
bool file = !lstrcmpi(l.how, "key") && l.key[0];
if ((methods & DMC_SSH_AUTH_PUBLICKEY) && file && !tried_file) {
if (key_login(l.key))
return;
} else if ((methods & DMC_SSH_AUTH_PUBLICKEY) && !file && !tried_keys) {
tried_keys = 1;
dmc_ssh_auth_auto(ssh->s);
if (!lstrcmpi(l.how, "agent"))
dmc_ssh_auth_agent(ssh->s);
else
dmc_ssh_auth_auto(ssh->s);
return;
}
text_free(key_text);
key_text = NULL;
if (!ssh)
return;
if (asks++ >= 3) {
dmc_ssh_auth_cancel(ssh->s);
return;
@@ -626,6 +775,8 @@ void remote_ssh(LPARAM l)
dmc_ssh_free(c->s);
free(c);
if (c == ssh) {
text_free(key_text);
key_text = NULL;
ssh = NULL;
shell = 0;
if (!m->data && exit_status > 0) {
@@ -703,6 +854,14 @@ static bool start_ssh(const char *host, int port, const char *user)
/* OpenSSH's config and known hosts, in the profile's .ssh */
dmc_ssh_option(s, "config", "");
dmc_ssh_option(s, "knownhosts", "");
/* the agent: the one given, else Pageant, else OpenSSH's */
Login l;
login_read(&l);
if (l.agent[0])
dmc_ssh_option(s, "agent", l.agent);
if (!lstrcmpi(l.how, "agent") && !dmc_ssh_agent_reachable(l.agent))
show_line(l.agent[0] ? "[No agent answers there: is it running?]\r\n" :
"[Neither Pageant nor OpenSSH's agent is running]\r\n");
Ssh *c = (Ssh *)calloc(1, sizeof(Ssh));
c->s = s;
DWORD id;
@@ -716,11 +875,225 @@ static bool start_ssh(const char *host, int port, const char *user)
ssh = c;
shell = 0;
tried_keys = 0;
tried_file = 0;
text_free(key_text);
key_text = NULL;
asks = 0;
exit_status = 0;
return true;
}
/* the profile's .ssh folder (made if it isn't there) into out */
static void ssh_dir(char *out, int n)
{
const char *home = getenv("USERPROFILE");
_snprintf(out, n, "%s\\.ssh", home ? home : ".");
out[n - 1] = '\0';
CreateDirectory(out, NULL);
}
/* a key file chosen into the dialog's field (ppk: PuTTY's alone) */
static bool choose_key(HWND d, bool ppk, char *file, int n)
{
char dir[MAX_PATH];
ssh_dir(dir, sizeof(dir));
OPENFILENAME o;
memset(&o, 0, sizeof(o));
o.lStructSize = OPENFILENAME_SIZE_VERSION_400;
o.hwndOwner = d;
o.lpstrFilter = ppk ? "PuTTY keys (*.ppk)\0*.ppk\0" :
"All keys\0*.*\0PuTTY keys (*.ppk)\0*.ppk\0";
o.lpstrFile = file;
o.nMaxFile = (DWORD)n;
o.lpstrInitialDir = dir;
o.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
file[0] = '\0';
return GetOpenFileName(&o) != 0;
}
/* a text written to path: true if it all was */
static bool text_write(const char *path, const char *t)
{
FILE *f = fopen(path, "wb");
if (!f)
return false;
size_t n = strlen(t);
bool ok = fwrite(t, 1, n, f) == n;
return fclose(f) == 0 && ok;
}
/* a .ppk imported: written to .ssh in OpenSSH's form (with the same
passphrase), its name into out; false if not */
static bool import_ppk(HWND d, const char *ppk, char *out, int n)
{
char *text = text_read(ppk);
if (!text) {
MessageBox(d, "That file can't be read.", "Import PuTTY Key", MB_ICONWARNING);
return false;
}
char pass[256] = "", *key = NULL, *pub = NULL;
bool enc = dmc_ssh_key_encrypted(text) != 0;
const char *why = "";
for (;;) {
if (enc && !ask_passphrase(d, ppk, why, "OK", pass, sizeof(pass)))
break;
if (dmc_ssh_key_export(text, pass, pass, &key) == 0 &&
dmc_ssh_key_public(key, pass, &pub) == 0)
break;
if (key) {
SecureZeroMemory(key, strlen(key));
dmc_ssh_free_string(key);
key = NULL;
}
if (!enc) {
MessageBox(d, "That file is not a PuTTY key that can be read.",
"Import PuTTY Key", MB_ICONWARNING);
break;
}
why = "That passphrase does not open the key.";
}
SecureZeroMemory(pass, sizeof(pass));
bool ok = false;
if (key && pub) {
/* (.ssh\<its name>, a name not yet taken) */
char dir[MAX_PATH], base[MAX_PATH], path[MAX_PATH], pubpath[MAX_PATH + 8];
ssh_dir(dir, sizeof(dir));
const char *slash = strrchr(ppk, '\\');
lstrcpyn(base, slash ? slash + 1 : ppk, sizeof(base));
char *dot = strrchr(base, '.');
if (dot && !lstrcmpi(dot, ".ppk"))
*dot = '\0';
if (!base[0])
lstrcpy(base, "putty-key");
for (int i = 1; i < 100; i++) {
if (i == 1)
_snprintf(path, sizeof(path), "%s\\%s", dir, base);
else
_snprintf(path, sizeof(path), "%s\\%s-%d", dir, base, i);
path[sizeof(path) - 1] = '\0';
_snprintf(pubpath, sizeof(pubpath), "%s.pub", path);
pubpath[sizeof(pubpath) - 1] = '\0';
if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES &&
GetFileAttributes(pubpath) == INVALID_FILE_ATTRIBUTES)
break;
}
/* (the .pub line with the .ppk's comment) */
char line[4096], comment[256] = "";
const char *cm = strstr(text, "\nComment: ");
if (cm) {
int cl = (int)strcspn(cm + 10, "\r\n");
_snprintf(comment, sizeof(comment), " %.*s", cl, cm + 10);
comment[sizeof(comment) - 1] = '\0';
}
_snprintf(line, sizeof(line), "%s%s\n", pub, comment);
line[sizeof(line) - 1] = '\0';
ok = text_write(path, key) && text_write(pubpath, line);
if (ok) {
lstrcpyn(out, path, n);
char m[MAX_PATH + 96];
_snprintf(m, sizeof(m), "Imported as %s, and set as the key file.", path);
m[sizeof(m) - 1] = '\0';
MessageBox(d, m, "Import PuTTY Key", MB_ICONINFORMATION);
} else {
MessageBox(d, "The key could not be saved in .ssh.", "Import PuTTY Key",
MB_ICONWARNING);
}
}
if (key) {
SecureZeroMemory(key, strlen(key));
dmc_ssh_free_string(key);
}
dmc_ssh_free_string(pub);
text_free(text);
return ok;
}
/* whether the agent typed answers, under it */
static void agent_state(HWND d)
{
char a[MAX_PATH];
GetDlgItemText(d, IDC_K_AGENT, a, sizeof(a));
SetDlgItemText(d, IDC_K_STATE, dmc_ssh_agent_reachable(a) ?
(a[0] ? "An agent answers there." :
FindWindow("Pageant", "Pageant") ? "Pageant is running." :
"OpenSSH's agent is running.") :
a[0] ? "No agent answers there." :
"Neither Pageant nor OpenSSH's agent is running.");
}
/* the ways in the list's order, as dmodem.ini has them */
static const char *const hows[] = { "auto", "agent", "key" };
static INT_PTR CALLBACK login_proc(HWND d, UINT msg, WPARAM wp, LPARAM lp)
{
Login *l = (Login *)GetWindowLongPtr(d, DWLP_USER);
switch (msg) {
case WM_INITDIALOG: {
l = (Login *)lp;
SetWindowLongPtr(d, DWLP_USER, (LONG_PTR)l);
HWND how = GetDlgItem(d, IDC_K_HOW);
SendMessage(how, CB_ADDSTRING, 0, (LPARAM)"My keys and the agent, as ssh does");
SendMessage(how, CB_ADDSTRING, 0, (LPARAM)"The agent's keys only");
SendMessage(how, CB_ADDSTRING, 0, (LPARAM)"A key file");
int sel = 0;
for (int i = 0; i < 3; i++)
if (!lstrcmpi(l->how, hows[i]))
sel = i;
SendMessage(how, CB_SETCURSEL, (WPARAM)sel, 0);
SetDlgItemText(d, IDC_K_KEY, l->key);
SetDlgItemText(d, IDC_K_AGENT, l->agent);
agent_state(d);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wp)) {
case IDC_K_BROWSE: {
char f[MAX_PATH];
if (choose_key(d, false, f, sizeof(f))) {
SetDlgItemText(d, IDC_K_KEY, f);
SendDlgItemMessage(d, IDC_K_HOW, CB_SETCURSEL, 2, 0);
}
return TRUE;
}
case IDC_K_IMPORT: {
char f[MAX_PATH], out[MAX_PATH];
if (choose_key(d, true, f, sizeof(f)) && import_ppk(d, f, out, sizeof(out))) {
SetDlgItemText(d, IDC_K_KEY, out);
SendDlgItemMessage(d, IDC_K_HOW, CB_SETCURSEL, 2, 0);
}
return TRUE;
}
case IDC_K_AGENT:
if (HIWORD(wp) == EN_CHANGE)
agent_state(d);
return TRUE;
case IDOK: {
int i = (int)SendDlgItemMessage(d, IDC_K_HOW, CB_GETCURSEL, 0, 0);
lstrcpyn(l->how, hows[i >= 0 && i < 3 ? i : 0], sizeof(l->how));
GetDlgItemText(d, IDC_K_KEY, l->key, sizeof(l->key));
GetDlgItemText(d, IDC_K_AGENT, l->agent, sizeof(l->agent));
EndDialog(d, IDOK);
return TRUE;
}
case IDCANCEL:
EndDialog(d, IDCANCEL);
return TRUE;
}
break;
}
return FALSE;
}
/* the SSH Login dialog: how SSH logs in, kept */
static void login_clicked()
{
Login l;
login_read(&l);
if (DialogBoxParam(app.inst, MAKEINTRESOURCE(IDD_LOGIN), app.main,
login_proc, (LPARAM)&l) == IDOK)
login_write(&l);
}
#else
static bool start_ssh(const char *host, int port, const char *user)
@@ -929,6 +1302,7 @@ static void proto_changed()
if (!cur[0] || !strcmp(cur, "22") || !strcmp(cur, "23") ||
!strcmp(cur, "513"))
SetWindowText(port_ed, p);
controls();
}
static void remote_command(HWND p, int id, int code)
@@ -938,6 +1312,11 @@ static void remote_command(HWND p, int id, int code)
case IDC_R_GO:
connect_clicked();
break;
#ifdef DMODEM_HAVE_SSH
case IDC_R_LOGIN:
login_clicked();
break;
#endif
case IDC_R_PROTO:
if (code == CBN_SELCHANGE)
proto_changed();
@@ -969,6 +1348,10 @@ HWND remote_page_new(HWND parent)
IDC_R_USER, WS_EX_CLIENTEDGE);
go_btn = make_child("BUTTON", "&Connect", WS_TABSTOP | BS_DEFPUSHBUTTON,
page, IDC_R_GO);
#ifdef DMODEM_HAVE_SSH
/* (how SSH logs in: keys, the agent, a PuTTY key) */
login_btn = make_child("BUTTON", "&Login...", WS_TABSTOP, page, IDC_R_LOGIN);
#endif
view = termview_new(page, IDC_R_TERM, typed, NULL);
status_txt = make_child("STATIC", "Not connected", SS_LEFTNOWORDWRAP,
page, IDC_R_STATUS);
@@ -1007,7 +1390,7 @@ void remote_page_layout(HWND p, int w, int h)
x += 88;
MoveWindow(GetDlgItem(p, IDC_R_HOSTLABEL), x, m + 4, 44, bh, TRUE);
x += 48;
int hw = w - x - 44 - 52 - 40 - 96 - 90 - 4 * m;
int hw = w - x - 44 - 52 - 40 - 96 - 90 - 4 * m - (login_btn ? 64 + m : 0);
if (hw < 80)
hw = 80;
MoveWindow(host_ed, x, m, hw, bh, TRUE);
@@ -1020,6 +1403,8 @@ void remote_page_layout(HWND p, int w, int h)
x += 40;
MoveWindow(user_ed, x, m, 96, bh, TRUE);
MoveWindow(go_btn, w - m - 90, m, 90, bh, TRUE);
if (login_btn)
MoveWindow(login_btn, w - 2 * m - 90 - 64, m, 64, bh, TRUE);
MoveWindow(view, m, 2 * m + bh, w - 2 * m, h - 4 * m - 2 * bh, TRUE);
int tw = 200;
MoveWindow(status_txt, m, h - m - bh + 4, w - 3 * m - tw - 64, bh - 4,
+10
View File
@@ -54,6 +54,7 @@
#define IDC_R_STATUS 1109
#define IDC_R_TYPELABEL 1110
#define IDC_R_TYPE 1111
#define IDC_R_LOGIN 1112
/* the Settings page */
#define IDC_GROUPS 600
@@ -137,4 +138,13 @@
#define IDC_A_E0 967
#define IDC_A_E0P 971
/* the Remote page's SSH Login dialog: the way, the key file, the agent */
#define IDD_LOGIN 980
#define IDC_K_HOW 981
#define IDC_K_KEY 982
#define IDC_K_BROWSE 983
#define IDC_K_IMPORT 984
#define IDC_K_AGENT 985
#define IDC_K_STATE 986
#endif