20#include <winpr/environment.h>
22#include <freerdp/config.h>
23#include <freerdp/freerdp.h>
26#include <freerdp/utils/passphrase.h>
35static char read_chr(FILE* f)
38 const BOOL isTty = _isatty(_fileno(f));
41 if (fscanf_s(f,
"%c", &chr, (UINT32)
sizeof(
char)) && !feof(f))
46int freerdp_interruptible_getc(rdpContext* context, FILE* f)
51const char* freerdp_passphrase_read(rdpContext* context,
const char* prompt,
char* buf,
52 size_t bufsiz,
int from_stdin)
54 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = {
'p',
'r',
'e',
'f',
'i',
55 'l',
'l',
'e',
'd',
'\0' };
56 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 };
59 WCHAR* promptW = ConvertUtf8ToWCharAlloc(prompt, NULL);
61 CredUICmdLinePromptForCredentialsW(promptW, NULL, 0, UserNameW, ARRAYSIZE(UserNameW),
62 PasswordW, ARRAYSIZE(PasswordW), &fSave, dwFlags);
64 if (ConvertWCharNToUtf8(PasswordW, ARRAYSIZE(PasswordW), buf, bufsiz) < 0)
69#elif !defined(ANDROID)
78#include <freerdp/utils/signal.h>
80#if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__)
84#include <sys/select.h>
87static int wait_for_fd(
int fd,
int timeout)
90#if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__)
91 struct pollfd pollset = { 0 };
93 pollset.events = POLLIN;
98 status = poll(&pollset, 1, timeout);
99 }
while ((status < 0) && (errno == EINTR));
103 struct timeval tv = { 0 };
109 tv.tv_sec = timeout / 1000;
110 tv.tv_usec = (timeout % 1000) * 1000;
115 status = select(fd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
116 }
while ((status < 0) && (errno == EINTR));
122static void replace_char(
char* buffer, WINPR_ATTR_UNUSED
size_t buffer_len,
const char* toreplace)
124 while (*toreplace !=
'\0')
127 while ((ptr = strrchr(buffer, *toreplace)) != NULL)
133static const char* freerdp_passphrase_read_tty(rdpContext* context,
const char* prompt,
char* buf,
134 size_t bufsiz,
int from_stdin)
136 BOOL terminal_needs_reset = FALSE;
137 char term_name[L_ctermid] = { 0 };
148 int terminal_fildes = 0;
149 if (from_stdin || (strcmp(term_name,
"") == 0))
152 terminal_fildes = STDIN_FILENO;
156 const int term_file = open(term_name, O_RDWR);
160 terminal_fildes = STDIN_FILENO;
164 fout = fdopen(term_file,
"w");
170 terminal_fildes = term_file;
174 struct termios orig_flags = { 0 };
175 if (tcgetattr(terminal_fildes, &orig_flags) != -1)
177 struct termios new_flags = { 0 };
178 new_flags = orig_flags;
179 new_flags.c_lflag &= (uint32_t)~ECHO;
180 new_flags.c_lflag |= ECHONL;
181 terminal_needs_reset = TRUE;
182 if (tcsetattr(terminal_fildes, TCSAFLUSH, &new_flags) == -1)
183 terminal_needs_reset = FALSE;
186 FILE* fp = fdopen(terminal_fildes,
"r");
190 (void)fprintf(fout,
"%s", prompt);
196 const SSIZE_T res = freerdp_interruptible_get_line(context, &ptr, &ptr_len, fp);
199 replace_char(ptr, ptr_len,
"\r\n");
201 strncpy(buf, ptr, MIN(bufsiz, ptr_len));
203 if (terminal_needs_reset)
205 if (tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags) == -1)
209 if (terminal_fildes != STDIN_FILENO)
217 int saved_errno = errno;
218 if (terminal_needs_reset)
219 (void)tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
221 if (terminal_fildes != STDIN_FILENO)
233static const char* freerdp_passphrase_read_askpass(
const char* prompt,
char* buf,
size_t bufsiz,
234 char const* askpass_env)
236 char command[4096] = { 0 };
238 (void)sprintf_s(command,
sizeof(command),
"%s 'FreeRDP authentication\n%s'", askpass_env,
241 FILE* askproc = popen(command,
"r");
244 WINPR_ASSERT(bufsiz <= INT32_MAX);
245 if (fgets(buf, (
int)bufsiz, askproc) != NULL)
246 buf[strcspn(buf,
"\r\n")] =
'\0';
249 const int status = pclose(askproc);
250 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
256const char* freerdp_passphrase_read(rdpContext* context,
const char* prompt,
char* buf,
257 size_t bufsiz,
int from_stdin)
260 const char* askpass_env = getenv(
"FREERDP_ASKPASS");
263 return freerdp_passphrase_read_askpass(prompt, buf, bufsiz, askpass_env);
265 return freerdp_passphrase_read_tty(context, prompt, buf, bufsiz, from_stdin);
268int freerdp_interruptible_getc(rdpContext* context, FILE* f)
271 const int fd = fileno(f);
273 const int orig = fcntl(fd, F_GETFL);
274 (void)fcntl(fd, F_SETFL, orig | O_NONBLOCK);
277 const int res = wait_for_fd(fd, 10);
281 const ssize_t rd = read(fd, &c, 1);
286 }
while (!freerdp_shall_disconnect_context(context));
288 (void)fcntl(fd, F_SETFL, orig);
294const char* freerdp_passphrase_read(rdpContext* context,
const char* prompt,
char* buf,
295 size_t bufsiz,
int from_stdin)
300int freerdp_interruptible_getc(rdpContext* context, FILE* f)
306SSIZE_T freerdp_interruptible_get_line(rdpContext* context,
char** plineptr,
size_t* psize,
316 if (!plineptr || !psize)
327 n = realloc(ptr, len);
337 c = freerdp_interruptible_getc(context, stream);
339 ptr[used++] = (char)c;
340 }
while ((c !=
'\n') && (c !=
'\r') && (c != EOF));
350 return WINPR_ASSERTING_INT_CAST(SSIZE_T, used);