Require that the modules call talloc for their instance handle.
authorAlan T. DeKok <aland@freeradius.org>
Tue, 19 Feb 2013 02:40:49 +0000 (21:40 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 19 Feb 2013 22:04:56 +0000 (17:04 -0500)
The server builds, but it WILL have talloc/malloc conflicts, as
the modules have not been updated.

The assumption here is that the modules return an instance handle,
EVEN IF they haev an error.  The server core will now take care
of calling the appropriate detach() method.

47 files changed:
src/main/modules.c
src/modules/rlm_always/rlm_always.c
src/modules/rlm_attr_filter/rlm_attr_filter.c
src/modules/rlm_attr_rewrite/rlm_attr_rewrite.c
src/modules/rlm_cache/rlm_cache.c
src/modules/rlm_checkval/rlm_checkval.c
src/modules/rlm_counter/rlm_counter.c
src/modules/rlm_dbm/rlm_dbm.c
src/modules/rlm_detail/rlm_detail.c
src/modules/rlm_dhcp/rlm_dhcp.c
src/modules/rlm_eap/rlm_eap.c
src/modules/rlm_eap2/rlm_eap2.c
src/modules/rlm_example/rlm_example.c
src/modules/rlm_exec/rlm_exec.c
src/modules/rlm_expiration/rlm_expiration.c
src/modules/rlm_expr/rlm_expr.c
src/modules/rlm_files/rlm_files.c
src/modules/rlm_ippool/rlm_ippool.c
src/modules/rlm_krb5/rlm_krb5.c
src/modules/rlm_ldap/rlm_ldap.c
src/modules/rlm_linelog/rlm_linelog.c
src/modules/rlm_logintime/rlm_logintime.c
src/modules/rlm_mschap/rlm_mschap.c
src/modules/rlm_otp/rlm_otp.c
src/modules/rlm_pam/rlm_pam.c
src/modules/rlm_pap/rlm_pap.c
src/modules/rlm_passwd/rlm_passwd.c
src/modules/rlm_perl/rlm_perl.c
src/modules/rlm_preprocess/rlm_preprocess.c
src/modules/rlm_python/rlm_python.c
src/modules/rlm_radutmp/rlm_radutmp.c
src/modules/rlm_realm/rlm_realm.c
src/modules/rlm_redis/rlm_redis.c
src/modules/rlm_rediswho/rlm_rediswho.c
src/modules/rlm_rest/rlm_rest.c
src/modules/rlm_ruby/rlm_ruby.c
src/modules/rlm_securid/rlm_securid.c
src/modules/rlm_sim_files/rlm_sim_files.c
src/modules/rlm_smsotp/rlm_smsotp.c
src/modules/rlm_soh/rlm_soh.c
src/modules/rlm_sometimes/rlm_sometimes.c
src/modules/rlm_sql/rlm_sql.c
src/modules/rlm_sqlcounter/rlm_sqlcounter.c
src/modules/rlm_sqlhpwippool/rlm_sqlhpwippool.c
src/modules/rlm_sqlippool/rlm_sqlippool.c
src/modules/rlm_unix/rlm_unix.c
src/modules/rlm_wimax/rlm_wimax.c

index a8fdb2e..9ca7efa 100644 (file)
@@ -397,16 +397,7 @@ static void module_instance_free_old(UNUSED CONF_SECTION *cs, module_instance_t
                        continue;
                }
 
-               cf_section_parse_free(cs, mh->insthandle);
-               
-               if (node->entry->module->detach) {
-                       if ((node->entry->module->detach)(mh->insthandle) < 0) {
-                               DEBUGW("Failed detaching module %s cleanly.  Doing forcible shutdown", node->name);
-
-                       }
-               } else {
-                       talloc_free(mh->insthandle);
-               }
+               talloc_free(mh->insthandle);
 
                *last = mh->next;
                talloc_free(mh);
@@ -423,12 +414,6 @@ static void module_instance_free(void *data)
 
        module_instance_free_old(this->cs, this, time(NULL) + 100);
 
-       cf_section_parse_free(this->cs, this->insthandle);
-
-       if (this->entry->module->detach) {
-               (this->entry->module->detach)(this->insthandle);
-       }
-
 #ifdef HAVE_PTHREAD_H
        if (this->mutex) {
                /*
@@ -459,13 +444,14 @@ static int module_entry_cmp(const void *one, const void *two)
 /*
  *     Free a module entry.
  */
-static void module_entry_free(void *data)
+static int module_entry_free(void *ctx)
 {
-       module_entry_t *this = data;
+       module_entry_t *this;
+
+       this = talloc_get_type_abort(ctx, module_entry_t);
 
        lt_dlclose(this->handle);       /* ignore any errors */
-       memset(this, 0, sizeof(*this));
-       talloc_free(this);
+       return 0;
 }
 
 
@@ -564,6 +550,7 @@ static module_entry_t *linkto_module(const char *module_name,
 
        /* make room for the module type */
        node = talloc_zero(cs, module_entry_t);
+       talloc_set_destructor((void *) node, module_entry_free);
        strlcpy(node->name, module_name, sizeof(node->name));
        node->module = module;
        node->handle = handle;
@@ -677,10 +664,21 @@ module_instance_t *find_module_instance(CONF_SECTION *modules,
                cf_log_err(cf_sectiontoitem(cs),
                           "Instantiation failed for module \"%s\"",
                           instname);
+               if (node->entry->module->detach) {
+                       talloc_set_destructor((void *) node->insthandle,
+                                             node->entry->module->detach);
+               }
+               talloc_free(node->insthandle);
                talloc_free(node);
                return NULL;
        }
 
+       if (node->insthandle) {
+               talloc_set_destructor((void *) node->insthandle,
+                                     node->entry->module->detach);
+               talloc_steal(node, node->insthandle);
+       }
+
        /*
         *      We're done.  Fill in the rest of the data structure,
         *      and link it to the module instance list.
@@ -1513,8 +1511,7 @@ int setup_modules(int reload, CONF_SECTION *config)
                /*
                 *      Set up the internal module struct.
                 */
-               module_tree = rbtree_create(module_entry_cmp,
-                                           module_entry_free, 0);
+               module_tree = rbtree_create(module_entry_cmp, NULL, 0);
                if (!module_tree) {
                        radlog(L_ERR, "Failed to initialize modules\n");
                        return -1;
index b399320..c1bfd62 100644 (file)
@@ -41,12 +41,6 @@ typedef struct rlm_always_t {
 
 /*
  *     A mapping of configuration file names to internal variables.
- *
- *     Note that the string is dynamically allocated, so it MUST
- *     be freed.  When the configuration file parse re-reads the string,
- *     it free's the old one, and strdup's the new one, placing the pointer
- *     to the strdup'd string into 'config.string'.  This gets around
- *     buffer over-flows.
  */
 static const CONF_PARSER module_config[] = {
   { "rcode",      PW_TYPE_STRING_PTR, offsetof(rlm_always_t,rcode_str),
@@ -88,37 +82,32 @@ static rlm_rcode_t str2rcode(const char *s)
 
 static int always_instantiate(CONF_SECTION *conf, void **instance)
 {
-       rlm_always_t *data;
+       rlm_always_t *inst;
 
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
+       *instance = inst = talloc_zero(conf, rlm_always_t);
+       if (!inst) {
                return -1;
        }
-       memset(data, 0, sizeof(*data));
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
-       if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
+       if (cf_section_parse(conf, inst, module_config) < 0) {
                return -1;
        }
 
        /*
         *      Convert the rcode string to an int, and get rid of it
         */
-       data->rcode = str2rcode(data->rcode_str);
-       if (data->rcode == RLM_MODULE_UNKNOWN) {
-               free(data);
+       inst->rcode = str2rcode(inst->rcode_str);
+       if (inst->rcode == RLM_MODULE_UNKNOWN) {
                return -1;
        }
 
-       *instance = data;
-
        return 0;
 }
 
@@ -148,18 +137,12 @@ static rlm_rcode_t always_checksimul(void *instance, REQUEST *request)
 }
 #endif
 
-static int always_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 module_t rlm_always = {
        RLM_MODULE_INIT,
        "always",
        RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
        always_instantiate,             /* instantiation */
-       always_detach,                  /* detach */
+       NULL,                           /* detach */
        {
                always_return,          /* authentication */
                always_return,          /* authorization */
index 1ed3a41..ccd7efb 100644 (file)
@@ -39,20 +39,20 @@ RCSID("$Id$")
  *     Define a structure with the module configuration, so it can
  *     be used as the instance handle.
  */
-struct attr_filter_instance {
-       char *file;
-       char *key;
-       int relaxed;
-       PAIR_LIST *attrs;
-};
+typedef struct rlm_attr_filter {
+       char            *file;
+       char            *key;
+       int             relaxed;
+       PAIR_LIST       *attrs;
+} rlm_attr_filter_t;
 
 static const CONF_PARSER module_config[] = {
        { "file",     PW_TYPE_FILENAME,
-         offsetof(struct attr_filter_instance,file), NULL, "${raddbdir}/attrs" },
+         offsetof(rlm_attr_filter_t, file), NULL, "${raddbdir}/attrs" },
        { "key",     PW_TYPE_STRING_PTR,
-         offsetof(struct attr_filter_instance,key), NULL, "%{Realm}" },
+         offsetof(rlm_attr_filter_t, key), NULL, "%{Realm}" },
        { "relaxed",    PW_TYPE_BOOLEAN,
-               offsetof(struct attr_filter_instance,relaxed), NULL, "no" },
+         offsetof(rlm_attr_filter_t, relaxed), NULL, "no" },
        { NULL, -1, 0, NULL, NULL }
 };
 
@@ -127,9 +127,9 @@ static int attr_filter_getfile(const char *filename, PAIR_LIST **pair_list)
  */
 static int attr_filter_detach(void *instance)
 {
-       struct attr_filter_instance *inst = instance;
+       rlm_attr_filter_t *inst = instance;
        pairlist_free(&inst->attrs);
-       free(inst);
+
        return 0;
 }
 
@@ -139,27 +139,25 @@ static int attr_filter_detach(void *instance)
  */
 static int attr_filter_instantiate(CONF_SECTION *conf, void **instance)
 {
-       struct attr_filter_instance *inst;
+       rlm_attr_filter_t *inst;
        int rcode;
 
-       inst = rad_malloc(sizeof *inst);
+       *instance = inst = talloc_zero(conf, rlm_attr_filter_t);
        if (!inst) {
                return -1;
        }
-       memset(inst, 0, sizeof(*inst));
 
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               attr_filter_detach(inst);
                return -1;
        }
 
        rcode = attr_filter_getfile(inst->file, &inst->attrs);
         if (rcode != 0) {
                radlog(L_ERR, "Errors reading %s", inst->file);
-               attr_filter_detach(inst);
+
                return -1;
        }
-       *instance = inst;
+
        return 0;
 }
 
@@ -170,7 +168,7 @@ static int attr_filter_instantiate(CONF_SECTION *conf, void **instance)
 static rlm_rcode_t attr_filter_common(void *instance, REQUEST *request,
                                      RADIUS_PACKET *packet)
 {
-       struct attr_filter_instance *inst = instance;
+       rlm_attr_filter_t *inst = instance;
        VALUE_PAIR      *vp;
        VALUE_PAIR      *output;
        VALUE_PAIR      **output_tail;
index 18f8a8e..6627607 100644 (file)
@@ -37,7 +37,7 @@ RCSID("$Id$")
 #define RLM_REGEX_INPROXY 3
 #define RLM_REGEX_INPROXYREPLY 4
 
-typedef struct rlm_attr_rewrite_t {
+typedef struct rlm_attr_rewrite {
        char *attribute;        //!< The attribute to search for.
        const DICT_ATTR *da;    //!< The attribute definition.
        char *search;           //!< The pattern to search for.
@@ -59,105 +59,109 @@ typedef struct rlm_attr_rewrite_t {
 } rlm_attr_rewrite_t;
 
 static const CONF_PARSER module_config[] = {
-  { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
-  { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
-  { "searchin",  PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
-  { "replacewith", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
-  { "append", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,append),NULL, "no" },
-  { "ignore_case", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
-  { "new_attribute", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
-  { "max_matches", PW_TYPE_INTEGER, offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
+  { "attribute", PW_TYPE_STRING_PTR,
+    offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
+  { "searchfor", PW_TYPE_STRING_PTR,
+    offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
+  { "searchin",  PW_TYPE_STRING_PTR,
+    offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
+  { "replacewith", PW_TYPE_STRING_PTR,
+    offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
+  { "append", PW_TYPE_BOOLEAN,
+    offsetof(rlm_attr_rewrite_t,append),NULL, "no" },
+  { "ignore_case", PW_TYPE_BOOLEAN,
+    offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
+  { "new_attribute", PW_TYPE_BOOLEAN,
+    offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
+  { "max_matches", PW_TYPE_INTEGER,
+    offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
   { NULL, -1, 0, NULL, NULL }
 };
 
 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
 {
-       rlm_attr_rewrite_t *data;
+       rlm_attr_rewrite_t *inst;
        const DICT_ATTR *dattr;
 
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
+       *instance = inst = talloc_zero(conf, rlm_attr_rewrite_t);
+       if (!inst) {
                return -1;
        }
-       memset(data, 0, sizeof(*data));
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
-       if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
+       if (cf_section_parse(conf, inst, module_config) < 0) {
                return -1;
        }
 
        /*
         *      Discover the attribute number of the key.
         */
-       if (data->attribute == NULL) {
+       if (inst->attribute == NULL) {
                radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
                return -1;
        }
-       if (data->search == NULL || data->replace == NULL) {
+       if (inst->search == NULL || inst->replace == NULL) {
                radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
                return -1;
        }
-       data->search_len = strlen(data->search);
-       data->replace_len = strlen(data->replace);
+       inst->search_len = strlen(inst->search);
+       inst->replace_len = strlen(inst->replace);
 
-       if (data->replace_len == 0 && data->new_attr){
+       if (inst->replace_len == 0 && inst->new_attr){
                radlog(L_ERR, "rlm_attr_rewrite: replace string must not be zero length in order to create new attribute.");
                return -1;
        }
 
-       if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
+       if (inst->num_matches < 1 || inst->num_matches > MAX_STRING_LEN) {
                radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
                return -1;
        }
-       if (data->searchin_str == NULL) {
+       if (inst->searchin_str == NULL) {
                radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
-               data->searchin = RLM_REGEX_INPACKET;
+               inst->searchin = RLM_REGEX_INPACKET;
        }
        else{
-               if (strcmp(data->searchin_str, "packet") == 0)
-                       data->searchin = RLM_REGEX_INPACKET;
-               else if (strcmp(data->searchin_str, "config") == 0)
-                       data->searchin = RLM_REGEX_INCONFIG;
-               else if (strcmp(data->searchin_str, "control") == 0)
-                       data->searchin = RLM_REGEX_INCONFIG;
-               else if (strcmp(data->searchin_str, "reply") == 0)
-                       data->searchin = RLM_REGEX_INREPLY;
+               if (strcmp(inst->searchin_str, "packet") == 0)
+                       inst->searchin = RLM_REGEX_INPACKET;
+               else if (strcmp(inst->searchin_str, "config") == 0)
+                       inst->searchin = RLM_REGEX_INCONFIG;
+               else if (strcmp(inst->searchin_str, "control") == 0)
+                       inst->searchin = RLM_REGEX_INCONFIG;
+               else if (strcmp(inst->searchin_str, "reply") == 0)
+                       inst->searchin = RLM_REGEX_INREPLY;
 #ifdef WITH_PROXY
-               else if (strcmp(data->searchin_str, "proxy") == 0)
-                       data->searchin = RLM_REGEX_INPROXY;
-               else if (strcmp(data->searchin_str, "proxy_reply") == 0)
-                       data->searchin = RLM_REGEX_INPROXYREPLY;
+               else if (strcmp(inst->searchin_str, "proxy") == 0)
+                       inst->searchin = RLM_REGEX_INPROXY;
+               else if (strcmp(inst->searchin_str, "proxy_reply") == 0)
+                       inst->searchin = RLM_REGEX_INPROXYREPLY;
 #endif
                else {
                        radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
-                       data->searchin = RLM_REGEX_INPACKET;
+                       inst->searchin = RLM_REGEX_INPACKET;
                }
        }
-       dattr = dict_attrbyname(data->attribute);
+       dattr = dict_attrbyname(inst->attribute);
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
-                               data->attribute);
+                               inst->attribute);
                return -1;
        }
-       data->da = dattr;
+       inst->da = dattr;
        /* Add the module instance name */
-       data->name = cf_section_name2(conf); /* may be NULL */
-
-       *instance = data;
+       inst->name = cf_section_name2(conf); /* may be NULL */
 
        return 0;
 }
 
 static rlm_rcode_t do_attr_rewrite(void *instance, REQUEST *request)
 {
-       rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
+       rlm_attr_rewrite_t *inst = (rlm_attr_rewrite_t *) instance;
        rlm_rcode_t rcode = RLM_MODULE_NOOP;
        VALUE_PAIR *attr_vp = NULL;
        VALUE_PAIR *tmp = NULL;
@@ -177,23 +181,23 @@ static rlm_rcode_t do_attr_rewrite(void *instance, REQUEST *request)
        char replace_STR[MAX_STRING_LEN];
 
        if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE, 0, TAG_ANY)) != NULL){
-               if (data->name == NULL || strcmp(data->name,attr_vp->vp_strvalue))
+               if (inst->name == NULL || strcmp(inst->name,attr_vp->vp_strvalue))
                        return RLM_MODULE_NOOP;
        }
 
-       if (data->new_attr){
+       if (inst->new_attr){
                /* new_attribute = yes */
-               if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL, NULL)) {
-                       DEBUG2("%s: xlat on replace string failed.", data->name);
+               if (!radius_xlat(replace_STR, sizeof(replace_STR), inst->replace, request, NULL, NULL)) {
+                       DEBUG2("%s: xlat on replace string failed.", inst->name);
                        return rcode;
                }
-               attr_vp = pairmake(data->attribute,replace_STR,0);
+               attr_vp = pairmake(inst->attribute,replace_STR,0);
                if (attr_vp == NULL){
-                       DEBUG2("%s: Could not add new attribute %s with value '%s'", data->name,
-                               data->attribute,replace_STR);
+                       DEBUG2("%s: Could not add new attribute %s with value '%s'", inst->name,
+                               inst->attribute,replace_STR);
                        return rcode;
                }
-               switch(data->searchin){
+               switch(inst->searchin){
                        case RLM_REGEX_INPACKET:
                                pairadd(&request->packet->vps,attr_vp);
                                break;
@@ -220,22 +224,22 @@ static rlm_rcode_t do_attr_rewrite(void *instance, REQUEST *request)
                                break;
 #endif
                        default:
-                               radlog(L_ERR, "%s: Illegal value for searchin. Changing to packet.", data->name);
-                               data->searchin = RLM_REGEX_INPACKET;
+                               radlog(L_ERR, "%s: Illegal value for searchin. Changing to packet.", inst->name);
+                               inst->searchin = RLM_REGEX_INPACKET;
                                pairadd(&request->packet->vps,attr_vp);
                                break;
                }
-               DEBUG2("%s: Added attribute %s with value '%s'", data->name,data->attribute,replace_STR);
+               DEBUG2("%s: Added attribute %s with value '%s'", inst->name,inst->attribute,replace_STR);
                rcode = RLM_MODULE_OK;
        } else {
                int replace_len = 0;
 
                /* new_attribute = no */
-               switch (data->searchin) {
+               switch (inst->searchin) {
                        case RLM_REGEX_INPACKET:
-                               if (!data->da->vendor && (data->da->attr == PW_USER_NAME))
+                               if (!inst->da->vendor && (inst->da->attr == PW_USER_NAME))
                                        attr_vp = request->username;
-                               else if (!data->da->vendor && (data->da->attr == PW_USER_PASSWORD))
+                               else if (!inst->da->vendor && (inst->da->attr == PW_USER_PASSWORD))
                                        attr_vp = request->password;
                                else
                                        tmp = request->packet->vps;
@@ -259,34 +263,34 @@ static rlm_rcode_t do_attr_rewrite(void *instance, REQUEST *request)
                                break;
 #endif
                        default:
-                               radlog(L_ERR, "%s: Illegal value for searchin. Changing to packet.", data->name);
-                               data->searchin = RLM_REGEX_INPACKET;
-                               attr_vp = pairfind(request->packet->vps, data->da->attr, data->da->vendor, TAG_ANY);
+                               radlog(L_ERR, "%s: Illegal value for searchin. Changing to packet.", inst->name);
+                               inst->searchin = RLM_REGEX_INPACKET;
+                               attr_vp = pairfind(request->packet->vps, inst->da->attr, inst->da->vendor, TAG_ANY);
                                break;
                }
 do_again:
                if (tmp != NULL)
-                       attr_vp = pairfind(tmp, data->da->attr, data->da->vendor, TAG_ANY);
+                       attr_vp = pairfind(tmp, inst->da->attr, inst->da->vendor, TAG_ANY);
                if (attr_vp == NULL) {
-                       DEBUG2("%s: Could not find value pair for attribute %s", data->name,data->attribute);
+                       DEBUG2("%s: Could not find value pair for attribute %s", inst->name,inst->attribute);
                        return rcode;
                }
                if (attr_vp->length == 0){
-                       DEBUG2("%s: Attribute %s string value NULL or of zero length", data->name,data->attribute);
+                       DEBUG2("%s: Attribute %s string value NULL or of zero length", inst->name,inst->attribute);
                        return rcode;
                }
                cflags |= REG_EXTENDED;
-               if (data->nocase)
+               if (inst->nocase)
                        cflags |= REG_ICASE;
 
-               if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL, NULL) && data->search_len != 0) {
-                       DEBUG2("%s: xlat on search string failed.", data->name);
+               if (!radius_xlat(search_STR, sizeof(search_STR), inst->search, request, NULL, NULL) && inst->search_len != 0) {
+                       DEBUG2("%s: xlat on search string failed.", inst->name);
                        return rcode;
                }
 
                if ((err = regcomp(&preg,search_STR,cflags))) {
                        regerror(err, &preg, err_msg, MAX_STRING_LEN);
-                       DEBUG2("%s: regcomp() returned error: %s", data->name,err_msg);
+                       DEBUG2("%s: regcomp() returned error: %s", inst->name,err_msg);
                        return rcode;
                }
 
@@ -301,12 +305,12 @@ do_again:
                ptr2 = attr_vp->vp_strvalue;
                counter = 0;
 
-               for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
+               for ( i = 0 ;i < (unsigned)inst->num_matches; i++) {
                        err = regexec(&preg, ptr2, REQUEST_MAX_REGEX, pmatch, 0);
                        if (err == REG_NOMATCH) {
                                if (i == 0) {
-                                       DEBUG2("%s: Does not match: %s = %s", data->name,
-                                                       data->attribute, attr_vp->vp_strvalue);
+                                       DEBUG2("%s: Does not match: %s = %s", inst->name,
+                                                       inst->attribute, attr_vp->vp_strvalue);
                                        regfree(&preg);
                                        goto to_do_again;
                                } else
@@ -314,21 +318,21 @@ do_again:
                        }
                        if (err != 0) {
                                regfree(&preg);
-                               radlog(L_ERR, "%s: match failure for attribute %s with value '%s'", data->name,
-                                               data->attribute, attr_vp->vp_strvalue);
+                               radlog(L_ERR, "%s: match failure for attribute %s with value '%s'", inst->name,
+                                               inst->attribute, attr_vp->vp_strvalue);
                                return rcode;
                        }
                        if (pmatch[0].rm_so == -1)
                                break;
                        len = pmatch[0].rm_so;
-                       if (data->append) {
+                       if (inst->append) {
                                len = len + (pmatch[0].rm_eo - pmatch[0].rm_so);
                        }
                        counter += len;
                        if (counter >= MAX_STRING_LEN) {
                                regfree(&preg);
-                               DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", data->name,
-                                               data->attribute, attr_vp->vp_strvalue);
+                               DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", inst->name,
+                                               inst->attribute, attr_vp->vp_strvalue);
                                return rcode;
                        }
 
@@ -371,20 +375,20 @@ do_again:
                        }
 
                        if (!done_xlat){
-                               if (data->replace_len != 0 &&
-                               radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL, NULL) == 0) {
-                                       DEBUG2("%s: xlat on replace string failed.", data->name);
+                               if (inst->replace_len != 0 &&
+                               radius_xlat(replace_STR, sizeof(replace_STR), inst->replace, request, NULL, NULL) == 0) {
+                                       DEBUG2("%s: xlat on replace string failed.", inst->name);
                                        return rcode;
                                }
-                               replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
+                               replace_len = (inst->replace_len != 0) ? strlen(replace_STR) : 0;
                                done_xlat = 1;
                        }
 
                        counter += replace_len;
                        if (counter >= MAX_STRING_LEN) {
                                regfree(&preg);
-                               DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", data->name,
-                                               data->attribute, attr_vp->vp_strvalue);
+                               DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", inst->name,
+                                               inst->attribute, attr_vp->vp_strvalue);
                                return rcode;
                        }
                        if (replace_len){
@@ -397,17 +401,17 @@ do_again:
                len = strlen(ptr2) + 1;         /* We add the ending NULL */
                counter += len;
                if (counter >= MAX_STRING_LEN){
-                       DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", data->name,
-                                       data->attribute, attr_vp->vp_strvalue);
+                       DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", inst->name,
+                                       inst->attribute, attr_vp->vp_strvalue);
                        return rcode;
                }
                memcpy(ptr, ptr2, len);
                ptr[len] = '\0';
 
-               DEBUG2("%s: Changed value for attribute %s from '%s' to '%s'", data->name,
-                               data->attribute, attr_vp->vp_strvalue, new_str);
+               DEBUG2("%s: Changed value for attribute %s from '%s' to '%s'", inst->name,
+                               inst->attribute, attr_vp->vp_strvalue, new_str);
                if (!pairparsevalue(attr_vp, new_str)) {
-                       DEBUG2("%s: Could not write value '%s' into attribute %s: %s", data->name, new_str, data->attribute, fr_strerror());
+                       DEBUG2("%s: Could not write value '%s' into attribute %s: %s", inst->name, new_str, inst->attribute, fr_strerror());
                        return rcode;
                }
 
@@ -466,12 +470,6 @@ static rlm_rcode_t attr_rewrite_postauth(void *instance, REQUEST *request)
        return do_attr_rewrite(instance, request);
 }
 
-static int attr_rewrite_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 /*
  *     The module name should be the only globally exported symbol.
  *     That is, everything else should be 'static'.
@@ -484,9 +482,9 @@ static int attr_rewrite_detach(void *instance)
 module_t rlm_attr_rewrite = {
        RLM_MODULE_INIT,
        "attr_rewrite",
-       RLM_TYPE_THREAD_UNSAFE,         /* type */
+       RLM_TYPE_THREAD_UNSAFE,                 /* type */
        attr_rewrite_instantiate,               /* instantiation */
-       attr_rewrite_detach,                    /* detach */
+       NULL,                                   /* detach */
        {
                attr_rewrite_authenticate,      /* authentication */
                attr_rewrite_authorize,         /* authorization */
index a83518a..34d9db4 100644 (file)
@@ -95,11 +95,11 @@ static void cache_entry_free(void *data)
 {
        rlm_cache_entry_t *c = data;
 
-       rad_cfree(c->key);
        pairfree(&c->control);
        pairfree(&c->request);
        pairfree(&c->reply);
-       free(c);
+
+       talloc_free(c);
 }
 
 /*
@@ -270,8 +270,8 @@ static rlm_cache_entry_t *cache_add(rlm_cache_t *inst, REQUEST *request,
        vp = pairfind(request->config_items, PW_CACHE_TTL, 0, TAG_ANY);
        if (vp && (vp->vp_integer == 0)) return NULL;
 
-       c = rad_calloc(sizeof(*c));
-       c->key = strdup(key);
+       c = talloc_zero(NULL, rlm_cache_entry_t);
+       c->key = talloc_strdup(c, key);
        c->created = c->expires = request->timestamp;
 
        /*
@@ -703,17 +703,15 @@ static const CONF_PARSER module_config[] = {
 static int cache_detach(void *instance)
 {
        rlm_cache_t *inst = instance;
-
-       rad_cfree(inst->xlat_name);
        
        radius_mapfree(&inst->maps);
 
        fr_heap_delete(inst->heap);
        rbtree_free(inst->cache);
+
 #ifdef HAVE_PTHREAD_H
        pthread_mutex_destroy(&inst->cache_mutex);
 #endif
-       free(instance);
        return 0;
 }
 
@@ -723,10 +721,9 @@ static int cache_detach(void *instance)
  */
 static int cache_instantiate(CONF_SECTION *conf, void **instance)
 {
-       const char *xlat_name;
        rlm_cache_t *inst;
 
-       inst = rad_calloc(sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_cache_t);
        inst->cs = conf;
        
        /*
@@ -734,45 +731,38 @@ static int cache_instantiate(CONF_SECTION *conf, void **instance)
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
-       xlat_name = cf_section_name2(conf);
-       if (xlat_name == NULL) {
-               xlat_name = cf_section_name1(conf);
+       inst->xlat_name = cf_section_name2(conf);
+       if (!inst->xlat_name) {
+               inst->xlat_name = cf_section_name1(conf);
        }
-       
-       rad_assert(xlat_name);
 
        /*
         *      Register the cache xlat function
         */
-       inst->xlat_name = strdup(xlat_name);
-       xlat_register(xlat_name, cache_xlat, inst);
+       xlat_register(inst->xlat_name, cache_xlat, inst);
 
        if (!inst->key || !*inst->key) {
                radlog(L_ERR, "rlm_cache: You must specify a key");
-               cache_detach(inst);
                return -1;
        }
 
        if (inst->ttl == 0) {
                radlog(L_ERR, "rlm_cache: TTL must be greater than zero");
-               cache_detach(inst);
                return -1;
        }
        
        if (inst->epoch != 0){
                radlog(L_ERR, "rlm_cache: Epoch should only be set dynamically");
-               cache_detach(inst);
                return -1;
        }
 
 #ifdef HAVE_PTHREAD_H
        if (pthread_mutex_init(&inst->cache_mutex, NULL) < 0) {
-               radlog(L_ERR, "rlm_cache: Failed initializing mutex: %s", strerror(errno));
-               cache_detach(inst);
+               radlog(L_ERR, "rlm_cache: Failed initializing mutex: %s",
+                      strerror(errno));
                return -1;
        }
 #endif
@@ -783,7 +773,6 @@ static int cache_instantiate(CONF_SECTION *conf, void **instance)
        inst->cache = rbtree_create(cache_entry_cmp, cache_entry_free, 0);
        if (!inst->cache) {
                radlog(L_ERR, "rlm_cache: Failed to create cache");
-               cache_detach(inst);
                return -1;
        }
 
@@ -794,7 +783,6 @@ static int cache_instantiate(CONF_SECTION *conf, void **instance)
                                    offsetof(rlm_cache_entry_t, offset));
        if (!inst->heap) {
                radlog(L_ERR, "rlm_cache: Failed to create cache");
-               cache_detach(inst);
                return -1;
        }
 
@@ -802,12 +790,9 @@ static int cache_instantiate(CONF_SECTION *conf, void **instance)
         *      Make sure the users don't screw up too badly.
         */
        if (cache_verify(inst, &inst->maps) < 0) {
-               cache_detach(inst);
                return -1;
        }
 
-       *instance = inst;
-
        return 0;
 }
 
index 6add473..03a0ff8 100644 (file)
@@ -83,13 +83,6 @@ static const CONF_PARSER module_config[] = {
        { NULL, -1, 0, NULL, NULL }             /* end the list */
 };
 
-
-static int checkval_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 /*
  *     Do any per-module initialization that is separate to each
  *     configured instance of the module.  e.g. set up connections
@@ -109,18 +102,16 @@ static int checkval_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_checkval_t);
        if (!inst) {
                return -1;
        }
-       memset(inst, 0, sizeof(*inst));
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               checkval_detach(inst);
                return -1;
        }
 
@@ -129,19 +120,16 @@ static int checkval_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (!inst->data_type || !*inst->data_type){
                radlog(L_ERR, "rlm_checkval: Data type not defined");
-               checkval_detach(inst);
                return -1;
        }
 
        if (!inst->item_name || !*inst->item_name){
                radlog(L_ERR, "rlm_checkval: Item name not defined");
-               checkval_detach(inst);
                return -1;
        }
 
        if (!inst->check_name || !*inst->check_name){
                radlog(L_ERR, "rlm_checkval: Check item name not defined");
-               checkval_detach(inst);
                return -1;
        }
 
@@ -152,7 +140,6 @@ static int checkval_instantiate(CONF_SECTION *conf, void **instance)
        if (!da) {
                radlog(L_ERR, "rlm_checkval: No such attribute %s",
                       inst->item_name);
-               checkval_detach(inst);
                return -1;
        }
        inst->item = da;
@@ -168,7 +155,6 @@ static int checkval_instantiate(CONF_SECTION *conf, void **instance)
        if (!da){
                radlog(L_ERR, "rlm_checkval: No such attribute %s",
                       inst->check_name);
-               checkval_detach(inst);
                return -1;
        }
        inst->check = da;
@@ -184,12 +170,9 @@ static int checkval_instantiate(CONF_SECTION *conf, void **instance)
        if (!inst->type) {
                radlog(L_ERR, "rlm_checkval: Data type %s in not known",
                       inst->data_type);
-               checkval_detach(inst);
                return -1;
        }
 
-       *instance = inst;
-
        return 0;
 }
 
@@ -329,9 +312,9 @@ static rlm_rcode_t checkval_accounting(void *instance, REQUEST *request)
 module_t rlm_checkval = {
         RLM_MODULE_INIT,
        "checkval",
-       0,              /* type */
+       0,                              /* type */
        checkval_instantiate,           /* instantiation */
-       checkval_detach,                /* detach */
+       NULL,                           /* detach */
        {
                NULL,                   /* authentication */
                checkval_authorize,     /* authorization */
index f43c5b4..33940c8 100644 (file)
@@ -339,19 +339,13 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               radlog(L_ERR, "rlm_counter: rad_malloc() failed.");
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_counter_t);
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
        cache_size = inst->cache_size;
@@ -416,7 +410,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (inst->counter_name == NULL) {
                radlog(L_ERR, "rlm_counter: 'counter-name' must be set.");
-               counter_detach(inst);
                return -1;
        }
 
@@ -426,7 +419,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to create counter attribute %s",
                                inst->counter_name);
-               counter_detach(inst);
                return -1;
        }
        inst->dict_attr = dattr->attr;
@@ -438,7 +430,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (inst->check_name == NULL) {
                radlog(L_ERR, "rlm_counter: 'check-name' must be set.");
-               counter_detach(inst);
                return -1;
        }
        dict_addattr(inst->check_name, 0, PW_TYPE_INTEGER, -1, flags);
@@ -446,7 +437,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to create check attribute %s",
                                inst->counter_name);
-               counter_detach(inst);
                return -1;
        }
        inst->check_attr = dattr->attr;
@@ -458,7 +448,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
                if ((dval = dict_valbyname(PW_SERVICE_TYPE, 0, inst->service_type)) == NULL) {
                        radlog(L_ERR, "rlm_counter: Failed to find attribute number for %s",
                                        inst->service_type);
-                       counter_detach(inst);
                        return -1;
                }
                inst->service_val = dval->value;
@@ -469,7 +458,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (inst->reset == NULL) {
                radlog(L_ERR, "rlm_counter: 'reset' must be set.");
-               counter_detach(inst);
                return -1;
        }
        now = time(NULL);
@@ -478,13 +466,11 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
 
        if (find_next_reset(inst,now) == -1){
                radlog(L_ERR, "rlm_counter: find_next_reset() returned -1. Exiting.");
-               counter_detach(inst);
                return -1;
        }
 
        if (inst->filename == NULL) {
                radlog(L_ERR, "rlm_counter: 'filename' must be set.");
-               counter_detach(inst);
                return -1;
        }
        inst->gdbm = gdbm_open(inst->filename, sizeof(int),
@@ -493,7 +479,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
        if (inst->gdbm == NULL) {
                radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
                                inst->filename, strerror(errno));
-               counter_detach(inst);
                return -1;
        }
        if (gdbm_setopt(inst->gdbm, GDBM_CACHESIZE, &cache_size,
@@ -530,7 +515,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
                        ret = reset_db(inst);
                        if (ret != RLM_MODULE_OK){
                                radlog(L_ERR, "rlm_counter: reset_db() failed");
-                               counter_detach(inst);
                                return -1;
                        }
                } else {
@@ -550,7 +534,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
                ret = add_defaults(inst);
                if (ret != RLM_MODULE_OK){
                        radlog(L_ERR, "rlm_counter: add_defaults() failed");
-                       counter_detach(inst);
                        return -1;
                }
        }
@@ -566,8 +549,6 @@ static int counter_instantiate(CONF_SECTION *conf, void **instance)
         */
        pthread_mutex_init(&inst->mutex, NULL);
 
-       *instance = inst;
-
        return 0;
 }
 
@@ -920,7 +901,6 @@ static int counter_detach(void *instance)
        
        pthread_mutex_destroy(&inst->mutex);
 
-       free(instance);
        return 0;
 }
 
index 423e55f..8c63186 100644 (file)
@@ -282,20 +282,17 @@ static int sm_postprocessor(VALUE_PAIR **reply UNUSED) {
        return 0;
 }
 
-static int rlm_dbm_instantiate(CONF_SECTION *conf, void **instance) {
+static int rlm_dbm_instantiate(CONF_SECTION *conf, void **instance)
+{
        struct rlm_dbm_t *inst;
 
-       inst = rad_malloc(sizeof(rlm_dbm_t));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, struct rlm_dbm_t);
+       if (!inst) return -1;
 
         if (cf_section_parse(conf, inst, module_config) < 0) {
-                free(inst);
                 return -1;
         }
-       *instance = inst;
+
        return 0;
 }
 static rlm_rcode_t rlm_dbm_authorize(void *instance, REQUEST *request)
@@ -355,21 +352,13 @@ static rlm_rcode_t rlm_dbm_authorize(void *instance, REQUEST *request)
        return found;
 }
 
-static int rlm_dbm_detach(void *instance)
-{
-       struct rlm_dbm_t *inst = instance;
-       free(inst);
-       return 0;
-}
-
-
 /* globally exported name */
 module_t rlm_dbm = {
        RLM_MODULE_INIT,
         "dbm",
         0,                              /* type: reserved */
         rlm_dbm_instantiate,            /* instantiation */
-        rlm_dbm_detach,                 /* detach */
+        NULL,                           /* detach */
         {
                 NULL,                   /* authentication */
                 rlm_dbm_authorize,      /* authorization */
index ad7d6a5..c98c7a1 100644 (file)
@@ -91,8 +91,6 @@ static int detail_detach(void *instance)
 {
         struct detail_instance *inst = instance;
        if (inst->ht) fr_hash_table_free(inst->ht);
-
-        free(inst);
        return 0;
 }
 
@@ -120,11 +118,8 @@ static int detail_instantiate(CONF_SECTION *conf, void **instance)
        detail_instance_t *inst;
        CONF_SECTION    *cs;
 
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, detail_instance_t);
+       if (!inst) return -1;
 
        if (cf_section_parse(conf, inst, module_config) < 0) {
                detail_detach(inst);
@@ -166,8 +161,6 @@ static int detail_instantiate(CONF_SECTION *conf, void **instance)
                }
        }
 
-
-       *instance = inst;
        return 0;
 }
 
index 4c98833..74a7c77 100644 (file)
@@ -90,26 +90,12 @@ static size_t dhcp_options_xlat(UNUSED void *instance, REQUEST *request,
 
 
 /*
- *     A mapping of configuration file names to internal variables.
- *
- *     Note that the string is dynamically allocated, so it MUST
- *     be freed.  When the configuration file parse re-reads the string,
- *     it free's the old one, and strdup's the new one, placing the pointer
- *     to the strdup'd string into 'config.string'.  This gets around
- *     buffer over-flows.
- */
-static const CONF_PARSER module_config[] = {
-       { NULL, -1, 0, NULL, NULL }             /* end the list */
-};
-
-
-/*
  *     Only free memory we allocated.  The strings allocated via
  *     cf_section_parse() do not need to be freed.
  */
 static int dhcp_detach(void *instance)
 {
-       free(instance);
+       xlat_unregister("dhcp_options", dhcp_options_xlat, instance);
        return 0;
 }
 
@@ -121,25 +107,11 @@ static int dhcp_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_dhcp_t *inst;
 
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_dhcp_t);
+       if (!inst) return -1;
        
        xlat_register("dhcp_options", dhcp_options_xlat, inst);
 
-       /*
-        *      If the configuration parameters can't be parsed, then
-        *      fail.
-        */
-       if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
-               return -1;
-       }
-
-       *instance = inst;
-
        return 0;
 }
 
index 55c7789..97e990e 100644 (file)
@@ -70,8 +70,6 @@ static int eap_detach(void *instance)
                inst->types[i] = NULL;
        }
 
-       free(inst);
-
        return 0;
 }
 
@@ -126,11 +124,9 @@ static int eap_instantiate(CONF_SECTION *cs, void **instance)
        CONF_SECTION    *scs;
        rlm_eap_t       *inst;
 
-       inst = (rlm_eap_t *) malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(cs, rlm_eap_t);
+       if (!inst) return -1;
+
        if (cf_section_parse(cs, inst, module_config) < 0) {
                eap_detach(inst);
                return -1;
@@ -268,7 +264,6 @@ static int eap_instantiate(CONF_SECTION *cs, void **instance)
        }
 #endif
 
-       *instance = inst;
        return 0;
 }
 
index 395a3b3..4867a1f 100644 (file)
@@ -347,8 +347,6 @@ static int eap_detach(void *instance)
 
        pthread_mutex_destroy(&(inst->session_mutex));
 
-       free(inst);
-
        return 0;
 }
 
@@ -527,11 +525,9 @@ static int eap_instantiate(CONF_SECTION *cs, void **instance)
        rlm_eap_t       *inst;
        CONF_SECTION    *scs;
 
-       inst = (rlm_eap_t *) malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_eap_t);
+       if (!inst) return -1;
+
        if (cf_section_parse(cs, inst, module_config) < 0) {
                eap_detach(inst);
                return -1;
@@ -647,8 +643,6 @@ static int eap_instantiate(CONF_SECTION *cs, void **instance)
        }
 
        pthread_mutex_init(&(inst->session_mutex), NULL);
-
-       *instance = inst;
        return 0;
 }
 
index d52b7a0..603e58c 100644 (file)
@@ -77,23 +77,17 @@ static int example_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_example_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                return -1;
        }
 
-       *instance = data;
-
        return 0;
 }
 
@@ -202,7 +196,7 @@ static rlm_rcode_t example_checksimul(void *instance, REQUEST *request)
  */
 static int example_detach(void *instance)
 {
-       free(instance);
+       /* free things here */
        return 0;
 }
 
index bcdd693..48c67b0 100644 (file)
@@ -163,10 +163,8 @@ static int exec_detach(void *instance)
 
        if (inst->xlat_name) {
                xlat_unregister(inst->xlat_name, exec_xlat, instance);
-               free(inst->xlat_name);
        }
 
-       free(inst);
        return 0;
 }
 
@@ -189,19 +187,16 @@ static int exec_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-
-       inst = rad_malloc(sizeof(rlm_exec_t));
-       if (!inst)
-               return -1;
-       memset(inst, 0, sizeof(rlm_exec_t));
+       *instance = inst = talloc_zero(conf, rlm_exec_t);
+       if (!inst) return -1;
        
        xlat_name = cf_section_name2(conf);
-       if (xlat_name == NULL) {
+       if (!xlat_name) {
                xlat_name = cf_section_name1(conf);
                inst->bare = 1;
        }
-       if (xlat_name){
-               inst->xlat_name = strdup(xlat_name);
+       if (xlat_name) {
+               inst->xlat_name = xlat_name;
                xlat_register(xlat_name, exec_xlat, inst);
        }
 
@@ -258,8 +253,6 @@ static int exec_instantiate(CONF_SECTION *conf, void **instance)
                inst->packet_code = dval->value;
        }
 
-       *instance = inst;
-
        return 0;
 }
 
index 9fc3887..7c842db 100644 (file)
@@ -139,7 +139,6 @@ static int expirecmp(void *instance, REQUEST *req,
 static int expiration_detach(void *instance)
 {
        paircompare_unregister(PW_EXPIRATION, expirecmp);
-       free(instance);
        return 0;
 }
 
@@ -160,19 +159,14 @@ static int expiration_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               radlog(L_ERR, "rlm_expiration: rad_malloc() failed.");
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_expiration_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                radlog(L_ERR, "rlm_expiration: Configuration parsing failed.");
                return -1;
        }
@@ -181,9 +175,6 @@ static int expiration_instantiate(CONF_SECTION *conf, void **instance)
         * Register the expiration comparison operation.
         */
        paircompare_register(PW_EXPIRATION, 0, expirecmp, data);
-
-       *instance = data;
-
        return 0;
 }
 
index a9b0c8a..b93ccf3 100644 (file)
@@ -701,9 +701,6 @@ static int expr_detach(void *instance)
 
        xlat_unregister(inst->xlat_name, expr_xlat, instance);
        pair_builtincompare_detach();
-       free(inst->xlat_name);
-
-       free(inst);
        return 0;
 }
 
@@ -720,29 +717,23 @@ static int expr_detach(void *instance)
 static int expr_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_expr_t      *inst;
-       const char      *xlat_name;
 
        /*
         *      Set up a storage area for instance data
         */
-
-       inst = rad_malloc(sizeof(rlm_expr_t));
-       if (!inst)
-               return -1;
-       memset(inst, 0, sizeof(rlm_expr_t));
+       *instance = inst = talloc_zero(conf, rlm_expr_t);
+       if (!inst) return -1;
        
        if (cf_section_parse(conf, inst, module_config) < 0) {
                expr_detach(inst);
                return -1;
        }
 
-       xlat_name = cf_section_name2(conf);
-       if (xlat_name == NULL)
-               xlat_name = cf_section_name1(conf);
-       if (xlat_name){
-               inst->xlat_name = strdup(xlat_name);
-               xlat_register(xlat_name, expr_xlat, inst);
+       inst->xlat_name = cf_section_name2(conf);
+       if (!inst->xlat_name) {
+               inst->xlat_name = cf_section_name1(conf);
        }
+       xlat_register(inst->xlat_name, expr_xlat, inst);
 
        xlat_register("rand", rand_xlat, inst);
        xlat_register("randstr", randstr_xlat, inst);
@@ -755,11 +746,9 @@ static int expr_instantiate(CONF_SECTION *conf, void **instance)
        xlat_register("base64tohex", base64_to_hex_xlat, inst);
 
        /*
-        * Initialize various paircompare functions
+        *      Initialize various paircompare functions
         */
        pair_builtincompare_init();
-       *instance = inst;
-
        return 0;
 }
 
index 66b9196..1f7d193 100644 (file)
@@ -330,7 +330,6 @@ static int file_detach(void *instance)
 #endif
        fr_hash_table_free(inst->auth_users);
        fr_hash_table_free(inst->postauth_users);
-       free(inst);
        return 0;
 }
 
@@ -344,14 +343,10 @@ static int file_instantiate(CONF_SECTION *conf, void **instance)
        struct file_instance *inst;
        int rcode;
 
-       inst = rad_malloc(sizeof *inst);
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, struct file_instance);
+       if (!inst) return -1;
 
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
index 83c0525..f8164be 100644 (file)
@@ -146,30 +146,24 @@ static int ippool_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_ippool_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                return -1;
        }
        cache_size = data->cache_size;
 
        if (data->session_db == NULL) {
                radlog(L_ERR, "rlm_ippool: 'session-db' must be set.");
-               free(data);
                return -1;
        }
        if (data->ip_index == NULL) {
                radlog(L_ERR, "rlm_ippool: 'ip-index' must be set.");
-               free(data);
                return -1;
        }
        data->range_start = htonl(data->range_start);
@@ -178,7 +172,6 @@ static int ippool_instantiate(CONF_SECTION *conf, void **instance)
        if (data->range_start == 0 || data->range_stop == 0 || \
                         data->range_start >= data->range_stop ) {
                radlog(L_ERR, "rlm_ippool: Invalid configuration data given.");
-               free(data);
                return -1;
        }
 
@@ -251,7 +244,6 @@ static int ippool_instantiate(CONF_SECTION *conf, void **instance)
                                                data->session_db, gdbm_strerror(gdbm_errno));
                                gdbm_close(data->gdbm);
                                gdbm_close(data->ip);
-                               free(data);
                                return -1;
                        }
                }
@@ -266,7 +258,6 @@ static int ippool_instantiate(CONF_SECTION *conf, void **instance)
                data->name = strdup(pool_name);
 
        pthread_mutex_init(&data->op_mutex, NULL);
-       *instance = data;
 
        return 0;
 }
@@ -799,8 +790,6 @@ static int ippool_detach(void *instance)
        gdbm_close(data->gdbm);
        gdbm_close(data->ip);
        pthread_mutex_destroy(&data->op_mutex);
-
-       free(instance);
        return 0;
 }
 
index caf6b53..167c499 100644 (file)
@@ -126,9 +126,8 @@ static int krb5_instantiate(CONF_SECTION *conf, void **instance)
                return -1;
        }
 
-       inst = rad_calloc(sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_krb5_t);
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
        
@@ -136,8 +135,6 @@ static int krb5_instantiate(CONF_SECTION *conf, void **instance)
        if (!inst->xlat_name) {
                inst->xlat_name = cf_section_name1(conf);
        }
-
-       rad_assert(inst->xlat_name);
        
        context = inst->context = rad_calloc(sizeof(*context));
        ret = krb5_init_context(context);
@@ -234,7 +231,6 @@ static int krb5_instantiate(CONF_SECTION *conf, void **instance)
        }
 #endif
         
-       *instance = inst;
        return 0;
        
        error:
index f18a3b7..4e5d411 100644 (file)
@@ -1320,7 +1320,7 @@ check_attr:
        ldap_msgfree(result);
        ldap_release_socket(inst, conn);
 
-       if (!found){
+       if (!found) {
                RDEBUG("User is not a member of specified group");
                return 1;
        }
@@ -1334,18 +1334,13 @@ check_attr:
 static int ldap_detach(void *instance)
 {
        ldap_instance *inst = instance;
-
-       if (inst->postauth) free(inst->postauth);
-       if (inst->accounting) free(inst->accounting);
        
        fr_connection_pool_delete(inst->pool);
-       
+
        if (inst->user_map) {
                radius_mapfree(&inst->user_map);
        }
 
-       free(inst);
-
        return 0;
 }
 
@@ -1367,14 +1362,10 @@ static int parse_sub_section(CONF_SECTION *parent,
                return 0;
        }
        
-       *config = rad_calloc(sizeof(**config));
+       *config = talloc_zero(inst, ldap_acct_section_t);
        if (cf_section_parse(cs, *config, acct_section_config) < 0) {
                radlog(L_ERR, "rlm_ldap (%s): Failed parsing configuration for "
                       "section %s", inst->xlat_name, name);
-               
-               free(*config);
-               *config = NULL;
-               
                return -1;
        }
                
@@ -1450,7 +1441,9 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
 {
        ldap_instance *inst;
 
-       inst = rad_calloc(sizeof *inst);
+       *instance = inst = talloc_zero(conf, ldap_instance);
+       if (!inst) return -1;
+
        inst->cs = conf;
 
        inst->chase_referrals = 2; /* use OpenLDAP defaults */
@@ -1460,8 +1453,6 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
        if (!inst->xlat_name) {
                inst->xlat_name = cf_section_name1(conf);
        }
-               
-       rad_assert(inst->xlat_name);
 
        /*
         *      If the configuration parameters can't be parsed, then fail.
@@ -1568,10 +1559,9 @@ static int ldap_instantiate(CONF_SECTION * conf, void **instance)
                return -1;
        }
        
-       *instance = inst;
        return 0;
-       
-       error:
+
+error:
        ldap_detach(inst);
        return -1;
 }
index f802e43..5aec790 100644 (file)
@@ -92,14 +92,6 @@ static const CONF_PARSER module_config[] = {
 };
 
 
-static int linelog_detach(void *instance)
-{
-       rlm_linelog_t *inst = instance;
-
-       free(inst);
-       return 0;
-}
-
 /*
  *     Instantiate the module.
  */
@@ -110,28 +102,25 @@ static int linelog_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(*inst));
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_linelog_t);
+       if (!inst) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               linelog_detach(inst);
                return -1;
        }
 
        if (!inst->filename) {
                radlog(L_ERR, "rlm_linelog: Must specify an output filename");
-               linelog_detach(inst);
                return -1;
        }
 
 #ifndef HAVE_SYSLOG_H
        if (strcmp(inst->filename, "syslog") == 0) {
                radlog(L_ERR, "rlm_linelog: Syslog output is not supported");
-               linelog_detach(inst);
                return -1;
        }
 #else
@@ -141,7 +130,6 @@ static int linelog_instantiate(CONF_SECTION *conf, void **instance)
                inst->facility = fr_str2int(syslog_str2fac, inst->syslog_facility, -1);
                if (inst->facility < 0) {
                        radlog(L_ERR, "rlm_linelog: Bad syslog facility '%s'", inst->syslog_facility);
-                       linelog_detach(inst);
                        return -1;
                }
        }
@@ -151,13 +139,10 @@ static int linelog_instantiate(CONF_SECTION *conf, void **instance)
 
        if (!inst->line) {
                radlog(L_ERR, "rlm_linelog: Must specify a log format");
-               linelog_detach(inst);
                return -1;
        }
 
        inst->cs = conf;
-       *instance = inst;
-
        return 0;
 }
 
@@ -354,7 +339,7 @@ module_t rlm_linelog = {
        "linelog",
        RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
        linelog_instantiate,            /* instantiation */
-       linelog_detach,                 /* detach */
+       NULL,                           /* detach */
        {
                do_linelog,     /* authentication */
                do_linelog,     /* authorization */
index d5b9292..84de511 100644 (file)
@@ -58,7 +58,6 @@ static const CONF_PARSER module_config[] = {
   { NULL, -1, 0, NULL, NULL }
 };
 
-static int logintime_detach(void *instance);
 
 /*
  *      Compare the current time to a range.
@@ -256,26 +255,20 @@ static int logintime_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               radlog(L_ERR, "rlm_logintime: rad_malloc() failed.");
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_logintime_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                radlog(L_ERR, "rlm_logintime: Configuration parsing failed.");
                return -1;
        }
 
        if (data->min_time == 0){
                radlog(L_ERR, "rlm_logintime: Minimum timeout should be non zero.");
-               free(data);
                return -1;
        }
 
@@ -285,16 +278,13 @@ static int logintime_instantiate(CONF_SECTION *conf, void **instance)
        paircompare_register(PW_CURRENT_TIME, 0, timecmp, data);
        paircompare_register(PW_TIME_OF_DAY, 0, time_of_day, data);
 
-       *instance = data;
-
        return 0;
 }
 
-static int logintime_detach(void *instance)
+static int logintime_detach(UNUSED void *instance)
 {
        paircompare_unregister(PW_CURRENT_TIME, timecmp);
        paircompare_unregister(PW_TIME_OF_DAY, time_of_day);
-       free(instance);
        return 0;
 }
 
index 18cd524..85a5967 100644 (file)
@@ -568,15 +568,13 @@ static const CONF_PARSER module_config[] = {
  *     deinstantiate module, free all memory allocated during
  *     mschap_instantiate()
  */
-static int mschap_detach(void *instance){
-#define inst ((rlm_mschap_t *)instance)
+static int mschap_detach(void *instance)
+{
+       rlm_mschap_t *inst = instance;
        if (inst->xlat_name) {
                xlat_unregister(inst->xlat_name, mschap_xlat, instance);
-               free(inst->xlat_name);
        }
-       free(instance);
        return 0;
-#undef inst
 }
 
 /*
@@ -588,14 +586,10 @@ static int mschap_instantiate(CONF_SECTION *conf, void **instance)
        const char *name;
        rlm_mschap_t *inst;
 
-       inst = *instance = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_mschap_t);
+       if (!inst) return -1;
 
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
@@ -607,7 +601,6 @@ static int mschap_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (inst->passwd_file) {
                radlog(L_ERR, "rlm_mschap: SMB password file is no longer supported in this module.  Use rlm_passwd module instead");
-               mschap_detach(inst);
                return -1;
        }
 
@@ -616,7 +609,7 @@ static int mschap_instantiate(CONF_SECTION *conf, void **instance)
         */
        name = cf_section_name2(conf);
        if (!name) name = cf_section_name1(conf);
-       inst->xlat_name = strdup(name);
+       inst->xlat_name = name;
        xlat_register(inst->xlat_name, mschap_xlat, inst);
 
        /*
@@ -1274,7 +1267,7 @@ static void mppe_chap2_gen_keys128(uint8_t *nt_hashhash,uint8_t *response,
  */
 static rlm_rcode_t mschap_authorize(void * instance, REQUEST *request)
 {
-#define inst ((rlm_mschap_t *)instance)
+       rlm_mschap_t *inst = instance;
        VALUE_PAIR *challenge = NULL;
 
        challenge = pairfind(request->packet->vps, PW_MSCHAP_CHALLENGE, VENDORPEC_MICROSOFT, TAG_ANY);
@@ -1307,7 +1300,6 @@ static rlm_rcode_t mschap_authorize(void * instance, REQUEST *request)
        }
 
        return RLM_MODULE_OK;
-#undef inst
 }
 
 /*
index 1891c93..bb10f1f 100644 (file)
@@ -72,11 +72,11 @@ static int otp_instantiate(CONF_SECTION *conf, void **instance)
        otp_option_t *opt;
 
        /* Set up a storage area for instance data. */
-       opt = rad_calloc(sizeof(*opt));
+       *instance = opt = talloc_zero(conf, otp_option_t);
+       if (!opt) return -1;
 
        /* If the configuration parameters can't be parsed, then fail. */
        if (cf_section_parse(conf, opt, module_config) < 0) {
-               free(opt);
                return -1;
        }
 
@@ -109,8 +109,6 @@ static int otp_instantiate(CONF_SECTION *conf, void **instance)
        if (!opt->allow_sync && !opt->allow_async) {
                radlog(L_ERR, "rlm_otp: %s: at least one of {allow_async, "
                       "allow_sync} must be set", __func__);
-               free(opt);
-               
                return -1;
        }
 
@@ -149,12 +147,9 @@ static int otp_instantiate(CONF_SECTION *conf, void **instance)
        if (!opt->name) {
                radlog(L_ERR, "rlm_otp: %s: no instance name "
                       "(this can't happen)", __func__);
-               free(opt);
                return -1;
        }
 
-       *instance = opt;
-       
        return 0;
 }
 
@@ -462,24 +457,6 @@ static rlm_rcode_t otp_authenticate(void *instance, REQUEST *request)
        return rc;
 }
 
-
-/*
- *     Per-instance destruction
- */
-static int otp_detach(void *instance)
-{
-       free(instance);
-       /*
-       * Only the main thread instantiates and detaches instances,
-       * so this does not need mutex protection.
-       */
-       if (--ninstance == 0) {
-               (void) memset(hmac_key, 0, sizeof(hmac_key));
-       }
-       
-       return 0;
-}
-
 /*
  *     If the module needs to temporarily modify it's instantiation
  *     data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
@@ -491,7 +468,7 @@ module_t rlm_otp = {
        "otp",
        RLM_TYPE_THREAD_SAFE,           /* type */
        otp_instantiate,                /* instantiation */
-       otp_detach,                     /* detach */
+       NULL,                           /* detach */
        {
                otp_authenticate,       /* authentication */
                otp_authorize,          /* authorization */
index c46c912..55fbfaa 100644 (file)
@@ -65,29 +65,13 @@ static int pam_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_pam_t *data;
 
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_pam_t);
+       if (!data) return -1;
 
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                return -1;
        }
 
-       *instance = data;
-       return 0;
-}
-
-/*
- *     Clean up.
- */
-static int pam_detach(void *instance)
-{
-       rlm_pam_t *data = (rlm_pam_t *) instance;
-
-        free((char *) data);
        return 0;
 }
 
@@ -283,7 +267,7 @@ module_t rlm_pam = {
        "pam",
        RLM_TYPE_THREAD_UNSAFE, /* The PAM libraries are not thread-safe */
        pam_instantiate,                /* instantiation */
-       pam_detach,                     /* detach */
+       NULL,                           /* detach */
        {
                pam_auth,               /* authenticate */
                NULL,                   /* authorize */
index be26804..0fae01d 100644 (file)
@@ -85,16 +85,6 @@ static const FR_NAME_NUMBER header_names[] = {
 };
 
 
-static int pap_detach(void *instance)
-{
-       rlm_pap_t *inst = (rlm_pap_t *) instance;
-
-       free(inst);
-
-       return 0;
-}
-
-
 static int pap_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_pap_t *inst;
@@ -103,18 +93,14 @@ static int pap_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_pap_t);
+       if (!inst) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               pap_detach(inst);
                return -1;
        }
 
@@ -130,8 +116,6 @@ static int pap_instantiate(CONF_SECTION *conf, void **instance)
                inst->auth_type = 0;
        }
 
-       *instance = inst;
-
        return 0;
 }
 
@@ -864,7 +848,7 @@ module_t rlm_pap = {
        "PAP",
        RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
        pap_instantiate,                /* instantiation */
-       pap_detach,                     /* detach */
+       NULL,                           /* detach */
        {
                pap_authenticate,       /* authentication */
                pap_authorize,          /* authorization */
index 9f9bf69..bee54d4 100644 (file)
@@ -189,6 +189,12 @@ static struct hashtable * build_hash_table (const char * file, int nfields,
                free(ht);
                               return NULL;
        }
+
+       /*
+        *      @todo: This code is SHIT.  It's badly formatted.  It's
+        *      hard to understand.  It re-implements tons of things
+        *      which are already in the server core.
+        */
        memset(ht->buffer, 0, 1024);
        ht->table = (struct mypasswd **) rad_malloc (tablesize * sizeof(struct mypasswd *));
        if (!ht->table) {
@@ -388,41 +394,34 @@ static const CONF_PARSER module_config[] = {
 
 static int passwd_instantiate(CONF_SECTION *conf, void **instance)
 {
-#define inst ((struct passwd_instance *)*instance)
        int nfields=0, keyfield=-1, listable=0;
        char *s;
        char *lf=NULL; /* destination list flags temporary */
        size_t len;
        int i;
        const DICT_ATTR * da;
+       struct passwd_instance *inst;
+
+       *instance = inst = talloc_zero(conf, struct passwd_instance);
+       if (inst) return -1;
 
-       *instance = rad_malloc(sizeof(struct passwd_instance));
-       if ( !*instance) {
-               radlog(L_ERR, "rlm_passwd: cann't alloc instance");
-               return -1;
-       }
-       memset(*instance, 0, sizeof(struct passwd_instance));
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                radlog(L_ERR, "rlm_passwd: cann't parse configuration");
                return -1;
        }
        if(!inst->filename || *inst->filename == '\0' || !inst->format || *inst->format == '\0') {
                radlog(L_ERR, "rlm_passwd: can't find passwd file and/or format in configuration");
-               free(inst);
                return -1;
        }
 
        if (inst->hashsize == 0) {
                radlog(L_ERR, "rlm_passwd: hashsize=0 is no longer permitted as it will break the server.");
-               free(inst);
                return -1;
        }
 
-       lf=strdup(inst->format);
+       lf = talloc_strdup(inst, inst->format);
        if ( lf == NULL) {
                radlog(L_ERR, "rlm_passwd: memory allocation failed for lf");
-               free(inst);
                return -1;
        }
        memset(lf, 0, strlen(inst->format));
@@ -451,30 +450,26 @@ static int passwd_instantiate(CONF_SECTION *conf, void **instance)
        }while(*s);
        if(keyfield < 0) {
                radlog(L_ERR, "rlm_passwd: no field market as key in format: %s", inst->format);
-               free(lf);
                return -1;
        }
        if (! (inst->ht = build_hash_table (inst->filename, nfields, keyfield, listable, inst->hashsize, inst->ignorenislike, *inst->delimiter)) ){
                radlog(L_ERR, "rlm_passwd: can't build hashtable from passwd file");
-               free(lf);
                return -1;
        }
        if (! (inst->pwdfmt = mypasswd_malloc(inst->format, nfields, &len)) ){
                radlog(L_ERR, "rlm_passwd: memory allocation failed");
                release_ht(inst->ht);
-               free(lf);
                return -1;
        }
        if (!string_to_entry(inst->format, nfields, ':', inst->pwdfmt , len)) {
                radlog(L_ERR, "rlm_passwd: unable to convert format entry");
                release_ht(inst->ht);
-               free(lf);
                return -1;
        }
 
        memcpy(inst->pwdfmt->listflag, lf, nfields);
 
-       free(lf);
+       talloc_free(lf);
        for (i=0; i<nfields; i++) {
                if (*inst->pwdfmt->field[i] == '*') inst->pwdfmt->field[i]++;
                if (*inst->pwdfmt->field[i] == ',') inst->pwdfmt->field[i]++;
@@ -504,7 +499,6 @@ static int passwd_instantiate(CONF_SECTION *conf, void **instance)
 static int passwd_detach (void *instance) {
 #define inst ((struct passwd_instance *)instance)
        if(inst->ht) release_ht(inst->ht);
-       free(instance);
        return 0;
 #undef inst
 }
index 7d5d314..3099700 100644 (file)
@@ -414,21 +414,20 @@ static int perl_instantiate(CONF_SECTION *conf, void **instance)
        const char *xlat_name;
        int exitstatus = 0, argc=0;
 
-        embed = rad_malloc(4 * sizeof(char *));
-        memset(embed, 0, 4 *sizeof(char *));
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(PERL_INST));
-       memset(inst, 0, sizeof(PERL_INST));
+       *instance = inst = talloc_zero(conf, PERL_INST);
+       if (!inst) return -1;
+
+        embed = talloc_size(inst, 4 * sizeof(char *));
+        memset(embed, 0, 4 *sizeof(char *));
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(embed);
-               free(inst);
                return -1;
        }
        
@@ -463,8 +462,6 @@ static int perl_instantiate(CONF_SECTION *conf, void **instance)
 #ifdef USE_ITHREADS
        if ((inst->perl = perl_alloc()) == NULL) {
                radlog(L_DBG, "rlm_perl: No memory for allocating new perl !");
-               free(embed);
-               free(inst);
                return (-1);
        }
 
@@ -478,8 +475,6 @@ static int perl_instantiate(CONF_SECTION *conf, void **instance)
 #else
        if ((inst->perl = perl_alloc()) == NULL) {
                radlog(L_ERR, "rlm_perl: No memory for allocating new perl !");
-               free(embed);
-               free(inst);
                return -1;
        }
 
@@ -501,8 +496,6 @@ static int perl_instantiate(CONF_SECTION *conf, void **instance)
                exitstatus = perl_run(inst->perl);
        } else {
                radlog(L_ERR,"rlm_perl: perl_parse failed: %s not found or has syntax errors. \n", inst->module);
-               free(embed);
-               free(inst);
                return (-1);
        }
 
@@ -511,13 +504,10 @@ static int perl_instantiate(CONF_SECTION *conf, void **instance)
        xlat_name = cf_section_name2(conf);
        if (xlat_name == NULL)
                xlat_name = cf_section_name1(conf);
-       if (xlat_name){
-               inst->xlat_name = strdup(xlat_name);
+       if (xlat_name) {
                xlat_register(xlat_name, perl_xlat, inst);
        }
 
-       *instance = inst;
-
        return 0;
 }
 
@@ -999,7 +989,6 @@ static int perl_detach(void *instance)
        }
 
        xlat_unregister(inst->xlat_name, perl_xlat, instance);
-       free(inst->xlat_name);
 
 #ifdef USE_ITHREADS
        rlm_perl_destruct(inst->perl);
@@ -1010,7 +999,6 @@ static int perl_detach(void *instance)
 #endif
 
         PERL_SYS_TERM();
-       free(inst);
        return exitstatus;
 }
 
index 4bf47bc..e84c5e7 100644 (file)
@@ -532,14 +532,13 @@ static int preprocess_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Allocate room to put the module's instantiation data.
         */
-       data = (rlm_preprocess_t *) rad_malloc(sizeof(*data));
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_preprocess_t);
+       if (!data) return -1;
 
        /*
         *      Read this modules configuration data.
         */
         if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                 return -1;
         }
 
@@ -571,11 +570,6 @@ static int preprocess_instantiate(CONF_SECTION *conf, void **instance)
                }
        }
 
-       /*
-        *      Save the instantiation data for later.
-        */
-       *instance = data;
-
        return 0;
 }
 
@@ -752,8 +746,6 @@ static int preprocess_detach(void *instance)
        pairlist_free(&(data->huntgroups));
        pairlist_free(&(data->hints));
 
-       free(data);
-
        return 0;
 }
 
index 5f0bd67..4446aea 100644 (file)
@@ -590,12 +590,10 @@ static int python_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       if ((data = malloc(sizeof(*data))) == NULL)
-               return -1;
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, struct rlm_python_t);
+       if (!data) return -1;
 
        if (python_init() != 0) {
-               free(data);
                return -1;
        }
 
@@ -604,7 +602,6 @@ static int python_instantiate(CONF_SECTION *conf, void **instance)
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                return -1;
        }
 
@@ -627,8 +624,6 @@ static int python_instantiate(CONF_SECTION *conf, void **instance)
 
 #undef A
 
-       *instance = data;
-
        /*
         *      Call the instantiate function.  No request.  Use the
         *      return value.
@@ -638,7 +633,6 @@ static int python_instantiate(CONF_SECTION *conf, void **instance)
  failed:
        python_error();
        python_instance_clear(data);
-       free(data);
        return -1;
 }
 
@@ -650,8 +644,6 @@ static int python_detach(void *instance)
        ret = python_function(NULL, data->detach.function, "detach");
        
        python_instance_clear(data);
-       
-       free(data);
        return ret;
 }
 
index 2bc9fef..f1dbd86 100644 (file)
@@ -78,36 +78,14 @@ static int radutmp_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_radutmp_t *inst;
 
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_radutmp_t);
+       if (!inst) return -1;
 
        if (cf_section_parse(conf, inst, module_config)) {
-               free(inst);
                return -1;
        }
 
        inst->nas_port_list = NULL;
-       *instance = inst;
-       return 0;
-}
-
-
-/*
- *     Detach.
- */
-static int radutmp_detach(void *instance)
-{
-       NAS_PORT *p, *next;
-       rlm_radutmp_t *inst = instance;
-
-       for (p = inst->nas_port_list ; p ; p=next) {
-               next = p->next;
-               free(p);
-       }
-       free(inst);
        return 0;
 }
 
@@ -521,13 +499,15 @@ static rlm_rcode_t radutmp_accounting(void *instance, REQUEST *request)
                 *      Remember where the entry was, because it's
                 *      easier than searching through the entire file.
                 */
-               if (cache == NULL) {
-                       cache = rad_malloc(sizeof(NAS_PORT));
-                       cache->nasaddr = ut.nas_address;
-                       cache->port = ut.nas_port;
-                       cache->offset = off;
-                       cache->next = inst->nas_port_list;
-                       inst->nas_port_list = cache;
+               if (!cache) {
+                       cache = talloc_zero(inst, NAS_PORT);
+                       if (cache) {
+                               cache->nasaddr = ut.nas_address;
+                               cache->port = ut.nas_port;
+                               cache->offset = off;
+                               cache->next = inst->nas_port_list;
+                               inst->nas_port_list = cache;
+                       }
                }
 
                ut.type = P_LOGIN;
@@ -742,9 +722,9 @@ static rlm_rcode_t radutmp_checksimul(void *instance, REQUEST *request)
 module_t rlm_radutmp = {
        RLM_MODULE_INIT,
        "radutmp",
-       RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
+       RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,        /* type */
        radutmp_instantiate,          /* instantiation */
-       radutmp_detach,               /* detach */
+       NULL,                          /* detach */
        {
                NULL,                 /* authentication */
                NULL,                 /* authorization */
index a7139f9..2e89f67 100644 (file)
@@ -356,41 +356,32 @@ static int realm_instantiate(CONF_SECTION *conf, void **instance)
         struct realm_config_t *inst;
 
         /* setup a storage area for instance data */
-        inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+        *instance = inst = talloc_zero(conf, struct realm_config_t);
+       if (!inst) return -1;
 
        if(cf_section_parse(conf, inst, module_config) < 0) {
-              free(inst);
                return -1;
        }
 
        if(strcasecmp(inst->formatstring, "suffix") == 0) {
             inst->format = REALM_FORMAT_SUFFIX;
+
        } else if(strcasecmp(inst->formatstring, "prefix") == 0) {
             inst->format = REALM_FORMAT_PREFIX;
+
         } else {
             radlog(L_ERR, "Bad value \"%s\" for realm format value", inst->formatstring);
-            free(inst);
             return -1;
        }
        if(strlen(inst->delim) != 1) {
             radlog(L_ERR, "Bad value \"%s\" for realm delimiter value", inst->delim);
-            free(inst);
             return -1;
        }
 
-       *instance = inst;
        return 0;
-
 }
 
 
-
-
-
 /*
  *  Examine a request for a username with an realm, and if it
  *  corresponds to something in the realms file, set that realm as
@@ -507,19 +498,13 @@ static rlm_rcode_t realm_coa(UNUSED void *instance, REQUEST *request)
 }
 #endif
 
-static int realm_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 /* globally exported name */
 module_t rlm_realm = {
        RLM_MODULE_INIT,
        "realm",
        RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
        realm_instantiate,              /* instantiation */
-       realm_detach,                   /* detach */
+       NULL,                           /* detach */
        {
                NULL,                   /* authentication */
                realm_authorize,        /* authorization */
index 34c2289..9909a45 100644 (file)
@@ -264,10 +264,7 @@ static int redis_detach(void *instance)
 
        if (inst->xlat_name) {
                xlat_unregister(inst->xlat_name, redis_xlat, instance);
-               free(inst->xlat_name);
        }
-       free(inst->xlat_name);
-       free(inst);
 
        return 0;
 }
@@ -341,18 +338,14 @@ static int redis_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof (REDIS_INST));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof (*inst));
+       *instance = inst = talloc_zero(conf, REDIS_INST);
+       if (!inst) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
@@ -361,14 +354,12 @@ static int redis_instantiate(CONF_SECTION *conf, void **instance)
        if (!xlat_name)
                xlat_name = cf_section_name1(conf);
 
-       inst->xlat_name = strdup(xlat_name);
        xlat_register(inst->xlat_name, redis_xlat, inst);
 
        inst->pool = fr_connection_pool_init(conf, inst,
                                             redis_create_conn, NULL,
                                             redis_delete_conn);
        if (!inst->pool) {
-               redis_detach(inst);
                return -1;
        }
 
@@ -376,8 +367,6 @@ static int redis_instantiate(CONF_SECTION *conf, void **instance)
        inst->redis_finish_query = rlm_redis_finish_query;
        inst->redis_escape_func = redis_escape_func;
 
-       *instance = inst;
-
        return 0;
 }
 
index a1caf07..8741e9f 100644 (file)
@@ -115,17 +115,7 @@ static int rediswho_command(const char *fmt, REDISSOCK **dissocket_p,
        return result;
 }
 
-static int rediswho_detach(void *instance)
-{
-       rlm_rediswho_t *inst;
-
-       inst = instance;
-       free(inst);
-
-       return 0;
-}
-
-static int rediswho_instantiate(CONF_SECTION * conf, void ** instance)
+static int rediswho_instantiate(CONF_SECTION *conf, void ** instance)
 {
        module_instance_t *modinst;
        rlm_rediswho_t *inst;
@@ -133,15 +123,14 @@ static int rediswho_instantiate(CONF_SECTION * conf, void ** instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = *instance = rad_malloc(sizeof (*inst));
-       memset(inst, 0, sizeof (*inst));
+       *instance = inst = talloc_zero(conf, rlm_rediswho_t);
+       if (!inst) return -1;
     
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
@@ -150,7 +139,6 @@ static int rediswho_instantiate(CONF_SECTION * conf, void ** instance)
        if (!inst->xlat_name) 
                inst->xlat_name = cf_section_name1(conf);
 
-       inst->xlat_name = strdup(inst->xlat_name);
        inst->cs = conf;
 
        modinst = find_module_instance(cf_section_find("modules"),
@@ -159,8 +147,6 @@ static int rediswho_instantiate(CONF_SECTION * conf, void ** instance)
                radlog(L_ERR,
                       "rediswho: failed to find module instance \"%s\"",
                       inst->redis_instance_name);
-
-               rediswho_detach(inst);
                return -1;
        }
 
@@ -168,8 +154,6 @@ static int rediswho_instantiate(CONF_SECTION * conf, void ** instance)
                radlog(L_ERR, "rediswho: Module \"%s\""
                       " is not an instance of the redis module",
                       inst->redis_instance_name);
-
-               rediswho_detach(inst);
                return -1;
        }
 
@@ -258,9 +242,9 @@ static rlm_rcode_t rediswho_accounting(void * instance, REQUEST * request)
 module_t rlm_rediswho = {
        RLM_MODULE_INIT,
        "rediswho",
-       RLM_TYPE_THREAD_SAFE, /* type */
-       rediswho_instantiate, /* instantiation */
-       rediswho_detach, /* detach */
+       RLM_TYPE_THREAD_SAFE,   /* type */
+       rediswho_instantiate,   /* instantiation */
+       NULL,                   /* detach */
        {
                NULL, /* authentication */
                NULL, /* authorization */
index ca611f3..4866895 100644 (file)
@@ -232,18 +232,14 @@ static int rlm_rest_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Allocate memory for instance data.
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_rest_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                return -1;
        }
 
@@ -288,8 +284,6 @@ static int rlm_rest_instantiate(CONF_SECTION *conf, void **instance)
                return -1;
        }
 
-       *instance = data;
-
        return 0;
 }
 
@@ -421,8 +415,6 @@ static int rlm_rest_detach(void *instance)
 
        fr_connection_pool_delete(my_instance->conn_pool);
 
-       free(my_instance);
-
        /* Free any memory used by libcurl */
        rest_cleanup();
 
index 385b897..9c56152 100644 (file)
@@ -77,8 +77,6 @@ typedef struct rlm_ruby_t {
  *     to the strdup'd string into 'config.string'.  This gets around
  *     buffer over-flows.
  */
-
-
 static const CONF_PARSER module_config[] = {
     { "scriptfile", PW_TYPE_FILENAME,
         offsetof(struct rlm_ruby_t, scriptFile), NULL, NULL},
@@ -339,7 +337,8 @@ static int load_ruby_function(const char *f_name, int *func, VALUE module) {
  *     in *instance otherwise put a null pointer there.
  *
  */
-static int ruby_instantiate(CONF_SECTION *conf, void **instance) {
+static int ruby_instantiate(CONF_SECTION *conf, void **instance)
+{
     rlm_ruby_t *data;
     VALUE module;
     int idx;
@@ -348,7 +347,6 @@ static int ruby_instantiate(CONF_SECTION *conf, void **instance) {
      * Initialize Ruby interpreter. Fatal error if this fails.
      */
 
-    radlog(L_DBG, "[rlm_ruby]: ruby_instantiate");
     ruby_init();
     ruby_init_loadpath();
     ruby_script("radiusd");
@@ -360,18 +358,14 @@ static int ruby_instantiate(CONF_SECTION *conf, void **instance) {
     /*
      * Set up a storage area for instance data
      */
-    data = rad_malloc(sizeof (*data));
-    if (!data) {
-        return -1;
-    }
-    memset(data, 0, sizeof (*data));
+    *instance = data = talloc_zero(conf, rlm_ruby_t);
+    if (!data) return -1;
 
     /*
      * If the configuration parameters can't be parsed, then
      * fail.
      */
     if (cf_section_parse(conf, data, module_config) < 0) {
-        free(data);
         return -1;
     }
 
@@ -381,7 +375,6 @@ static int ruby_instantiate(CONF_SECTION *conf, void **instance) {
 
     if ((module = data->pModule_builtin = rb_define_module(data->moduleName)) == 0) {
         radlog(L_ERR, "Ruby rb_define_module failed");
-        free(data);
         return -1;
     }
     /*
@@ -408,7 +401,6 @@ static int ruby_instantiate(CONF_SECTION *conf, void **instance) {
      * Import user modules.
      */
 #define RLM_RUBY_LOAD(foo) if (load_ruby_function(#foo, &data->func_##foo, data->pModule_builtin)==-1) { \
-       /* TODO: check if we need to cleanup data */ \
        return -1; \
     }
 
@@ -427,8 +419,6 @@ static int ruby_instantiate(CONF_SECTION *conf, void **instance) {
 #endif
     RLM_RUBY_LOAD(detach);
 
-    *instance = data;
-
     /* Call the instantiate function.  No request.  Use the return value. */
     return ruby_function(NULL, data->func_instantiate, data->pModule_builtin, "instantiate");
 }
@@ -453,24 +443,14 @@ RLM_RUBY_FUNC(recvcoa)
 RLM_RUBY_FUNC(sendcoa)
 #endif
 
-static int ruby_detach(void *instance) {
-    int return_value;
-
-    /* Default return value is failure */
-    return_value = -1;
-    free(instance);
+static int ruby_detach(UNUSED void *instance)
+{
     ruby_finalize();
     ruby_cleanup(0);
-    radlog(L_DBG, "ruby_detach done");
-
-    //Ok, we cheat, and returon ok value for now :)
-    return_value = RLM_MODULE_OK;
 
-    /* Return the specified by the Ruby module */
-    return return_value;
+    return 0;
 }
 
-
 /*
  *     The module name should be the only globally exported symbol.
  *     That is, everything else should be 'static'.
index c17d1b0..ff8bdbb 100644 (file)
@@ -427,7 +427,6 @@ static int securid_detach(void *instance)
 
        pthread_mutex_destroy(&(inst->session_mutex));
 
-       free(inst);
        return 0;
 }
 
@@ -437,14 +436,12 @@ static rlm_rcode_t securid_instantiate(CONF_SECTION *conf, void **instance)
        rlm_securid_t *inst;
 
        /* Set up a storage area for instance data */
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst)  return -1;
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_securid_t);
+       if (!inst) return -1;
 
         /* If the configuration parameters can't be parsed, then fail. */
        if (cf_section_parse(conf, inst, module_config) < 0) {
                radlog(L_ERR, "rlm_securid: Unable to parse configuration section.");
-               securid_detach(inst);
                return -1;
         }
 
@@ -455,13 +452,10 @@ static rlm_rcode_t securid_instantiate(CONF_SECTION *conf, void **instance)
        inst->session_tree = rbtree_create(securid_session_cmp, NULL, 0);
        if (!inst->session_tree) {
                radlog(L_ERR, "rlm_securid: Cannot initialize session tree.");
-               securid_detach(inst);
                return -1;
        }
 
        pthread_mutex_init(&(inst->session_mutex), NULL);
-
-        *instance = inst;
         return 0;
 }
 
index c67faec..89ca101 100644 (file)
@@ -76,18 +76,13 @@ static int sim_file_instantiate(CONF_SECTION *conf, void **instance)
 {
        struct sim_file_instance *inst;
 
-       inst = rad_malloc(sizeof *inst);
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, struct sim_file_instance);
+       if (!inst) return -1;
 
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
-       *instance = inst;
        return 0;
 }
 
@@ -242,25 +237,13 @@ static rlm_rcode_t sim_file_authorize(void *instance, REQUEST *request)
 }
 
 
-/*
- *     Clean up.
- */
-static int sim_file_detach(void *instance)
-{
-       struct sim_file_instance *inst = instance;
-
-       free(inst);
-       return 0;
-}
-
-
 /* globally exported name */
 module_t rlm_sim_files = {
        RLM_MODULE_INIT,
        "sim_files",
        0,                              /* type: reserved */
        sim_file_instantiate,           /* instantiation */
-       sim_file_detach,                /* detach */
+       NULL,                           /* detach */
        {
                NULL,                   /* authentication */
                sim_file_authorize,     /* authorization */
index 3db22bb..65478da 100644 (file)
@@ -75,23 +75,17 @@ static int smsotp_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_smsotp_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
                return -1;
        }
 
-       *instance = data;
-
        return 0;
 }
 
@@ -227,16 +221,6 @@ static rlm_rcode_t smsotp_authorize(void *instance, REQUEST *request)
        return RLM_MODULE_OK;
 }
 
-/*
- *     Only free memory we allocated.  The strings allocated via
- *     cf_section_parse() do not need to be freed.
- */
-static int smsotp_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 /* forward declarations */
 static smsotp_fd_t *smsotp_fd_head = NULL;
 static pthread_mutex_t smsotp_fd_head_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -466,7 +450,7 @@ module_t rlm_smsotp = {
        "smsotp",
        RLM_TYPE_THREAD_SAFE,           /* type */
        smsotp_instantiate,             /* instantiation */
-       smsotp_detach,                  /* detach */
+       NULL,                           /* detach */
        {
                smsotp_authenticate,    /* authentication */
                smsotp_authorize,       /* authorization */
index cc6e99c..3127df7 100644 (file)
@@ -104,35 +104,32 @@ static const CONF_PARSER module_config[] = {
        { NULL, -1, 0, NULL, NULL }             /* end the list */
 };
 
-static int soh_detach(void *instance) {
+static int soh_detach(void *instance)
+{
        rlm_soh_t       *inst = instance;
 
        if (inst->xlat_name) {
                xlat_unregister(inst->xlat_name, soh_xlat, instance);
-               free(inst->xlat_name);
        }
-       free(instance);
        return 0;
 }
 
-static int soh_instantiate(CONF_SECTION *conf, void **instance) {
+static int soh_instantiate(CONF_SECTION *conf, void **instance)
+{
        const char *name;
        rlm_soh_t *inst;
 
-       inst = *instance = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_soh_t);
+       if (!inst) return -1;
 
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               free(inst);
                return -1;
        }
 
        name = cf_section_name2(conf);
        if (!name) name = cf_section_name1(conf);
-       inst->xlat_name = strdup(name);
+       inst->xlat_name = name;
+       if (!inst->xlat_name) return -1;
        xlat_register(inst->xlat_name, soh_xlat, inst);
 
        return 0;
@@ -223,7 +220,7 @@ module_t rlm_soh = {
        "SoH",
        RLM_TYPE_THREAD_SAFE,           /* type */
        soh_instantiate,                /* instantiation */
-       soh_detach,             /* detach */
+       soh_detach,                     /* detach */
        {
                NULL,                   /* authenticate */
                soh_authorize,          /* authorize */
index 4763ffa..627ee67 100644 (file)
@@ -91,17 +91,6 @@ static int str2rcode(const char *s)
        }
 }
 
-static int sometimes_detach(void *instance)
-{
-       rlm_sometimes_t *inst = instance;
-
-       free(inst->rcode_str);
-       free(inst->key);
-       free(inst);
-
-       return 0;
-}
-
 static int sometimes_instantiate(CONF_SECTION *conf, void **instance)
 {
        rlm_sometimes_t *inst;
@@ -109,18 +98,14 @@ static int sometimes_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_sometimes_t);
+       if (!inst) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               sometimes_detach(inst);
                return -1;
        }
 
@@ -129,7 +114,6 @@ static int sometimes_instantiate(CONF_SECTION *conf, void **instance)
         */
        inst->rcode = str2rcode(inst->rcode_str);
        if (inst->rcode == -1) {
-               sometimes_detach(inst);
                return -1;
        }
 
@@ -137,7 +121,6 @@ static int sometimes_instantiate(CONF_SECTION *conf, void **instance)
        if (!inst->da) {
                radlog(L_ERR, "rlm_sometimes; Unknown attributes %s", inst->key);
                return -1;
-               return -1;
        }
 
        *instance = inst;
@@ -238,7 +221,7 @@ module_t rlm_sometimes = {
        "sometimes",
        RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
        sometimes_instantiate,          /* instantiation */
-       sometimes_detach,               /* detach */
+       NULL,                           /* detach */
        {
                sometimes_packet,       /* authentication */
                sometimes_packet,       /* authorization */
index ba1e4e8..bba5e24 100644 (file)
@@ -314,8 +314,7 @@ static int generate_sql_clients(SQL_INST *inst)
                DEBUG("rlm_sql (%s): Read entry nasname=%s,shortname=%s,secret=%s",inst->config->xlat_name,
                        row[1],row[2],row[4]);
 
-               c = rad_malloc(sizeof(*c));
-               memset(c, 0, sizeof(*c));
+               c = talloc_zero(inst, RADCLIENT);
 
 #ifdef WITH_DYNAMIC_CLIENTS
                c->dynamic = 1;
@@ -331,7 +330,7 @@ static int generate_sql_clients(SQL_INST *inst)
                        if ((c->prefix < 0) || (c->prefix > 128)) {
                                radlog(L_ERR, "rlm_sql (%s): Invalid Prefix value '%s' for IP.",
                                       inst->config->xlat_name, prefix_ptr + 1);
-                               free(c);
+                               talloc_free(c);
                                continue;
                        }
                        /* Replace '/' with '\0' */
@@ -345,12 +344,12 @@ static int generate_sql_clients(SQL_INST *inst)
                        radlog(L_ERR, "rlm_sql (%s): Failed to look up hostname %s: %s",
                               inst->config->xlat_name,
                               row[1], fr_strerror());
-                       free(c);
+                       talloc_free(c);
                        continue;
                } else {
                        char buffer[256];
                        ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
-                       c->longname = strdup(buffer);
+                       c->longname = talloc_strdup(c, buffer);
                }
 
                if (c->prefix < 0) switch (c->ipaddr.af) {
@@ -367,8 +366,8 @@ static int generate_sql_clients(SQL_INST *inst)
                /*
                 *      Other values (secret, shortname, nastype, virtual_server)
                 */
-               c->secret = strdup(row[4]);
-               c->shortname = strdup(row[2]);
+               c->secret = talloc_strdup(c, row[4]);
+               c->shortname = talloc_strdup(c, row[2]);
                if(row[3] != NULL)
                        c->nastype = strdup(row[3]);
 
@@ -762,18 +761,11 @@ static int rlm_sql_detach(void *instance)
        paircompare_unregister(PW_SQL_GROUP, sql_groupcmp);
        
        if (inst->config) {
-               if (inst->config->postauth) free(inst->config->postauth);
-               if (inst->config->accounting) free(inst->config->accounting);
-       
                if (inst->pool) sql_poolfree(inst);
 
                if (inst->config->xlat_name) {
                        xlat_unregister(inst->config->xlat_name, sql_xlat, instance);
-                       rad_cfree(inst->config->xlat_name);
                }
-
-               free(inst->config);
-               inst->config = NULL;
        }
 
        if (inst->handle) {
@@ -784,7 +776,6 @@ static int rlm_sql_detach(void *instance)
                lt_dlclose(inst->handle);       /* ignore any errors */
 #endif
        }
-       free(inst);
 
        return 0;
 }
@@ -807,15 +798,11 @@ static int parse_sub_section(CONF_SECTION *parent,
                return 0;
        }
        
-       *config = rad_calloc(sizeof(**config));
+       *config = talloc_zero(parent, sql_acct_section_t);
        if (cf_section_parse(cs, *config, acct_section_config) < 0) {
                radlog(L_ERR, "rlm_sql (%s): Couldn't find configuration for "
                       "%s, will return NOOP for calls from this section",
                       inst->config->xlat_name, name);
-               
-               free(*config);
-               *config = NULL;
-               
                return -1;
        }
                
@@ -824,21 +811,20 @@ static int parse_sub_section(CONF_SECTION *parent,
        return 0;
 }
 
-static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
+static int rlm_sql_instantiate(CONF_SECTION *conf, void **instance)
 {
        SQL_INST *inst;
        const char *xlat_name;
 
-       inst = rad_calloc(sizeof(SQL_INST));
+       *instance = inst = talloc_zero(conf, SQL_INST);
+       if (!inst) return -1;
        
        /*
         *      Cache the SQL-User-Name DICT_ATTR, so we can be slightly
         *      more efficient about creating SQL-User-Name attributes.
         */
        inst->sql_user = dict_attrbyname("SQL-User-Name");
-       if (!inst->sql_user) {
-               goto error;
-       }
+       if (!inst->sql_user) return -1;
 
        /*
         *      Export these methods, too.  This avoids RTDL_GLOBAL.
@@ -851,7 +837,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
        inst->sql_select_query          = rlm_sql_select_query;
        inst->sql_fetch_row             = rlm_sql_fetch_row;
        
-       inst->config = rad_calloc(sizeof(SQL_CONFIG));
+       inst->config = talloc_zero(inst, SQL_CONFIG);
        inst->cs = conf;
 
        xlat_name = cf_section_name2(conf);
@@ -865,8 +851,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
                /*
                 *      Allocate room for <instance>-SQL-Group
                 */
-               group_name = rad_malloc((strlen(xlat_name) + 1 + 11) * sizeof(char));
-               sprintf(group_name,"%s-SQL-Group", xlat_name);
+               group_name = talloc_asprintf(inst, "%s-SQL-Group", xlat_name);
                DEBUG("rlm_sql (%s): Creating new attribute %s",
                      xlat_name, group_name);
 
@@ -875,18 +860,14 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
                        radlog(L_ERR, "rlm_sql (%s): Failed to create "
                               "attribute %s: %s", xlat_name, group_name,
                               fr_strerror());
-                       free(group_name);
-                       goto error;
+                       return -1;
                }
 
                dattr = dict_attrbyname(group_name);
-               if (dattr == NULL){
+               if (!dattr) {
                        radlog(L_ERR, "rlm_sql (%s): Failed to create "
                               "attribute %s", xlat_name, group_name);
-                              
-                       free(group_name);
-
-                       goto error;
+                       return -1;
                }
 
                if (inst->config->groupmemb_query && 
@@ -896,17 +877,14 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
                        paircompare_register(dattr->attr, PW_USER_NAME,
                                             sql_groupcmp, inst);
                }
-
-               free(group_name);
        }
        
        rad_assert(xlat_name);
-       
 
        /*
         *      Register the SQL xlat function
         */
-       inst->config->xlat_name = strdup(xlat_name);
+       inst->config->xlat_name = talloc_strdup(inst->config, xlat_name);
        xlat_register(xlat_name, sql_xlat, inst);
 
        /*
@@ -921,7 +899,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
                               RLM_COMPONENT_POST_AUTH) < 0)) {
                radlog(L_ERR, "rlm_sql (%s): Failed parsing configuration",
                       inst->config->xlat_name);
-               goto error;
+               return -1;
        }
                
        /*
@@ -930,7 +908,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
        if (strncmp(inst->config->sql_driver, "rlm_sql_", 8) != 0) {
                radlog(L_ERR, "rlm_sql (%s): \"%s\" is NOT an SQL driver!",
                       inst->config->xlat_name, inst->config->sql_driver);
-               goto error;
+               return -1;
        }
 
        /*
@@ -943,8 +921,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
                       lt_dlerror());
                radlog(L_ERR, "Make sure it (and all its dependent libraries!)"
                       "are in the search path of your system's ld.");
-
-               goto error;
+               return -1;
        }
 
        inst->module = (rlm_sql_module_t *) lt_dlsym(inst->handle,
@@ -953,8 +930,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
                radlog(L_ERR, "Could not link symbol %s: %s",
                       inst->config->sql_driver,
                       lt_dlerror());
-
-               goto error;
+               return -1;
        }
 
        radlog(L_INFO, "rlm_sql (%s): Driver %s (module %s) loaded and linked",
@@ -969,8 +945,7 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
               inst->config->sql_server, inst->config->sql_port,
               inst->config->sql_db);
               
-       if (sql_init_socketpool(inst) < 0)
-               goto error;
+       if (sql_init_socketpool(inst) < 0) return -1;
 
        if (inst->config->groupmemb_query && 
            inst->config->groupmemb_query[0]) {
@@ -980,19 +955,11 @@ static int rlm_sql_instantiate(CONF_SECTION * conf, void **instance)
        if (inst->config->do_clients) {
                if (generate_sql_clients(inst) == -1){
                        radlog(L_ERR, "Failed to load clients from SQL.");
-                       
-                       goto error;
+                       return -1;
                }
        }
 
-       *instance = inst;
-
        return RLM_MODULE_OK;
-       
-       error:
-       rlm_sql_detach(inst);
-       
-       return -1;
 }
 
 
index 4157adc..10c858b 100644 (file)
@@ -31,8 +31,6 @@ RCSID("$Id$")
 
 #define MAX_QUERY_LEN 1024
 
-static int sqlcounter_detach(void *instance);
-
 /*
  *     Note: When your counter spans more than 1 period (ie 3 months
  *     or 2 weeks), this module probably does NOT do what you want! It
@@ -344,6 +342,15 @@ static int sqlcounter_cmp(void *instance, REQUEST *req,
 }
 
 
+static int sqlcounter_detach(void *instance)
+{
+       rlm_sqlcounter_t *inst = (rlm_sqlcounter_t *)instance;
+
+       paircompare_unregister(inst->dict_attr->attr, sqlcounter_cmp);
+
+       return 0;
+}
+
 /*
  *     Do any per-module initialization that is separate to each
  *     configured instance of the module.  e.g. set up connections
@@ -364,12 +371,8 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       if (!data) {
-               radlog(L_ERR, "rlm_sqlcounter: Not enough memory.");
-               return -1;
-       }
-       memset(data, 0, sizeof(*data));
+       *instance = data = talloc_zero(conf, rlm_sqlcounter_t);
+       if (!data) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
@@ -377,7 +380,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (cf_section_parse(conf, data, module_config) < 0) {
                radlog(L_ERR, "rlm_sqlcounter: Unable to parse parameters.");
-               sqlcounter_detach(data);
                return -1;
        }
 
@@ -386,7 +388,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (data->query == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: 'query' must be set.");
-               sqlcounter_detach(data);
                return -1;
        }
 
@@ -395,14 +396,12 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (data->key_name == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: 'key' must be set.");
-               sqlcounter_detach(data);
                return -1;
        }
        dattr = dict_attrbyname(data->key_name);
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: No such attribute %s",
                                data->key_name);
-               sqlcounter_detach(data);
                return -1;
        }
        data->key_attr = dattr;
@@ -411,7 +410,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: No such attribute %s",
                               data->reply_name);
-               sqlcounter_detach(data);
                return -1;
        }
        data->reply_attr = dattr;
@@ -423,7 +421,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (data->sqlmod_inst == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: 'sqlmod-inst' must be set.");
-               sqlcounter_detach(data);
                return -1;
        }
 
@@ -432,7 +429,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (data->counter_name == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: 'counter-name' must be set.");
-               sqlcounter_detach(data);
                return -1;
        }
 
@@ -442,12 +438,10 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: Failed to create counter attribute %s",
                                data->counter_name);
-               sqlcounter_detach(data);
                return -1;
        }
        if (dattr->vendor != 0) {
                radlog(L_ERR, "Counter attribute must not be a VSA");
-               sqlcounter_detach(data);
                return -1;
        }
        data->dict_attr = dattr;
@@ -457,7 +451,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (data->check_name == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: 'check-name' must be set.");
-               sqlcounter_detach(data);
                return -1;
        }
        dict_addattr(data->check_name, 0, PW_TYPE_INTEGER, -1, flags);
@@ -465,7 +458,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
        if (dattr == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: Failed to create check attribute %s",
                                data->check_name);
-               sqlcounter_detach(data);
                return -1;
        }
        DEBUG2("rlm_sqlcounter: Check attribute %s is number %d",
@@ -476,7 +468,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
         */
        if (data->reset == NULL) {
                radlog(L_ERR, "rlm_sqlcounter: 'reset' must be set.");
-               sqlcounter_detach(data);
                return -1;
        }
        now = time(NULL);
@@ -484,7 +475,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
 
        if (find_next_reset(data,now) == -1) {
                radlog(L_ERR, "rlm_sqlcounter: Failed to find the next reset time.");
-               sqlcounter_detach(data);
                return -1;
        }
 
@@ -495,7 +485,6 @@ static int sqlcounter_instantiate(CONF_SECTION *conf, void **instance)
 
        if (find_prev_reset(data,now) == -1) {
                radlog(L_ERR, "rlm_sqlcounter: Failed to find the previous reset time.");
-               sqlcounter_detach(data);
                return -1;
        }
 
@@ -659,16 +648,6 @@ static rlm_rcode_t sqlcounter_authorize(void *instance, REQUEST *request)
        return rcode;
 }
 
-static int sqlcounter_detach(void *instance)
-{
-       rlm_sqlcounter_t *inst = (rlm_sqlcounter_t *)instance;
-
-       paircompare_unregister(inst->dict_attr->attr, sqlcounter_cmp);
-
-       free(inst);
-       return 0;
-}
-
 /*
  *     The module name should be the only globally exported symbol.
  *     That is, everything else should be 'static'.
index eb1f393..382210d 100644 (file)
@@ -281,69 +281,52 @@ static int nvp_cleanup(rlm_sqlhpwippool_t *data)
        return 1;
 }
 
-static int sqlhpwippool_detach(void *instance)
-{
-       rlm_sqlhpwippool_t *data = (rlm_sqlhpwippool_t *) instance;
-
-       /* (*data) is zeroed on instantiation */
-       if (data->sqlinst_name) free(data->sqlinst_name);
-       if (data->db_name)      free(data->db_name);
-       free(data);
-
-       return 0;
-}
-
 /* standard foobar code */
 static int sqlhpwippool_instantiate(CONF_SECTION *conf, void **instance)
 {
-       rlm_sqlhpwippool_t *data;
-       module_instance_t *modinst;
+       rlm_sqlhpwippool_t *inst;
+       module_instance_t *sqlinst;
 
        /* set up a storage area for instance data */
-       data = rad_malloc(sizeof(*data));
-       if (!data) return -1;
-       memset(data, 0, sizeof(*data)); /* so _detach will know what to free */
+       *instance = inst = talloc_zero(conf, rlm_sqlhpwippool_t);
+       if (!inst) return -1;
 
        /* fail if the configuration parameters can't be parsed */
-       if (cf_section_parse(conf, data, module_config) < 0) {
-               sqlhpwippool_detach(*instance);
+       if (cf_section_parse(conf, inst, module_config) < 0) {
                return -1;
        }
 
        /* save my name */
-       data->myname = cf_section_name2(conf);
-       if (!data->myname) {
-               data->myname = "(no name)";
+       inst->myname = cf_section_name2(conf);
+       if (!inst->myname) {
+               inst->myname = "(no name)";
        }
 
-       data->sincesync = 0;
+       inst->sincesync = 0;
 
-       modinst = find_module_instance(cf_section_find("modules"), (data->sqlinst_name), 1 );
-       if (!modinst) {
-               nvp_log(__LINE__, data, L_ERR,
+       sqlinst = find_module_instance(cf_section_find("modules"), (inst->sqlinst_name), 1 );
+       if (!sqlinst) {
+               nvp_log(__LINE__, inst, L_ERR,
                        "sqlhpwippool_instantiate(): cannot find module instance "
                        "named \"%s\"",
-                       data->sqlinst_name);
+                       inst->sqlinst_name);
                return -1;
        }
 
        /* check if the given instance is really a rlm_sql instance */
-       if (strcmp(modinst->entry->name, "rlm_sql") != 0) {
-               nvp_log(__LINE__, data, L_ERR,
+       if (strcmp(sqlinst->entry->name, "rlm_sql") != 0) {
+               nvp_log(__LINE__, inst, L_ERR,
                        "sqlhpwippool_instantiate(): given instance (%s) is not "
                        "an instance of the rlm_sql module",
-                       data->sqlinst_name);
+                       inst->sqlinst_name);
                return -1;
        }
 
        /* save pointers to useful "objects" */
-       data->sqlinst = (SQL_INST *) modinst->insthandle;
-       data->db = (rlm_sql_module_t *) data->sqlinst->module;
-
-       /* everything went ok, cleanup pool */
-       *instance = data;
+       inst->sqlinst = (SQL_INST *) sqlinst->insthandle;
+       inst->db = (rlm_sql_module_t *) inst->sqlinst->module;
 
-       return ((nvp_cleanup(data)) ? 0 : -1);
+       return ((nvp_cleanup(inst)) ? 0 : -1);
 }
 
 /* assign new IP address, if required */
@@ -362,12 +345,12 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
                      weights_sum, used_sum, ip_start, ip_stop, connid;
        long prio;
 
-       rlm_sqlhpwippool_t *data = (rlm_sqlhpwippool_t *) instance;
+       rlm_sqlhpwippool_t *inst = (rlm_sqlhpwippool_t *) instance;
 
        /* if IP is already there, then nothing to do */
        vp = pairfind(request->reply->vps, PW_FRAMED_IP_ADDRESS, 0, TAG_ANY);
        if (vp) {
-               nvp_log(__LINE__, data, L_DBG,
+               nvp_log(__LINE__, inst, L_DBG,
                        "sqlhpwippool_postauth(): IP address "
                        "already in the reply packet - exiting");
                return RLM_MODULE_NOOP;
@@ -377,12 +360,12 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
        vp = pairfind(request->reply->vps, ASN_IP_POOL_NAME, VENDORPEC_ASN, TAG_ANY);
        if (vp) {
                pname = vp->vp_strvalue;
-               nvp_log(__LINE__, data, L_DBG,
+               nvp_log(__LINE__, inst, L_DBG,
                        "sqlhpwippool_postauth(): pool name = '%s'",
                        pname);
        }
        else {
-               nvp_log(__LINE__, data, L_DBG,
+               nvp_log(__LINE__, inst, L_DBG,
                        "sqlhpwippool_postauth(): no IP pool name - exiting");
                return RLM_MODULE_NOOP;
        }
@@ -394,64 +377,64 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
        }
        else {
                nasip = 0;
-               nvp_log(__LINE__, data, L_DBG,
+               nvp_log(__LINE__, inst, L_DBG,
                        "sqlhpwippool_postauth(): no NAS IP address in "
                        "the request packet - using \"0.0.0.0/0\" (any)");
        }
 
        /* get our database connection */
-       sqlsock = sql_get_socket(data->sqlinst);
+       sqlsock = sql_get_socket(inst->sqlinst);
        if (!sqlsock) {
-               nvp_log(__LINE__, data, L_ERR,
+               nvp_log(__LINE__, inst, L_ERR,
                        "sqlhpwippool_postauth(): error while requesting an SQL socket");
                return RLM_MODULE_FAIL;
        }
 
        /* get connection id as temporary unique integer */
-       if (nvp_select(__LINE__, data, sqlsock, "SELECT CONNECTION_ID()") < 1) {
-               nvp_log(__LINE__, data, L_ERR, "sqlhpwippool_postauth(): WTF ;-)!");
-               nvp_select_finish(data, sqlsock);
-               sql_release_socket(data->sqlinst, sqlsock);
+       if (nvp_select(__LINE__, inst, sqlsock, "SELECT CONNECTION_ID()") < 1) {
+               nvp_log(__LINE__, inst, L_ERR, "sqlhpwippool_postauth(): WTF ;-)!");
+               nvp_select_finish(inst, sqlsock);
+               sql_release_socket(inst->sqlinst, sqlsock);
                return RLM_MODULE_FAIL;
        }
 
        connid = strtoul(sqlsock->row[0], (char **) NULL, 10);
-       nvp_select_finish(data, sqlsock);
+       nvp_select_finish(inst, sqlsock);
 
        /* synchronize with radacct db, if needed */
-       if (++data->sincesync >= data->syncafter
+       if (++inst->sincesync >= inst->syncafter
 #ifdef HAVE_PTHREAD_D
-           && (pthread_mutex_trylock(&data->mutex)) == 0
+           && (pthread_mutex_trylock(&inst->mutex)) == 0
 #endif
           ) {
                int r;
 
-               data->sincesync = 0;
+               inst->sincesync = 0;
 
-               nvp_log(__LINE__, data, L_DBG,
+               nvp_log(__LINE__, inst, L_DBG,
                        "sqlhpwippool_postauth(): syncing with radacct table");
 
-               r = (nvp_freeclosed(data, sqlsock) && nvp_syncfree(data, sqlsock));
+               r = (nvp_freeclosed(inst, sqlsock) && nvp_syncfree(inst, sqlsock));
 
 #ifdef HAVE_PTHREAD_D
-               pthread_mutex_unlock(&data->mutex);
+               pthread_mutex_unlock(&inst->mutex);
 #endif
 
                if (!r) {
-                       nvp_log(__LINE__, data, L_ERR,
+                       nvp_log(__LINE__, inst, L_ERR,
                                "sqlhpwippool_postauth(): synchronization failed");
-                       sql_release_socket(data->sqlinst, sqlsock);
+                       sql_release_socket(inst->sqlinst, sqlsock);
                        return RLM_MODULE_FAIL;
                }
        }
 
        for (s_gid = 0; s_gid < RLM_NETVIM_MAX_ROWS && !(ip.s_addr); s_gid++) {
-               nvp_log(__LINE__, data, L_DBG,
+               nvp_log(__LINE__, inst, L_DBG,
                        "sqlhpwippool_postauth(): selecting gid on position %lu",
                        s_gid);
 
                /* find the most specific group which NAS belongs to */
-               switch (nvp_select(__LINE__, data, sqlsock,
+               switch (nvp_select(__LINE__, inst, sqlsock,
                       "SELECT `host_groups`.`gid` "
                        "FROM "
                                "`%s`.`host_groups`, "
@@ -464,28 +447,28 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
                                "%lu BETWEEN `gid_ip`.`ip_start` AND `gid_ip`.`ip_stop` "
                        "ORDER BY (`gid_ip`.`ip_stop` - `gid_ip`.`ip_start`) ASC "
                        "LIMIT %lu, 1",
-                      data->db_name, nasip, s_gid)) {
+                      inst->db_name, nasip, s_gid)) {
                        case -1:
-                               nvp_log(__LINE__, data, L_ERR,
+                               nvp_log(__LINE__, inst, L_ERR,
                                        "sqlhpwippool_postauth(): couldn't find "
                                        "any more matching host groups");
                                goto end_gid;                  /* exit the main loop */
                        case 0:
-                               sql_release_socket(data->sqlinst, sqlsock);
+                               sql_release_socket(inst->sqlinst, sqlsock);
                                return RLM_MODULE_FAIL;
                }
 
                /* store the group ID and free memory occupied by results */
                gid = strtoul(sqlsock->row[0], (char **) NULL, 10);
-               nvp_select_finish(data, sqlsock);
+               nvp_select_finish(inst, sqlsock);
 
                for (s_prio = 0; s_prio < RLM_NETVIM_MAX_ROWS && !(ip.s_addr); s_prio++) {
-                       nvp_log(__LINE__, data, L_DBG,
+                       nvp_log(__LINE__, inst, L_DBG,
                                "sqlhpwippool_postauth(): selecting prio on position %lu",
                                s_prio);
 
                        /* prepare to search for best fit pool */
-                       switch (nvp_select(__LINE__, data, sqlsock,
+                       switch (nvp_select(__LINE__, inst, sqlsock,
                                "SELECT "
                                        "`ip_pools`.`prio`, "
                                        "SUM(`ip_pools`.`weight`) AS `weights_sum`, "
@@ -505,15 +488,15 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
                                        "GROUP BY `prio` "
                                        "ORDER BY `prio` ASC "
                                        "LIMIT %lu, 1",
-                               data->db_name, gid, pname, s_prio)) {
+                               inst->db_name, gid, pname, s_prio)) {
                                case -1:
-                                       nvp_log(__LINE__, data, L_DBG,
+                                       nvp_log(__LINE__, inst, L_DBG,
                                                "sqlhpwippool_postauth(): couldn't find "
                                                "any more matching pools for gid = %u",
                                                gid);
                                        goto end_prio;               /* select next gid */
                                case 0:
-                                       sql_release_socket(data->sqlinst, sqlsock);
+                                       sql_release_socket(inst->sqlinst, sqlsock);
                                        return RLM_MODULE_FAIL;
                        }
 
@@ -523,15 +506,15 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
                        used_sum = strtoul(sqlsock->row[2], (char **) NULL, 10);
 
                        /* free memory */
-                       nvp_select_finish(data, sqlsock);
+                       nvp_select_finish(inst, sqlsock);
 
                        for (s_pid = 0; s_pid < RLM_NETVIM_MAX_ROWS && !(ip.s_addr); s_pid++) {
-                               nvp_log(__LINE__, data, L_DBG,
+                               nvp_log(__LINE__, inst, L_DBG,
                                        "sqlhpwippool_postauth(): selecting PID on position %lu",
                                        s_pid);
 
                                /* search for best fit pool */
-                               switch (nvp_select(__LINE__, data, sqlsock,
+                               switch (nvp_select(__LINE__, inst, sqlsock,
                                        "SELECT "
                                                "`ip_pools`.`pid`, "
                                                "`ip_pools`.`ip_start`, "
@@ -550,27 +533,27 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
                                                        "`prio` = %ld "
                                                "ORDER BY (`weight`/%lu.0000 - (`total` - `free`)/%lu) DESC "
                                                "LIMIT %lu, 1",
-                                       data->db_name, gid, pname, prio,
+                                       inst->db_name, gid, pname, prio,
                                        weights_sum, used_sum, s_pid)) {
                                        case -1:
-                                               nvp_log(__LINE__, data, L_DBG,
+                                               nvp_log(__LINE__, inst, L_DBG,
                                                        "sqlhpwippool_postauth(): couldn't find any more "
                                                        "matching pools of prio = %ld for gid = %lu",
                                                        prio, gid);
                                                goto end_pid;              /* select next prio */
                                        case 0:
-                                               sql_release_socket(data->sqlinst, sqlsock);
+                                               sql_release_socket(inst->sqlinst, sqlsock);
                                                return RLM_MODULE_FAIL;
                                }
 
-                               /* store the data and free memory occupied by results */
+                               /* store the inst and free memory occupied by results */
                                pid = strtoul(sqlsock->row[0], (char **) NULL, 10);
                                ip_start = strtoul(sqlsock->row[1], (char **) NULL, 10);
                                ip_stop = strtoul(sqlsock->row[2], (char **) NULL, 10);
-                               nvp_select_finish(data, sqlsock);
+                               nvp_select_finish(inst, sqlsock);
 
                                /* reserve an IP address */
-                               if (!nvp_query(__LINE__, data, sqlsock,
+                               if (!nvp_query(__LINE__, inst, sqlsock,
                                    "UPDATE `%s`.`ips` "
                                        "SET "
                                                "`pid` = %lu, "
@@ -585,52 +568,52 @@ static rlm_rcode_t sqlhpwippool_postauth(void *instance, REQUEST *request)
                                                ") "
                                        "ORDER BY RAND() "
                                        "LIMIT 1",
-                                   data->db_name, pid, connid, data->freeafter, ip_start, ip_stop)) {
-                                       sql_release_socket(data->sqlinst, sqlsock);
+                                   inst->db_name, pid, connid, inst->freeafter, ip_start, ip_stop)) {
+                                       sql_release_socket(inst->sqlinst, sqlsock);
                                        return RLM_MODULE_FAIL;
                                }
                                else {
-                                       nvp_finish(data, sqlsock);
+                                       nvp_finish(inst, sqlsock);
                                }
 
                                /* select assigned IP address */
-                               switch (nvp_select(__LINE__, data, sqlsock,
+                               switch (nvp_select(__LINE__, inst, sqlsock,
                                        "SELECT `ip` "
                                                "FROM `%s`.`ips` "
                                                "WHERE `rsv_by` = '" RLM_NETVIM_TMP_PREFIX "%lu' "
                                                "ORDER BY `rsv_since` DESC "
                                                "LIMIT 1",
-                                       data->db_name, connid)) {
+                                       inst->db_name, connid)) {
                                        case -1:
-                                               nvp_log(__LINE__, data, L_ERR,
+                                               nvp_log(__LINE__, inst, L_ERR,
                                                        "sqlhpwippool_postauth(): couldn't reserve an IP address "
                                                        "from pool of pid = %lu (prio = %ld, gid = %lu)",
                                                        pid, prio, gid);
                                                continue;                            /* select next pid */
                                        case 0:
-                                               sql_release_socket(data->sqlinst, sqlsock);
+                                               sql_release_socket(inst->sqlinst, sqlsock);
                                                return RLM_MODULE_FAIL;
                                }
 
                                /* update free IPs count */
-                               if (!nvp_query(__LINE__, data, sqlsock,
+                               if (!nvp_query(__LINE__, inst, sqlsock,
                                    "UPDATE `%s`.`ip_pools` "
                                        "SET "
                                                "`free` = `free` - 1 "
                                        "WHERE "
                                                "`pid` = %lu "
                                "LIMIT 1",
-                                   data->db_name, pid)) {
-                                       sql_release_socket(data->sqlinst, sqlsock);
+                                   inst->db_name, pid)) {
+                                       sql_release_socket(inst->sqlinst, sqlsock);
                                        return RLM_MODULE_FAIL;
                                }
                                else {
-                                       nvp_finish(data, sqlsock);
+                                       nvp_finish(inst, sqlsock);
                                }
 
                                /* get assigned IP and free memory */
                                ip.s_addr = htonl(strtoul(sqlsock->row[0], (char **) NULL, 10));
-                               nvp_select_finish(data, sqlsock);
+                               nvp_select_finish(inst, sqlsock);
                        } /* pid */
 end_pid: continue;           /* stupid */
                } /* prio */
@@ -639,19 +622,19 @@ end_prio: continue;          /* stupid */
 end_gid:
 
        /* release SQL socket */
-       sql_release_socket(data->sqlinst, sqlsock);
+       sql_release_socket(inst->sqlinst, sqlsock);
 
        /* no free IP address found */
        if (!ip.s_addr) {
-               nvp_log(__LINE__, data, L_INFO,
+               nvp_log(__LINE__, inst, L_INFO,
                        "sqlhpwippool_postauth(): no free IP address found!");
 
-               if (data->nofreefail) {
-                       nvp_log(__LINE__, data, L_DBG, "sqlhpwippool_postauth(): rejecting user");
+               if (inst->nofreefail) {
+                       nvp_log(__LINE__, inst, L_DBG, "sqlhpwippool_postauth(): rejecting user");
                        return RLM_MODULE_REJECT;
                }
                else {
-                       nvp_log(__LINE__, data, L_DBG, "sqlhpwippool_postauth(): exiting");
+                       nvp_log(__LINE__, inst, L_DBG, "sqlhpwippool_postauth(): exiting");
                        return RLM_MODULE_NOOP;
                }
        }
@@ -661,7 +644,7 @@ end_gid:
                               PW_FRAMED_IP_ADDRESS, 0);
        vp->vp_ipaddr = ip.s_addr;
 
-       nvp_log(__LINE__, data, L_DBG, "sqlhpwippool_postauth(): returning %s",
+       nvp_log(__LINE__, inst, L_DBG, "sqlhpwippool_postauth(): returning %s",
                inet_ntoa(ip));
        return RLM_MODULE_OK;
 }
@@ -676,7 +659,7 @@ static rlm_rcode_t sqlhpwippool_accounting(void *instance, REQUEST *request)
        uint32_t framedip = 0;     /* client's IP, host byte order */
        uint32_t acct_type;
 
-       rlm_sqlhpwippool_t *data = (rlm_sqlhpwippool_t *) instance;
+       rlm_sqlhpwippool_t *inst = (rlm_sqlhpwippool_t *) instance;
 
        /* if no unique session ID, don't even try */
        vp = pairfind(request->packet->vps, PW_ACCT_UNIQUE_SESSION_ID, 0, TAG_ANY);
@@ -684,7 +667,7 @@ static rlm_rcode_t sqlhpwippool_accounting(void *instance, REQUEST *request)
                sessid = vp->vp_strvalue;
        }
        else {
-               nvp_log(__LINE__, data, L_ERR,
+               nvp_log(__LINE__, inst, L_ERR,
                        "sqlhpwippool_accounting(): unique session ID not found");
                return RLM_MODULE_FAIL;
        }
@@ -694,7 +677,7 @@ static rlm_rcode_t sqlhpwippool_accounting(void *instance, REQUEST *request)
                acct_type = vp->vp_integer;
        }
        else {
-               nvp_log(__LINE__, data, L_ERR, "sqlhpwippool_accounting(): "
+               nvp_log(__LINE__, inst, L_ERR, "sqlhpwippool_accounting(): "
                                               "couldn't find type of accounting packet");
                return RLM_MODULE_FAIL;
        }
@@ -708,9 +691,9 @@ static rlm_rcode_t sqlhpwippool_accounting(void *instance, REQUEST *request)
        }
 
        /* connect to database */
-       sqlsock = sql_get_socket(data->sqlinst);
+       sqlsock = sql_get_socket(inst->sqlinst);
        if (!sqlsock) {
-               nvp_log(__LINE__, data, L_ERR,
+               nvp_log(__LINE__, inst, L_ERR,
                        "sqlhpwippool_accounting(): couldn't connect to database");
                return RLM_MODULE_FAIL;
        }
@@ -721,28 +704,28 @@ static rlm_rcode_t sqlhpwippool_accounting(void *instance, REQUEST *request)
                case PW_STATUS_ALIVE:
                        vp = pairfind(request->packet->vps, PW_FRAMED_IP_ADDRESS, 0, TAG_ANY);
                        if (!vp) {
-                               nvp_log(__LINE__, data, L_ERR, "sqlhpwippool_accounting(): no framed IP");
-                               sql_release_socket(data->sqlinst, sqlsock);
+                               nvp_log(__LINE__, inst, L_ERR, "sqlhpwippool_accounting(): no framed IP");
+                               sql_release_socket(inst->sqlinst, sqlsock);
                                return RLM_MODULE_FAIL;
                        }
 
                        framedip = ntohl(vp->vp_ipaddr);
 
-                       if (!nvp_query(__LINE__, data, sqlsock,
+                       if (!nvp_query(__LINE__, inst, sqlsock,
                            "UPDATE `%s`.`ips` "
                                "SET "
                                        "`rsv_until` = 0, "
                                        "`rsv_by` = '%s' "
                                "WHERE `ip` = %lu",
-                           data->db_name, sessid, framedip)) {
-                               sql_release_socket(data->sqlinst, sqlsock);
+                           inst->db_name, sessid, framedip)) {
+                               sql_release_socket(inst->sqlinst, sqlsock);
                                return RLM_MODULE_FAIL;
                        }
-                       nvp_finish(data, sqlsock);
+                       nvp_finish(inst, sqlsock);
                        break;
 
                case PW_STATUS_STOP:
-                       if (!nvp_query(__LINE__, data, sqlsock,
+                       if (!nvp_query(__LINE__, inst, sqlsock,
                            "UPDATE `%s`.`ips`, `%1$s`.`ip_pools` "
                                "SET "
                                        "`ips`.`rsv_until` = NOW() + INTERVAL %u SECOND, "
@@ -750,41 +733,41 @@ static rlm_rcode_t sqlhpwippool_accounting(void *instance, REQUEST *request)
                                "WHERE "
                                        "`ips`.`rsv_by` = '%s' AND "
                                        "`ips`.`ip` BETWEEN `ip_pools`.`ip_start` AND `ip_pools`.`ip_stop`",
-                           data->db_name, data->freeafter, sessid)) {
-                               sql_release_socket(data->sqlinst, sqlsock);
+                           inst->db_name, inst->freeafter, sessid)) {
+                               sql_release_socket(inst->sqlinst, sqlsock);
                                return RLM_MODULE_FAIL;
                        }
-                       nvp_finish(data, sqlsock);
+                       nvp_finish(inst, sqlsock);
                break;
 
                case PW_STATUS_ACCOUNTING_OFF:
                case PW_STATUS_ACCOUNTING_ON:
                        vp = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS, 0, TAG_ANY);
                        if (!vp) {
-                               nvp_log(__LINE__, data, L_ERR, "sqlhpwippool_accounting(): no NAS IP");
-                               sql_release_socket(data->sqlinst, sqlsock);
+                               nvp_log(__LINE__, inst, L_ERR, "sqlhpwippool_accounting(): no NAS IP");
+                               sql_release_socket(inst->sqlinst, sqlsock);
                                return RLM_MODULE_FAIL;
                        }
 
                        nasip.s_addr = vp->vp_ipaddr;
                        strlcpy(nasipstr, inet_ntoa(nasip), sizeof(nasipstr));
 
-                       if (!nvp_query(__LINE__, data, sqlsock,
+                       if (!nvp_query(__LINE__, inst, sqlsock,
                            "UPDATE `%s`.`ips`, `radacct` "
                                "SET `ips`.`rsv_until` = NOW() + INTERVAL %u SECOND "
                                "WHERE "
                                        "`radacct`.`nasipaddress` = '%s' AND "
                                        "`ips`.`rsv_by` = `radacct`.`acctuniqueid`",
-                           data->db_name, data->freeafter, nasipstr)) {
-                               sql_release_socket(data->sqlinst, sqlsock);
+                           inst->db_name, inst->freeafter, nasipstr)) {
+                               sql_release_socket(inst->sqlinst, sqlsock);
                                return RLM_MODULE_FAIL;
                        }
-                       nvp_finish(data, sqlsock);
+                       nvp_finish(inst, sqlsock);
 
                        break;
        }
 
-       sql_release_socket(data->sqlinst, sqlsock);
+       sql_release_socket(inst->sqlinst, sqlsock);
        return RLM_MODULE_OK;
 }
 
@@ -793,7 +776,7 @@ module_t rlm_sqlhpwippool = {
        "sqlhpwippool",                 /* name */
        RLM_TYPE_THREAD_SAFE,           /* type */
        sqlhpwippool_instantiate,       /* instantiation */
-       sqlhpwippool_detach,            /* detach */
+       NULL,                           /* detach */
        {
                NULL,                   /* authentication */
                NULL,                   /* authorization */
index 493725a..f1446ac 100644 (file)
@@ -388,12 +388,6 @@ static int sqlippool_query1(char * out, int outlen, const char * fmt,
        return retval;
 }
 
-static int sqlippool_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 #define IS_EMPTY(_x) (!_x ||!*_x)
 
 /*
@@ -406,30 +400,27 @@ static int sqlippool_detach(void *instance)
  *     that must be referenced in later calls, store a handle to it
  *     in *instance otherwise put a null pointer there.
  */
-static int sqlippool_instantiate(CONF_SECTION * conf, void ** instance)
+static int sqlippool_instantiate(CONF_SECTION *conf, void **instance)
 {
-       module_instance_t *modinst;
-       rlm_sqlippool_t * data;
+       module_instance_t *sqlinst;
+       rlm_sqlippool_t *inst;
        const char * pool_name = NULL;
 
        /*
         *      Set up a storage area for instance data
         */
-       data = rad_malloc(sizeof(*data));
-       memset(data, 0, sizeof(*data));
+       *instance = inst = talloc_zero(conf, rlm_sqlippool_t);
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
-       if (cf_section_parse(conf, data, module_config) < 0) {
-               free(data);
+       if (cf_section_parse(conf, inst, module_config) < 0) {
                return -1;
        }
 
-       if (IS_EMPTY(data->sql_instance_name)) {
+       if (IS_EMPTY(inst->sql_instance_name)) {
                radlog(L_ERR, "rlm_sqlippool: the 'sql-instance-name' variable must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
@@ -437,79 +428,67 @@ static int sqlippool_instantiate(CONF_SECTION * conf, void ** instance)
         *      Check that all the queries are in place
         */
 
-       if (IS_EMPTY(data->allocate_clear)) {
+       if (IS_EMPTY(inst->allocate_clear)) {
                radlog(L_ERR, "rlm_sqlippool: the 'allocate-clear' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->allocate_find)) {
+       if (IS_EMPTY(inst->allocate_find)) {
                radlog(L_ERR, "rlm_sqlippool: the 'allocate-find' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->allocate_update)) {
+       if (IS_EMPTY(inst->allocate_update)) {
                radlog(L_ERR, "rlm_sqlippool: the 'allocate-update' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->start_update)) {
+       if (IS_EMPTY(inst->start_update)) {
                radlog(L_ERR, "rlm_sqlippool: the 'start-update' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->alive_update)) {
+       if (IS_EMPTY(inst->alive_update)) {
                radlog(L_ERR, "rlm_sqlippool: the 'alive-update' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->stop_clear)) {
+       if (IS_EMPTY(inst->stop_clear)) {
                radlog(L_ERR, "rlm_sqlippool: the 'stop-clear' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->on_clear)) {
+       if (IS_EMPTY(inst->on_clear)) {
                radlog(L_ERR, "rlm_sqlippool: the 'on-clear' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
-       if (IS_EMPTY(data->off_clear)) {
+       if (IS_EMPTY(inst->off_clear)) {
                radlog(L_ERR, "rlm_sqlippool: the 'off-clear' statement must be set.");
-               sqlippool_detach(data);
                return -1;
        }
 
        pool_name = cf_section_name2(conf);
        if (pool_name != NULL)
-               data->pool_name = strdup(pool_name);
+               inst->pool_name = talloc_strdup(inst, pool_name);
        else
-               data->pool_name = strdup("ippool");
+               inst->pool_name = talloc_strdup(inst, "ippool");
 
-       modinst = find_module_instance(cf_section_find("modules"),
-                                      data->sql_instance_name, 1);
-       if (!modinst) {
-               radlog(L_ERR, "sqlippool_instantiate: failed to find sql instance named %s", data->sql_instance_name);
-               sqlippool_detach(data);
+       sqlinst = find_module_instance(cf_section_find("modules"),
+                                      inst->sql_instance_name, 1);
+       if (!sqlinst) {
+               radlog(L_ERR, "sqlippool_instantiate: failed to find sql instance named %s", inst->sql_instance_name);
                return -1;
        }
 
-       if (strcmp(modinst->entry->name, "rlm_sql") != 0) {
+       if (strcmp(sqlinst->entry->name, "rlm_sql") != 0) {
                radlog(L_ERR, "sqlippool_instantiate: Module \"%s\""
                       " is not an instance of the rlm_sql module",
-                      data->sql_instance_name);
-               sqlippool_detach(data);
+                      inst->sql_instance_name);
                return -1;
        }
 
-       data->sql_inst = (SQL_INST *) modinst->insthandle;
-
-       *instance = data;
+       inst->sql_inst = (SQL_INST *) sqlinst->insthandle;
        return 0;
 }
 
@@ -531,7 +510,7 @@ static int do_logging(char *str, int retcode)
  */
 static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
 {
-       rlm_sqlippool_t * data = (rlm_sqlippool_t *) instance;
+       rlm_sqlippool_t * inst = (rlm_sqlippool_t *) instance;
        char allocation[MAX_STRING_LEN];
        int allocation_len;
        uint32_t ip_allocation;
@@ -545,7 +524,7 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
         */
        if (pairfind(request->reply->vps, PW_FRAMED_IP_ADDRESS, 0, TAG_ANY) != NULL) {
                /* We already have a Framed-IP-Address */
-               radius_xlat(logstr, sizeof(logstr), data->log_exists,
+               radius_xlat(logstr, sizeof(logstr), inst->log_exists,
                            request, NULL, NULL);
                RDEBUG("Framed-IP-Address already exists");
 
@@ -554,40 +533,40 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
 
        if (pairfind(request->config_items, PW_POOL_NAME, 0, TAG_ANY) == NULL) {
                RDEBUG("No Pool-Name defined.");
-               radius_xlat(logstr, sizeof(logstr), data->log_nopool,
+               radius_xlat(logstr, sizeof(logstr), inst->log_nopool,
                            request, NULL, NULL);
 
                return do_logging(logstr, RLM_MODULE_NOOP);
        }
 
-       sqlsocket = data->sql_inst->sql_get_socket(data->sql_inst);
+       sqlsocket = inst->sql_inst->sql_get_socket(inst->sql_inst);
        if (sqlsocket == NULL) {
                RDEBUG("cannot allocate sql connection");
                return RLM_MODULE_FAIL;
        }
 
-       if (data->sql_inst->sql_set_user(data->sql_inst, request, NULL) < 0) {
+       if (inst->sql_inst->sql_set_user(inst->sql_inst, request, NULL) < 0) {
                return RLM_MODULE_FAIL;
        }
 
        /*
         * BEGIN
         */
-       sqlippool_command(data->allocate_begin, sqlsocket, data, request,
+       sqlippool_command(inst->allocate_begin, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * CLEAR
         */
-       sqlippool_command(data->allocate_clear, sqlsocket, data, request,
+       sqlippool_command(inst->allocate_clear, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * FIND
         */
        allocation_len = sqlippool_query1(allocation, sizeof(allocation),
-                                         data->allocate_find, sqlsocket,
-                                         data, request, (char *) NULL, 0);
+                                         inst->allocate_find, sqlsocket,
+                                         inst, request, (char *) NULL, 0);
 
        /*
         *      Nothing found...
@@ -596,23 +575,23 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
                /*
                 * COMMIT
                 */
-               sqlippool_command(data->allocate_commit, sqlsocket, instance,
+               sqlippool_command(inst->allocate_commit, sqlsocket, instance,
                                  request, (char *) NULL, 0);
 
                /*
                 * Should we perform pool-check ?
                 */
-               if (data->pool_check && *data->pool_check) {
+               if (inst->pool_check && *inst->pool_check) {
 
                        /*
                         * Ok, so the allocate-find query found nothing ...
                         * Let's check if the pool exists at all
                         */
                        allocation_len = sqlippool_query1(allocation, sizeof(allocation),
-                                                data->pool_check, sqlsocket, data, request,
+                                                inst->pool_check, sqlsocket, inst, request,
                                                (char *) NULL, 0);
 
-                       data->sql_inst->sql_release_socket(data->sql_inst, sqlsocket);
+                       inst->sql_inst->sql_release_socket(inst->sql_inst, sqlsocket);
 
                        if (allocation_len) {
 
@@ -625,7 +604,7 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
                                 *      NOTFOUND
                                 */
                                RDEBUG("pool appears to be full");
-                               radius_xlat(logstr, sizeof(logstr), data->log_failed, request, NULL, NULL);
+                               radius_xlat(logstr, sizeof(logstr), inst->log_failed, request, NULL, NULL);
                                return do_logging(logstr, RLM_MODULE_NOTFOUND);
 
                        }
@@ -641,10 +620,10 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
 
                }
 
-               data->sql_inst->sql_release_socket(data->sql_inst, sqlsocket);
+               inst->sql_inst->sql_release_socket(inst->sql_inst, sqlsocket);
 
                RDEBUG("IP address could not be allocated.");
-               radius_xlat(logstr, sizeof(logstr), data->log_failed,
+               radius_xlat(logstr, sizeof(logstr), inst->log_failed,
                            request, NULL, NULL);
 
                return do_logging(logstr, RLM_MODULE_NOOP);
@@ -659,12 +638,12 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
                /*
                 * COMMIT
                 */
-               sqlippool_command(data->allocate_commit, sqlsocket, instance,
+               sqlippool_command(inst->allocate_commit, sqlsocket, instance,
                                  request, (char *) NULL, 0);
 
-               RDEBUG("Invalid IP number [%s] returned from database query.", allocation);
-               data->sql_inst->sql_release_socket(data->sql_inst, sqlsocket);
-               radius_xlat(logstr, sizeof(logstr), data->log_failed,
+               RDEBUG("Invalid IP number [%s] returned from instbase query.", allocation);
+               inst->sql_inst->sql_release_socket(inst->sql_inst, sqlsocket);
+               radius_xlat(logstr, sizeof(logstr), inst->log_failed,
                            request, NULL, NULL);
 
                return do_logging(logstr, RLM_MODULE_NOOP);
@@ -673,7 +652,7 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
        /*
         * UPDATE
         */
-       sqlippool_command(data->allocate_update, sqlsocket, data, request,
+       sqlippool_command(inst->allocate_update, sqlsocket, inst, request,
                          allocation, allocation_len);
 
        RDEBUG("Allocated IP %s [%08x]", allocation, ip_allocation);
@@ -685,134 +664,134 @@ static rlm_rcode_t sqlippool_postauth(void *instance, REQUEST * request)
        /*
         * COMMIT
         */
-       sqlippool_command(data->allocate_commit, sqlsocket, data, request,
+       sqlippool_command(inst->allocate_commit, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
-       data->sql_inst->sql_release_socket(data->sql_inst, sqlsocket);
-       radius_xlat(logstr, sizeof(logstr), data->log_success, request, NULL, NULL);
+       inst->sql_inst->sql_release_socket(inst->sql_inst, sqlsocket);
+       radius_xlat(logstr, sizeof(logstr), inst->log_success, request, NULL, NULL);
 
        return do_logging(logstr, RLM_MODULE_OK);
 }
 
 static int sqlippool_accounting_start(SQLSOCK * sqlsocket,
-                                     rlm_sqlippool_t *data, REQUEST *request)
+                                     rlm_sqlippool_t *inst, REQUEST *request)
 {
        /*
         * BEGIN
         */
-       sqlippool_command(data->start_begin, sqlsocket, data, request,
+       sqlippool_command(inst->start_begin, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * UPDATE
         */
-       sqlippool_command(data->start_update, sqlsocket, data, request,
+       sqlippool_command(inst->start_update, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * COMMIT
         */
-       sqlippool_command(data->start_commit, sqlsocket, data, request,
+       sqlippool_command(inst->start_commit, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        return RLM_MODULE_OK;
 }
 
 static int sqlippool_accounting_alive(SQLSOCK * sqlsocket,
-                                     rlm_sqlippool_t *data, REQUEST *request)
+                                     rlm_sqlippool_t *inst, REQUEST *request)
 {
        /*
         * BEGIN
         */
-       sqlippool_command(data->alive_begin, sqlsocket, data, request,
+       sqlippool_command(inst->alive_begin, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * UPDATE
         */
-       sqlippool_command(data->alive_update, sqlsocket, data, request,
+       sqlippool_command(inst->alive_update, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * COMMIT
         */
-       sqlippool_command(data->alive_commit, sqlsocket, data, request,
+       sqlippool_command(inst->alive_commit, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        return RLM_MODULE_OK;
 }
 
 static int sqlippool_accounting_stop(SQLSOCK * sqlsocket,
-                                     rlm_sqlippool_t *data, REQUEST *request)
+                                     rlm_sqlippool_t *inst, REQUEST *request)
 {
        char    logstr[MAX_STRING_LEN];
 
        /*
         * BEGIN
         */
-       sqlippool_command(data->stop_begin, sqlsocket, data, request,
+       sqlippool_command(inst->stop_begin, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * CLEAR
         */
-       sqlippool_command(data->stop_clear, sqlsocket, data, request,
+       sqlippool_command(inst->stop_clear, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * COMMIT
         */
-       sqlippool_command(data->stop_commit, sqlsocket, data, request,
+       sqlippool_command(inst->stop_commit, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
-       radius_xlat(logstr, sizeof(logstr), data->log_clear, request, NULL, NULL);
+       radius_xlat(logstr, sizeof(logstr), inst->log_clear, request, NULL, NULL);
 
        return do_logging(logstr, RLM_MODULE_OK);
 }
 
 static int sqlippool_accounting_on(SQLSOCK * sqlsocket,
-                                     rlm_sqlippool_t *data, REQUEST *request)
+                                     rlm_sqlippool_t *inst, REQUEST *request)
 {
        /*
         * BEGIN
         */
-       sqlippool_command(data->on_begin, sqlsocket, data, request,
+       sqlippool_command(inst->on_begin, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * CLEAR
         */
-       sqlippool_command(data->on_clear, sqlsocket, data, request,
+       sqlippool_command(inst->on_clear, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * COMMIT
         */
-       sqlippool_command(data->on_commit, sqlsocket, data, request,
+       sqlippool_command(inst->on_commit, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        return RLM_MODULE_OK;
 }
 
 static int sqlippool_accounting_off(SQLSOCK * sqlsocket,
-                                     rlm_sqlippool_t *data, REQUEST *request)
+                                     rlm_sqlippool_t *inst, REQUEST *request)
 {
        /*
         * BEGIN
         */
-       sqlippool_command(data->off_begin, sqlsocket, data, request,
+       sqlippool_command(inst->off_begin, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * CLEAR
         */
-       sqlippool_command(data->off_clear, sqlsocket, data, request,
+       sqlippool_command(inst->off_clear, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        /*
         * COMMIT
         */
-       sqlippool_command(data->off_commit, sqlsocket, data, request,
+       sqlippool_command(inst->off_commit, sqlsocket, inst, request,
                          (char *) NULL, 0);
 
        return RLM_MODULE_OK;
@@ -828,7 +807,7 @@ static rlm_rcode_t sqlippool_accounting(void * instance, REQUEST * request)
        int rcode;
        VALUE_PAIR * vp;
        int acct_status_type;
-       rlm_sqlippool_t * data = (rlm_sqlippool_t *) instance;
+       rlm_sqlippool_t * inst = (rlm_sqlippool_t *) instance;
        SQLSOCK * sqlsocket;
 
        vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0, TAG_ANY);
@@ -851,39 +830,39 @@ static rlm_rcode_t sqlippool_accounting(void * instance, REQUEST * request)
                return RLM_MODULE_NOOP;
        }
 
-       sqlsocket = data->sql_inst->sql_get_socket(data->sql_inst);
+       sqlsocket = inst->sql_inst->sql_get_socket(inst->sql_inst);
        if (sqlsocket == NULL) {
                RDEBUG("cannot allocate sql connection");
                return RLM_MODULE_NOOP;
        }
 
-       if (data->sql_inst->sql_set_user(data->sql_inst, request, NULL) < 0) {
+       if (inst->sql_inst->sql_set_user(inst->sql_inst, request, NULL) < 0) {
                return RLM_MODULE_FAIL;
        }
 
        switch (acct_status_type) {
        case PW_STATUS_START:
-               rcode = sqlippool_accounting_start(sqlsocket, data, request);
+               rcode = sqlippool_accounting_start(sqlsocket, inst, request);
                break;
 
        case PW_STATUS_ALIVE:
-               rcode = sqlippool_accounting_alive(sqlsocket, data, request);
+               rcode = sqlippool_accounting_alive(sqlsocket, inst, request);
                break;
 
        case PW_STATUS_STOP:
-               rcode = sqlippool_accounting_stop(sqlsocket, data, request);
+               rcode = sqlippool_accounting_stop(sqlsocket, inst, request);
                break;
 
        case PW_STATUS_ACCOUNTING_ON:
-               rcode = sqlippool_accounting_on(sqlsocket, data, request);
+               rcode = sqlippool_accounting_on(sqlsocket, inst, request);
                break;
 
        case PW_STATUS_ACCOUNTING_OFF:
-               rcode = sqlippool_accounting_off(sqlsocket, data, request);
+               rcode = sqlippool_accounting_off(sqlsocket, inst, request);
                break;
        }
 
-       data->sql_inst->sql_release_socket(data->sql_inst, sqlsocket);
+       inst->sql_inst->sql_release_socket(inst->sql_inst, sqlsocket);
 
        return rcode;
 }
@@ -902,7 +881,7 @@ module_t rlm_sqlippool = {
        "SQL IP Pool",
        RLM_TYPE_THREAD_SAFE,           /* type */
        sqlippool_instantiate,          /* instantiation */
-       sqlippool_detach,               /* detach */
+       NULL,                           /* detach */
        {
                NULL,                   /* authentication */
                NULL,                   /* authorization */
index 2fa6877..63905f0 100644 (file)
@@ -115,7 +115,7 @@ static int groupcmp(void *instance, REQUEST *req, UNUSED VALUE_PAIR *request,
 /*
  *     Detach.
  */
-static int unix_detach(void *instance)
+static int unix_detach(UNUSED void *instance)
 {
 #define inst ((struct unix_instance *)instance)
 
@@ -124,7 +124,6 @@ static int unix_detach(void *instance)
        paircompare_unregister(PW_GROUP_NAME, groupcmp);
 #endif
 #undef inst
-       free(instance);
        return 0;
 }
 
@@ -138,17 +137,13 @@ static int unix_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Allocate room for the instance.
         */
-       inst = *instance = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, struct unix_instance);
+       if (!inst) return -1;
 
        /*
         *      Parse the configuration, failing if we can't do so.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               unix_detach(inst);
                return -1;
        }
 
@@ -159,8 +154,6 @@ static int unix_instantiate(CONF_SECTION *conf, void **instance)
        paircompare_register(PW_GROUP_NAME, PW_USER_NAME, groupcmp, NULL);
 #endif
 
-#undef inst
-
        return 0;
 }
 
index 82fd59a..dfd109d 100644 (file)
@@ -50,17 +50,6 @@ static const CONF_PARSER module_config[] = {
   { NULL, -1, 0, NULL, NULL }          /* end the list */
 };
 
-
-/*
- *     Only free memory we allocated.  The strings allocated via
- *     cf_section_parse() do not need to be freed.
- */
-static int wimax_detach(void *instance)
-{
-       free(instance);
-       return 0;
-}
-
 /*
  *     Do any per-module initialization that is separate to each
  *     configured instance of the module.  e.g. set up connections
@@ -78,23 +67,17 @@ static int wimax_instantiate(CONF_SECTION *conf, void **instance)
        /*
         *      Set up a storage area for instance data
         */
-       inst = rad_malloc(sizeof(*inst));
-       if (!inst) {
-               return -1;
-       }
-       memset(inst, 0, sizeof(*inst));
+       *instance = inst = talloc_zero(conf, rlm_wimax_t);
+       if (!inst) return -1;
 
        /*
         *      If the configuration parameters can't be parsed, then
         *      fail.
         */
        if (cf_section_parse(conf, inst, module_config) < 0) {
-               wimax_detach(inst);
                return -1;
        }
 
-       *instance = inst;
-
        return 0;
 }
 
@@ -526,7 +509,7 @@ module_t rlm_wimax = {
        "wimax",
        RLM_TYPE_THREAD_SAFE,           /* type */
        wimax_instantiate,              /* instantiation */
-       wimax_detach,                   /* detach */
+       NULL,                           /* detach */
        {
                NULL,                   /* authentication */
                wimax_authorize,        /* authorization */