Skip to content

Commit

Permalink
Move attribute creation to bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
alandekok committed Apr 29, 2015
1 parent cfff77b commit f0b8381
Showing 1 changed file with 36 additions and 60 deletions.
96 changes: 36 additions & 60 deletions src/modules/rlm_sql/rlm_sql.c
Expand Up @@ -807,6 +807,41 @@ static int mod_detach(void *instance)
return 0;
}

static int mod_bootstrap(CONF_SECTION *conf, void *instance)
{
rlm_sql_t *inst = instance;

if (inst->config->groupmemb_query) {
if (!cf_section_name2(conf)) {
char buffer[256];

snprintf(buffer, sizeof(buffer), "%s-SQL-Group", inst->name);

if (paircompare_register_byname(buffer, dict_attrbyvalue(PW_USER_NAME, 0), false, sql_groupcmp, inst) < 0) {
ERROR("Error registering group comparison: %s", fr_strerror());
return -1;
}

inst->group_da = dict_attrbyname(buffer);

/*
* We're the default instance
*/
} else {
if (paircompare_register_byname("SQL-Group", dict_attrbyvalue(PW_USER_NAME, 0),
false, sql_groupcmp, inst) < 0) {
ERROR("Error registering group comparison: %s", fr_strerror());
return -1;
}

inst->group_da = dict_attrbyname("SQL-Group");
}
}

return 0;
}


static int mod_instantiate(CONF_SECTION *conf, void *instance)
{
rlm_sql_t *inst = instance;
Expand All @@ -820,39 +855,6 @@ static int mod_instantiate(CONF_SECTION *conf, void *instance)
inst->name = cf_section_name2(conf);
if (!inst->name) {
inst->name = cf_section_name1(conf);
} else {
char *group_name;
DICT_ATTR const *da;
ATTR_FLAGS flags;

/*
* Allocate room for <instance>-SQL-Group
*/
group_name = talloc_typed_asprintf(inst, "%s-SQL-Group", inst->name);
DEBUG("rlm_sql (%s): Creating new attribute %s",
inst->name, group_name);

memset(&flags, 0, sizeof(flags));
if (dict_addattr(group_name, -1, 0, PW_TYPE_STRING, flags) < 0) {
ERROR("rlm_sql (%s): Failed to create "
"attribute %s: %s", inst->name, group_name,
fr_strerror());
return -1;
}

da = dict_attrbyname(group_name);
if (!da) {
ERROR("rlm_sql (%s): Failed to create "
"attribute %s", inst->name, group_name);
return -1;
}

if (inst->config->groupmemb_query) {
DEBUG("rlm_sql (%s): Registering sql_groupcmp for %s",
inst->name, group_name);
paircompare_register(da, dict_attrbyvalue(PW_USER_NAME, 0),
false, sql_groupcmp, inst);
}
}

rad_assert(inst->name);
Expand Down Expand Up @@ -996,33 +998,6 @@ do { \
inst->pool = fr_connection_pool_module_init(inst->cs, inst, mod_conn_create, NULL, NULL);
if (!inst->pool) return -1;

if (inst->config->groupmemb_query) {
if (!cf_section_name2(conf)) {
char buffer[256];

snprintf(buffer, sizeof(buffer), "%s-SQL-Group", inst->name);

if (paircompare_register_byname(buffer, dict_attrbyvalue(PW_USER_NAME, 0), false, sql_groupcmp, inst) < 0) {
ERROR("Error registering group comparison: %s", fr_strerror());
return -1;
}

inst->group_da = dict_attrbyname(buffer);

/*
* We're the default instance
*/
} else {
if (paircompare_register_byname("SQL-Group", dict_attrbyvalue(PW_USER_NAME, 0),
false, sql_groupcmp, inst) < 0) {
ERROR("Error registering group comparison: %s", fr_strerror());
return -1;
}

inst->group_da = dict_attrbyname("SQL-Group");
}
}

if (inst->config->do_clients) {
if (generate_sql_clients(inst) == -1){
ERROR("Failed to load clients from SQL");
Expand Down Expand Up @@ -1710,6 +1685,7 @@ module_t rlm_sql = {
.type = RLM_TYPE_THREAD_SAFE,
.inst_size = sizeof(rlm_sql_t),
.config = module_config,
.bootstrap = mod_bootstrap,
.instantiate = mod_instantiate,
.detach = mod_detach,
.methods = {
Expand Down

0 comments on commit f0b8381

Please sign in to comment.