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 #endif
28 
29 #include "syshead.h"
30 
31 #include "options_util.h"
32 
33 const char *
34 parse_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);
58  o->server_backoff_time = 0;
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 }
gc_new
static struct gc_arena gc_new(void)
Definition: buffer.h:1031
parse_auth_failed_temp
const char * parse_auth_failed_temp(struct options *o, const char *reason)
Definition: options_util.c:34
options_util.h
string_alloc
char * string_alloc(const char *str, struct gc_arena *gc)
Definition: buffer.c:693
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:1039
config.h
options::server_backoff_time
int server_backoff_time
Definition: options.h:291
msg
#define msg(flags,...)
Definition: error.h:150