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:
parent
0d3a933c2a
commit
6d167ae0f6
|
@ -72,6 +72,7 @@ static fd_set write_sfd_set;
|
|||
|
||||
int PassInputFd = STDIN_FILENO;
|
||||
int PassOutputFd = STDOUT_FILENO;
|
||||
int PassErrorFd = STDERR_FILENO;
|
||||
|
||||
/*
|
||||
* We store cookies for authorize
|
||||
|
|
109
readpass.c
109
readpass.c
|
@ -54,6 +54,7 @@
|
|||
|
||||
extern int PassInputFd;
|
||||
extern int PassOutputFd;
|
||||
extern int PassErrorFd;
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -334,109 +335,39 @@ read_passphrase(const char *prompt, int flags)
|
|||
* Show prompt for user.
|
||||
*/
|
||||
|
||||
_write(PassOutputFd, 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.
|
||||
*/
|
||||
_write(PassErrorFd, prompt, strlen(prompt));
|
||||
|
||||
len = retr = 0;
|
||||
int bufsize = sizeof(buf);
|
||||
|
||||
do
|
||||
{
|
||||
retr = _read(PassInputFd, buf + len, sizeof(buf) - 1 - len);
|
||||
while (_kbhit())
|
||||
_getch();
|
||||
|
||||
/*
|
||||
* If read error.
|
||||
*/
|
||||
while ( len < bufsize ) {
|
||||
|
||||
if (retr == -1)
|
||||
{
|
||||
int winerr = GetLastError();
|
||||
buf[len] = (unsigned char) _getch() ;
|
||||
|
||||
/*
|
||||
* Non fatal. Only write message and try again.
|
||||
*/
|
||||
|
||||
if (errno == EINTR || errno == EAGAIN ||
|
||||
winerr == WSAENOTSOCK || winerr == WSAEWOULDBLOCK)
|
||||
{
|
||||
debug("ERROR. read_passphrase() : [errno = %d, winerr = %d], "
|
||||
"trying again...\n", errno, winerr);
|
||||
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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;
|
||||
|
||||
if ( buf[len] == '\r' ) {
|
||||
if (_kbhit() )
|
||||
_getch(); // read linefeed if its there
|
||||
break;
|
||||
}
|
||||
else if ( buf[len] == '\n' ) {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* No error.
|
||||
*/
|
||||
|
||||
else
|
||||
{
|
||||
len += retr;
|
||||
}
|
||||
} while (len < sizeof(buf) - 1 && buf[len] == '\r');
|
||||
|
||||
/*
|
||||
* Put zero at string end.
|
||||
*/
|
||||
|
||||
else if ( buf[len] == '\b' ) { // backspace
|
||||
if (len > 0 )
|
||||
{
|
||||
buf[len - 1] = '\0';
|
||||
len--; // overwrite last character
|
||||
}
|
||||
else {
|
||||
|
||||
_putch( (int) '*' ); // show a star in place of what is typed
|
||||
len++; // keep reading in the loop
|
||||
}
|
||||
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.
|
||||
*/
|
||||
buf[len] = '\0' ; // get rid of the cr/lf
|
||||
|
||||
ret = xstrdup(buf);
|
||||
|
||||
|
|
Loading…
Reference in New Issue