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 }
89 #ifdef WITHOUT_LIBLTDL
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"
119 * This doesn't really do anything...
121 p = getenv(LD_LIBRARY_PATH);
123 snprintf(buffer, sizeof(buffer), "%s:%s", p, radlib_dir);
129 return setenv(LD_LIBRARY_PATH, val, 1);
132 lt_dlhandle lt_dlopenext(const char *name)
138 * Prefer loading our libraries by absolute path.
140 snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT);
141 handle = dlopen(buffer, RTLD_NOW | RTLD_LOCAL);
142 if (handle) return handle;
144 strlcpy(buffer, name, sizeof(buffer));
147 * FIXME: Make this configurable...
149 strlcat(buffer, LT_SHREXT, sizeof(buffer));
151 return dlopen(buffer, RTLD_NOW | RTLD_LOCAL);
154 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
156 return dlsym(handle, symbol);
159 int lt_dlclose(lt_dlhandle handle)
161 if (!handle) return 0;
163 return dlclose(handle);
166 const char *lt_dlerror(void)
172 #else /* without dlopen */
173 typedef struct lt_dlmodule_t {
178 typedef struct eap_type_t EAP_TYPE;
179 typedef struct rlm_sql_module_t rlm_sql_module_t;
182 * FIXME: Write hackery to auto-generate this data.
183 * We only need to do this on systems that don't have dlopen.
185 extern module_t rlm_pap;
186 extern module_t rlm_chap;
187 extern module_t rlm_eap;
188 extern module_t rlm_sql;
191 extern EAP_TYPE rlm_eap_md5;
192 extern rlm_sql_module_t rlm_sql_mysql;
195 static const lt_dlmodule_t lt_dlmodules[] = {
196 { "rlm_pap", &rlm_pap },
197 { "rlm_chap", &rlm_chap },
198 { "rlm_eap", &rlm_eap },
201 { "rlm_eap_md5", &rlm_eap_md5 },
204 { "rlm_sql_mysql", &rlm_sql_mysql },
210 #define fr_dlopenext lt_dlopenext
211 lt_dlhandle lt_dlopenext(const char *name)
215 for (i = 0; lt_dlmodules[i].name != NULL; i++) {
216 if (strcmp(name, lt_dlmodules[i].name) == 0) {
217 return lt_dlmodules[i].ref;
224 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
229 int lt_dlclose(lt_dlhandle handle)
234 const char *lt_dlerror(void)
236 return "Unspecified error";
239 #endif /* WITH_DLOPEN */
240 #else /* WITHOUT_LIBLTDL */
243 * Solve the issues of libraries linking to other libraries
244 * by using a newer libltdl API.
246 #ifndef HAVE_LT_DLADVISE_INIT
247 #define fr_dlopenext lt_dlopenext
249 static lt_dlhandle fr_dlopenext(const char *filename)
251 lt_dlhandle handle = 0;
254 if (!lt_dladvise_init (&advise) &&
255 !lt_dladvise_ext (&advise) &&
256 !lt_dladvise_global (&advise)) {
257 handle = lt_dlopenadvise (filename, advise);
260 lt_dladvise_destroy (&advise);
264 #endif /* HAVE_LT_DLADVISE_INIT */
265 #endif /* WITHOUT_LIBLTDL */
267 static int virtual_server_idx(const char *name)
273 hash = fr_hash_string(name);
275 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
278 static virtual_server_t *virtual_server_find(const char *name)
281 virtual_server_t *server;
283 rcode = virtual_server_idx(name);
284 for (server = virtual_servers[rcode];
286 server = server->next) {
287 if (!name && !server->name) break;
289 if ((name && server->name) &&
290 (strcmp(name, server->name) == 0)) break;
296 static void virtual_server_free(virtual_server_t *server)
300 if (server->components) rbtree_free(server->components);
301 server->components = NULL;
306 void virtual_servers_free(time_t when)
309 virtual_server_t **last;
311 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
312 virtual_server_t *server, *next;
314 last = &virtual_servers[i];
315 for (server = virtual_servers[i];
321 * If we delete it, fix the links so that
322 * we don't orphan anything. Also,
323 * delete it if it's old, AND a newer one
326 * Otherwise, the last pointer gets set to
327 * the one we didn't delete.
330 ((server->created < when) && server->can_free)) {
331 *last = server->next;
332 virtual_server_free(server);
334 last = &(server->next);
340 static void indexed_modcallable_free(void *data)
342 indexed_modcallable *c = data;
344 modcallable_free(&c->modulelist);
348 static int indexed_modcallable_cmp(const void *one, const void *two)
350 const indexed_modcallable *a = one;
351 const indexed_modcallable *b = two;
353 if (a->comp < b->comp) return -1;
354 if (a->comp > b->comp) return +1;
356 return a->idx - b->idx;
361 * Compare two module entries
363 static int module_instance_cmp(const void *one, const void *two)
365 const module_instance_t *a = one;
366 const module_instance_t *b = two;
368 return strcmp(a->name, b->name);
372 static void module_instance_free_old(CONF_SECTION *cs, module_instance_t *node,
375 fr_module_hup_t *mh, **last;
378 * Walk the list, freeing up old instances.
385 * Free only every 60 seconds.
387 if ((when - mh->when) < 60) {
392 cf_section_parse_free(cs, mh->insthandle);
394 if (node->entry->module->detach) {
395 (node->entry->module->detach)(mh->insthandle);
397 free(mh->insthandle);
407 * Free a module instance.
409 static void module_instance_free(void *data)
411 module_instance_t *this = data;
413 module_instance_free_old(this->cs, this, time(NULL) + 100);
415 if (this->entry->module->detach) {
416 (this->entry->module->detach)(this->insthandle);
419 #ifdef HAVE_PTHREAD_H
423 * The mutex MIGHT be locked...
424 * we'll check for that later, I guess.
426 pthread_mutex_destroy(this->mutex);
430 memset(this, 0, sizeof(*this));
436 * Compare two module entries
438 static int module_entry_cmp(const void *one, const void *two)
440 const module_entry_t *a = one;
441 const module_entry_t *b = two;
443 return strcmp(a->name, b->name);
447 * Free a module entry.
449 static void module_entry_free(void *data)
451 module_entry_t *this = data;
453 lt_dlclose(this->handle); /* ignore any errors */
454 memset(this, 0, sizeof(*this));
460 * Remove the module lists.
462 int detach_modules(void)
464 rbtree_free(instance_tree);
465 rbtree_free(module_tree);
474 * Find a module on disk or in memory, and link to it.
476 static module_entry_t *linkto_module(const char *module_name,
479 module_entry_t myentry;
480 module_entry_t *node;
481 lt_dlhandle handle = NULL;
482 char module_struct[256];
484 const module_t *module;
486 strlcpy(myentry.name, module_name, sizeof(myentry.name));
487 node = rbtree_finddata(module_tree, &myentry);
488 if (node) return node;
491 * Link to the module's rlm_FOO{} module structure.
493 * The module_name variable has the version number
494 * embedded in it, and we don't want that here.
496 strcpy(module_struct, module_name);
497 p = strrchr(module_struct, '-');
500 #if defined(WITHOUT_LIBLTDL) && defined (WITH_DLOPEN) && defined(RTLD_SELF)
501 module = lt_dlsym(RTLD_SELF, module_struct);
502 if (module) goto open_self;
506 * Keep the handle around so we can dlclose() it.
508 handle = fr_dlopenext(module_name);
509 if (handle == NULL) {
510 cf_log_err(cf_sectiontoitem(cs),
511 "Failed to link to module '%s': %s\n",
512 module_name, lt_dlerror());
516 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
519 * libltld MAY core here, if the handle it gives us contains
522 module = lt_dlsym(handle, module_struct);
524 cf_log_err(cf_sectiontoitem(cs),
525 "Failed linking to %s structure: %s\n",
526 module_name, lt_dlerror());
531 #if defined(WITHOUT_LIBLTDL) && defined (WITH_DLOPEN) && defined(RTLD_SELF)
535 * Before doing anything else, check if it's sane.
537 if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
539 cf_log_err(cf_sectiontoitem(cs),
540 "Invalid version in module '%s'",
546 /* make room for the module type */
547 node = rad_malloc(sizeof(*node));
548 memset(node, 0, sizeof(*node));
549 strlcpy(node->name, module_name, sizeof(node->name));
550 node->module = module;
551 node->handle = handle;
553 cf_log_module(cs, "Linked to module %s", module_name);
556 * Add the module as "rlm_foo-version" to the configuration
559 if (!rbtree_insert(module_tree, node)) {
560 radlog(L_ERR, "Failed to cache module %s", module_name);
570 * Find a module instance.
572 module_instance_t *find_module_instance(CONF_SECTION *modules,
573 const char *instname, int do_link)
575 int check_config_safe = FALSE;
578 module_instance_t *node, myNode;
579 char module_name[256];
581 if (!modules) return NULL;
584 * Module instances are declared in the modules{} block
585 * and referenced later by their name, which is the
586 * name2 from the config section, or name1 if there was
589 cs = cf_section_sub_find_name2(modules, NULL, instname);
591 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
596 * If there's already a module instance, return it.
598 strlcpy(myNode.name, instname, sizeof(myNode.name));
599 node = rbtree_finddata(instance_tree, &myNode);
600 if (node) return node;
602 if (!do_link) return NULL;
604 name1 = cf_section_name1(cs);
607 * Found the configuration entry.
609 node = rad_malloc(sizeof(*node));
610 memset(node, 0, sizeof(*node));
612 node->insthandle = NULL;
616 * Names in the "modules" section aren't prefixed
617 * with "rlm_", so we add it here.
619 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
621 node->entry = linkto_module(module_name, cs);
624 /* linkto_module logs any errors */
628 if (check_config && (node->entry->module->instantiate) &&
629 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
630 const char *value = NULL;
633 cp = cf_pair_find(cs, "force_check_config");
634 if (cp) value = cf_pair_value(cp);
636 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
638 cf_log_module(cs, "Skipping instantiation of %s", instname);
641 check_config_safe = TRUE;
642 cf_log_module(cs, "Instantiating module \"%s\" from file %s",
643 instname, cf_section_filename(cs));
647 * Call the module's instantiation routine.
649 if ((node->entry->module->instantiate) &&
650 (!check_config || check_config_safe) &&
651 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
652 cf_log_err(cf_sectiontoitem(cs),
653 "Instantiation failed for module \"%s\"",
660 * We're done. Fill in the rest of the data structure,
661 * and link it to the module instance list.
663 strlcpy(node->name, instname, sizeof(node->name));
665 #ifdef HAVE_PTHREAD_H
667 * If we're threaded, check if the module is thread-safe.
669 * If it isn't, we create a mutex.
671 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
672 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
674 * Initialize the mutex.
676 pthread_mutex_init(node->mutex, NULL);
679 * The module is thread-safe. Don't give it a mutex.
685 rbtree_insert(instance_tree, node);
690 static indexed_modcallable *lookup_by_index(rbtree_t *components,
693 indexed_modcallable myc;
698 return rbtree_finddata(components, &myc);
702 * Create a new sublist.
704 static indexed_modcallable *new_sublist(rbtree_t *components, int comp, int idx)
706 indexed_modcallable *c;
708 c = lookup_by_index(components, comp, idx);
710 /* It is an error to try to create a sublist that already
711 * exists. It would almost certainly be caused by accidental
712 * duplication in the config file.
714 * index 0 is the exception, because it is used when we want
715 * to collect _all_ listed modules under a single index by
716 * default, which is currently the case in all components
717 * except authenticate. */
725 c = rad_malloc(sizeof(*c));
726 c->modulelist = NULL;
730 if (!rbtree_insert(components, c)) {
738 int indexed_modcall(int comp, int idx, REQUEST *request)
741 modcallable *list = NULL;
742 virtual_server_t *server;
745 * Hack to find the correct virtual server.
747 server = virtual_server_find(request->server);
749 RDEBUG("No such virtual server \"%s\"", request->server);
750 return RLM_MODULE_FAIL;
754 list = server->mc[comp];
755 if (!list) RDEBUG2(" WARNING: Empty %s section. Using default return values.", section_type_value[comp].section);
758 indexed_modcallable *this;
760 this = lookup_by_index(server->components, comp, idx);
762 list = this->modulelist;
764 RDEBUG2(" WARNING: Unknown value specified for %s. Cannot perform requested action.",
765 section_type_value[comp].typename);
769 if (server->subcs[comp]) {
771 RDEBUG("# Executing section %s from file %s",
772 section_type_value[comp].section,
773 cf_section_filename(server->subcs[comp]));
775 RDEBUG("# Executing group from file %s",
776 cf_section_filename(server->subcs[comp]));
779 request->component = section_type_value[comp].section;
781 rcode = modcall(comp, list, request);
783 request->module = "";
784 request->component = "<core>";
789 * Load a sub-module list, as found inside an Auth-Type foo {}
792 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
793 rbtree_t *components,
794 const DICT_ATTR *dattr, int comp)
796 indexed_modcallable *subcomp;
799 const char *name2 = cf_section_name2(cs);
801 rad_assert(comp >= RLM_COMPONENT_AUTH);
802 rad_assert(comp < RLM_COMPONENT_COUNT);
808 cf_log_err(cf_sectiontoitem(cs),
809 "No name specified for %s block",
810 section_type_value[comp].typename);
817 ml = compile_modgroup(parent, comp, cs);
823 * We must assign a numeric index to this subcomponent.
824 * It is generated and placed in the dictionary
825 * automatically. If it isn't found, it's a serious
828 dval = dict_valbyname(dattr->attr, dattr->vendor, name2);
830 cf_log_err(cf_sectiontoitem(cs),
831 "%s %s Not previously configured",
832 section_type_value[comp].typename, name2);
833 modcallable_free(&ml);
837 subcomp = new_sublist(components, comp, dval->value);
839 modcallable_free(&ml);
843 subcomp->modulelist = ml;
847 static int define_type(const DICT_ATTR *dattr, const char *name)
853 * If the value already exists, don't
856 dval = dict_valbyname(dattr->attr, dattr->vendor, name);
860 * Create a new unique value with a
861 * meaningless number. You can't look at
862 * it from outside of this code, so it
863 * doesn't matter. The only requirement
864 * is that it's unique.
867 value = fr_rand() & 0x00ffffff;
868 } while (dict_valbyattr(dattr->attr, dattr->vendor, value));
870 DEBUG2(" Module: Creating %s = %s", dattr->name, name);
871 if (dict_addvalue(name, dattr->name, value) < 0) {
872 radlog(L_ERR, "%s", fr_strerror());
879 static int load_component_section(CONF_SECTION *cs,
880 rbtree_t *components, int comp)
885 indexed_modcallable *subcomp;
887 const char *visiblename;
888 const DICT_ATTR *dattr;
891 * Find the attribute used to store VALUEs for this section.
893 dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
895 cf_log_err(cf_sectiontoitem(cs),
896 "No such attribute %s",
897 section_type_value[comp].typename);
902 * Loop over the entries in the named section, loading
903 * the sections this time.
905 for (modref = cf_item_find_next(cs, NULL);
907 modref = cf_item_find_next(cs, modref)) {
909 CONF_PAIR *cp = NULL;
910 CONF_SECTION *scs = NULL;
912 if (cf_item_is_section(modref)) {
913 scs = cf_itemtosection(modref);
915 name1 = cf_section_name1(scs);
918 section_type_value[comp].typename) == 0) {
919 if (!load_subcomponent_section(NULL, scs,
923 return -1; /* FIXME: memleak? */
930 } else if (cf_item_is_pair(modref)) {
931 cp = cf_itemtopair(modref);
934 continue; /* ignore it */
938 * Try to compile one entry.
940 this = compile_modsingle(NULL, comp, modref, &modname);
942 cf_log_err(cf_sectiontoitem(cs),
943 "Errors parsing %s section.\n",
944 cf_section_name1(cs));
949 * Look for Auth-Type foo {}, which are special
950 * cases of named sections, and allowable ONLY
953 * i.e. They're not allowed in a "group" or "redundant"
956 if (comp == RLM_COMPONENT_AUTH) {
958 const char *modrefname = NULL;
960 modrefname = cf_pair_attr(cp);
962 modrefname = cf_section_name2(scs);
964 modcallable_free(&this);
965 cf_log_err(cf_sectiontoitem(cs),
966 "Errors parsing %s sub-section.\n",
967 cf_section_name1(scs));
972 dval = dict_valbyname(PW_AUTH_TYPE, 0, modrefname);
975 * It's a section, but nothing we
978 modcallable_free(&this);
979 cf_log_err(cf_sectiontoitem(cs),
980 "Unknown Auth-Type \"%s\" in %s sub-section.",
981 modrefname, section_type_value[comp].section);
986 /* See the comment in new_sublist() for explanation
987 * of the special index 0 */
991 subcomp = new_sublist(components, comp, idx);
992 if (subcomp == NULL) {
993 modcallable_free(&this);
997 /* If subcomp->modulelist is NULL, add_to_modcallable will
999 visiblename = cf_section_name2(cs);
1000 if (visiblename == NULL)
1001 visiblename = cf_section_name1(cs);
1002 add_to_modcallable(&subcomp->modulelist, this,
1009 static int load_byserver(CONF_SECTION *cs)
1012 const char *name = cf_section_name2(cs);
1013 rbtree_t *components;
1014 virtual_server_t *server = NULL;
1015 indexed_modcallable *c;
1018 cf_log_info(cs, "server %s { # from file %s",
1019 name, cf_section_filename(cs));
1021 cf_log_info(cs, "server { # from file %s",
1022 cf_section_filename(cs));
1025 cf_log_info(cs, " modules {");
1027 components = rbtree_create(indexed_modcallable_cmp,
1028 indexed_modcallable_free, 0);
1030 radlog(L_ERR, "Failed to initialize components\n");
1034 server = rad_malloc(sizeof(*server));
1035 memset(server, 0, sizeof(*server));
1037 server->name = name;
1038 server->created = time(NULL);
1040 server->components = components;
1043 * Define types first.
1045 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1046 CONF_SECTION *subcs;
1050 subcs = cf_section_sub_find(cs,
1051 section_type_value[comp].section);
1052 if (!subcs) continue;
1054 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1057 * Find the attribute used to store VALUEs for this section.
1059 dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
1061 cf_log_err(cf_sectiontoitem(subcs),
1062 "No such attribute %s",
1063 section_type_value[comp].typename);
1065 if (debug_flag == 0) {
1066 radlog(L_ERR, "Failed to load virtual server %s",
1067 (name != NULL) ? name : "<default>");
1069 virtual_server_free(server);
1074 * Define dynamic types, so that others can reference
1077 for (modref = cf_item_find_next(subcs, NULL);
1079 modref = cf_item_find_next(subcs, modref)) {
1081 CONF_SECTION *subsubcs;
1084 * Create types for simple references
1085 * only when parsing the authenticate
1088 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
1089 cf_item_is_pair(modref)) {
1090 CONF_PAIR *cp = cf_itemtopair(modref);
1091 if (!define_type(dattr, cf_pair_attr(cp))) {
1098 if (!cf_item_is_section(modref)) continue;
1100 subsubcs = cf_itemtosection(modref);
1101 name1 = cf_section_name1(subsubcs);
1103 if (strcmp(name1, section_type_value[comp].typename) == 0) {
1104 if (!define_type(dattr,
1105 cf_section_name2(subsubcs))) {
1110 } /* loop over components */
1113 * Loop over all of the known components, finding their
1114 * configuration section, and loading it.
1117 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1118 CONF_SECTION *subcs;
1120 subcs = cf_section_sub_find(cs,
1121 section_type_value[comp].section);
1122 if (!subcs) continue;
1124 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1126 cf_log_module(cs, "Checking %s {...} for more modules to load",
1127 section_type_value[comp].section);
1130 * Skip pre/post-proxy sections if we're not
1135 !mainconfig.proxy_requests &&
1137 ((comp == RLM_COMPONENT_PRE_PROXY) ||
1138 (comp == RLM_COMPONENT_POST_PROXY))) {
1142 #ifndef WITH_ACCOUNTING
1143 if (comp == RLM_COMPONENT_ACCT) continue;
1146 #ifndef WITH_SESSION_MGMT
1147 if (comp == RLM_COMPONENT_SESS) continue;
1150 if (load_component_section(subcs, components, comp) < 0) {
1155 * Cache a default, if it exists. Some people
1156 * put empty sections for some reason...
1158 c = lookup_by_index(components, comp, 0);
1159 if (c) server->mc[comp] = c->modulelist;
1161 server->subcs[comp] = subcs;
1164 } /* loop over components */
1167 * We haven't loaded any of the normal sections. Maybe we're
1168 * supposed to load the vmps section.
1170 * This is a bit of a hack...
1173 CONF_SECTION *subcs;
1175 subcs = cf_section_sub_find(cs, "vmps");
1177 cf_log_module(cs, "Checking vmps {...} for more modules to load");
1178 if (load_component_section(subcs, components,
1179 RLM_COMPONENT_POST_AUTH) < 0) {
1182 c = lookup_by_index(components,
1183 RLM_COMPONENT_POST_AUTH, 0);
1184 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1190 const DICT_ATTR *dattr;
1192 dattr = dict_attrbyname("DHCP-Message-Type");
1195 * Handle each DHCP Message type separately.
1197 if (dattr) for (subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1199 subcs = cf_subsection_find_next(cs, subcs,
1201 const char *name2 = cf_section_name2(subcs);
1203 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
1204 if (!load_subcomponent_section(NULL, subcs,
1207 RLM_COMPONENT_POST_AUTH)) {
1208 goto error; /* FIXME: memleak? */
1210 c = lookup_by_index(components,
1211 RLM_COMPONENT_POST_AUTH, 0);
1212 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1219 cf_log_info(cs, " } # modules");
1220 cf_log_info(cs, "} # server");
1222 if (!flag && name) {
1223 DEBUG("WARNING: Server %s is empty, and will do nothing!",
1227 if (debug_flag == 0) {
1228 radlog(L_INFO, "Loaded virtual server %s",
1229 (name != NULL) ? name : "<default>");
1233 * Now that it is OK, insert it into the list.
1235 * This is thread-safe...
1237 comp = virtual_server_idx(name);
1238 server->next = virtual_servers[comp];
1239 virtual_servers[comp] = server;
1242 * Mark OLDER ones of the same name as being unused.
1244 server = server->next;
1246 if ((!name && !server->name) ||
1247 (name && server->name &&
1248 (strcmp(server->name, name) == 0))) {
1249 server->can_free = TRUE;
1252 server = server->next;
1260 * Load all of the virtual servers.
1262 int virtual_servers_load(CONF_SECTION *config)
1265 static int first_time = TRUE;
1267 DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1270 * If we have "server { ...}", then there SHOULD NOT be
1271 * bare "authorize", etc. sections. if there is no such
1272 * server, then try to load the old-style sections first.
1274 * In either case, load the "default" virtual server first.
1275 * this matches better iwth users expectations.
1277 cs = cf_section_find_name2(cf_subsection_find_next(config, NULL,
1281 if (load_byserver(cs) < 0) {
1285 if (load_byserver(config) < 0) {
1291 * Load all of the virtual servers.
1293 for (cs = cf_subsection_find_next(config, NULL, "server");
1295 cs = cf_subsection_find_next(config, cs, "server")) {
1297 virtual_server_t *server;
1299 name2 = cf_section_name2(cs);
1300 if (!name2) continue; /* handled above */
1302 server = virtual_server_find(name2);
1304 (cf_top_section(server->cs) == config)) {
1305 radlog(L_ERR, "Duplicate virtual server \"%s\" in file %s:%d and file %s:%d",
1307 cf_section_filename(server->cs),
1308 cf_section_lineno(server->cs),
1309 cf_section_filename(cs),
1310 cf_section_lineno(cs));
1314 if (load_byserver(cs) < 0) {
1316 * Once we successfully started once,
1317 * continue loading the OTHER servers,
1318 * even if one fails.
1320 if (!first_time) continue;
1326 * If we succeed the first time around, remember that.
1333 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1335 void *insthandle = NULL;
1336 fr_module_hup_t *mh;
1339 !node->entry->module->instantiate ||
1340 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1344 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1346 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
1347 cf_log_err(cf_sectiontoitem(cs),
1348 "HUP failed for module \"%s\". Using old configuration.",
1353 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
1355 module_instance_free_old(cs, node, when);
1358 * Save the old instance handle for later deletion.
1360 mh = rad_malloc(sizeof(*mh));
1363 mh->insthandle = node->insthandle;
1364 mh->next = node->mh;
1367 node->insthandle = insthandle;
1370 * FIXME: Set a timeout to come back in 60s, so that
1371 * we can pro-actively clean up the old instances.
1378 int module_hup(CONF_SECTION *modules)
1383 module_instance_t *node;
1385 if (!modules) return 0;
1390 * Loop over the modules
1392 for (ci=cf_item_find_next(modules, NULL);
1394 ci=cf_item_find_next(modules, ci)) {
1395 const char *instname;
1396 module_instance_t myNode;
1399 * If it's not a section, ignore it.
1401 if (!cf_item_is_section(ci)) continue;
1403 cs = cf_itemtosection(ci);
1404 instname = cf_section_name2(cs);
1405 if (!instname) instname = cf_section_name1(cs);
1407 strlcpy(myNode.name, instname, sizeof(myNode.name));
1408 node = rbtree_finddata(instance_tree, &myNode);
1410 module_hup_module(cs, node, when);
1418 * Parse the module config sections, and load
1419 * and call each module's init() function.
1421 * Libtool makes your life a LOT easier, especially with libltdl.
1422 * see: http://www.gnu.org/software/libtool/
1424 int setup_modules(int reload, CONF_SECTION *config)
1426 CONF_ITEM *ci, *next;
1427 CONF_SECTION *cs, *modules;
1428 rad_listen_t *listener;
1430 if (reload) return 0;
1433 * If necessary, initialize libltdl.
1437 * This line works around a completely
1439 * RIDICULOUS INSANE IDIOTIC
1441 * bug in libltdl on certain systems. The "set
1442 * preloaded symbols" macro below ends up
1443 * referencing this name, but it isn't defined
1444 * anywhere in the libltdl source. As a result,
1445 * any program STUPID enough to rely on libltdl
1446 * fails to link, because the symbol isn't
1449 * It's like libtool and libltdl are some kind
1452 #ifdef IE_LIBTOOL_DIE
1453 #define lt__PROGRAM__LTX_preloaded_symbols lt_libltdl_LTX_preloaded_symbols
1457 * Set the default list of preloaded symbols.
1458 * This is used to initialize libltdl's list of
1459 * preloaded modules.
1461 * i.e. Static modules.
1463 LTDL_SET_PRELOADED_SYMBOLS();
1465 if (lt_dlinit() != 0) {
1466 radlog(L_ERR, "Failed to initialize libraries: %s\n",
1472 * Set the search path to ONLY our library directory.
1473 * This prevents the modules from being found from
1474 * any location on the disk.
1476 lt_dlsetsearchpath(radlib_dir);
1479 * Set up the internal module struct.
1481 module_tree = rbtree_create(module_entry_cmp,
1482 module_entry_free, 0);
1484 radlog(L_ERR, "Failed to initialize modules\n");
1488 instance_tree = rbtree_create(module_instance_cmp,
1489 module_instance_free, 0);
1490 if (!instance_tree) {
1491 radlog(L_ERR, "Failed to initialize modules\n");
1496 memset(virtual_servers, 0, sizeof(virtual_servers));
1499 * Remember where the modules were stored.
1501 modules = cf_section_sub_find(config, "modules");
1503 radlog(L_INFO, "WARNING: Cannot find a \"modules\" section in the configuration file!");
1506 DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1509 * Loop over module definitions, looking for duplicates.
1511 * This is O(N^2) in the number of modules, but most
1512 * systems should have less than 100 modules.
1514 for (ci=cf_item_find_next(modules, NULL);
1517 const char *name1, *name2;
1518 CONF_SECTION *subcs, *duplicate;
1520 next = cf_item_find_next(modules, ci);
1522 if (!cf_item_is_section(ci)) continue;
1524 if (!next || !cf_item_is_section(next)) continue;
1526 subcs = cf_itemtosection(ci);
1527 name1 = cf_section_name1(subcs);
1528 name2 = cf_section_name2(subcs);
1530 duplicate = cf_section_find_name2(cf_itemtosection(next),
1532 if (!duplicate) continue;
1534 if (!name2) name2 = "";
1536 radlog(L_ERR, "Duplicate module \"%s %s\", in file %s:%d and file %s:%d",
1538 cf_section_filename(subcs),
1539 cf_section_lineno(subcs),
1540 cf_section_filename(duplicate),
1541 cf_section_lineno(duplicate));
1546 * Look for the 'instantiate' section, which tells us
1547 * the instantiation order of the modules, and also allows
1548 * us to load modules with no authorize/authenticate/etc.
1551 cs = cf_section_sub_find(config, "instantiate");
1554 module_instance_t *module;
1557 cf_log_info(cs, " instantiate {");
1560 * Loop over the items in the 'instantiate' section.
1562 for (ci=cf_item_find_next(cs, NULL);
1564 ci=cf_item_find_next(cs, ci)) {
1567 * Skip sections and "other" stuff.
1568 * Sections will be handled later, if
1569 * they're referenced at all...
1571 if (!cf_item_is_pair(ci)) {
1575 cp = cf_itemtopair(ci);
1576 name = cf_pair_attr(cp);
1577 module = find_module_instance(modules, name, 1);
1581 } /* loop over items in the subsection */
1583 cf_log_info(cs, " }");
1584 } /* if there's an 'instantiate' section. */
1587 * Loop over the listeners, figuring out which sections
1590 for (listener = mainconfig.listen;
1592 listener = listener->next) {
1596 if (listener->type == RAD_LISTEN_PROXY) continue;
1599 cs = cf_section_sub_find_name2(config,
1600 "server", listener->server);
1601 if (!cs && (listener->server != NULL)) {
1602 listener->print(listener, buffer, sizeof(buffer));
1604 radlog(L_ERR, "No server has been defined for %s", buffer);
1609 if (virtual_servers_load(config) < 0) return -1;
1615 * Call all authorization modules until one returns
1616 * somethings else than RLM_MODULE_OK
1618 int module_authorize(int autz_type, REQUEST *request)
1620 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1624 * Authenticate a user/password with various methods.
1626 int module_authenticate(int auth_type, REQUEST *request)
1628 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1631 #ifdef WITH_ACCOUNTING
1633 * Do pre-accounting for ALL configured sessions
1635 int module_preacct(REQUEST *request)
1637 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1641 * Do accounting for ALL configured sessions
1643 int module_accounting(int acct_type, REQUEST *request)
1645 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1649 #ifdef WITH_SESSION_MGMT
1651 * See if a user is already logged in.
1653 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1655 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1659 if(!request->username)
1662 request->simul_count = 0;
1663 request->simul_max = maxsimul;
1664 request->simul_mpp = 1;
1666 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1668 if (rcode != RLM_MODULE_OK) {
1669 /* FIXME: Good spot for a *rate-limited* warning to the log */
1673 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1679 * Do pre-proxying for ALL configured sessions
1681 int module_pre_proxy(int type, REQUEST *request)
1683 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1687 * Do post-proxying for ALL configured sessions
1689 int module_post_proxy(int type, REQUEST *request)
1691 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1696 * Do post-authentication for ALL configured sessions
1698 int module_post_auth(int postauth_type, REQUEST *request)
1700 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1704 int module_recv_coa(int recv_coa_type, REQUEST *request)
1706 return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
1709 int module_send_coa(int send_coa_type, REQUEST *request)
1711 return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);