]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add xlat wrapper function for bootstrap / instantiate ephemeral
authorAlan T. DeKok <aland@freeradius.org>
Sat, 7 Oct 2023 21:29:21 +0000 (17:29 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 7 Oct 2023 21:29:21 +0000 (17:29 -0400)
* xlat_bootstrap() - when reading the configuration files
  pretty much only called by xlat_redundant()

* xlat_instantiate_ephemeral() - when we have a #request_t and
  are parsing things at run-time

* xlat_finalize() - when we have a #tmpl_rules_t, and we're not
  sure if we're run-time or config load time.

The issue is that the various xlat tokenize funtions call each
other in different combinations.  The only way to consistently
know if we're run-time or config time is via the #tmpl_rules_t.

This change (also via previous commits) allows us to get rid of
the various public tokenize_ephemeral() functions, which were
confusing the issue.

src/lib/server/tmpl_tokenize.c
src/lib/unlang/xlat.h
src/lib/unlang/xlat_expr.c
src/lib/unlang/xlat_inst.c
src/lib/unlang/xlat_tokenize.c

index e15247659c04048b5902a8f313dbf3ecb9979003..df05f6d954f8df5fa311b50b5b69eff532167a1e 100644 (file)
@@ -3278,14 +3278,7 @@ fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out,
                 *      so that their instance data will be created.
                 */
                if (head) {
-                       int rcode;
-
-                       if (!t_rules->at_runtime) {
-                               rcode = xlat_bootstrap(head);
-                       } else {
-                               rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
-                       }
-                       if (rcode < 0) {
+                       if (xlat_finalize(head, t_rules) < 0) {
                                fr_strerror_const("Failed to bootstrap xlat");
                                FR_SBUFF_ERROR_RETURN(&our_in);
                        }
index b86be7fe15a3aaa59ff3a5996601511b37b6e54d..d9a39a3cf581402ba108d5f3dd3506c4182eef58 100644 (file)
@@ -466,6 +466,8 @@ int         xlat_bootstrap(xlat_exp_head_t *root);
 
 void           xlat_instances_free(void);
 
+int            xlat_finalize(xlat_exp_head_t *head, tmpl_rules_t const *t_rules); /* xlat_bootstrap() or xlat_instantiate_ephemeral() */
+
 /*
  *     xlat_purify.c
  */
index 49b3a4e2b7ffe973570c24b764c1a2348ee0feb0..0bbdadc6434e3df8aa976712ef05373dd3169cc8 100644 (file)
@@ -2833,7 +2833,6 @@ static const fr_sbuff_term_t operator_terms = FR_SBUFF_TERMS(
 static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in,
                                                   fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool cond)
 {
-       int rcode;
        ssize_t slen;
        fr_sbuff_parse_rules_t *bracket_rules = NULL;
        fr_sbuff_parse_rules_t *terminal_rules = NULL;
@@ -2908,12 +2907,7 @@ static fr_slen_t xlat_tokenize_expression_internal(TALLOC_CTX *ctx, xlat_exp_hea
         *      Add nodes that need to be bootstrapped to
         *      the registry.
         */
-       if (!t_rules || !t_rules->xlat.runtime_el) {
-               rcode = xlat_bootstrap(head);
-       } else {
-               rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
-       }
-       if (rcode < 0) {
+       if (xlat_finalize(head, t_rules) < 0) {
                talloc_free(head);
                return -1;
        }
index 9713b0a7a1a9b0111f4ade4f960976d99271dd9d..f00c4ec2bc7bf97a1f41b9ec37926dd8411f18bc 100644 (file)
@@ -348,9 +348,31 @@ static int _xlat_instantiate_ephemeral_walker(xlat_exp_t *node, void *uctx)
        return 0;
 }
 
+
+/** Bootstrap static xlats, or instantiate ephemeral ones.
+ *
+ *  @note - this function should be called when we have a
+ *  #tmpl_rules_t.  i.e. instead of calling xlat_bootstrap() or
+ *  xlat_instantiate_ephemeral()
+ *
+ * @param[in] head of xlat tree to create instance data for.
+ * @param[in] t_rules parsing rules with #fr_event_list_t
+ */
+int xlat_finalize(xlat_exp_head_t *head, tmpl_rules_t const *t_rules)
+{
+       if (!t_rules || !t_rules->xlat.runtime_el) {
+               fr_assert(!t_rules->at_runtime);
+               return xlat_bootstrap(head);
+       }
+
+       fr_assert(t_rules->at_runtime);
+       return xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
+}
+
 /** Create instance data for "ephemeral" xlats
  *
- * @note This must only be used for xlats created at runtime.
+ * @note This function should only be called from routines which get
+ * passed a #request_t.
  *
  * @param[in] head of xlat tree to create instance data for.
  * @param[in] el event list used to run any instantiate data
@@ -587,9 +609,9 @@ static int _xlat_bootstrap_walker(xlat_exp_t *node, UNUSED void *uctx)
 
 /** Create instance data for "permanent" xlats
  *
- * @note This must only be used for xlats created during startup.
+ * @note This must only be used for xlats which are read from the configuration files.
  *      IF THIS IS CALLED FOR XLATS TOKENIZED AT RUNTIME YOU WILL LEAK LARGE AMOUNTS OF MEMORY.
- *      USE xlat_instantiate_request() INSTEAD.
+ *      If the caller has a #tmpl_rules_t, it should call xlat_finalize() instead.
  *
  * @param[in] head of xlat tree to create instance data for.
  */
index c362907f2131591237856c4fa0ea1aa98a4f719f..945dcba9e59f05ca064c0a82b8fe0c210eb25035 100644 (file)
@@ -1860,7 +1860,6 @@ fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t
 fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in,
                        fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
 {
-       int rcode;
        fr_sbuff_t      our_in = FR_SBUFF(in);
        xlat_exp_head_t *head;
 
@@ -1881,12 +1880,7 @@ fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **out, fr_sbuff_t *in,
         *      Add nodes that need to be bootstrapped to
         *      the registry.
         */
-       if (!t_rules || !t_rules->xlat.runtime_el) {
-               rcode = xlat_bootstrap(head);
-       } else {
-               rcode = xlat_instantiate_ephemeral(head, t_rules->xlat.runtime_el);
-       }
-       if (rcode < 0) {
+       if (xlat_finalize(head, t_rules) < 0) {
                talloc_free(head);
                return 0;
        }