check for request->packet. Closes #1935
[freeradius.git] / src / modules / rlm_cache / rlm_cache.c
index 8437a3d..248de8b 100644 (file)
@@ -49,8 +49,7 @@ static const CONF_PARSER module_config[] = {
        /* Should be a type which matches time_t, @fixme before 2038 */
        { "epoch", FR_CONF_OFFSET(PW_TYPE_SIGNED, rlm_cache_t, epoch), "0" },
        { "add_stats", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, rlm_cache_t, stats), "no" },
-
-       { NULL, -1, 0, NULL, NULL }             /* end the list */
+       CONF_PARSER_TERMINATOR
 };
 
 static int cache_acquire(rlm_cache_handle_t **out, rlm_cache_t *inst, REQUEST *request)
@@ -119,7 +118,7 @@ static void CC_HINT(nonnull) cache_merge(rlm_cache_t *inst, REQUEST *request, rl
 {
        VALUE_PAIR *vp;
 
-       vp = pairfind(request->config, PW_CACHE_MERGE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_CACHE_MERGE, 0, TAG_ANY);
        if (vp && (vp->vp_integer == 0)) {
                RDEBUG2("Told not to merge entry into request");
                return;
@@ -129,31 +128,32 @@ static void CC_HINT(nonnull) cache_merge(rlm_cache_t *inst, REQUEST *request, rl
 
        if (c->packet && request->packet) {
                rdebug_pair_list(L_DBG_LVL_2, request, c->packet, "&request:");
-               radius_pairmove(request, &request->packet->vps, paircopy(request->packet, c->packet), false);
+               radius_pairmove(request, &request->packet->vps, fr_pair_list_copy(request->packet, c->packet), false);
        }
 
        if (c->reply && request->reply) {
                rdebug_pair_list(L_DBG_LVL_2, request, c->reply, "&reply:");
-               radius_pairmove(request, &request->reply->vps, paircopy(request->reply, c->reply), false);
+               radius_pairmove(request, &request->reply->vps, fr_pair_list_copy(request->reply, c->reply), false);
        }
 
        if (c->control) {
                rdebug_pair_list(L_DBG_LVL_2, request, c->control, "&control:");
-               radius_pairmove(request, &request->config, paircopy(request, c->control), false);
+               radius_pairmove(request, &request->config, fr_pair_list_copy(request, c->control), false);
        }
 
        if (c->state) {
                rdebug_pair_list(L_DBG_LVL_2, request, c->state, "&session-state:");
-               radius_pairmove(request, &request->state, paircopy(request->state, c->state), false);
+
+               fr_pair_list_mcopy_by_num(request->state_ctx, &request->state, &c->state, 0, 0, TAG_ANY);
        }
 
        if (inst->stats) {
                rad_assert(request->packet != NULL);
-               vp = pairfind(request->packet->vps, PW_CACHE_ENTRY_HITS, 0, TAG_ANY);
+               vp = fr_pair_find_by_num(request->packet->vps, PW_CACHE_ENTRY_HITS, 0, TAG_ANY);
                if (!vp) {
-                       vp = paircreate(request->packet, PW_CACHE_ENTRY_HITS, 0);
+                       vp = fr_pair_afrom_num(request->packet, PW_CACHE_ENTRY_HITS, 0);
                        rad_assert(vp != NULL);
-                       pairadd(&request->packet->vps, vp);
+                       fr_pair_add(&request->packet->vps, vp);
                }
                vp->vp_integer = c->hits;
        }
@@ -246,7 +246,7 @@ static rlm_rcode_t cache_insert(rlm_cache_t *inst, REQUEST *request, rlm_cache_h
        VALUE_PAIR *vp, *to_cache;
        vp_cursor_t src_list, packet, reply, control, state;
 
-       value_pair_map_t const *map;
+       vp_map_t const *map;
 
        bool merge = true;
        rlm_cache_entry_t *c;
@@ -274,14 +274,11 @@ static rlm_rcode_t cache_insert(rlm_cache_t *inst, REQUEST *request, rlm_cache_h
        for (map = inst->maps; map != NULL; map = map->next) {
                rad_assert(map->lhs && map->rhs);
 
-               if (map_to_vp(&to_cache, request, map, NULL) < 0) {
+               if (map_to_vp(c, &to_cache, request, map, NULL) < 0) {
                        RDEBUG("Skipping %s", map->rhs->name);
                        continue;
                }
 
-               /*
-                *      Reparent the VPs map_to_vp may return multiple.
-                */
                for (vp = fr_cursor_init(&src_list, &to_cache);
                     vp;
                     vp = fr_cursor_next(&src_list)) {
@@ -307,7 +304,6 @@ static rlm_rcode_t cache_insert(rlm_cache_t *inst, REQUEST *request, rlm_cache_h
                        RINDENT();
                        if (RDEBUG_ENABLED2) map_debug_log(request, map, vp);
                        REXDENT();
-                       (void) talloc_steal(c, vp);
 
                        vp->op = map->op;
 
@@ -337,7 +333,7 @@ static rlm_rcode_t cache_insert(rlm_cache_t *inst, REQUEST *request, rlm_cache_h
        /*
         *      Check to see if we need to merge the entry into the request
         */
-       vp = pairfind(request->config, PW_CACHE_MERGE, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_CACHE_MERGE, 0, TAG_ANY);
        if (vp && (vp->vp_integer == 0)) merge = false;
 
        if (merge) cache_merge(inst, request, c);
@@ -352,7 +348,7 @@ static rlm_rcode_t cache_insert(rlm_cache_t *inst, REQUEST *request, rlm_cache_h
                        return RLM_MODULE_FAIL;
 
                case CACHE_OK:
-                       RDEBUG("Commited entry, TTL %d seconds", ttl);
+                       RDEBUG("Committed entry, TTL %d seconds", ttl);
                        cache_free(inst, &c);
                        return RLM_MODULE_UPDATED;
 
@@ -366,7 +362,7 @@ static rlm_rcode_t cache_insert(rlm_cache_t *inst, REQUEST *request, rlm_cache_h
 /** Verify that a map in the cache section makes sense
  *
  */
-static int cache_verify(value_pair_map_t *map, void *ctx)
+static int cache_verify(vp_map_t *map, void *ctx)
 {
        if (modcall_fixup_update(map, ctx) < 0) return -1;
 
@@ -462,7 +458,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_cache_it(void *instance, REQUEST *reques
         *      If Cache-Status-Only == yes, only return whether we found a
         *      valid cache entry
         */
-       vp = pairfind(request->config, PW_CACHE_STATUS_ONLY, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_CACHE_STATUS_ONLY, 0, TAG_ANY);
        if (vp && vp->vp_integer) {
                rcode = c ? RLM_MODULE_OK:
                            RLM_MODULE_NOTFOUND;
@@ -474,7 +470,7 @@ static rlm_rcode_t CC_HINT(nonnull) mod_cache_it(void *instance, REQUEST *reques
         *      A TTL of 0 means "delete from the cache".
         *      A TTL < 0 means "delete from the cache and recreate the entry".
         */
-       vp = pairfind(request->config, PW_CACHE_TTL, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_CACHE_TTL, 0, TAG_ANY);
        if (vp) ttl = vp->vp_signed;
 
        /*
@@ -520,7 +516,7 @@ insert:
         *      If Cache-Read-Only == yes, then we only allow already cached entries
         *      to be merged into the request
         */
-       vp = pairfind(request->config, PW_CACHE_READ_ONLY, 0, TAG_ANY);
+       vp = fr_pair_find_by_num(request->config, PW_CACHE_READ_ONLY, 0, TAG_ANY);
        if (vp && vp->vp_integer) {
                rcode = RLM_MODULE_NOTFOUND;
                goto finish;
@@ -626,7 +622,7 @@ static ssize_t cache_xlat(void *instance, REQUEST *request,
                goto finish;
        }
 
-       vp = pairfind(vps, target->attr, target->vendor, TAG_ANY);
+       vp = fr_pair_find_by_num(vps, target->attr, target->vendor, TAG_ANY);
        if (!vp) {
                RDEBUG("No instance of this attribute has been cached");
                *out = '\0';
@@ -676,51 +672,62 @@ static int mod_detach(void *instance)
        return 0;
 }
 
-/*
- *     Instantiate the module.
- */
-static int mod_instantiate(CONF_SECTION *conf, void *instance)
+
+static int mod_bootstrap(CONF_SECTION *conf, void *instance)
 {
        rlm_cache_t *inst = instance;
-       CONF_SECTION *update;
 
        inst->cs = conf;
 
-       inst->xlat_name = cf_section_name2(conf);
-       if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
+       inst->name = cf_section_name2(conf);
+       if (!inst->name) inst->name = cf_section_name1(conf);
 
        /*
         *      Register the cache xlat function
         */
-       xlat_register(inst->xlat_name, cache_xlat, NULL, inst);
+       xlat_register(inst->name, cache_xlat, NULL, inst);
+
+       return 0;
+}
+
+
+/*
+ *     Instantiate the module.
+ */
+static int mod_instantiate(CONF_SECTION *conf, void *instance)
+{
+       rlm_cache_t *inst = instance;
+       CONF_SECTION *update;
+
+       inst->cs = conf;
 
        /*
         *      Sanity check for crazy people.
         */
        if (strncmp(inst->driver_name, "rlm_cache_", 8) != 0) {
-               ERROR("rlm_cache (%s): \"%s\" is NOT an Cache driver!", inst->xlat_name, inst->driver_name);
+               cf_log_err_cs(conf, "\"%s\" is NOT an Cache driver!", inst->driver_name);
                return -1;
        }
 
        /*
         *      Load the appropriate driver for our database
         */
-       inst->handle = lt_dlopenext(inst->driver_name);
+       inst->handle = fr_dlopenext(inst->driver_name);
        if (!inst->handle) {
-               ERROR("rlm_cache (%s): Could not link driver %s: %s", inst->xlat_name, inst->driver_name, dlerror());
-               ERROR("rlm_cache (%s): Make sure it (and all its dependent libraries!) are in the search path"
-                     "of your system's ld", inst->xlat_name);
+               cf_log_err_cs(conf, "Could not link driver %s: %s", inst->driver_name, dlerror());
+               cf_log_err_cs(conf, "Make sure it (and all its dependent libraries!) are in the search path"
+                             " of your system's ld");
                return -1;
        }
 
        inst->module = (cache_module_t *) dlsym(inst->handle, inst->driver_name);
        if (!inst->module) {
-               ERROR("rlm_cache (%s): Could not link symbol %s: %s", inst->xlat_name, inst->driver_name, dlerror());
+               cf_log_err_cs(conf, "Could not link symbol %s: %s", inst->driver_name, dlerror());
                return -1;
        }
 
-       INFO("rlm_cache (%s): Driver %s (module %s) loaded and linked", inst->xlat_name,
-            inst->driver_name, inst->module->name);
+       DEBUG("rlm_cache (%s): Driver %s (module %s) loaded and linked", inst->name,
+             inst->driver_name, inst->module->name);
 
        /*
         *      Non optional fields and callbacks
@@ -730,7 +737,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        rad_assert(inst->module->insert);
        rad_assert(inst->module->expire);
 
-       if (inst->module->mod_instantiate) {
+       if (inst->module->instantiate) {
                CONF_SECTION *cs;
                char const *name;
 
@@ -753,7 +760,7 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
                 *      Should write its instance data in inst->driver,
                 *      and parent it off of inst.
                 */
-               if (inst->module->mod_instantiate(cs, inst) < 0) return -1;
+               if (inst->module->instantiate(cs, inst) < 0) return -1;
        }
 
        rad_assert(inst->key && *inst->key);
@@ -806,6 +813,7 @@ module_t rlm_cache = {
        .name           = "cache",
        .inst_size      = sizeof(rlm_cache_t),
        .config         = module_config,
+       .bootstrap      = mod_bootstrap,
        .instantiate    = mod_instantiate,
        .detach         = mod_detach,
        .methods = {