We don't actually copy...
[freeradius.git] / src / modules / rlm_perl / rlm_perl.c
index f5ce029..369a17b 100644 (file)
@@ -49,43 +49,48 @@ extern char **environ;
  */
 typedef struct rlm_perl_t {
        /* Name of the perl module */
-       char    *module;
+       char const      *module;
 
        /* Name of the functions for each module method */
-       char    *func_authorize;
-       char    *func_authenticate;
-       char    *func_accounting;
-       char    *func_start_accounting;
-       char    *func_stop_accounting;
-       char    *func_preacct;
-       char    *func_checksimul;
-       char    *func_detach;
-       char    *func_xlat;
+       char const      *func_authorize;
+       char const      *func_authenticate;
+       char const      *func_accounting;
+       char const      *func_start_accounting;
+       char const      *func_stop_accounting;
+       char const      *func_preacct;
+       char const      *func_checksimul;
+       char const      *func_detach;
+       char const      *func_xlat;
 #ifdef WITH_PROXY
-       char    *func_pre_proxy;
-       char    *func_post_proxy;
+       char const      *func_pre_proxy;
+       char const      *func_post_proxy;
 #endif
-       char    *func_post_auth;
+       char const      *func_post_auth;
 #ifdef WITH_COA
-       char    *func_recv_coa;
-       char    *func_send_coa;
+       char const      *func_recv_coa;
+       char const      *func_send_coa;
 #endif
-       char    *xlat_name;
-       char    *perl_flags;
-       PerlInterpreter *perl;
+       char const      *xlat_name;
+       char const      *perl_flags;
+       PerlInterpreter *perl;
        pthread_key_t   *thread_key;
 
-       pthread_mutex_t clone_mutex;
+#ifdef USE_ITHREADS
+       pthread_mutex_t clone_mutex;
+#endif
+
+       HV              *rad_perlconf_hv;       //!< holds "config" items (perl %RAD_PERLCONF hash).
+
 } rlm_perl_t;
 /*
  *     A mapping of configuration file names to internal variables.
  */
-#define RLM_PERL_CONF(_x) { "func_" Stringify(_x), PW_TYPE_STRING_PTR, \
-                       offsetof(rlm_perl_t,func_##_x), NULL, Stringify(_x)}
+#define RLM_PERL_CONF(_x) { "func_" STRINGIFY(_x), PW_TYPE_STRING, \
+                       offsetof(rlm_perl_t,func_##_x), NULL, STRINGIFY(_x)}
 
 static const CONF_PARSER module_config[] = {
-       { "module",  PW_TYPE_FILENAME,
-         offsetof(rlm_perl_t,module), NULL,  "module"},
+       { "module", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_DEPRECATED, rlm_perl_t, module), NULL },
+       { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_INPUT | PW_TYPE_REQUIRED, rlm_perl_t, module), NULL },
 
        RLM_PERL_CONF(authorize),
        RLM_PERL_CONF(authenticate),
@@ -104,14 +109,11 @@ static const CONF_PARSER module_config[] = {
        RLM_PERL_CONF(recv_coa),
        RLM_PERL_CONF(send_coa),
 #endif
-       { "perl_flags", PW_TYPE_STRING_PTR,
-         offsetof(rlm_perl_t,perl_flags), NULL, NULL},
+       { "perl_flags", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_perl_t, perl_flags), NULL },
 
-       { "func_start_accounting", PW_TYPE_STRING_PTR,
-         offsetof(rlm_perl_t,func_start_accounting), NULL, NULL},
+       { "func_start_accounting", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_perl_t, func_start_accounting), NULL },
 
-       { "func_stop_accounting", PW_TYPE_STRING_PTR,
-         offsetof(rlm_perl_t,func_stop_accounting), NULL, NULL},
+       { "func_stop_accounting", FR_CONF_OFFSET(PW_TYPE_STRING, rlm_perl_t, func_stop_accounting), NULL },
 
        { NULL, -1, 0, NULL, NULL }             /* end the list */
 };
@@ -126,7 +128,7 @@ EXTERN_C void boot_DynaLoader(pTHX_ CV* cv);
 #define dl_modules "DynaLoader::dl_modules"
 static void rlm_perl_clear_handles(pTHX)
 {
-       AV *librefs = get_av(dl_librefs, FALSE);
+       AV *librefs = get_av(dl_librefs, false);
        if (librefs) {
                av_clear(librefs);
        }
@@ -135,8 +137,8 @@ static void rlm_perl_clear_handles(pTHX)
 static void **rlm_perl_get_handles(pTHX)
 {
        I32 i;
-       AV *librefs = get_av(dl_librefs, FALSE);
-       AV *modules = get_av(dl_modules, FALSE);
+       AV *librefs = get_av(dl_librefs, false);
+       AV *modules = get_av(dl_modules, false);
        void **handles;
 
        if (!librefs) return NULL;
@@ -149,7 +151,7 @@ static void **rlm_perl_get_handles(pTHX)
 
        for (i=0; i<=AvFILL(librefs); i++) {
                void *handle;
-               SV *handle_sv = *av_fetch(librefs, i, FALSE);
+               SV *handle_sv = *av_fetch(librefs, i, false);
 
                if(!handle_sv) {
                        ERROR("Could not fetch $%s[%d]!\n",
@@ -187,6 +189,7 @@ static void rlm_perl_close_handles(void **handles)
        free(handles);
 }
 
+DIAG_OFF(shadow)
 static void rlm_perl_destruct(PerlInterpreter *perl)
 {
        dTHXa(perl);
@@ -197,11 +200,10 @@ static void rlm_perl_destruct(PerlInterpreter *perl)
 
        PL_origenviron = environ;
 
-DIAG_OFF(shadow)
+
        {
                dTHXa(perl);
        }
-DIAG_ON(shadow)
        /*
         * FIXME: This shouldn't happen
         *
@@ -213,6 +215,7 @@ DIAG_ON(shadow)
        perl_destruct(perl);
        perl_free(perl);
 }
+DIAG_ON(shadow)
 
 static void rlm_destroy_perl(PerlInterpreter *perl)
 {
@@ -235,7 +238,7 @@ static void rlm_perl_make_key(pthread_key_t *key)
 static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key)
 {
        int ret;
-       
+
        PerlInterpreter *interp;
        UV clone_flags = 0;
 
@@ -255,12 +258,12 @@ static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key
        PL_ptr_table = NULL;
 
        PERL_SET_CONTEXT(aTHX);
-       rlm_perl_clear_handles(aTHX);
+       rlm_perl_clear_handles(aTHX);
 
        ret = pthread_setspecific(*key, interp);
        if (ret != 0) {
-               DEBUG("rlm_perl: Failed associating interpretor with thread %s", strerror(ret));
-               
+               DEBUG("rlm_perl: Failed associating interpretor with thread %s", fr_syserror(ret));
+
                rlm_perl_destruct(interp);
                return NULL;
        }
@@ -269,20 +272,10 @@ static PerlInterpreter *rlm_perl_clone(PerlInterpreter *perl, pthread_key_t *key
 }
 #endif
 
-static void xs_init(pTHX)
-{
-       const char *file = __FILE__;
-
-       /* DynaLoader is a special case */
-       newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
-
-}
 /*
- *
  * This is wrapper for radlog
  * Now users can call radiusd::radlog(level,msg) wich is the same
  * calling radlog from C code.
- * Boyan
  */
 static XS(XS_radiusd_radlog)
 {
@@ -305,49 +298,62 @@ static XS(XS_radiusd_radlog)
        XSRETURN_NO;
 }
 
+static void xs_init(pTHX)
+{
+       char const *file = __FILE__;
+
+       /* DynaLoader is a special case */
+       newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
+
+       newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl");
+}
+
 /*
  * The xlat function
  */
-static size_t perl_xlat(void *instance, REQUEST *request, const char *fmt, char *out, size_t freespace)
+static ssize_t perl_xlat(void *instance, REQUEST *request, char const *fmt, char *out, size_t freespace)
 {
 
-       rlm_perl_t      *inst= (rlm_perl_t *) instance;
-       PerlInterpreter *perl;
+       rlm_perl_t      *inst = (rlm_perl_t *) instance;
        char            *tmp;
-       const char      *p, *q;
+       char const      *p, *q;
        int             count;
        size_t          ret = 0;
        STRLEN          n_a;
 
-#ifndef WITH_ITHREADS
-       perl = inst->perl;
-#else
-       perl = rlm_perl_clone(inst->perl,inst->thread_key);
+#ifdef USE_ITHREADS
+       PerlInterpreter *interp;
+
+       pthread_mutex_lock(&inst->clone_mutex);
+       interp = rlm_perl_clone(inst->perl, inst->thread_key);
        {
-               dTHXa(perl);
+               dTHXa(interp);
+               PERL_SET_CONTEXT(interp);
        }
+       pthread_mutex_unlock(&inst->clone_mutex);
+#else
+       PERL_SET_CONTEXT(inst->perl);
 #endif
-       PERL_SET_CONTEXT(perl);
        {
                dSP;
                ENTER;SAVETMPS;
 
                PUSHMARK(SP);
-               
+
                p = fmt;
                while ((q = strchr(p, ' '))) {
                        XPUSHs(sv_2mortal(newSVpv(p, p - q)));
-                       
+
                        p = q + 1;
                }
-               
+
                PUTBACK;
 
                count = call_pv(inst->func_xlat, G_SCALAR | G_EVAL);
 
                SPAGAIN;
                if (SvTRUE(ERRSV)) {
-                       RDEBUGE("Exit %s", SvPV(ERRSV,n_a));
+                       REDEBUG("Exit %s", SvPV(ERRSV,n_a));
                        (void)POPs;
                } else if (count > 0) {
                        tmp = POPp;
@@ -362,9 +368,77 @@ static size_t perl_xlat(void *instance, REQUEST *request, const char *fmt, char
                LEAVE ;
 
        }
-       
+
        return ret;
 }
+
+/*
+ *     Parse a configuration section, and populate a HV.
+ *     This function is recursively called (allows to have nested hashes.)
+ */
+static void perl_parse_config(CONF_SECTION *cs, int lvl, HV *rad_hv)
+{
+       if (!cs || !rad_hv) return;
+
+       int indent_section = (lvl + 1) * 4;
+       int indent_item = (lvl + 2) * 4;
+
+       DEBUG("%*s%s {", indent_section, " ", cf_section_name1(cs));
+
+       CONF_ITEM *ci;
+
+       for (ci = cf_item_find_next(cs, NULL);
+            ci;
+            ci = cf_item_find_next(cs, ci)) {
+               /*
+                *  This is a section.
+                *  Create a new HV, store it as a reference in current HV,
+                *  Then recursively call perl_parse_config with this section and the new HV.
+                */
+               if (cf_item_is_section(ci)) {
+                       CONF_SECTION    *sub_cs = cf_itemtosection(ci);
+                       char const      *key = cf_section_name1(sub_cs); /* hash key */
+                       HV              *sub_hv;
+                       SV              *ref;
+
+                       if (!key) continue;
+
+                       if (hv_exists(rad_hv, key, strlen(key))) {
+                               WARN("rlm_perl: Ignoring duplicate config section '%s'", key);
+                               continue;
+                       }
+
+                       sub_hv = newHV();
+                       ref = newRV_inc((SV*) sub_hv);
+
+                       (void)hv_store(rad_hv, key, strlen(key), ref, 0);
+
+                       perl_parse_config(sub_cs, lvl + 1, sub_hv);
+               } else if (cf_item_is_pair(ci)){
+                       CONF_PAIR       *cp = cf_itemtopair(ci);
+                       char const      *key = cf_pair_attr(cp);        /* hash key */
+                       char const      *value = cf_pair_value(cp);     /* hash value */
+
+                       if (!key || !value) continue;
+
+                       /*
+                        *  This is an item.
+                        *  Store item attr / value in current HV.
+                        */
+                       if (hv_exists(rad_hv, key, strlen(key))) {
+                               WARN("rlm_perl: Ignoring duplicate config item '%s'", key);
+                               continue;
+                       }
+
+                       (void)hv_store(rad_hv, key, strlen(key), newSVpv(value, strlen(value)), 0);
+
+                       DEBUG("%*s%s = %s", indent_item, " ", key, value);
+               }
+       }
+
+       DEBUG("%*s}", indent_section, " ");
+}
+
 /*
  *     Do any per-module initialization that is separate to each
  *     configured instance of the module.  e.g. set up connections
@@ -375,7 +449,6 @@ static size_t perl_xlat(void *instance, REQUEST *request, const char *fmt, char
  *     that must be referenced in later calls, store a handle to it
  *     in *instance otherwise put a null pointer there.
  *
- *     Boyan:
  *     Setup a hashes wich we will use later
  *     parse a module and give him a chance to live
  *
@@ -385,13 +458,14 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        rlm_perl_t       *inst = instance;
        AV              *end_AV;
 
+       char const **embed_c;   /* Stupid Perl and lack of const consistency */
        char **embed;
        char **envp = NULL;
-       const char *xlat_name;
+       char const *xlat_name;
        int exitstatus = 0, argc=0;
 
-       MEM(embed = talloc_zero_array(inst, char *, 4));
-
+       MEM(embed_c = talloc_zero_array(inst, char const *, 4));
+       memcpy(&embed, &embed_c, sizeof(embed));
        /*
         *      Create pthread key. This key will be stored in instance
         */
@@ -401,21 +475,21 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
 
        inst->thread_key = rad_malloc(sizeof(*inst->thread_key));
        memset(inst->thread_key,0,sizeof(*inst->thread_key));
-       
+
        rlm_perl_make_key(inst->thread_key);
 #endif
 
        char arg[] = "0";
-       
-       embed[0] = NULL;
+
+       embed_c[0] = NULL;
        if (inst->perl_flags) {
-               embed[1] = inst->perl_flags;
-               embed[2] = inst->module;
-               embed[3] = arg;
+               embed_c[1] = inst->perl_flags;
+               embed_c[2] = inst->module;
+               embed_c[3] = arg;
                argc = 4;
        } else {
-               embed[1] = inst->module;
-               embed[2] = arg;
+               embed_c[1] = inst->module;
+               embed_c[2] = arg;
                argc = 3;
        }
 
@@ -446,10 +520,8 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
        end_AV = PL_endav;
        PL_endav = Nullav;
 
-       newXS("radiusd::radlog",XS_radiusd_radlog, "rlm_perl");
-
        if(!exitstatus) {
-               exitstatus = perl_run(inst->perl);
+               perl_run(inst->perl);
        } else {
                ERROR("rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
                return (-1);
@@ -464,6 +536,18 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
                xlat_register(xlat_name, perl_xlat, NULL, inst);
        }
 
+       /* parse perl configuration sub-section */
+       CONF_SECTION *cs;
+       cs = cf_section_sub_find(conf, "config");
+       if (cs) {
+               DEBUG("rlm_perl (%s): parsing 'config' section...", xlat_name);
+
+               inst->rad_perlconf_hv = get_hv("RAD_PERLCONF",1);
+               perl_parse_config(cs, 0, inst->rad_perlconf_hv);
+
+               DEBUG("rlm_perl (%s): done parsing 'config'.", xlat_name);
+       }
+
        return 0;
 }
 
@@ -473,71 +557,83 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
  *     Example for this is Cisco-AVPair that holds multiple values.
  *     Which will be available as array_ref in $RAD_REQUEST{'Cisco-AVPair'}
  */
-static void perl_store_vps(TALLOC_CTX *ctx, VALUE_PAIR *vps, HV *rad_hv)
+static void perl_store_vps(UNUSED TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR *vps, HV *rad_hv)
 {
-       VALUE_PAIR *head, *sublist;
-       AV *av;
-       const char *name;
-       char namebuf[256];
-       char buffer[1024];
-       int len;
+       VALUE_PAIR *vp;
 
        hv_undef(rad_hv);
-       
-       /*
-        *      Copy the valuepair list so we can remove attributes
-        *      we've already processed.  This is a horrible hack to
-        *      get around various other stupidity.
-        */
-       head = paircopy(ctx, vps);
 
-       while (head) {
+       vp_cursor_t cursor;
+
+       pairsort(&vps, attrtagcmp);
+       for (vp = fr_cursor_init(&cursor, &vps);
+            vp;
+            vp = fr_cursor_next(&cursor)) {
+               VALUE_PAIR *next;
+
+               char const *name;
+               char namebuf[256];
+               char buffer[1024];
+
+               size_t len;
+
                /*
                 *      Tagged attributes are added to the hash with name
                 *      <attribute>:<tag>, others just use the normal attribute
                 *      name as the key.
                 */
-               if (head->da->flags.has_tag && (head->tag != 0)) {
-                       snprintf(namebuf, sizeof(namebuf), "%s:%d",
-                                head->da->name, head->tag);
+               if (vp->da->flags.has_tag && (vp->tag != TAG_ANY)) {
+                       snprintf(namebuf, sizeof(namebuf), "%s:%d", vp->da->name, vp->tag);
                        name = namebuf;
                } else {
-                       name = head->da->name;
+                       name = vp->da->name;
                }
 
                /*
-                *      Create a new list with all the attributes like this one
-                *      which are in the same tag group.
+                *      We've sorted by type, then tag, so attributes of the
+                *      same type/tag should follow on from each other.
                 */
-               sublist = NULL;
-               pairfilter(ctx, &sublist, &head, head->da->attr, head->da->vendor, head->tag);
-
-               /*
-                *      Attribute has multiple values
-                */
-               if (sublist->next) {
-                       VALUE_PAIR *vp;
+               if ((next = fr_cursor_next_peek(&cursor)) && ATTRIBUTE_EQ(vp, next)) {
+                       AV *av;
 
                        av = newAV();
-                       for (vp = sublist; vp; vp = vp->next) {
-                               len = vp_prints_value(buffer, sizeof(buffer), vp, FALSE);
-                               av_push(av, newSVpv(buffer, len));
+                       for (next = fr_cursor_first(&cursor);
+                            next;
+                            next = fr_cursor_next_by_da(&cursor, vp->da, vp->tag)) {
+                               switch (vp->da->type) {
+                               case PW_TYPE_STRING:
+                                       av_push(av, newSVpv(next->vp_strvalue, next->length));
+                                       RDEBUG("<--  %s = %s", next->da->name, next->vp_strvalue);
+                                       break;
+
+                               default:
+                                       len = vp_prints_value(buffer, sizeof(buffer), next, 0);
+                                       RDEBUG("<--  %s = %s", next->da->name, buffer);
+                                       av_push(av, newSVpv(buffer, truncate_len(len, sizeof(buffer))));
+                                       break;
+                               }
                        }
                        (void)hv_store(rad_hv, name, strlen(name), newRV_noinc((SV *)av), 0);
-               
-                       /*
-                        *      Attribute has a single value, so its value just gets
-                        *      added to the hash.
-                        */
-               } else {
-                       len = vp_prints_value(buffer, sizeof(buffer), sublist, FALSE);
-                       (void)hv_store(rad_hv, name, strlen(name), newSVpv(buffer, len), 0);
+
+                       continue;
                }
 
-               pairfree(&sublist);
+               /*
+                *      It's a normal single valued attribute
+                */
+               switch (vp->da->type) {
+               case PW_TYPE_STRING:
+                       RDEBUG("<--  %s = %s", next->da->name, next->vp_strvalue);
+                       (void)hv_store(rad_hv, name, strlen(name), newSVpv(vp->vp_strvalue, vp->length), 0);
+                       break;
+
+               default:
+                       len = vp_prints_value(buffer, sizeof(buffer), next, 0);
+                       RDEBUG("<--  %s = %s", next->da->name, buffer);
+                       (void)hv_store(rad_hv, name, strlen(name), newSVpv(buffer, truncate_len(len, sizeof(buffer))), 0);
+                       break;
+               }
        }
-
-       rad_assert(!head);
 }
 
 /*
@@ -546,29 +642,37 @@ static void perl_store_vps(TALLOC_CTX *ctx, VALUE_PAIR *vps, HV *rad_hv)
  *     Value Pair Format
  *
  */
-static int pairadd_sv(TALLOC_CTX *ctx, VALUE_PAIR **vps, char *key, SV *sv, FR_TOKEN op)
+static int pairadd_sv(TALLOC_CTX *ctx, REQUEST *request, VALUE_PAIR **vps, char *key, SV *sv, FR_TOKEN op)
 {
        char        *val;
        VALUE_PAIR      *vp;
 
        if (SvOK(sv)) {
-               val = SvPV_nolen(sv);
-               vp = pairmake(ctx, vps, key, val, op);
-               if (vp != NULL) {
-                       DEBUG("rlm_perl: Added pair %s = %s", key, val);
-                       return 1;
+               STRLEN len;
+               val = SvPV(sv, len);
+               vp = pairmake(ctx, vps, key, NULL, op);
+               if (!vp) {
+               fail:
+                       REDEBUG("Failed to create pair %s = %s", key, val);
+                       return 0;
+               }
+
+               if (vp->da->type != PW_TYPE_STRING) {
+                       if (pairparsevalue(vp, val, 0) < 0) goto fail;
                } else {
-                       DEBUG("rlm_perl: ERROR: Failed to create pair %s = %s", key, val);
+                       pairstrncpy(vp, val, len);
                }
+
+               RDEBUG("-->  %s = %s", key, val);
+               return 1;
        }
        return 0;
 }
 
 /*
- *     Boyan :
  *     Gets the content from hashes
  */
-static int get_hv_content(TALLOC_CTX *ctx, HV *my_hv, VALUE_PAIR **vps)
+static int get_hv_content(TALLOC_CTX *ctx, REQUEST *request, HV *my_hv, VALUE_PAIR **vps)
 {
        SV              *res_sv, **av_sv;
        AV              *av;
@@ -584,9 +688,9 @@ static int get_hv_content(TALLOC_CTX *ctx, HV *my_hv, VALUE_PAIR **vps)
                        len = av_len(av);
                        for (j = 0; j <= len; j++) {
                                av_sv = av_fetch(av, j, 0);
-                               ret = pairadd_sv(ctx, vps, key, *av_sv, T_OP_ADD) + ret;
+                               ret = pairadd_sv(ctx, request, vps, key, *av_sv, T_OP_ADD) + ret;
                        }
-               } else ret = pairadd_sv(ctx, vps, key, res_sv, T_OP_EQ) + ret;
+               } else ret = pairadd_sv(ctx, request, vps, key, res_sv, T_OP_EQ) + ret;
        }
 
        return ret;
@@ -597,7 +701,7 @@ static int get_hv_content(TALLOC_CTX *ctx, HV *my_hv, VALUE_PAIR **vps)
  *     Store all vps in hashes %RAD_CHECK %RAD_REPLY %RAD_REQUEST
  *
  */
-static int do_perl(void *instance, REQUEST *request, char *function_name)
+static int do_perl(void *instance, REQUEST *request, char const *function_name)
 {
 
        rlm_perl_t      *inst = instance;
@@ -613,7 +717,7 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
        HV              *rad_request_proxy_hv;
        HV              *rad_request_proxy_reply_hv;
 #endif
-       
+
        /*
         *      Radius has told us to call this function, but none
         *      is defined.
@@ -630,7 +734,7 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
                dTHXa(interp);
                PERL_SET_CONTEXT(interp);
        }
-       
+
        pthread_mutex_unlock(&inst->clone_mutex);
 #else
        PERL_SET_CONTEXT(inst->perl);
@@ -647,23 +751,23 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
                rad_config_hv = get_hv("RAD_CONFIG",1);
                rad_request_hv = get_hv("RAD_REQUEST",1);
 
-               perl_store_vps(request->reply, request->reply->vps, rad_reply_hv);
-               perl_store_vps(request, request->config_items, rad_check_hv);
-               perl_store_vps(request->packet, request->packet->vps, rad_request_hv);
-               perl_store_vps(request, request->config_items, rad_config_hv);
+               perl_store_vps(request->reply, request, request->reply->vps, rad_reply_hv);
+               perl_store_vps(request, request, request->config_items, rad_check_hv);
+               perl_store_vps(request->packet, request, request->packet->vps, rad_request_hv);
+               perl_store_vps(request, request, request->config_items, rad_config_hv);
 
 #ifdef WITH_PROXY
                rad_request_proxy_hv = get_hv("RAD_REQUEST_PROXY",1);
                rad_request_proxy_reply_hv = get_hv("RAD_REQUEST_PROXY_REPLY",1);
 
                if (request->proxy != NULL) {
-                       perl_store_vps(request->proxy, request->proxy->vps, rad_request_proxy_hv);
+                       perl_store_vps(request->proxy, request, request->proxy->vps, rad_request_proxy_hv);
                } else {
                        hv_undef(rad_request_proxy_hv);
                }
 
                if (request->proxy_reply !=NULL) {
-                       perl_store_vps(request->proxy_reply, request->proxy_reply->vps, rad_request_proxy_reply_hv);
+                       perl_store_vps(request->proxy_reply, request, request->proxy_reply->vps, rad_request_proxy_reply_hv);
                } else {
                        hv_undef(rad_request_proxy_reply_hv);
                }
@@ -702,7 +806,7 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
                LEAVE;
 
                vp = NULL;
-               if ((get_hv_content(request->packet, rad_request_hv, &vp)) > 0 ) {
+               if ((get_hv_content(request->packet, request, rad_request_hv, &vp)) > 0 ) {
                        pairfree(&request->packet->vps);
                        request->packet->vps = vp;
                        vp = NULL;
@@ -716,13 +820,13 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
                                request->password = pairfind(request->packet->vps, PW_CHAP_PASSWORD, 0, TAG_ANY);
                }
 
-               if ((get_hv_content(request->reply, rad_reply_hv, &vp)) > 0 ) {
+               if ((get_hv_content(request->reply, request, rad_reply_hv, &vp)) > 0 ) {
                        pairfree(&request->reply->vps);
                        request->reply->vps = vp;
                        vp = NULL;
                }
 
-               if ((get_hv_content(request, rad_check_hv, &vp)) > 0 ) {
+               if ((get_hv_content(request, request, rad_check_hv, &vp)) > 0 ) {
                        pairfree(&request->config_items);
                        request->config_items = vp;
                        vp = NULL;
@@ -730,14 +834,14 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
 
 #ifdef WITH_PROXY
                if (request->proxy &&
-                   (get_hv_content(request->proxy, rad_request_proxy_hv, &vp) > 0)) {
+                   (get_hv_content(request->proxy, request, rad_request_proxy_hv, &vp) > 0)) {
                        pairfree(&request->proxy->vps);
                        request->proxy->vps = vp;
                        vp = NULL;
                }
 
                if (request->proxy_reply &&
-                   (get_hv_content(request->proxy_reply, rad_request_proxy_reply_hv, &vp) > 0)) {
+                   (get_hv_content(request->proxy_reply, request, rad_request_proxy_reply_hv, &vp) > 0)) {
                        pairfree(&request->proxy_reply->vps);
                        request->proxy_reply->vps = vp;
                        vp = NULL;
@@ -748,7 +852,7 @@ static int do_perl(void *instance, REQUEST *request, char *function_name)
        return exitstatus;
 }
 
-#define RLM_PERL_FUNC(_x) static rlm_rcode_t mod_##_x(void *instance, REQUEST *request) \
+#define RLM_PERL_FUNC(_x) static rlm_rcode_t CC_HINT(nonnull) mod_##_x(void *instance, REQUEST *request) \
        {                                                               \
                return do_perl(instance, request,                       \
                               ((rlm_perl_t *)instance)->func_##_x); \
@@ -775,7 +879,7 @@ RLM_PERL_FUNC(preacct)
 /*
  *     Write accounting information to this modules database.
  */
-static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
+static rlm_rcode_t CC_HINT(nonnull) mod_accounting(void *instance, REQUEST *request)
 {
        VALUE_PAIR      *pair;
        int             acctstatustype=0;
@@ -821,11 +925,14 @@ static rlm_rcode_t mod_accounting(void *instance, REQUEST *request)
 /*
  * Detach a instance give a chance to a module to make some internal setup ...
  */
+DIAG_OFF(nested-externs)
 static int mod_detach(void *instance)
 {
        rlm_perl_t      *inst = (rlm_perl_t *) instance;
        int             exitstatus = 0, count = 0;
 
+       hv_undef(inst->rad_perlconf_hv);
+
 #if 0
        /*
         *      FIXME: Call this in the destruct function?
@@ -888,7 +995,7 @@ static int mod_detach(void *instance)
        PERL_SYS_TERM();
        return exitstatus;
 }
-
+DIAG_ON(nested-externs)
 
 /*
  *     The module name should be the only globally exported symbol.