OpenVPN
misc.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/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#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include "syshead.h"
30
31#include "buffer.h"
32#include "misc.h"
33#include "base64.h"
34#include "tun.h"
35#include "error.h"
36#include "otime.h"
37#include "plugin.h"
38#include "options.h"
39#include "manage.h"
40#include "crypto.h"
41#include "route.h"
42#include "console.h"
43#include "win32.h"
44
45#include "memdbg.h"
46
47#ifdef ENABLE_IPROUTE
48const char *iproute_path = IPROUTE_PATH; /* GLOBAL */
49#endif
50
51/*
52 * Set standard file descriptors to /dev/null
53 */
54void
55set_std_files_to_null(bool stdin_only)
56{
57#if defined(HAVE_DUP) && defined(HAVE_DUP2)
58 int fd;
59 if ((fd = open("/dev/null", O_RDWR, 0)) != -1)
60 {
61 dup2(fd, 0);
62 if (!stdin_only)
63 {
64 dup2(fd, 1);
65 dup2(fd, 2);
66 }
67 if (fd > 2)
68 {
69 close(fd);
70 }
71 }
72#endif
73}
74
75#if defined(__GNUC__) || defined(__clang__)
76#pragma GCC diagnostic push
77#pragma GCC diagnostic ignored "-Wsign-conversion"
78#endif
79
80#ifdef ENABLE_MANAGEMENT
81/* Get username/password from the management interface */
82static bool
83auth_user_pass_mgmt(struct user_pass *up, const char *prefix, const unsigned int flags,
84 const char *auth_challenge)
85{
86 const char *sc = NULL;
87
89 {
90 management_auth_failure(management, prefix, "previous auth credentials failed");
91 }
92
94 {
95 sc = auth_challenge;
96 }
97 if (!management_query_user_pass(management, up, prefix, flags, sc))
98 {
99 if ((flags & GET_USER_PASS_NOFATAL) != 0)
100 {
101 return false;
102 }
103 else
104 {
105 msg(M_FATAL,
106 "ERROR: could not read %s username/password/ok/string from management interface",
107 prefix);
108 }
109 }
110 return true;
111}
112
126static struct auth_challenge_info *
128{
130 const size_t len = strlen(auth_challenge);
131 if (!buf_size_valid(len))
132 {
133 return NULL;
134 }
135
136 const int work_len = (int)len + 1;
137 char *work = (char *)gc_malloc(work_len, false, gc);
138
139 struct buffer b;
140 buf_set_read(&b, (const uint8_t *)auth_challenge, len);
141
142 struct auth_challenge_info *ac;
144
145 /* parse prefix */
146 if (!buf_parse(&b, ':', work, work_len))
147 {
148 return NULL;
149 }
150 if (strcmp(work, "CRV1"))
151 {
152 return NULL;
153 }
154
155 /* parse flags */
156 if (!buf_parse(&b, ':', work, work_len))
157 {
158 return NULL;
159 }
160 for (const char *cp = work; *cp != '\0'; ++cp)
161 {
162 const char c = *cp;
163 if (c == 'E')
164 {
165 ac->flags |= CR_ECHO;
166 }
167 else if (c == 'R')
168 {
169 ac->flags |= CR_RESPONSE;
170 }
171 }
172
173 /* parse state ID */
174 if (!buf_parse(&b, ':', work, work_len))
175 {
176 return NULL;
177 }
178 ac->state_id = string_alloc(work, gc);
179
180 /* parse user name */
181 if (!buf_parse(&b, ':', work, work_len))
182 {
183 return NULL;
184 }
185 ac->user = (char *)gc_malloc(strlen(work) + 1, true, gc);
186 openvpn_base64_decode(work, (void *)ac->user, -1);
187
188 /* parse challenge text */
190
191 return ac;
192}
193
194#endif /* ifdef ENABLE_MANAGEMENT */
195
196/*
197 * Get and store a username/password
198 */
199
200bool
201get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix,
202 const unsigned int flags, const char *auth_challenge)
203{
204 struct gc_arena gc = gc_new();
205
206 if (!up->defined)
207 {
208 bool from_authfile = (auth_file && !streq(auth_file, "stdin"));
209 bool username_from_stdin = false;
210 bool password_from_stdin = false;
211 bool response_from_stdin = true;
212
215 {
216 msg(M_WARN, "Note: previous '%s' credentials failed", prefix);
217 }
218#ifdef ENABLE_MANAGEMENT
219 /*
220 * Get username/password from management interface?
221 */
222 if (management && (!from_authfile && (flags & GET_USER_PASS_MANAGEMENT))
224 {
225 response_from_stdin = false;
226 if (!auth_user_pass_mgmt(up, prefix, flags, auth_challenge))
227 {
228 return false;
229 }
230 }
231 else
232#endif /* ifdef ENABLE_MANAGEMENT */
233 /*
234 * Get NEED_OK confirmation from the console
235 */
236 if (flags & GET_USER_PASS_NEED_OK)
237 {
238 struct buffer user_prompt = alloc_buf_gc(128, &gc);
239
240 buf_printf(&user_prompt, "NEED-OK|%s|%s:", prefix, up->username);
241 if (!query_user_SINGLE(BSTR(&user_prompt), up->password, USER_PASS_LEN, false))
242 {
243 msg(M_FATAL, "ERROR: could not read %s ok-confirmation from stdin", prefix);
244 }
245
246 if (!strlen(up->password))
247 {
248 strcpy(up->password, "ok");
249 }
250 }
251 else if (flags & GET_USER_PASS_INLINE_CREDS)
252 {
253 struct buffer buf;
254 buf_set_read(&buf, (uint8_t *)auth_file, strlen(auth_file) + 1);
255 if (!(flags & GET_USER_PASS_PASSWORD_ONLY))
256 {
257 buf_parse(&buf, '\n', up->username, USER_PASS_LEN);
258 /* The password starts on the next line, even if the username was truncated. */
259 const char *password = strchr(auth_file, '\n');
260 password = password ? password + 1 : "";
261 buf_set_read(&buf, (uint8_t *)password, strlen(password) + 1);
262 }
263 buf_parse(&buf, '\n', up->password, USER_PASS_LEN);
264
265 if (strlen(up->password) == 0)
266 {
267 password_from_stdin = 1;
268 }
269 }
270 /*
271 * Read from auth file unless this is a dynamic challenge request.
272 */
273 else if (from_authfile && !(flags & GET_USER_PASS_DYNAMIC_CHALLENGE))
274 {
275 /*
276 * Try to get username/password from a file.
277 */
278 FILE *fp;
279 char password_buf[USER_PASS_LEN] = { '\0' };
280
281 fp = platform_fopen(auth_file, "r");
282 if (!fp)
283 {
284 msg(M_ERR, "Error opening '%s' auth file: %s", prefix, auth_file);
285 }
286
287 if ((flags & GET_USER_PASS_PASSWORD_ONLY) == 0)
288 {
289 /* Read username first */
290 if (fgets(up->username, USER_PASS_LEN, fp) == NULL)
291 {
292 msg(M_FATAL, "Error reading username from %s authfile: %s", prefix,
293 auth_file);
294 }
295 if (!strchr(up->username, '\n'))
296 {
297 /* Consume the rest of the username line before reading the password. */
298 int c;
299 while ((c = fgetc(fp)) != '\n' && c != EOF)
300 {
301 }
302 }
303 }
304 chomp(up->username);
305
306 if (fgets(password_buf, USER_PASS_LEN, fp) != NULL)
307 {
308 chomp(password_buf);
309 }
310
311 if (flags & GET_USER_PASS_PASSWORD_ONLY && !password_buf[0])
312 {
313 msg(M_FATAL, "Error reading password from %s authfile: %s", prefix, auth_file);
314 }
315
316 if (password_buf[0])
317 {
318 strlcpy(up->password, password_buf, USER_PASS_LEN);
319 }
320 else
321 {
322 password_from_stdin = 1;
323 }
324
325 fclose(fp);
326
327 if (!(flags & GET_USER_PASS_PASSWORD_ONLY) && strlen(up->username) == 0)
328 {
329 msg(M_FATAL, "ERROR: username from %s authfile '%s' is empty", prefix,
330 auth_file);
331 }
332 }
333 else
334 {
335 username_from_stdin = true;
336 password_from_stdin = true;
337 }
338
339 /*
340 * Get username/password from standard input?
341 */
342 if (username_from_stdin || password_from_stdin || response_from_stdin)
343 {
344#ifdef ENABLE_MANAGEMENT
345 /* If management-query-passwords is true, we could be here because
346 * of no password present in auth-file or inline. In that case
347 * query via the management interface instead of stdin/console.
348 */
349 if (management && (flags & GET_USER_PASS_MANAGEMENT)
351 && password_from_stdin
352 && !(flags & GET_USER_PASS_USERNAME_ONLY))
353 {
354 msg(D_LOW,
355 "No '%s' password found in authfile or inline. Querying the management interface",
356 prefix);
357 if (!auth_user_pass_mgmt(up, prefix, flags, auth_challenge))
358 {
359 return false;
360 }
361 }
362 else if (auth_challenge && (flags & GET_USER_PASS_DYNAMIC_CHALLENGE) && response_from_stdin)
363 {
365 if (ac)
366 {
367 char *response = (char *)gc_malloc(USER_PASS_LEN, false, &gc);
368 struct buffer packed_resp, challenge;
369
370 challenge = alloc_buf_gc(14 + strlen(ac->challenge_text), &gc);
371 buf_printf(&challenge, "CHALLENGE: %s", ac->challenge_text);
372 buf_set_write(&packed_resp, (uint8_t *)up->password, USER_PASS_LEN);
373
374 if (!query_user_SINGLE(BSTR(&challenge), response,
376 {
377 msg(M_FATAL, "ERROR: could not read challenge response from stdin");
378 }
380 buf_printf(&packed_resp, "CRV1::%s::%s", ac->state_id, response);
381 }
382 else
383 {
384 msg(M_FATAL, "ERROR: received malformed challenge request from server");
385 }
386 }
387 else
388#endif /* ifdef ENABLE_MANAGEMENT */
389 {
390 struct buffer user_prompt = alloc_buf_gc(128, &gc);
391 struct buffer pass_prompt = alloc_buf_gc(128, &gc);
392
394 buf_printf(&user_prompt, "Enter %s Username:", prefix);
395 buf_printf(&pass_prompt, "Enter %s Password:", prefix);
396
397 if (username_from_stdin && !(flags & GET_USER_PASS_PASSWORD_ONLY))
398 {
399 query_user_add(BSTR(&user_prompt), up->username, USER_PASS_LEN, true);
400 }
401
402 if (password_from_stdin && !(flags & GET_USER_PASS_USERNAME_ONLY))
403 {
404 query_user_add(BSTR(&pass_prompt), up->password, USER_PASS_LEN, false);
405 }
406
407 if (!query_user_exec())
408 {
409 msg(M_FATAL, "ERROR: Failed retrieving username or password");
410 }
411
412 if (!(flags & GET_USER_PASS_PASSWORD_ONLY))
413 {
414 if (strlen(up->username) == 0)
415 {
416 msg(M_FATAL, "ERROR: %s username is empty", prefix);
417 }
418 }
419
420#ifdef ENABLE_MANAGEMENT
422 && response_from_stdin)
423 {
424 char *response = (char *)gc_malloc(USER_PASS_LEN, false, &gc);
425 struct buffer packed_resp, challenge;
426 char *pw64 = NULL, *resp64 = NULL;
427
428 challenge = alloc_buf_gc(14 + strlen(auth_challenge), &gc);
429 buf_printf(&challenge, "CHALLENGE: %s", auth_challenge);
430
431 if (!query_user_SINGLE(BSTR(&challenge), response, USER_PASS_LEN,
433 {
434 msg(M_FATAL, "ERROR: could not retrieve static challenge response");
435 }
437 {
438 if (openvpn_base64_encode(up->password, (int)strlen(up->password), &pw64) == -1
439 || openvpn_base64_encode(response, (int)strlen(response), &resp64) == -1)
440 {
441 msg(M_FATAL, "ERROR: could not base64-encode password/static_response");
442 }
443 buf_set_write(&packed_resp, (uint8_t *)up->password, USER_PASS_LEN);
444 buf_printf(&packed_resp, "SCRV1:%s:%s", pw64, resp64);
445 string_clear(pw64);
446 free(pw64);
447 string_clear(resp64);
448 free(resp64);
449 }
450 else
451 {
452 if (strlcat(up->password, response, USER_PASS_LEN) >= USER_PASS_LEN)
453 {
454 msg(M_FATAL,
455 "ERROR: could not concatenate password/static_response: string too long");
456 }
457 }
458 }
459#endif /* ifdef ENABLE_MANAGEMENT */
460 }
461 }
462
463 /* Use tag for blank password if we are not prompting for one */
464 if (flags & GET_USER_PASS_USERNAME_ONLY)
465 {
466 strlcpy(up->password, blank_up, sizeof(up->password));
467 }
468
471
472 up->defined = true;
473 }
474
475#if 0
476 msg(M_INFO, "GET_USER_PASS %s u='%s' p='%s'", prefix, up->username, up->password);
477#endif
478
479 gc_free(&gc);
480
481 return true;
482}
483
484void
485purge_user_pass(struct user_pass *up, const bool force)
486{
487 const bool nocache = up->nocache;
488
489 if (nocache || force)
490 {
491 secure_memzero(up, sizeof(*up));
492 up->nocache = nocache;
493 }
494 else
495 {
496 static bool warn_shown = false;
497
499 /*
500 * don't show warning if the pass has been replaced by a token: this is an
501 * artificial "auth-nocache"
502 */
503 if (!warn_shown)
504 {
505 msg(M_WARN,
506 "WARNING: this configuration may cache passwords in memory -- use the auth-nocache option to prevent this");
507 warn_shown = true;
508 }
509 }
510}
511
512void
513set_auth_token(struct user_pass *tk, const char *token)
514{
515 if (strlen(token))
516 {
518 strlcpy(tk->password, token, USER_PASS_LEN);
519 tk->token_defined = true;
520
521 /*
522 * If username already set, tk is fully defined.
523 */
524 if (strlen(tk->username))
525 {
526 tk->defined = true;
527 }
529 }
530}
531
532void
533set_auth_token_user(struct user_pass *tk, const char *username)
534{
535 if (strlen(username))
536 {
538 /* Clear the username before decoding to ensure no old material is left
539 * and also allow decoding to not use all space to ensure the last byte is
540 * always 0 */
541 CLEAR(tk->username);
542 int len = openvpn_base64_decode(username, tk->username, USER_PASS_LEN - 1);
543 tk->defined = len > 0;
544 if (!tk->defined)
545 {
546 msg(D_PUSH, "Error decoding auth-token-username");
547 }
549 }
550}
551
552
553/*
554 * Process string received by untrusted peer before
555 * printing to console or log file.
556 *
557 * Assumes that string has been null terminated.
558 */
559const char *
560safe_print(const char *str, struct gc_arena *gc)
561{
562 return string_mod_const(str, CC_PRINT, CC_CRLF, '.', gc);
563}
564
565const char **
566make_arg_array(const char *first, const char *parms, struct gc_arena *gc)
567{
568 char **ret = NULL;
569 int base = 0;
570 const int max_parms = MAX_PARMS + 2;
571 int n = 0;
572
573 /* alloc return array */
574 ALLOC_ARRAY_CLEAR_GC(ret, char *, max_parms, gc);
575
576 /* process first parameter, if provided */
577 if (first)
578 {
579 ret[base++] = string_alloc(first, gc);
580 }
581
582 if (parms)
583 {
584 n = parse_line(parms, &ret[base], max_parms - base - 1, "make_arg_array", 0, M_WARN, gc);
585 ASSERT(n >= 0 && n + base + 1 <= max_parms);
586 }
587 ret[base + n] = NULL;
588
589 return (const char **)ret;
590}
591
592static const char **
593make_inline_array(const char *str, struct gc_arena *gc)
594{
595 char line[OPTION_LINE_SIZE];
596 struct buffer buf;
597 int len = 0;
598 char **ret = NULL;
599 int i = 0;
600
601 buf_set_read(&buf, (const uint8_t *)str, strlen(str));
602 while (buf_parse(&buf, '\n', line, sizeof(line)))
603 {
604 ++len;
605 }
606
607 /* alloc return array */
608 ALLOC_ARRAY_CLEAR_GC(ret, char *, len + 1, gc);
609
610 buf_set_read(&buf, (const uint8_t *)str, strlen(str));
611 while (buf_parse(&buf, '\n', line, sizeof(line)))
612 {
613 chomp(line);
614 ASSERT(i < len);
615 ret[i] = string_alloc(skip_leading_whitespace(line), gc);
616 ++i;
617 }
618 ASSERT(i <= len);
619 ret[i] = NULL;
620 return (const char **)ret;
621}
622
623static const char **
624make_arg_copy(char **p, struct gc_arena *gc)
625{
626 char **ret = NULL;
627 const int len = string_array_len((const char **)p);
628 const int max_parms = len + 1;
629 int i;
630
631 /* alloc return array */
632 ALLOC_ARRAY_CLEAR_GC(ret, char *, max_parms, gc);
633
634 for (i = 0; i < len; ++i)
635 {
636 ret[i] = p[i];
637 }
638
639 return (const char **)ret;
640}
641
642const char **
643make_extended_arg_array(char **p, bool is_inline, struct gc_arena *gc)
644{
645 const int argc = string_array_len((const char **)p);
646 if (is_inline)
647 {
648 return make_inline_array(p[0], gc);
649 }
650 else if (argc == 0)
651 {
652 return make_arg_array(NULL, NULL, gc);
653 }
654 else if (argc == 1)
655 {
656 return make_arg_array(p[0], NULL, gc);
657 }
658 else if (argc == 2)
659 {
660 return make_arg_array(p[0], p[1], gc);
661 }
662 else
663 {
664 return make_arg_copy(p, gc);
665 }
666}
667
668/*
669 * Remove security-sensitive strings from control message
670 * so that they will not be output to log file.
671 */
672const char *
673sanitize_control_message(const char *src, struct gc_arena *gc)
674{
675 char *ret = gc_malloc(strlen(src) + 1, false, gc);
676 char *dest = ret;
677 bool redact = false;
678 int skip = 0;
679
680 for (;;)
681 {
682 const char c = *src;
683 if (c == '\0')
684 {
685 break;
686 }
687 if (c == 'S' && !strncmp(src, "SESS_ID_", 8))
688 {
689 skip = 7;
690 redact = true;
691 }
692 else if (c == 'e' && !strncmp(src, "echo ", 5))
693 {
694 skip = 4;
695 redact = true;
696 }
697 else if (!check_debug_level(D_SHOW_KEYS) && (c == 'a' && !strncmp(src, "auth-token ", 11)))
698 {
699 /* Unless --verb is 7 or higher (D_SHOW_KEYS), hide
700 * the auth-token value coming in the src string
701 */
702 skip = 10;
703 redact = true;
704 }
705
706 if (c == ',') /* end of redacted item? */
707 {
708 skip = 0;
709 redact = false;
710 }
711
712 if (redact)
713 {
714 if (skip > 0)
715 {
716 --skip;
717 *dest++ = c;
718 }
719 }
720 else
721 {
722 *dest++ = c;
723 }
724
725 ++src;
726 }
727 *dest = '\0';
728 return ret;
729}
730
731/* helper to parse peer_info received from multi client, validate
732 * (this is untrusted data) and put into environment
733 */
734bool
736{
737 int state = 0;
738 while (*line)
739 {
740 const uint8_t c = *line;
741 switch (state)
742 {
743 case 0:
744 case 1:
745 if (c == '=' && state == 1)
746 {
747 state = 2;
748 }
749 else if (isalnum(c) || c == '_')
750 {
751 state = 1;
752 }
753 else
754 {
755 return false;
756 }
757 /* Intentional [[fallthrough]]; */
758 case 2:
759 /* after the '=', replace non-printable or shell meta with '_' */
760 if (!isprint(c) || isspace(c) || c == '$' || c == '(' || c == '`')
761 {
762 *line = '_';
763 }
764 }
765 line++;
766 }
767 return (state == 2);
768}
769
770#if defined(__GNUC__) || defined(__clang__)
771#pragma GCC diagnostic pop
772#endif
773
774void
775output_peer_info_env(struct env_set *es, const char *peer_info)
776{
777 char line[256];
778 struct buffer buf;
779 buf_set_read(&buf, (const uint8_t *)peer_info, strlen(peer_info));
780 while (buf_parse(&buf, '\n', line, sizeof(line)))
781 {
782 chomp(line);
784 && (strncmp(line, "IV_", 3) == 0 || strncmp(line, "UV_", 3) == 0
785 || strncmp(line, "ID", 2) == 0))
786 {
787 msg(M_INFO, "peer info: %s", line);
788 env_set_add(es, line);
789 }
790 else
791 {
792 msg(M_WARN, "validation failed on peer_info line received");
793 }
794 }
795}
796
797struct buffer
798prepend_dir(const char *dir, const char *path, struct gc_arena *gc)
799{
800 size_t len = strlen(dir) + strlen(PATH_SEPARATOR_STR) + strlen(path) + 1;
801 struct buffer combined_path = alloc_buf_gc(len, gc);
802 buf_printf(&combined_path, "%s%s%s", dir, PATH_SEPARATOR_STR, path);
803 ASSERT(combined_path.len > 0);
804
805 return combined_path;
806}
807
808void
810{
811 if (up->protected)
812 {
813 return;
814 }
815#ifdef _WIN32
816 if (protect_buffer_win32(up->username, sizeof(up->username))
817 && protect_buffer_win32(up->password, sizeof(up->password)))
818 {
819 up->protected = true;
820 }
821 else
822 {
823 purge_user_pass(up, true);
824 }
825#endif
826}
827
828void
830{
831 if (!up->protected)
832 {
833 return;
834 }
835#ifdef _WIN32
836 if (unprotect_buffer_win32(up->username, sizeof(up->username))
837 && unprotect_buffer_win32(up->password, sizeof(up->password)))
838 {
839 up->protected = false;
840 }
841 else
842 {
843 purge_user_pass(up, true);
844 }
845#endif
846}
const char * skip_leading_whitespace(const char *str)
Return a pointer past any leading whitespace in a string.
Definition buffer.c:559
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void string_clear(char *str)
Securely clear a null-terminated string.
Definition buffer.c:651
void chomp(char *str)
Remove trailing newline and carriage-return characters from a string.
Definition buffer.c:596
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
bool string_mod(char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace)
Modifies a string in place by replacing certain classes of characters of it with a specified characte...
Definition buffer.c:1021
const char * string_mod_const(const char *str, const unsigned int inclusive, const unsigned int exclusive, const char replace, struct gc_arena *gc)
Returns a copy of a string with certain classes of characters of it replaced with a specified charact...
Definition buffer.c:1072
int string_array_len(const char **array)
Return the number of elements in a NULL-terminated array of strings.
Definition buffer.c:663
bool buf_parse(struct buffer *buf, const int delim, char *line, const int size)
Extract the next token from a buffer, delimited by a given character.
Definition buffer.c:783
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
Buffer management functions and garbage collection.
static bool buf_size_valid(const size_t size)
Return true iff size is within the allowed buffer size range.
Definition buffer.h:543
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
#define streq(x, y)
helper to work around the semantics of the strcmp() return value
Definition buffer.h:1822
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc)
Allocate and zero-initialise a garbage-collected array of n elements.
Definition buffer.h:2091
#define CC_CRLF
carriage return or newline
Definition buffer.h:1723
static void buf_set_read(struct buffer *buf, const uint8_t *data, size_t size)
Initialise a buffer with an externally provided read-only memory region.
Definition buffer.h:687
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
static void buf_set_write(struct buffer *buf, uint8_t *data, size_t size)
Initialise a buffer with an externally provided writable memory region.
Definition buffer.h:658
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
Definition buffer.h:2132
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
#define CC_PRINT
printable (>= 32, != 127)
Definition buffer.h:1694
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
size_t strlcpy(char *dest, const char *src, size_t maxlen)
size_t strlcat(char *dest, const char *src, size_t maxlen)
void query_user_clear(void)
Wipes all data put into all of the query_user structs.
Definition console.c:44
void query_user_add(char *prompt, char *resp, size_t resp_len, bool echo)
Adds an item to ask the user for.
Definition console.c:56
static bool query_user_SINGLE(char *prompt, char *resp, size_t resp_len, bool echo)
A plain "make Gert happy" wrapper.
Definition console.h:118
static bool query_user_exec(void)
Wrapper function enabling query_user_exec() if no alternative methods have been enabled.
Definition console.h:103
Data Channel Cryptography Module.
void env_set_add(struct env_set *es, const char *str)
Definition env_set.c:192
#define D_PUSH
Definition errlevel.h:81
#define D_SHOW_KEYS
Definition errlevel.h:118
#define D_LOW
Definition errlevel.h:95
#define M_INFO
Definition errlevel.h:54
bool management_query_user_pass(struct management *man, struct user_pass *up, const char *type, const unsigned int flags, const char *static_challenge)
Definition manage.c:3647
void management_auth_failure(const struct management *man, const char *type, const char *reason)
Definition manage.c:3253
static bool management_query_user_pass_enabled(const struct management *man)
Definition manage.h:430
static const char ** make_inline_array(const char *str, struct gc_arena *gc)
Definition misc.c:593
void unprotect_user_pass(struct user_pass *up)
Decrypt username and password buffers in user_pass.
Definition misc.c:829
bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *auth_challenge)
Retrieves the user credentials from various sources depending on the flags.
Definition misc.c:201
const char ** make_arg_array(const char *first, const char *parms, struct gc_arena *gc)
Definition misc.c:566
void purge_user_pass(struct user_pass *up, const bool force)
Definition misc.c:485
bool validate_peer_info_line(char *line)
Definition misc.c:735
void set_auth_token_user(struct user_pass *tk, const char *username)
Sets the auth-token username by base64 decoding the passed username.
Definition misc.c:533
void set_std_files_to_null(bool stdin_only)
Definition misc.c:55
void output_peer_info_env(struct env_set *es, const char *peer_info)
Definition misc.c:775
struct buffer prepend_dir(const char *dir, const char *path, struct gc_arena *gc)
Prepend a directory to a path.
Definition misc.c:798
static struct auth_challenge_info * parse_auth_challenge(const char *auth_challenge, struct gc_arena *gc)
Parses an authentication challenge string and returns an auth_challenge_info structure.
Definition misc.c:127
void protect_user_pass(struct user_pass *up)
Encrypt username and password buffers in user_pass.
Definition misc.c:809
const char ** make_extended_arg_array(char **p, bool is_inline, struct gc_arena *gc)
Definition misc.c:643
static const char ** make_arg_copy(char **p, struct gc_arena *gc)
Definition misc.c:624
const char * safe_print(const char *str, struct gc_arena *gc)
Definition misc.c:560
const char * sanitize_control_message(const char *src, struct gc_arena *gc)
Definition misc.c:673
static bool auth_user_pass_mgmt(struct user_pass *up, const char *prefix, const unsigned int flags, const char *auth_challenge)
Definition misc.c:83
void set_auth_token(struct user_pass *tk, const char *token)
Sets the auth-token to token.
Definition misc.c:513
#define USER_PASS_LEN
Definition misc.h:67
#define GET_USER_PASS_STATIC_CHALLENGE_CONCAT
indicates password and response should be concatenated
Definition misc.h:128
#define GET_USER_PASS_MANAGEMENT
Definition misc.h:113
#define GET_USER_PASS_PASSWORD_ONLY
Definition misc.h:115
#define GET_USER_PASS_USERNAME_ONLY
indicate that only username should be prompted for auth-user-pass
Definition misc.h:130
#define GET_USER_PASS_STATIC_CHALLENGE_ECHO
SCRV1 protocol – echo response.
Definition misc.h:123
#define CR_ECHO
Definition misc.h:80
#define CR_RESPONSE
Definition misc.h:81
#define GET_USER_PASS_INLINE_CREDS
indicates that auth_file is actually inline creds
Definition misc.h:126
#define GET_USER_PASS_STATIC_CHALLENGE
SCRV1 protocol – static challenge.
Definition misc.h:122
#define GET_USER_PASS_NEED_OK
Definition misc.h:116
#define GET_USER_PASS_NOFATAL
Definition misc.h:117
static const char blank_up[]
Definition misc.h:52
#define GET_USER_PASS_PREVIOUS_CREDS_FAILED
Definition misc.h:119
#define GET_USER_PASS_DYNAMIC_CHALLENGE
CRV1 protocol – dynamic challenge.
Definition misc.h:121
#define BOOL_CAST(x)
Definition basic.h:26
#define CLEAR(x)
Definition basic.h:32
static bool check_debug_level(msglvl_t level)
Definition error.h:253
#define M_FATAL
Definition error.h:90
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
int parse_line(const char *line, char *p[], const int n, const char *file, const int line_num, msglvl_t msglevel, struct gc_arena *gc)
#define MAX_PARMS
Definition options.h:51
#define OPTION_LINE_SIZE
Definition options.h:57
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:505
int openvpn_base64_decode(const char *str, void *data, int size)
Definition base64.c:160
int openvpn_base64_encode(const void *data, int size, char **str)
Definition base64.c:51
static char * auth_challenge
Definition ssl.c:308
const char * user
Definition misc.h:84
const char * state_id
Definition misc.h:85
const char * challenge_text
Definition misc.h:86
unsigned int flags
Definition misc.h:82
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
bool token_defined
Definition misc.h:59
bool protected
Definition misc.h:61
bool defined
Definition misc.h:56
char password[USER_PASS_LEN]
Definition misc.h:71
bool nocache
Definition misc.h:60
char username[USER_PASS_LEN]
Definition misc.h:70
#define PATH_SEPARATOR_STR
Definition syshead.h:429
struct gc_arena gc
Definition test_ssl.c:138
bool protect_buffer_win32(char *buf, DWORD len)
Encrypt a region of memory using CryptProtectMemory() with access restricted to the current process.
Definition win32.c:1628
bool unprotect_buffer_win32(char *buf, DWORD len)
Decrypt a previously encrypted region of memory using CryptUnProtectMemory() with access restricted t...
Definition win32.c:1646