OpenVPN
misc.h
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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef MISC_H
25 #define MISC_H
26 
27 #include "argv.h"
28 #include "basic.h"
29 #include "common.h"
30 #include "env_set.h"
31 #include "integer.h"
32 #include "buffer.h"
33 #include "platform.h"
34 
35 /* forward declarations */
36 struct plugin_list;
37 
38 
39 /* Set standard file descriptors to /dev/null */
40 void set_std_files_to_null(bool stdin_only);
41 
42 /* Make arrays of strings */
43 
44 const char **make_arg_array(const char *first, const char *parms, struct gc_arena *gc);
45 
46 const char **make_extended_arg_array(char **p, bool is_inline,
47  struct gc_arena *gc);
48 
49 /* prepend a random prefix to hostname */
50 const char *hostname_randomize(const char *hostname, struct gc_arena *gc);
51 
52 /*
53  * Get and store a username/password
54  */
55 
56 struct user_pass
57 {
58  bool defined;
59  /* For auth-token username and token can be set individually, so we
60  * use this second bool to track if the token (password) is defined */
62  bool nocache;
63  bool protected;
64 
65 /* max length of username/password */
66 #ifdef ENABLE_PKCS11
67 #define USER_PASS_LEN 4096
68 #else
69 #define USER_PASS_LEN 128
70 #endif
71  /* Note that username and password are expected to be null-terminated */
74 };
75 
76 #ifdef ENABLE_MANAGEMENT
77 /*
78  * Challenge response info on client as pushed by server.
79  */
81 #define CR_ECHO (1<<0) /* echo response when typed by user */
82 #define CR_RESPONSE (1<<1) /* response needed */
83  unsigned int flags;
84 
85  const char *user;
86  const char *state_id;
87  const char *challenge_text;
88 };
89 
90 /*
91  * Challenge response info on client as pushed by server.
92  */
94 #define SC_ECHO (1<<0) /* echo response when typed by user */
95 #define SC_CONCAT (1<<1) /* concatenate password and response and do not base64 encode */
96  unsigned int flags;
97 
98  const char *challenge_text;
99 };
100 
101 #else /* ifdef ENABLE_MANAGEMENT */
102 struct auth_challenge_info {};
103 struct static_challenge_info {};
104 #endif /* ifdef ENABLE_MANAGEMENT */
105 
106 /*
107  * Flags for get_user_pass and management_query_user_pass
108  */
109 #define GET_USER_PASS_MANAGEMENT (1<<0)
110 /* GET_USER_PASS_SENSITIVE (1<<1) not used anymore */
111 #define GET_USER_PASS_PASSWORD_ONLY (1<<2)
112 #define GET_USER_PASS_NEED_OK (1<<3)
113 #define GET_USER_PASS_NOFATAL (1<<4)
114 #define GET_USER_PASS_NEED_STR (1<<5)
115 #define GET_USER_PASS_PREVIOUS_CREDS_FAILED (1<<6)
116 
117 #define GET_USER_PASS_DYNAMIC_CHALLENGE (1<<7) /* CRV1 protocol -- dynamic challenge */
118 #define GET_USER_PASS_STATIC_CHALLENGE (1<<8) /* SCRV1 protocol -- static challenge */
119 #define GET_USER_PASS_STATIC_CHALLENGE_ECHO (1<<9) /* SCRV1 protocol -- echo response */
120 
121 #define GET_USER_PASS_INLINE_CREDS (1<<10) /* indicates that auth_file is actually inline creds */
122 #define GET_USER_PASS_STATIC_CHALLENGE_CONCAT (1<<11) /* indicates password and response should be concatenated */
123 
134 bool get_user_pass_cr(struct user_pass *up,
135  const char *auth_file,
136  const char *prefix,
137  const unsigned int flags,
138  const char *auth_challenge);
139 
149 static inline bool
151  const char *auth_file,
152  const char *prefix,
153  const unsigned int flags)
154 {
155  return get_user_pass_cr(up, auth_file, prefix, flags, NULL);
156 }
157 
158 void purge_user_pass(struct user_pass *up, const bool force);
159 
169 void set_auth_token(struct user_pass *tk, const char *token);
170 
180 void set_auth_token_user(struct user_pass *tk, const char *username);
181 
182 /*
183  * Process string received by untrusted peer before
184  * printing to console or log file.
185  * Assumes that string has been null terminated.
186  */
187 const char *safe_print(const char *str, struct gc_arena *gc);
188 
189 const char *sanitize_control_message(const char *str, struct gc_arena *gc);
190 
191 /*
192  * /sbin/ip path, may be overridden
193  */
194 #ifdef ENABLE_IPROUTE
195 extern const char *iproute_path;
196 #endif
197 
198 /* helper to parse peer_info received from multi client, validate
199  * (this is untrusted data) and put into environment */
200 bool validate_peer_info_line(char *line);
201 
202 void output_peer_info_env(struct env_set *es, const char *peer_info);
203 
207 struct buffer
208 prepend_dir(const char *dir, const char *path, struct gc_arena *gc);
209 
213 void
214 protect_user_pass(struct user_pass *up);
215 
219 void
220 unprotect_user_pass(struct user_pass *up);
221 
222 
223 #define _STRINGIFY(S) #S
224 /* *INDENT-OFF* - uncrustify need to ignore this macro */
225 #define MAC_FMT _STRINGIFY(%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx)
226 /* *INDENT-ON* */
227 #define MAC_PRINT_ARG(_mac) _mac[0], _mac[1], _mac[2], \
228  _mac[3], _mac[4], _mac[5]
229 #define MAC_SCAN_ARG(_mac) &_mac[0], &_mac[1], &_mac[2], \
230  &_mac[3], &_mac[4], &_mac[5]
231 
232 #endif /* ifndef MISC_H */
sanitize_control_message
const char * sanitize_control_message(const char *str, struct gc_arena *gc)
Definition: misc.c:670
prepend_dir
struct buffer prepend_dir(const char *dir, const char *path, struct gc_arena *gc)
Prepend a directory to a path.
Definition: misc.c:793
unprotect_user_pass
void unprotect_user_pass(struct user_pass *up)
Decrypt username and password buffers in user_pass.
Definition: misc.c:824
es
struct env_set * es
Definition: test_pkcs11.c:141
user_pass::username
char username[USER_PASS_LEN]
Definition: misc.h:72
set_auth_token_user
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:530
user_pass::defined
bool defined
Definition: misc.h:58
make_extended_arg_array
const char ** make_extended_arg_array(char **p, bool is_inline, struct gc_arena *gc)
Definition: misc.c:640
output_peer_info_env
void output_peer_info_env(struct env_set *es, const char *peer_info)
Definition: misc.c:771
protect_user_pass
void protect_user_pass(struct user_pass *up)
Encrypt username and password buffers in user_pass.
Definition: misc.c:804
get_user_pass_cr
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:211
auth_challenge_info
Definition: misc.h:80
argv.h
hostname_randomize
const char * hostname_randomize(const char *hostname, struct gc_arena *gc)
Definition: misc.c:82
env_set.h
auth_challenge_info::challenge_text
const char * challenge_text
Definition: misc.h:87
auth_challenge
static char * auth_challenge
Definition: ssl.c:285
make_arg_array
const char ** make_arg_array(const char *first, const char *parms, struct gc_arena *gc)
Definition: misc.c:563
purge_user_pass
void purge_user_pass(struct user_pass *up, const bool force)
Definition: misc.c:485
static_challenge_info::flags
unsigned int flags
Definition: misc.h:96
get_user_pass
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition: misc.h:150
user_pass::nocache
bool nocache
Definition: misc.h:62
static_challenge_info::challenge_text
const char * challenge_text
Definition: misc.h:98
buffer
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
static_challenge_info
Definition: misc.h:93
auth_challenge_info::state_id
const char * state_id
Definition: misc.h:86
buffer.h
platform.h
gc_arena
Garbage collection arena used to keep track of dynamically allocated memory.
Definition: buffer.h:116
env_set
Definition: env_set.h:42
auth_challenge_info::user
const char * user
Definition: misc.h:85
plugin_list
Definition: plugin.h:94
common.h
basic.h
user_pass::token_defined
bool token_defined
Definition: misc.h:61
safe_print
const char * safe_print(const char *str, struct gc_arena *gc)
Definition: misc.c:557
USER_PASS_LEN
#define USER_PASS_LEN
Definition: misc.h:69
user_pass::password
char password[USER_PASS_LEN]
Definition: misc.h:73
validate_peer_info_line
bool validate_peer_info_line(char *line)
Definition: misc.c:733
auth_challenge_info::flags
unsigned int flags
Definition: misc.h:83
user_pass
Definition: misc.h:56
integer.h
set_std_files_to_null
void set_std_files_to_null(bool stdin_only)
Definition: misc.c:56
gc
struct gc_arena gc
Definition: test_ssl.c:155
set_auth_token
void set_auth_token(struct user_pass *tk, const char *token)
Sets the auth-token to token.
Definition: misc.c:510