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-2023 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 #elif defined(_MSC_VER)
28 #include "config-msvc.h"
29 #endif
30 
31 #include "syshead.h"
32 
33 #include "options_util.h"
34 
35 const char *
36 parse_auth_failed_temp(struct options *o, const char *reason)
37 {
38  struct gc_arena gc = gc_new();
39 
40  const char *message = reason;
41  char *m = string_alloc(reason, &gc);
42 
43  /* Check if the message uses the TEMP[flags]: message format*/
44  char *endofflags = strstr(m, "]");
45 
46  /* Temporary failure from the server */
47  if (m[0] == '[' && endofflags)
48  {
49  message = strstr(reason, "]") + 1;
50  /* null terminate the substring to only looks for flags between [ and ] */
51  *endofflags = '\x00';
52  const char *token = strtok(m, "[,");
53  while (token)
54  {
55  if (!strncmp(token, "backoff ", strlen("backoff ")))
56  {
57  if (sscanf(token, "backoff %d", &o->server_backoff_time) != 1)
58  {
59  msg(D_PUSH, "invalid AUTH_FAIL,TEMP flag: %s", token);
60  o->server_backoff_time = 0;
61  }
62  }
63  else if (!strncmp(token, "advance ", strlen("advance ")))
64  {
65  token += strlen("advance ");
66  if (!strcmp(token, "no"))
67  {
68  o->no_advance = true;
69  }
70  else if (!strcmp(token, "remote"))
71  {
72  o->advance_next_remote = true;
73  o->no_advance = false;
74  }
75  else if (!strcmp(token, "addr"))
76  {
77  /* Go on to the next remote */
78  o->no_advance = false;
79  }
80  }
81  else
82  {
83  msg(D_PUSH_ERRORS, "WARNING: unknown AUTH_FAIL,TEMP flag: %s", token);
84  }
85  token = strtok(NULL, "[,");
86  }
87  }
88 
89  /* Look for the message in the original buffer to safely be
90  * able to return it */
91  if (!message || message[0] != ':')
92  {
93  message = "";
94  }
95  else
96  {
97  /* Skip the : at the beginning */
98  message += 1;
99  }
100  gc_free(&gc);
101  return message;
102 }
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1011
config-msvc.h
parse_auth_failed_temp
const char * parse_auth_failed_temp(struct options *o, const char *reason)
Definition: options_util.c:36
options_util.h
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:695
options
Definition: options.h:236
options::no_advance
bool no_advance
Definition: options.h:280
D_PUSH_ERRORS
#define D_PUSH_ERRORS
Definition: errlevel.h:67
syshead.h
D_PUSH
#define D_PUSH
Definition: errlevel.h:83
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
options::advance_next_remote
bool advance_next_remote
Definition: options.h:283
gc_free
static void gc_free(struct gc_arena *a)
Definition: buffer.h:1019
config.h
options::server_backoff_time
int server_backoff_time
Definition: options.h:291
msg
#define msg(flags,...)
Definition: error.h:150