]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
move to xlat_tokenize(), as ephemeral checks are in t_rules
authorAlan T. DeKok <aland@freeradius.org>
Sat, 7 Oct 2023 15:00:46 +0000 (11:00 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 7 Oct 2023 15:00:46 +0000 (11:00 -0400)
src/bin/unit_test_module.c
src/lib/server/tmpl_tokenize.c
src/lib/unlang/xlat.h
src/lib/unlang/xlat_builtin.c
src/lib/unlang/xlat_eval.c
src/lib/unlang/xlat_tokenize.c

index 647fe3abddcd5dafc293c1e75b43aa6b837c171e..961edcae33b0b818ff2709ce2aa275d8d82676f9 100644 (file)
@@ -455,8 +455,20 @@ static bool do_xlats(fr_event_list_t *el, request_t *request, char const *filena
                        TALLOC_CTX              *xlat_ctx = talloc_init_const("xlat");
                        xlat_exp_head_t         *head = NULL;
                        fr_sbuff_parse_rules_t  p_rules = { .escapes = &fr_value_unescape_double };
+                       tmpl_rules_t            t_rules = (tmpl_rules_t) {
+                                                               .attr = {
+                                                                       .dict_def = dict_protocol,
+                                                                       .list_def = request_attr_request,
+                                                                       .allow_unresolved = true,
+                                                               },
+                                                               .xlat = {
+                                                                       .runtime_el = el,
+                                                               },
+                                                               .at_runtime = true,
+                                                               };
+
 
-                       slen = xlat_tokenize_ephemeral(xlat_ctx, &head, el, &line, &p_rules, NULL);
+                       slen = xlat_tokenize(xlat_ctx, &head, &line, &p_rules, &t_rules);
                        if (slen <= 0) {
                                talloc_free(xlat_ctx);
                                fr_sbuff_in_sprintf(&out, "ERROR offset %d '%s'", (int) -slen, fr_strerror());
index 68bea073c0506cc415da57d6ace881c8b9851646..a46723bfc8a03c350525449f33cc1fe6764417e9 100644 (file)
@@ -3041,14 +3041,7 @@ fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out,
                        xlat_exp_head_t *head = NULL;
 
                        vpt = tmpl_alloc_null(ctx);
-                       if (!t_rules->at_runtime) {
-                               slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
-                       } else {
-                               slen = xlat_tokenize_ephemeral(vpt, &head,
-                                                              t_rules->xlat.runtime_el, &our_in,
-                                                              p_rules, t_rules);
-                       }
-
+                       slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
                        if (slen <= 0) FR_SBUFF_ERROR_RETURN(&our_in);
 
                        if (xlat_needs_resolving(head)) UNRESOLVED_SET(&type);
@@ -3216,12 +3209,7 @@ fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out,
 
                vpt = tmpl_alloc_null(ctx);
 
-               if (!t_rules->at_runtime) {
-                       slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
-               } else {
-                       slen = xlat_tokenize_ephemeral(vpt, &head, t_rules->xlat.runtime_el,
-                                                      &our_in, p_rules, t_rules);
-               }
+               slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
                if (slen < 0) FR_SBUFF_ERROR_RETURN(&our_in);
 
                /*
@@ -3314,14 +3302,7 @@ fr_slen_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out,
 
                vpt = tmpl_alloc_null(ctx);
 
-               if (!t_rules->at_runtime) {
-                       slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
-               } else {
-                       slen = xlat_tokenize_ephemeral(vpt, &head,
-                                                      t_rules->xlat.runtime_el, &our_in,
-                                                      p_rules, t_rules);
-               }
-
+               slen = xlat_tokenize(vpt, &head, &our_in, p_rules, t_rules);
                if (slen < 0) FR_SBUFF_ERROR_RETURN(&our_in);
 
                /*
index e9d2cfd77a76ca80b811f0147125e16c5e813d52..b86be7fe15a3aaa59ff3a5996601511b37b6e54d 100644 (file)
@@ -392,10 +392,6 @@ fr_slen_t  xlat_tokenize_expression(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_s
 fr_slen_t      xlat_tokenize_condition(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in,
                                        fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules);
 
-fr_slen_t      xlat_tokenize_ephemeral(TALLOC_CTX *ctx, xlat_exp_head_t **head,
-                                       fr_event_list_t *el, fr_sbuff_t *in,
-                                       fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules);
-
 fr_slen_t      xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in,
                                   fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool comma, bool allow_attr);
 
index f61bf47832f60f18770a24d6213c9be32df8e8b3..9951e2e5f113234645a34e08ab52a3025ec00db4 100644 (file)
@@ -860,22 +860,25 @@ static xlat_action_t xlat_func_eval(TALLOC_CTX *ctx, fr_dcursor_t *out,
        /*
         *      Parse the input as a literal expansion
         */
-       if (xlat_tokenize_ephemeral(rctx,
-                                   &rctx->ex, unlang_interpret_event_list(request),
-                                   &FR_SBUFF_IN(arg->vb_strvalue, arg->vb_length),
-                                   &(fr_sbuff_parse_rules_t){
-                                           .escapes = &escape_rules
-                                   },
-                                   &(tmpl_rules_t){
-                                       .attr = {
-                                               .dict_def = request->dict,
-                                               .list_def = request_attr_request,
-                                               .allow_unknown = false,
-                                               .allow_unresolved = false,
-                                               .allow_foreign = false,
+       if (xlat_tokenize(rctx,
+                         &rctx->ex, 
+                         &FR_SBUFF_IN(arg->vb_strvalue, arg->vb_length),
+                         &(fr_sbuff_parse_rules_t){
+                                 .escapes = &escape_rules
+                         },
+                         &(tmpl_rules_t){
+                                 .attr = {
+                                         .dict_def = request->dict,
+                                         .list_def = request_attr_request,
+                                         .allow_unknown = false,
+                                         .allow_unresolved = false,
+                                         .allow_foreign = false,
                                        },
-                                       .at_runtime = true
-                                   }) < 0) {
+                                 .xlat = {
+                                         .runtime_el = unlang_interpret_event_list(request),
+                                 },
+                                 .at_runtime = true
+                         }) < 0) {
                RPEDEBUG("Failed parsing expansion");
        error:
                talloc_free(rctx);
index e2edaf0dabf142561b92214f6b2442750ee4de3c..404cdf7a567c91b9021d1723f2d2d3efa7d0948d 100644 (file)
@@ -1497,15 +1497,19 @@ ssize_t _xlat_eval(TALLOC_CTX *ctx, char **out, size_t outlen, request_t *reques
        /*
         *      Give better errors than the old code.
         */
-       len = xlat_tokenize_ephemeral(ctx, &head, unlang_interpret_event_list(request),
-                                     &FR_SBUFF_IN(fmt, strlen(fmt)),
-                                     NULL,
-                                     &(tmpl_rules_t){
-                                       .attr = {
-                                               .dict_def = request->dict,
-                                               .list_def = request_attr_request,
-                                       }
-                                     });
+       len = xlat_tokenize(ctx, &head,
+                           &FR_SBUFF_IN(fmt, strlen(fmt)),
+                           NULL,
+                           &(tmpl_rules_t){
+                                   .attr = {
+                                           .dict_def = request->dict,
+                                           .list_def = request_attr_request,
+                                   },
+                                   .xlat = {
+                                           .runtime_el = unlang_interpret_event_list(request),
+                                   },
+                                   .at_runtime = true,
+                           });
        if (len == 0) {
                if (*out) {
                        **out = '\0';
index c43023fc3e87884cec1ce4d7a3139d516d7bc895..f679ebfec5818d48962f21f6ee562d657a5cc7ae 100644 (file)
@@ -1630,68 +1630,6 @@ ssize_t xlat_print(fr_sbuff_t *out, xlat_exp_head_t const *head, fr_sbuff_escape
 }
 
 
-/** Tokenize an xlat expansion at runtime
- *
- * This is used for runtime parsing of xlat expansions, such as those we receive from datastores
- * like LDAP or SQL.
- *
- * @param[in] ctx      to allocate dynamic buffers in.
- * @param[out] out     the head of the xlat list / tree structure.
- * @param[in] el       for registering any I/O handlers.
- * @param[in] in       the format string to expand.
- * @param[in] p_rules  from the encompassing grammar.
- * @param[in] t_rules  controlling how attribute references are parsed.
- * @return
- *     - >0 on success.
- *     - 0 and *head == NULL - Parse failure on first char.
- *     - 0 and *head != NULL - Zero length expansion
- *     - <0 the negative offset of the parse failure.
- */
-fr_slen_t xlat_tokenize_ephemeral(TALLOC_CTX *ctx, xlat_exp_head_t **out,
-                                 fr_event_list_t *el, fr_sbuff_t *in,
-                                 fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
-{
-       fr_sbuff_t      our_in = FR_SBUFF(in);
-       tmpl_rules_t    our_t_rules = {};
-       xlat_exp_head_t *head;
-
-       MEM(head = xlat_exp_head_alloc(ctx));
-
-       if (t_rules) {
-               head->dict = t_rules->attr.dict_def;
-               our_t_rules = *t_rules;
-       }
-
-       our_t_rules.xlat.runtime_el = el;
-
-       fr_strerror_clear();    /* Clear error buffer */
-       if (xlat_tokenize_input(head, &our_in, p_rules, &our_t_rules) < 0) {
-               talloc_free(head);
-               FR_SBUFF_ERROR_RETURN(&our_in);
-       }
-
-       /*
-        *      Zero length expansion, return a zero length node.
-        */
-       if (!xlat_exp_head(head)) {
-               *out = head;
-               return 0;
-       }
-
-       /*
-        *      Create ephemeral instance data for the xlat
-        */
-       if (xlat_instantiate_ephemeral(head, el) < 0) {
-               fr_strerror_const("Failed performing ephemeral instantiation for xlat");
-               talloc_free(head);
-               return 0;
-       }
-
-       *out = head;
-
-       FR_SBUFF_SET_RETURN(in, &our_in);
-}
-
 /** Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments
  *
  * @param[in] ctx              to allocate nodes in.  Note: All nodes will be
@@ -1922,6 +1860,7 @@ 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;
 
@@ -1939,7 +1878,12 @@ 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 (xlat_exp_head(head) && (xlat_bootstrap(head) < 0)) {
+       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) {
                talloc_free(head);
                return 0;
        }