From: "cron2 (Code Review)" Date: Tue, 7 Oct 2025 20:12:18 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="fqIn/R+vi5U="; charset=UTF-8 Subject: [Openvpn-devel] [XL] Change in openvpn[master]: options: Factor out parsing code to separate options_parse.c List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: plaisthos , openvpn-devel --fqIn/R+vi5U= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/12= 42?usp=3Demail ) Change subject: options: Factor out parsing code to separate options_parse.c ...................................................................... options: Factor out parsing code to separate options_parse.c For easier testability. And because everything that reduces the length of that file in a sensible manner is a good idea. Change-Id: I18e38862df1318740928c6cfa21dc4dcd7d44b89 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1242 Message-Id: <20251007185110.19267-1-gert@...1296...> URL: https://sourceforge.net/p/openvpn/mailman/message/59243506/ Signed-off-by: Gert Doering --- M CMakeLists.txt M src/openvpn/Makefile.am M src/openvpn/common.h M src/openvpn/options.c M src/openvpn/options.h A src/openvpn/options_parse.c 6 files changed, 669 insertions(+), 622 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f6196f..be66357 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -513,6 +513,7 @@ src/openvpn/options.h src/openvpn/options_util.c src/openvpn/options_util.h + src/openvpn/options_parse.c src/openvpn/otime.c src/openvpn/otime.h src/openvpn/ovpn_dco_win.h diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index 1247f11..e44fb2b 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -109,6 +109,7 @@ openvpn.c openvpn.h \ options.c options.h \ options_util.c options_util.h \ + options_parse.c \ otime.c otime.h \ packet_id.c packet_id.h \ perf.c perf.h \ diff --git a/src/openvpn/common.h b/src/openvpn/common.h index c0947da..7779614 100644 --- a/src/openvpn/common.h +++ b/src/openvpn/common.h @@ -23,6 +23,8 @@ #ifndef COMMON_H #define COMMON_H +#include + /* * Statistics counters and associated printf format. */ diff --git a/src/openvpn/options.c b/src/openvpn/options.c index f35738d..f2e6dec 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -4838,7 +4838,7 @@ /* * Print the help message. */ -static void +void usage(void) { FILE *fp =3D msg_fp(0); @@ -4967,581 +4967,6 @@ } #endif -static inline bool -space(char c) -{ - return c =3D=3D '\0' || isspace(c); -} - -int -parse_line(const char *line, char *p[], const int n, const char *file, con= st int line_num, - msglvl_t msglevel, struct gc_arena *gc) -{ - const int STATE_INITIAL =3D 0; - const int STATE_READING_QUOTED_PARM =3D 1; - const int STATE_READING_UNQUOTED_PARM =3D 2; - const int STATE_DONE =3D 3; - const int STATE_READING_SQUOTED_PARM =3D 4; - - const char *error_prefix =3D ""; - - int ret =3D 0; - const char *c =3D line; - int state =3D STATE_INITIAL; - bool backslash =3D false; - char in, out; - - char parm[OPTION_PARM_SIZE]; - unsigned int parm_len =3D 0; - - msglevel &=3D ~M_OPTERR; - - if (msglevel & M_MSG_VIRT_OUT) - { - error_prefix =3D "ERROR: "; - } - - do - { - in =3D *c; - out =3D 0; - - if (!backslash && in =3D=3D '\\' && state !=3D STATE_READING_SQUOT= ED_PARM) - { - backslash =3D true; - } - else - { - if (state =3D=3D STATE_INITIAL) - { - if (!space(in)) - { - if (in =3D=3D ';' || in =3D=3D '#') /* comment */ - { - break; - } - if (!backslash && in =3D=3D '\"') - { - state =3D STATE_READING_QUOTED_PARM; - } - else if (!backslash && in =3D=3D '\'') - { - state =3D STATE_READING_SQUOTED_PARM; - } - else - { - out =3D in; - state =3D STATE_READING_UNQUOTED_PARM; - } - } - } - else if (state =3D=3D STATE_READING_UNQUOTED_PARM) - { - if (!backslash && space(in)) - { - state =3D STATE_DONE; - } - else - { - out =3D in; - } - } - else if (state =3D=3D STATE_READING_QUOTED_PARM) - { - if (!backslash && in =3D=3D '\"') - { - state =3D STATE_DONE; - } - else - { - out =3D in; - } - } - else if (state =3D=3D STATE_READING_SQUOTED_PARM) - { - if (in =3D=3D '\'') - { - state =3D STATE_DONE; - } - else - { - out =3D in; - } - } - if (state =3D=3D STATE_DONE) - { - /* ASSERT (parm_len > 0); */ - p[ret] =3D gc_malloc(parm_len + 1, true, gc); - memcpy(p[ret], parm, parm_len); - p[ret][parm_len] =3D '\0'; - state =3D STATE_INITIAL; - parm_len =3D 0; - ++ret; - } - - if (backslash && out) - { - if (!(out =3D=3D '\\' || out =3D=3D '\"' || space(out))) - { -#ifdef ENABLE_SMALL - msg(msglevel, "%sOptions warning: Bad backslash ('\\')= usage in %s:%d", - error_prefix, file, line_num); -#else - msg(msglevel, - "%sOptions warning: Bad backslash ('\\') usage in = %s:%d: remember that backslashes are treated as shell-escapes and if you ne= ed to pass backslash characters as part of a Windows filename, you should u= se double backslashes such as \"c:\\\\" PACKAGE - "\\\\static.key\"", - error_prefix, file, line_num); -#endif - return 0; - } - } - backslash =3D false; - } - - /* store parameter character */ - if (out) - { - if (parm_len >=3D SIZE(parm)) - { - parm[SIZE(parm) - 1] =3D 0; - msg(msglevel, "%sOptions error: Parameter at %s:%d is too = long (%d chars max): %s", - error_prefix, file, line_num, (int)SIZE(parm), parm); - return 0; - } - parm[parm_len++] =3D out; - } - - /* avoid overflow if too many parms in one config file line */ - if (ret >=3D n) - { - break; - } - - } while (*c++ !=3D '\0'); - - if (state =3D=3D STATE_READING_QUOTED_PARM) - { - msg(msglevel, "%sOptions error: No closing quotation (\") in %s:%d= ", error_prefix, file, - line_num); - return 0; - } - if (state =3D=3D STATE_READING_SQUOTED_PARM) - { - msg(msglevel, "%sOptions error: No closing single quotation (\') i= n %s:%d", error_prefix, - file, line_num); - return 0; - } - if (state !=3D STATE_INITIAL) - { - msg(msglevel, "%sOptions error: Residual parse state (%d) in %s:%d= ", error_prefix, state, - file, line_num); - return 0; - } -#if 0 - { - int i; - for (i =3D 0; i < ret; ++i) - { - msg(M_INFO|M_NOPREFIX, "%s:%d ARG[%d] '%s'", file, line_num, i= , p[i]); - } - } -#endif - return ret; -} - -static void -bypass_doubledash(char **p) -{ - if (strlen(*p) >=3D 3 && !strncmp(*p, "--", 2)) - { - *p +=3D 2; - } -} - -struct in_src -{ -#define IS_TYPE_FP 1 -#define IS_TYPE_BUF 2 - int type; - union - { - FILE *fp; - struct buffer *multiline; - } u; -}; - -static bool -in_src_get(const struct in_src *is, char *line, const int size) -{ - if (is->type =3D=3D IS_TYPE_FP) - { - return BOOL_CAST(fgets(line, size, is->u.fp)); - } - else if (is->type =3D=3D IS_TYPE_BUF) - { - bool status =3D buf_parse(is->u.multiline, '\n', line, size); - if ((int)strlen(line) + 1 < size) - { - strcat(line, "\n"); - } - return status; - } - else - { - ASSERT(0); - return false; - } -} - -static char * -read_inline_file(struct in_src *is, const char *close_tag, int *num_lines,= struct gc_arena *gc) -{ - char line[OPTION_LINE_SIZE]; - struct buffer buf =3D alloc_buf(8 * OPTION_LINE_SIZE); - char *ret; - bool endtagfound =3D false; - - while (in_src_get(is, line, sizeof(line))) - { - (*num_lines)++; - char *line_ptr =3D line; - /* Remove leading spaces */ - while (isspace(*line_ptr)) - { - line_ptr++; - } - if (!strncmp(line_ptr, close_tag, strlen(close_tag))) - { - endtagfound =3D true; - break; - } - if (!buf_safe(&buf, strlen(line) + 1)) - { - /* Increase buffer size */ - struct buffer buf2 =3D alloc_buf(buf.capacity * 2); - ASSERT(buf_copy(&buf2, &buf)); - buf_clear(&buf); - free_buf(&buf); - buf =3D buf2; - } - buf_printf(&buf, "%s", line); - } - if (!endtagfound) - { - msg(M_FATAL, "ERROR: Endtag %s missing", close_tag); - } - ret =3D string_alloc(BSTR(&buf), gc); - buf_clear(&buf); - free_buf(&buf); - secure_memzero(line, sizeof(line)); - return ret; -} - -static int -check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) -{ - int num_inline_lines =3D 0; - - if (p[0] && !p[1]) - { - char *arg =3D p[0]; - if (arg[0] =3D=3D '<' && arg[strlen(arg) - 1] =3D=3D '>') - { - struct buffer close_tag; - - arg[strlen(arg) - 1] =3D '\0'; - p[0] =3D string_alloc(arg + 1, gc); - close_tag =3D alloc_buf(strlen(p[0]) + 4); - buf_printf(&close_tag, "", p[0]); - p[1] =3D read_inline_file(is, BSTR(&close_tag), &num_inline_li= nes, gc); - p[2] =3D NULL; - free_buf(&close_tag); - } - } - return num_inline_lines; -} - -static int -check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc) -{ - struct in_src is; - is.type =3D IS_TYPE_FP; - is.u.fp =3D fp; - return check_inline_file(&is, p, gc); -} - -static int -check_inline_file_via_buf(struct buffer *multiline, char *p[], struct gc_a= rena *gc) -{ - struct in_src is; - is.type =3D IS_TYPE_BUF; - is.u.multiline =3D multiline; - return check_inline_file(&is, p, gc); -} - -static void add_option(struct options *options, char *p[], bool is_inline,= const char *file, - int line, const int level, const msglvl_t msglevel, - const unsigned int permission_mask, unsigned int *o= ption_types_found, - struct env_set *es); - -static void remove_option(struct context *c, struct options *options, char= *p[], bool is_inline, - const char *file, int line, const msglvl_t msgle= vel, - const unsigned int permission_mask, unsigned int= *option_types_found, - struct env_set *es); - -static void update_option(struct context *c, struct options *options, char= *p[], bool is_inline, - const char *file, int line, const int level, con= st msglvl_t msglevel, - const unsigned int permission_mask, unsigned int= *option_types_found, - struct env_set *es, unsigned int *update_options= _found); - -static void -read_config_file(struct options *options, const char *file, int level, con= st char *top_file, - const int top_line, const msglvl_t msglevel, - const unsigned int permission_mask, unsigned int *option_= types_found, - struct env_set *es) -{ - const int max_recursive_levels =3D 10; - FILE *fp; - int line_num; - char line[OPTION_LINE_SIZE + 1]; - char *p[MAX_PARMS + 1]; - - ++level; - if (level <=3D max_recursive_levels) - { - if (streq(file, "stdin")) - { - fp =3D stdin; - } - else - { - fp =3D platform_fopen(file, "r"); - } - if (fp) - { - line_num =3D 0; - while (fgets(line, sizeof(line), fp)) - { - int offset =3D 0; - CLEAR(p); - ++line_num; - if (strlen(line) =3D=3D OPTION_LINE_SIZE) - { - msg(msglevel, - "In %s:%d: Maximum option line length (%d) exceede= d, line starts with %s", - file, line_num, OPTION_LINE_SIZE, line); - } - - /* Ignore UTF-8 BOM at start of stream */ - if (line_num =3D=3D 1 && strncmp(line, "\xEF\xBB\xBF", 3) = =3D=3D 0) - { - offset =3D 3; - } - if (parse_line(line + offset, p, SIZE(p) - 1, file, line_n= um, msglevel, - &options->gc)) - { - bypass_doubledash(&p[0]); - int lines_inline =3D check_inline_file_via_fp(fp, p, &= options->gc); - add_option(options, p, lines_inline, file, line_num, l= evel, msglevel, - permission_mask, option_types_found, es); - line_num +=3D lines_inline; - } - } - if (fp !=3D stdin) - { - fclose(fp); - } - } - else - { - msg(msglevel, "In %s:%d: Error opening configuration file: %s"= , top_file, top_line, - file); - } - } - else - { - msg(msglevel, - "In %s:%d: Maximum recursive include levels exceeded in includ= e attempt of file %s -- probably you have a configuration file that tries t= o include itself.", - top_file, top_line, file); - } - secure_memzero(line, sizeof(line)); - CLEAR(p); -} - -static void -read_config_string(const char *prefix, struct options *options, const char= *config, - const msglvl_t msglevel, const unsigned int permission_= mask, - unsigned int *option_types_found, struct env_set *es) -{ - char line[OPTION_LINE_SIZE]; - struct buffer multiline; - int line_num =3D 0; - - buf_set_read(&multiline, (uint8_t *)config, strlen(config)); - - while (buf_parse(&multiline, '\n', line, sizeof(line))) - { - char *p[MAX_PARMS + 1]; - CLEAR(p); - ++line_num; - if (parse_line(line, p, SIZE(p) - 1, prefix, line_num, msglevel, &= options->gc)) - { - bypass_doubledash(&p[0]); - int lines_inline =3D check_inline_file_via_buf(&multiline, p, = &options->gc); - add_option(options, p, lines_inline, prefix, line_num, 0, msgl= evel, permission_mask, - option_types_found, es); - line_num +=3D lines_inline; - } - CLEAR(p); - } - secure_memzero(line, sizeof(line)); -} - -void -parse_argv(struct options *options, const int argc, char *argv[], const ms= glvl_t msglevel, - const unsigned int permission_mask, unsigned int *option_types_= found, struct env_set *es) -{ - /* usage message */ - if (argc <=3D 1) - { - usage(); - } - - /* config filename specified only? */ - if (argc =3D=3D 2 && strncmp(argv[1], "--", 2)) - { - char *p[MAX_PARMS + 1]; - CLEAR(p); - p[0] =3D "config"; - p[1] =3D argv[1]; - add_option(options, p, false, NULL, 0, 0, msglevel, permission_mas= k, option_types_found, - es); - } - else - { - /* parse command line */ - for (int i =3D 1; i < argc; ++i) - { - char *p[MAX_PARMS + 1]; - CLEAR(p); - p[0] =3D argv[i]; - if (strncmp(p[0], "--", 2)) - { - msg(msglevel, - "I'm trying to parse \"%s\" as an --option parameter b= ut I don't see a leading '--'", - p[0]); - } - else - { - p[0] +=3D 2; - } - - int j; - for (j =3D 1; j < MAX_PARMS; ++j) - { - if (i + j < argc) - { - char *arg =3D argv[i + j]; - if (strncmp(arg, "--", 2)) - { - p[j] =3D arg; - } - else - { - break; - } - } - } - add_option(options, p, false, NULL, 0, 0, msglevel, permission= _mask, option_types_found, - es); - i +=3D j - 1; - } - } -} - -bool -apply_push_options(struct context *c, struct options *options, struct buff= er *buf, - unsigned int permission_mask, unsigned int *option_type= s_found, - struct env_set *es, bool is_update) -{ - char line[OPTION_PARM_SIZE]; - int line_num =3D 0; - const char *file =3D "[PUSH-OPTIONS]"; - const msglvl_t msglevel =3D D_PUSH_ERRORS | M_OPTERR; - unsigned int update_options_found =3D 0; - - while (buf_parse(buf, ',', line, sizeof(line))) - { - char *p[MAX_PARMS + 1]; - CLEAR(p); - ++line_num; - unsigned int push_update_option_flags =3D 0; - int i =3D 0; - - /* skip leading spaces matching the behaviour of parse_line */ - while (isspace(line[i])) - { - i++; - } - - /* If we are not in a 'PUSH_UPDATE' we just check `apply_pull_filt= er()` - * otherwise we must call `check_push_update_option_flags()` first - */ - if ((is_update && !check_push_update_option_flags(line, &i, &push_= update_option_flags)) - || !apply_pull_filter(options, &line[i])) - { - /* In case we are in a `PUSH_UPDATE` and `check_push_update_op= tion_flags()` - * or `apply_pull_filter()` fail but the option is flagged by = `PUSH_OPT_OPTIONAL`, - * instead of restarting, we just ignore the option and we pro= cess the next one - */ - if (push_update_option_flags & PUSH_OPT_OPTIONAL) - { - continue; /* Ignoring this option */ - } - return false; /* Cause push/pull error and stop push processin= g */ - } - - if (parse_line(&line[i], p, SIZE(p) - 1, file, line_num, msglevel,= &options->gc)) - { - if (!is_update) - { - add_option(options, p, false, file, line_num, 0, msglevel,= permission_mask, - option_types_found, es); - } - else if (push_update_option_flags & PUSH_OPT_TO_REMOVE) - { - remove_option(c, options, p, false, file, line_num, msglev= el, permission_mask, - option_types_found, es); - } - else - { - update_option(c, options, p, false, file, line_num, 0, msg= level, permission_mask, - option_types_found, es, &update_options_foun= d); - } - } - } - return true; -} - -void -options_server_import(struct options *o, const char *filename, msglvl_t ms= glevel, - unsigned int permission_mask, unsigned int *option_t= ypes_found, - struct env_set *es) -{ - msg(D_PUSH, "OPTIONS IMPORT: reading client specific options from: %s"= , filename); - read_config_file(o, filename, 0, filename, 0, msglevel, permission_mas= k, option_types_found, - es); -} - -void -options_string_import(struct options *options, const char *config, const m= sglvl_t msglevel, - const unsigned int permission_mask, unsigned int *op= tion_types_found, - struct env_set *es) -{ - read_config_string("[CONFIG-STRING]", options, config, msglevel, permi= ssion_mask, - option_types_found, es); -} - #define VERIFY_PERMISSION(mask) = \ { = \ if (!verify_permission(p[0], file, line, (mask), permission_mask, = option_types_found, \ @@ -5642,27 +5067,7 @@ option_ptr->flags =3D 0; \ } -/** - * @brief Resets options found in the PUSH_UPDATE message that are precede= d by the `-` flag. - * This function is used in push-updates to reset specified options. - * The number of parameters `p` must always be 1. If the permission= is verified, - * all related options are erased or reset to their default values. - * Upon successful permission verification (by VERIFY_PERMISSION()), - * `option_types_found` is filled with the flag corresponding to th= e option. - * - * @param c The context structure. - * @param options A pointer to the options structure. - * @param p An array of strings containing the options and their parameter= s. - * @param is_inline A boolean indicating if the option is inline. - * @param file The file where the function is called. - * @param line The line number where the function is called. - * @param msglevel The message level. - * @param permission_mask The permission mask used by VERIFY_PERMISSION(). - * @param option_types_found A pointer to the variable where the flags cor= responding to the options - * found are stored. - * @param es The environment set structure. - */ -static void +void remove_option(struct context *c, struct options *options, char *p[], bool = is_inline, const char *file, int line, const msglvl_t msglevel, const unsigned int permission_mask, unsigned int *option_typ= es_found, @@ -5982,30 +5387,7 @@ return true; } -/** - * @brief Processes an option to update. It first checks whether it has al= ready - * received an option of the same type within the same update messa= ge. - * If the option has already been received, it calls add_option(). - * Otherwise, it deletes all existing values related to that option= before calling - * add_option(). - * - * @param c The context structure. - * @param options A pointer to the options structure. - * @param p An array of strings containing the options and their parameter= s. - * @param is_inline A boolean indicating if the option is inline. - * @param file The file where the function is called. - * @param line The line number where the function is called. - * @param level The level of the option. - * @param msglevel The message level for logging. - * @param permission_mask The permission mask used by VERIFY_PERMISSION(). - * @param option_types_found A pointer to the variable where the flags cor= responding to the options - * found are stored. - * @param es The environment set structure. - * @param update_options_found A pointer to the variable where the flags c= orresponding to the update - * options found are stored, used to check if an option of the same type h= as already been processed - * by update_option() within the same push-update message. - */ -static void +void update_option(struct context *c, struct options *options, char *p[], bool = is_inline, const char *file, int line, const int level, const msglvl_t = msglevel, const unsigned int permission_mask, unsigned int *option_typ= es_found, @@ -6190,7 +5572,7 @@ return ret; } -static void +void add_option(struct options *options, char *p[], bool is_inline, const char = *file, int line, const int level, const msglvl_t msglevel, const unsigned int pe= rmission_mask, unsigned int *option_types_found, struct env_set *es) diff --git a/src/openvpn/options.h b/src/openvpn/options.h index b033068..f038975 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -822,14 +822,83 @@ struct pull_filter *tail; }; +void add_option(struct options *options, char *p[], bool is_inline, const = char *file, + int line, const int level, const msglvl_t msglevel, + const unsigned int permission_mask, unsigned int *option_t= ypes_found, + struct env_set *es); + +/** + * @brief Resets options found in the PUSH_UPDATE message that are precede= d by the `-` flag. + * This function is used in push-updates to reset specified options. + * The number of parameters `p` must always be 1. If the permission= is verified, + * all related options are erased or reset to their default values. + * Upon successful permission verification (by VERIFY_PERMISSION()), + * `option_types_found` is filled with the flag corresponding to th= e option. + * + * @param c The context structure. + * @param options A pointer to the options structure. + * @param p An array of strings containing the options and their parameter= s. + * @param is_inline A boolean indicating if the option is inline. + * @param file The file where the function is called. + * @param line The line number where the function is called. + * @param msglevel The message level. + * @param permission_mask The permission mask used by VERIFY_PERMISSION(). + * @param option_types_found A pointer to the variable where the flags cor= responding to the options + * found are stored. + * @param es The environment set structure. + */ +void remove_option(struct context *c, struct options *options, char *p[], = bool is_inline, + const char *file, int line, const msglvl_t msglevel, + const unsigned int permission_mask, unsigned int *optio= n_types_found, + struct env_set *es); + +/** + * @brief Processes an option to update. It first checks whether it has al= ready + * received an option of the same type within the same update messa= ge. + * If the option has already been received, it calls add_option(). + * Otherwise, it deletes all existing values related to that option= before calling + * add_option(). + * + * @param c The context structure. + * @param options A pointer to the options structure. + * @param p An array of strings containing the options and their parameter= s. + * @param is_inline A boolean indicating if the option is inline. + * @param file The file where the function is called. + * @param line The line number where the function is called. + * @param level The level of the option. + * @param msglevel The message level for logging. + * @param permission_mask The permission mask used by VERIFY_PERMISSION(). + * @param option_types_found A pointer to the variable where the flags cor= responding to the options + * found are stored. + * @param es The environment set structure. + * @param update_options_found A pointer to the variable where the flags c= orresponding to the update + * options found are stored, used to check if an option of the same type h= as already been processed + * by update_option() within the same push-update message. + */ +void update_option(struct context *c, struct options *options, char *p[], = bool is_inline, + const char *file, int line, const int level, const msgl= vl_t msglevel, + const unsigned int permission_mask, unsigned int *optio= n_types_found, + struct env_set *es, unsigned int *update_options_found); + void parse_argv(struct options *options, const int argc, char *argv[], con= st msglvl_t msglevel, const unsigned int permission_mask, unsigned int *option_t= ypes_found, struct env_set *es); +void read_config_file(struct options *options, const char *file, int level= , const char *top_file, + const int top_line, const msglvl_t msglevel, + const unsigned int permission_mask, unsigned int *op= tion_types_found, + struct env_set *es); + +void read_config_string(const char *prefix, struct options *options, const= char *config, + const msglvl_t msglevel, const unsigned int permis= sion_mask, + unsigned int *option_types_found, struct env_set *= es); + void notnull(const char *arg, const char *description); void usage_small(void); +void usage(void); + void show_library_versions(const unsigned int flags); #ifdef _WIN32 diff --git a/src/openvpn/options_parse.c b/src/openvpn/options_parse.c new file mode 100644 index 0000000..bb5b404 --- /dev/null +++ b/src/openvpn/options_parse.c @@ -0,0 +1,592 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2025 OpenVPN Inc + * Copyright (C) 2008-2025 David Sommerseth + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "options.h" +#include "options_util.h" +#include "push.h" + +static void +bypass_doubledash(char **p) +{ + if (strlen(*p) >=3D 3 && !strncmp(*p, "--", 2)) + { + *p +=3D 2; + } +} + +static inline bool +space(char c) +{ + return c =3D=3D '\0' || isspace(c); +} + +int +parse_line(const char *line, char *p[], const int n, const char *file, con= st int line_num, + msglvl_t msglevel, struct gc_arena *gc) +{ + const int STATE_INITIAL =3D 0; + const int STATE_READING_QUOTED_PARM =3D 1; + const int STATE_READING_UNQUOTED_PARM =3D 2; + const int STATE_DONE =3D 3; + const int STATE_READING_SQUOTED_PARM =3D 4; + + const char *error_prefix =3D ""; + + int ret =3D 0; + const char *c =3D line; + int state =3D STATE_INITIAL; + bool backslash =3D false; + char in, out; + + char parm[OPTION_PARM_SIZE]; + unsigned int parm_len =3D 0; + + msglevel &=3D ~M_OPTERR; + + if (msglevel & M_MSG_VIRT_OUT) + { + error_prefix =3D "ERROR: "; + } + + do + { + in =3D *c; + out =3D 0; + + if (!backslash && in =3D=3D '\\' && state !=3D STATE_READING_SQUOT= ED_PARM) + { + backslash =3D true; + } + else + { + if (state =3D=3D STATE_INITIAL) + { + if (!space(in)) + { + if (in =3D=3D ';' || in =3D=3D '#') /* comment */ + { + break; + } + if (!backslash && in =3D=3D '\"') + { + state =3D STATE_READING_QUOTED_PARM; + } + else if (!backslash && in =3D=3D '\'') + { + state =3D STATE_READING_SQUOTED_PARM; + } + else + { + out =3D in; + state =3D STATE_READING_UNQUOTED_PARM; + } + } + } + else if (state =3D=3D STATE_READING_UNQUOTED_PARM) + { + if (!backslash && space(in)) + { + state =3D STATE_DONE; + } + else + { + out =3D in; + } + } + else if (state =3D=3D STATE_READING_QUOTED_PARM) + { + if (!backslash && in =3D=3D '\"') + { + state =3D STATE_DONE; + } + else + { + out =3D in; + } + } + else if (state =3D=3D STATE_READING_SQUOTED_PARM) + { + if (in =3D=3D '\'') + { + state =3D STATE_DONE; + } + else + { + out =3D in; + } + } + if (state =3D=3D STATE_DONE) + { + /* ASSERT (parm_len > 0); */ + p[ret] =3D gc_malloc(parm_len + 1, true, gc); + memcpy(p[ret], parm, parm_len); + p[ret][parm_len] =3D '\0'; + state =3D STATE_INITIAL; + parm_len =3D 0; + ++ret; + } + + if (backslash && out) + { + if (!(out =3D=3D '\\' || out =3D=3D '\"' || space(out))) + { +#ifdef ENABLE_SMALL + msg(msglevel, "%sOptions warning: Bad backslash ('\\')= usage in %s:%d", + error_prefix, file, line_num); +#else + msg(msglevel, + "%sOptions warning: Bad backslash ('\\') usage in = %s:%d: remember that backslashes are treated as shell-escapes and if you ne= ed to pass backslash characters as part of a Windows filename, you should u= se double backslashes such as \"c:\\\\" PACKAGE + "\\\\static.key\"", + error_prefix, file, line_num); +#endif + return 0; + } + } + backslash =3D false; + } + + /* store parameter character */ + if (out) + { + if (parm_len >=3D SIZE(parm)) + { + parm[SIZE(parm) - 1] =3D 0; + msg(msglevel, "%sOptions error: Parameter at %s:%d is too = long (%d chars max): %s", + error_prefix, file, line_num, (int)SIZE(parm), parm); + return 0; + } + parm[parm_len++] =3D out; + } + + /* avoid overflow if too many parms in one config file line */ + if (ret >=3D n) + { + break; + } + + } while (*c++ !=3D '\0'); + + if (state =3D=3D STATE_READING_QUOTED_PARM) + { + msg(msglevel, "%sOptions error: No closing quotation (\") in %s:%d= ", error_prefix, file, + line_num); + return 0; + } + if (state =3D=3D STATE_READING_SQUOTED_PARM) + { + msg(msglevel, "%sOptions error: No closing single quotation (\') i= n %s:%d", error_prefix, + file, line_num); + return 0; + } + if (state !=3D STATE_INITIAL) + { + msg(msglevel, "%sOptions error: Residual parse state (%d) in %s:%d= ", error_prefix, state, + file, line_num); + return 0; + } +#if 0 + { + int i; + for (i =3D 0; i < ret; ++i) + { + msg(M_INFO|M_NOPREFIX, "%s:%d ARG[%d] '%s'", file, line_num, i= , p[i]); + } + } +#endif + return ret; +} + +struct in_src +{ +#define IS_TYPE_FP 1 +#define IS_TYPE_BUF 2 + int type; + union + { + FILE *fp; + struct buffer *multiline; + } u; +}; + +static bool +in_src_get(const struct in_src *is, char *line, const int size) +{ + if (is->type =3D=3D IS_TYPE_FP) + { + return BOOL_CAST(fgets(line, size, is->u.fp)); + } + else if (is->type =3D=3D IS_TYPE_BUF) + { + bool status =3D buf_parse(is->u.multiline, '\n', line, size); + if ((int)strlen(line) + 1 < size) + { + strcat(line, "\n"); + } + return status; + } + else + { + ASSERT(0); + return false; + } +} + +static char * +read_inline_file(struct in_src *is, const char *close_tag, int *num_lines,= struct gc_arena *gc) +{ + char line[OPTION_LINE_SIZE]; + struct buffer buf =3D alloc_buf(8 * OPTION_LINE_SIZE); + char *ret; + bool endtagfound =3D false; + + while (in_src_get(is, line, sizeof(line))) + { + (*num_lines)++; + char *line_ptr =3D line; + /* Remove leading spaces */ + while (isspace(*line_ptr)) + { + line_ptr++; + } + if (!strncmp(line_ptr, close_tag, strlen(close_tag))) + { + endtagfound =3D true; + break; + } + if (!buf_safe(&buf, strlen(line) + 1)) + { + /* Increase buffer size */ + struct buffer buf2 =3D alloc_buf(buf.capacity * 2); + ASSERT(buf_copy(&buf2, &buf)); + buf_clear(&buf); + free_buf(&buf); + buf =3D buf2; + } + buf_printf(&buf, "%s", line); + } + if (!endtagfound) + { + msg(M_FATAL, "ERROR: Endtag %s missing", close_tag); + } + ret =3D string_alloc(BSTR(&buf), gc); + buf_clear(&buf); + free_buf(&buf); + secure_memzero(line, sizeof(line)); + return ret; +} + +static int +check_inline_file(struct in_src *is, char *p[], struct gc_arena *gc) +{ + int num_inline_lines =3D 0; + + if (p[0] && !p[1]) + { + char *arg =3D p[0]; + if (arg[0] =3D=3D '<' && arg[strlen(arg) - 1] =3D=3D '>') + { + struct buffer close_tag; + + arg[strlen(arg) - 1] =3D '\0'; + p[0] =3D string_alloc(arg + 1, gc); + close_tag =3D alloc_buf(strlen(p[0]) + 4); + buf_printf(&close_tag, "", p[0]); + p[1] =3D read_inline_file(is, BSTR(&close_tag), &num_inline_li= nes, gc); + p[2] =3D NULL; + free_buf(&close_tag); + } + } + return num_inline_lines; +} + +static int +check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc) +{ + struct in_src is; + is.type =3D IS_TYPE_FP; + is.u.fp =3D fp; + return check_inline_file(&is, p, gc); +} + +static int +check_inline_file_via_buf(struct buffer *multiline, char *p[], struct gc_a= rena *gc) +{ + struct in_src is; + is.type =3D IS_TYPE_BUF; + is.u.multiline =3D multiline; + return check_inline_file(&is, p, gc); +} + +void +read_config_file(struct options *options, const char *file, int level, con= st char *top_file, + const int top_line, const msglvl_t msglevel, + const unsigned int permission_mask, unsigned int *option_= types_found, + struct env_set *es) +{ + const int max_recursive_levels =3D 10; + FILE *fp; + int line_num; + char line[OPTION_LINE_SIZE + 1]; + char *p[MAX_PARMS + 1]; + + ++level; + if (level <=3D max_recursive_levels) + { + if (streq(file, "stdin")) + { + fp =3D stdin; + } + else + { + fp =3D platform_fopen(file, "r"); + } + if (fp) + { + line_num =3D 0; + while (fgets(line, sizeof(line), fp)) + { + int offset =3D 0; + CLEAR(p); + ++line_num; + if (strlen(line) =3D=3D OPTION_LINE_SIZE) + { + msg(msglevel, + "In %s:%d: Maximum option line length (%d) exceede= d, line starts with %s", + file, line_num, OPTION_LINE_SIZE, line); + } + + /* Ignore UTF-8 BOM at start of stream */ + if (line_num =3D=3D 1 && strncmp(line, "\xEF\xBB\xBF", 3) = =3D=3D 0) + { + offset =3D 3; + } + if (parse_line(line + offset, p, SIZE(p) - 1, file, line_n= um, msglevel, + &options->gc)) + { + bypass_doubledash(&p[0]); + int lines_inline =3D check_inline_file_via_fp(fp, p, &= options->gc); + add_option(options, p, lines_inline, file, line_num, l= evel, msglevel, + permission_mask, option_types_found, es); + line_num +=3D lines_inline; + } + } + if (fp !=3D stdin) + { + fclose(fp); + } + } + else + { + msg(msglevel, "In %s:%d: Error opening configuration file: %s"= , top_file, top_line, + file); + } + } + else + { + msg(msglevel, + "In %s:%d: Maximum recursive include levels exceeded in includ= e attempt of file %s -- probably you have a configuration file that tries t= o include itself.", + top_file, top_line, file); + } + secure_memzero(line, sizeof(line)); + CLEAR(p); +} + +void +read_config_string(const char *prefix, struct options *options, const char= *config, + const msglvl_t msglevel, const unsigned int permission_= mask, + unsigned int *option_types_found, struct env_set *es) +{ + char line[OPTION_LINE_SIZE]; + struct buffer multiline; + int line_num =3D 0; + + buf_set_read(&multiline, (uint8_t *)config, strlen(config)); + + while (buf_parse(&multiline, '\n', line, sizeof(line))) + { + char *p[MAX_PARMS + 1]; + CLEAR(p); + ++line_num; + if (parse_line(line, p, SIZE(p) - 1, prefix, line_num, msglevel, &= options->gc)) + { + bypass_doubledash(&p[0]); + int lines_inline =3D check_inline_file_via_buf(&multiline, p, = &options->gc); + add_option(options, p, lines_inline, prefix, line_num, 0, msgl= evel, permission_mask, + option_types_found, es); + line_num +=3D lines_inline; + } + CLEAR(p); + } + secure_memzero(line, sizeof(line)); +} + +void +parse_argv(struct options *options, const int argc, char *argv[], const ms= glvl_t msglevel, + const unsigned int permission_mask, unsigned int *option_types_= found, struct env_set *es) +{ + /* usage message */ + if (argc <=3D 1) + { + usage(); + } + + /* config filename specified only? */ + if (argc =3D=3D 2 && strncmp(argv[1], "--", 2)) + { + char *p[MAX_PARMS + 1]; + CLEAR(p); + p[0] =3D "config"; + p[1] =3D argv[1]; + add_option(options, p, false, NULL, 0, 0, msglevel, permission_mas= k, option_types_found, + es); + } + else + { + /* parse command line */ + for (int i =3D 1; i < argc; ++i) + { + char *p[MAX_PARMS + 1]; + CLEAR(p); + p[0] =3D argv[i]; + if (strncmp(p[0], "--", 2)) + { + msg(msglevel, + "I'm trying to parse \"%s\" as an --option parameter b= ut I don't see a leading '--'", + p[0]); + } + else + { + p[0] +=3D 2; + } + + int j; + for (j =3D 1; j < MAX_PARMS; ++j) + { + if (i + j < argc) + { + char *arg =3D argv[i + j]; + if (strncmp(arg, "--", 2)) + { + p[j] =3D arg; + } + else + { + break; + } + } + } + add_option(options, p, false, NULL, 0, 0, msglevel, permission= _mask, option_types_found, + es); + i +=3D j - 1; + } + } +} + +bool +apply_push_options(struct context *c, struct options *options, struct buff= er *buf, + unsigned int permission_mask, unsigned int *option_type= s_found, + struct env_set *es, bool is_update) +{ + char line[OPTION_PARM_SIZE]; + int line_num =3D 0; + const char *file =3D "[PUSH-OPTIONS]"; + const msglvl_t msglevel =3D D_PUSH_ERRORS | M_OPTERR; + unsigned int update_options_found =3D 0; + + while (buf_parse(buf, ',', line, sizeof(line))) + { + char *p[MAX_PARMS + 1]; + CLEAR(p); + ++line_num; + unsigned int push_update_option_flags =3D 0; + int i =3D 0; + + /* skip leading spaces matching the behaviour of parse_line */ + while (isspace(line[i])) + { + i++; + } + + /* If we are not in a 'PUSH_UPDATE' we just check `apply_pull_filt= er()` + * otherwise we must call `check_push_update_option_flags()` first + */ + if ((is_update && !check_push_update_option_flags(line, &i, &push_= update_option_flags)) + || !apply_pull_filter(options, &line[i])) + { + /* In case we are in a `PUSH_UPDATE` and `check_push_update_op= tion_flags()` + * or `apply_pull_filter()` fail but the option is flagged by = `PUSH_OPT_OPTIONAL`, + * instead of restarting, we just ignore the option and we pro= cess the next one + */ + if (push_update_option_flags & PUSH_OPT_OPTIONAL) + { + continue; /* Ignoring this option */ + } + return false; /* Cause push/pull error and stop push processin= g */ + } + + if (parse_line(&line[i], p, SIZE(p) - 1, file, line_num, msglevel,= &options->gc)) + { + if (!is_update) + { + add_option(options, p, false, file, line_num, 0, msglevel,= permission_mask, + option_types_found, es); + } + else if (push_update_option_flags & PUSH_OPT_TO_REMOVE) + { + remove_option(c, options, p, false, file, line_num, msglev= el, permission_mask, + option_types_found, es); + } + else + { + update_option(c, options, p, false, file, line_num, 0, msg= level, permission_mask, + option_types_found, es, &update_options_foun= d); + } + } + } + return true; +} + +void +options_server_import(struct options *o, const char *filename, msglvl_t ms= glevel, + unsigned int permission_mask, unsigned int *option_t= ypes_found, + struct env_set *es) +{ + msg(D_PUSH, "OPTIONS IMPORT: reading client specific options from: %s"= , filename); + read_config_file(o, filename, 0, filename, 0, msglevel, permission_mas= k, option_types_found, + es); +} + +void +options_string_import(struct options *options, const char *config, const m= sglvl_t msglevel, + const unsigned int permission_mask, unsigned int *op= tion_types_found, + struct env_set *es) +{ + read_config_string("[CONFIG-STRING]", options, config, msglevel, permi= ssion_mask, + option_types_found, es); +} -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1242?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: merged Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I18e38862df1318740928c6cfa21dc4dcd7d44b89 Gerrit-Change-Number: 1242 Gerrit-PatchSet: 2 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --fqIn/R+vi5U= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 sub= mitted this change.

View Change

options: Factor out parsing code to separate opt=
ions_parse.c

For easier testability. And because everything that
= reduces the length of that file in a sensible manner
is a good idea.
=
Change-Id: I18e38862df1318740928c6cfa21dc4dcd7d44b89
Signed-off-by: = Frank Lichtenheld <frank@...2641...>
Acked-by: Gert Doering <ge= rt@...1296...>
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/124= 2
Message-Id: <20251007185110.19267-1-gert@...1296...>
URL: htt= ps://sourceforge.net/p/openvpn/mailman/message/59243506/
Signed-off-by: = Gert Doering <gert@...1296...>
---
M CMakeLists.txt
M src/op= envpn/Makefile.am
M src/openvpn/common.h
M src/openvpn/options.c
M= src/openvpn/options.h
A src/openvpn/options_parse.c
6 files changed,= 669 insertions(+), 622 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3f619= 6f..be66357 100644
--- a/CMakeLists.txt
+++= b/CMakeLists.txt
@@ -513,6 +513,7 @@
= src/openvpn/options.h
src/openvpn/options_util.c
src/openvpn/options_util.h
+ src/openvpn/options_parse.c
= src/openvpn/otime.c
src/openvpn/otime.h
src/openvpn/ovpn_dco_win.h

diff --git a/src/openvpn= /Makefile.am b/src/openvpn/Makefile.am
index 1247f11..e44fb= 2b 100644
--- a/src/openvpn/Makefile.am
+++= b/src/openvpn/Makefile.am
@@ -109,6 +109,7 @@
openvpn.c openvpn.h \
options.c options.h \<= br> options_util.c options_util.h \
+ options_parse.c \
otime.c otime.h= \
packet_id.c packet_id.h \
perf.c per= f.h \
diff --git a/src/openvpn/common.h b/src/openvpn/commo= n.h
index c0947da..7779614 100644
--- a/src= /openvpn/common.h
+++ b/src/openvpn/common.h
@@ -23,6 +23,8 @@
#ifndef COMMON_H
#def= ine COMMON_H

+#include <stdint.h>
+
/*
* Statistics counte= rs and associated printf format.
*/
diff = --git a/src/openvpn/options.c b/src/openvpn/options.c
index= f35738d..f2e6dec 100644
--- a/src/openvpn/options.c=
+++ b/src/openvpn/options.c
@@ -4838,7 +4838,7 @@=
/*
* Print the help message.
= */
-static void=
+void
usage(void)
{
FILE *fp =3D msg_fp(= 0);
@@ -4967,581 +4967,6 @@
}
#endif

-static inline bool
-space(char c)
-{
- return c =3D=3D = 9;\0' || isspace(c);
-
-int
-parse_line(const char *line, char *p[], const int n, con= st char *file, const int line_num,
- msglvl_t msglevel, struct gc_arena *gc)
<= span style=3D"color: hsl(0, 100%, 40%);">-{

- const int STATE_INITIAL =3D 0;
- const int STATE_READING_QUOTED_PARM = =3D 1;
- const int S= TATE_READING_UNQUOTED_PARM =3D 2;
- const int STATE_DONE =3D 3;
- const int STATE_READING_SQUOTED_PARM =3D 4;
-
- const char *error_prefix =3D "";
-
- int ret =3D 0;
- const char *c =3D line;
- int state =3D STATE_INITIAL;<= br>- bool backslash =3D false;=
- char in, out;

-
- char parm[OPTION_PARM_SIZE];
- unsigned int parm_len =3D 0;
-
- msglevel &=3D ~M_OPTERR;
= -
- if (msglevel & M_MSG_VIRT_OUT)
- {
- error_prefix =3D "ERROR: ";=
- }
-
- do
- = {
- in =3D *c;
- out =3D 0;

-
- if (!backslash && in =3D=3D = 9;\\' && state !=3D STATE_READING_SQUOTED_PARM)
- {
- backslash =3D true;
- }
- else
- {
- {
- if (!space(in))
- {
- if (in =3D=3D ';' || in = =3D=3D '#') /* comment */
- {
- break;
- }
- if (!backslash && in =3D= =3D '\"')
= - {
= - state =3D STATE_READING_QUOTED_PARM;
- }
- else if (!backs= lash && in =3D=3D '\'')
- {
- state =3D STATE_READING_SQUOT= ED_PARM;
- = }
- = else
- = {
- = out =3D in;
- state =3D STATE_READING_UNQUOTED_PARM;- }- }
- }
- else if (state =3D=3D STATE_RE= ADING_UNQUOTED_PARM)
- = {
- = if (!backslash && space(in))
- {
- state =3D STATE_DONE;
- }
- else
- {
- out =3D in;
- }
- }
- else if (state =3D=3D STATE_READING_= QUOTED_PARM)
- = {
- i= f (!backslash && in =3D=3D '\"')
- {
- state =3D STATE_DONE;

- }- else
= - {
- out =3D in;
- }- }
- else if (state =3D=3D STAT= E_READING_SQUOTED_PARM)
- {
- = if (in =3D=3D '\'')
- {
- state =3D STATE_DONE;
- }
- else
- {
- out =3D in;
- }
- }
- if (state =3D=3D STATE_DONE)- {
- /* ASSERT (parm_len &g= t; 0); */
- = p[ret] =3D gc_malloc(parm_len + 1, true, gc);
- memcpy(p[ret], parm, parm_len);=
- p[ret= ][parm_len] =3D '\0';
- state =3D STATE_INITIAL;
- parm_len =3D 0;
- ++ret;
- }
-
- if (backslash && out)
- {
- if (!(out =3D=3D '\\' || ou= t =3D=3D '\"' || space(out)))
- {
-#ifdef ENABLE_SMALL
- msg(msglevel, "%sOptions warning: B= ad backslash ('\\') usage in %s:%d",
- error_prefix, file, lin= e_num);
-#else- msg(msglev= el,
- = "%sOptions warning: Bad backslash ('\\') usage in %s:%d:= remember that backslashes are treated as shell-escapes and if you need to = pass backslash characters as part of a Windows filename, you should use dou= ble backslashes such as \"c:\\\\" PACKAGE
- "\\\\static.ke= y\"",
- = error_prefix, file, line_num);
-#endif
- return 0;
- }
- }
- backslash =3D false;
- }
-
- /* sto= re parameter character */
- if (out)
- = {
- i= f (parm_len >=3D SIZE(parm))
- {
- parm[SIZE(parm) - 1] =3D 0;
- msg(msglevel, "%sOptions error= : Parameter at %s:%d is too long (%d chars max): %s",
- error_prefix, file= , line_num, (int)SIZE(parm), parm);
- return 0;
- }
- parm[parm_len++] =3D out;
- }
-
- = /* avoid overflow if too many parms in one config file line */
<= span style=3D"color: hsl(0, 100%, 40%);">- if (ret >=3D n)=
- {
- break;
- }
-
- = } while (*c++ !=3D '\0');
-
- if= (state =3D=3D STATE_READING_QUOTED_PARM)
- {
- msg(msglevel, "%sOptions error: No closing quotation (\&qu= ot;) in %s:%d", error_prefix, file,
- line_num);
- return 0;
- }
-= if (state =3D=3D STATE_READING_SQUOTED_PARM)
- {
- msg(msglevel, "%sOptions error: No closing single q= uotation (\') in %s:%d", error_prefix,
- file, line_num);
- return 0;
- }
- if (state !=3D STATE_INITIAL)
- {
- msg(msglevel, "%sOptions error: Residual parse state = (%d) in %s:%d", error_prefix, state,
- file, line_num);
- return 0;
- }
-#if 0
- {

- int i;
= - for (i =3D 0; i < ret= ; ++i)
- {

- msg(M_INFO|M_NO= PREFIX, "%s:%d ARG[%d] '%s'", file, line_num, i, p[i]);
- }
- }

-#endif
- return ret;
-
-static void
-bypass_doubledash(char **p)
-{
- if (strlen(*p) >=3D 3 && !strncmp(*p, "--&= quot;, 2))
- {
- *p +=3D 2;- }
-}
-
-struct in_sr= c
-{
-#define IS_TYPE_FP 1
-#define IS_TYPE_BUF 2
- int type;
- union
- {
- = FILE *fp;
- = struct buffer *multiline;
- } u;
-};

-
-static bool
-in_src_get(const struct in_src *is, char *line, const in= t size)
-{
- if (is->type =3D=3D IS_TYPE= _FP)
- {
<= span style=3D"color: hsl(0, 100%, 40%);">- return BOOL_CAST(fgets(li= ne, size, is->u.fp));
- else if (= is->type =3D=3D IS_TYPE_BUF)
- {
- = bool status =3D buf_parse(is->u.multiline, '\n', line, size);<= /span>
- if ((int)strle= n(line) + 1 < size)
= - {
- = strcat(line, "\n");
- }
- = return status;
- = }
- else- {
- ASSERT(0);
- return false;
- }
-}
-
= -static char *
-read_inline_file(struct in_src *is, cons= t char *close_tag, int *num_lines, struct gc_arena *gc)
-{
- char line[OPTION_LINE_SIZE];
- struct buffer buf =3D alloc_buf(8 * OPTION_LIN= E_SIZE);
- char *ret= ;
- bool endtagfound= =3D false;
-- while (in_src_get(is, line,= sizeof(line)))
- {<= /span>
- (*num_lines)++= ;
- char *line_p= tr =3D line;
- /= * Remove leading spaces */
- while (isspace(*line_ptr))
- {
- line_ptr++;
- }
- = if (!strncmp(line_ptr, close_tag, strlen(close_tag)))
- {
- endtagfound =3D true;
- break;
- }
- if (!buf_safe(&buf, strlen(line) + 1))
- {
- /* Increase buffer size *= /

- struct b= uffer buf2 =3D alloc_buf(buf.capacity * 2);
- ASSERT(buf_copy(&buf2, &buf));

- buf_clear(&a= mp;buf);
- f= ree_buf(&buf);
- = buf =3D buf2;
= - }
- buf= _printf(&buf, "%s", line);
- }
- msg(M= _FATAL, "ERROR: Endtag %s missing", close_tag);
- }
- ret =3D string_alloc(BSTR(&buf), gc);- buf_clear(&buf);=
- free_buf(&buf);
- secure_memzero(line, s= izeof(line));
- retu= rn ret;
-}
-

-static int
-check_inline_file(struct in_src *is, char *p[], struct gc_arena *g= c)
-{
- int num_inline_lines =3D 0;<= br>-
- if (p[0] && !p[1])
- {
- char *arg =3D p[0];
- if (arg[0] =3D=3D '<' &&= arg[strlen(arg) - 1] =3D=3D '>')
- {
- struct buffer close_tag;
-
- arg[strlen(arg) - 1] =3D '\0';
- p[0] =3D string_alloc(arg + 1= , gc);
- clo= se_tag =3D alloc_buf(strlen(p[0]) + 4);
- buf_printf(&close_tag, "</%s>&= quot;, p[0]);
- = p[1] =3D read_inline_file(is, BSTR(&close_tag), &num_inline_lin= es, gc);
- p= [2] =3D NULL;
- = free_buf(&close_tag);
- }
- = }
- return num_inlin= e_lines;
-}
<= span style=3D"color: hsl(0, 100%, 40%);">-
-static int
-check_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc= )
-{
- struct in_src is;
- is.type =3D IS_TYPE_FP;
- is.u.fp =3D fp;
- return check_inline_file(&is= , p, gc);
-}
= -
-static int
-check_inline_file_via_buf(struct buffer *multiline, char *p[], s= truct gc_arena *gc)
-{<= /span>
- struct in_src is;<= /span>
- is.type =3D IS_TYP= E_BUF;
- is.u.multil= ine =3D multiline;
- = return check_inline_file(&is, p, gc);
-}
= -
-static void add_opti= on(struct options *options, char *p[], bool is_inline, const char *file,

- i= nt line, const int level, const msglvl_t msglevel,
- const unsigned int perm= ission_mask, unsigned int *option_types_found,
- struct env_set *es);=
-
-static void remove_option(struct context *c, stru= ct options *options, char *p[], bool is_inline,
- const char *file, int l= ine, const msglvl_t msglevel,
- const unsigned int permission_mask, unsig= ned int *option_types_found,
- struct env_set *es);
-
-static void update_option(struct context *c, struct options *opt= ions, char *p[], bool is_inline,
- const char *file, int line, const int = level, const msglvl_t msglevel,
- const unsigned int permission_mask, uns= igned int *option_types_found,
- struct env_set *es, unsigned int *update= _options_found);
-
-static void
-read_config_file(struct options *opt= ions, const char *file, int level, const char *top_file,

- const int top_line, con= st msglvl_t msglevel,
-= const unsigned int permission_mask, unsigned int *option_t= ypes_found,
- = struct env_set *es)
-{
- const int m= ax_recursive_levels =3D 10;
- FILE *fp;
- = int line_num;
- ch= ar line[OPTION_LINE_SIZE + 1];
- char *p[MAX_PARMS + 1];
-
- ++= level;
- if (level &= lt;=3D max_recursive_levels)
- {
- i= f (streq(file, "stdin"))
- {
= - fp =3D stdin;
- }
- = else
- {=
- fp =3D platform_= fopen(file, "r");
- }
- = if (fp)
- {
- line_num =3D= 0;
- while = (fgets(line, sizeof(line), fp))
- {
- int offset =3D 0;
- CLEAR(p);
- ++line_num;
- if (strlen(line) =3D=3D OPTION_LINE= _SIZE)
- = {
- = msg(msglevel,
- = "In %s:%d: Maximum option line length (%d) exceeded, = line starts with %s",
- file, line_num, OPTION_LINE_SIZE, line);
- }-
- /* Ignore UTF-8 BOM at start of stre= am */
- = if (line_num =3D=3D 1 && strncmp(line, "\xEF\xBB\xBF", 3)= =3D=3D 0)
- = {
- = offset =3D 3;
- = }
- = if (parse_line(line + offset, p, SIZE(p) - 1, file, line_num, msgl= evel,
- = &options->gc))
- {
- bypass_doubledash(&p[0]);
- int lines_inline= =3D check_inline_file_via_fp(fp, p, &options->gc);

- add_option(options= , p, lines_inline, file, line_num, level, msglevel,
- permission_m= ask, option_types_found, es);
- line_num +=3D lines_inline;
- }
- }
- if (fp !=3D stdin)
- {
- fclose(fp);
- }
- }
- else
- = msg(msglevel, "In %s:%d: Error opening configuration file: %s"= , top_file, top_line,
-= file);
= - }
- }
- else
- {
- msg(msglevel,
- "In %s:%d: Maximum recursive include le= vels exceeded in include attempt of file %s -- probably you have a configur= ation file that tries to include itself.",
- top_file, top_line, file);
= - }
- secure_memzero(line, sizeof(line));- CLEAR(p);
-}
-
-stat= ic void
-read_config_st= ring(const char *prefix, struct options *options, const char *config,
- const ms= glvl_t msglevel, const unsigned int permission_mask,
- unsigned int *option_typ= es_found, struct env_set *es)
-{
- char lin= e[OPTION_LINE_SIZE];
- = struct buffer multiline;
- int line_num =3D 0;
-
- buf_set_r= ead(&multiline, (uint8_t *)config, strlen(config));
-
- while (buf_parse(&multiline, '\n', line, sizeo= f(line)))
- {=
- char *p[MAX_PARMS + = 1];
- CLEAR(p);<= /span>
- ++line_num;
- if (parse_line(l= ine, p, SIZE(p) - 1, prefix, line_num, msglevel, &options->gc))
- {
- bypass_doubledash(&p[0= ]);
- int li= nes_inline =3D check_inline_file_via_buf(&multiline, p, &options-&g= t;gc);
- add= _option(options, p, lines_inline, prefix, line_num, 0, msglevel, permission= _mask,
- = option_types_found, es);
- line_num +=3D lines_inline;
- }
- CLEAR(p);
- }
- = secure_memzero(line, sizeof(line));
-}
-
-void
-parse_argv(struct options *options, const i= nt argc, char *argv[], const msglvl_t msglevel,
- const unsigned int permission_mask, un= signed int *option_types_found, struct env_set *es)
-{
- /* usage message */
- if (argc <=3D 1)
- {
-= usage();
- }=
-
- /* config filename specified only? */<= /span>
- if (argc =3D=3D 2 = && strncmp(argv[1], "--", 2))
- {
- char *p[MAX_PARMS + 1];
- CLEAR(p);
- p[0] =3D "config";
- p[1] =3D argv[1];
- add_option(options, p, false, N= ULL, 0, 0, msglevel, permission_mask, option_types_found,
- es);
- }

- else
- {
- = /* parse command line */
- for (int i =3D 1; i < argc; ++i)
- {
- char *p[MAX_PARMS + 1];
- CLEAR(p);
- p[0] =3D argv[i];
- if (strncmp(p[0], "--&= quot;, 2))
- = {
- msg= (msglevel,
- = "I'm trying to parse \"%s\" as an --option para= meter but I don't see a leading '--'",
- p[0]);
- }

- else
- {
- p[0] +=3D 2;
- }
-
- = int j;
- = for (j =3D 1; j < MAX_PARMS; ++j)
- {
- if (i + j < argc)
- {
- char *arg =3D argv[i + j];
- if (st= rncmp(arg, "--", 2))
- {
- p[j] =3D arg;
- }
- else
- {
- break;
- }
- }
- }
- add_option(options, p, false, NULL, 0,= 0, msglevel, permission_mask, option_types_found,
- es);
- i +=3D j - 1;
- }

- }
-}
--bool
-apply_push_options(struct context *c, struct op= tions *options, struct buffer *buf,
- unsigned int permission_mask, unsigned int= *option_types_found,
-= struct env_set *es, bool is_update)
-{
- char line[OPTION_PARM_SIZE];
- int line_num =3D 0;
- const char *file =3D "[PUSH-OPTIONS]&quo= t;;
- const msglvl_t= msglevel =3D D_PUSH_ERRORS | M_OPTERR;
- unsigned int update_options_found =3D 0;
-

- while (buf_parse(buf, ',', line, sizeof(lin= e)))
- {
<= span style=3D"color: hsl(0, 100%, 40%);">- char *p[MAX_PARMS + 1];
- CLEAR(p);
- ++line_num;<= br>- unsigned int push_upd= ate_option_flags =3D 0;
- int i =3D 0;
= -
- /* skip lead= ing spaces matching the behaviour of parse_line */
- while (isspace(line[i]))
- {

- i++;
- }
-
- /*= If we are not in a 'PUSH_UPDATE' we just check `apply_pull_filter(= )`
- * otherwis= e we must call `check_push_update_option_flags()` first
- */
- if ((is_update && !check_push_updat= e_option_flags(line, &i, &push_update_option_flags))
- || !apply_pull_filter(op= tions, &line[i]))

-= {
- = /* In case we are in a `PUSH_UPDATE` and `check_push_update_option_flags()`=
- * or `ap= ply_pull_filter()` fail but the option is flagged by `PUSH_OPT_OPTIONAL`,
- * instead = of restarting, we just ignore the option and we process the next one=
- */
- if (push_update_option= _flags & PUSH_OPT_OPTIONAL)

- {
- continue; /* Ignoring this option */
- }
- return false; /* Cause push/pull error= and stop push processing */
- }
-
- if (parse_line(&am= p;line[i], p, SIZE(p) - 1, file, line_num, msglevel, &options->gc))<= /span>
- {
- if (!is_update)
=
- {
- add_option(options, = p, false, file, line_num, 0, msglevel, permission_mask,

- option_types_f= ound, es);
- = }
- else if= (push_update_option_flags & PUSH_OPT_TO_REMOVE)
- {
- remove_option(c, options, p, false,= file, line_num, msglevel, permission_mask,
- option_types_found, es)= ;
- }=
- else
<= span style=3D"color: hsl(0, 100%, 40%);">- {

- update_option(c, options,= p, false, file, line_num, 0, msglevel, permission_mask,
- option_typ= es_found, es, &update_options_found);
- }
- }
-= }
- return true;=
-}
-
-void
-opti= ons_server_import(struct options *o, const char *filename, msglvl_t msgleve= l,
- = unsigned int permission_mask, unsigned int *option_types_found,- struct e= nv_set *es)
-{- msg(D_PUSH, "OPTIONS = IMPORT: reading client specific options from: %s", filename);- read_config_file(o, filena= me, 0, filename, 0, msglevel, permission_mask, option_types_found,- es);
-}
-
-void
-options_s= tring_import(struct options *options, const char *config, const msglvl_t ms= glevel,
- = const unsigned int permission_mask, unsigned int *option_types_foun= d,
- = struct env_set *es)
= -{
- read_config_str= ing("[CONFIG-STRING]", options, config, msglevel, permission_mask= ,
- = option_types_found, es);
-}
-
#define VERIFY_PERMISSION(mask) = \

{ = \
if (!verify_permission(p[0], file, line, (mask), permission_ma= sk, option_types_found, \

@@ -5642,27 +5067,7 @@
= option_ptr->flags =3D 0; \
}


-/**
- * @brief Res= ets options found in the PUSH_UPDATE message that are preceded by the `-` f= lag.
- * This fu= nction is used in push-updates to reset specified options.
- * The number of parameters `p`= must always be 1. If the permission is verified,
- * all related options are erased or res= et to their default values.
- * Upon successful permission verification (by VERIFY_PERMISSI= ON()),
- * `opti= on_types_found` is filled with the flag corresponding to the option.=
- *
- * @param c The context structure.
- * @param options A pointer to the= options structure.

- *= @param p An array of strings containing the options and their parameters.<= /span>
- * @param is_inline A = boolean indicating if the option is inline.
- * @param file The file where the function is called.=
- * @param line The li= ne number where the function is called.
- * @param msglevel The message level.
- * @param permission_mask The permission = mask used by VERIFY_PERMISSION().
- * @param option_types_found A pointer to the variable where th= e flags corresponding to the options
- * found are stored.
- * @param es The environment set structure.
- */
-static void
+void
remove_option(struct context *c, struct opt= ions *options, char *p[], bool is_inline,
co= nst char *file, int line, const msglvl_t msglevel,
= const unsigned int permission_mask, unsigned int *option_types_found= ,
@@ -5982,30 +5387,7 @@
return true;<= /span>
}

-/**
- * = @brief Processes an option to update. It first checks whether it has alread= y
- * received a= n option of the same type within the same update message.
- * If the option has already bee= n received, it calls add_option().
- * Otherwise, it deletes all existing values related to= that option before calling
- * add_option().
= - *
- * @param c The co= ntext structure.
- * @p= aram options A pointer to the options structure.
- * @param p An array of strings containing the o= ptions and their parameters.
- * @param is_inline A boolean indicating if the option is inline.
- * @param file The file = where the function is called.
- * @param line The line number where the function is called.=
- * @param level The level of= the option.
- * @param= msglevel The message level for logging.
- * @param permission_mask The permission mask used by VE= RIFY_PERMISSION().
- * = @param option_types_found A pointer to the variable where the flags corresp= onding to the options
-= * found are stored.
- = * @param es The environment set structure.
- * @param update_options_found A pointer to the variab= le where the flags corresponding to the update
- * options found are stored, used to check if an o= ption of the same type has already been processed
- * by update_option() within the same push-upda= te message.
- */=
-static void
+void
update_option(= struct context *c, struct options *options, char *p[], bool is_inline,
const char *file, int line, const int level, con= st msglvl_t msglevel,
const unsigned int per= mission_mask, unsigned int *option_types_found,
@@ -6190,7 = +5572,7 @@
return ret;
}

-static void
+void
ad= d_option(struct options *options, char *p[], bool is_inline, const char *fi= le, int line,
const int level, const msglvl_t m= sglevel, const unsigned int permission_mask,
un= signed int *option_types_found, struct env_set *es)
diff --= git a/src/openvpn/options.h b/src/openvpn/options.h
index b= 033068..f038975 100644
--- a/src/openvpn/options.h+++ b/src/openvpn/options.h
@@ -822,14 +822,83 @@
struct pull_filter *tail;
};
+void add_op= tion(struct options *options, char *p[], bool is_inline, const char *file,<= /span>
+ int = line, const int level, const msglvl_t msglevel,
+ const unsigned int permission_m= ask, unsigned int *option_types_found,
+ struct env_set *es);
+
+/**
= + * @brief Resets options found in the PUSH_UPDATE message that are precede= d by the `-` flag.
+ = * This function is used in push-updates to reset specified options.<= /span>
+ * The number= of parameters `p` must always be 1. If the permission is verified,<= br>+ * all related optio= ns are erased or reset to their default values.
+ * Upon successful permission verificati= on (by VERIFY_PERMISSION()),
+ * `option_types_found` is filled with the flag correspondi= ng to the option.
+ *=
+ * @param c The con= text structure.
+ * @= param options A pointer to the options structure.
+ * @param p An array of strings containing th= e options and their parameters.
+ * @param is_inline A boolean indicating if the option is inlin= e.
+ * @param file Th= e file where the function is called.
+ * @param line The line number where the function is calle= d.
+ * @param msgleve= l The message level.
= + * @param permission_mask The permission mask used by VERIFY_PERMISSION().=
+ * @param option_ty= pes_found A pointer to the variable where the flags corresponding to the op= tions
+ * found are s= tored.
+ * @param es = The environment set structure.
+ */
+void = remove_option(struct context *c, struct options *options, char *p[], bool i= s_inline,
+ = const char *file, int line, const msglvl_t msglevel,
+ const unsigned= int permission_mask, unsigned int *option_types_found,

+ struct env_set *es);=
+
+/**
+ * @brief Processes an option to update. It first chec= ks whether it has already
+ * received an option of the same type within the same update = message.
+ * I= f the option has already been received, it calls add_option().
+ * Otherwise, it deletes = all existing values related to that option before calling

+ * add_option().
+ *
+ * @param c The context structure.
+ * @param options A pointer to the option= s structure.
+ * @par= am p An array of strings containing the options and their parameters.
+ * @param is_inline A boo= lean indicating if the option is inline.
+ * @param file The file where the function is called.<= /span>
+ * @param line The l= ine number where the function is called.
+ * @param level The level of the option.
+ * @param msglevel The message le= vel for logging.

+ * = @param permission_mask The permission mask used by VERIFY_PERMISSION().
+ * @param option_types_= found A pointer to the variable where the flags corresponding to the option= s
+ * found are store= d.
+ * @param es The = environment set structure.
+ * @param update_options_found A pointer to the variable where the f= lags corresponding to the update
+ * options found are stored, used to check if an option of the= same type has already been processed
+ * by update_option() within the same push-update message= .
+ */
+void update_option(struct context = *c, struct options *options, char *p[], bool is_inline,

+ const char *file, in= t line, const int level, const msglvl_t msglevel,
+ const unsigned int permiss= ion_mask, unsigned int *option_types_found,
+ struct env_set *es, unsigned int= *update_options_found);
+
void parse_argv(struct options *options, const int a= rgc, char *argv[], const msglvl_t msglevel,
= const unsigned int permission_mask, unsigned int *option_types_found,
struct env_set *es);
=
+void read_config_file(stru= ct options *options, const char *file, int level, const char *top_file,
+ c= onst int top_line, const msglvl_t msglevel,
+ const unsigned int permission= _mask, unsigned int *option_types_found,
+ struct env_set *es);
<= span style=3D"color: hsl(120, 100%, 40%);">+

+void read_config_string(const char *prefix, struct= options *options, const char *config,
+ const msglvl_t msglevel, const u= nsigned int permission_mask,
+ unsigned int *option_types_found, struct e= nv_set *es);
+=
void notnull(const char *arg, const char *description);
void usage_small(void);
=
+void usage(void);+
void show_= library_versions(const unsigned int flags);

#ifdef _WIN32

diff --git a/src/openvpn/options_parse.c = b/src/openvpn/options_parse.c
new file mode 100644index 0000000..bb5b404
--- /dev/null
+++ b/src/openvpn/options_parse.c

@@ -0,0 +1,592 @@
+/*
+ * OpenVPN -- An application to securely= tunnel IP networks
+= * over a single UDP port, with support for SSL/TLS-based
+ * session au= thentication and key exchange,
+ * packet encryption, packet authentication, and
+ * packet c= ompression.
+ *
+ * Copyright (C) 2002-20= 25 OpenVPN Inc <sales@...515...>
+ * Copyright (C) 2008-2025 David Sommerseth <dazo@...1186...= .2108...>
+ *
+ * This program is free= software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Publ= ic License version 2
= + * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be u= seful,
+ * but WITHO= UT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PART= ICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Pub= lic License along
+ *= with this program; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#inc= lude <string.h>
+
+#include "op= tions.h"
+#inclu= de "options_util.h"
+#include "push.h"
+
+s= tatic void
+bypass_do= ubledash(char **p)
+{=
+ if (strlen(*p) = >=3D 3 && !strncmp(*p, "--", 2))
+ {
+ *p +=3D 2;
+ }
+}
+
+static inline bool

+space(char c)

+{
+ return c =3D=3D '\0' || isspace(c);+}
+
+int
+parse_l= ine(const char *line, char *p[], const int n, const char *file, const int l= ine_num,
+ = msglvl_t msglevel, struct gc_arena *gc)
+{
+ const int STATE_INITIAL =3D 0;
+ const int STATE_READING_QUOTED_PARM =3D 1;
<= span style=3D"color: hsl(120, 100%, 40%);">+ const int STATE_READING_UNQ= UOTED_PARM =3D 2;

+ = const int STATE_DONE =3D 3;
+ const int STATE_READING_SQUOTED_PARM =3D 4;
+
+ const char *error_prefix =3D "";+
+ int ret =3D 0;
+ const char *c =3D line;
+ int state =3D STATE_INITIAL;+ bool backslash =3D false= ;
+ char in, out;<= /span>
+
+ char parm[OPTION_PARM_SIZE];=
+ unsigned int parm_len = =3D 0;
+
+ msglevel &=3D ~M_OPTERR;=

+
+ if (msglevel & M_MSG_VIRT_OUT)=
+ {
+ error_prefix =3D "ER= ROR: ";

+ }
+
+ do
+ {
+ in =3D *c;
+ out =3D 0;
+
+ if (!= backslash && in =3D=3D '\\' && state !=3D STATE_REA= DING_SQUOTED_PARM)
+ = {
+ = backslash =3D true;
= + }
+ e= lse
+ {=
+ if (state =3D= =3D STATE_INITIAL)
+ = {
+ = if (!space(in))
+ {
+ = {
+ = break;
+ }
+ if (!backslash && in =3D=3D '\&quo= t;')
+ = {
+ = state =3D STATE_READING_QUOTED_PARM;
+ }
+ else if (!backslash= && in =3D=3D '\'')
+ {
+ state =3D STATE_READING_SQUOT= ED_PARM;
+ = }
+ = else
+ = {
+ = out =3D in;
+ state =3D STATE_READING_UNQUOTED_PARM= ;
+ = }
+ = }
+ }
+ else if (sta= te =3D=3D STATE_READING_UNQUOTED_PARM)
+ {
+ if (!backslash && space(in))+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ state =3D S= TATE_DONE;

+ = }
+ = else
+ = {
+ = out =3D in;
+= }
+ = }
+ = else if (state =3D=3D STATE_READING_QUOTED_PARM)
+ {
+ if (!backslash && in = =3D=3D '\"')
+ {
+ state =3D STATE_DONE;
+ }
+ else
+ {
+ out =3D in;
+ }
+ }
+ else if (state =3D=3D STATE_READING= _SQUOTED_PARM)
+ = {
+ = if (in =3D=3D '\'')
+ {
+ state =3D STATE_DONE;
+ }
+ else
+ {
+ out =3D in;+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ if (state =3D=3D STATE_DO= NE)
+ {
+ /* ASS= ERT (parm_len > 0); */
+ p[ret] =3D gc_malloc(parm_len + 1, true, gc);<= br>+ memcpy(p[re= t], parm, parm_len);
= + p[ret][parm_len] =3D '\0';
+ state =3D STATE_INITIAL;<= /span>
+ parm= _len =3D 0;
+ = ++ret;
+ = }
+
= + if (backslash &= ;& out)
+ = {
+ = if (!(out =3D=3D '\\' || out =3D=3D '\"' || space(ou= t)))
+ = {
+#ifdef ENABLE_SMA= LL
+ = msg(msglevel, "%sOptions warning: Bad backslash ('\\') usag= e in %s:%d",
+ = error_prefix, file, line_num);
+#else
+ msg(msglevel,
+ "%sOptions w= arning: Bad backslash ('\\') usage in %s:%d: remember that backslas= hes are treated as shell-escapes and if you need to pass backslash characte= rs as part of a Windows filename, you should use double backslashes such as= \"c:\\\\" PACKAGE
+ "\\\\static.key\"",<= br>+ err= or_prefix, file, line_num);
+#endif
+ = return 0;
+ }
+ = backslash =3D false;
+ }
+=
+ /* store pa= rameter character */
= + if (out)
+ = {
+ = if (parm_len >=3D SIZE(parm))
+ {
+ parm[SIZE(parm) - 1] =3D 0;
+ msg(msglevel, "%sOption= s error: Parameter at %s:%d is too long (%d chars max): %s",+ error_pre= fix, file, line_num, (int)SIZE(parm), parm);
+ return 0;
+ }
+ parm[parm_len++] =3D out;
+ }

+
+ /* avoid overflow if too many parms in one config fi= le line */
+ i= f (ret >=3D n)
+ = {
+ = break;
+ }
+
+ } while (*c++ !=3D '\0');
+
+ if (state =3D=3D STATE_READING_QUOTED= _PARM)
+ {<= br>+ msg(msglevel, "= ;%sOptions error: No closing quotation (\") in %s:%d", error_pref= ix, file,
+ = line_num);
+ = return 0;
+ }
+ if (state =3D=3D ST= ATE_READING_SQUOTED_PARM)
+ {
+ = msg(msglevel, "%sOptions error: No closing single quotation (\') i= n %s:%d", error_prefix,
+ file, line_num);
+ return 0;
+ }
+ if (state !=3D STATE_INITIAL)
+ {
+ msg(msglevel, "%sOptions error: Residual parse state (%d) in= %s:%d", error_prefix, state,
+ file, line_num);
+ return 0;
+ }
+#if 0
+ {<= /span>
+ int i;
+ for (i =3D 0; i &= lt; ret; ++i)
+ = {
+ msg(= M_INFO|M_NOPREFIX, "%s:%d ARG[%d] '%s'", file, line_num, = i, p[i]);
+ }<= /span>
+ }
+#endif

+ return ret;
+}
+
+struct in_src=
+{
+#define IS_TYPE_FP 1
+#define IS_TYPE_BUF 2

+ int type;

+ union
+ {
+ FILE *fp;
+ struct buffer *multiline;
+ } u;
+};
+
+static bool
<= span style=3D"color: hsl(120, 100%, 40%);">+in_src_get(const struct in_src = *is, char *line, const int size)

+{
+ i= f (is->type =3D=3D IS_TYPE_FP)
+ {
+= return BOOL_CAST(fgets(line, size, is->u.fp));
+ }
+ else if (is->type =3D=3D IS_TYPE_BUF)=
+ {
+ bool status =3D buf_parse(is->= ;u.multiline, '\n', line, size);
+ if ((int)strlen(line) + 1 < size)+ {
+ strcat(line, "\n"= );
+ }<= br>+ return status;
+ }
+ else
+ {
+ ASSERT(0);
+ return false;
+ }
+}=
+
+static char *
+read_inline_file(struct in_src *is, const ch= ar *close_tag, int *num_lines, struct gc_arena *gc)
+{
+ char line[OPTION_LINE_SIZE];
+ struct buffer buf =3D alloc_buf(8 * OPTION= _LINE_SIZE);
+ cha= r *ret;
+ bool end= tagfound =3D false;
+=
+ while (in_src_g= et(is, line, sizeof(line)))
+ {
+ = (*num_lines)++;
+ = char *line_ptr =3D line;
+ /* Remove leading spaces */
+ while (isspace(*line_ptr))
+ {

+ line_ptr++;
+ }
+ if (!strncmp(line_ptr, close_tag, strle= n(close_tag)))
+ = {
+ end= tagfound =3D true;
+ = break;
+ = }
+ if (= !buf_safe(&buf, strlen(line) + 1))
+ {
+ /* Increase buffer size */
+ struct buffer buf2 =3D alloc_buf(bu= f.capacity * 2);
+ = ASSERT(buf_copy(&buf2, &buf));
+ buf_clear(&buf);
+ free_buf(&buf);
+ buf =3D bu= f2;
+ }=
+ buf_printf(&bu= f, "%s", line);
+ }
+ if (= !endtagfound)
+ {<= /span>
+ msg(M_FATAL,= "ERROR: Endtag %s missing", close_tag);
+ }
+ ret =3D string_alloc(BSTR(&buf), gc);
+ buf_clear(&buf);
<= br>+ free_buf(&buf);
+ secure_memzero(line= , sizeof(line));
+ = return ret;
+}
+
+static int
+check_inline_file(struct in_src *is, char *p[], struc= t gc_arena *gc)
+{
+ int num_inline_lin= es =3D 0;
++ if (p[0] && !p[1]= )
+ {
+ char *arg =3D p[0];

+ if (arg[0] =3D= =3D '<' && arg[strlen(arg) - 1] =3D=3D '>')
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ struct buffer close= _tag;

+
+ arg[strlen(arg) - 1] = =3D '\0';

+ = p[0] =3D string_alloc(arg + 1, gc);
+ close_tag =3D alloc_buf(strlen(p[0]) = + 4);
+ bu= f_printf(&close_tag, "</%s>", p[0]);
+ p[1] =3D read_inline_file(= is, BSTR(&close_tag), &num_inline_lines, gc);
+ p[2] =3D NULL;
+ free_buf(&close_ta= g);

+ }=
+ }
+ return num_inline_lines;
<= span style=3D"color: hsl(120, 100%, 40%);">+}

+
+static int
+ch= eck_inline_file_via_fp(FILE *fp, char *p[], struct gc_arena *gc)
= +{
+ struct in_src is;
+ is.type =3D IS_TYPE_FP;
+ is.u.fp =3D fp;
+ return check_inline_file(&is, = p, gc);
+}
= +
+static int
+check_inline_file_via_buf(struct buffer *multiline, char *= p[], struct gc_arena *gc)
+{
+ struct i= n_src is;
+ is.typ= e =3D IS_TYPE_BUF;
+ = is.u.multiline =3D multiline;
+ return check_inline_file(&is, p, gc);
+}
+
+void
+read_config_f= ile(struct options *options, const char *file, int level, const char *top_f= ile,
+ = const int top_line, const msglvl_t msglevel,
+ const unsigned int permission_m= ask, unsigned int *option_types_found,
+ struct env_set *es)
+{
+ const int max_recursive_levels =3D 10;
+ FILE *fp;

+ int line_num;
+ char line[OPTION_LINE_SIZE + 1];
+ char *p[MAX_PARMS + = 1];
+
+ ++level;

+ if (level <=3D max_recursive_level= s)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (streq(file, "s= tdin"))

+ = {
+ fp = =3D stdin;
+ }=
+ else=
+ {
+ fp =3D platform_fopen(fi= le, "r");
+= }
+ if= (fp)
+ {
+ line_num =3D= 0;
+ whil= e (fgets(line, sizeof(line), fp))
+ {
+ int offset =3D 0;
+ CLEAR(p);
+ ++line_num;
+ if (strlen(line) =3D=3D= OPTION_LINE_SIZE)
+ = {
+ = msg(msglevel,
+ "In %s:%d: Maximum option line leng= th (%d) exceeded, line starts with %s",
+ file, line_num, OPTION_LIN= E_SIZE, line);
+ = }
+=
+ /* Ignore = UTF-8 BOM at start of stream */
+ if (line_num =3D=3D 1 && strncmp(line, = "\xEF\xBB\xBF", 3) =3D=3D 0)
+ {
+ offset =3D 3;
+ }
+ if (parse_line(line + offset, = p, SIZE(p) - 1, file, line_num, msglevel,
+ &options->gc))<= /span>
+ {
+ by= pass_doubledash(&p[0]);
+ int lines_inline =3D check_inline_file_via_fp(f= p, p, &options->gc);
+ add_option(options, p, lines_inline, file, line= _num, level, msglevel,
+ = line_num +=3D lines_inline;
+ }
+ }
= + if (fp !=3D stdin)
+ {
+ fclose(fp);
+ }
+ }
+ = else
+ {=
+ msg(msg= level, "In %s:%d: Error opening configuration file: %s", top_file= , top_line,
+ = file);
+ = }
+ }+ else
+ {
+ msg(msglevel,
+ "In %s:%d: Maximum recursive includ= e levels exceeded in include attempt of file %s -- probably you have a conf= iguration file that tries to include itself.",
+ top_file, top_line, file);
+ }
+ secure_memzero(line, sizeof(line= ));
+ CLEAR(p);
+}
+
+void
= +read_config_string(const char *prefix, struct options *options, const char= *config,
+ = const msglvl_t msglevel, const unsigned int permission_mask,
+ unsign= ed int *option_types_found, struct env_set *es)
+{
+ char line[OPTION_LINE_SIZE];
+ struct buffer multiline;
+ int line_num =3D 0;
+
+ buf_set_read(&multiline, (uint8_t *)config, strl= en(config));
+=
+ while (buf_parse(&= multiline, '\n', line, sizeof(line)))
+ {
+ char *p[MAX_PARMS + 1];
+ CLEAR(p);
+ ++line_num;
+ if (parse_line(line, p, SIZE(p) - 1, prefix, = line_num, msglevel, &options->gc))
+ {
+ bypass_doubledash(&p[0]);
+ int lines_inline =3D check_in= line_file_via_buf(&multiline, p, &options->gc);
+ add_option(options, p, l= ines_inline, prefix, line_num, 0, msglevel, permission_mask,
+ option_type= s_found, es);

+ = line_num +=3D lines_inline;
+ }
+ CLEAR(p);
+ }
+ secure_m= emzero(line, sizeof(line));
+}
++void
+parse_argv(struct options *options, const in= t argc, char *argv[], const msglvl_t msglevel,
+ const unsigned int permission_mask, u= nsigned int *option_types_found, struct env_set *es)
+{
+ /* usage message */
+ if (argc <=3D 1)
+ {
+ usage();
+ }
+=
+ /* config filename spe= cified only? */
+ = if (argc =3D=3D 2 && strncmp(argv[1], "--", 2))+ {
+ char *p[MAX_PARMS + 1];+ CLEAR(p);
<= span style=3D"color: hsl(120, 100%, 40%);">+ p[0] =3D "config&q= uot;;

+ p[1] = =3D argv[1];
+ = add_option(options, p, false, NULL, 0, 0, msglevel, permission_mask, optio= n_types_found,
+ = es);
+ = }
+ else=
+ {
+ /* parse command line */<= br>+ for (int i =3D 1; i= < argc; ++i)
+ = {
+ c= har *p[MAX_PARMS + 1];
+ p[0] =3D argv[i];
+ if (strncmp(p[0], "--", 2))
= + {
+ msg(msglevel,=
+ "= I'm trying to parse \"%s\" as an --option parameter but I don= 't see a leading '--'",
+ p[0]);
+ }
+ else
+ {
+ p[0] +=3D 2;
+ }
+
+ = int j;
+ = for (j =3D 1; j < MAX_PARMS; ++j)
+ {
+ if (i + j < argc)
+ {
+ char *arg =3D argv[i = + j];
+ = if (strncmp(arg, "--", 2))
+ {
+ p[j] =3D arg;
+ }

= + else
+ {
+ = break;
+ = }
+ = }
+ = }
+ add_op= tion(options, p, false, NULL, 0, 0, msglevel, permission_mask, option_types= _found,
+ = es);
+ = i +=3D j - 1;
+ }
+ }
+}
+
+bool
= +apply_push_options(struct context *c, struct options *options, struct buff= er *buf,
+ = unsigned int permission_mask, unsigned int *option_types_found,
+ stru= ct env_set *es, bool is_update)
+{
+ ch= ar line[OPTION_PARM_SIZE];
+ int line_num =3D 0;
+ const char *file =3D "[PUSH-OPTIONS]";
<= span style=3D"color: hsl(120, 100%, 40%);">+ const msglvl_t msglevel =3D= D_PUSH_ERRORS | M_OPTERR;

+ unsigned int update_options_found =3D 0;
+
+ while (buf_parse(buf, ',', line, sizeof(line)))<= /span>
+ {
+ char *p[MAX_PARMS + 1];
+ CLEAR(p);

+ ++line_num;
+ unsigned int pus= h_update_option_flags =3D 0;
+ int i =3D 0;
+
+ /= * skip leading spaces matching the behaviour of parse_line */
+ while (isspace(line[i]))<= /span>
+ {
= + i++;
+ }

+
+ /* If we are not in a 'PUSH_UPDATE' we just = check `apply_pull_filter()`
+ * otherwise we must call `check_push_update_option_flags()= ` first
+ */<= /span>
+ if ((is_upda= te && !check_push_update_option_flags(line, &i, &push_updat= e_option_flags))
+ = || !apply_pull_filter(options, &line[i]))
+ {
+ /* In case we are in a `PUSH_UPDATE` a= nd `check_push_update_option_flags()`
+ * or `apply_pull_filter()` fail but the opti= on is flagged by `PUSH_OPT_OPTIONAL`,
+ * instead of restarting, we just ignore the = option and we process the next one
+ */
+ if (push_update_option_flags & PUSH_OPT_OPTIONAL= )
+ {
+ continue= ; /* Ignoring this option */
+ }
+ }
+
+ if (parse_line(&line[i], p, S= IZE(p) - 1, file, line_num, msglevel, &options->gc))
+ {

+ if (!is_update)
+ {
+ add_option(options, p, fa= lse, file, line_num, 0, msglevel, permission_mask,
+ option_types_foun= d, es);
+ = }
+ else i= f (push_update_option_flags & PUSH_OPT_TO_REMOVE)
+ {
+ remove_option(c, options, p, fa= lse, file, line_num, msglevel, permission_mask,
+ option_types_foun= d, es);
+ = }
+ else
+ {<= br>+ update_opti= on(c, options, p, false, file, line_num, 0, msglevel, permission_mask,
+ = option_types_found, es, &update_options_found);
+ }
+ }
+ }
+ return true;
+}
+
= +void
+options_server_import(struct options *o, cons= t char *filename, msglvl_t msglevel,
+ unsigned int permission_mask, unsign= ed int *option_types_found,
+ struct env_set *es)
+{
+ msg(D_PUSH, "OPTIONS IMPORT: reading client specific o= ptions from: %s", filename);
+ read_config_file(o, filename, 0, filename, 0, msglevel, p= ermission_mask, option_types_found,
+ es);
+}
+
+void
= +options_string_import(struct o= ptions *options, const char *config, const msglvl_t msglevel,
+ const unsig= ned int permission_mask, unsigned int *option_types_found,

+ struct env_set= *es)
+{
+ read_config_string("[CO= NFIG-STRING]", options, config, msglevel, permission_mask,

<= span style=3D"color: hsl(120, 100%, 40%);">+ option_t= ypes_found, es);

+}

To view, visit change 1242. To unsubscribe, = or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I18e38862df1318740928c6cfa21= dc4dcd7d44b89
Gerrit-Change-Number: 1242
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --fqIn/R+vi5U=--