1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-10 01:48:00 +00:00

PSCP: After a password supplied with `-pw' fails to authenticate,

we should _not_ fall back to console input for a second attempt,
because this hangs batch files.

[originally from svn r513]
This commit is contained in:
Simon Tatham 2000-07-21 09:17:05 +00:00
parent 7a01fd48b6
commit 9546cf7393
3 changed files with 49 additions and 12 deletions

18
scp.c
View File

@ -77,16 +77,22 @@ static void bump(char *fmt, ...)
exit(1); exit(1);
} }
static void get_password(const char *prompt, char *str, int maxlen) static int get_password(const char *prompt, char *str, int maxlen)
{ {
HANDLE hin, hout; HANDLE hin, hout;
DWORD savemode, i; DWORD savemode, i;
if (password) { if (password) {
strncpy(str, password, maxlen); static int tried_once = 0;
str[maxlen-1] = '\0';
password = NULL; if (tried_once) {
return; return 0;
} else {
strncpy(str, password, maxlen);
str[maxlen-1] = '\0';
tried_once = 1;
return 1;
}
} }
hin = GetStdHandle(STD_INPUT_HANDLE); hin = GetStdHandle(STD_INPUT_HANDLE);
@ -107,6 +113,8 @@ static void get_password(const char *prompt, char *str, int maxlen)
str[i] = '\0'; str[i] = '\0';
WriteFile(hout, "\r\n", 2, &i, NULL); WriteFile(hout, "\r\n", 2, &i, NULL);
return 1;
} }
/* /*

2
scp.h
View File

@ -9,7 +9,7 @@
/* Exported from ssh.c */ /* Exported from ssh.c */
extern int scp_flags; extern int scp_flags;
extern void (*ssh_get_password)(const char *prompt, char *str, int maxlen); extern int (*ssh_get_password)(const char *prompt, char *str, int maxlen);
char * ssh_scp_init(char *host, int port, char *cmd, char **realhost); char * ssh_scp_init(char *host, int port, char *cmd, char **realhost);
int ssh_scp_recv(unsigned char *buf, int len); int ssh_scp_recv(unsigned char *buf, int len);
void ssh_scp_send(unsigned char *buf, int len); void ssh_scp_send(unsigned char *buf, int len);

41
ssh.c
View File

@ -81,7 +81,7 @@ static SOCKET s = INVALID_SOCKET;
static unsigned char session_key[32]; static unsigned char session_key[32];
static struct ssh_cipher *cipher = NULL; static struct ssh_cipher *cipher = NULL;
int scp_flags = 0; int scp_flags = 0;
void (*ssh_get_password)(const char *prompt, char *str, int maxlen) = NULL; int (*ssh_get_password)(const char *prompt, char *str, int maxlen) = NULL;
static char *savedhost; static char *savedhost;
@ -267,8 +267,12 @@ next_packet:
static void ssh_gotdata(unsigned char *data, int datalen) static void ssh_gotdata(unsigned char *data, int datalen)
{ {
while (datalen > 0) { while (datalen > 0) {
if ( s_rdpkt(&data, &datalen) == 0 ) if ( s_rdpkt(&data, &datalen) == 0 ) {
ssh_protocol(NULL, 0, 1); ssh_protocol(NULL, 0, 1);
if (ssh_state == SSH_STATE_CLOSED) {
return;
}
}
} }
} }
@ -781,7 +785,17 @@ static int do_ssh_login(unsigned char *in, int inlen, int ispkt)
if (IS_SCP) { if (IS_SCP) {
char prompt[200]; char prompt[200];
sprintf(prompt, "%s@%s's password: ", cfg.username, savedhost); sprintf(prompt, "%s@%s's password: ", cfg.username, savedhost);
ssh_get_password(prompt, password, sizeof(password)); if (!ssh_get_password(prompt, password, sizeof(password))) {
/*
* get_password failed to get a password (for
* example because one was supplied on the command
* line which has already failed to work).
* Terminate.
*/
logevent("No more passwords to try");
ssh_state = SSH_STATE_CLOSED;
crReturn(1);
}
} else { } else {
if (pktin.type == SSH_SMSG_FAILURE && if (pktin.type == SSH_SMSG_FAILURE &&
@ -845,7 +859,8 @@ static int do_ssh_login(unsigned char *in, int inlen, int ispkt)
logevent("Authentication refused"); logevent("Authentication refused");
} else if (pktin.type == SSH_MSG_DISCONNECT) { } else if (pktin.type == SSH_MSG_DISCONNECT) {
logevent("Received disconnect request"); logevent("Received disconnect request");
crReturn(0); ssh_state = SSH_STATE_CLOSED;
crReturn(1);
} else if (pktin.type != SSH_SMSG_SUCCESS) { } else if (pktin.type != SSH_SMSG_SUCCESS) {
fatalbox("Strange packet received, type %d", pktin.type); fatalbox("Strange packet received, type %d", pktin.type);
} }
@ -861,8 +876,11 @@ static void ssh_protocol(unsigned char *in, int inlen, int ispkt) {
random_init(); random_init();
while (!do_ssh_login(in, inlen, ispkt)) while (!do_ssh_login(in, inlen, ispkt)) {
crReturnV; crReturnV;
}
if (ssh_state == SSH_STATE_CLOSED)
crReturnV;
if (!cfg.nopty) { if (!cfg.nopty) {
send_packet(SSH_CMSG_REQUEST_PTY, send_packet(SSH_CMSG_REQUEST_PTY,
@ -982,6 +1000,11 @@ static int ssh_msg (WPARAM wParam, LPARAM lParam) {
return 0; return 0;
} }
ssh_gotdata (buf, ret); ssh_gotdata (buf, ret);
if (ssh_state == SSH_STATE_CLOSED) {
closesocket(s);
s = INVALID_SOCKET;
return 0;
}
return 1; return 1;
} }
return 1; /* shouldn't happen, but WTF */ return 1; /* shouldn't happen, but WTF */
@ -1180,7 +1203,13 @@ char *ssh_scp_init(char *host, int port, char *cmd, char **realhost)
get_packet(); get_packet();
if (s == INVALID_SOCKET) if (s == INVALID_SOCKET)
return "Connection closed by remote host"; return "Connection closed by remote host";
} while (!do_ssh_login(NULL, 0, 1)); } while (!do_ssh_login(NULL, 0, 1));
if (ssh_state == SSH_STATE_CLOSED) {
closesocket(s);
s = INVALID_SOCKET;
return "Session initialisation error";
}
/* Execute command */ /* Execute command */
sprintf(buf, "Sending command: %.100s", cmd); sprintf(buf, "Sending command: %.100s", cmd);