From: Alan T. DeKok Date: Sun, 21 Aug 2022 13:46:31 +0000 (-0400) Subject: check run-time variables for new conditions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f7072ead3b18431f62d2d761aad12348163fa18;p=thirdparty%2Ffreeradius-server.git check run-time variables for new conditions and update the unit tests to use the new conditions, except for a few cases which haven't yet been migrated. --- diff --git a/src/lib/server/cf_file.c b/src/lib/server/cf_file.c index 4110f67e5dd..64c6ef1d448 100644 --- a/src/lib/server/cf_file.c +++ b/src/lib/server/cf_file.c @@ -60,6 +60,8 @@ RCSID("$Id$") #include #include +#include + bool check_config = false; static uid_t conf_check_uid = (uid_t)-1; static gid_t conf_check_gid = (gid_t)-1; @@ -1146,12 +1148,10 @@ static int process_template(cf_stack_t *stack) static int cf_file_fill(cf_stack_t *stack); -#ifdef WITH_XLAT_COND static const fr_sbuff_term_t if_terminals = FR_SBUFF_TERMS( L(""), L("{"), ); -#endif static uint8_t const *skip_string(uint8_t const *p, char c) { @@ -1254,12 +1254,10 @@ static CONF_ITEM *process_if(cf_stack_t *stack) CONF_SECTION *parent = frame->current; char *buff[4]; tmpl_rules_t t_rules; -#ifdef WITH_XLAT_COND xlat_exp_head_t *head = NULL; fr_sbuff_parse_rules_t p_rules = { }; p_rules.terminals = &if_terminals; -#endif /* * Short names are nicer. @@ -1478,9 +1476,7 @@ static CONF_ITEM *process_if(cf_stack_t *stack) ptr = buff[3]; slen = my_slen; -#ifdef WITH_XLAT_COND parse_error: -#endif fr_canonicalize_error(cs, &spaces, &text, slen, ptr); cf_log_err(cs, "Parse error in condition"); @@ -1493,14 +1489,14 @@ static CONF_ITEM *process_if(cf_stack_t *stack) return NULL; } -#ifdef WITH_XLAT_COND - my_slen = xlat_tokenize_condition(cs, &head, &FR_SBUFF_IN(buff[3], strlen(buff[3])), &p_rules, &t_rules); - if (my_slen <= 0) { - ptr = buff[3]; - slen = my_slen; - goto parse_error; + if (main_config->parse_new_conditions) { + my_slen = xlat_tokenize_condition(cs, &head, &FR_SBUFF_IN(buff[3], strlen(buff[3])), &p_rules, &t_rules); + if (my_slen <= 0) { + ptr = buff[3]; + slen = my_slen; + goto parse_error; + } } -#endif } MEM(cs->name2 = talloc_typed_strdup(cs, buff[2])); @@ -1521,9 +1517,7 @@ static CONF_ITEM *process_if(cf_stack_t *stack) * the condition to the CONF_SECTION. */ cf_data_add(cs, cond, NULL, true); -#ifdef WITH_XLAT_COND cf_data_add(cs, head, NULL, true); -#endif stack->ptr = ptr; cs->allow_unlang = true; diff --git a/src/lib/unlang/compile.c b/src/lib/unlang/compile.c index cb227b6e72c..4799580f6d9 100644 --- a/src/lib/unlang/compile.c +++ b/src/lib/unlang/compile.c @@ -41,7 +41,6 @@ RCSID("$Id$") #include "switch_priv.h" #include "edit_priv.h" - #define UNLANG_IGNORE ((unlang_t *) -1) static unsigned int unlang_number = 1; @@ -3107,7 +3106,6 @@ static unlang_t *compile_if_subsection(unlang_t *parent, unlang_compile_t *unlan unlang_cond_t *gext; fr_cond_t *cond; -#ifdef WITH_XLAT_COND xlat_exp_head_t *head; bool is_truthy, value; xlat_res_rules_t xr_rules = { @@ -3115,7 +3113,6 @@ static unlang_t *compile_if_subsection(unlang_t *parent, unlang_compile_t *unlan .dict_def = unlang_ctx->rules->attr.dict_def, }, }; -#endif if (!cf_section_name2(cs)) { cf_log_err(cs, "'%s' without condition", unlang_ops[ext->type].name); @@ -3125,21 +3122,28 @@ static unlang_t *compile_if_subsection(unlang_t *parent, unlang_compile_t *unlan cond = cf_data_value(cf_data_find(cs, fr_cond_t, NULL)); fr_assert(cond != NULL); -#ifdef WITH_XLAT_COND - head = cf_data_value(cf_data_find(cs, xlat_exp_head_t, NULL)); - fr_assert(head != NULL); - /* - * Resolve the xlat first. + * Migration support. */ - if (xlat_resolve(head, &xr_rules) < 0) { - cf_log_err(cs, "Failed resolving condition - %s", fr_strerror()); - return NULL; - } + if (main_config->parse_new_conditions) { + head = cf_data_value(cf_data_find(cs, xlat_exp_head_t, NULL)); + fr_assert(head != NULL); - is_truthy = xlat_is_truthy(head, &value); -#endif + /* + * Resolve the xlat first. + */ + if (xlat_resolve(head, &xr_rules) < 0) { + cf_log_err(cs, "Failed resolving condition - %s", fr_strerror()); + return NULL; + } + + is_truthy = xlat_is_truthy(head, &value); + } + /* + * We still do some resolving of old-style conditions, + * and skipping of sections. + */ if (cond->type == COND_TYPE_FALSE) { cf_log_debug_prefix(cs, "Skipping contents of '%s' as it is always 'false'", unlang_ops[ext->type].name); @@ -3195,11 +3199,9 @@ static unlang_t *compile_if_subsection(unlang_t *parent, unlang_compile_t *unlan gext = unlang_group_to_cond(g); gext->cond = cond; -#ifdef WITH_XLAT_COND gext->head = head; gext->is_truthy = is_truthy; gext->value = value; -#endif return c; } diff --git a/src/lib/unlang/condition.c b/src/lib/unlang/condition.c index da5ee314084..e29cc577d0b 100644 --- a/src/lib/unlang/condition.c +++ b/src/lib/unlang/condition.c @@ -27,7 +27,6 @@ RCSID("$Id$") #include "condition_priv.h" #include "group_priv.h" -#ifdef WITH_XLAT_COND typedef struct { fr_value_box_list_t out; //!< Head of the result of a nested ///< expansion. @@ -71,40 +70,40 @@ static unlang_action_t unlang_if_resume(rlm_rcode_t *p_result, request_t *reques */ return unlang_group(p_result, request, frame); } -#endif static unlang_action_t unlang_if(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame) { unlang_group_t *g = unlang_generic_to_group(frame->instruction); unlang_cond_t *gext = unlang_group_to_cond(g); -#ifdef WITH_XLAT_COND unlang_frame_state_cond_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_cond_t); -#endif fr_assert(gext->cond != NULL); -#ifndef WITH_XLAT_COND - if (!cond_eval(request, *p_result, gext->cond)) { - RDEBUG2("..."); - return UNLANG_ACTION_EXECUTE_NEXT; + /* + * Migration support. + */ + if (!main_config->use_new_conditions) { + if (!cond_eval(request, *p_result, gext->cond)) { + RDEBUG2("..."); + return UNLANG_ACTION_EXECUTE_NEXT; + } + + /* + * Tell the main interpreter to skip over the else / + * elsif blocks, as this "if" condition was taken. + */ + while (frame->next && + ((frame->next->type == UNLANG_TYPE_ELSE) || + (frame->next->type == UNLANG_TYPE_ELSIF))) { + frame->next = frame->next->next; + } + + /* + * We took the "if". Go recurse into its' children. + */ + return unlang_group(p_result, request, frame); } - /* - * Tell the main interpreter to skip over the else / - * elsif blocks, as this "if" condition was taken. - */ - while (frame->next && - ((frame->next->type == UNLANG_TYPE_ELSE) || - (frame->next->type == UNLANG_TYPE_ELSIF))) { - frame->next = frame->next->next; - } - - /* - * We took the "if". Go recurse into its' children. - */ - return unlang_group(p_result, request, frame); -#else - /* * If we always run this condition, then don't bother pushing anything onto the stack. * @@ -131,7 +130,6 @@ static unlang_action_t unlang_if(rlm_rcode_t *p_result, request_t *request, unla request, gext->head, UNLANG_SUB_FRAME) < 0) return UNLANG_ACTION_FAIL; return UNLANG_ACTION_PUSHED_CHILD; -#endif } void unlang_condition_init(void) @@ -141,10 +139,8 @@ void unlang_condition_init(void) .name = "if", .interpret = unlang_if, .debug_braces = true, -#ifdef WITH_XLAT_COND .frame_state_size = sizeof(unlang_frame_state_cond_t), .frame_state_type = "unlang_frame_state_cond_t", -#endif }); unlang_register(UNLANG_TYPE_ELSE, @@ -159,9 +155,7 @@ void unlang_condition_init(void) .name = "elseif", .interpret = unlang_if, .debug_braces = true, -#ifdef WITH_XLAT_COND .frame_state_size = sizeof(unlang_frame_state_cond_t), .frame_state_type = "unlang_frame_state_cond_t", -#endif }); } diff --git a/src/tests/keywords/all.mk b/src/tests/keywords/all.mk index 660b0b59039..9174df5a7a0 100644 --- a/src/tests/keywords/all.mk +++ b/src/tests/keywords/all.mk @@ -36,6 +36,15 @@ $(eval $(call TEST_BOOTSTRAP)) # define KEYWORD_TEST test.keywords.${1}: $(addprefix $(OUTPUT)/,${1}) + +# +# Migration support. Some of the tests don't run under the new +# conditions, so we don't run them under the new conditions. +# +ifeq "$(findstring ${1}, paircmp xlat-string xlat-subst)" "" +$(OUTPUT)/${1}: NEW_COND=-S parse_new_conditions=yes -S use_new_conditions=yes +endif + endef $(foreach x,$(FILES),$(eval $(call KEYWORD_TEST,$x))) @@ -97,7 +106,7 @@ KEYWORD_LIBS := $(addsuffix .la,$(addprefix rlm_,$(KEYWORD_MODULES))) rlm_csv.la # NOTE: Grepping for $< is not safe cross platform, as on Linux it # expands to the full absolute path, and on macOS it appears to be relative. $(OUTPUT)/%: $(DIR)/% $(TEST_BIN_DIR)/unit_test_module | $(KEYWORD_RADDB) $(KEYWORD_LIBS) build.raddb rlm_test.la rlm_csv.la rlm_unpack.la - $(eval CMD:=KEYWORD=$(notdir $@) $(TEST_BIN)/unit_test_module $(UNIT_TEST_KEYWORD_ARGS.$(subst -,_,$(notdir $@))) -D share/dictionary -d src/tests/keywords/ -i "$@.attrs" -f "$@.attrs" -r "$@" -xx) + $(eval CMD:=KEYWORD=$(notdir $@) $(TEST_BIN)/unit_test_module $(NEW_COND) $(UNIT_TEST_KEYWORD_ARGS.$(subst -,_,$(notdir $@))) -D share/dictionary -d src/tests/keywords/ -i "$@.attrs" -f "$@.attrs" -r "$@" -xx) @echo "KEYWORD-TEST $(notdir $@)" ${Q}cp $(if $(wildcard $<.attrs),$<.attrs,$(dir $<)/default-input.attrs) $@.attrs ${Q}if ! $(CMD) > "$@.log" 2>&1 || ! test -f "$@"; then \