OpenVPN
options_util.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-2024 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2021 Fox Crypto B.V. <openvpn@foxcrypto.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include "syshead.h"
30
31#include "options_util.h"
32
33const char *
34parse_auth_failed_temp(struct options *o, const char *reason)
35{
36 struct gc_arena gc = gc_new();
37
38 const char *message = reason;
39 char *m = string_alloc(reason, &gc);
40
41 /* Check if the message uses the TEMP[flags]: message format*/
42 char *endofflags = strstr(m, "]");
43
44 /* Temporary failure from the server */
45 if (m[0] == '[' && endofflags)
46 {
47 message = strstr(reason, "]") + 1;
48 /* null terminate the substring to only looks for flags between [ and ] */
49 *endofflags = '\x00';
50 const char *token = strtok(m, "[,");
51 while (token)
52 {
53 if (!strncmp(token, "backoff ", strlen("backoff ")))
54 {
55 if (sscanf(token, "backoff %d", &o->server_backoff_time) != 1)
56 {
57 msg(D_PUSH, "invalid AUTH_FAIL,TEMP flag: %s", token);
59 }
60 }
61 else if (!strncmp(token, "advance ", strlen("advance ")))
62 {
63 token += strlen("advance ");
64 if (!strcmp(token, "no"))
65 {
66 o->no_advance = true;
67 }
68 else if (!strcmp(token, "remote"))
69 {
70 o->advance_next_remote = true;
71 o->no_advance = false;
72 }
73 else if (!strcmp(token, "addr"))
74 {
75 /* Go on to the next remote */
76 o->no_advance = false;
77 }
78 }
79 else
80 {
81 msg(D_PUSH_ERRORS, "WARNING: unknown AUTH_FAIL,TEMP flag: %s", token);
82 }
83 token = strtok(NULL, "[,");
84 }
85 }
86
87 /* Look for the message in the original buffer to safely be
88 * able to return it */
89 if (!message || message[0] != ':')
90 {
91 message = "";
92 }
93 else
94 {
95 /* Skip the : at the beginning */
96 message += 1;
97 }
98 gc_free(&gc);
99 return message;
100}
101
102bool
103valid_integer(const char *str, bool positive)
104{
105 char *endptr;
106 long long i = strtoll(str, &endptr, 10);
107
108 if (i < INT_MIN || (positive && i < 0) || *endptr != '\0' || i > INT_MAX)
109 {
110 return false;
111 }
112 else
113 {
114 return true;
115 }
116}
117
118int
119positive_atoi(const char *str, int msglevel)
120{
121 char *endptr;
122 long long i = strtoll(str, &endptr, 10);
123
124 if (i < 0 || *endptr != '\0' || i > INT_MAX)
125 {
126 msg(msglevel, "Cannot parse argument '%s' as non-negative integer",
127 str);
128 i = 0;
129 }
130
131 return (int) i;
132}
133
134int
135atoi_warn(const char *str, int msglevel)
136{
137 char *endptr;
138 long long i = strtoll(str, &endptr, 10);
139
140 if (i < INT_MIN || *endptr != '\0' || i > INT_MAX)
141 {
142 msg(msglevel, "Cannot parse argument '%s' as integer", str);
143 i = 0;
144 }
145
146 return (int) i;
147}
char * string_alloc(const char *str, struct gc_arena *gc)
Definition buffer.c:649
static void gc_free(struct gc_arena *a)
Definition buffer.h:1033
static struct gc_arena gc_new(void)
Definition buffer.h:1025
#define D_PUSH
Definition errlevel.h:83
#define D_PUSH_ERRORS
Definition errlevel.h:67
#define msg(flags,...)
Definition error.h:144
int positive_atoi(const char *str, int msglevel)
Converts a str to a positive number if the string represents a postive integer number.
int atoi_warn(const char *str, int msglevel)
Converts a str to an integer if the string can be represented as an integer number.
const char * parse_auth_failed_temp(struct options *o, const char *reason)
bool valid_integer(const char *str, bool positive)
Checks if the string is a valid integer by checking if it can be converted to an integer.
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:117
int server_backoff_time
Definition options.h:304
bool no_advance
Definition options.h:293
bool advance_next_remote
Definition options.h:296
struct gc_arena gc
Definition test_ssl.c:155