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;
DWORD savemode, i;
if (password) {
- strncpy(str, password, maxlen);
- str[maxlen-1] = '\0';
- password = NULL;
- return;
+ static int tried_once = 0;
+
+ if (tried_once) {
+ return 0;
+ } else {
+ strncpy(str, password, maxlen);
+ str[maxlen-1] = '\0';
+ tried_once = 1;
+ return 1;
+ }
}
hin = GetStdHandle(STD_INPUT_HANDLE);
str[i] = '\0';
WriteFile(hout, "\r\n", 2, &i, NULL);
+
+ return 1;
}
/*
/* Exported from ssh.c */
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);
int ssh_scp_recv(unsigned char *buf, int len);
void ssh_scp_send(unsigned char *buf, int len);
static unsigned char session_key[32];
static struct ssh_cipher *cipher = NULL;
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 void ssh_gotdata(unsigned char *data, int datalen)
{
while (datalen > 0) {
- if ( s_rdpkt(&data, &datalen) == 0 )
+ if ( s_rdpkt(&data, &datalen) == 0 ) {
ssh_protocol(NULL, 0, 1);
+ if (ssh_state == SSH_STATE_CLOSED) {
+ return;
+ }
+ }
}
}
if (IS_SCP) {
char prompt[200];
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 {
if (pktin.type == SSH_SMSG_FAILURE &&
logevent("Authentication refused");
} else if (pktin.type == SSH_MSG_DISCONNECT) {
logevent("Received disconnect request");
- crReturn(0);
+ ssh_state = SSH_STATE_CLOSED;
+ crReturn(1);
} else if (pktin.type != SSH_SMSG_SUCCESS) {
fatalbox("Strange packet received, type %d", pktin.type);
}
random_init();
- while (!do_ssh_login(in, inlen, ispkt))
+ while (!do_ssh_login(in, inlen, ispkt)) {
crReturnV;
+ }
+ if (ssh_state == SSH_STATE_CLOSED)
+ crReturnV;
if (!cfg.nopty) {
send_packet(SSH_CMSG_REQUEST_PTY,
return 0;
}
ssh_gotdata (buf, ret);
+ if (ssh_state == SSH_STATE_CLOSED) {
+ closesocket(s);
+ s = INVALID_SOCKET;
+ return 0;
+ }
return 1;
}
return 1; /* shouldn't happen, but WTF */
get_packet();
if (s == INVALID_SOCKET)
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 */
sprintf(buf, "Sending command: %.100s", cmd);