OpenVPN
console_builtin.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2014-2015 David Sommerseth <davids@redhat.com>
10 * Copyright (C) 2016-2025 David Sommerseth <davids@openvpn.net>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, see <https://www.gnu.org/licenses/>.
23 */
24
25/*
26 * These functions covers handing user input/output using the default consoles
27 *
28 */
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include "syshead.h"
35#include "console.h"
36#include "error.h"
37#include "buffer.h"
38#include "misc.h"
39
40#ifdef HAVE_TERMIOS_H
41#include <termios.h>
42#endif
43
44#ifdef _WIN32
45
46#include "win32.h"
47
59static bool
60get_console_input_win32(const char *prompt, const bool echo, char *input, const int capacity)
61{
62 ASSERT(prompt);
63 ASSERT(input);
64 ASSERT(capacity > 0);
65
66 input[0] = '\0';
67
68 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
69 int orig_stderr = get_orig_stderr(); /* guaranteed to be always valid */
70 if ((in == INVALID_HANDLE_VALUE) || win32_service_interrupt(&win32_signal)
71 || (_write(orig_stderr, prompt, strlen(prompt)) == -1))
72 {
73 msg(M_WARN | M_ERRNO, "get_console_input_win32(): unexpected error");
74 return false;
75 }
76
77 bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
78 DWORD flags_save = 0;
79 int status = 0;
80 WCHAR *winput;
81
82 if (is_console)
83 {
84 if (GetConsoleMode(in, &flags_save))
85 {
86 DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
87 if (echo)
88 {
89 flags |= ENABLE_ECHO_INPUT;
90 }
91 SetConsoleMode(in, flags);
92 }
93 else
94 {
95 is_console = 0;
96 }
97 }
98
99 DWORD len = 0;
100
101 if (is_console)
102 {
103 winput = malloc(capacity * sizeof(WCHAR));
104 if (winput == NULL)
105 {
106 return false;
107 }
108
109 status = ReadConsoleW(in, winput, capacity, &len, NULL);
110 WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
111 free(winput);
112 }
113 else
114 {
115 status = ReadFile(in, input, capacity, &len, NULL);
116 }
117
118 string_null_terminate(input, (int)len, capacity);
119 chomp(input);
120
121 if (!echo)
122 {
123 _write(orig_stderr, "\r\n", 2);
124 }
125 if (is_console)
126 {
127 SetConsoleMode(in, flags_save);
128 }
130 {
131 return true;
132 }
133
134 return false;
135}
136
137#endif /* _WIN32 */
138
139
140#ifdef HAVE_TERMIOS_H
141
152static FILE *
153open_tty(const bool write)
154{
155 FILE *ret;
156 ret = fopen("/dev/tty", write ? "w" : "r");
157 if (!ret)
158 {
159 ret = write ? stderr : stdin;
160 }
161 return ret;
162}
163
170static void
171close_tty(FILE *fp)
172{
173 if (fp != stderr && fp != stdin)
174 {
175 fclose(fp);
176 }
177}
178
179#endif /* HAVE_TERMIOS_H */
180
181
192static bool
193get_console_input(const char *prompt, const bool echo, char *input, const int capacity)
194{
195 bool ret = false;
196 ASSERT(prompt);
197 ASSERT(input);
198 ASSERT(capacity > 0);
199 input[0] = '\0';
200
201#if defined(_WIN32)
202 return get_console_input_win32(prompt, echo, input, capacity);
203#elif defined(HAVE_TERMIOS_H)
204 bool restore_tty = false;
205 struct termios tty_tmp, tty_save;
206
207 /* did we --daemon'ize before asking for passwords?
208 * (in which case neither stdin or stderr are connected to a tty and
209 * /dev/tty can not be open()ed anymore)
210 */
211 if (!isatty(0) && !isatty(2))
212 {
213 int fd = open("/dev/tty", O_RDWR);
214 if (fd < 0)
215 {
216 msg(M_FATAL,
217 "neither stdin nor stderr are a tty device and you have neither a "
218 "controlling tty nor systemd - can't ask for '%s'. If you used --daemon, "
219 "you need to use --askpass to make passphrase-protected keys work, and you "
220 "can not use --auth-nocache.",
221 prompt);
222 }
223 close(fd);
224 }
225
226 FILE *fp = open_tty(true);
227 fprintf(fp, "%s", prompt);
228 fflush(fp);
229 close_tty(fp);
230
231 fp = open_tty(false);
232
233 if (!echo && (tcgetattr(fileno(fp), &tty_tmp) == 0))
234 {
235 tty_save = tty_tmp;
236 tty_tmp.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG);
237 restore_tty = (tcsetattr(fileno(fp), TCSAFLUSH, &tty_tmp) == 0);
238 }
239
240 if (fgets(input, capacity, fp) != NULL)
241 {
242 chomp(input);
243 ret = true;
244 }
245
246 if (restore_tty)
247 {
248 if (tcsetattr(fileno(fp), TCSAFLUSH, &tty_save) == -1)
249 {
250 msg(M_WARN | M_ERRNO, "tcsetattr() failed to restore tty settings");
251 }
252
253 /* Echo the non-echoed newline */
254 close_tty(fp);
255 fp = open_tty(true);
256 fprintf(fp, "\n");
257 fflush(fp);
258 }
259
260 close_tty(fp);
261#else /* if defined(_WIN32) */
262 msg(M_FATAL, "Sorry, but I can't get console input on this OS (%s)", prompt);
263#endif /* if defined(_WIN32) */
264 return ret;
265}
266
267
280bool
282{
283 bool ret = true; /* Presume everything goes okay */
284 int i;
285
286 /* Loop through configured query_user slots */
287 for (i = 0; i < QUERY_USER_NUMSLOTS && query_user[i].response != NULL; i++)
288 {
289 if (!get_console_input(query_user[i].prompt, query_user[i].echo, query_user[i].response,
290 query_user[i].response_len))
291 {
292 /* Force the final result state to failed on failure */
293 ret = false;
294 }
295 }
296
297 return ret;
298}
void string_null_terminate(char *str, int len, int capacity)
Definition buffer.c:597
void chomp(char *str)
Definition buffer.c:614
struct _query_user query_user[QUERY_USER_NUMSLOTS]
Global variable, declared in console.c.
Definition console.c:40
#define QUERY_USER_NUMSLOTS
Definition console.h:42
bool query_user_exec_builtin(void)
Wrapper function enabling query_user_exec() if no alternative methods have been enabled.
static bool get_console_input_win32(const char *prompt, const bool echo, char *input, const int capacity)
Get input from a Windows console.
static bool get_console_input(const char *prompt, const bool echo, char *input, const int capacity)
Core function for getting input from console.
static SERVICE_STATUS status
Definition interactive.c:51
@ write
static int orig_stderr
Definition error.c:486
int get_orig_stderr(void)
Definition error.c:489
#define M_FATAL
Definition error.h:88
#define msg(flags,...)
Definition error.h:150
#define ASSERT(x)
Definition error.h:217
#define M_WARN
Definition error.h:90
#define M_ERRNO
Definition error.h:93
char * response
The user's response.
Definition console.h:37
bool win32_service_interrupt(struct win32_signal *ws)
Definition win32.c:619