1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-03-22 14:39:24 -05:00

ssh_get_password has become ssh_get_line, so it can handle usernames

as well. This should fix the multiple-reads-on-stdin bug in plink.

[originally from svn r994]
This commit is contained in:
Simon Tatham 2001-03-12 15:31:53 +00:00
parent 9ee21069b5
commit 55659a959f
5 changed files with 148 additions and 105 deletions

19
plink.c
View File

@ -160,12 +160,12 @@ struct input_data {
HANDLE event, eventback; HANDLE event, eventback;
}; };
static int get_password(const char *prompt, char *str, int maxlen) static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
{ {
HANDLE hin, hout; HANDLE hin, hout;
DWORD savemode, i; DWORD savemode, newmode, i;
if (password) { if (is_pw && password) {
static int tried_once = 0; static int tried_once = 0;
if (tried_once) { if (tried_once) {
@ -186,8 +186,12 @@ static int get_password(const char *prompt, char *str, int maxlen)
} }
GetConsoleMode(hin, &savemode); GetConsoleMode(hin, &savemode);
SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); if (is_pw)
newmode &= ~ENABLE_ECHO_INPUT;
else
newmode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hin, newmode);
WriteFile(hout, prompt, strlen(prompt), &i, NULL); WriteFile(hout, prompt, strlen(prompt), &i, NULL);
ReadFile(hin, str, maxlen-1, &i, NULL); ReadFile(hin, str, maxlen-1, &i, NULL);
@ -197,7 +201,8 @@ static int get_password(const char *prompt, char *str, int maxlen)
if ((int)i > maxlen) i = maxlen-1; else i = i - 2; if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
str[i] = '\0'; str[i] = '\0';
WriteFile(hout, "\r\n", 2, &i, NULL); if (is_pw)
WriteFile(hout, "\r\n", 2, &i, NULL);
return 1; return 1;
} }
@ -265,7 +270,7 @@ int main(int argc, char **argv) {
int skcount, sksize; int skcount, sksize;
int connopen; int connopen;
ssh_get_password = get_password; ssh_get_line = get_line;
sklist = NULL; skcount = sksize = 0; sklist = NULL; skcount = sksize = 0;

17
psftp.c
View File

@ -860,10 +860,10 @@ static void ssh_sftp_init(void) {
} }
static char *password = NULL; static char *password = NULL;
static int get_password(const char *prompt, char *str, int maxlen) static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
{ {
HANDLE hin, hout; HANDLE hin, hout;
DWORD savemode, i; DWORD savemode, newmode, i;
if (password) { if (password) {
static int tried_once = 0; static int tried_once = 0;
@ -886,8 +886,12 @@ static int get_password(const char *prompt, char *str, int maxlen)
} }
GetConsoleMode(hin, &savemode); GetConsoleMode(hin, &savemode);
SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); if (is_pw)
newmode &= ~ENABLE_ECHO_INPUT;
else
newmode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hin, newmode);
WriteFile(hout, prompt, strlen(prompt), &i, NULL); WriteFile(hout, prompt, strlen(prompt), &i, NULL);
ReadFile(hin, str, maxlen-1, &i, NULL); ReadFile(hin, str, maxlen-1, &i, NULL);
@ -897,7 +901,8 @@ static int get_password(const char *prompt, char *str, int maxlen)
if ((int)i > maxlen) i = maxlen-1; else i = i - 2; if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
str[i] = '\0'; str[i] = '\0';
WriteFile(hout, "\r\n", 2, &i, NULL); if (is_pw)
WriteFile(hout, "\r\n", 2, &i, NULL);
return 1; return 1;
} }
@ -948,7 +953,7 @@ int main(int argc, char *argv[])
char *err; char *err;
flags = FLAG_STDERR; flags = FLAG_STDERR;
ssh_get_password = &get_password; ssh_get_line = &get_line;
init_winsock(); init_winsock();
sk_init(); sk_init();

View File

@ -382,7 +382,8 @@ extern Backend telnet_backend;
* Exports from ssh.c. * Exports from ssh.c.
*/ */
extern int (*ssh_get_password)(const char *prompt, char *str, int maxlen); extern int (*ssh_get_line)(const char *prompt, char *str, int maxlen,
int is_pw);
extern Backend ssh_backend; extern Backend ssh_backend;
/* /*

19
scp.c
View File

@ -395,12 +395,12 @@ static void bump(char *fmt, ...)
exit(1); exit(1);
} }
static int get_password(const char *prompt, char *str, int maxlen) static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
{ {
HANDLE hin, hout; HANDLE hin, hout;
DWORD savemode, i; DWORD savemode, newmode, i;
if (password) { if (is_pw && password) {
static int tried_once = 0; static int tried_once = 0;
if (tried_once) { if (tried_once) {
@ -423,8 +423,12 @@ static int get_password(const char *prompt, char *str, int maxlen)
bump("Cannot get standard input/output handles"); bump("Cannot get standard input/output handles");
GetConsoleMode(hin, &savemode); GetConsoleMode(hin, &savemode);
SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) | newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT); if (is_pw)
newmode &= ~ENABLE_ECHO_INPUT;
else
newmode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hin, newmode);
WriteFile(hout, prompt, strlen(prompt), &i, NULL); WriteFile(hout, prompt, strlen(prompt), &i, NULL);
ReadFile(hin, str, maxlen-1, &i, NULL); ReadFile(hin, str, maxlen-1, &i, NULL);
@ -434,7 +438,8 @@ static int get_password(const char *prompt, char *str, int maxlen)
if ((int)i > maxlen) i = maxlen-1; else i = i - 2; if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
str[i] = '\0'; str[i] = '\0';
WriteFile(hout, "\r\n", 2, &i, NULL); if (is_pw)
WriteFile(hout, "\r\n", 2, &i, NULL);
} }
return 1; return 1;
@ -1223,7 +1228,7 @@ int main(int argc, char *argv[])
default_protocol = PROT_TELNET; default_protocol = PROT_TELNET;
flags = FLAG_STDERR; flags = FLAG_STDERR;
ssh_get_password = &get_password; ssh_get_line = &get_line;
init_winsock(); init_winsock();
sk_init(); sk_init();

195
ssh.c
View File

@ -269,7 +269,8 @@ static const struct ssh_compress *sccomp = NULL;
static const struct ssh_kex *kex = NULL; static const struct ssh_kex *kex = NULL;
static const struct ssh_signkey *hostkey = NULL; static const struct ssh_signkey *hostkey = NULL;
static unsigned char ssh2_session_id[20]; static unsigned char ssh2_session_id[20];
int (*ssh_get_password)(const char *prompt, char *str, int maxlen) = NULL; int (*ssh_get_line)(const char *prompt, char *str, int maxlen,
int is_pw) = NULL;
static char *savedhost; static char *savedhost;
static int savedport; static int savedport;
@ -1503,43 +1504,56 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
static int pos = 0; static int pos = 0;
static char c; static char c;
if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { if ((flags & FLAG_INTERACTIVE) && !*cfg.username) {
c_write_str("login as: "); if (ssh_get_line) {
ssh_send_ok = 1; if (!ssh_get_line("login as: ",
while (pos >= 0) { username, sizeof(username), FALSE)) {
crWaitUntil(!ispkt); /*
while (inlen--) switch (c = *in++) { * get_line failed to get a username.
case 10: case 13: * Terminate.
username[pos] = 0; */
pos = -1; logevent("No username provided. Abandoning session.");
break; ssh_state = SSH_STATE_CLOSED;
case 8: case 127: crReturn(1);
if (pos > 0) { }
c_write_str("\b \b"); } else {
pos--; c_write_str("login as: ");
} ssh_send_ok = 1;
break; while (pos >= 0) {
case 21: case 27: crWaitUntil(!ispkt);
while (pos > 0) { while (inlen--) switch (c = *in++) {
c_write_str("\b \b"); case 10: case 13:
pos--; username[pos] = 0;
} pos = -1;
break; break;
case 3: case 4: case 8: case 127:
random_save_seed(); if (pos > 0) {
exit(0); c_write_str("\b \b");
break; pos--;
default: }
if (((c >= ' ' && c <= '~') || break;
((unsigned char)c >= 160)) && pos < 40) { case 21: case 27:
username[pos++] = c; while (pos > 0) {
c_write(&c, 1); c_write_str("\b \b");
} pos--;
break; }
} break;
} case 3: case 4:
c_write_str("\r\n"); random_save_seed();
username[strcspn(username, "\n\r")] = '\0'; exit(0);
} else { break;
default:
if (((c >= ' ' && c <= '~') ||
((unsigned char)c >= 160)) && pos < 40) {
username[pos++] = c;
c_write(&c, 1);
}
break;
}
}
c_write_str("\r\n");
username[strcspn(username, "\n\r")] = '\0';
}
} else {
strncpy(username, cfg.username, 99); strncpy(username, cfg.username, 99);
username[99] = '\0'; username[99] = '\0';
} }
@ -1741,13 +1755,12 @@ static int do_ssh1_login(unsigned char *in, int inlen, int ispkt)
sfree(comment); sfree(comment);
} }
if (ssh_get_password) { if (ssh_get_line) {
if (!ssh_get_password(prompt, password, sizeof(password))) { if (!ssh_get_line(prompt, password, sizeof(password), TRUE)) {
/* /*
* get_password failed to get a password (for * get_line failed to get a password (for example
* example because one was supplied on the command * because one was supplied on the command line
* line which has already failed to work). * which has already failed to work). Terminate.
* Terminate.
*/ */
logevent("No more passwords to try"); logevent("No more passwords to try");
ssh_state = SSH_STATE_CLOSED; ssh_state = SSH_STATE_CLOSED;
@ -2843,40 +2856,53 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
*/ */
pos = 0; pos = 0;
if ((flags & FLAG_INTERACTIVE) && !*cfg.username) { if ((flags & FLAG_INTERACTIVE) && !*cfg.username) {
c_write_str("login as: "); if (ssh_get_line) {
ssh_send_ok = 1; if (!ssh_get_line("login as: ",
while (pos >= 0) { username, sizeof(username), FALSE)) {
crWaitUntilV(!ispkt); /*
while (inlen--) switch (c = *in++) { * get_line failed to get a username.
case 10: case 13: * Terminate.
username[pos] = 0; */
pos = -1; logevent("No username provided. Abandoning session.");
break; ssh_state = SSH_STATE_CLOSED;
case 8: case 127: crReturn(1);
if (pos > 0) { }
c_write_str("\b \b"); } else {
pos--; c_write_str("login as: ");
} ssh_send_ok = 1;
break; while (pos >= 0) {
case 21: case 27: crWaitUntilV(!ispkt);
while (pos > 0) { while (inlen--) switch (c = *in++) {
c_write_str("\b \b"); case 10: case 13:
pos--; username[pos] = 0;
} pos = -1;
break; break;
case 3: case 4: case 8: case 127:
random_save_seed(); if (pos > 0) {
exit(0); c_write_str("\b \b");
break; pos--;
default: }
if (((c >= ' ' && c <= '~') || break;
((unsigned char)c >= 160)) && pos < 40) { case 21: case 27:
username[pos++] = c; while (pos > 0) {
c_write(&c, 1); c_write_str("\b \b");
} pos--;
break; }
} break;
} case 3: case 4:
random_save_seed();
exit(0);
break;
default:
if (((c >= ' ' && c <= '~') ||
((unsigned char)c >= 160)) && pos < 40) {
username[pos++] = c;
c_write(&c, 1);
}
break;
}
}
}
c_write_str("\r\n"); c_write_str("\r\n");
username[strcspn(username, "\n\r")] = '\0'; username[strcspn(username, "\n\r")] = '\0';
} else { } else {
@ -3158,13 +3184,14 @@ static void do_ssh2_authconn(unsigned char *in, int inlen, int ispkt)
} }
if (need_pw) { if (need_pw) {
if (ssh_get_password) { if (ssh_get_line) {
if (!ssh_get_password(pwprompt, password, sizeof(password))) { if (!ssh_get_line(pwprompt, password,
sizeof(password), TRUE)) {
/* /*
* get_password failed to get a password (for * get_line failed to get a password (for
* example because one was supplied on the command * example because one was supplied on the
* line which has already failed to work). * command line which has already failed to
* Terminate. * work). Terminate.
*/ */
logevent("No more passwords to try"); logevent("No more passwords to try");
ssh_state = SSH_STATE_CLOSED; ssh_state = SSH_STATE_CLOSED;