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>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modpriv.h>
29 #include <freeradius-devel/modcall.h>
30 #include <freeradius-devel/parser.h>
31 #include <freeradius-devel/rad_assert.h>
33 extern bool check_config;
35 typedef struct indexed_modcallable {
36 rlm_components_t comp;
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 }
92 #define RTLD_LOCAL (0)
96 #define LT_SHREXT ".dylib"
97 #define LD_LIBRARY_PATH "DYLD_FALLBACK_LIBRARY_PATH"
99 #define LT_SHREXT ".dll"
101 #define LT_SHREXT ".so"
102 #define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
105 /** Check if the magic number in the module matches the one in the library
107 * This is used to detect potential ABI issues caused by running with modules which
108 * were built for a different version of the server.
110 * @param cs being parsed.
111 * @param module being loaded.
112 * @returns 0 on success, -1 if prefix mismatch, -2 if version mismatch, -3 if commit mismatch.
114 static int check_module_magic(CONF_SECTION *cs, module_t const *module)
116 if (MAGIC_PREFIX(module->magic) != MAGIC_PREFIX(RADIUSD_MAGIC_NUMBER)) {
117 cf_log_err_cs(cs, "Application and rlm_%s magic number (prefix) mismatch."
118 " application: %x module: %x", module->name,
119 MAGIC_PREFIX(RADIUSD_MAGIC_NUMBER),
120 MAGIC_PREFIX(module->magic));
124 if (MAGIC_VERSION(module->magic) != MAGIC_VERSION(RADIUSD_MAGIC_NUMBER)) {
125 cf_log_err_cs(cs, "Application and rlm_%s magic number (version) mismatch."
126 " application: %lx module: %lx", module->name,
127 (unsigned long) MAGIC_VERSION(RADIUSD_MAGIC_NUMBER),
128 (unsigned long) MAGIC_VERSION(module->magic));
132 if (MAGIC_COMMIT(module->magic) != MAGIC_COMMIT(RADIUSD_MAGIC_NUMBER)) {
133 cf_log_err_cs(cs, "Application and rlm_%s magic number (commit) mismatch."
134 " application: %lx module: %lx", module->name,
135 (unsigned long) MAGIC_COMMIT(RADIUSD_MAGIC_NUMBER),
136 (unsigned long) MAGIC_COMMIT(module->magic));
144 * Because dlopen produces really shitty and inaccurate error messages
146 static void check_lib_access(char const *name)
148 if (access(name, R_OK) < 0) switch (errno) {
150 WDEBUG("Library \"%s\" exists, but we don't have permission to read", name);
153 DEBUG4("Library not found at path \"%s\"", name);
156 DEBUG4("Possible issue accessing Library \"%s\": %s", name, strerror(errno));
161 lt_dlhandle lt_dlopenext(char const *name)
163 int flags = RTLD_NOW;
168 if (strcmp(name, "rlm_perl") == 0) {
169 flags |= RTLD_GLOBAL;
175 * Prefer loading our libraries by absolute path.
177 snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT);
179 check_lib_access(buffer);
181 handle = dlopen(buffer, flags);
182 if (handle) return handle;
184 strlcpy(buffer, name, sizeof(buffer));
187 * FIXME: Make this configurable...
189 strlcat(buffer, LT_SHREXT, sizeof(buffer));
191 return dlopen(buffer, flags);
194 void *lt_dlsym(lt_dlhandle handle, UNUSED char const *symbol)
196 return dlsym(handle, symbol);
199 int lt_dlclose(lt_dlhandle handle)
201 if (!handle) return 0;
203 return dlclose(handle);
206 char const *lt_dlerror(void)
211 static int virtual_server_idx(char const *name)
217 hash = fr_hash_string(name);
219 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
222 static virtual_server_t *virtual_server_find(char const *name)
225 virtual_server_t *server;
227 rcode = virtual_server_idx(name);
228 for (server = virtual_servers[rcode];
230 server = server->next) {
231 if (!name && !server->name) break;
233 if ((name && server->name) &&
234 (strcmp(name, server->name) == 0)) break;
240 static int virtual_server_free(virtual_server_t *server)
242 if (server->components) rbtree_free(server->components);
246 void virtual_servers_free(time_t when)
249 virtual_server_t **last;
251 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
252 virtual_server_t *server, *next;
254 last = &virtual_servers[i];
255 for (server = virtual_servers[i];
261 * If we delete it, fix the links so that
262 * we don't orphan anything. Also,
263 * delete it if it's old, AND a newer one
266 * Otherwise, the last pointer gets set to
267 * the one we didn't delete.
270 ((server->created < when) && server->can_free)) {
271 *last = server->next;
274 last = &(server->next);
280 static int indexed_modcallable_free(indexed_modcallable *this)
282 modcallable_free(&this->modulelist);
286 static int indexed_modcallable_cmp(void const *one, void const *two)
288 indexed_modcallable const *a = one;
289 indexed_modcallable const *b = two;
291 if (a->comp < b->comp) return -1;
292 if (a->comp > b->comp) return +1;
294 return a->idx - b->idx;
299 * Compare two module entries
301 static int module_instance_cmp(void const *one, void const *two)
303 module_instance_t const *a = one;
304 module_instance_t const *b = two;
306 return strcmp(a->name, b->name);
310 static void module_instance_free_old(UNUSED CONF_SECTION *cs, module_instance_t *node,
313 fr_module_hup_t *mh, **last;
316 * Walk the list, freeing up old instances.
323 * Free only every 60 seconds.
325 if ((when - mh->when) < 60) {
330 talloc_free(mh->insthandle);
339 * Free a module instance.
341 static void module_instance_free(void *data)
343 module_instance_t *this = data;
345 module_instance_free_old(this->cs, this, time(NULL) + 100);
347 #ifdef HAVE_PTHREAD_H
351 * The mutex MIGHT be locked...
352 * we'll check for that later, I guess.
354 pthread_mutex_destroy(this->mutex);
355 talloc_free(this->mutex);
360 * Remove any registered paircompares.
362 paircompare_unregister_instance(this->insthandle);
364 xlat_unregister(this->name, NULL, this->insthandle);
367 memset(this, 0, sizeof(*this));
374 * Compare two module entries
376 static int module_entry_cmp(void const *one, void const *two)
378 module_entry_t const *a = one;
379 module_entry_t const *b = two;
381 return strcmp(a->name, b->name);
385 * Free a module entry.
387 static int module_entry_free(module_entry_t *this)
391 * Don't dlclose() modules if we're doing memory
392 * debugging. This removes the symbols needed by
395 if (!mainconfig.debug_memory)
397 dlclose(this->handle); /* ignore any errors */
403 * Remove the module lists.
405 int detach_modules(void)
407 rbtree_free(instance_tree);
408 rbtree_free(module_tree);
415 * Find a module on disk or in memory, and link to it.
417 static module_entry_t *linkto_module(char const *module_name,
420 module_entry_t myentry;
421 module_entry_t *node;
423 module_t const *module;
425 strlcpy(myentry.name, module_name, sizeof(myentry.name));
426 node = rbtree_finddata(module_tree, &myentry);
427 if (node) return node;
430 * Link to the module's rlm_FOO{} structure, the same as
434 #if !defined(WITH_LIBLTDL) && defined(HAVE_DLFCN_H) && defined(RTLD_SELF)
435 module = dlsym(RTLD_SELF, module_name);
436 if (module) goto open_self;
440 * Keep the handle around so we can dlclose() it.
442 handle = lt_dlopenext(module_name);
445 "Failed to link to module '%s': %s\n",
446 module_name, dlerror());
450 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
453 * libltld MAY core here, if the handle it gives us contains
456 module = dlsym(handle, module_name);
459 "Failed linking to %s structure: %s\n",
460 module_name, dlerror());
465 #if !defined(WITH_LIBLTDL) && defined (HAVE_DLFCN_H) && defined(RTLD_SELF)
469 * Before doing anything else, check if it's sane.
471 if (check_module_magic(cs, module) < 0) {
476 /* make room for the module type */
477 node = talloc_zero(cs, module_entry_t);
478 talloc_set_destructor(node, module_entry_free);
479 strlcpy(node->name, module_name, sizeof(node->name));
480 node->module = module;
481 node->handle = handle;
483 cf_log_module(cs, "Loaded module %s", module_name);
486 * Add the module as "rlm_foo-version" to the configuration
489 if (!rbtree_insert(module_tree, node)) {
490 ERROR("Failed to cache module %s", module_name);
499 /** Parse module's configuration section and setup destructors
502 static int module_conf_parse(module_instance_t *node, void **handle)
507 * If there is supposed to be instance data, allocate it now.
508 * Also parse the configuration data, if required.
510 if (node->entry->module->inst_size) {
511 /* FIXME: make this rlm_config_t ?? */
512 *handle = talloc_zero_array(node, uint8_t, node->entry->module->inst_size);
516 * So we can see where this configuration is from
517 * FIXME: set it to rlm_NAME_t, or some such thing
519 talloc_set_name(*handle, "rlm_config_t");
521 if (node->entry->module->config &&
522 (cf_section_parse(node->cs, *handle, node->entry->module->config) < 0)) {
523 cf_log_err_cs(node->cs,"Invalid configuration for module \"%s\"", node->name);
524 talloc_free(*handle);
530 * Set the destructor.
532 if (node->entry->module->detach) {
533 talloc_set_destructor(*handle, node->entry->module->detach);
541 * Find a module instance.
543 module_instance_t *find_module_instance(CONF_SECTION *modules,
544 char const *askedname, int do_link)
546 bool check_config_safe = false;
548 char const *name1, *instname;
549 module_instance_t *node, myNode;
550 char module_name[256];
552 if (!modules) return NULL;
555 * Look for the real name. Ignore the first character,
556 * which tells the server "it's OK for this module to not
559 instname = askedname;
560 if (instname[0] == '-') {
565 * Module instances are declared in the modules{} block
566 * and referenced later by their name, which is the
567 * name2 from the config section, or name1 if there was
570 cs = cf_section_sub_find_name2(modules, NULL, instname);
572 ERROR("Cannot find a configuration entry for module \"%s\"", instname);
577 * If there's already a module instance, return it.
579 strlcpy(myNode.name, instname, sizeof(myNode.name));
581 node = rbtree_finddata(instance_tree, &myNode);
590 name1 = cf_section_name1(cs);
593 * Found the configuration entry, hang the node struct off of the
594 * configuration section. If the CS is free'd the instance will
597 node = talloc_zero(cs, module_instance_t);
601 * Names in the "modules" section aren't prefixed
602 * with "rlm_", so we add it here.
604 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
607 * Pull in the module object
609 node->entry = linkto_module(module_name, cs);
612 /* linkto_module logs any errors */
616 if (check_config && (node->entry->module->instantiate) &&
617 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
618 char const *value = NULL;
621 cp = cf_pair_find(cs, "force_check_config");
623 value = cf_pair_value(cp);
626 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
628 cf_log_module(cs, "Skipping instantiation of %s", instname);
631 check_config_safe = true;
632 cf_log_module(cs, "Instantiating module \"%s\" from file %s", instname,
633 cf_section_filename(cs));
636 strlcpy(node->name, instname, sizeof(node->name));
639 * Parse the module configuration, and setup destructors so the
640 * module's detach method is called when it's instance data is
643 if (module_conf_parse(node, &node->insthandle) < 0) {
650 * Call the module's instantiation routine.
652 if ((node->entry->module->instantiate) &&
653 (!check_config || check_config_safe) &&
654 ((node->entry->module->instantiate)(cs, node->insthandle) < 0)) {
655 cf_log_err_cs(cs, "Instantiation failed for module \"%s\"", node->name);
661 #ifdef HAVE_PTHREAD_H
663 * If we're threaded, check if the module is thread-safe.
665 * If it isn't, we create a mutex.
667 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
668 node->mutex = talloc_zero(node, pthread_mutex_t);
671 * Initialize the mutex.
673 pthread_mutex_init(node->mutex, NULL);
676 * The module is thread-safe. Don't give it a mutex.
682 rbtree_insert(instance_tree, node);
687 static indexed_modcallable *lookup_by_index(rbtree_t *components,
688 rlm_components_t comp, int idx)
690 indexed_modcallable myc;
695 return rbtree_finddata(components, &myc);
699 * Create a new sublist.
701 static indexed_modcallable *new_sublist(CONF_SECTION *cs,
702 rbtree_t *components, rlm_components_t comp, int idx)
704 indexed_modcallable *c;
706 c = lookup_by_index(components, comp, idx);
708 /* It is an error to try to create a sublist that already
709 * exists. It would almost certainly be caused by accidental
710 * duplication in the config file.
712 * index 0 is the exception, because it is used when we want
713 * to collect _all_ listed modules under a single index by
714 * default, which is currently the case in all components
715 * except authenticate. */
723 c = talloc_zero(cs, indexed_modcallable);
724 c->modulelist = NULL;
728 if (!rbtree_insert(components, c)) {
733 talloc_set_destructor(c, indexed_modcallable_free);
738 rlm_rcode_t indexed_modcall(rlm_components_t 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) RWDEBUG2("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 RWDEBUG2("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 DICT_ATTR const *dattr, rlm_components_t comp)
796 indexed_modcallable *subcomp;
799 char const *name2 = cf_section_name2(cs);
811 ml = compile_modgroup(parent, comp, cs);
817 * We must assign a numeric index to this subcomponent.
818 * It is generated and placed in the dictionary
819 * automatically. If it isn't found, it's a serious
822 dval = dict_valbyname(dattr->attr, dattr->vendor, name2);
825 "%s %s Not previously configured",
826 section_type_value[comp].typename, name2);
827 modcallable_free(&ml);
831 subcomp = new_sublist(cs, components, comp, dval->value);
833 modcallable_free(&ml);
837 subcomp->modulelist = ml;
841 static int define_type(CONF_SECTION *cs, DICT_ATTR const *dattr, char const *name)
847 * If the value already exists, don't
850 dval = dict_valbyname(dattr->attr, dattr->vendor, name);
854 * Create a new unique value with a
855 * meaningless number. You can't look at
856 * it from outside of this code, so it
857 * doesn't matter. The only requirement
858 * is that it's unique.
861 value = fr_rand() & 0x00ffffff;
862 } while (dict_valbyattr(dattr->attr, dattr->vendor, value));
864 cf_log_module(cs, "Creating %s = %s", dattr->name, name);
865 if (dict_addvalue(name, dattr->name, value) < 0) {
866 ERROR("%s", fr_strerror());
873 static int load_component_section(CONF_SECTION *cs,
874 rbtree_t *components, rlm_components_t comp)
879 indexed_modcallable *subcomp;
881 char const *visiblename;
882 DICT_ATTR const *dattr;
885 * Find the attribute used to store VALUEs for this section.
887 dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
890 "No such attribute %s",
891 section_type_value[comp].typename);
896 * Loop over the entries in the named section, loading
897 * the sections this time.
899 for (modref = cf_item_find_next(cs, NULL);
901 modref = cf_item_find_next(cs, modref)) {
903 CONF_PAIR *cp = NULL;
904 CONF_SECTION *scs = NULL;
906 if (cf_item_is_section(modref)) {
907 scs = cf_itemtosection(modref);
909 name1 = cf_section_name1(scs);
912 section_type_value[comp].typename) == 0) {
913 if (!load_subcomponent_section(NULL, scs,
917 return -1; /* FIXME: memleak? */
925 * Skip commented-out sections.
927 * We skip an "if" ONLY when there's no
928 * "else" after it, as the run-time
929 * interpretor needs the results of the
932 if (strcmp(name1, "if") == 0) {
936 next_ci = cf_item_find_next(scs, modref);
937 if (next_ci && cf_item_is_section(next_ci)) {
938 char const *next_name;
939 CONF_SECTION *next_cs;
941 next_cs = cf_itemtosection(next_ci);
942 next_name = cf_section_name1(next_cs);
943 if ((strcmp(next_name, "else") == 0) ||
944 (strcmp(next_name, "elseif") == 0)) {
947 c = cf_data_find(scs, "if");
950 c = cf_data_find(scs, "if");
953 if (c && c->type == COND_TYPE_FALSE) {
954 DEBUG(" # Skipping contents of '%s' at %s:%d as it statically evaluates to 'false'",
955 name1, cf_section_filename(scs), cf_section_lineno(scs));
961 } else if (cf_item_is_pair(modref)) {
962 cp = cf_itemtopair(modref);
965 continue; /* ignore it */
969 * Try to compile one entry.
971 this = compile_modsingle(NULL, comp, modref, &modname);
974 * It's OK for the module to not exist.
976 if (!this && modname && (modname[0] == '-')) {
977 WDEBUG("Ignoring \"%s\" (see raddb/mods-available/README.rst)", modname + 1);
983 "Errors parsing %s section.\n",
984 cf_section_name1(cs));
989 * Look for Auth-Type foo {}, which are special
990 * cases of named sections, and allowable ONLY
993 * i.e. They're not allowed in a "group" or "redundant"
996 if (comp == RLM_COMPONENT_AUTH) {
998 char const *modrefname = NULL;
1000 modrefname = cf_pair_attr(cp);
1002 modrefname = cf_section_name2(scs);
1004 modcallable_free(&this);
1006 "Errors parsing %s sub-section.\n",
1007 cf_section_name1(scs));
1012 dval = dict_valbyname(PW_AUTH_TYPE, 0, modrefname);
1015 * It's a section, but nothing we
1018 modcallable_free(&this);
1020 "Unknown Auth-Type \"%s\" in %s sub-section.",
1021 modrefname, section_type_value[comp].section);
1026 /* See the comment in new_sublist() for explanation
1027 * of the special index 0 */
1031 subcomp = new_sublist(cs, components, comp, idx);
1032 if (subcomp == NULL) {
1033 modcallable_free(&this);
1037 /* If subcomp->modulelist is NULL, add_to_modcallable will
1039 visiblename = cf_section_name2(cs);
1040 if (visiblename == NULL)
1041 visiblename = cf_section_name1(cs);
1042 add_to_modcallable(&subcomp->modulelist, this,
1049 static int load_byserver(CONF_SECTION *cs)
1051 rlm_components_t comp, found;
1052 char const *name = cf_section_name2(cs);
1053 rbtree_t *components;
1054 virtual_server_t *server = NULL;
1055 indexed_modcallable *c;
1058 cf_log_info(cs, "server %s { # from file %s",
1059 name, cf_section_filename(cs));
1061 cf_log_info(cs, "server { # from file %s",
1062 cf_section_filename(cs));
1065 components = rbtree_create(indexed_modcallable_cmp, NULL, 0);
1067 ERROR("Failed to initialize components\n");
1071 server = talloc_zero(cs, virtual_server_t);
1072 server->name = name;
1073 server->created = time(NULL);
1075 server->components = components;
1076 talloc_set_destructor(server, virtual_server_free);
1079 * Define types first.
1081 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1082 CONF_SECTION *subcs;
1084 DICT_ATTR const *dattr;
1086 subcs = cf_section_sub_find(cs,
1087 section_type_value[comp].section);
1088 if (!subcs) continue;
1090 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1093 * Find the attribute used to store VALUEs for this section.
1095 dattr = dict_attrbyvalue(section_type_value[comp].attr, 0);
1097 cf_log_err_cs(subcs,
1098 "No such attribute %s",
1099 section_type_value[comp].typename);
1101 if (debug_flag == 0) {
1102 ERROR("Failed to load virtual server %s",
1103 (name != NULL) ? name : "<default>");
1105 talloc_free(server);
1110 * Define dynamic types, so that others can reference
1113 for (modref = cf_item_find_next(subcs, NULL);
1115 modref = cf_item_find_next(subcs, modref)) {
1117 CONF_SECTION *subsubcs;
1120 * Create types for simple references
1121 * only when parsing the authenticate
1124 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
1125 cf_item_is_pair(modref)) {
1126 CONF_PAIR *cp = cf_itemtopair(modref);
1127 if (!define_type(cs, dattr, cf_pair_attr(cp))) {
1134 if (!cf_item_is_section(modref)) continue;
1136 subsubcs = cf_itemtosection(modref);
1137 name1 = cf_section_name1(subsubcs);
1139 if (strcmp(name1, section_type_value[comp].typename) == 0) {
1140 if (!define_type(cs, dattr,
1141 cf_section_name2(subsubcs))) {
1146 } /* loop over components */
1149 * Loop over all of the known components, finding their
1150 * configuration section, and loading it.
1153 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
1154 CONF_SECTION *subcs;
1156 subcs = cf_section_sub_find(cs,
1157 section_type_value[comp].section);
1158 if (!subcs) continue;
1160 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1162 cf_log_module(cs, "Loading %s {...}",
1163 section_type_value[comp].section);
1166 * Skip pre/post-proxy sections if we're not
1171 !mainconfig.proxy_requests &&
1173 ((comp == RLM_COMPONENT_PRE_PROXY) ||
1174 (comp == RLM_COMPONENT_POST_PROXY))) {
1178 #ifndef WITH_ACCOUNTING
1179 if (comp == RLM_COMPONENT_ACCT) continue;
1182 #ifndef WITH_SESSION_MGMT
1183 if (comp == RLM_COMPONENT_SESS) continue;
1186 if (load_component_section(subcs, components, comp) < 0) {
1191 * Cache a default, if it exists. Some people
1192 * put empty sections for some reason...
1194 c = lookup_by_index(components, comp, 0);
1195 if (c) server->mc[comp] = c->modulelist;
1197 server->subcs[comp] = subcs;
1200 } /* loop over components */
1203 * We haven't loaded any of the normal sections. Maybe we're
1204 * supposed to load the vmps section.
1206 * This is a bit of a hack...
1209 CONF_SECTION *subcs;
1211 DICT_ATTR const *dattr;
1214 subcs = cf_section_sub_find(cs, "vmps");
1216 cf_log_module(cs, "Checking vmps {...} for more modules to load");
1217 if (load_component_section(subcs, components,
1218 RLM_COMPONENT_POST_AUTH) < 0) {
1221 c = lookup_by_index(components,
1222 RLM_COMPONENT_POST_AUTH, 0);
1223 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1229 subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1230 dattr = dict_attrbyname("DHCP-Message-Type");
1231 if (!dattr && subcs) {
1232 cf_log_err_cs(subcs, "Found a 'dhcp' section, but no DHCP dictionaries have been loaded");
1239 * Handle each DHCP Message type separately.
1242 char const *name2 = cf_section_name2(subcs);
1244 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
1245 if (!load_subcomponent_section(NULL, subcs,
1248 RLM_COMPONENT_POST_AUTH)) {
1249 goto error; /* FIXME: memleak? */
1251 c = lookup_by_index(components,
1252 RLM_COMPONENT_POST_AUTH, 0);
1253 if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
1256 subcs = cf_subsection_find_next(cs, subcs, "dhcp");
1262 cf_log_info(cs, "} # server %s", name);
1264 cf_log_info(cs, "} # server");
1267 if (!found && name) {
1268 WDEBUG("Server %s is empty, and will do nothing!",
1272 if (debug_flag == 0) {
1273 INFO("Loaded virtual server %s",
1274 (name != NULL) ? name : "<default>");
1278 * Now that it is OK, insert it into the list.
1280 * This is thread-safe...
1282 comp = virtual_server_idx(name);
1283 server->next = virtual_servers[comp];
1284 virtual_servers[comp] = server;
1287 * Mark OLDER ones of the same name as being unused.
1289 server = server->next;
1291 if ((!name && !server->name) ||
1292 (name && server->name &&
1293 (strcmp(server->name, name) == 0))) {
1294 server->can_free = true;
1297 server = server->next;
1305 * Load all of the virtual servers.
1307 int virtual_servers_load(CONF_SECTION *config)
1310 virtual_server_t *server;
1311 static int first_time = true;
1313 DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1316 * If we have "server { ...}", then there SHOULD NOT be
1317 * bare "authorize", etc. sections. if there is no such
1318 * server, then try to load the old-style sections first.
1320 * In either case, load the "default" virtual server first.
1321 * this matches better with users expectations.
1323 cs = cf_section_find_name2(cf_subsection_find_next(config, NULL,
1327 if (load_byserver(cs) < 0) {
1331 if (load_byserver(config) < 0) {
1337 * Load all of the virtual servers.
1339 for (cs = cf_subsection_find_next(config, NULL, "server");
1341 cs = cf_subsection_find_next(config, cs, "server")) {
1344 name2 = cf_section_name2(cs);
1345 if (!name2) continue; /* handled above */
1347 server = virtual_server_find(name2);
1349 (cf_top_section(server->cs) == config)) {
1350 ERROR("Duplicate virtual server \"%s\" in file %s:%d and file %s:%d",
1352 cf_section_filename(server->cs),
1353 cf_section_lineno(server->cs),
1354 cf_section_filename(cs),
1355 cf_section_lineno(cs));
1359 if (load_byserver(cs) < 0) {
1361 * Once we successfully started once,
1362 * continue loading the OTHER servers,
1363 * even if one fails.
1365 if (!first_time) continue;
1371 * Now that we've loaded everything, run pass 2 over the
1372 * conditions and xlats.
1374 for (cs = cf_subsection_find_next(config, NULL, "server");
1376 cs = cf_subsection_find_next(config, cs, "server")) {
1380 name2 = cf_section_name2(cs);
1382 server = virtual_server_find(name2);
1383 if (!server) continue;
1385 for (i = RLM_COMPONENT_AUTH; i < RLM_COMPONENT_COUNT; i++) {
1386 if (!modcall_pass2(server->mc[i])) return -1;
1391 * If we succeed the first time around, remember that.
1398 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1401 fr_module_hup_t *mh;
1404 !node->entry->module->instantiate ||
1405 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1409 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1412 * Parse the module configuration, and setup destructors so the
1413 * module's detach method is called when it's instance data is
1414 * about to be freed.
1416 if (module_conf_parse(node, &insthandle) < 0) {
1417 cf_log_err_cs(cs, "HUP failed for module \"%s\" (parsing config failed). "
1418 "Using old configuration", node->name);
1423 if ((node->entry->module->instantiate)(cs, insthandle) < 0) {
1424 cf_log_err_cs(cs, "HUP failed for module \"%s\". Using old configuration.", node->name);
1425 talloc_free(insthandle);
1430 INFO(" Module: Reloaded module \"%s\"", node->name);
1432 module_instance_free_old(cs, node, when);
1435 * Save the old instance handle for later deletion.
1437 mh = talloc_zero(cs, fr_module_hup_t);
1440 mh->insthandle = node->insthandle;
1441 mh->next = node->mh;
1444 node->insthandle = insthandle;
1447 * FIXME: Set a timeout to come back in 60s, so that
1448 * we can pro-actively clean up the old instances.
1455 int module_hup(CONF_SECTION *modules)
1460 module_instance_t *node;
1462 if (!modules) return 0;
1467 * Loop over the modules
1469 for (ci=cf_item_find_next(modules, NULL);
1471 ci=cf_item_find_next(modules, ci)) {
1472 char const *instname;
1473 module_instance_t myNode;
1476 * If it's not a section, ignore it.
1478 if (!cf_item_is_section(ci)) continue;
1480 cs = cf_itemtosection(ci);
1481 instname = cf_section_name2(cs);
1482 if (!instname) instname = cf_section_name1(cs);
1484 strlcpy(myNode.name, instname, sizeof(myNode.name));
1485 node = rbtree_finddata(instance_tree, &myNode);
1487 module_hup_module(cs, node, when);
1495 * Parse the module config sections, and load
1496 * and call each module's init() function.
1498 * Libtool makes your life a LOT easier, especially with libltdl.
1499 * see: http://www.gnu.org/software/libtool/
1501 int setup_modules(int reload, CONF_SECTION *config)
1503 CONF_ITEM *ci, *next;
1504 CONF_SECTION *cs, *modules;
1505 rad_listen_t *listener;
1507 if (reload) return 0;
1510 * If necessary, initialize libltdl.
1514 * Set up the internal module struct.
1516 module_tree = rbtree_create(module_entry_cmp, NULL, 0);
1518 ERROR("Failed to initialize modules\n");
1522 instance_tree = rbtree_create(module_instance_cmp,
1523 module_instance_free, 0);
1524 if (!instance_tree) {
1525 ERROR("Failed to initialize modules\n");
1530 memset(virtual_servers, 0, sizeof(virtual_servers));
1533 * Remember where the modules were stored.
1535 modules = cf_section_sub_find(config, "modules");
1537 WARN("Cannot find a \"modules\" section in the configuration file!");
1540 DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1543 * Loop over module definitions, looking for duplicates.
1545 * This is O(N^2) in the number of modules, but most
1546 * systems should have less than 100 modules.
1548 for (ci=cf_item_find_next(modules, NULL);
1551 char const *name1, *name2;
1552 CONF_SECTION *subcs, *duplicate;
1554 next = cf_item_find_next(modules, ci);
1556 if (!cf_item_is_section(ci)) continue;
1558 if (!next || !cf_item_is_section(next)) continue;
1560 subcs = cf_itemtosection(ci);
1561 name1 = cf_section_name1(subcs);
1562 name2 = cf_section_name2(subcs);
1564 duplicate = cf_section_find_name2(cf_itemtosection(next),
1566 if (!duplicate) continue;
1568 if (!name2) name2 = "";
1570 ERROR("Duplicate module \"%s %s\", in file %s:%d and file %s:%d",
1572 cf_section_filename(subcs),
1573 cf_section_lineno(subcs),
1574 cf_section_filename(duplicate),
1575 cf_section_lineno(duplicate));
1580 * Look for the 'instantiate' section, which tells us
1581 * the instantiation order of the modules, and also allows
1582 * us to load modules with no authorize/authenticate/etc.
1585 cs = cf_section_sub_find(config, "instantiate");
1588 module_instance_t *module;
1591 cf_log_info(cs, " instantiate {");
1594 * Loop over the items in the 'instantiate' section.
1596 for (ci=cf_item_find_next(cs, NULL);
1598 ci=cf_item_find_next(cs, ci)) {
1601 * Skip sections and "other" stuff.
1602 * Sections will be handled later, if
1603 * they're referenced at all...
1605 if (!cf_item_is_pair(ci)) {
1609 cp = cf_itemtopair(ci);
1610 name = cf_pair_attr(cp);
1611 module = find_module_instance(modules, name, 1);
1612 if (!module && (name[0] != '-')) {
1615 } /* loop over items in the subsection */
1617 cf_log_info(cs, " }");
1618 } /* if there's an 'instantiate' section. */
1621 * Now that we've loaded the explicitly ordered modules,
1622 * load everything in the "modules" section. This is
1623 * because we've now split up the modules into
1626 cf_log_info(cs, " modules {");
1627 for (ci=cf_item_find_next(modules, NULL);
1631 module_instance_t *module;
1632 CONF_SECTION *subcs;
1634 next = cf_item_find_next(modules, ci);
1636 if (!cf_item_is_section(ci)) continue;
1638 subcs = cf_itemtosection(ci);
1639 name = cf_section_name2(subcs);
1640 if (!name) name = cf_section_name1(subcs);
1642 module = find_module_instance(modules, name, 1);
1643 if (!module) return -1;
1645 cf_log_info(cs, " } # modules");
1648 * Loop over the listeners, figuring out which sections
1651 for (listener = mainconfig.listen;
1653 listener = listener->next) {
1657 if (listener->type == RAD_LISTEN_PROXY) continue;
1660 cs = cf_section_sub_find_name2(config,
1661 "server", listener->server);
1662 if (!cs && (listener->server != NULL)) {
1663 listener->print(listener, buffer, sizeof(buffer));
1665 ERROR("No server has been defined for %s", buffer);
1670 if (virtual_servers_load(config) < 0) return -1;
1676 * Call all authorization modules until one returns
1677 * somethings else than RLM_MODULE_OK
1679 rlm_rcode_t process_authorize(int autz_type, REQUEST *request)
1681 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1685 * Authenticate a user/password with various methods.
1687 rlm_rcode_t process_authenticate(int auth_type, REQUEST *request)
1689 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1692 #ifdef WITH_ACCOUNTING
1694 * Do pre-accounting for ALL configured sessions
1696 rlm_rcode_t module_preacct(REQUEST *request)
1698 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1702 * Do accounting for ALL configured sessions
1704 rlm_rcode_t process_accounting(int acct_type, REQUEST *request)
1706 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1710 #ifdef WITH_SESSION_MGMT
1712 * See if a user is already logged in.
1714 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1716 int process_checksimul(int sess_type, REQUEST *request, int maxsimul)
1720 if(!request->username)
1723 request->simul_count = 0;
1724 request->simul_max = maxsimul;
1725 request->simul_mpp = 1;
1727 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1729 if (rcode != RLM_MODULE_OK) {
1730 /* FIXME: Good spot for a *rate-limited* warning to the log */
1734 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1740 * Do pre-proxying for ALL configured sessions
1742 rlm_rcode_t process_pre_proxy(int type, REQUEST *request)
1744 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1748 * Do post-proxying for ALL configured sessions
1750 rlm_rcode_t process_post_proxy(int type, REQUEST *request)
1752 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1757 * Do post-authentication for ALL configured sessions
1759 rlm_rcode_t process_post_auth(int postauth_type, REQUEST *request)
1761 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1765 rlm_rcode_t process_recv_coa(int recv_coa_type, REQUEST *request)
1767 return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
1770 rlm_rcode_t process_send_coa(int send_coa_type, REQUEST *request)
1772 return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);