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 typedef struct section_type_value_t {
74 } section_type_value_t;
77 * Ordered by component
79 static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
80 { "authenticate", "Auth-Type", PW_AUTH_TYPE },
81 { "authorize", "Autz-Type", PW_AUTZ_TYPE },
82 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
83 { "accounting", "Acct-Type", PW_ACCT_TYPE },
84 { "session", "Session-Type", PW_SESSION_TYPE },
85 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
86 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
87 { "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE }
90 { "recv-coa", "Recv-CoA-Type", PW_RECV_COA_TYPE },
91 { "send-coa", "Send-CoA-Type", PW_SEND_COA_TYPE }
96 #ifdef WITHOUT_LIBLTDL
104 #define RTLD_LOCAL (0)
107 lt_dlhandle lt_dlopenext(const char *name)
111 strlcpy(buffer, name, sizeof(buffer));
114 * FIXME: Make this configurable...
116 strlcat(buffer, ".so", sizeof(buffer));
118 return dlopen(buffer, RTLD_NOW | RTLD_LOCAL);
121 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
123 return dlsym(handle, symbol);
126 int lt_dlclose(lt_dlhandle handle)
128 return dlclose(handle);
131 const char *lt_dlerror(void)
137 #else /* without dlopen */
138 typedef struct lt_dlmodule_t {
143 typedef struct eap_type_t EAP_TYPE;
144 typedef struct rlm_sql_module_t rlm_sql_module_t;
147 * FIXME: Write hackery to auto-generate this data.
148 * We only need to do this on systems that don't have dlopen.
150 extern module_t rlm_pap;
151 extern module_t rlm_chap;
152 extern module_t rlm_eap;
153 extern module_t rlm_sql;
156 extern EAP_TYPE rlm_eap_md5;
157 extern rlm_sql_module_t rlm_sql_mysql;
160 static const lt_dlmodule_t lt_dlmodules[] = {
161 { "rlm_pap", &rlm_pap },
162 { "rlm_chap", &rlm_chap },
163 { "rlm_eap", &rlm_eap },
166 { "rlm_eap_md5", &rlm_eap_md5 },
169 { "rlm_sql_mysql", &rlm_sql_mysql },
175 lt_dlhandle lt_dlopenext(const char *name)
179 for (i = 0; lt_dlmodules[i].name != NULL; i++) {
180 if (strcmp(name, lt_dlmodules[i].name) == 0) {
181 return lt_dlmodules[i].ref;
188 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
193 int lt_dlclose(lt_dlhandle handle)
198 const char *lt_dlerror(void)
200 return "Unspecified error";
203 #endif /* WITH_DLOPEN */
204 #else /* WITHOUT_LIBLTDL */
207 * Solve the issues of libraries linking to other libraries
208 * by using a newer libltdl API.
210 #ifndef HAVE_LT_DLADVISE_INIT
211 #define fr_dlopenext lt_dlopenext
213 static lt_dlhandle fr_dlopenext(const char *filename)
215 lt_dlhandle handle = 0;
218 if (!lt_dladvise_init (&advise) &&
219 !lt_dladvise_ext (&advise) &&
220 !lt_dladvise_global (&advise)) {
221 handle = lt_dlopenadvise (filename, advise);
224 lt_dladvise_destroy (&advise);
228 #endif /* HAVE_LT_DLADVISE_INIT */
229 #endif /* WITHOUT_LIBLTDL */
231 static int virtual_server_idx(const char *name)
237 hash = fr_hash_string(name);
239 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
242 static void virtual_server_free(virtual_server_t *server)
246 if (server->components) rbtree_free(server->components);
247 server->components = NULL;
252 void virtual_servers_free(time_t when)
255 virtual_server_t **last;
257 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
258 virtual_server_t *server, *next;
260 last = &virtual_servers[i];
261 for (server = virtual_servers[i];
267 * If we delete it, fix the links so that
268 * we don't orphan anything. Also,
269 * delete it if it's old, AND a newer one
272 * Otherwise, the last pointer gets set to
273 * the one we didn't delete.
276 ((server->created < when) && server->can_free)) {
277 *last = server->next;
278 virtual_server_free(server);
280 last = &(server->next);
286 static void indexed_modcallable_free(void *data)
288 indexed_modcallable *c = data;
290 modcallable_free(&c->modulelist);
294 static int indexed_modcallable_cmp(const void *one, const void *two)
296 const indexed_modcallable *a = one;
297 const indexed_modcallable *b = two;
299 if (a->comp < b->comp) return -1;
300 if (a->comp > b->comp) return +1;
302 return a->idx - b->idx;
307 * Compare two module entries
309 static int module_instance_cmp(const void *one, const void *two)
311 const module_instance_t *a = one;
312 const module_instance_t *b = two;
314 return strcmp(a->name, b->name);
318 static void module_instance_free_old(CONF_SECTION *cs, module_instance_t *node,
321 fr_module_hup_t *mh, **last;
324 * Walk the list, freeing up old instances.
331 * Free only every 60 seconds.
333 if ((when - mh->when) < 60) {
338 cf_section_parse_free(cs, mh->insthandle);
340 if (node->entry->module->detach) {
341 (node->entry->module->detach)(mh->insthandle);
343 free(mh->insthandle);
353 * Free a module instance.
355 static void module_instance_free(void *data)
357 module_instance_t *this = data;
359 module_instance_free_old(this->cs, this, time(NULL) + 100);
361 if (this->entry->module->detach) {
362 (this->entry->module->detach)(this->insthandle);
365 #ifdef HAVE_PTHREAD_H
369 * The mutex MIGHT be locked...
370 * we'll check for that later, I guess.
372 pthread_mutex_destroy(this->mutex);
376 memset(this, 0, sizeof(*this));
382 * Compare two module entries
384 static int module_entry_cmp(const void *one, const void *two)
386 const module_entry_t *a = one;
387 const module_entry_t *b = two;
389 return strcmp(a->name, b->name);
393 * Free a module entry.
395 static void module_entry_free(void *data)
397 module_entry_t *this = data;
399 lt_dlclose(this->handle); /* ignore any errors */
400 memset(this, 0, sizeof(*this));
406 * Remove the module lists.
408 int detach_modules(void)
410 rbtree_free(instance_tree);
411 rbtree_free(module_tree);
420 * Find a module on disk or in memory, and link to it.
422 static module_entry_t *linkto_module(const char *module_name,
425 module_entry_t myentry;
426 module_entry_t *node;
428 char module_struct[256];
430 const module_t *module;
432 strlcpy(myentry.name, module_name, sizeof(myentry.name));
433 node = rbtree_finddata(module_tree, &myentry);
434 if (node) return node;
437 * Keep the handle around so we can dlclose() it.
439 handle = fr_dlopenext(module_name);
440 if (handle == NULL) {
441 cf_log_err(cf_sectiontoitem(cs),
442 "Failed to link to module '%s': %s\n",
443 module_name, lt_dlerror());
448 * Link to the module's rlm_FOO{} module structure.
450 * The module_name variable has the version number
451 * embedded in it, and we don't want that here.
453 strcpy(module_struct, module_name);
454 p = strrchr(module_struct, '-');
457 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
460 * libltld MAY core here, if the handle it gives us contains
463 module = lt_dlsym(handle, module_struct);
465 cf_log_err(cf_sectiontoitem(cs),
466 "Failed linking to %s structure: %s\n",
467 module_name, lt_dlerror());
472 * Before doing anything else, check if it's sane.
474 if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
476 cf_log_err(cf_sectiontoitem(cs),
477 "Invalid version in module '%s'",
483 /* make room for the module type */
484 node = rad_malloc(sizeof(*node));
485 memset(node, 0, sizeof(*node));
486 strlcpy(node->name, module_name, sizeof(node->name));
487 node->module = module;
488 node->handle = handle;
490 cf_log_module(cs, "Linked to module %s", module_name);
493 * Add the module as "rlm_foo-version" to the configuration
496 if (!rbtree_insert(module_tree, node)) {
497 radlog(L_ERR, "Failed to cache module %s", module_name);
507 * Find a module instance.
509 module_instance_t *find_module_instance(CONF_SECTION *modules,
510 const char *instname, int do_link)
512 int check_config_safe = FALSE;
515 module_instance_t *node, myNode;
516 char module_name[256];
518 if (!modules) return NULL;
521 * Module instances are declared in the modules{} block
522 * and referenced later by their name, which is the
523 * name2 from the config section, or name1 if there was
526 cs = cf_section_sub_find_name2(modules, NULL, instname);
528 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
533 * If there's already a module instance, return it.
535 strlcpy(myNode.name, instname, sizeof(myNode.name));
536 node = rbtree_finddata(instance_tree, &myNode);
537 if (node) return node;
539 if (!do_link) return NULL;
541 name1 = cf_section_name1(cs);
544 * Found the configuration entry.
546 node = rad_malloc(sizeof(*node));
547 memset(node, 0, sizeof(*node));
549 node->insthandle = NULL;
553 * Names in the "modules" section aren't prefixed
554 * with "rlm_", so we add it here.
556 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
558 node->entry = linkto_module(module_name, cs);
561 /* linkto_module logs any errors */
565 if (check_config && (node->entry->module->instantiate) &&
566 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
567 const char *value = NULL;
570 cp = cf_pair_find(cs, "force_check_config");
571 if (cp) value = cf_pair_value(cp);
573 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
575 cf_log_module(cs, "Skipping instantiation of %s", instname);
578 check_config_safe = TRUE;
579 cf_log_module(cs, "Instantiating %s", instname);
583 * Call the module's instantiation routine.
585 if ((node->entry->module->instantiate) &&
586 (!check_config || check_config_safe) &&
587 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
588 cf_log_err(cf_sectiontoitem(cs),
589 "Instantiation failed for module \"%s\"",
596 * We're done. Fill in the rest of the data structure,
597 * and link it to the module instance list.
599 strlcpy(node->name, instname, sizeof(node->name));
601 #ifdef HAVE_PTHREAD_H
603 * If we're threaded, check if the module is thread-safe.
605 * If it isn't, we create a mutex.
607 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
608 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
610 * Initialize the mutex.
612 pthread_mutex_init(node->mutex, NULL);
615 * The module is thread-safe. Don't give it a mutex.
621 rbtree_insert(instance_tree, node);
626 static indexed_modcallable *lookup_by_index(rbtree_t *components,
629 indexed_modcallable myc;
634 return rbtree_finddata(components, &myc);
638 * Create a new sublist.
640 static indexed_modcallable *new_sublist(rbtree_t *components, int comp, int idx)
642 indexed_modcallable *c;
644 c = lookup_by_index(components, comp, idx);
646 /* It is an error to try to create a sublist that already
647 * exists. It would almost certainly be caused by accidental
648 * duplication in the config file.
650 * index 0 is the exception, because it is used when we want
651 * to collect _all_ listed modules under a single index by
652 * default, which is currently the case in all components
653 * except authenticate. */
661 c = rad_malloc(sizeof(*c));
662 c->modulelist = NULL;
666 if (!rbtree_insert(components, c)) {
674 int indexed_modcall(int comp, int idx, REQUEST *request)
677 modcallable *list = NULL;
678 virtual_server_t *server;
681 * Hack to find the correct virtual server.
683 rcode = virtual_server_idx(request->server);
684 for (server = virtual_servers[rcode];
686 server = server->next) {
687 if (!request->server && !server->name) break;
689 if ((request->server && server->name) &&
690 (strcmp(request->server, server->name) == 0)) break;
694 RDEBUG("No such virtual server \"%s\"", request->server);
695 return RLM_MODULE_FAIL;
699 list = server->mc[comp];
700 if (!list) RDEBUG2(" WARNING: Empty section. Using default return values.");
703 indexed_modcallable *this;
705 this = lookup_by_index(server->components, comp, idx);
707 list = this->modulelist;
709 RDEBUG2(" WARNING: Unknown value specified for %s. Cannot perform requested action.",
710 section_type_value[comp].typename);
714 request->component = section_type_value[comp].section;
716 rcode = modcall(comp, list, request);
718 request->module = "";
719 request->component = "";
724 * Load a sub-module list, as found inside an Auth-Type foo {}
727 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
728 rbtree_t *components, int attr, int comp)
730 indexed_modcallable *subcomp;
733 const char *name2 = cf_section_name2(cs);
735 rad_assert(comp >= RLM_COMPONENT_AUTH);
736 rad_assert(comp < RLM_COMPONENT_COUNT);
742 cf_log_err(cf_sectiontoitem(cs),
743 "No name specified for %s block",
744 section_type_value[comp].typename);
751 ml = compile_modgroup(parent, comp, cs);
757 * We must assign a numeric index to this subcomponent.
758 * It is generated and placed in the dictionary
759 * automatically. If it isn't found, it's a serious
762 dval = dict_valbyname(attr, name2);
764 cf_log_err(cf_sectiontoitem(cs),
765 "%s %s Not previously configured",
766 section_type_value[comp].typename, name2);
767 modcallable_free(&ml);
771 subcomp = new_sublist(components, comp, dval->value);
773 modcallable_free(&ml);
777 subcomp->modulelist = ml;
781 static int define_type(const DICT_ATTR *dattr, const char *name)
787 * If the value already exists, don't
790 dval = dict_valbyname(dattr->attr, name);
794 * Create a new unique value with a
795 * meaningless number. You can't look at
796 * it from outside of this code, so it
797 * doesn't matter. The only requirement
798 * is that it's unique.
801 value = fr_rand() & 0x00ffffff;
802 } while (dict_valbyattr(dattr->attr, value));
804 if (dict_addvalue(name, dattr->name, value) < 0) {
805 radlog(L_ERR, "%s", fr_strerror());
812 static int load_component_section(CONF_SECTION *cs,
813 rbtree_t *components, int comp)
818 indexed_modcallable *subcomp;
820 const char *visiblename;
821 const DICT_ATTR *dattr;
824 * Find the attribute used to store VALUEs for this section.
826 dattr = dict_attrbyvalue(section_type_value[comp].attr);
828 cf_log_err(cf_sectiontoitem(cs),
829 "No such attribute %s",
830 section_type_value[comp].typename);
835 * Loop over the entries in the named section, loading
836 * the sections this time.
838 for (modref = cf_item_find_next(cs, NULL);
840 modref = cf_item_find_next(cs, modref)) {
842 CONF_PAIR *cp = NULL;
843 CONF_SECTION *scs = NULL;
845 if (cf_item_is_section(modref)) {
846 scs = cf_itemtosection(modref);
848 name1 = cf_section_name1(scs);
851 section_type_value[comp].typename) == 0) {
852 if (!load_subcomponent_section(NULL, scs,
856 return -1; /* FIXME: memleak? */
863 } else if (cf_item_is_pair(modref)) {
864 cp = cf_itemtopair(modref);
867 continue; /* ignore it */
871 * Try to compile one entry.
873 this = compile_modsingle(NULL, comp, modref, &modname);
875 cf_log_err(cf_sectiontoitem(cs),
876 "Errors parsing %s section.\n",
877 cf_section_name1(cs));
882 * Look for Auth-Type foo {}, which are special
883 * cases of named sections, and allowable ONLY
886 * i.e. They're not allowed in a "group" or "redundant"
889 if (comp == RLM_COMPONENT_AUTH) {
891 const char *modrefname = NULL;
893 modrefname = cf_pair_attr(cp);
895 modrefname = cf_section_name2(scs);
897 modcallable_free(&this);
898 cf_log_err(cf_sectiontoitem(cs),
899 "Errors parsing %s sub-section.\n",
900 cf_section_name1(scs));
905 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
908 * It's a section, but nothing we
911 modcallable_free(&this);
912 cf_log_err(cf_sectiontoitem(cs),
913 "Unknown Auth-Type \"%s\" in %s sub-section.",
914 modrefname, section_type_value[comp].section);
919 /* See the comment in new_sublist() for explanation
920 * of the special index 0 */
924 subcomp = new_sublist(components, comp, idx);
925 if (subcomp == NULL) {
926 modcallable_free(&this);
930 /* If subcomp->modulelist is NULL, add_to_modcallable will
932 visiblename = cf_section_name2(cs);
933 if (visiblename == NULL)
934 visiblename = cf_section_name1(cs);
935 add_to_modcallable(&subcomp->modulelist, this,
942 static int load_byserver(CONF_SECTION *cs)
945 const char *name = cf_section_name2(cs);
946 rbtree_t *components;
947 virtual_server_t *server = NULL;
948 indexed_modcallable *c;
951 cf_log_info(cs, "server %s {", name);
953 cf_log_info(cs, "server {");
956 cf_log_info(cs, " modules {");
958 components = rbtree_create(indexed_modcallable_cmp,
959 indexed_modcallable_free, 0);
961 radlog(L_ERR, "Failed to initialize components\n");
965 server = rad_malloc(sizeof(*server));
966 memset(server, 0, sizeof(*server));
969 server->created = time(NULL);
971 server->components = components;
974 * Define types first.
976 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
981 subcs = cf_section_sub_find(cs,
982 section_type_value[comp].section);
983 if (!subcs) continue;
985 if (cf_item_find_next(subcs, NULL) == NULL) continue;
988 * Find the attribute used to store VALUEs for this section.
990 dattr = dict_attrbyvalue(section_type_value[comp].attr);
992 cf_log_err(cf_sectiontoitem(subcs),
993 "No such attribute %s",
994 section_type_value[comp].typename);
996 if (debug_flag == 0) {
997 radlog(L_ERR, "Failed to load virtual server %s",
998 (name != NULL) ? name : "<default>");
1000 virtual_server_free(server);
1005 * Define dynamic types, so that others can reference
1008 for (modref = cf_item_find_next(subcs, NULL);
1010 modref = cf_item_find_next(subcs, modref)) {
1012 CONF_SECTION *subsubcs;
1015 * Create types for simple references
1016 * only when parsing the authenticate
1019 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
1020 cf_item_is_pair(modref)) {
1021 CONF_PAIR *cp = cf_itemtopair(modref);
1022 if (!define_type(dattr, cf_pair_attr(cp))) {
1029 if (!cf_item_is_section(modref)) continue;
1031 subsubcs = cf_itemtosection(modref);
1032 name1 = cf_section_name1(subsubcs);
1034 if (strcmp(name1, section_type_value[comp].typename) == 0) {
1035 if (!define_type(dattr,
1036 cf_section_name2(subsubcs))) {
1041 } /* loop over components */
1044 * Loop over all of the known components, finding their
1045 * configuration section, and loading it.
1048 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1049 CONF_SECTION *subcs;
1051 subcs = cf_section_sub_find(cs,
1052 section_type_value[comp].section);
1053 if (!subcs) continue;
1055 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1057 cf_log_module(cs, "Checking %s {...} for more modules to load",
1058 section_type_value[comp].section);
1062 * Skip pre/post-proxy sections if we're not
1065 if (!mainconfig.proxy_requests &&
1066 ((comp == PW_PRE_PROXY_TYPE) ||
1067 (comp == PW_PRE_PROXY_TYPE))) {
1072 if (load_component_section(subcs, components, comp) < 0) {
1077 * Cache a default, if it exists. Some people
1078 * put empty sections for some reason...
1080 c = lookup_by_index(components, comp, 0);
1081 if (c) server->mc[comp] = c->modulelist;
1083 server->subcs[comp] = subcs;
1086 } /* loop over components */
1089 * We haven't loaded any of the normal sections. Maybe we're
1090 * supposed to load the vmps section.
1092 * This is a bit of a hack...
1095 CONF_SECTION *subcs;
1097 subcs = cf_section_sub_find(cs, "vmps");
1099 cf_log_module(cs, "Checking vmps {...} for more modules to load");
1100 if (load_component_section(subcs, components,
1101 RLM_COMPONENT_POST_AUTH) < 0) {
1104 c = lookup_by_index(components,
1105 RLM_COMPONENT_POST_AUTH, 0);
1106 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1112 const DICT_ATTR *dattr;
1114 dattr = dict_attrbyname("DHCP-Message-Type");
1117 * Handle each DHCP Message type separately.
1119 if (dattr) for (subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1121 subcs = cf_subsection_find_next(cs, subcs,
1123 const char *name2 = cf_section_name2(subcs);
1125 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
1126 if (!load_subcomponent_section(NULL, subcs,
1129 RLM_COMPONENT_POST_AUTH)) {
1130 goto error; /* FIXME: memleak? */
1132 c = lookup_by_index(components,
1133 RLM_COMPONENT_POST_AUTH, 0);
1134 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1141 cf_log_info(cs, " } # modules");
1142 cf_log_info(cs, "} # server");
1144 if (!flag && name) {
1145 DEBUG("WARNING: Server %s is empty, and will do nothing!",
1149 if (debug_flag == 0) {
1150 radlog(L_INFO, "Loaded virtual server %s",
1151 (name != NULL) ? name : "<default>");
1155 * Now that it is OK, insert it into the list.
1157 * This is thread-safe...
1159 comp = virtual_server_idx(name);
1160 server->next = virtual_servers[comp];
1161 virtual_servers[comp] = server;
1164 * Mark OLDER ones of the same name as being unused.
1166 server = server->next;
1168 if ((!name && !server->name) ||
1169 (name && server->name &&
1170 (strcmp(server->name, name) == 0))) {
1171 server->can_free = TRUE;
1174 server = server->next;
1182 * Load all of the virtual servers.
1184 int virtual_servers_load(CONF_SECTION *config)
1186 int null_server = FALSE;
1188 static int first_time = TRUE;
1190 DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1193 * Load all of the virtual servers.
1195 for (cs = cf_subsection_find_next(config, NULL, "server");
1197 cs = cf_subsection_find_next(config, cs, "server")) {
1198 if (!cf_section_name2(cs)) null_server = TRUE;
1200 if (load_byserver(cs) < 0) {
1202 * Once we successfully staryed once,
1203 * continue loading the OTHER servers,
1204 * even if one fails.
1206 if (!first_time) continue;
1212 * No empty server defined. Try to load an old-style
1213 * one for backwards compatibility.
1216 if (load_byserver(config) < 0) {
1222 * If we succeed the first time around, remember that.
1229 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1231 void *insthandle = NULL;
1232 fr_module_hup_t *mh;
1235 !node->entry->module->instantiate ||
1236 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1240 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1242 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
1243 cf_log_err(cf_sectiontoitem(cs),
1244 "HUP failed for module \"%s\". Using old configuration.",
1249 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
1251 module_instance_free_old(cs, node, when);
1254 * Save the old instance handle for later deletion.
1256 mh = rad_malloc(sizeof(*mh));
1259 mh->insthandle = node->insthandle;
1260 mh->next = node->mh;
1263 node->insthandle = insthandle;
1266 * FIXME: Set a timeout to come back in 60s, so that
1267 * we can pro-actively clean up the old instances.
1274 int module_hup(CONF_SECTION *modules)
1279 module_instance_t *node;
1281 if (!modules) return 0;
1286 * Loop over the modules
1288 for (ci=cf_item_find_next(modules, NULL);
1290 ci=cf_item_find_next(modules, ci)) {
1291 const char *instname;
1292 module_instance_t myNode;
1295 * If it's not a section, ignore it.
1297 if (!cf_item_is_section(ci)) continue;
1299 cs = cf_itemtosection(ci);
1300 instname = cf_section_name2(cs);
1301 if (!instname) instname = cf_section_name1(cs);
1303 strlcpy(myNode.name, instname, sizeof(myNode.name));
1304 node = rbtree_finddata(instance_tree, &myNode);
1306 module_hup_module(cs, node, when);
1314 * Parse the module config sections, and load
1315 * and call each module's init() function.
1317 * Libtool makes your life a LOT easier, especially with libltdl.
1318 * see: http://www.gnu.org/software/libtool/
1320 int setup_modules(int reload, CONF_SECTION *config)
1322 CONF_SECTION *cs, *modules;
1323 rad_listen_t *listener;
1325 if (reload) return 0;
1328 * If necessary, initialize libltdl.
1332 * This line works around a completely
1334 * RIDICULOUS INSANE IDIOTIC
1336 * bug in libltdl on certain systems. The "set
1337 * preloaded symbols" macro below ends up
1338 * referencing this name, but it isn't defined
1339 * anywhere in the libltdl source. As a result,
1340 * any program STUPID enough to rely on libltdl
1341 * fails to link, because the symbol isn't
1344 * It's like libtool and libltdl are some kind
1347 #ifdef IE_LIBTOOL_DIE
1348 #define lt__PROGRAM__LTX_preloaded_symbols lt_libltdl_LTX_preloaded_symbols
1352 * Set the default list of preloaded symbols.
1353 * This is used to initialize libltdl's list of
1354 * preloaded modules.
1356 * i.e. Static modules.
1358 LTDL_SET_PRELOADED_SYMBOLS();
1360 if (lt_dlinit() != 0) {
1361 radlog(L_ERR, "Failed to initialize libraries: %s\n",
1367 * Set the search path to ONLY our library directory.
1368 * This prevents the modules from being found from
1369 * any location on the disk.
1371 lt_dlsetsearchpath(radlib_dir);
1374 * Set up the internal module struct.
1376 module_tree = rbtree_create(module_entry_cmp,
1377 module_entry_free, 0);
1379 radlog(L_ERR, "Failed to initialize modules\n");
1383 instance_tree = rbtree_create(module_instance_cmp,
1384 module_instance_free, 0);
1385 if (!instance_tree) {
1386 radlog(L_ERR, "Failed to initialize modules\n");
1391 memset(virtual_servers, 0, sizeof(virtual_servers));
1394 * Remember where the modules were stored.
1396 modules = cf_section_sub_find(config, "modules");
1398 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
1402 DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1405 * Look for the 'instantiate' section, which tells us
1406 * the instantiation order of the modules, and also allows
1407 * us to load modules with no authorize/authenticate/etc.
1410 cs = cf_section_sub_find(config, "instantiate");
1414 module_instance_t *module;
1417 cf_log_info(cs, " instantiate {");
1420 * Loop over the items in the 'instantiate' section.
1422 for (ci=cf_item_find_next(cs, NULL);
1424 ci=cf_item_find_next(cs, ci)) {
1427 * Skip sections and "other" stuff.
1428 * Sections will be handled later, if
1429 * they're referenced at all...
1431 if (!cf_item_is_pair(ci)) {
1435 cp = cf_itemtopair(ci);
1436 name = cf_pair_attr(cp);
1437 module = find_module_instance(modules, name, 1);
1441 } /* loop over items in the subsection */
1443 cf_log_info(cs, " }");
1444 } /* if there's an 'instantiate' section. */
1447 * Loop over the listeners, figuring out which sections
1450 for (listener = mainconfig.listen;
1452 listener = listener->next) {
1456 if (listener->type == RAD_LISTEN_PROXY) continue;
1459 cs = cf_section_sub_find_name2(config,
1460 "server", listener->server);
1461 if (!cs && (listener->server != NULL)) {
1462 listener->print(listener, buffer, sizeof(buffer));
1464 radlog(L_ERR, "No server has been defined for %s", buffer);
1469 if (virtual_servers_load(config) < 0) return -1;
1475 * Call all authorization modules until one returns
1476 * somethings else than RLM_MODULE_OK
1478 int module_authorize(int autz_type, REQUEST *request)
1480 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1484 * Authenticate a user/password with various methods.
1486 int module_authenticate(int auth_type, REQUEST *request)
1488 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1491 #ifdef WITH_ACCOUNTING
1493 * Do pre-accounting for ALL configured sessions
1495 int module_preacct(REQUEST *request)
1497 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1501 * Do accounting for ALL configured sessions
1503 int module_accounting(int acct_type, REQUEST *request)
1505 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1509 #ifdef WITH_SESSION_MGMT
1511 * See if a user is already logged in.
1513 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1515 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1519 if(!request->username)
1522 request->simul_count = 0;
1523 request->simul_max = maxsimul;
1524 request->simul_mpp = 1;
1526 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1528 if (rcode != RLM_MODULE_OK) {
1529 /* FIXME: Good spot for a *rate-limited* warning to the log */
1533 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1539 * Do pre-proxying for ALL configured sessions
1541 int module_pre_proxy(int type, REQUEST *request)
1543 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1547 * Do post-proxying for ALL configured sessions
1549 int module_post_proxy(int type, REQUEST *request)
1551 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1556 * Do post-authentication for ALL configured sessions
1558 int module_post_auth(int postauth_type, REQUEST *request)
1560 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1564 int module_recv_coa(int recv_coa_type, REQUEST *request)
1566 return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
1569 int module_send_coa(int send_coa_type, REQUEST *request)
1571 return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);