2011-09-24 01:02:06 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-24 01:02:06 +04:00
|
|
|
* Passphrase Handling Utils
|
|
|
|
*
|
|
|
|
* Copyright 2011 Shea Levy <shea@shealevy.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2023-03-14 12:39:18 +03:00
|
|
|
#include <freerdp/freerdp.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-09-24 21:21:00 +04:00
|
|
|
#include <errno.h>
|
2011-09-25 03:18:25 +04:00
|
|
|
#include <freerdp/utils/passphrase.h>
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2011-09-25 03:18:25 +04:00
|
|
|
#ifdef _WIN32
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2021-10-11 23:00:52 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <io.h>
|
2021-10-19 00:36:35 +03:00
|
|
|
#include <conio.h>
|
2023-03-18 11:40:53 +03:00
|
|
|
#include <wincred.h>
|
2021-10-11 23:00:52 +03:00
|
|
|
|
2023-02-27 10:38:27 +03:00
|
|
|
static char read_chr(FILE* f)
|
2021-10-11 23:00:52 +03:00
|
|
|
{
|
2021-10-19 00:36:35 +03:00
|
|
|
char chr;
|
2023-02-27 10:38:27 +03:00
|
|
|
const BOOL isTty = _isatty(_fileno(f));
|
2021-10-11 23:00:52 +03:00
|
|
|
if (isTty)
|
2023-02-27 10:38:27 +03:00
|
|
|
return fgetc(f);
|
|
|
|
if (fscanf_s(f, "%c", &chr, (UINT32)sizeof(char)) && !feof(f))
|
2021-10-11 23:00:52 +03:00
|
|
|
return chr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-27 10:38:27 +03:00
|
|
|
int freerdp_interruptible_getc(rdpContext* context, FILE* f)
|
2011-09-25 03:18:25 +04:00
|
|
|
{
|
2023-03-15 12:34:05 +03:00
|
|
|
return read_chr(f);
|
2023-02-27 10:38:27 +03:00
|
|
|
}
|
|
|
|
|
2024-09-12 15:45:02 +03:00
|
|
|
const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf,
|
|
|
|
size_t bufsiz, int from_stdin)
|
2023-02-27 10:38:27 +03:00
|
|
|
{
|
2023-03-18 11:40:53 +03:00
|
|
|
WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 'p', 'r', 'e', 'f', 'i',
|
|
|
|
'l', 'l', 'e', 'd', '\0' };
|
|
|
|
WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 };
|
|
|
|
BOOL fSave = FALSE;
|
|
|
|
DWORD dwFlags = 0;
|
|
|
|
WCHAR* promptW = ConvertUtf8ToWCharAlloc(prompt, NULL);
|
|
|
|
const DWORD status =
|
|
|
|
CredUICmdLinePromptForCredentialsW(promptW, NULL, 0, UserNameW, ARRAYSIZE(UserNameW),
|
|
|
|
PasswordW, ARRAYSIZE(PasswordW), &fSave, dwFlags);
|
|
|
|
free(promptW);
|
|
|
|
if (ConvertWCharNToUtf8(PasswordW, ARRAYSIZE(PasswordW), buf, bufsiz) < 0)
|
|
|
|
return NULL;
|
|
|
|
return buf;
|
2011-09-25 03:18:25 +04:00
|
|
|
}
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2013-01-25 20:50:40 +04:00
|
|
|
#elif !defined(ANDROID)
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2011-09-24 08:45:49 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2011-09-24 21:32:13 +04:00
|
|
|
#include <termios.h>
|
2011-09-24 08:45:49 +04:00
|
|
|
#include <unistd.h>
|
2011-09-28 09:37:43 +04:00
|
|
|
#include <freerdp/utils/signal.h>
|
2011-09-24 01:02:06 +04:00
|
|
|
|
2023-07-06 00:43:12 +03:00
|
|
|
#if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__)
|
2023-02-25 13:08:26 +03:00
|
|
|
#include <poll.h>
|
|
|
|
#else
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int wait_for_fd(int fd, int timeout)
|
2011-09-24 01:02:06 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
2023-07-06 00:43:12 +03:00
|
|
|
#if defined(WINPR_HAVE_POLL_H) && !defined(__APPLE__)
|
2023-02-25 13:08:26 +03:00
|
|
|
struct pollfd pollset = { 0 };
|
|
|
|
pollset.fd = fd;
|
|
|
|
pollset.events = POLLIN;
|
|
|
|
pollset.revents = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status = poll(&pollset, 1, timeout);
|
|
|
|
} while ((status < 0) && (errno == EINTR));
|
|
|
|
|
|
|
|
#else
|
|
|
|
fd_set rset = { 0 };
|
|
|
|
struct timeval tv = { 0 };
|
|
|
|
FD_ZERO(&rset);
|
2023-06-14 11:45:02 +03:00
|
|
|
FD_SET(fd, &rset);
|
2023-02-25 13:08:26 +03:00
|
|
|
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
tv.tv_sec = timeout / 1000;
|
|
|
|
tv.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2023-06-14 11:45:02 +03:00
|
|
|
status = select(fd + 1, &rset, NULL, NULL, timeout ? &tv : NULL);
|
2023-02-25 13:08:26 +03:00
|
|
|
} while ((status < 0) && (errno == EINTR));
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void replace_char(char* buffer, size_t buffer_len, const char* toreplace)
|
|
|
|
{
|
|
|
|
while (*toreplace != '\0')
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
char* ptr = NULL;
|
2023-02-25 13:08:26 +03:00
|
|
|
while ((ptr = strrchr(buffer, *toreplace)) != NULL)
|
|
|
|
*ptr = '\0';
|
|
|
|
toreplace++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:45:02 +03:00
|
|
|
static const char* freerdp_passphrase_read_tty(rdpContext* context, const char* prompt, char* buf,
|
|
|
|
size_t bufsiz, int from_stdin)
|
2023-02-25 13:08:26 +03:00
|
|
|
{
|
|
|
|
BOOL terminal_needs_reset = FALSE;
|
|
|
|
char term_name[L_ctermid] = { 0 };
|
2024-01-23 18:49:54 +03:00
|
|
|
int term_file = 0;
|
2023-02-25 13:08:26 +03:00
|
|
|
|
|
|
|
FILE* fout = NULL;
|
2011-09-24 10:11:32 +04:00
|
|
|
|
|
|
|
if (bufsiz == 0)
|
2011-09-24 21:21:00 +04:00
|
|
|
{
|
|
|
|
errno = EINVAL;
|
2011-09-24 10:11:32 +04:00
|
|
|
return NULL;
|
2011-09-24 21:21:00 +04:00
|
|
|
}
|
2011-09-24 08:45:49 +04:00
|
|
|
|
|
|
|
ctermid(term_name);
|
2024-01-23 18:49:54 +03:00
|
|
|
int terminal_fildes = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
if (from_stdin || strcmp(term_name, "") == 0 || (term_file = open(term_name, O_RDWR)) == -1)
|
2011-09-25 01:30:56 +04:00
|
|
|
{
|
2023-02-25 13:08:26 +03:00
|
|
|
fout = stdout;
|
2011-09-28 09:37:43 +04:00
|
|
|
terminal_fildes = STDIN_FILENO;
|
2011-09-25 01:30:56 +04:00
|
|
|
}
|
2011-09-25 01:05:35 +04:00
|
|
|
else
|
2011-09-25 01:30:56 +04:00
|
|
|
{
|
2023-02-25 13:08:26 +03:00
|
|
|
fout = fdopen(term_file, "w");
|
2011-09-28 09:37:43 +04:00
|
|
|
terminal_fildes = term_file;
|
2011-09-25 01:30:56 +04:00
|
|
|
}
|
2011-09-24 10:11:32 +04:00
|
|
|
|
2023-02-25 13:08:26 +03:00
|
|
|
struct termios orig_flags = { 0 };
|
2011-09-28 09:37:43 +04:00
|
|
|
if (tcgetattr(terminal_fildes, &orig_flags) != -1)
|
2011-09-24 22:15:36 +04:00
|
|
|
{
|
2023-02-25 13:08:26 +03:00
|
|
|
struct termios new_flags = { 0 };
|
2011-09-28 09:37:43 +04:00
|
|
|
new_flags = orig_flags;
|
|
|
|
new_flags.c_lflag &= ~ECHO;
|
|
|
|
new_flags.c_lflag |= ECHONL;
|
2023-02-25 13:08:26 +03:00
|
|
|
terminal_needs_reset = TRUE;
|
2011-09-28 09:37:43 +04:00
|
|
|
if (tcsetattr(terminal_fildes, TCSAFLUSH, &new_flags) == -1)
|
2023-02-25 13:08:26 +03:00
|
|
|
terminal_needs_reset = FALSE;
|
2011-09-24 22:15:36 +04:00
|
|
|
}
|
|
|
|
|
2023-02-25 13:08:26 +03:00
|
|
|
FILE* fp = fdopen(terminal_fildes, "r");
|
|
|
|
if (!fp)
|
2011-09-24 23:13:23 +04:00
|
|
|
goto error;
|
2011-09-24 10:11:32 +04:00
|
|
|
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(fout, "%s", prompt);
|
|
|
|
(void)fflush(fout);
|
2023-02-25 13:08:26 +03:00
|
|
|
|
|
|
|
char* ptr = NULL;
|
|
|
|
size_t ptr_len = 0;
|
|
|
|
|
|
|
|
const SSIZE_T res = freerdp_interruptible_get_line(context, &ptr, &ptr_len, fp);
|
|
|
|
if (res < 0)
|
2011-09-24 23:13:23 +04:00
|
|
|
goto error;
|
2023-02-25 13:08:26 +03:00
|
|
|
replace_char(ptr, ptr_len, "\r\n");
|
2011-09-24 10:11:32 +04:00
|
|
|
|
2023-02-25 13:08:26 +03:00
|
|
|
strncpy(buf, ptr, MIN(bufsiz, ptr_len));
|
|
|
|
free(ptr);
|
2011-09-28 09:37:43 +04:00
|
|
|
if (terminal_needs_reset)
|
2011-09-24 23:03:24 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags) == -1)
|
2011-09-24 23:13:23 +04:00
|
|
|
goto error;
|
2011-09-24 23:03:24 +04:00
|
|
|
}
|
|
|
|
|
2011-09-28 09:37:43 +04:00
|
|
|
if (terminal_fildes != STDIN_FILENO)
|
2011-09-25 01:30:56 +04:00
|
|
|
{
|
2023-02-25 13:08:26 +03:00
|
|
|
if (fclose(fp) == -1)
|
2011-09-25 01:30:56 +04:00
|
|
|
goto error;
|
|
|
|
}
|
2011-09-24 10:11:32 +04:00
|
|
|
|
|
|
|
return buf;
|
2011-09-24 23:13:23 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
error:
|
|
|
|
{
|
|
|
|
int saved_errno = errno;
|
|
|
|
if (terminal_needs_reset)
|
2024-09-13 11:48:03 +03:00
|
|
|
(void)tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (terminal_fildes != STDIN_FILENO)
|
2023-02-25 13:08:26 +03:00
|
|
|
{
|
2023-04-27 10:23:51 +03:00
|
|
|
if (fp)
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fclose(fp);
|
2023-02-25 13:08:26 +03:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
errno = saved_errno;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-24 01:02:06 +04:00
|
|
|
}
|
2012-10-14 04:31:01 +04:00
|
|
|
|
2024-09-12 18:07:16 +03:00
|
|
|
static const char* freerdp_passphrase_read_askpass(const char* prompt, char* buf, size_t bufsiz,
|
|
|
|
char const* askpass_env)
|
2024-08-28 06:44:21 +03:00
|
|
|
{
|
|
|
|
char command[4096] = { 0 };
|
|
|
|
|
2024-08-28 11:07:36 +03:00
|
|
|
(void)sprintf_s(command, sizeof(command), "%s 'FreeRDP authentication\n%s'", askpass_env,
|
|
|
|
prompt);
|
|
|
|
FILE* askproc = popen(command, "r");
|
2024-08-28 06:44:21 +03:00
|
|
|
if (!askproc)
|
|
|
|
return NULL;
|
|
|
|
if (fgets(buf, bufsiz, askproc) != NULL)
|
|
|
|
buf[strcspn(buf, "\r\n")] = '\0';
|
|
|
|
else
|
|
|
|
buf = NULL;
|
2024-08-28 11:07:36 +03:00
|
|
|
const int status = pclose(askproc);
|
2024-08-28 06:44:21 +03:00
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
buf = NULL;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2024-09-12 18:07:16 +03:00
|
|
|
const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf,
|
|
|
|
size_t bufsiz, int from_stdin)
|
2024-08-28 06:44:21 +03:00
|
|
|
{
|
2024-08-28 11:13:39 +03:00
|
|
|
const char* askpass_env = getenv("FREERDP_ASKPASS");
|
2024-08-28 06:44:21 +03:00
|
|
|
|
|
|
|
if (askpass_env)
|
|
|
|
return freerdp_passphrase_read_askpass(prompt, buf, bufsiz, askpass_env);
|
|
|
|
else
|
|
|
|
return freerdp_passphrase_read_tty(context, prompt, buf, bufsiz, from_stdin);
|
|
|
|
}
|
|
|
|
|
2023-02-25 13:08:26 +03:00
|
|
|
int freerdp_interruptible_getc(rdpContext* context, FILE* f)
|
|
|
|
{
|
|
|
|
int rc = EOF;
|
|
|
|
const int fd = fileno(f);
|
|
|
|
|
|
|
|
const int orig = fcntl(fd, F_GETFL);
|
2024-09-14 21:55:48 +03:00
|
|
|
(void)fcntl(fd, F_SETFL, orig | O_NONBLOCK);
|
2023-02-25 13:08:26 +03:00
|
|
|
do
|
|
|
|
{
|
|
|
|
const int res = wait_for_fd(fd, 10);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2023-02-27 10:38:27 +03:00
|
|
|
char c = 0;
|
|
|
|
const ssize_t rd = read(fd, &c, 1);
|
|
|
|
if (rd == 1)
|
2024-09-11 21:16:31 +03:00
|
|
|
rc = (int)c;
|
2023-02-27 10:38:27 +03:00
|
|
|
break;
|
2023-02-25 13:08:26 +03:00
|
|
|
}
|
|
|
|
} while (!freerdp_shall_disconnect_context(context));
|
|
|
|
|
2024-09-14 21:55:48 +03:00
|
|
|
(void)fcntl(fd, F_SETFL, orig);
|
2023-02-25 13:08:26 +03:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-10-14 04:31:01 +04:00
|
|
|
#else
|
|
|
|
|
2024-09-12 15:45:02 +03:00
|
|
|
const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf,
|
|
|
|
size_t bufsiz, int from_stdin)
|
2012-10-14 04:31:01 +04:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-25 13:08:26 +03:00
|
|
|
int freerdp_interruptible_getc(rdpContext* context, FILE* f)
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
2011-09-25 03:18:25 +04:00
|
|
|
#endif
|
2023-02-25 13:08:26 +03:00
|
|
|
|
|
|
|
SSIZE_T freerdp_interruptible_get_line(rdpContext* context, char** plineptr, size_t* psize,
|
|
|
|
FILE* stream)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int c = 0;
|
|
|
|
char* n = NULL;
|
2023-02-25 13:08:26 +03:00
|
|
|
size_t step = 32;
|
|
|
|
size_t used = 0;
|
|
|
|
char* ptr = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
if (!plineptr || !psize)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (used + 2 >= len)
|
|
|
|
{
|
|
|
|
len += step;
|
|
|
|
n = realloc(ptr, len);
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = n;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = freerdp_interruptible_getc(context, stream);
|
|
|
|
if (c != EOF)
|
2023-08-23 15:21:49 +03:00
|
|
|
ptr[used++] = (char)c;
|
2023-02-25 13:08:26 +03:00
|
|
|
} while ((c != '\n') && (c != '\r') && (c != EOF));
|
|
|
|
|
|
|
|
ptr[used] = '\0';
|
|
|
|
if (c == EOF)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
*plineptr = ptr;
|
|
|
|
*psize = used;
|
|
|
|
return used;
|
|
|
|
}
|