From: Alan T. DeKok Date: Thu, 3 Sep 2015 22:39:38 +0000 (-0400) Subject: Add module_instantiate_method() X-Git-Tag: release_3_0_10~170 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d8bd59731b0dc7dd4eac22d7c7171f95e0dcb7e;p=thirdparty%2Ffreeradius-server.git Add module_instantiate_method() which allows the caller to instantiate "module.method" --- diff --git a/src/include/modpriv.h b/src/include/modpriv.h index fc8ec5aae36..d5e2c2392ba 100644 --- a/src/include/modpriv.h +++ b/src/include/modpriv.h @@ -57,6 +57,7 @@ typedef struct module_instance_t { } module_instance_t; module_instance_t *module_instantiate(CONF_SECTION *modules, char const *askedname); +module_instance_t *module_instantiate_method(CONF_SECTION *modules, char const *askedname, rlm_components_t *method); module_instance_t *module_find(CONF_SECTION *modules, char const *askedname); int find_module_sibling_section(CONF_SECTION **out, CONF_SECTION *module, char const *name); int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when); diff --git a/src/main/modcall.c b/src/main/modcall.c index b6d4647529e..e503d85f6e5 100644 --- a/src/main/modcall.c +++ b/src/main/modcall.c @@ -2537,63 +2537,17 @@ static modcallable *do_compile_modsingle(modcallable *parent, * modules belongs in raddb/mods-available/, * which isn't loaded into the "modules" section. */ - if (cf_section_sub_find_name2(modules, NULL, realname)) { - this = module_instantiate(modules, realname); - if (this) goto allocate_csingle; - - /* - * - */ - if (realname != modrefname) { - return NULL; - } - - } else { - /* - * We were asked to MAYBE load it and it - * doesn't exist. Return a soft error. - */ - if (realname != modrefname) { - *modname = modrefname; - return NULL; - } - } - } - - /* - * No module found by that name. Maybe we're calling - * module.method - */ - p = strrchr(modrefname, '.'); - if (p) { - rlm_components_t i; - p++; + this = module_instantiate_method(modules, realname, &method); + if (this) goto allocate_csingle; /* - * Find the component. + * We were asked to MAYBE load it and it + * doesn't exist. Return a soft error. */ - for (i = MOD_AUTHENTICATE; - i < MOD_COUNT; - i++) { - if (strcmp(p, comp2str[i]) == 0) { - char buffer[256]; - - strlcpy(buffer, modrefname, sizeof(buffer)); - buffer[p - modrefname - 1] = '\0'; - component = i; - - this = module_instantiate(modules, buffer); - if (this) { - method = i; - goto allocate_csingle; - } - } + if (realname != modrefname) { + *modname = modrefname; + return NULL; } - - /* - * FIXME: check for "module", and give error "no - * such component" when we don't find the method. - */ } /* diff --git a/src/main/modules.c b/src/main/modules.c index 58bf4130d6e..b80c737aa3e 100644 --- a/src/main/modules.c +++ b/src/main/modules.c @@ -699,7 +699,6 @@ module_instance_t *module_find(CONF_SECTION *modules, char const *askedname) * */ module_instance_t *module_instantiate(CONF_SECTION *modules, char const *askedname) - { module_instance_t *node; @@ -766,6 +765,52 @@ module_instance_t *module_instantiate(CONF_SECTION *modules, char const *askedna return node; } + +module_instance_t *module_instantiate_method(CONF_SECTION *modules, char const *name, rlm_components_t *method) +{ + char *p; + rlm_components_t i; + module_instance_t *mi; + + /* + * Don't change "method" if it's just "module" name. + */ + mi = module_instantiate(modules, name); + if (mi) return mi; + + /* + * Find out which method is being used. + */ + p = strrchr(name, "."); + if (!p) return NULL; + + p++; + + /* + * Find the component. + */ + for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) { + if (strcmp(p, section_type_value[i].section) == 0) { + char buffer[256]; + + strlcpy(buffer, name, sizeof(buffer)); + buffer[p - name - 1] = '\0'; + + mi = module_instantiate(modules, buffer); + if (mi) { + if (method) *method = i; + return mi; + } + } + } + + /* + * Not found. + */ + return NULL; +} + + /** Resolve polymorphic item's from a module's CONF_SECTION to a subsection in another module * * This allows certain module sections to reference module sections in other instances @@ -1900,7 +1945,6 @@ int modules_init(CONF_SECTION *config) for (ci=cf_item_find_next(cs, NULL); ci != NULL; ci=cf_item_find_next(cs, ci)) { - /* * Skip sections and "other" stuff. * Sections will be handled later, if @@ -1968,7 +2012,10 @@ int modules_init(CONF_SECTION *config) return -1; } - module = module_instantiate(modules, cf_pair_attr(cp)); + /* + * Allow "foo.authorize" in subsections. + */ + module = module_instantiate_method(modules, cf_pair_attr(cp), NULL); if (!module) { return -1; }