Readpassphrase #692 (#156)

Fix for #692
PowerShell/Win32-OpenSSH#692
Implement readpassphrase to align with unix implementation.
This commit is contained in:
bagajjal 2017-06-08 11:47:59 -07:00 committed by Manoj Ampalam
parent 8f5a769312
commit 45de6ba06c

View File

@ -1044,58 +1044,63 @@ w32_strerror(int errnum)
return _sys_errlist_ext[errnum - EADDRINUSE]; return _sys_errlist_ext[errnum - EADDRINUSE];
return strerror(errnum); return strerror(errnum);
} }
/*
* Temporary implementation of readpassphrase.
* TODO - this needs to be reimplemented as per
* https://linux.die.net/man/3/readpassphrase
*/
char *
readpassphrase(const char *prompt, char *out, size_t out_len, int flags) {
char *askpass = NULL;
char *ret = NULL;
DWORD mode; char *
size_t len = 0; readpassphrase(const char *prompt, char *outBuf, size_t outBufLen, int flags) {
int retr = 0; int current_index = 0;
char ch;
wchar_t* wtmp = NULL;
/* prompt user */ if (outBufLen == 0) {
wchar_t* wtmp = utf8_to_utf16(prompt); errno = EINVAL;
return NULL;
}
while (_kbhit()) _getch();
wtmp = utf8_to_utf16(prompt);
if (wtmp == NULL) if (wtmp == NULL)
fatal("unable to alloc memory"); fatal("unable to alloc memory");
_cputws(wtmp); _cputws(wtmp);
free(wtmp); free(wtmp);
len = retr = 0; while (current_index < outBufLen - 1) {
ch = _getch();
while (_kbhit())
_getch(); if (ch == '\r') {
if (_kbhit()) _getch(); /* read linefeed if its there */
while (len < out_len) {
out[len] = (unsigned char)_getch();
if (out[len] == '\r') {
if (_kbhit()) /* read linefeed if its there */
_getch();
break; break;
} } else if (ch == '\n') {
else if (out[len] == '\n') {
break; break;
} } else if (ch == '\b') { /* backspace */
else if (out[len] == '\b') { /* backspace */ if (current_index > 0) {
if (len > 0) if (flags & RPP_ECHO_ON)
len--; /* overwrite last character */ printf("%c \b", ch);
}
else if (out[len] == '\003') { current_index--; /* overwrite last character */
/* exit on Ctrl+C */ }
} else if (ch == '\003') { /* exit on Ctrl+C */
fatal(""); fatal("");
} } else {
else { if (flags & RPP_SEVENBIT)
len++; /* keep reading in the loop */ ch &= 0x7f;
if (isalpha((unsigned char)ch)) {
if(flags & RPP_FORCELOWER)
ch = tolower((unsigned char)ch);
if(flags & RPP_FORCEUPPER)
ch = toupper((unsigned char)ch);
}
outBuf[current_index++] = ch;
if(flags & RPP_ECHO_ON)
printf("%c", ch);
} }
} }
out[len] = '\0'; /* get rid of the cr/lf */ outBuf[current_index] = '\0';
_cputs("\n"); /*show a newline as we do not echo password or the line */ _cputs("\n");
return out; return outBuf;
} }