Enable building #WITHOUT_PROXY
[freeradius.git] / src / main / modules.c
index 0a77269..a569c07 100644 (file)
@@ -94,42 +94,98 @@ static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
 
 
 #ifdef WITHOUT_LIBLTDL
+#ifdef WITH_DLOPEN
+#include <dlfcn.h>
+
+#ifndef RTLD_NOW
+#define RTLD_NOW (0)
+#endif
+#ifndef RTLD_LOCAL
+#define RTLD_LOCAL (0)
+#endif
+
+#define fr_dlopenext lt_dlopenext
+#ifndef LT_SHREXT
+#ifdef __APPLE__
+#define LT_SHREXT ".so"
+#elif defined (WIN32)
+#define LT_SHREXT ".dll"
+#else
+#define LT_SHREXT ".dylib"
+#endif
+#endif
+
+lt_dlhandle lt_dlopenext(const char *name)
+{
+       char buffer[256];
+
+       strlcpy(buffer, name, sizeof(buffer));
+
+       /*
+        *      FIXME: Make this configurable...
+        */
+       strlcat(buffer, LT_SHREXT, sizeof(buffer));
+
+       return dlopen(buffer, RTLD_NOW | RTLD_LOCAL);
+}
+
+void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
+{
+       return dlsym(handle, symbol);
+}
+
+int lt_dlclose(lt_dlhandle handle)
+{
+       if (!handle) return 0;
+
+       return dlclose(handle);
+}
+
+const char *lt_dlerror(void)
+{
+       return dlerror();
+}
+
+
+#else  /* without dlopen */
 typedef struct lt_dlmodule_t {
   const char   *name;
   void         *ref;
 } lt_dlmodule_t;
 
+typedef struct eap_type_t EAP_TYPE;
+typedef struct rlm_sql_module_t rlm_sql_module_t;
+
 /*
- *     Define modules here.
+ *     FIXME: Write hackery to auto-generate this data.
+ *     We only need to do this on systems that don't have dlopen.
  */
 extern module_t rlm_pap;
 extern module_t rlm_chap;
 extern module_t rlm_eap;
+extern module_t rlm_sql;
+/* and so on ... */
 
-/*
- *     EAP structures are defined elsewhere.
- */
-typedef struct eap_type_t EAP_TYPE;
-
-/*
- *     And so on for other EAP types.
- */
 extern EAP_TYPE rlm_eap_md5;
+extern rlm_sql_module_t rlm_sql_mysql;
+/* and so on ... */
 
 static const lt_dlmodule_t lt_dlmodules[] = {
        { "rlm_pap", &rlm_pap },
        { "rlm_chap", &rlm_chap },
        { "rlm_eap", &rlm_eap },
+       /* and so on ... */
+
        { "rlm_eap_md5", &rlm_eap_md5 },
-       
-       /*
-        *      Add other modules here.
-        */
+       /* and so on ... */
+               
+       { "rlm_sql_mysql", &rlm_sql_mysql },
+       /* and so on ... */
                
        { NULL, NULL }
 };
 
-
+#define fr_dlopenext lt_dlopenext
 lt_dlhandle lt_dlopenext(const char *name)
 {
        int i;
@@ -147,6 +203,43 @@ void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
 {
        return handle;
 }
+
+int lt_dlclose(lt_dlhandle handle)
+{
+       return 0;
+}
+
+const char *lt_dlerror(void)
+{
+       return "Unspecified error";
+}
+
+#endif /* WITH_DLOPEN */
+#else  /* WITHOUT_LIBLTDL */
+
+/*
+ *     Solve the issues of libraries linking to other libraries
+ *     by using a newer libltdl API.
+ */
+#ifndef HAVE_LT_DLADVISE_INIT
+#define fr_dlopenext lt_dlopenext
+#else
+static lt_dlhandle fr_dlopenext(const char *filename)
+{
+       lt_dlhandle handle = 0;
+       lt_dladvise advise;
+
+       if (!lt_dladvise_init (&advise) &&
+           !lt_dladvise_ext (&advise) &&
+           !lt_dladvise_global (&advise)) {
+               handle = lt_dlopenadvise (filename, advise);
+       }
+
+       lt_dladvise_destroy (&advise);
+
+       return handle;
+}
+#endif /* HAVE_LT_DLADVISE_INIT */
 #endif /* WITHOUT_LIBLTDL */
 
 static int virtual_server_idx(const char *name)
@@ -345,7 +438,7 @@ static module_entry_t *linkto_module(const char *module_name,
 {
        module_entry_t myentry;
        module_entry_t *node;
-       lt_dlhandle handle;
+       lt_dlhandle handle = NULL;
        char module_struct[256];
        char *p;
        const module_t *module;
@@ -355,17 +448,6 @@ static module_entry_t *linkto_module(const char *module_name,
        if (node) return node;
 
        /*
-        *      Keep the handle around so we can dlclose() it.
-        */
-       handle = lt_dlopenext(module_name);
-       if (handle == NULL) {
-               cf_log_err(cf_sectiontoitem(cs),
-                          "Failed to link to module '%s': %s\n",
-                          module_name, lt_dlerror());
-               return NULL;
-       }
-
-       /*
         *      Link to the module's rlm_FOO{} module structure.
         *
         *      The module_name variable has the version number
@@ -375,6 +457,22 @@ static module_entry_t *linkto_module(const char *module_name,
        p = strrchr(module_struct, '-');
        if (p) *p = '\0';
 
+#if defined(WITHOUT_LIBLTDL) && defined (WITH_DLOPEN) && defined(RTLD_SELF)
+       module = lt_dlsym(RTLD_SELF, module_struct);
+       if (module) goto open_self;
+#endif
+
+       /*
+        *      Keep the handle around so we can dlclose() it.
+        */
+       handle = fr_dlopenext(module_name);
+       if (handle == NULL) {
+               cf_log_err(cf_sectiontoitem(cs),
+                          "Failed to link to module '%s': %s\n",
+                          module_name, lt_dlerror());
+               return NULL;
+       }
+
        DEBUG3("    (Loaded %s, checking if it's valid)", module_name);
 
        /*
@@ -389,6 +487,10 @@ static module_entry_t *linkto_module(const char *module_name,
                lt_dlclose(handle);
                return NULL;
        }
+
+#if defined(WITHOUT_LIBLTDL) && defined (WITH_DLOPEN) && defined(RTLD_SELF)
+ open_self:
+#endif
        /*
         *      Before doing anything else, check if it's sane.
         */
@@ -497,7 +599,8 @@ module_instance_t *find_module_instance(CONF_SECTION *modules,
        } else {
        print_inst:
                check_config_safe = TRUE;
-               cf_log_module(cs, "Instantiating %s", instname);
+               cf_log_module(cs, "Instantiating module \"%s\" from file %s",
+                             instname, cf_section_filename(cs));
        }
 
        /*
@@ -612,13 +715,13 @@ int indexed_modcall(int comp, int idx, REQUEST *request)
        }
 
        if (!server) {
-               RDEBUG("No such virtual server %s", request->server);
+               RDEBUG("No such virtual server \"%s\"", request->server);
                return RLM_MODULE_FAIL;
        }
 
        if (idx == 0) {
                list = server->mc[comp];
-               if (!list) RDEBUG2("  WARNING: Empty section.  Using default return values.");
+               if (!list) RDEBUG2("  WARNING: Empty %s section.  Using default return values.", section_type_value[comp].section);
 
        } else {
                indexed_modcallable *this;
@@ -631,13 +734,23 @@ int indexed_modcall(int comp, int idx, REQUEST *request)
                                section_type_value[comp].typename);
                }
        }
-
+       
+       if (server->subcs[comp]) {
+               if (idx == 0) {
+                       RDEBUG("# Executing section %s from file %s",
+                              section_type_value[comp].section,
+                              cf_section_filename(server->subcs[comp]));
+               } else {
+                       RDEBUG("# Executing group from file %s",
+                              cf_section_filename(server->subcs[comp]));
+               }
+       }
        request->component = section_type_value[comp].section;
 
        rcode = modcall(comp, list, request);
 
        request->module = "";
-       request->component = "";
+       request->component = "<core>";
        return rcode;
 }
 
@@ -680,7 +793,7 @@ static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
         *      automatically.  If it isn't found, it's a serious
         *      error.
         */
-       dval = dict_valbyname(attr, name2);
+       dval = dict_valbyname(attr, 0, name2);
        if (!dval) {
                cf_log_err(cf_sectiontoitem(cs),
                           "%s %s Not previously configured",
@@ -708,7 +821,7 @@ static int define_type(const DICT_ATTR *dattr, const char *name)
         *      If the value already exists, don't
         *      create it again.
         */
-       dval = dict_valbyname(dattr->attr, name);
+       dval = dict_valbyname(dattr->attr, dattr->vendor, name);
        if (dval) return 1;
 
        /*
@@ -720,7 +833,7 @@ static int define_type(const DICT_ATTR *dattr, const char *name)
         */
        do {
                value = fr_rand() & 0x00ffffff;
-       } while (dict_valbyattr(dattr->attr, value));
+       } while (dict_valbyattr(dattr->attr, dattr->vendor, value));
 
        if (dict_addvalue(name, dattr->name, value) < 0) {
                radlog(L_ERR, "%s", fr_strerror());
@@ -744,7 +857,7 @@ static int load_component_section(CONF_SECTION *cs,
        /*
         *      Find the attribute used to store VALUEs for this section.
         */
-       dattr = dict_attrbyvalue(section_type_value[comp].attr);
+       dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
        if (!dattr) {
                cf_log_err(cf_sectiontoitem(cs),
                           "No such attribute %s",
@@ -823,7 +936,7 @@ static int load_component_section(CONF_SECTION *cs,
                                }
                        }
 
-                       dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
+                       dval = dict_valbyname(PW_AUTH_TYPE, 0, modrefname);
                        if (!dval) {
                                /*
                                 *      It's a section, but nothing we
@@ -869,9 +982,11 @@ static int load_byserver(CONF_SECTION *cs)
        indexed_modcallable *c;
 
        if (name) {
-               cf_log_info(cs, "server %s {", name);
+               cf_log_info(cs, "server %s { # from file %s",
+                           name, cf_section_filename(cs));
        } else {
-               cf_log_info(cs, "server {");
+               cf_log_info(cs, "server { # from file %s",
+                           cf_section_filename(cs));
        }
 
        cf_log_info(cs, " modules {");
@@ -908,7 +1023,7 @@ static int load_byserver(CONF_SECTION *cs)
                /*
                 *      Find the attribute used to store VALUEs for this section.
                 */
-               dattr = dict_attrbyvalue(section_type_value[comp].attr);
+               dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
                if (!dattr) {
                        cf_log_err(cf_sectiontoitem(subcs),
                                   "No such attribute %s",
@@ -978,17 +1093,18 @@ static int load_byserver(CONF_SECTION *cs)
                cf_log_module(cs, "Checking %s {...} for more modules to load",
                       section_type_value[comp].section);
 
-#ifdef WITH_PROXY
                /*
                 *      Skip pre/post-proxy sections if we're not
                 *      proxying.
                 */
-               if (!mainconfig.proxy_requests &&
-                   ((comp == PW_PRE_PROXY_TYPE) ||
-                    (comp == PW_PRE_PROXY_TYPE))) {
+               if (
+#ifdef WITH_PROXY
+                   !mainconfig.proxy_requests &&
+#endif
+                   ((comp == RLM_COMPONENT_PRE_PROXY) ||
+                    (comp == RLM_COMPONENT_POST_PROXY))) {
                        continue;
                }
-#endif
 
                if (load_component_section(subcs, components, comp) < 0) {
                        goto error;
@@ -1265,7 +1381,9 @@ int setup_modules(int reload, CONF_SECTION *config)
                 *      It's like libtool and libltdl are some kind
                 *      of sick joke.
                 */
+#ifdef IE_LIBTOOL_DIE
 #define lt__PROGRAM__LTX_preloaded_symbols lt_libltdl_LTX_preloaded_symbols
+#endif
 
                /*
                 *      Set the default list of preloaded symbols.
@@ -1371,7 +1489,9 @@ int setup_modules(int reload, CONF_SECTION *config)
             listener = listener->next) {
                char buffer[256];
 
+#ifdef WITH_PROXY
                if (listener->type == RAD_LISTEN_PROXY) continue;
+#endif
 
                cs = cf_section_sub_find_name2(config,
                                               "server", listener->server);