2 * modules.c Radius module support.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2003,2006 The FreeRADIUS server project
21 * Copyright 2000 Alan DeKok <aland@ox.org>
22 * Copyright 2000 Alan Curry <pacman@world.std.com>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modpriv.h>
30 #include <freeradius-devel/modcall.h>
31 #include <freeradius-devel/rad_assert.h>
33 extern int check_config;
35 typedef struct indexed_modcallable {
38 modcallable *modulelist;
39 } indexed_modcallable;
41 typedef struct virtual_server_t {
47 modcallable *mc[RLM_COMPONENT_COUNT];
48 CONF_SECTION *subcs[RLM_COMPONENT_COUNT];
49 struct virtual_server_t *next;
53 * Keep a hash of virtual servers, so that we can reload them.
55 #define VIRTUAL_SERVER_HASH_SIZE (256)
56 static virtual_server_t *virtual_servers[VIRTUAL_SERVER_HASH_SIZE];
58 static rbtree_t *module_tree = NULL;
60 static rbtree_t *instance_tree = NULL;
62 struct fr_module_hup_t {
63 module_instance_t *mi;
66 fr_module_hup_t *next;
70 * Ordered by component
72 const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
73 { "authenticate", "Auth-Type", PW_AUTH_TYPE },
74 { "authorize", "Autz-Type", PW_AUTZ_TYPE },
75 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
76 { "accounting", "Acct-Type", PW_ACCT_TYPE },
77 { "session", "Session-Type", PW_SESSION_TYPE },
78 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
79 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
80 { "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE }
83 { "recv-coa", "Recv-CoA-Type", PW_RECV_COA_TYPE },
84 { "send-coa", "Send-CoA-Type", PW_SEND_COA_TYPE }
97 #define RTLD_LOCAL (0)
100 #define fr_dlopenext lt_dlopenext
103 #define LT_SHREXT ".dylib"
104 #define LD_LIBRARY_PATH "DYLD_FALLBACK_LIBRARY_PATH"
105 #elif defined (WIN32)
106 #define LT_SHREXT ".dll"
108 #define LT_SHREXT ".so"
109 #define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
120 * This doesn't really do anything...
122 p = getenv(LD_LIBRARY_PATH);
124 snprintf(buffer, sizeof(buffer), "%s:%s", p, radlib_dir);
130 return setenv(LD_LIBRARY_PATH, val, 1);
133 lt_dlhandle lt_dlopenext(const char *name)
135 int flags = RTLD_NOW;
140 if (strcmp(name, "rlm_perl") == 0) {
141 flags |= RTLD_GLOBAL;
147 * Prefer loading our libraries by absolute path.
149 snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT);
150 handle = dlopen(buffer, flags);
151 if (handle) return handle;
153 strlcpy(buffer, name, sizeof(buffer));
156 * FIXME: Make this configurable...
158 strlcat(buffer, LT_SHREXT, sizeof(buffer));
160 return dlopen(buffer, flags);
163 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
165 return dlsym(handle, symbol);
168 int lt_dlclose(lt_dlhandle handle)
170 if (!handle) return 0;
172 return dlclose(handle);
175 const char *lt_dlerror(void)
181 #else /* without dlopen */
182 typedef struct lt_dlmodule_t {
187 typedef struct eap_type_t EAP_TYPE;
188 typedef struct rlm_sql_module_t rlm_sql_module_t;
191 * FIXME: Write hackery to auto-generate this data.
192 * We only need to do this on systems that don't have dlopen.
194 extern module_t rlm_pap;
195 extern module_t rlm_chap;
196 extern module_t rlm_eap;
197 extern module_t rlm_sql;
200 extern EAP_TYPE rlm_eap_md5;
201 extern rlm_sql_module_t rlm_sql_mysql;
204 static const lt_dlmodule_t lt_dlmodules[] = {
205 { "rlm_pap", &rlm_pap },
206 { "rlm_chap", &rlm_chap },
207 { "rlm_eap", &rlm_eap },
210 { "rlm_eap_md5", &rlm_eap_md5 },
213 { "rlm_sql_mysql", &rlm_sql_mysql },
219 #define fr_dlopenext lt_dlopenext
220 lt_dlhandle lt_dlopenext(const char *name)
224 for (i = 0; lt_dlmodules[i].name != NULL; i++) {
225 if (strcmp(name, lt_dlmodules[i].name) == 0) {
226 return lt_dlmodules[i].ref;
233 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
238 int lt_dlclose(lt_dlhandle handle)
243 const char *lt_dlerror(void)
245 return "Unspecified error";
248 #endif /* HAVE_DLFCN_H */
249 #else /* WIT_LIBLTDL */
252 * Solve the issues of libraries linking to other libraries
253 * by using a newer libltdl API.
255 #ifndef HAVE_LT_DLADVISE_INIT
256 #define fr_dlopenext lt_dlopenext
258 static lt_dlhandle fr_dlopenext(const char *filename)
260 lt_dlhandle handle = 0;
263 if (!lt_dladvise_init (&advise) &&
264 !lt_dladvise_ext (&advise) &&
265 !lt_dladvise_global (&advise)) {
266 handle = lt_dlopenadvise (filename, advise);
269 lt_dladvise_destroy (&advise);
273 #endif /* HAVE_LT_DLADVISE_INIT */
274 #endif /* WITH_LIBLTDL */
276 static int virtual_server_idx(const char *name)
282 hash = fr_hash_string(name);
284 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
287 static virtual_server_t *virtual_server_find(const char *name)
290 virtual_server_t *server;
292 rcode = virtual_server_idx(name);
293 for (server = virtual_servers[rcode];
295 server = server->next) {
296 if (!name && !server->name) break;
298 if ((name && server->name) &&
299 (strcmp(name, server->name) == 0)) break;
305 static int virtual_server_free(void *ctx)
307 virtual_server_t *server;
309 server = talloc_get_type_abort(ctx, virtual_server_t);
310 if (server->components) rbtree_free(server->components);
314 void virtual_servers_free(time_t when)
317 virtual_server_t **last;
319 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
320 virtual_server_t *server, *next;
322 last = &virtual_servers[i];
323 for (server = virtual_servers[i];
329 * If we delete it, fix the links so that
330 * we don't orphan anything. Also,
331 * delete it if it's old, AND a newer one
334 * Otherwise, the last pointer gets set to
335 * the one we didn't delete.
338 ((server->created < when) && server->can_free)) {
339 *last = server->next;
342 last = &(server->next);
348 static void indexed_modcallable_free(void *data)
350 indexed_modcallable *c = data;
352 modcallable_free(&c->modulelist);
356 static int indexed_modcallable_cmp(const void *one, const void *two)
358 const indexed_modcallable *a = one;
359 const indexed_modcallable *b = two;
361 if (a->comp < b->comp) return -1;
362 if (a->comp > b->comp) return +1;
364 return a->idx - b->idx;
369 * Compare two module entries
371 static int module_instance_cmp(const void *one, const void *two)
373 const module_instance_t *a = one;
374 const module_instance_t *b = two;
376 return strcmp(a->name, b->name);
380 static void module_instance_free_old(UNUSED CONF_SECTION *cs, module_instance_t *node,
383 fr_module_hup_t *mh, **last;
386 * Walk the list, freeing up old instances.
393 * Free only every 60 seconds.
395 if ((when - mh->when) < 60) {
400 talloc_free(mh->insthandle);
409 * Free a module instance.
411 static void module_instance_free(void *data)
413 module_instance_t *this = data;
415 module_instance_free_old(this->cs, this, time(NULL) + 100);
417 #ifdef HAVE_PTHREAD_H
421 * The mutex MIGHT be locked...
422 * we'll check for that later, I guess.
424 pthread_mutex_destroy(this->mutex);
425 talloc_free(this->mutex);
428 memset(this, 0, sizeof(*this));
434 * Compare two module entries
436 static int module_entry_cmp(const void *one, const void *two)
438 const module_entry_t *a = one;
439 const module_entry_t *b = two;
441 return strcmp(a->name, b->name);
445 * Free a module entry.
447 static int module_entry_free(void *ctx)
449 module_entry_t *this;
451 this = talloc_get_type_abort(ctx, module_entry_t);
453 lt_dlclose(this->handle); /* ignore any errors */
459 * Remove the module lists.
461 int detach_modules(void)
463 rbtree_free(instance_tree);
464 rbtree_free(module_tree);
473 * Find a module on disk or in memory, and link to it.
475 static module_entry_t *linkto_module(const char *module_name,
478 module_entry_t myentry;
479 module_entry_t *node;
480 lt_dlhandle handle = NULL;
481 char module_struct[256];
483 const module_t *module;
485 strlcpy(myentry.name, module_name, sizeof(myentry.name));
486 node = rbtree_finddata(module_tree, &myentry);
487 if (node) return node;
488 if (strlen(module_name) >= sizeof(module_struct)) {
489 cf_log_err(cf_sectiontoitem(cs),
490 "Failed to link to module '%s': Name is too long\n",
496 * Link to the module's rlm_FOO{} module structure.
498 * The module_name variable has the version number
499 * embedded in it, and we don't want that here.
501 strlcpy(module_struct, module_name, sizeof(module_struct));
502 p = strrchr(module_struct, '-');
505 #if !defined(WITH_LIBLTDL) && defined(HAVE_DLFCN_H) && defined(RTLD_SELF)
506 module = lt_dlsym(RTLD_SELF, module_struct);
507 if (module) goto open_self;
511 * Keep the handle around so we can dlclose() it.
513 handle = fr_dlopenext(module_name);
514 if (handle == NULL) {
515 cf_log_err(cf_sectiontoitem(cs),
516 "Failed to link to module '%s': %s\n",
517 module_name, lt_dlerror());
521 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
524 * libltld MAY core here, if the handle it gives us contains
527 module = lt_dlsym(handle, module_struct);
529 cf_log_err(cf_sectiontoitem(cs),
530 "Failed linking to %s structure: %s\n",
531 module_name, lt_dlerror());
536 #if !defined(WIT_LIBLTDL) && defined (HAVE_DLFCN_H) && defined(RTLD_SELF)
540 * Before doing anything else, check if it's sane.
542 if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
544 cf_log_err(cf_sectiontoitem(cs),
545 "Invalid version in module '%s'",
551 /* make room for the module type */
552 node = talloc_zero(cs, module_entry_t);
553 talloc_set_destructor((void *) node, module_entry_free);
554 strlcpy(node->name, module_name, sizeof(node->name));
555 node->module = module;
556 node->handle = handle;
558 cf_log_module(cs, "Linked to module %s", module_name);
561 * Add the module as "rlm_foo-version" to the configuration
564 if (!rbtree_insert(module_tree, node)) {
565 radlog(L_ERR, "Failed to cache module %s", module_name);
575 * Find a module instance.
577 module_instance_t *find_module_instance(CONF_SECTION *modules,
578 const char *askedname, int do_link)
580 int check_config_safe = FALSE;
582 const char *name1, *instname;
583 module_instance_t *node, myNode;
584 char module_name[256];
586 if (!modules) return NULL;
589 * Look for the real name. Ignore the first character,
590 * which tells the server "it's OK for this module to not
593 instname = askedname;
594 if (instname[0] == '-') instname++;
597 * Module instances are declared in the modules{} block
598 * and referenced later by their name, which is the
599 * name2 from the config section, or name1 if there was
602 cs = cf_section_sub_find_name2(modules, NULL, instname);
604 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
609 * If there's already a module instance, return it.
611 strlcpy(myNode.name, instname, sizeof(myNode.name));
612 node = rbtree_finddata(instance_tree, &myNode);
613 if (node) return node;
615 if (!do_link) return NULL;
617 name1 = cf_section_name1(cs);
620 * Found the configuration entry.
622 node = talloc_zero(cs, module_instance_t);
624 node->insthandle = NULL;
628 * Names in the "modules" section aren't prefixed
629 * with "rlm_", so we add it here.
631 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
633 node->entry = linkto_module(module_name, cs);
636 /* linkto_module logs any errors */
640 if (check_config && (node->entry->module->instantiate) &&
641 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
642 const char *value = NULL;
645 cp = cf_pair_find(cs, "force_check_config");
646 if (cp) value = cf_pair_value(cp);
648 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
650 cf_log_module(cs, "Skipping instantiation of %s", instname);
653 check_config_safe = TRUE;
654 cf_log_module(cs, "Instantiating module \"%s\" from file %s",
655 instname, cf_section_filename(cs));
659 * Call the module's instantiation routine.
661 if ((node->entry->module->instantiate) &&
662 (!check_config || check_config_safe) &&
663 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
664 cf_log_err(cf_sectiontoitem(cs),
665 "Instantiation failed for module \"%s\"",
667 if (node->entry->module->detach) {
668 talloc_set_destructor((void *) node->insthandle,
669 node->entry->module->detach);
671 talloc_free(node->insthandle);
676 if (node->insthandle) {
677 talloc_set_destructor((void *) node->insthandle,
678 node->entry->module->detach);
679 talloc_steal(node, node->insthandle);
683 * We're done. Fill in the rest of the data structure,
684 * and link it to the module instance list.
686 strlcpy(node->name, instname, sizeof(node->name));
688 #ifdef HAVE_PTHREAD_H
690 * If we're threaded, check if the module is thread-safe.
692 * If it isn't, we create a mutex.
694 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
695 node->mutex = talloc_zero(node, pthread_mutex_t);
698 * Initialize the mutex.
700 pthread_mutex_init(node->mutex, NULL);
703 * The module is thread-safe. Don't give it a mutex.
709 rbtree_insert(instance_tree, node);
714 static indexed_modcallable *lookup_by_index(rbtree_t *components,
717 indexed_modcallable myc;
722 return rbtree_finddata(components, &myc);
726 * Create a new sublist.
728 static indexed_modcallable *new_sublist(CONF_SECTION *cs,
729 rbtree_t *components, int comp, int idx)
731 indexed_modcallable *c;
733 c = lookup_by_index(components, comp, idx);
735 /* It is an error to try to create a sublist that already
736 * exists. It would almost certainly be caused by accidental
737 * duplication in the config file.
739 * index 0 is the exception, because it is used when we want
740 * to collect _all_ listed modules under a single index by
741 * default, which is currently the case in all components
742 * except authenticate. */
750 c = talloc_zero(cs, indexed_modcallable);
751 c->modulelist = NULL;
755 if (!rbtree_insert(components, c)) {
763 rlm_rcode_t indexed_modcall(int comp, int idx, REQUEST *request)
766 modcallable *list = NULL;
767 virtual_server_t *server;
770 * Hack to find the correct virtual server.
772 server = virtual_server_find(request->server);
774 RDEBUG("No such virtual server \"%s\"", request->server);
775 return RLM_MODULE_FAIL;
779 list = server->mc[comp];
780 if (!list) RDEBUG2W("Empty %s section. Using default return values.", section_type_value[comp].section);
783 indexed_modcallable *this;
785 this = lookup_by_index(server->components, comp, idx);
787 list = this->modulelist;
789 RDEBUG2W("Unknown value specified for %s. Cannot perform requested action.",
790 section_type_value[comp].typename);
794 if (server->subcs[comp]) {
796 RDEBUG("# Executing section %s from file %s",
797 section_type_value[comp].section,
798 cf_section_filename(server->subcs[comp]));
800 RDEBUG("# Executing group from file %s",
801 cf_section_filename(server->subcs[comp]));
804 request->component = section_type_value[comp].section;
806 rcode = modcall(comp, list, request);
808 request->module = "";
809 request->component = "<core>";
814 * Load a sub-module list, as found inside an Auth-Type foo {}
817 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
818 rbtree_t *components,
819 const DICT_ATTR *dattr, int comp)
821 indexed_modcallable *subcomp;
824 const char *name2 = cf_section_name2(cs);
826 rad_assert(comp >= RLM_COMPONENT_AUTH);
827 rad_assert(comp < RLM_COMPONENT_COUNT);
833 cf_log_err(cf_sectiontoitem(cs),
834 "No name specified for %s block",
835 section_type_value[comp].typename);
842 ml = compile_modgroup(parent, comp, cs);
848 * We must assign a numeric index to this subcomponent.
849 * It is generated and placed in the dictionary
850 * automatically. If it isn't found, it's a serious
853 dval = dict_valbyname(dattr->attr, dattr->vendor, name2);
855 cf_log_err(cf_sectiontoitem(cs),
856 "%s %s Not previously configured",
857 section_type_value[comp].typename, name2);
858 modcallable_free(&ml);
862 subcomp = new_sublist(cs, components, comp, dval->value);
864 modcallable_free(&ml);
868 subcomp->modulelist = ml;
872 static int define_type(const DICT_ATTR *dattr, const char *name)
878 * If the value already exists, don't
881 dval = dict_valbyname(dattr->attr, dattr->vendor, name);
885 * Create a new unique value with a
886 * meaningless number. You can't look at
887 * it from outside of this code, so it
888 * doesn't matter. The only requirement
889 * is that it's unique.
892 value = fr_rand() & 0x00ffffff;
893 } while (dict_valbyattr(dattr->attr, dattr->vendor, value));
895 DEBUG2(" Module: Creating %s = %s", dattr->name, name);
896 if (dict_addvalue(name, dattr->name, value) < 0) {
897 radlog(L_ERR, "%s", fr_strerror());
904 static int load_component_section(CONF_SECTION *cs,
905 rbtree_t *components, int comp)
910 indexed_modcallable *subcomp;
912 const char *visiblename;
913 const DICT_ATTR *dattr;
916 * Find the attribute used to store VALUEs for this section.
918 dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
920 cf_log_err(cf_sectiontoitem(cs),
921 "No such attribute %s",
922 section_type_value[comp].typename);
927 * Loop over the entries in the named section, loading
928 * the sections this time.
930 for (modref = cf_item_find_next(cs, NULL);
932 modref = cf_item_find_next(cs, modref)) {
934 CONF_PAIR *cp = NULL;
935 CONF_SECTION *scs = NULL;
937 if (cf_item_is_section(modref)) {
938 scs = cf_itemtosection(modref);
940 name1 = cf_section_name1(scs);
943 section_type_value[comp].typename) == 0) {
944 if (!load_subcomponent_section(NULL, scs,
948 return -1; /* FIXME: memleak? */
955 } else if (cf_item_is_pair(modref)) {
956 cp = cf_itemtopair(modref);
959 continue; /* ignore it */
963 * Try to compile one entry.
965 this = compile_modsingle(NULL, comp, modref, &modname);
968 * It's OK for the module to not exist.
970 if (!this && modname && (modname[0] == '-')) {
971 DEBUGW("Not loading module \"%s\" as it is not enabled", modname + 1);
976 cf_log_err(cf_sectiontoitem(cs),
977 "Errors parsing %s section.\n",
978 cf_section_name1(cs));
983 * Look for Auth-Type foo {}, which are special
984 * cases of named sections, and allowable ONLY
987 * i.e. They're not allowed in a "group" or "redundant"
990 if (comp == RLM_COMPONENT_AUTH) {
992 const char *modrefname = NULL;
994 modrefname = cf_pair_attr(cp);
996 modrefname = cf_section_name2(scs);
998 modcallable_free(&this);
999 cf_log_err(cf_sectiontoitem(cs),
1000 "Errors parsing %s sub-section.\n",
1001 cf_section_name1(scs));
1006 dval = dict_valbyname(PW_AUTH_TYPE, 0, modrefname);
1009 * It's a section, but nothing we
1012 modcallable_free(&this);
1013 cf_log_err(cf_sectiontoitem(cs),
1014 "Unknown Auth-Type \"%s\" in %s sub-section.",
1015 modrefname, section_type_value[comp].section);
1020 /* See the comment in new_sublist() for explanation
1021 * of the special index 0 */
1025 subcomp = new_sublist(cs, components, comp, idx);
1026 if (subcomp == NULL) {
1027 modcallable_free(&this);
1031 /* If subcomp->modulelist is NULL, add_to_modcallable will
1033 visiblename = cf_section_name2(cs);
1034 if (visiblename == NULL)
1035 visiblename = cf_section_name1(cs);
1036 add_to_modcallable(&subcomp->modulelist, this,
1043 static int load_byserver(CONF_SECTION *cs)
1046 const char *name = cf_section_name2(cs);
1047 rbtree_t *components;
1048 virtual_server_t *server = NULL;
1049 indexed_modcallable *c;
1052 cf_log_info(cs, "server %s { # from file %s",
1053 name, cf_section_filename(cs));
1055 cf_log_info(cs, "server { # from file %s",
1056 cf_section_filename(cs));
1059 cf_log_info(cs, " modules {");
1061 components = rbtree_create(indexed_modcallable_cmp,
1062 indexed_modcallable_free, 0);
1064 radlog(L_ERR, "Failed to initialize components\n");
1068 server = talloc_zero(cs, virtual_server_t);
1069 server->name = name;
1070 server->created = time(NULL);
1072 server->components = components;
1073 talloc_set_destructor((void *) server, virtual_server_free);
1076 * Define types first.
1078 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1079 CONF_SECTION *subcs;
1081 const DICT_ATTR *dattr;
1083 subcs = cf_section_sub_find(cs,
1084 section_type_value[comp].section);
1085 if (!subcs) continue;
1087 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1090 * Find the attribute used to store VALUEs for this section.
1092 dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
1094 cf_log_err(cf_sectiontoitem(subcs),
1095 "No such attribute %s",
1096 section_type_value[comp].typename);
1098 if (debug_flag == 0) {
1099 radlog(L_ERR, "Failed to load virtual server %s",
1100 (name != NULL) ? name : "<default>");
1102 talloc_free(server);
1107 * Define dynamic types, so that others can reference
1110 for (modref = cf_item_find_next(subcs, NULL);
1112 modref = cf_item_find_next(subcs, modref)) {
1114 CONF_SECTION *subsubcs;
1117 * Create types for simple references
1118 * only when parsing the authenticate
1121 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
1122 cf_item_is_pair(modref)) {
1123 CONF_PAIR *cp = cf_itemtopair(modref);
1124 if (!define_type(dattr, cf_pair_attr(cp))) {
1131 if (!cf_item_is_section(modref)) continue;
1133 subsubcs = cf_itemtosection(modref);
1134 name1 = cf_section_name1(subsubcs);
1136 if (strcmp(name1, section_type_value[comp].typename) == 0) {
1137 if (!define_type(dattr,
1138 cf_section_name2(subsubcs))) {
1143 } /* loop over components */
1146 * Loop over all of the known components, finding their
1147 * configuration section, and loading it.
1150 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1151 CONF_SECTION *subcs;
1153 subcs = cf_section_sub_find(cs,
1154 section_type_value[comp].section);
1155 if (!subcs) continue;
1157 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1159 cf_log_module(cs, "Checking %s {...} for more modules to load",
1160 section_type_value[comp].section);
1163 * Skip pre/post-proxy sections if we're not
1168 !mainconfig.proxy_requests &&
1170 ((comp == RLM_COMPONENT_PRE_PROXY) ||
1171 (comp == RLM_COMPONENT_POST_PROXY))) {
1175 #ifndef WITH_ACCOUNTING
1176 if (comp == RLM_COMPONENT_ACCT) continue;
1179 #ifndef WITH_SESSION_MGMT
1180 if (comp == RLM_COMPONENT_SESS) continue;
1183 if (load_component_section(subcs, components, comp) < 0) {
1188 * Cache a default, if it exists. Some people
1189 * put empty sections for some reason...
1191 c = lookup_by_index(components, comp, 0);
1192 if (c) server->mc[comp] = c->modulelist;
1194 server->subcs[comp] = subcs;
1197 } /* loop over components */
1200 * We haven't loaded any of the normal sections. Maybe we're
1201 * supposed to load the vmps section.
1203 * This is a bit of a hack...
1206 CONF_SECTION *subcs;
1208 subcs = cf_section_sub_find(cs, "vmps");
1210 cf_log_module(cs, "Checking vmps {...} for more modules to load");
1211 if (load_component_section(subcs, components,
1212 RLM_COMPONENT_POST_AUTH) < 0) {
1215 c = lookup_by_index(components,
1216 RLM_COMPONENT_POST_AUTH, 0);
1217 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1223 const DICT_ATTR *dattr;
1225 dattr = dict_attrbyname("DHCP-Message-Type");
1228 * Handle each DHCP Message type separately.
1230 if (dattr) for (subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1232 subcs = cf_subsection_find_next(cs, subcs,
1234 const char *name2 = cf_section_name2(subcs);
1236 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
1237 if (!load_subcomponent_section(NULL, subcs,
1240 RLM_COMPONENT_POST_AUTH)) {
1241 goto error; /* FIXME: memleak? */
1243 c = lookup_by_index(components,
1244 RLM_COMPONENT_POST_AUTH, 0);
1245 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1252 cf_log_info(cs, " } # modules");
1253 cf_log_info(cs, "} # server");
1255 if (!flag && name) {
1256 DEBUGW("Server %s is empty, and will do nothing!",
1260 if (debug_flag == 0) {
1261 radlog(L_INFO, "Loaded virtual server %s",
1262 (name != NULL) ? name : "<default>");
1266 * Now that it is OK, insert it into the list.
1268 * This is thread-safe...
1270 comp = virtual_server_idx(name);
1271 server->next = virtual_servers[comp];
1272 virtual_servers[comp] = server;
1275 * Mark OLDER ones of the same name as being unused.
1277 server = server->next;
1279 if ((!name && !server->name) ||
1280 (name && server->name &&
1281 (strcmp(server->name, name) == 0))) {
1282 server->can_free = TRUE;
1285 server = server->next;
1293 * Load all of the virtual servers.
1295 int virtual_servers_load(CONF_SECTION *config)
1298 static int first_time = TRUE;
1300 DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1303 * If we have "server { ...}", then there SHOULD NOT be
1304 * bare "authorize", etc. sections. if there is no such
1305 * server, then try to load the old-style sections first.
1307 * In either case, load the "default" virtual server first.
1308 * this matches better iwth users expectations.
1310 cs = cf_section_find_name2(cf_subsection_find_next(config, NULL,
1314 if (load_byserver(cs) < 0) {
1318 if (load_byserver(config) < 0) {
1324 * Load all of the virtual servers.
1326 for (cs = cf_subsection_find_next(config, NULL, "server");
1328 cs = cf_subsection_find_next(config, cs, "server")) {
1330 virtual_server_t *server;
1332 name2 = cf_section_name2(cs);
1333 if (!name2) continue; /* handled above */
1335 server = virtual_server_find(name2);
1337 (cf_top_section(server->cs) == config)) {
1338 radlog(L_ERR, "Duplicate virtual server \"%s\" in file %s:%d and file %s:%d",
1340 cf_section_filename(server->cs),
1341 cf_section_lineno(server->cs),
1342 cf_section_filename(cs),
1343 cf_section_lineno(cs));
1347 if (load_byserver(cs) < 0) {
1349 * Once we successfully started once,
1350 * continue loading the OTHER servers,
1351 * even if one fails.
1353 if (!first_time) continue;
1359 * If we succeed the first time around, remember that.
1366 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1368 void *insthandle = NULL;
1369 fr_module_hup_t *mh;
1372 !node->entry->module->instantiate ||
1373 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1377 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1379 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
1380 cf_log_err(cf_sectiontoitem(cs),
1381 "HUP failed for module \"%s\". Using old configuration.",
1386 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
1388 module_instance_free_old(cs, node, when);
1391 * Save the old instance handle for later deletion.
1393 mh = talloc_zero(cs, fr_module_hup_t);
1396 mh->insthandle = node->insthandle;
1397 mh->next = node->mh;
1400 node->insthandle = insthandle;
1403 * FIXME: Set a timeout to come back in 60s, so that
1404 * we can pro-actively clean up the old instances.
1411 int module_hup(CONF_SECTION *modules)
1416 module_instance_t *node;
1418 if (!modules) return 0;
1423 * Loop over the modules
1425 for (ci=cf_item_find_next(modules, NULL);
1427 ci=cf_item_find_next(modules, ci)) {
1428 const char *instname;
1429 module_instance_t myNode;
1432 * If it's not a section, ignore it.
1434 if (!cf_item_is_section(ci)) continue;
1436 cs = cf_itemtosection(ci);
1437 instname = cf_section_name2(cs);
1438 if (!instname) instname = cf_section_name1(cs);
1440 strlcpy(myNode.name, instname, sizeof(myNode.name));
1441 node = rbtree_finddata(instance_tree, &myNode);
1443 module_hup_module(cs, node, when);
1451 * Parse the module config sections, and load
1452 * and call each module's init() function.
1454 * Libtool makes your life a LOT easier, especially with libltdl.
1455 * see: http://www.gnu.org/software/libtool/
1457 int setup_modules(int reload, CONF_SECTION *config)
1459 CONF_ITEM *ci, *next;
1460 CONF_SECTION *cs, *modules;
1461 rad_listen_t *listener;
1463 if (reload) return 0;
1466 * If necessary, initialize libltdl.
1470 * This line works around a completely
1472 * RIDICULOUS INSANE IDIOTIC
1474 * bug in libltdl on certain systems. The "set
1475 * preloaded symbols" macro below ends up
1476 * referencing this name, but it isn't defined
1477 * anywhere in the libltdl source. As a result,
1478 * any program STUPID enough to rely on libltdl
1479 * fails to link, because the symbol isn't
1482 * It's like libtool and libltdl are some kind
1485 #ifdef IE_LIBTOOL_DIE
1486 #define lt__PROGRAM__LTX_preloaded_symbols lt_libltdl_LTX_preloaded_symbols
1490 * Set the default list of preloaded symbols.
1491 * This is used to initialize libltdl's list of
1492 * preloaded modules.
1494 * i.e. Static modules.
1496 LTDL_SET_PRELOADED_SYMBOLS();
1498 if (lt_dlinit() != 0) {
1499 radlog(L_ERR, "Failed to initialize libraries: %s\n",
1505 * Set the search path to ONLY our library directory.
1506 * This prevents the modules from being found from
1507 * any location on the disk.
1509 lt_dlsetsearchpath(radlib_dir);
1512 * Set up the internal module struct.
1514 module_tree = rbtree_create(module_entry_cmp, NULL, 0);
1516 radlog(L_ERR, "Failed to initialize modules\n");
1520 instance_tree = rbtree_create(module_instance_cmp,
1521 module_instance_free, 0);
1522 if (!instance_tree) {
1523 radlog(L_ERR, "Failed to initialize modules\n");
1528 memset(virtual_servers, 0, sizeof(virtual_servers));
1531 * Remember where the modules were stored.
1533 modules = cf_section_sub_find(config, "modules");
1535 radlog(L_INFO, "WARNING: Cannot find a \"modules\" section in the configuration file!");
1538 DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1541 * Loop over module definitions, looking for duplicates.
1543 * This is O(N^2) in the number of modules, but most
1544 * systems should have less than 100 modules.
1546 for (ci=cf_item_find_next(modules, NULL);
1549 const char *name1, *name2;
1550 CONF_SECTION *subcs, *duplicate;
1552 next = cf_item_find_next(modules, ci);
1554 if (!cf_item_is_section(ci)) continue;
1556 if (!next || !cf_item_is_section(next)) continue;
1558 subcs = cf_itemtosection(ci);
1559 name1 = cf_section_name1(subcs);
1560 name2 = cf_section_name2(subcs);
1562 duplicate = cf_section_find_name2(cf_itemtosection(next),
1564 if (!duplicate) continue;
1566 if (!name2) name2 = "";
1568 radlog(L_ERR, "Duplicate module \"%s %s\", in file %s:%d and file %s:%d",
1570 cf_section_filename(subcs),
1571 cf_section_lineno(subcs),
1572 cf_section_filename(duplicate),
1573 cf_section_lineno(duplicate));
1578 * Look for the 'instantiate' section, which tells us
1579 * the instantiation order of the modules, and also allows
1580 * us to load modules with no authorize/authenticate/etc.
1583 cs = cf_section_sub_find(config, "instantiate");
1586 module_instance_t *module;
1589 cf_log_info(cs, " instantiate {");
1592 * Loop over the items in the 'instantiate' section.
1594 for (ci=cf_item_find_next(cs, NULL);
1596 ci=cf_item_find_next(cs, ci)) {
1599 * Skip sections and "other" stuff.
1600 * Sections will be handled later, if
1601 * they're referenced at all...
1603 if (!cf_item_is_pair(ci)) {
1607 cp = cf_itemtopair(ci);
1608 name = cf_pair_attr(cp);
1609 module = find_module_instance(modules, name, 1);
1610 if (!module && (name[0] != '-')) {
1613 } /* loop over items in the subsection */
1615 cf_log_info(cs, " }");
1616 } /* if there's an 'instantiate' section. */
1619 * Loop over the listeners, figuring out which sections
1622 for (listener = mainconfig.listen;
1624 listener = listener->next) {
1628 if (listener->type == RAD_LISTEN_PROXY) continue;
1631 cs = cf_section_sub_find_name2(config,
1632 "server", listener->server);
1633 if (!cs && (listener->server != NULL)) {
1634 listener->print(listener, buffer, sizeof(buffer));
1636 radlog(L_ERR, "No server has been defined for %s", buffer);
1641 if (virtual_servers_load(config) < 0) return -1;
1647 * Call all authorization modules until one returns
1648 * somethings else than RLM_MODULE_OK
1650 rlm_rcode_t module_authorize(int autz_type, REQUEST *request)
1652 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1656 * Authenticate a user/password with various methods.
1658 rlm_rcode_t module_authenticate(int auth_type, REQUEST *request)
1660 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1663 #ifdef WITH_ACCOUNTING
1665 * Do pre-accounting for ALL configured sessions
1667 rlm_rcode_t module_preacct(REQUEST *request)
1669 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1673 * Do accounting for ALL configured sessions
1675 rlm_rcode_t module_accounting(int acct_type, REQUEST *request)
1677 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1681 #ifdef WITH_SESSION_MGMT
1683 * See if a user is already logged in.
1685 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1687 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1691 if(!request->username)
1694 request->simul_count = 0;
1695 request->simul_max = maxsimul;
1696 request->simul_mpp = 1;
1698 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1700 if (rcode != RLM_MODULE_OK) {
1701 /* FIXME: Good spot for a *rate-limited* warning to the log */
1705 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1711 * Do pre-proxying for ALL configured sessions
1713 rlm_rcode_t module_pre_proxy(int type, REQUEST *request)
1715 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1719 * Do post-proxying for ALL configured sessions
1721 rlm_rcode_t module_post_proxy(int type, REQUEST *request)
1723 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1728 * Do post-authentication for ALL configured sessions
1730 rlm_rcode_t module_post_auth(int postauth_type, REQUEST *request)
1732 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1736 rlm_rcode_t module_recv_coa(int recv_coa_type, REQUEST *request)
1738 return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
1741 rlm_rcode_t module_send_coa(int send_coa_type, REQUEST *request)
1743 return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);
1747 char *module_failure_msg(REQUEST *request, const char *fmt, ...)
1753 vp = paircreate(PW_MODULE_FAILURE_MESSAGE, 0);
1759 vsnprintf(vp->vp_strvalue, sizeof(vp->vp_strvalue), fmt, ap);
1761 pairadd(&request->packet->vps, vp);
1763 return vp->vp_strvalue;