Add module_instantiate_method()
authorAlan T. DeKok <aland@freeradius.org>
Thu, 3 Sep 2015 22:39:38 +0000 (18:39 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 3 Sep 2015 22:39:38 +0000 (18:39 -0400)
which allows the caller to instantiate "module.method"

src/include/modpriv.h
src/main/modcall.c
src/main/modules.c

index fc8ec5a..d5e2c23 100644 (file)
@@ -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);
index b6d4647..e503d85 100644 (file)
@@ -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.
-                */
        }
 
        /*
index 58bf413..b80c737 100644 (file)
@@ -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;
                                                }