From: "cron2 (Code Review)" Date: Tue, 7 Oct 2025 20:12:15 +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="AKvBr/gqBjc="; 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 , plaisthos Cc: openvpn-devel --AKvBr/gqBjc= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#2) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1242?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 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(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/42/1242/2 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: newpatchset 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 --AKvBr/gqBjc= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #2 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2

options: Factor out parsing code to=
 separate options_parse.c

For easier testability. And because everyt= hing that
reduces the length of that file in a sensible manner
is a g= ood idea.

Change-Id: I18e38862df1318740928c6cfa21dc4dcd7d44b89
Si= gned-off-by: Frank Lichtenheld <frank@...2641...>
Acked-by: Gert D= oering <gert@...1296...>
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/
Si= gned-off-by: Gert Doering <gert@...1296...>
---
M CMakeLists.tx= t
M src/openvpn/Makefile.am
M src/openvpn/common.h
M src/openvpn/o= ptions.c
M src/openvpn/options.h
A src/openvpn/options_parse.c
6 f= iles changed, 669 insertions(+), 622 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/42/1242/2<=
/pre>
+    src/openvpn/options_parse.c
src/openvpn/otime.c
src/openvpn/otime.h src/openvpn/ovpn_dco_win.h
diff --git a/src/op= envpn/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 @@<= br> openvpn.c openvpn.h \
options.c options.h \
options_util.c options_util.h \
+ options_parse.c \
otime.c ot= ime.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 <stdint.h>
+
/*
* Statistics c= ounters 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 ms= g_fp(0);
@@ -4967,581 +4967,6 @@
}<= br> #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, const int line_num,
- msglvl_t msglevel, struct gc_arena *gc)
-{
- const int STATE_INITIAL =3D 0;
<= span style=3D"color: hsl(0, 100%, 40%);">- const int STATE_READING_QUOTE= D_PARM =3D 1;

- cons= t 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 "&q= uot;;
-
- int ret =3D 0;
- const char *c =3D line;
- int state =3D STATE_INITIAL;
- bool backslash =3D = false;
- char in, ou= t;
-
- char parm[OPTION_PARM_SIZE];<= br>- 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_SQUOTED_PARM)
<= span style=3D"color: hsl(0, 100%, 40%);">- {

- backslash =3D true;
<= span style=3D"color: hsl(0, 100%, 40%);">- }
- else
- {
- if (state =3D=3D STATE_INITIAL)
- {
- if (!space(in))
- {
- if (in =3D=3D ';= 9; || in =3D=3D '#') /* comment */
- {
- break;
- }
- if (!backslash &&am= p; in =3D=3D '\"')
- {
- state =3D STATE_READING_QUOTED_PARM;
- }
- else i= f (!backslash && in =3D=3D '\'')
- {
- state =3D STATE_REA= DING_SQUOTED_PARM;
- = }
- = else
-= {
-= out =3D in;
- state =3D STATE_READING_UNQUOTED_PARM= ;
- = }
- }
- }
= - else if (state =3D= =3D STATE_READING_UNQUOTED_PARM)
- {
- {
- state =3D STATE_DONE;
- }

- else=
- {
= - out =3D in;<= /span>
- }
- }
- else if (state =3D=3D = STATE_READING_QUOTED_PARM)

- {
- = if (!backslash && in =3D=3D '\"')<= br>- {
<= span style=3D"color: hsl(0, 100%, 40%);">- state =3D STA= TE_DONE;

- = }
- e= lse
- {<= /span>
- ou= t =3D in;
- = }
- }
- else if (sta= te =3D=3D STATE_READING_SQUOTED_PARM)
- {
- if (in =3D=3D '\'')
- {
- state =3D STATE_DONE;
- }
- else=
- {
= - out =3D in;<= /span>
- }
- }
- if (state =3D=3D STATE= _DONE)

- {
- /* ASSE= RT (parm_len > 0); */
- memcpy(p[ret], p= arm, parm_len);
- = p[ret][parm_len] =3D '\0';
- state =3D STATE_INITIAL;
<= span style=3D"color: hsl(0, 100%, 40%);">- parm_len =3D 0;
- ++ret;<= /span>
- }-
- if (backslash && out)- {
- if (!(out =3D=3D '\= \' || out =3D=3D '\"' || space(out)))
- {
-#ifdef ENABLE_SMALL
- msg(msglevel, "%sOption= s warning: Bad backslash ('\\') usage in %s:%d",
- error_prefi= x, file, line_num);

-#e= lse
- = msg(msglevel,
- = "%sOptions warning: Bad backslash ('\\') usa= ge 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 sh= ould use double backslashes such as \"c:\\\\" PACKAGE
<= span style=3D"color: hsl(0, 100%, 40%);">- "\\\= \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, "%s= Options 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;<= br>- }
-
- } while (*c++ !=3D '\0');
-
- if (state =3D=3D STATE_READING_QUOTED_PARM)
- {
- msg(msglevel, "%sOptions error: No closing q= uotation (\") 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 (\') in %s:%d", error_prefix,
<= span style=3D"color: hsl(0, 100%, 40%);">- file, line_num);
- return 0;- }
- if (state !=3D STATE_INITIAL)
- {

- msg(msglevel, "%sOptions error: Resid= ual parse state (%d) in %s:%d", error_prefix, state,
- file, line_num);
= - return 0;
- }

-#if 0
- {
- i= nt 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 && !st= rncmp(*p, "--", 2))
- {
- = *p +=3D 2;
- }
-}
-
-struct in_src
-{=
-#define IS_TYPE_FP 1=
-#define IS_TYPE_BUF 2=
- int type;<= br>- union
- {
- FILE *fp;
- struct buffer *multiline;
- } u;
-};
-
-static bool
-in_src_get(const struct in_src *is, c= har *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&= #39;, line, size);
- = if ((int)strlen(line) + 1 < size)
- {
- strcat(line, "\n");
- }
- return status;
- }
= - else
- {=
- ASSERT(0);- return false;- }
-}
-
-static char *=
-read_inline_file(stru= ct in_src *is, const char *close_tag, int *num_lines, struct gc_arena *gc)<= /span>
-{
- char line[OPTION_LINE_SIZE];- struct buffer buf =3D alloc= _buf(8 * OPTION_LINE_SIZE);
- char *ret;
- = bool endtagfound =3D false;
-
- while (i= n_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, str= len(line) + 1))

- = {
- /* Inc= rease buffer size */
- = struct buffer buf2 =3D alloc_buf(buf.capacity * 2);
- ASSERT(buf_copy(&b= uf2, &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(&b= uf), gc);

- buf_clea= r(&buf);
- free_= buf(&buf);
- sec= ure_memzero(line, sizeof(line));
- return ret;
-}
-
-static int

-check_inline_file(struct in_src *is, char *p[],= struct gc_arena *gc)
-= {
- int num_inline_l= ines =3D 0;
-- if (p[0] && !p[1])<= /span>
- {
- char *arg =3D p[0];
= - if (arg[0] =3D=3D '&= lt;' && arg[strlen(arg) - 1] =3D=3D '>')
<= span style=3D"color: hsl(0, 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);
- buf_printf(&close_ta= g, "</%s>", p[0]);

- p[1] =3D read_inline_file(is, BSTR(&close_tag)= , &num_inline_lines, gc);
- p[2] =3D NULL;
- free_buf(&close_tag);
- }
- }
= - return num_inline_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 che= ck_inline_file(&is, p, gc);
-}
--static int
-check_inline_file_via_buf(struct buffer *m= ultiline, char *p[], struct gc_arena *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,
- c= onst unsigned int permission_mask, unsigned int *option_types_found,=
- struc= t env_set *es);
-
-static void remove_option(s= truct context *c, struct options *options, char *p[], bool is_inline,
- c= onst char *file, int line, const msglvl_t msglevel,
- const unsigned in= t permission_mask, unsigned int *option_types_found,
- struct env_set *e= s);
-
-static void update_option(struct contex= t *c, struct options *options, char *p[], bool is_inline,
- const char *f= ile, int line, const int level, const msglvl_t msglevel,
- const unsigned= int permission_mask, unsigned int *option_types_found,
- struct env_set = *es, unsigned int *update_options_found);
-
-s= tatic 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_mas= k, 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];
<= span style=3D"color: hsl(0, 100%, 40%);">-

- ++level;
- if (level <=3D max_recursive_levels)
- {
- if (streq(file, "stdin"))
- {

- fp =3D stdin;
- }
- else
- {
- = fp =3D platform_fopen(file, "r");
- }
- if (fp)
- {
- while (fgets(line, sizeof(line), fp))
- {
- int offset =3D 0;
- CLEAR(p);
- ++line_num;
- if (strl= en(line) =3D=3D OPTION_LINE_SIZE)
- {
- msg(msglevel,
- "In %s:%d: Maximum option = line length (%d) exceeded, line starts with %s",
- file, line_num, OPT= ION_LINE_SIZE, line);
-= }
-
- /* Ignore= UTF-8 BOM at start of stream */
- if (line_num =3D=3D 1 && strncmp(line, &= quot;\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 style=3D"color: hsl(0, 100%, 40%);">- {

- bypass_doubledash= (&p[0]);

- = int lines_inline =3D check_inline_file_via_fp(fp, p, &option= s->gc);
- = add_option(options, p, lines_inline, file, line_num, level, msglev= el,
- = permission_mask, option_types_found, es);
- line_num +=3D lines_in= line;
- = }
- }=
- if (fp !=3D stdi= n)
- {
- fclose(fp);=
- }<= br>- }
- else
- {
- msg(msglevel, "In %s:%d: Error opening config= uration file: %s", top_file, top_line,
- file);
- }
- }
- e= lse
- {
- msg(msglevel,

- "In %s:%d: Maximu= m recursive include levels exceeded in include attempt of file %s -- probab= ly you have a configuration file that tries to include itself.",
- top_file, top_l= ine, file);
- }
- secure_memzero(line, s= izeof(line));
- CLEA= R(p);
-}
-

-static void
-read_config_string(const char *prefix, struct options *options, con= st char *config,
- = const msglvl_t msglevel, const unsigned int permission_mask,<= /span>
- uns= igned 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];
- = ++line_num;
- = if (parse_line(line, p, SIZE(p) - 1, prefix, line_num, msglevel, &= options->gc))
- = {
- bypas= s_doubledash(&p[0]);
- 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<= /span>
-parse_argv(struct opti= ons *options, const int argc, char *argv[], const msglvl_t msglevel,=
- const unsigned in= t permission_mask, unsigned int *option_types_found, struct env_set *es)

-{
- /* usage message */
- if (argc <=3D 1)
- {
- usage();
- }
-<= /span>
- /* config filename= specified only? */
- = if (argc =3D=3D 2 && strncmp(argv[1], "--", 2))<= br>- {
- char *p[MAX_PARMS + 1];
<= span style=3D"color: hsl(0, 100%, 40%);">- CLEAR(p);

- p[0] =3D "config";<= /span>
- p[1] =3D argv[= 1];
- add_option= (options, p, false, NULL, 0, 0, msglevel, permission_mask, option_types_fou= nd,
- = es);
- }
= - else
- {
- /* parse command line */
- for (int i =3D 1; i < argc; ++i)
- {
- char *p[MAX_PARMS + 1];
- CLEAR(p);
- p[0] =3D arg= v[i];
- if (= strncmp(p[0], "--", 2))
- {
- msg(msglevel,
- "I'm trying to parse \"%s\&qu= ot; as an --option parameter but I don't see a leading '--'&quo= t;,
- = p[0]);
- }=
- else
- {
- p[0] +=3D 2;

- }
-
- int j;
- for (j =3D 1; j < MAX_PARMS; ++j)
= - {
- if (i + j < argc)

- {<= br>- char *arg= =3D argv[i + j];
- = if (strncmp(arg, "--", 2))
- {
- p[j] =3D arg;
- }
- else
- {
- = break;
- = }
- = }
- }
- add_option(opti= ons, p, false, NULL, 0, 0, msglevel, permission_mask, option_types_found,
- = es);
- i += =3D j - 1;
- }
- }
-}
-
-bool<= /span>
-apply_push_options(str= uct context *c, struct options *options, struct buffer *buf,
- unsigned int perm= ission_mask, unsigned int *option_types_found,

- struct env_set *es, bool is_upd= ate)
-{
- 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 (isspac= e(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
- */
- if ((is_update &am= p;& !check_push_update_option_flags(line, &i, &push_update_opti= on_flags))
- = || !apply_pull_filter(options, &line[i]))
- {
- /* In case we are in a `PUSH_UPDATE` and `check_pu= sh_update_option_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 pr= ocess the next one
- = */
- = if (push_update_option_flags & PUSH_OPT_OPTIONAL)
- {
- continue; /* Ignoring this optio= n */
- }
- return false;= /* Cause push/pull error and stop push processing */
- }
-
- = if (parse_line(&line[i], p, SIZE(p) - 1, file, line_num, msgleve= l, &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_REMOV= E)
- {
- remove_opti= on(c, options, p, false, file, line_num, msglevel, permission_mask,<= br>- = option_types_found, es);
- }
- = else
- = {
- u= pdate_option(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, const char *fi= lename, msglvl_t msglevel,
- unsigned int permission_mask, unsigned int *opti= on_types_found,
- = struct env_set *es)
-{
- ms= g(D_PUSH, "OPTIONS IMPORT: reading client specific options from: %s&qu= ot;, filename);
- re= ad_config_file(o, filename, 0, filename, 0, msglevel, permission_mask, opti= on_types_found,
- = es);
-}<= /span>
-
-void
-options_string_import(struct options *options, const char *c= onfig, const msglvl_t msglevel,
- const unsigned int permission_mask, unsigne= d int *option_types_found,
- struct env_set *es)
-{
- read_config_string("[CONFIG-STRING]", options, config, m= sglevel, permission_mask,
- option_types_found, es);
-}
-
#define VERIFY_PERMISSION(mask) = \
{ = = \
if (!verify_permission(p[0], file, l= ine, (mask), permission_mask, option_types_found, \
@@ -564= 2,27 +5067,7 @@
option_ptr->flags =3D 0; = \
}

-/**
- * @brief Resets options found in the PUSH_UPDATE message tha= t are preceded by the `-` flag.
- * This function is used in push-updates to reset specifie= d options.
- * T= he number of parameters `p` must always be 1. If the permission is verified= ,
- * all relate= d options are erased or reset to their default values.
- * Upon successful permission verif= ication (by VERIFY_PERMISSION()),
- * `option_types_found` is filled with the flag correspo= nding to the option.
- = *
- * @param c The cont= ext structure.
- * @par= am options A pointer to the options structure.
- * @param p An array of strings containing the opt= ions and their parameters.
- * @param is_inline A boolean indicating if the option is inline.
- * @param file The file wh= ere the function is called.
- * @param line The line number where the function is called.- * @param msglevel The message= level.
- * @param perm= ission_mask The permission mask used by VERIFY_PERMISSION().
- * @param option_types_found A point= er to the variable where the flags corresponding to the options

<= span style=3D"color: hsl(0, 100%, 40%);">- * found are stored.

- * @param es The environment set s= tructure.

- */-static void
+void
remove_option(st= ruct context *c, struct options *options, char *p[], bool is_inline,=
const char *file, int line, const msglvl_t msgleve= l,
const unsigned int permission_mask, unsig= ned int *option_types_found,
@@ -5982,30 +5387,7 @@<= br> return true;
}

<= span style=3D"color: hsl(0, 100%, 40%);">-/**

- * @brief Processes an option to update. It first c= hecks whether it has already
- * received an option of the same type within the same update= message.
- * 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 parameters.
- * @param is_inline A boolean indicating = if the option is inline.
- * @param line The line number where the= function is called.
- = * @param level The level of the option.
- * @param msglevel The message level for logging.<= br>- * @param permission_mask The= permission mask used by VERIFY_PERMISSION().
- * @param option_types_found A pointer to the varia= ble where the flags corresponding to the options
- * found are stored.
- * @param es The environment set structure.
- * @param update_options_fo= und A pointer to the variable where the flags corresponding to the update
- * options found are st= ored, used to check if an option of the same type has already been processe= d
- * by update_option(= ) within the same push-update message.
- */
-s= tatic 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_types_found,<= /span>
@@ -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,<= /span>
+ cons= t unsigned int permission_mask, unsigned int *option_types_found,+ struct env_se= t *es);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+/**

+ * @brief Resets options found in the PUSH_UPDA= TE message that are preceded by the `-` flag.
+ * This function is used in push-updates t= o reset specified options.
+ * The number of parameters `p` must always be 1. If the perm= ission is verified,
+= * all related options are erased or reset to their default values.<= /span>
+ * Upon succe= ssful permission verification (by VERIFY_PERMISSION()),
+ * `option_types_found` is fille= d with the flag corresponding to the option.
+ *
+ * @param c The context structure.
+ * @param options A pointer to the options structure= .
+ * @param p An arr= ay of strings containing the options and their parameters.
+ * @param is_inline A boolean indica= ting if the option is inline.
+ * @param file The file where the function is called.
<= span style=3D"color: hsl(120, 100%, 40%);">+ * @param line The line number = where the function is called.

+ * @param msglevel The message level.
+ * @param permission_mask The permission mask u= sed by VERIFY_PERMISSION().
+ * @param option_types_found A pointer to the variable where the fl= ags corresponding 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 msglv= l_t msglevel,
+ = const unsigned int permission_mask, unsigned int *option_types= _found,
+ = struct env_set *es);
+
+/**<= br>+ * @brief Processes an opti= on to update. It first checks whether it has already
+ * received an option of the same = type within the same update message.
+ * If the option has already been received, it call= s add_option().
+ * = Otherwise, it deletes all existing values related to that option befo= re calling
+ * add_op= tion().
+ *+ * @param c The context struc= ture.
+ * @param opti= ons A pointer to the options structure.
+ * @param p An array of strings containing the options = and their parameters.
+ * @param is_inline A boolean indicating if the option is inline.<= br>+ * @param file The file whe= re the function is called.
+ * @param line The line number where the function is called.<= br>+ * @param level The level o= f the option.
+ * @pa= ram 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 = corresponding to the options
+ * found are stored.
+ * @param es The environment set structure.
+ * @param update_options_found A pointer = to the variable where the flags corresponding to the update
+ * options found are stored, used t= o check if an option of the same type has already been processed

= + * by update_option() within t= he same push-update message.
+ */
+void up= date_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_types_found,
+ struc= t env_set *es, unsigned int *update_options_found);
+
void parse_argv(struct = options *options, const int argc, char *argv[], const msglvl_t msglevel,
const unsigned int permission_mask, unsigned= int *option_types_found,
struct env_set *= es);

+ const int top_line, const msglvl_t msglevel,
+ = const unsigned int permission_mask, unsigned int *option_types_found,
+ str= uct env_set *es);
+
+void read_config_stri= ng(const char *prefix, struct options *options, const char *config,<= br>+ con= st msglvl_t msglevel, const unsigned int permission_mask,
+ unsigned int = *option_types_found, struct env_set *es);
+
void notnull(const char *arg, const= char *description);

void usage_small(vo= id);

= +
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 sup= port for SSL/TLS-based
+ * packet encryption, pa= cket authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2008-2025 = David Sommerseth <dazo@...2108...>
+ *
+ * This program is free software; you can redistribute it and/or modif= y
+ * it under the t= erms of the GNU General Public License version 2
+ * as published by the Free Software Foundati= on.
+ *
+ * This program is distributed i= n the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warra= nty of
+ * MERCHANTA= BILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more d= etails.
+ *+ * You should have received = a copy of the GNU General Public License along
+ * with this program; if not, see <https://w= ww.gnu.org/licenses/>.
+ */
++#ifdef HAVE_CONFIG_H+#include "config.h"= ;
+#endif
<= span style=3D"color: hsl(120, 100%, 40%);">+

+#include <string.h>
+
+#include "options.h"
+#include "options_util.h"
+#include "push.h"+
+static void
+bypass_doubledash(char **p)
+{
+ if (strlen(*p) >=3D 3 && !strncmp(*p, "--&quo= t;, 2))
+ {=
+ *p +=3D 2;<= br>+ }
+}
+
+sta= tic inline bool
+spac= e(char c)
+{+ return c =3D=3D '\0&= #39; || isspace(c);
+= }
+
+int
+parse_line(const char *line, char *p[], const int n, = const char *file, const int line_num,
+ msglvl_t msglevel, struct gc_arena *gc)=
+{
+ const int STATE_INITIAL =3D 0;+ const int STATE_READING_Q= UOTED_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;
+<= br>+ const char *error_prefi= x =3D "";
+=
+ int ret =3D 0;<= /span>
+ const char *c = =3D line;
+ int st= ate =3D STATE_INITIAL;
+ 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_SQUOTED_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_QUOT= ED_PARM;
+ = }
+ = else if (!backslash && in =3D=3D '\'')
+ {
+ = state =3D STATE_READING_SQUOTED_PARM;
+ }
+ else
+ {
+ out =3D in;
<= span style=3D"color: hsl(120, 100%, 40%);">+ state = =3D STATE_READING_UNQUOTED_PARM;

+ }
+ }
+ }
+ else if (state =3D=3D STATE_READING_UNQUOTED_PARM)
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (!backslash = && space(in))

+ {
+= state =3D STATE_DONE;
+ }
+ else
+ {
+ out =3D in;
+ }
+ }
+ else if (state =3D=3D STATE_READING_QUOT= ED_PARM)
+ = {
+ i= f (!backslash && in =3D=3D '\"')
+ {
+ state =3D STATE_DONE;=
+ }
+ else<= /span>
+ {
+ ou= t =3D in;
+ = }
+ = }
+ else i= f (state =3D=3D STATE_READING_SQUOTED_PARM)
+ {
+ if (in =3D=3D '\'')+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ state =3D S= TATE_DONE;

+ = }
+ = else
+ = {
+ = out =3D in;
+= }
+ = }
+ = if (state =3D=3D STATE_DONE)
+ {
+ /* ASSERT (parm_len > 0); */
+ p[ret] =3D gc_malloc(par= m_len + 1, true, gc);
+ memcpy(p[ret], parm, parm_len);
+ p[ret][parm_len] =3D '\0&= #39;;
+ = state =3D STATE_INITIAL;
+ parm_len =3D 0;
+ ++ret;
+ }
+
+ = if (backslash && out)
+ {
+ if (!(out =3D=3D '\\' || out =3D=3D &#= 39;\"' || space(out)))
+ {
+#ifdef ENABLE_SMALL
+ msg(msglevel, "%sOptions warning: Bad ba= ckslash ('\\') usage in %s:%d",
+ error_prefix, file, line_n= um);
+#else+ msg(msgle= vel,
+ = "%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 = 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;
+ m= sg(msglevel, "%sOptions error: Parameter at %s:%d is too long (%d char= s max): %s",
+ = error_prefix, file, line_num, (int)SIZE(parm), parm);
+ return = 0;
+ }
+ parm[parm_l= en++] =3D out;
+ = }
+
+ /* avoid overflow if too m= any 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 sin= gle quotation (\') in %s:%d", error_prefix,
+ file, line_num);
+ return 0;

+ }

+ if (state !=3D STATE_INITIAL)
+ {

+ msg(msglevel, "%sOptions error: Res= idual 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= 9;", file, line_num, i, p[i]);
+ }
+ }
+#endif
+ return ret;
+}
+
+struct in_src
+{
+#define I= S_TYPE_FP 1
+#define= IS_TYPE_BUF 2
+ i= nt 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-&= gt;u.fp));
+ }
+ else if (is->typ= e =3D=3D IS_TYPE_BUF)
+ {
+ bool= status =3D buf_parse(is->u.multiline, '\n', line, size);=
+ if ((int)strlen(li= ne) + 1 < size)
+ = {
+ = strcat(line, "\n");
+ }
= + return status;
+ }
+ else
+ {
+ ASSERT(0);

+ return false;
+ }

+}
+
+static cha= r *
+read_inline_file= (struct in_src *is, const char *close_tag, int *num_lines, struct gc_arena = *gc)
+{
+ char line[OPTION_LINE_SIZE];<= /span>
+ struct buffer bu= f =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(li= ne_ptr, close_tag, strlen(close_tag)))
+ {
+ endtagfound =3D true;
+ break;
+ }
+ if (!buf_safe(&buf, strlen(line) + 1))
+ {

+ /* Increase buffer size */
+ struct buf= fer buf2 =3D alloc_buf(buf.capacity * 2);
+ ASSERT(buf_copy(&buf2, &buf));
+ buf_clear(= &buf);
+ = free_buf(&buf);
+ }
+ = buf_printf(&buf, "%s", line);
+ }
+ if (!endtagfound)
+ {
+ msg(M_FATAL, "ERROR: Endtag %s missing", close_tag);=
+ }
+ ret =3D string_alloc(BSTR(&am= p;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)
+{
+
+ = 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, "</%s>", p= [0]);
+ p[= 1] =3D read_inline_file(is, BSTR(&close_tag), &num_inline_lines, gc= );
+ p[2] = =3D NULL;
+ = free_buf(&close_tag);
+ }
+ = }
+ return num_i= nline_lines;
+}
+
+static int
+check_inline_file_via_fp(FILE *fp, char *p[], struct = gc_arena *gc)
+{
+ struct in_src is;
+ is.type =3D IS_TYP= E_FP;
+ is.u.fp = =3D fp;
+ return c= heck_inline_file(&is, p, gc);
+}
+
+static int
+check_inline_file_via_buf(struct= buffer *multiline, char *p[], struct gc_arena *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, const char *top_file,
+ const int top_line, const msglvl_t msglevel,<= /span>
+ con= st unsigned int permission_mask, unsigned int *option_types_found,+ struct env_= set *es)
+{+ const int max_recursive_l= evels =3D 10;
+ FI= LE *fp;
+ int line= _num;
+ char line[= OPTION_LINE_SIZE + 1];
+
+ ++le= vel;
+ if (level &= lt;=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;<= br>+ CLEAR(p);
+ ++lin= e_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 stream */
+ if (line_num =3D=3D 1= && strncmp(line, "\xEF\xBB\xBF", 3) =3D=3D 0)
= + {
+ offset =3D 3= ;

+ }<= /span>
+ if (= parse_line(line + offset, p, SIZE(p) - 1, file, line_num, msglevel,<= br>+ = &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_mask= , option_types_found, es);
+ line_num +=3D lines_inline;
+ }
+ }
+ if (fp !=3D stdin)
+ {
+ fclose(fp);
+ }

+ }
+ else
+ {
+ msg(msglevel, "In %s:%d: Error opening configurat= ion file: %s", top_file, top_line,
+ file);
+ }
+ }
+ = else
+ {=
+ msg(msglevel,
+ "In %s:= %d: Maximum recursive include levels exceeded in include attempt of file %s= -- probably you have a configuration file that tries to include itself.&qu= ot;,
+ top= _file, top_line, file);
+ }
+ secure= _memzero(line, sizeof(line));
+ CLEAR(p);
+}
+
+void

+read_config_string(const char *prefix, struct op= tions *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 multilin= e;
+ 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];<= br>+ CLEAR(p);+ ++line_num;+ if (parse_line(line, = p, SIZE(p) - 1, prefix, line_num, msglevel, &options->gc))+ {
+ bypass_doubledash(&p[0]= );
+ int l= ines_inline =3D check_inline_file_via_buf(&multiline, p, &options-&= gt;gc);
+ = add_option(options, p, lines_inline, prefix, line_num, 0, msglevel, permiss= ion_mask,
+ = option_types_found, es);
+ line_num +=3D lines_inline;
+ }
+ CLEAR(p);
+ }
+ secure_memzero(line, sizeof(line));
+}
+
+voi= d
+parse_argv(struct = options *options, const int argc, char *argv[], const msglvl_t msglevel,
+ const unsig= ned 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], &q= uot;--", 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_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], "= --", 2))
+ = {
+ = msg(msglevel,
+ = "I'm trying to parse \"%s\" as an --op= tion 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 < a= rgc)

+ = {
+ = 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, permi= ssion_mask, option_types_found,
+ es);
+ i +=3D j - 1;
+ }
+ }
+}
+
+bool

+apply_push_options(struct context *c, struct optio= ns *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-OPT= IONS]";
+ con= st 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 par= se_line */
+ w= hile (isspace(line[i]))
+ {
+ = i++;
+ }=
+
+ /* If we are not in a 'PUS= H_UPDATE' we just check `apply_pull_filter()`
+ * otherwise we must call `check_push= _update_option_flags()` first
+ */
+ if ((is_update && !check_push_update_option_flags(line, &= amp;i, &push_update_option_flags))
+ || !apply_pull_filter(options, &line[i])= )
+ {+ /* In case we ar= e in a `PUSH_UPDATE` and `check_push_update_option_flags()`
+ * or `apply_pull_filte= r()` fail but the option is flagged by `PUSH_OPT_OPTIONAL`,

+ * instead of restartin= g, 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 er= ror and stop push processing */
+ }
+
+ if (parse= _line(&line[i], p, SIZE(p) - 1, file, line_num, msglevel, &options-= >gc))
+ {
+ if (!is_u= pdate)
+ {=
+ 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_op= tion(c, options, p, false, file, line_num, msglevel, permission_mask,
+ = option_types_found, es);
+ }
+ else
= + {
+ = update_option(c, options, p, false, file, line_num, 0, msglevel,= permission_mask,
+ = option_types_found, es, &update_options_fou= nd);
+ }
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+ return true;
+}
+
+void
+options_server_import(str= uct options *o, const char *filename, msglvl_t msglevel,
+ unsigned int per= mission_mask, unsigned int *option_types_found,
+ struct env_set *es)
+{
+ msg(D_PUSH, "OPTIONS IMPORT: rea= ding client specific options from: %s", filename);
+ read_config_file(o, filename, 0, fi= lename, 0, msglevel, permission_mask, option_types_found,
+ es);
<= span style=3D"color: hsl(120, 100%, 40%);">+}

+
+void
+options_= string_import(struct options *options, const char *config, const msglvl_t m= sglevel,
+ = const unsigned int permission_mask, unsigned int *option_types_f= ound,
+ = struct env_set *es)
+{
+ read_c= onfig_string("[CONFIG-STRING]", options, config, msglevel, permis= sion_mask,
+ = option_types_found, es);
+}

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

Gerrit-MessageType: newpatchset
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>
= --AKvBr/gqBjc=--