fix sftp.exe client to work using password authentication

ssh.exe client invoked underneath was not able to send password prompt
and read password from user as stdin and stdout handles were redirected
to sockets by sftp.exe ; stderr which is not redirected is used to show
prompt to users and data is read from console
This commit is contained in:
quamrulmina 2015-09-27 23:54:10 -05:00
parent 0d3a933c2a
commit 6d167ae0f6
2 changed files with 29 additions and 97 deletions

View File

@ -72,6 +72,7 @@ static fd_set write_sfd_set;
int PassInputFd = STDIN_FILENO; int PassInputFd = STDIN_FILENO;
int PassOutputFd = STDOUT_FILENO; int PassOutputFd = STDOUT_FILENO;
int PassErrorFd = STDERR_FILENO;
/* /*
* We store cookies for authorize * We store cookies for authorize

View File

@ -54,6 +54,7 @@
extern int PassInputFd; extern int PassInputFd;
extern int PassOutputFd; extern int PassOutputFd;
extern int PassErrorFd;
#endif #endif
@ -334,109 +335,39 @@ read_passphrase(const char *prompt, int flags)
* Show prompt for user. * Show prompt for user.
*/ */
_write(PassOutputFd, prompt, strlen(prompt)); _write(PassErrorFd, prompt, strlen(prompt));
/*
* Disable stdin echo.
*/
GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode & (~ENABLE_ECHO_INPUT));
/*
* Read pass from stdin socket.
*/
len = retr = 0; len = retr = 0;
int bufsize = sizeof(buf);
do while (_kbhit())
{ _getch();
retr = _read(PassInputFd, buf + len, sizeof(buf) - 1 - len);
/* while ( len < bufsize ) {
* If read error.
*/
if (retr == -1) buf[len] = (unsigned char) _getch() ;
{
int winerr = GetLastError();
/*
* Non fatal. Only write message and try again.
*/
if (errno == EINTR || errno == EAGAIN || if ( buf[len] == '\r' ) {
winerr == WSAENOTSOCK || winerr == WSAEWOULDBLOCK) if (_kbhit() )
{ _getch(); // read linefeed if its there
debug("ERROR. read_passphrase() : [errno = %d, winerr = %d], " break;
"trying again...\n", errno, winerr); }
else if ( buf[len] == '\n' ) {
break;
}
else if ( buf[len] == '\b' ) { // backspace
if (len > 0 )
len--; // overwrite last character
}
else {
fflush(stderr); _putch( (int) '*' ); // show a star in place of what is typed
} len++; // keep reading in the loop
}
}
/* buf[len] = '\0' ; // get rid of the cr/lf
* Fatal error. Break loop with empty string.
*/
else
{
error("ERROR. read_passphrase() : fatal error"
" [errno = %d, winerr = %d]...\n", errno, winerr);
fflush(stderr);
buf[0] = 0;
len = 1;
break;
}
}
/*
* No error.
*/
else
{
len += retr;
}
} while (len < sizeof(buf) - 1 && buf[len] == '\r');
/*
* Put zero at string end.
*/
if (len > 0)
{
buf[len - 1] = '\0';
}
else
{
buf[0] = '\0';
}
_write(PassOutputFd, "\n", strlen("\n"));
len = strlen(buf);
while (len > 0 && buf[len - 1] == 0xa || buf[len - 1] == 0xd)
{
buf[len - 1] = 0;
len --;
}
/*
* Set stdin echo back.
*/
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode | ENABLE_ECHO_INPUT);
/*
* Return copy of pass readed from stdin.
*/
ret = xstrdup(buf); ret = xstrdup(buf);