]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
xlat: Add module find function
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 15 Jun 2023 15:16:46 +0000 (11:16 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 15 Jun 2023 15:16:46 +0000 (11:16 -0400)
src/lib/unlang/xlat_func.c
src/lib/unlang/xlat_func.h

index c5b24c589d189afdb0285494d82f39e21d18de7e..225778563f4c5c537283d4e0f205e71c194684e9 100644 (file)
@@ -156,6 +156,32 @@ static int xlat_arg_cmp_list_no_escape(xlat_arg_parser_t const a[], xlat_arg_par
 }
 #endif
 
+xlat_t *xlat_func_find_module(module_inst_ctx_t const *mctx, char const *name)
+{
+       char inst_name[256];
+
+       fr_assert(xlat_root);
+
+       if (!*name) {
+               ERROR("%s: Invalid xlat name", __FUNCTION__);
+               return NULL;
+       }
+
+       /*
+        *      Name xlats other than those which are just the module instance
+        *      as <instance name>.<function name>
+        */
+       if (mctx && name != mctx->inst->name) {
+               snprintf(inst_name, sizeof(inst_name), "%s.%s", mctx->inst->name, name);
+               name = inst_name;
+       }
+
+       /*
+        *      If it already exists, replace the instance.
+        */
+       return fr_rb_find(xlat_root, &(xlat_t){ .name = name });
+}
+
 /** Register an xlat function for a module
  *
  * @param[in] ctx              Used to automate deregistration of the xlat fnction.
@@ -169,7 +195,7 @@ static int xlat_arg_cmp_list_no_escape(xlat_arg_parser_t const a[], xlat_arg_par
  *     - NULL on failure.
  */
 xlat_t *xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx,
-                            char const *name, xlat_func_t func, fr_type_t return_type)
+                                 char const *name, xlat_func_t func, fr_type_t return_type)
 {
        xlat_t  *c;
        module_inst_ctx_t *our_mctx = NULL;
index 7d23b1f20b0058ebf3acf2325c7bdb6d647c8c40..ef664d76494e8366885125ad32a38d9e70e4df76 100644 (file)
@@ -52,6 +52,8 @@ typedef       int (*xlat_resolve_t)(xlat_exp_t *xlat, void *inst, xlat_res_rules_t con
  */
 typedef int (*xlat_purify_t)(xlat_exp_t *xlat, void *inst, request_t *request);
 
+xlat_t         *xlat_func_find_module(module_inst_ctx_t const *mctx, char const *name);
+
 xlat_t         *xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx,
                                           char const *name, xlat_func_t func, fr_type_t return_type);
 xlat_t         *xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type) CC_HINT(nonnull(2));