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;
if (len > 0) { fwrite(buf, len, 1, stderr); fputc('\n', stderr); }
return;
}
- while (len--) {
- int new_head = (inbuf_head + 1) & INBUF_MASK;
- if (new_head != inbuf_reap) {
- inbuf[inbuf_head] = *buf++;
- inbuf_head = new_head;
- } else {
- term_out();
- if( inbuf_head == inbuf_reap ) len++; else break;
- }
- }
+ while (len--)
+ c_write1(*buf++);
}
struct Packet {
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);