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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2012-06-11 15:47:29 +04:00
|
|
|
char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz, int from_stdin)
|
2011-09-25 03:18:25 +04:00
|
|
|
{
|
2012-10-14 04:31:01 +04:00
|
|
|
errno = ENOSYS;
|
2011-09-25 03:18:25 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
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
|
|
|
|
2012-06-06 13:53:30 +04:00
|
|
|
char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz, int from_stdin)
|
2011-09-24 01:02:06 +04:00
|
|
|
{
|
2011-09-24 10:11:32 +04:00
|
|
|
char read_char;
|
|
|
|
char* buf_iter;
|
2011-09-24 08:45:49 +04:00
|
|
|
char term_name[L_ctermid];
|
2011-09-28 09:37:43 +04:00
|
|
|
int term_file, write_file;
|
2011-09-24 21:21:00 +04:00
|
|
|
ssize_t nbytes;
|
2011-09-24 10:11:32 +04:00
|
|
|
size_t read_bytes = 0;
|
|
|
|
|
|
|
|
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);
|
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
|
|
|
{
|
2011-09-25 01:05:35 +04:00
|
|
|
write_file = STDERR_FILENO;
|
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
|
|
|
{
|
|
|
|
write_file = term_file;
|
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
|
|
|
|
2011-09-28 09:37:43 +04:00
|
|
|
if (tcgetattr(terminal_fildes, &orig_flags) != -1)
|
2011-09-24 22:15:36 +04:00
|
|
|
{
|
2011-09-28 09:37:43 +04:00
|
|
|
new_flags = orig_flags;
|
|
|
|
new_flags.c_lflag &= ~ECHO;
|
|
|
|
new_flags.c_lflag |= ECHONL;
|
|
|
|
terminal_needs_reset = 1;
|
|
|
|
if (tcsetattr(terminal_fildes, TCSAFLUSH, &new_flags) == -1)
|
|
|
|
terminal_needs_reset = 0;
|
2011-09-24 22:15:36 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (write(write_file, prompt, strlen(prompt)) == (ssize_t)-1)
|
2011-09-24 23:13:23 +04:00
|
|
|
goto error;
|
2011-09-24 10:11:32 +04:00
|
|
|
|
|
|
|
buf_iter = buf;
|
2011-09-28 09:37:43 +04:00
|
|
|
while ((nbytes = read(terminal_fildes, &read_char, sizeof read_char)) == (sizeof read_char))
|
2011-09-24 10:11:32 +04:00
|
|
|
{
|
|
|
|
if (read_char == '\n')
|
|
|
|
break;
|
2019-11-06 17:24:51 +03:00
|
|
|
if (read_bytes < (bufsiz - (size_t)1))
|
2011-09-24 10:11:32 +04:00
|
|
|
{
|
2011-09-24 21:23:52 +04:00
|
|
|
read_bytes++;
|
2011-09-24 10:11:32 +04:00
|
|
|
*buf_iter = read_char;
|
|
|
|
buf_iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*buf_iter = '\0';
|
2011-09-24 10:25:55 +04:00
|
|
|
buf_iter = NULL;
|
2011-09-24 21:32:13 +04:00
|
|
|
read_char = '\0';
|
2019-11-06 17:24:51 +03:00
|
|
|
if (nbytes == (ssize_t)-1)
|
2011-09-24 23:13:23 +04:00
|
|
|
goto error;
|
2011-09-24 10:11:32 +04:00
|
|
|
|
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-28 09:37:43 +04:00
|
|
|
terminal_needs_reset = 0;
|
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
|
|
|
{
|
2011-09-28 09:37:43 +04:00
|
|
|
if (close(terminal_fildes) == -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;
|
|
|
|
buf_iter = NULL;
|
|
|
|
read_char = '\0';
|
|
|
|
if (terminal_needs_reset)
|
|
|
|
tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
|
|
|
|
if (terminal_fildes != STDIN_FILENO)
|
|
|
|
close(terminal_fildes);
|
|
|
|
errno = saved_errno;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-24 01:02:06 +04:00
|
|
|
}
|
2012-10-14 04:31:01 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz, int from_stdin)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-25 03:18:25 +04:00
|
|
|
#endif
|