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 #endif /* WITHOUT_LIBLTDL */
206 static int virtual_server_idx(const char *name)
212 hash = fr_hash_string(name);
214 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
217 static void virtual_server_free(virtual_server_t *server)
221 if (server->components) rbtree_free(server->components);
222 server->components = NULL;
227 void virtual_servers_free(time_t when)
230 virtual_server_t **last;
232 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
233 virtual_server_t *server, *next;
235 last = &virtual_servers[i];
236 for (server = virtual_servers[i];
242 * If we delete it, fix the links so that
243 * we don't orphan anything. Also,
244 * delete it if it's old, AND a newer one
247 * Otherwise, the last pointer gets set to
248 * the one we didn't delete.
251 ((server->created < when) && server->can_free)) {
252 *last = server->next;
253 virtual_server_free(server);
255 last = &(server->next);
261 static void indexed_modcallable_free(void *data)
263 indexed_modcallable *c = data;
265 modcallable_free(&c->modulelist);
269 static int indexed_modcallable_cmp(const void *one, const void *two)
271 const indexed_modcallable *a = one;
272 const indexed_modcallable *b = two;
274 if (a->comp < b->comp) return -1;
275 if (a->comp > b->comp) return +1;
277 return a->idx - b->idx;
282 * Compare two module entries
284 static int module_instance_cmp(const void *one, const void *two)
286 const module_instance_t *a = one;
287 const module_instance_t *b = two;
289 return strcmp(a->name, b->name);
293 static void module_instance_free_old(CONF_SECTION *cs, module_instance_t *node,
296 fr_module_hup_t *mh, **last;
299 * Walk the list, freeing up old instances.
306 * Free only every 60 seconds.
308 if ((when - mh->when) < 60) {
313 cf_section_parse_free(cs, mh->insthandle);
315 if (node->entry->module->detach) {
316 (node->entry->module->detach)(mh->insthandle);
318 free(mh->insthandle);
328 * Free a module instance.
330 static void module_instance_free(void *data)
332 module_instance_t *this = data;
334 module_instance_free_old(this->cs, this, time(NULL) + 100);
336 if (this->entry->module->detach) {
337 (this->entry->module->detach)(this->insthandle);
340 #ifdef HAVE_PTHREAD_H
344 * The mutex MIGHT be locked...
345 * we'll check for that later, I guess.
347 pthread_mutex_destroy(this->mutex);
351 memset(this, 0, sizeof(*this));
357 * Compare two module entries
359 static int module_entry_cmp(const void *one, const void *two)
361 const module_entry_t *a = one;
362 const module_entry_t *b = two;
364 return strcmp(a->name, b->name);
368 * Free a module entry.
370 static void module_entry_free(void *data)
372 module_entry_t *this = data;
374 lt_dlclose(this->handle); /* ignore any errors */
375 memset(this, 0, sizeof(*this));
381 * Remove the module lists.
383 int detach_modules(void)
385 rbtree_free(instance_tree);
386 rbtree_free(module_tree);
395 * Find a module on disk or in memory, and link to it.
397 static module_entry_t *linkto_module(const char *module_name,
400 module_entry_t myentry;
401 module_entry_t *node;
403 char module_struct[256];
405 const module_t *module;
407 strlcpy(myentry.name, module_name, sizeof(myentry.name));
408 node = rbtree_finddata(module_tree, &myentry);
409 if (node) return node;
412 * Keep the handle around so we can dlclose() it.
414 handle = lt_dlopenext(module_name);
415 if (handle == NULL) {
416 cf_log_err(cf_sectiontoitem(cs),
417 "Failed to link to module '%s': %s\n",
418 module_name, lt_dlerror());
423 * Link to the module's rlm_FOO{} module structure.
425 * The module_name variable has the version number
426 * embedded in it, and we don't want that here.
428 strcpy(module_struct, module_name);
429 p = strrchr(module_struct, '-');
432 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
435 * libltld MAY core here, if the handle it gives us contains
438 module = lt_dlsym(handle, module_struct);
440 cf_log_err(cf_sectiontoitem(cs),
441 "Failed linking to %s structure: %s\n",
442 module_name, lt_dlerror());
447 * Before doing anything else, check if it's sane.
449 if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
451 cf_log_err(cf_sectiontoitem(cs),
452 "Invalid version in module '%s'",
458 /* make room for the module type */
459 node = rad_malloc(sizeof(*node));
460 memset(node, 0, sizeof(*node));
461 strlcpy(node->name, module_name, sizeof(node->name));
462 node->module = module;
463 node->handle = handle;
465 cf_log_module(cs, "Linked to module %s", module_name);
468 * Add the module as "rlm_foo-version" to the configuration
471 if (!rbtree_insert(module_tree, node)) {
472 radlog(L_ERR, "Failed to cache module %s", module_name);
482 * Find a module instance.
484 module_instance_t *find_module_instance(CONF_SECTION *modules,
485 const char *instname, int do_link)
487 int check_config_safe = FALSE;
490 module_instance_t *node, myNode;
491 char module_name[256];
493 if (!modules) return NULL;
496 * Module instances are declared in the modules{} block
497 * and referenced later by their name, which is the
498 * name2 from the config section, or name1 if there was
501 cs = cf_section_sub_find_name2(modules, NULL, instname);
503 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
508 * If there's already a module instance, return it.
510 strlcpy(myNode.name, instname, sizeof(myNode.name));
511 node = rbtree_finddata(instance_tree, &myNode);
512 if (node) return node;
514 if (!do_link) return NULL;
516 name1 = cf_section_name1(cs);
519 * Found the configuration entry.
521 node = rad_malloc(sizeof(*node));
522 memset(node, 0, sizeof(*node));
524 node->insthandle = NULL;
528 * Names in the "modules" section aren't prefixed
529 * with "rlm_", so we add it here.
531 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
533 node->entry = linkto_module(module_name, cs);
536 /* linkto_module logs any errors */
540 if (check_config && (node->entry->module->instantiate) &&
541 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
542 const char *value = NULL;
545 cp = cf_pair_find(cs, "force_check_config");
546 if (cp) value = cf_pair_value(cp);
548 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
550 cf_log_module(cs, "Skipping instantiation of %s", instname);
553 check_config_safe = TRUE;
554 cf_log_module(cs, "Instantiating %s", instname);
558 * Call the module's instantiation routine.
560 if ((node->entry->module->instantiate) &&
561 (!check_config || check_config_safe) &&
562 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
563 cf_log_err(cf_sectiontoitem(cs),
564 "Instantiation failed for module \"%s\"",
571 * We're done. Fill in the rest of the data structure,
572 * and link it to the module instance list.
574 strlcpy(node->name, instname, sizeof(node->name));
576 #ifdef HAVE_PTHREAD_H
578 * If we're threaded, check if the module is thread-safe.
580 * If it isn't, we create a mutex.
582 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
583 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
585 * Initialize the mutex.
587 pthread_mutex_init(node->mutex, NULL);
590 * The module is thread-safe. Don't give it a mutex.
596 rbtree_insert(instance_tree, node);
601 static indexed_modcallable *lookup_by_index(rbtree_t *components,
604 indexed_modcallable myc;
609 return rbtree_finddata(components, &myc);
613 * Create a new sublist.
615 static indexed_modcallable *new_sublist(rbtree_t *components, int comp, int idx)
617 indexed_modcallable *c;
619 c = lookup_by_index(components, comp, idx);
621 /* It is an error to try to create a sublist that already
622 * exists. It would almost certainly be caused by accidental
623 * duplication in the config file.
625 * index 0 is the exception, because it is used when we want
626 * to collect _all_ listed modules under a single index by
627 * default, which is currently the case in all components
628 * except authenticate. */
636 c = rad_malloc(sizeof(*c));
637 c->modulelist = NULL;
641 if (!rbtree_insert(components, c)) {
649 int indexed_modcall(int comp, int idx, REQUEST *request)
652 modcallable *list = NULL;
653 virtual_server_t *server;
656 * Hack to find the correct virtual server.
658 rcode = virtual_server_idx(request->server);
659 for (server = virtual_servers[rcode];
661 server = server->next) {
662 if (!request->server && !server->name) break;
664 if ((request->server && server->name) &&
665 (strcmp(request->server, server->name) == 0)) break;
669 RDEBUG("No such virtual server %s", request->server);
670 return RLM_MODULE_FAIL;
674 list = server->mc[comp];
675 if (!list) RDEBUG2(" WARNING: Empty section. Using default return values.");
678 indexed_modcallable *this;
680 this = lookup_by_index(server->components, comp, idx);
682 list = this->modulelist;
684 RDEBUG2(" WARNING: Unknown value specified for %s. Cannot perform requested action.",
685 section_type_value[comp].typename);
689 request->component = section_type_value[comp].section;
691 rcode = modcall(comp, list, request);
693 request->module = "";
694 request->component = "";
699 * Load a sub-module list, as found inside an Auth-Type foo {}
702 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
703 rbtree_t *components, int attr, int comp)
705 indexed_modcallable *subcomp;
708 const char *name2 = cf_section_name2(cs);
710 rad_assert(comp >= RLM_COMPONENT_AUTH);
711 rad_assert(comp < RLM_COMPONENT_COUNT);
717 cf_log_err(cf_sectiontoitem(cs),
718 "No name specified for %s block",
719 section_type_value[comp].typename);
726 ml = compile_modgroup(parent, comp, cs);
732 * We must assign a numeric index to this subcomponent.
733 * It is generated and placed in the dictionary
734 * automatically. If it isn't found, it's a serious
737 dval = dict_valbyname(attr, name2);
739 cf_log_err(cf_sectiontoitem(cs),
740 "%s %s Not previously configured",
741 section_type_value[comp].typename, name2);
742 modcallable_free(&ml);
746 subcomp = new_sublist(components, comp, dval->value);
748 modcallable_free(&ml);
752 subcomp->modulelist = ml;
756 static int define_type(const DICT_ATTR *dattr, const char *name)
762 * If the value already exists, don't
765 dval = dict_valbyname(dattr->attr, name);
769 * Create a new unique value with a
770 * meaningless number. You can't look at
771 * it from outside of this code, so it
772 * doesn't matter. The only requirement
773 * is that it's unique.
776 value = fr_rand() & 0x00ffffff;
777 } while (dict_valbyattr(dattr->attr, value));
779 if (dict_addvalue(name, dattr->name, value) < 0) {
780 radlog(L_ERR, "%s", fr_strerror());
787 static int load_component_section(CONF_SECTION *cs,
788 rbtree_t *components, int comp)
793 indexed_modcallable *subcomp;
795 const char *visiblename;
796 const DICT_ATTR *dattr;
799 * Find the attribute used to store VALUEs for this section.
801 dattr = dict_attrbyvalue(section_type_value[comp].attr);
803 cf_log_err(cf_sectiontoitem(cs),
804 "No such attribute %s",
805 section_type_value[comp].typename);
810 * Loop over the entries in the named section, loading
811 * the sections this time.
813 for (modref = cf_item_find_next(cs, NULL);
815 modref = cf_item_find_next(cs, modref)) {
817 CONF_PAIR *cp = NULL;
818 CONF_SECTION *scs = NULL;
820 if (cf_item_is_section(modref)) {
821 scs = cf_itemtosection(modref);
823 name1 = cf_section_name1(scs);
826 section_type_value[comp].typename) == 0) {
827 if (!load_subcomponent_section(NULL, scs,
831 return -1; /* FIXME: memleak? */
838 } else if (cf_item_is_pair(modref)) {
839 cp = cf_itemtopair(modref);
842 continue; /* ignore it */
846 * Try to compile one entry.
848 this = compile_modsingle(NULL, comp, modref, &modname);
850 cf_log_err(cf_sectiontoitem(cs),
851 "Errors parsing %s section.\n",
852 cf_section_name1(cs));
857 * Look for Auth-Type foo {}, which are special
858 * cases of named sections, and allowable ONLY
861 * i.e. They're not allowed in a "group" or "redundant"
864 if (comp == RLM_COMPONENT_AUTH) {
866 const char *modrefname = NULL;
868 modrefname = cf_pair_attr(cp);
870 modrefname = cf_section_name2(scs);
872 modcallable_free(&this);
873 cf_log_err(cf_sectiontoitem(cs),
874 "Errors parsing %s sub-section.\n",
875 cf_section_name1(scs));
880 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
883 * It's a section, but nothing we
886 modcallable_free(&this);
887 cf_log_err(cf_sectiontoitem(cs),
888 "Unknown Auth-Type \"%s\" in %s sub-section.",
889 modrefname, section_type_value[comp].section);
894 /* See the comment in new_sublist() for explanation
895 * of the special index 0 */
899 subcomp = new_sublist(components, comp, idx);
900 if (subcomp == NULL) {
901 modcallable_free(&this);
905 /* If subcomp->modulelist is NULL, add_to_modcallable will
907 visiblename = cf_section_name2(cs);
908 if (visiblename == NULL)
909 visiblename = cf_section_name1(cs);
910 add_to_modcallable(&subcomp->modulelist, this,
917 static int load_byserver(CONF_SECTION *cs)
920 const char *name = cf_section_name2(cs);
921 rbtree_t *components;
922 virtual_server_t *server = NULL;
923 indexed_modcallable *c;
926 cf_log_info(cs, "server %s {", name);
928 cf_log_info(cs, "server {");
931 cf_log_info(cs, " modules {");
933 components = rbtree_create(indexed_modcallable_cmp,
934 indexed_modcallable_free, 0);
936 radlog(L_ERR, "Failed to initialize components\n");
940 server = rad_malloc(sizeof(*server));
941 memset(server, 0, sizeof(*server));
944 server->created = time(NULL);
946 server->components = components;
949 * Define types first.
951 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
956 subcs = cf_section_sub_find(cs,
957 section_type_value[comp].section);
958 if (!subcs) continue;
960 if (cf_item_find_next(subcs, NULL) == NULL) continue;
963 * Find the attribute used to store VALUEs for this section.
965 dattr = dict_attrbyvalue(section_type_value[comp].attr);
967 cf_log_err(cf_sectiontoitem(subcs),
968 "No such attribute %s",
969 section_type_value[comp].typename);
971 if (debug_flag == 0) {
972 radlog(L_ERR, "Failed to load virtual server %s",
973 (name != NULL) ? name : "<default>");
975 virtual_server_free(server);
980 * Define dynamic types, so that others can reference
983 for (modref = cf_item_find_next(subcs, NULL);
985 modref = cf_item_find_next(subcs, modref)) {
987 CONF_SECTION *subsubcs;
990 * Create types for simple references
991 * only when parsing the authenticate
994 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
995 cf_item_is_pair(modref)) {
996 CONF_PAIR *cp = cf_itemtopair(modref);
997 if (!define_type(dattr, cf_pair_attr(cp))) {
1004 if (!cf_item_is_section(modref)) continue;
1006 subsubcs = cf_itemtosection(modref);
1007 name1 = cf_section_name1(subsubcs);
1009 if (strcmp(name1, section_type_value[comp].typename) == 0) {
1010 if (!define_type(dattr,
1011 cf_section_name2(subsubcs))) {
1016 } /* loop over components */
1019 * Loop over all of the known components, finding their
1020 * configuration section, and loading it.
1023 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1024 CONF_SECTION *subcs;
1026 subcs = cf_section_sub_find(cs,
1027 section_type_value[comp].section);
1028 if (!subcs) continue;
1030 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1032 cf_log_module(cs, "Checking %s {...} for more modules to load",
1033 section_type_value[comp].section);
1037 * Skip pre/post-proxy sections if we're not
1040 if (!mainconfig.proxy_requests &&
1041 ((comp == PW_PRE_PROXY_TYPE) ||
1042 (comp == PW_PRE_PROXY_TYPE))) {
1047 if (load_component_section(subcs, components, comp) < 0) {
1052 * Cache a default, if it exists. Some people
1053 * put empty sections for some reason...
1055 c = lookup_by_index(components, comp, 0);
1056 if (c) server->mc[comp] = c->modulelist;
1058 server->subcs[comp] = subcs;
1061 } /* loop over components */
1064 * We haven't loaded any of the normal sections. Maybe we're
1065 * supposed to load the vmps section.
1067 * This is a bit of a hack...
1070 CONF_SECTION *subcs;
1072 subcs = cf_section_sub_find(cs, "vmps");
1074 cf_log_module(cs, "Checking vmps {...} for more modules to load");
1075 if (load_component_section(subcs, components,
1076 RLM_COMPONENT_POST_AUTH) < 0) {
1079 c = lookup_by_index(components,
1080 RLM_COMPONENT_POST_AUTH, 0);
1081 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1087 const DICT_ATTR *dattr;
1089 dattr = dict_attrbyname("DHCP-Message-Type");
1092 * Handle each DHCP Message type separately.
1094 if (dattr) for (subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1096 subcs = cf_subsection_find_next(cs, subcs,
1098 const char *name2 = cf_section_name2(subcs);
1100 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
1101 if (!load_subcomponent_section(NULL, subcs,
1104 RLM_COMPONENT_POST_AUTH)) {
1105 goto error; /* FIXME: memleak? */
1107 c = lookup_by_index(components,
1108 RLM_COMPONENT_POST_AUTH, 0);
1109 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1116 cf_log_info(cs, " } # modules");
1117 cf_log_info(cs, "} # server");
1119 if (!flag && name) {
1120 DEBUG("WARNING: Server %s is empty, and will do nothing!",
1124 if (debug_flag == 0) {
1125 radlog(L_INFO, "Loaded virtual server %s",
1126 (name != NULL) ? name : "<default>");
1130 * Now that it is OK, insert it into the list.
1132 * This is thread-safe...
1134 comp = virtual_server_idx(name);
1135 server->next = virtual_servers[comp];
1136 virtual_servers[comp] = server;
1139 * Mark OLDER ones of the same name as being unused.
1141 server = server->next;
1143 if ((!name && !server->name) ||
1144 (name && server->name &&
1145 (strcmp(server->name, name) == 0))) {
1146 server->can_free = TRUE;
1149 server = server->next;
1157 * Load all of the virtual servers.
1159 int virtual_servers_load(CONF_SECTION *config)
1161 int null_server = FALSE;
1163 static int first_time = TRUE;
1165 DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1168 * Load all of the virtual servers.
1170 for (cs = cf_subsection_find_next(config, NULL, "server");
1172 cs = cf_subsection_find_next(config, cs, "server")) {
1173 if (!cf_section_name2(cs)) null_server = TRUE;
1175 if (load_byserver(cs) < 0) {
1177 * Once we successfully staryed once,
1178 * continue loading the OTHER servers,
1179 * even if one fails.
1181 if (!first_time) continue;
1187 * No empty server defined. Try to load an old-style
1188 * one for backwards compatibility.
1191 if (load_byserver(config) < 0) {
1197 * If we succeed the first time around, remember that.
1204 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1206 void *insthandle = NULL;
1207 fr_module_hup_t *mh;
1210 !node->entry->module->instantiate ||
1211 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1215 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1217 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
1218 cf_log_err(cf_sectiontoitem(cs),
1219 "HUP failed for module \"%s\". Using old configuration.",
1224 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
1226 module_instance_free_old(cs, node, when);
1229 * Save the old instance handle for later deletion.
1231 mh = rad_malloc(sizeof(*mh));
1234 mh->insthandle = node->insthandle;
1235 mh->next = node->mh;
1238 node->insthandle = insthandle;
1241 * FIXME: Set a timeout to come back in 60s, so that
1242 * we can pro-actively clean up the old instances.
1249 int module_hup(CONF_SECTION *modules)
1254 module_instance_t *node;
1256 if (!modules) return 0;
1261 * Loop over the modules
1263 for (ci=cf_item_find_next(modules, NULL);
1265 ci=cf_item_find_next(modules, ci)) {
1266 const char *instname;
1267 module_instance_t myNode;
1270 * If it's not a section, ignore it.
1272 if (!cf_item_is_section(ci)) continue;
1274 cs = cf_itemtosection(ci);
1275 instname = cf_section_name2(cs);
1276 if (!instname) instname = cf_section_name1(cs);
1278 strlcpy(myNode.name, instname, sizeof(myNode.name));
1279 node = rbtree_finddata(instance_tree, &myNode);
1281 module_hup_module(cs, node, when);
1289 * Parse the module config sections, and load
1290 * and call each module's init() function.
1292 * Libtool makes your life a LOT easier, especially with libltdl.
1293 * see: http://www.gnu.org/software/libtool/
1295 int setup_modules(int reload, CONF_SECTION *config)
1297 CONF_SECTION *cs, *modules;
1298 rad_listen_t *listener;
1300 if (reload) return 0;
1303 * If necessary, initialize libltdl.
1307 * This line works around a completely
1309 * RIDICULOUS INSANE IDIOTIC
1311 * bug in libltdl on certain systems. The "set
1312 * preloaded symbols" macro below ends up
1313 * referencing this name, but it isn't defined
1314 * anywhere in the libltdl source. As a result,
1315 * any program STUPID enough to rely on libltdl
1316 * fails to link, because the symbol isn't
1319 * It's like libtool and libltdl are some kind
1322 #ifdef IE_LIBTOOL_DIE
1323 #define lt__PROGRAM__LTX_preloaded_symbols lt_libltdl_LTX_preloaded_symbols
1327 * Set the default list of preloaded symbols.
1328 * This is used to initialize libltdl's list of
1329 * preloaded modules.
1331 * i.e. Static modules.
1333 LTDL_SET_PRELOADED_SYMBOLS();
1335 if (lt_dlinit() != 0) {
1336 radlog(L_ERR, "Failed to initialize libraries: %s\n",
1342 * Set the search path to ONLY our library directory.
1343 * This prevents the modules from being found from
1344 * any location on the disk.
1346 lt_dlsetsearchpath(radlib_dir);
1349 * Set up the internal module struct.
1351 module_tree = rbtree_create(module_entry_cmp,
1352 module_entry_free, 0);
1354 radlog(L_ERR, "Failed to initialize modules\n");
1358 instance_tree = rbtree_create(module_instance_cmp,
1359 module_instance_free, 0);
1360 if (!instance_tree) {
1361 radlog(L_ERR, "Failed to initialize modules\n");
1366 memset(virtual_servers, 0, sizeof(virtual_servers));
1369 * Remember where the modules were stored.
1371 modules = cf_section_sub_find(config, "modules");
1373 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
1377 DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1380 * Look for the 'instantiate' section, which tells us
1381 * the instantiation order of the modules, and also allows
1382 * us to load modules with no authorize/authenticate/etc.
1385 cs = cf_section_sub_find(config, "instantiate");
1389 module_instance_t *module;
1392 cf_log_info(cs, " instantiate {");
1395 * Loop over the items in the 'instantiate' section.
1397 for (ci=cf_item_find_next(cs, NULL);
1399 ci=cf_item_find_next(cs, ci)) {
1402 * Skip sections and "other" stuff.
1403 * Sections will be handled later, if
1404 * they're referenced at all...
1406 if (!cf_item_is_pair(ci)) {
1410 cp = cf_itemtopair(ci);
1411 name = cf_pair_attr(cp);
1412 module = find_module_instance(modules, name, 1);
1416 } /* loop over items in the subsection */
1418 cf_log_info(cs, " }");
1419 } /* if there's an 'instantiate' section. */
1422 * Loop over the listeners, figuring out which sections
1425 for (listener = mainconfig.listen;
1427 listener = listener->next) {
1430 if (listener->type == RAD_LISTEN_PROXY) continue;
1432 cs = cf_section_sub_find_name2(config,
1433 "server", listener->server);
1434 if (!cs && (listener->server != NULL)) {
1435 listener->print(listener, buffer, sizeof(buffer));
1437 radlog(L_ERR, "No server has been defined for %s", buffer);
1442 if (virtual_servers_load(config) < 0) return -1;
1448 * Call all authorization modules until one returns
1449 * somethings else than RLM_MODULE_OK
1451 int module_authorize(int autz_type, REQUEST *request)
1453 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1457 * Authenticate a user/password with various methods.
1459 int module_authenticate(int auth_type, REQUEST *request)
1461 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1464 #ifdef WITH_ACCOUNTING
1466 * Do pre-accounting for ALL configured sessions
1468 int module_preacct(REQUEST *request)
1470 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1474 * Do accounting for ALL configured sessions
1476 int module_accounting(int acct_type, REQUEST *request)
1478 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1482 #ifdef WITH_SESSION_MGMT
1484 * See if a user is already logged in.
1486 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1488 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1492 if(!request->username)
1495 request->simul_count = 0;
1496 request->simul_max = maxsimul;
1497 request->simul_mpp = 1;
1499 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1501 if (rcode != RLM_MODULE_OK) {
1502 /* FIXME: Good spot for a *rate-limited* warning to the log */
1506 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1512 * Do pre-proxying for ALL configured sessions
1514 int module_pre_proxy(int type, REQUEST *request)
1516 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1520 * Do post-proxying for ALL configured sessions
1522 int module_post_proxy(int type, REQUEST *request)
1524 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1529 * Do post-authentication for ALL configured sessions
1531 int module_post_auth(int postauth_type, REQUEST *request)
1533 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1537 int module_recv_coa(int recv_coa_type, REQUEST *request)
1539 return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
1542 int module_send_coa(int send_coa_type, REQUEST *request)
1544 return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);