]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add ttl.get function for cache module
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 21 Mar 2024 04:24:27 +0000 (00:24 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 21 Mar 2024 04:24:37 +0000 (00:24 -0400)
src/lib/unlang/xlat_func.c
src/modules/rlm_cache/rlm_cache.c
src/tests/modules/cache_rbtree/cache-xlat.unlang

index 44cb674848930357805bb2f46892422bb769fd0d..2ef35b08dee54fc50b562f3d6928a107818b3c1b 100644 (file)
@@ -294,10 +294,8 @@ xlat_t *xlat_func_register_module(TALLOC_CTX *ctx, module_inst_ctx_t const *mctx
        c = xlat_func_register(ctx, name, func, return_type);
        if (!c) return NULL;
 
-       if (mctx) {
-               MEM(our_mctx = talloc_zero(c, module_inst_ctx_t));      /* Original won't stick around */
-               memcpy(our_mctx, mctx, sizeof(*our_mctx));
-       }
+       MEM(our_mctx = talloc_zero(c, module_inst_ctx_t));      /* Original won't stick around */
+       memcpy(our_mctx, mctx, sizeof(*our_mctx));
        c->mctx = our_mctx;
 
        return c;
index b2ea7c8ab319e33be1a9f14babf574afaea110b5..c0578f3bca9ac2712a9f5009cee643eafaed2318 100644 (file)
@@ -888,6 +888,43 @@ xlat_action_t cache_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
        return XLAT_ACTION_DONE;
 }
 
+static xlat_action_t cache_ttl_get_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out,
+                                       xlat_ctx_t const *xctx,
+                                       request_t *request, UNUSED fr_value_box_list_t *in)
+{
+
+       rlm_cache_entry_t       *c = NULL;
+       rlm_cache_t             *inst = talloc_get_type_abort(xctx->mctx->inst->data, rlm_cache_t);
+       cache_call_env_t        *env = talloc_get_type_abort(xctx->env_data, cache_call_env_t);
+       rlm_cache_handle_t      *handle = NULL;
+
+       rlm_rcode_t             rcode = RLM_MODULE_NOOP;
+
+       fr_value_box_t          *vb;
+
+       if (cache_acquire(&handle, inst, request) < 0) {
+               return XLAT_ACTION_FAIL;
+       }
+
+       cache_find(&rcode, &c, inst, request, &handle, env->key);
+       switch (rcode) {
+       case RLM_MODULE_OK:             /* found */
+               break;
+
+       default:
+               cache_release(inst, request, &handle);
+               return XLAT_ACTION_DONE;
+       }
+
+       MEM(vb = fr_value_box_alloc_null(ctx));
+       vb->vb_int64 = fr_time_delta_unwrap(fr_unix_time_sub(c->expires, fr_time_to_unix_time(request->packet->timestamp)));
+       fr_dcursor_append(out, vb);
+
+       cache_release(inst, request, &handle);
+
+       return XLAT_ACTION_DONE;
+}
+
 /** Release the allocated resources and cleanup the avps
  */
 static void cache_unref(request_t *request, rlm_cache_t const *inst, rlm_cache_entry_t *entry,
@@ -1384,6 +1421,9 @@ static int mod_bootstrap(module_inst_ctx_t const *mctx)
        xlat_func_args_set(xlat, cache_xlat_args);
        xlat_func_call_env_set(xlat, &cache_method_env);
 
+       xlat = xlat_func_register_module(inst, mctx, "ttl.get", cache_ttl_get_xlat, FR_TYPE_VOID);
+       xlat_func_call_env_set(xlat, &cache_method_env);
+
        return 0;
 }
 
index 721af1db30fc07569a16389fe20f2ca6de9b6496..eae4648a1f90fc82aaefbe6ccbdcffd27bc5d1aa 100644 (file)
@@ -9,7 +9,12 @@ if (!ok) {
         test_fail
 }
 
-&request.Login-LAT-Service := %cache(request.Callback-Id)
+# Check the cache TTL function works
+if (%cache.ttl.get() < 4) {
+        test_fail
+}
+
+&request.Login-LAT-Service := %cache('request.Callback-Id')
 
 if (&Login-LAT-Service != &control.Callback-Id) {
         test_fail