20 #include <winpr/environment.h>
22 #include <freerdp/config.h>
23 #include <freerdp/freerdp.h>
26 #include <freerdp/utils/passphrase.h>
35 static char read_chr(FILE* f)
38 const BOOL isTty = _isatty(_fileno(f));
41 if (fscanf_s(f,
"%c", &chr, (UINT32)
sizeof(
char)) && !feof(f))
46 int freerdp_interruptible_getc(rdpContext* context, FILE* f)
51 const 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)
77 #include <freerdp/utils/signal.h>
79 #if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__)
83 #include <sys/select.h>
86 static int wait_for_fd(
int fd,
int timeout)
89 #if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__)
90 struct pollfd pollset = { 0 };
92 pollset.events = POLLIN;
97 status = poll(&pollset, 1, timeout);
98 }
while ((status < 0) && (errno == EINTR));
102 struct timeval tv = { 0 };
108 tv.tv_sec = timeout / 1000;
109 tv.tv_usec = (timeout % 1000) * 1000;
114 status = select(fd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
115 }
while ((status < 0) && (errno == EINTR));
121 static void replace_char(
char* buffer,
size_t buffer_len,
const char* toreplace)
123 while (*toreplace !=
'\0')
126 while ((ptr = strrchr(buffer, *toreplace)) != NULL)
132 static const char* freerdp_passphrase_read_tty(rdpContext* context,
const char* prompt,
char* buf,
133 size_t bufsiz,
int from_stdin)
135 BOOL terminal_needs_reset = FALSE;
136 char term_name[L_ctermid] = { 0 };
147 int terminal_fildes = 0;
148 if (from_stdin || (strcmp(term_name,
"") == 0))
151 terminal_fildes = STDIN_FILENO;
155 const int term_file = open(term_name, O_RDWR);
159 terminal_fildes = STDIN_FILENO;
163 fout = fdopen(term_file,
"w");
169 terminal_fildes = term_file;
173 struct termios orig_flags = { 0 };
174 if (tcgetattr(terminal_fildes, &orig_flags) != -1)
176 struct termios new_flags = { 0 };
177 new_flags = orig_flags;
178 new_flags.c_lflag &= (uint32_t)~ECHO;
179 new_flags.c_lflag |= ECHONL;
180 terminal_needs_reset = TRUE;
181 if (tcsetattr(terminal_fildes, TCSAFLUSH, &new_flags) == -1)
182 terminal_needs_reset = FALSE;
185 FILE* fp = fdopen(terminal_fildes,
"r");
189 (void)fprintf(fout,
"%s", prompt);
195 const SSIZE_T res = freerdp_interruptible_get_line(context, &ptr, &ptr_len, fp);
198 replace_char(ptr, ptr_len,
"\r\n");
200 strncpy(buf, ptr, MIN(bufsiz, ptr_len));
202 if (terminal_needs_reset)
204 if (tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags) == -1)
208 if (terminal_fildes != STDIN_FILENO)
210 if (fclose(fp) == -1)
218 int saved_errno = errno;
219 if (terminal_needs_reset)
220 (void)tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
222 if (terminal_fildes != STDIN_FILENO)
232 static const char* freerdp_passphrase_read_askpass(
const char* prompt,
char* buf,
size_t bufsiz,
233 char const* askpass_env)
235 char command[4096] = { 0 };
237 (void)sprintf_s(command,
sizeof(command),
"%s 'FreeRDP authentication\n%s'", askpass_env,
240 FILE* askproc = popen(command,
"r");
243 WINPR_ASSERT(bufsiz <= INT32_MAX);
244 if (fgets(buf, (
int)bufsiz, askproc) != NULL)
245 buf[strcspn(buf,
"\r\n")] =
'\0';
248 const int status = pclose(askproc);
249 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
255 const char* freerdp_passphrase_read(rdpContext* context,
const char* prompt,
char* buf,
256 size_t bufsiz,
int from_stdin)
258 const char* askpass_env = getenv(
"FREERDP_ASKPASS");
261 return freerdp_passphrase_read_askpass(prompt, buf, bufsiz, askpass_env);
263 return freerdp_passphrase_read_tty(context, prompt, buf, bufsiz, from_stdin);
266 int freerdp_interruptible_getc(rdpContext* context, FILE* f)
269 const int fd = fileno(f);
271 const int orig = fcntl(fd, F_GETFL);
272 (void)fcntl(fd, F_SETFL, orig | O_NONBLOCK);
275 const int res = wait_for_fd(fd, 10);
279 const ssize_t rd = read(fd, &c, 1);
284 }
while (!freerdp_shall_disconnect_context(context));
286 (void)fcntl(fd, F_SETFL, orig);
292 const char* freerdp_passphrase_read(rdpContext* context,
const char* prompt,
char* buf,
293 size_t bufsiz,
int from_stdin)
298 int freerdp_interruptible_getc(rdpContext* context, FILE* f)
304 SSIZE_T freerdp_interruptible_get_line(rdpContext* context,
char** plineptr,
size_t* psize,
314 if (!plineptr || !psize)
325 n = realloc(ptr, len);
335 c = freerdp_interruptible_getc(context, stream);
337 ptr[used++] = (char)c;
338 }
while ((c !=
'\n') && (c !=
'\r') && (c != EOF));
348 return WINPR_ASSERTING_INT_CAST(SSIZE_T, used);