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-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2014-2015 David Sommerseth <davids@redhat.com>
10 * Copyright (C) 2016-2026 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#ifndef _WIN32
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, (unsigned int)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
81 if (is_console)
82 {
83 if (GetConsoleMode(in, &flags_save))
84 {
85 DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
86 if (echo)
87 {
88 flags |= ENABLE_ECHO_INPUT;
89 }
90 SetConsoleMode(in, flags);
91 }
92 else
93 {
94 is_console = 0;
95 }
96 }
97
98 DWORD len = 0;
99
100 if (is_console)
101 {
102 WCHAR *winput = malloc(capacity * sizeof(WCHAR));
103 if (winput == NULL)
104 {
105 return false;
106 }
107
108 status = ReadConsoleW(in, winput, capacity, &len, NULL);
109 WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
110 free(winput);
111 }
112 else
113 {
114 status = ReadFile(in, input, capacity, &len, NULL);
115 }
116
117 string_null_terminate(input, (int)len, capacity);
118 chomp(input);
119
120 if (!echo)
121 {
122 _write(orig_stderr, "\r\n", 2);
123 }
124 if (is_console)
125 {
126 SetConsoleMode(in, flags_save);
127 }
129 {
130 return true;
131 }
132
133 return false;
134}
135
136#else
137
148static FILE *
149open_tty(const bool write)
150{
151 FILE *ret;
152 ret = fopen("/dev/tty", write ? "w" : "r");
153 if (!ret)
154 {
155 ret = write ? stderr : stdin;
156 }
157 return ret;
158}
159
166static void
167close_tty(FILE *fp)
168{
169 if (fp != stderr && fp != stdin)
170 {
171 fclose(fp);
172 }
173}
174
175#endif
176
177
188static bool
189get_console_input(const char *prompt, const bool echo, char *input, const int capacity)
190{
191 ASSERT(prompt);
192 ASSERT(input);
193 ASSERT(capacity > 0);
194 input[0] = '\0';
195
196#ifdef _WIN32
197 return get_console_input_win32(prompt, echo, input, capacity);
198#else
199 bool restore_tty = false;
200 bool ret = false;
201 struct termios tty_tmp, tty_save;
202
203 /* did we --daemon'ize before asking for passwords?
204 * (in which case neither stdin or stderr are connected to a tty and
205 * /dev/tty can not be open()ed anymore)
206 */
207 if (!isatty(0) && !isatty(2))
208 {
209 int fd = open("/dev/tty", O_RDWR);
210 if (fd < 0)
211 {
212 msg(M_FATAL,
213 "neither stdin nor stderr are a tty device and you have neither a "
214 "controlling tty nor systemd - can't ask for '%s'. If you used --daemon, "
215 "you need to use --askpass to make passphrase-protected keys work, and you "
216 "can not use --auth-nocache.",
217 prompt);
218 }
219 close(fd);
220 }
221
222 FILE *fp = open_tty(true);
223 fprintf(fp, "%s", prompt);
224 fflush(fp);
225 close_tty(fp);
226
227 fp = open_tty(false);
228
229 if (!echo && (tcgetattr(fileno(fp), &tty_tmp) == 0))
230 {
231 tty_save = tty_tmp;
232 tty_tmp.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG);
233 restore_tty = (tcsetattr(fileno(fp), TCSAFLUSH, &tty_tmp) == 0);
234 }
235
236 if (fgets(input, capacity, fp) != NULL)
237 {
238 chomp(input);
239 ret = true;
240 }
241
242 if (restore_tty)
243 {
244 if (tcsetattr(fileno(fp), TCSAFLUSH, &tty_save) == -1)
245 {
246 msg(M_WARN | M_ERRNO, "tcsetattr() failed to restore tty settings");
247 }
248
249 /* Echo the non-echoed newline */
250 close_tty(fp);
251 fp = open_tty(true);
252 fprintf(fp, "\n");
253 fflush(fp);
254 }
255
256 close_tty(fp);
257 return ret;
258#endif
259}
260
273bool
275{
276 bool ret = true; /* Presume everything goes okay */
277 int i;
278
279 /* Loop through configured query_user slots */
280 for (i = 0; i < QUERY_USER_NUMSLOTS && query_user[i].response != NULL; i++)
281 {
282 if (!get_console_input(query_user[i].prompt, query_user[i].echo, query_user[i].response,
283 query_user[i].response_len))
284 {
285 /* Force the final result state to failed on failure */
286 ret = false;
287 }
288 }
289
290 return ret;
291}
void string_null_terminate(char *str, int len, int capacity)
Null-terminate a fixed-length string buffer.
Definition buffer.c:566
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:584
Buffer management functions and garbage collection.
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:41
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:52
@ write
static int orig_stderr
Definition error.c:483
int get_orig_stderr(void)
Definition error.c:486
#define M_FATAL
Definition error.h:90
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
char * response
The user's response.
Definition console.h:36
bool win32_service_interrupt(struct win32_signal *ws)
Definition win32.c:717