2 * FreeRDP: A Remote Desktop Protocol Client
3 * Passphrase Handling Utils
5 * Copyright 2011 Shea Levy <shea@shealevy.com>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include <freerdp/utils/passphrase.h>
23 char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz)
35 #include <freerdp/utils/signal.h>
37 char* freerdp_passphrase_read(const char* prompt, char* buf, size_t bufsiz)
41 char term_name[L_ctermid];
42 int term_file, write_file;
44 size_t read_bytes = 0;
53 if(strcmp(term_name, "") == 0
54 || (term_file = open(term_name, O_RDWR)) == -1)
56 write_file = STDERR_FILENO;
57 terminal_fildes = STDIN_FILENO;
61 write_file = term_file;
62 terminal_fildes = term_file;
65 if (tcgetattr(terminal_fildes, &orig_flags) != -1)
67 new_flags = orig_flags;
68 new_flags.c_lflag &= ~ECHO;
69 new_flags.c_lflag |= ECHONL;
70 terminal_needs_reset = 1;
71 if (tcsetattr(terminal_fildes, TCSAFLUSH, &new_flags) == -1)
72 terminal_needs_reset = 0;
75 if (write(write_file, prompt, strlen(prompt)) == (ssize_t) -1)
79 while ((nbytes = read(terminal_fildes, &read_char, sizeof read_char)) == (sizeof read_char))
81 if (read_char == '\n')
83 if (read_bytes < (bufsiz - (size_t) 1))
86 *buf_iter = read_char;
93 if (nbytes == (ssize_t) -1)
96 if (terminal_needs_reset)
98 if (tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags) == -1)
100 terminal_needs_reset = 0;
103 if (terminal_fildes != STDIN_FILENO)
105 if (close(terminal_fildes) == -1)
113 int saved_errno = errno;
116 if (terminal_needs_reset)
117 tcsetattr(terminal_fildes, TCSAFLUSH, &orig_flags);
118 if (terminal_fildes != STDIN_FILENO)
119 close(terminal_fildes);