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 typedef struct indexed_modcallable {
34 rlm_components_t comp;
36 modcallable *modulelist;
37 } indexed_modcallable;
39 typedef struct virtual_server_t {
45 modcallable *mc[MOD_COUNT];
46 CONF_SECTION *subcs[MOD_COUNT];
47 struct virtual_server_t *next;
51 * Keep a hash of virtual servers, so that we can reload them.
53 #define VIRTUAL_SERVER_HASH_SIZE (256)
54 static virtual_server_t *virtual_servers[VIRTUAL_SERVER_HASH_SIZE];
56 static rbtree_t *module_tree = NULL;
58 static rbtree_t *instance_tree = NULL;
60 struct fr_module_hup_t {
61 module_instance_t *mi;
64 fr_module_hup_t *next;
68 * Ordered by component
70 const section_type_value_t section_type_value[MOD_COUNT] = {
71 { "authenticate", "Auth-Type", PW_AUTH_TYPE },
72 { "authorize", "Autz-Type", PW_AUTZ_TYPE },
73 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
74 { "accounting", "Acct-Type", PW_ACCT_TYPE },
75 { "session", "Session-Type", PW_SESSION_TYPE },
76 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
77 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
78 { "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE }
81 { "recv-coa", "Recv-CoA-Type", PW_RECV_COA_TYPE },
82 { "send-coa", "Send-CoA-Type", PW_SEND_COA_TYPE }
90 #define RTLD_LOCAL (0)
94 # define LT_SHREXT ".dylib"
96 # define LT_SHREXT ".dll"
98 # define LT_SHREXT ".so"
101 /** Check if the magic number in the module matches the one in the library
103 * This is used to detect potential ABI issues caused by running with modules which
104 * were built for a different version of the server.
106 * @param cs being parsed.
107 * @param module being loaded.
108 * @returns 0 on success, -1 if prefix mismatch, -2 if version mismatch, -3 if commit mismatch.
110 static int check_module_magic(CONF_SECTION *cs, module_t const *module)
113 dladdr(module, &dl_info);
115 if (MAGIC_PREFIX(module->magic) != MAGIC_PREFIX(RADIUSD_MAGIC_NUMBER)) {
116 cf_log_err_cs(cs, "Failed loading module rlm_%s from file %s", module->name, dl_info.dli_fname);
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, "Failed loading module rlm_%s from file %s", module->name, dl_info.dli_fname);
126 cf_log_err_cs(cs, "Application and rlm_%s magic number (version) mismatch."
127 " application: %lx module: %lx", module->name,
128 (unsigned long) MAGIC_VERSION(RADIUSD_MAGIC_NUMBER),
129 (unsigned long) MAGIC_VERSION(module->magic));
133 if (MAGIC_COMMIT(module->magic) != MAGIC_COMMIT(RADIUSD_MAGIC_NUMBER)) {
134 cf_log_err_cs(cs, "Failed loading module rlm_%s from file %s", module->name, dl_info.dli_fname);
135 cf_log_err_cs(cs, "Application and rlm_%s magic number (commit) mismatch."
136 " application: %lx module: %lx", module->name,
137 (unsigned long) MAGIC_COMMIT(RADIUSD_MAGIC_NUMBER),
138 (unsigned long) MAGIC_COMMIT(module->magic));
145 lt_dlhandle lt_dlopenext(char const *name)
147 int flags = RTLD_NOW;
153 if (strcmp(name, "rlm_perl") == 0) {
154 flags |= RTLD_GLOBAL;
161 * Bind all the symbols *NOW* so we don't hit errors later
166 * Prefer loading our libraries by absolute path.
171 snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT);
173 DEBUG4("Loading library using absolute path \"%s\"", buffer);
175 handle = dlopen(buffer, flags);
176 if (handle) return handle;
179 fr_strerror_printf("%s", error);
180 DEBUG4("Failed with error: %s", error);
183 * Because dlopen (on OSX at least) produces really
184 * shitty and inaccurate error messages
186 if (access(buffer, R_OK) < 0) {
189 WARN("Library file found, but we don't have permission to read it");
193 DEBUG4("Library file not found");
197 DEBUG4("Issue accessing library file: %s", fr_syserror(errno));
203 DEBUG4("Loading library using linker search path(s)");
204 if (DEBUG_ENABLED4) {
206 env = getenv("LD_LIBRARY_PATH");
208 DEBUG4("LD_LIBRARY_PATH : %s", env);
210 env = getenv("DYLD_LIBRARY_PATH");
212 DEBUG4("DYLB_LIBRARY_PATH : %s", env);
214 env = getenv("DYLD_FALLBACK_LIBRARY_PATH");
216 DEBUG4("DYLD_FALLBACK_LIBRARY_PATH : %s", env);
218 env = getcwd(buffer, sizeof(buffer));
220 DEBUG4("Current directory : %s", env);
223 env = getenv("LD_LIBRARY_PATH");
225 DEBUG4("LD_LIBRARY_PATH : %s", env);
227 DEBUG4("Defaults : /lib:/usr/lib");
231 strlcpy(buffer, name, sizeof(buffer));
233 * FIXME: Make this configurable...
235 strlcat(buffer, LT_SHREXT, sizeof(buffer));
237 handle = dlopen(buffer, flags);
239 char *error = dlerror();
241 DEBUG4("Failed with error: %s", error);
243 * Don't overwrite the previous message
244 * It's likely to contain a better error.
247 fr_strerror_printf("%s", dlerror());
254 void *lt_dlsym(lt_dlhandle handle, char const *symbol)
256 return dlsym(handle, symbol);
259 int lt_dlclose(lt_dlhandle handle)
261 if (!handle) return 0;
263 return dlclose(handle);
266 char const *lt_dlerror(void)
271 static int virtual_server_idx(char const *name)
277 hash = fr_hash_string(name);
279 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
282 static virtual_server_t *virtual_server_find(char const *name)
285 virtual_server_t *server;
287 rcode = virtual_server_idx(name);
288 for (server = virtual_servers[rcode];
290 server = server->next) {
291 if (!name && !server->name) break;
293 if ((name && server->name) &&
294 (strcmp(name, server->name) == 0)) break;
300 static int _virtual_server_free(virtual_server_t *server)
302 if (server->components) rbtree_free(server->components);
306 void virtual_servers_free(time_t when)
309 virtual_server_t **last;
311 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
312 virtual_server_t *server, *next;
314 last = &virtual_servers[i];
315 for (server = virtual_servers[i];
321 * If we delete it, fix the links so that
322 * we don't orphan anything. Also,
323 * delete it if it's old, AND a newer one
326 * Otherwise, the last pointer gets set to
327 * the one we didn't delete.
330 ((server->created < when) && server->can_free)) {
331 *last = server->next;
334 last = &(server->next);
340 static int indexed_modcallable_cmp(void const *one, void const *two)
342 indexed_modcallable const *a = one;
343 indexed_modcallable const *b = two;
345 if (a->comp < b->comp) return -1;
346 if (a->comp > b->comp) return +1;
348 return a->idx - b->idx;
353 * Compare two module entries
355 static int module_instance_cmp(void const *one, void const *two)
357 module_instance_t const *a = one;
358 module_instance_t const *b = two;
360 return strcmp(a->name, b->name);
364 static void module_instance_free_old(UNUSED CONF_SECTION *cs, module_instance_t *node, time_t when)
366 fr_module_hup_t *mh, **last;
369 * Walk the list, freeing up old instances.
376 * Free only every 60 seconds.
378 if ((when - mh->when) < 60) {
383 talloc_free(mh->insthandle);
392 * Free a module instance.
394 static void module_instance_free(void *data)
396 module_instance_t *module = talloc_get_type_abort(data, module_instance_t);
398 module_instance_free_old(module->cs, module, time(NULL) + 100);
400 #ifdef HAVE_PTHREAD_H
404 * The mutex MIGHT be locked...
405 * we'll check for that later, I guess.
407 pthread_mutex_destroy(module->mutex);
408 talloc_free(module->mutex);
413 * Remove any registered paircompares.
415 paircompare_unregister_instance(module->insthandle);
417 xlat_unregister(module->name, NULL, module->insthandle);
419 * Remove all xlat's registered to module instance.
421 if (module->insthandle) xlat_unregister_module(module->insthandle);
427 * Compare two module entries
429 static int module_entry_cmp(void const *one, void const *two)
431 module_entry_t const *a = one;
432 module_entry_t const *b = two;
434 return strcmp(a->name, b->name);
438 * Free a module entry.
440 static int _module_entry_free(module_entry_t *this)
444 * Don't dlclose() modules if we're doing memory
445 * debugging. This removes the symbols needed by
448 if (!main_config.debug_memory)
450 dlclose(this->handle); /* ignore any errors */
456 * Remove the module lists.
458 int modules_free(void)
460 rbtree_free(instance_tree);
461 rbtree_free(module_tree);
470 static module_entry_t *module_dlopen(CONF_SECTION *cs, char const *module_name)
472 module_entry_t myentry;
473 module_entry_t *node;
475 module_t const *module;
477 strlcpy(myentry.name, module_name, sizeof(myentry.name));
478 node = rbtree_finddata(module_tree, &myentry);
479 if (node) return node;
482 * Link to the module's rlm_FOO{} structure, the same as
486 #if !defined(WITH_LIBLTDL) && defined(HAVE_DLFCN_H) && defined(RTLD_SELF)
487 module = dlsym(RTLD_SELF, module_name);
488 if (module) goto open_self;
492 * Keep the handle around so we can dlclose() it.
494 handle = lt_dlopenext(module_name);
496 cf_log_err_cs(cs, "Failed to link to module '%s': %s", module_name, fr_strerror());
500 DEBUG3("Loaded %s, checking if it's valid", module_name);
502 module = dlsym(handle, module_name);
504 cf_log_err_cs(cs, "Failed linking to %s structure: %s", module_name, dlerror());
509 #if !defined(WITH_LIBLTDL) && defined (HAVE_DLFCN_H) && defined(RTLD_SELF)
513 * Before doing anything else, check if it's sane.
515 if (check_module_magic(cs, module) < 0) {
520 /* make room for the module type */
521 node = talloc_zero(cs, module_entry_t);
522 talloc_set_destructor(node, _module_entry_free);
523 strlcpy(node->name, module_name, sizeof(node->name));
524 node->module = module;
525 node->handle = handle;
527 cf_log_module(cs, "Loaded module %s", module_name);
530 * Add the module as "rlm_foo-version" to the configuration
533 if (!rbtree_insert(module_tree, node)) {
534 ERROR("Failed to cache module %s", module_name);
543 /** Parse module's configuration section and setup destructors
546 static int module_conf_parse(module_instance_t *node, void **handle)
551 * If there is supposed to be instance data, allocate it now.
552 * Also parse the configuration data, if required.
554 if (node->entry->module->inst_size) {
555 *handle = talloc_zero_array(node, uint8_t, node->entry->module->inst_size);
558 talloc_set_name(*handle, "rlm_%s_t",
559 node->entry->module->name ? node->entry->module->name : "config");
561 if (node->entry->module->config &&
562 (cf_section_parse(node->cs, *handle, node->entry->module->config) < 0)) {
563 cf_log_err_cs(node->cs,"Invalid configuration for module \"%s\"", node->name);
564 talloc_free(*handle);
570 * Set the destructor.
572 if (node->entry->module->detach) {
573 talloc_set_destructor(*handle, node->entry->module->detach);
580 /** Bootstrap a module.
582 * Load the module shared library, allocate instance memory for it,
583 * parse the module configuration, and call the modules "bootstrap" method.
585 static module_instance_t *module_bootstrap(CONF_SECTION *cs)
587 char const *name1, *name2;
588 module_instance_t *node, myNode;
589 char module_name[256];
592 * Figure out which module we want to load.
594 name1 = cf_section_name1(cs);
595 name2 = cf_section_name2(cs);
596 if (!name2) name2 = name1;
598 strlcpy(myNode.name, name2, sizeof(myNode.name));
601 * See if the module already exists.
603 node = rbtree_finddata(instance_tree, &myNode);
605 ERROR("Duplicate module \"%s %s\", in file %s:%d and file %s:%d",
607 cf_section_filename(cs),
608 cf_section_lineno(cs),
609 cf_section_filename(node->cs),
610 cf_section_lineno(node->cs));
615 * Hang the node struct off of the configuration
616 * section. If the CS is free'd the instance will be
619 node = talloc_zero(cs, module_instance_t);
621 strlcpy(node->name, name2, sizeof(node->name));
624 * Names in the "modules" section aren't prefixed
625 * with "rlm_", so we add it here.
627 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
630 * Load the module shared library.
632 node->entry = module_dlopen(cs, module_name);
638 cf_log_module(cs, "Loading module \"%s\" from file %s", node->name,
639 cf_section_filename(cs));
642 * Parse the modules configuration.
644 if (module_conf_parse(node, &node->insthandle) < 0) {
650 * Bootstrap the module.
652 if (node->entry->module->bootstrap &&
653 ((node->entry->module->bootstrap)(cs, node->insthandle) < 0)) {
654 cf_log_err_cs(cs, "Instantiation failed for module \"%s\"", node->name);
660 * Remember the module for later.
662 rbtree_insert(instance_tree, node);
668 /** Find an existing module instance.
671 module_instance_t *module_find(CONF_SECTION *modules, char const *askedname)
673 char const *instname;
674 module_instance_t myNode;
676 if (!modules) return NULL;
679 * Look for the real name. Ignore the first character,
680 * which tells the server "it's OK for this module to not
683 instname = askedname;
684 if (instname[0] == '-') instname++;
686 strlcpy(myNode.name, instname, sizeof(myNode.name));
688 return rbtree_finddata(instance_tree, &myNode);
692 /** Load a module, and instantiate it.
695 module_instance_t *module_instantiate(CONF_SECTION *modules, char const *askedname)
698 module_instance_t *node;
701 * Find the module. If it's not there, do nothing.
703 node = module_find(modules, askedname);
705 ERROR("Cannot find a configuration entry for module \"%s\"", askedname);
710 * The module is already instantiated. Return it.
712 if (node->instantiated) return node;
715 * Now that ALL modules are instantiated, and ALL xlats
716 * are defined, go compile the config items marked as XLAT.
718 if (node->entry->module->config &&
719 (cf_section_parse_pass2(node->cs, node->insthandle,
720 node->entry->module->config) < 0)) {
725 * Call the instantiate method, if any.
727 if (node->entry->module->instantiate) {
728 cf_log_module(node->cs, "Instantiating module \"%s\" from file %s", node->name,
729 cf_section_filename(node->cs));
732 * Call the module's instantiation routine.
734 if ((node->entry->module->instantiate)(node->cs, node->insthandle) < 0) {
735 cf_log_err_cs(node->cs, "Instantiation failed for module \"%s\"", node->name);
741 #ifdef HAVE_PTHREAD_H
743 * If we're threaded, check if the module is thread-safe.
745 * If it isn't, we create a mutex.
747 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
748 node->mutex = talloc_zero(node, pthread_mutex_t);
751 * Initialize the mutex.
753 pthread_mutex_init(node->mutex, NULL);
757 node->instantiated = true;
762 /** Resolve polymorphic item's from a module's CONF_SECTION to a subsection in another module
764 * This allows certain module sections to reference module sections in other instances
765 * of the same module and share CONF_DATA associated with them.
779 * @param out where to write the pointer to a module's config section. May be NULL on success, indicating the config
780 * item was not found within the module CONF_SECTION, or the chain of module references was followed and the
781 * module at the end of the chain did not a subsection.
782 * @param module CONF_SECTION.
783 * @param name of the polymorphic sub-section.
784 * @return 0 on success with referenced section, 1 on success with local section, or -1 on failure.
786 int find_module_sibling_section(CONF_SECTION **out, CONF_SECTION *module, char const *name)
788 static bool loop = true; /* not used, we just need a valid pointer to quiet static analysis */
793 module_instance_t *inst;
794 char const *inst_name;
796 #define FIND_SIBLING_CF_KEY "find_sibling"
801 * Is a real section (not referencing sibling module).
803 cs = cf_section_sub_find(module, name);
811 * Item omitted completely from module config.
813 cp = cf_pair_find(module, name);
816 if (cf_data_find(module, FIND_SIBLING_CF_KEY)) {
817 cf_log_err_cp(cp, "Module reference loop found");
821 cf_data_add(module, FIND_SIBLING_CF_KEY, &loop, NULL);
824 * Item found, resolve it to a module instance.
825 * This triggers module loading, so we don't have
826 * instantiation order issues.
828 inst_name = cf_pair_value(cp);
829 inst = module_instantiate(cf_item_parent(cf_section_to_item(module)), inst_name);
832 * Remove the config data we added for loop
835 cf_data_remove(module, FIND_SIBLING_CF_KEY);
837 cf_log_err_cp(cp, "Unknown module instance \"%s\"", inst_name);
843 * Check the module instances are of the same type.
845 if (strcmp(cf_section_name1(inst->cs), cf_section_name1(module)) != 0) {
846 cf_log_err_cp(cp, "Referenced module is a rlm_%s instance, must be a rlm_%s instance",
847 cf_section_name1(inst->cs), cf_section_name1(module));
852 *out = cf_section_sub_find(inst->cs, name);
857 static indexed_modcallable *lookup_by_index(rbtree_t *components,
858 rlm_components_t comp, int idx)
860 indexed_modcallable myc;
865 return rbtree_finddata(components, &myc);
869 * Create a new sublist.
871 static indexed_modcallable *new_sublist(CONF_SECTION *cs,
872 rbtree_t *components, rlm_components_t comp, int idx)
874 indexed_modcallable *c;
876 c = lookup_by_index(components, comp, idx);
878 /* It is an error to try to create a sublist that already
879 * exists. It would almost certainly be caused by accidental
880 * duplication in the config file.
882 * index 0 is the exception, because it is used when we want
883 * to collect _all_ listed modules under a single index by
884 * default, which is currently the case in all components
885 * except authenticate. */
893 c = talloc_zero(cs, indexed_modcallable);
894 c->modulelist = NULL;
898 if (!rbtree_insert(components, c)) {
906 rlm_rcode_t indexed_modcall(rlm_components_t comp, int idx, REQUEST *request)
909 modcallable *list = NULL;
910 virtual_server_t *server;
913 * Hack to find the correct virtual server.
915 server = virtual_server_find(request->server);
917 RDEBUG("No such virtual server \"%s\"", request->server);
918 return RLM_MODULE_FAIL;
922 list = server->mc[comp];
923 if (!list) RDEBUG3("Empty %s section. Using default return values.", section_type_value[comp].section);
926 indexed_modcallable *this;
928 this = lookup_by_index(server->components, comp, idx);
930 list = this->modulelist;
932 RDEBUG2("%s sub-section not found. Ignoring.", section_type_value[comp].typename);
936 if (server->subcs[comp]) {
938 RDEBUG("# Executing section %s from file %s",
939 section_type_value[comp].section,
940 cf_section_filename(server->subcs[comp]));
942 RDEBUG("# Executing group from file %s",
943 cf_section_filename(server->subcs[comp]));
946 request->component = section_type_value[comp].section;
948 rcode = modcall(comp, list, request);
950 request->module = "";
951 request->component = "<core>";
956 * Load a sub-module list, as found inside an Auth-Type foo {}
959 static int load_subcomponent_section(CONF_SECTION *cs,
960 rbtree_t *components,
961 DICT_ATTR const *da, rlm_components_t comp)
963 indexed_modcallable *subcomp;
966 char const *name2 = cf_section_name2(cs);
978 ml = compile_modgroup(NULL, comp, cs);
984 * We must assign a numeric index to this subcomponent.
985 * It is generated and placed in the dictionary
986 * automatically. If it isn't found, it's a serious
989 dval = dict_valbyname(da->attr, da->vendor, name2);
993 "The %s attribute has no VALUE defined for %s",
994 section_type_value[comp].typename, name2);
998 subcomp = new_sublist(cs, components, comp, dval->value);
1005 * Link it into the talloc hierarchy.
1007 subcomp->modulelist = talloc_steal(subcomp, ml);
1012 * Don't complain too often.
1014 #define MAX_IGNORED (32)
1015 static int last_ignored = -1;
1016 static char const *ignored[MAX_IGNORED];
1018 static int load_component_section(CONF_SECTION *cs,
1019 rbtree_t *components, rlm_components_t comp)
1024 indexed_modcallable *subcomp;
1025 char const *modname;
1026 DICT_ATTR const *da;
1029 * Find the attribute used to store VALUEs for this section.
1031 da = dict_attrbyvalue(section_type_value[comp].attr, 0);
1034 "No such attribute %s",
1035 section_type_value[comp].typename);
1040 * Loop over the entries in the named section, loading
1041 * the sections this time.
1043 for (modref = cf_item_find_next(cs, NULL);
1045 modref = cf_item_find_next(cs, modref)) {
1047 CONF_PAIR *cp = NULL;
1048 CONF_SECTION *scs = NULL;
1050 if (cf_item_is_section(modref)) {
1051 scs = cf_item_to_section(modref);
1053 name1 = cf_section_name1(scs);
1056 section_type_value[comp].typename) == 0) {
1057 if (!load_subcomponent_section(scs,
1062 return -1; /* FIXME: memleak? */
1069 } else if (cf_item_is_pair(modref)) {
1070 cp = cf_item_to_pair(modref);
1073 continue; /* ignore it */
1077 * Look for Auth-Type foo {}, which are special
1078 * cases of named sections, and allowable ONLY
1081 * i.e. They're not allowed in a "group" or "redundant"
1084 if (comp == MOD_AUTHENTICATE) {
1086 char const *modrefname = NULL;
1088 modrefname = cf_pair_attr(cp);
1090 modrefname = cf_section_name2(scs);
1093 "Errors parsing %s sub-section.\n",
1094 cf_section_name1(scs));
1099 dval = dict_valbyname(PW_AUTH_TYPE, 0, modrefname);
1102 * It's a section, but nothing we
1106 "Unknown Auth-Type \"%s\" in %s sub-section.",
1107 modrefname, section_type_value[comp].section);
1112 /* See the comment in new_sublist() for explanation
1113 * of the special index 0 */
1117 subcomp = new_sublist(cs, components, comp, idx);
1118 if (!subcomp) continue;
1121 * Try to compile one entry.
1123 this = compile_modsingle(subcomp, &subcomp->modulelist, comp, modref, &modname);
1126 * It's OK for the module to not exist.
1128 if (!this && modname && (modname[0] == '-')) {
1131 if (last_ignored < 0) {
1134 ignored[last_ignored] = modname;
1137 WARN("Ignoring \"%s\" (see raddb/mods-available/README.rst)", modname + 1);
1141 if (last_ignored >= MAX_IGNORED) goto complain;
1143 for (i = 0; i <= last_ignored; i++) {
1144 if (strcmp(ignored[i], modname) == 0) {
1149 if (i > last_ignored) goto save_complain;
1155 "Errors parsing %s section.\n",
1156 cf_section_name1(cs));
1160 if (rad_debug_lvl > 2) modcall_debug(this, 2);
1162 add_to_modcallable(subcomp->modulelist, this);
1169 static int load_byserver(CONF_SECTION *cs)
1171 rlm_components_t comp, found;
1172 char const *name = cf_section_name2(cs);
1173 rbtree_t *components;
1174 virtual_server_t *server = NULL;
1175 indexed_modcallable *c;
1179 cf_log_info(cs, "server %s { # from file %s",
1180 name, cf_section_filename(cs));
1182 cf_log_info(cs, "server { # from file %s",
1183 cf_section_filename(cs));
1186 is_bare = (cf_item_parent(cf_section_to_item(cs)) == NULL);
1188 server = talloc_zero(cs, virtual_server_t);
1189 server->name = name;
1190 server->created = time(NULL);
1192 server->components = components = rbtree_create(server, indexed_modcallable_cmp, NULL, 0);
1194 ERROR("Failed to initialize components");
1197 if (rad_debug_lvl == 0) {
1198 ERROR("Failed to load virtual server %s",
1199 (name != NULL) ? name : "<default>");
1203 talloc_set_destructor(server, _virtual_server_free);
1206 * Loop over all of the known components, finding their
1207 * configuration section, and loading it.
1210 for (comp = 0; comp < MOD_COUNT; ++comp) {
1211 CONF_SECTION *subcs;
1213 subcs = cf_section_sub_find(cs,
1214 section_type_value[comp].section);
1215 if (!subcs) continue;
1218 cf_log_err_cs(subcs, "The %s section should be inside of a 'server { ... }' block!",
1219 section_type_value[comp].section);
1222 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1225 * Skip pre/post-proxy sections if we're not
1230 !main_config.proxy_requests &&
1232 ((comp == MOD_PRE_PROXY) ||
1233 (comp == MOD_POST_PROXY))) {
1237 #ifndef WITH_ACCOUNTING
1238 if (comp == MOD_ACCOUNTING) continue;
1241 #ifndef WITH_SESSION_MGMT
1242 if (comp == MOD_SESSION) continue;
1245 if (rad_debug_lvl <= 3) {
1246 cf_log_module(cs, "Loading %s {...}",
1247 section_type_value[comp].section);
1249 DEBUG(" %s {", section_type_value[comp].section);
1252 if (load_component_section(subcs, components, comp) < 0) {
1256 if (rad_debug_lvl > 3) {
1257 DEBUG(" } # %s", section_type_value[comp].section);
1261 * Cache a default, if it exists. Some people
1262 * put empty sections for some reason...
1264 c = lookup_by_index(components, comp, 0);
1265 if (c) server->mc[comp] = c->modulelist;
1267 server->subcs[comp] = subcs;
1270 } /* loop over components */
1273 * We haven't loaded any of the normal sections. Maybe we're
1274 * supposed to load the vmps section.
1276 * This is a bit of a hack...
1279 #if defined(WITH_VMPS) || defined(WITH_DHCP)
1280 CONF_SECTION *subcs;
1283 DICT_ATTR const *da;
1287 subcs = cf_section_sub_find(cs, "vmps");
1289 cf_log_module(cs, "Loading vmps {...}");
1290 if (load_component_section(subcs, components,
1291 MOD_POST_AUTH) < 0) {
1294 c = lookup_by_index(components,
1296 if (c) server->mc[MOD_POST_AUTH] = c->modulelist;
1303 * It's OK to not have DHCP.
1305 subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1308 da = dict_attrbyname("DHCP-Message-Type");
1311 * Handle each DHCP Message type separately.
1314 char const *name2 = cf_section_name2(subcs);
1317 cf_log_module(cs, "Loading dhcp %s {...}", name2);
1319 cf_log_module(cs, "Loading dhcp {...}");
1321 if (!load_subcomponent_section(subcs,
1325 goto error; /* FIXME: memleak? */
1327 c = lookup_by_index(components,
1329 if (c) server->mc[MOD_POST_AUTH] = c->modulelist;
1331 subcs = cf_subsection_find_next(cs, subcs, "dhcp");
1337 cf_log_info(cs, "} # server %s", name);
1339 cf_log_info(cs, "} # server");
1342 if (rad_debug_lvl == 0) {
1343 INFO("Loaded virtual server %s",
1344 (name != NULL) ? name : "<default>");
1348 * Now that it is OK, insert it into the list.
1350 * This is thread-safe...
1352 comp = virtual_server_idx(name);
1353 server->next = virtual_servers[comp];
1354 virtual_servers[comp] = server;
1357 * Mark OLDER ones of the same name as being unused.
1359 server = server->next;
1361 if ((!name && !server->name) ||
1362 (name && server->name &&
1363 (strcmp(server->name, name) == 0))) {
1364 server->can_free = true;
1367 server = server->next;
1374 static int pass2_cb(UNUSED void *ctx, void *data)
1376 indexed_modcallable *this = data;
1378 if (!modcall_pass2(this->modulelist)) return -1;
1385 * Load all of the virtual servers.
1387 int virtual_servers_load(CONF_SECTION *config)
1390 virtual_server_t *server;
1391 static bool first_time = true;
1393 DEBUG2("%s: #### Loading Virtual Servers ####", main_config.name);
1396 * If we have "server { ...}", then there SHOULD NOT be
1397 * bare "authorize", etc. sections. if there is no such
1398 * server, then try to load the old-style sections first.
1400 * In either case, load the "default" virtual server first.
1401 * this matches better with users expectations.
1403 cs = cf_section_find_name2(cf_subsection_find_next(config, NULL,
1407 if (load_byserver(cs) < 0) {
1411 if (load_byserver(config) < 0) {
1417 * Load all of the virtual servers.
1419 for (cs = cf_subsection_find_next(config, NULL, "server");
1421 cs = cf_subsection_find_next(config, cs, "server")) {
1424 name2 = cf_section_name2(cs);
1425 if (!name2) continue; /* handled above */
1427 server = virtual_server_find(name2);
1429 (cf_top_section(server->cs) == config)) {
1430 ERROR("Duplicate virtual server \"%s\" in file %s:%d and file %s:%d",
1432 cf_section_filename(server->cs),
1433 cf_section_lineno(server->cs),
1434 cf_section_filename(cs),
1435 cf_section_lineno(cs));
1439 if (load_byserver(cs) < 0) {
1441 * Once we successfully started once,
1442 * continue loading the OTHER servers,
1443 * even if one fails.
1445 if (!first_time) continue;
1451 * Try to compile the "authorize", etc. sections which
1452 * aren't in a virtual server.
1454 server = virtual_server_find(NULL);
1458 for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) {
1459 if (!modcall_pass2(server->mc[i])) return -1;
1462 if (server->components &&
1463 (rbtree_walk(server->components, RBTREE_IN_ORDER,
1464 pass2_cb, NULL) != 0)) {
1470 * Now that we've loaded everything, run pass 2 over the
1471 * conditions and xlats.
1473 for (cs = cf_subsection_find_next(config, NULL, "server");
1475 cs = cf_subsection_find_next(config, cs, "server")) {
1479 name2 = cf_section_name2(cs);
1481 server = virtual_server_find(name2);
1482 if (!server) continue;
1484 for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) {
1485 if (!modcall_pass2(server->mc[i])) return -1;
1488 if (server->components &&
1489 (rbtree_walk(server->components, RBTREE_IN_ORDER,
1490 pass2_cb, NULL) != 0)) {
1496 * If we succeed the first time around, remember that.
1503 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1506 fr_module_hup_t *mh;
1509 node->entry->module->bootstrap ||
1510 !node->entry->module->instantiate ||
1511 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1515 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1518 * Parse the module configuration, and setup destructors so the
1519 * module's detach method is called when it's instance data is
1520 * about to be freed.
1522 if (module_conf_parse(node, &insthandle) < 0) {
1523 cf_log_err_cs(cs, "HUP failed for module \"%s\" (parsing config failed). "
1524 "Using old configuration", node->name);
1529 if ((node->entry->module->instantiate)(cs, insthandle) < 0) {
1530 cf_log_err_cs(cs, "HUP failed for module \"%s\". Using old configuration.", node->name);
1531 talloc_free(insthandle);
1536 INFO(" Module: Reloaded module \"%s\"", node->name);
1538 module_instance_free_old(cs, node, when);
1541 * Save the old instance handle for later deletion.
1543 mh = talloc_zero(cs, fr_module_hup_t);
1546 mh->insthandle = node->insthandle;
1547 mh->next = node->mh;
1551 * Replace the instance handle while the module is running.
1553 node->insthandle = insthandle;
1556 * FIXME: Set a timeout to come back in 60s, so that
1557 * we can pro-actively clean up the old instances.
1564 int modules_hup(CONF_SECTION *modules)
1569 module_instance_t *node;
1571 if (!modules) return 0;
1576 * Loop over the modules
1578 for (ci=cf_item_find_next(modules, NULL);
1580 ci=cf_item_find_next(modules, ci)) {
1581 char const *instname;
1582 module_instance_t myNode;
1585 * If it's not a section, ignore it.
1587 if (!cf_item_is_section(ci)) continue;
1589 cs = cf_item_to_section(ci);
1590 instname = cf_section_name2(cs);
1591 if (!instname) instname = cf_section_name1(cs);
1593 strlcpy(myNode.name, instname, sizeof(myNode.name));
1594 node = rbtree_finddata(instance_tree, &myNode);
1596 module_hup_module(cs, node, when);
1603 static int define_type(CONF_SECTION *cs, DICT_ATTR const *da, char const *name)
1609 * If the value already exists, don't
1612 dval = dict_valbyname(da->attr, da->vendor, name);
1614 if (dval->value == 0) {
1615 ERROR("The dictionaries must not define VALUE %s %s 0",
1623 * Create a new unique value with a
1624 * meaningless number. You can't look at
1625 * it from outside of this code, so it
1626 * doesn't matter. The only requirement
1627 * is that it's unique.
1630 value = (fr_rand() & 0x00ffffff) + 1;
1631 } while (dict_valbyattr(da->attr, da->vendor, value));
1633 cf_log_module(cs, "Creating %s = %s", da->name, name);
1634 if (dict_addvalue(name, da->name, value) < 0) {
1635 ERROR("%s", fr_strerror());
1643 * Define Auth-Type, etc. in a server.
1645 static bool server_define_types(CONF_SECTION *cs)
1647 rlm_components_t comp;
1650 * Loop over all of the components
1652 for (comp = 0; comp < MOD_COUNT; ++comp) {
1653 CONF_SECTION *subcs;
1655 DICT_ATTR const *da;
1657 subcs = cf_section_sub_find(cs,
1658 section_type_value[comp].section);
1659 if (!subcs) continue;
1661 if (cf_item_find_next(subcs, NULL) == NULL) continue;
1664 * Find the attribute used to store VALUEs for this section.
1666 da = dict_attrbyvalue(section_type_value[comp].attr, 0);
1668 cf_log_err_cs(subcs,
1669 "No such attribute %s",
1670 section_type_value[comp].typename);
1675 * Define dynamic types, so that others can reference
1678 for (modref = cf_item_find_next(subcs, NULL);
1680 modref = cf_item_find_next(subcs, modref)) {
1682 CONF_SECTION *subsubcs;
1685 * Create types for simple references
1686 * only when parsing the authenticate
1689 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
1690 cf_item_is_pair(modref)) {
1691 CONF_PAIR *cp = cf_item_to_pair(modref);
1692 if (!define_type(cs, da, cf_pair_attr(cp))) {
1699 if (!cf_item_is_section(modref)) continue;
1701 subsubcs = cf_item_to_section(modref);
1702 name1 = cf_section_name1(subsubcs);
1704 if (strcmp(name1, section_type_value[comp].typename) == 0) {
1705 if (!define_type(cs, da,
1706 cf_section_name2(subsubcs))) {
1711 } /* loop over components */
1716 extern char const *unlang_keyword[];
1718 static bool is_reserved_word(const char *name)
1722 if (!name || !*name) return false;
1724 for (i = 1; unlang_keyword[i] != NULL; i++) {
1725 if (strcmp(name, unlang_keyword[i]) == 0) return true;
1733 * Parse the module config sections, and load
1734 * and call each module's init() function.
1736 int modules_init(CONF_SECTION *config)
1738 CONF_ITEM *ci, *next;
1739 CONF_SECTION *cs, *modules;
1742 * Set up the internal module struct.
1744 module_tree = rbtree_create(NULL, module_entry_cmp, NULL, 0);
1746 ERROR("Failed to initialize modules\n");
1750 instance_tree = rbtree_create(NULL, module_instance_cmp,
1751 module_instance_free, 0);
1752 if (!instance_tree) {
1753 ERROR("Failed to initialize modules\n");
1757 memset(virtual_servers, 0, sizeof(virtual_servers));
1760 * Remember where the modules were stored.
1762 modules = cf_section_sub_find(config, "modules");
1764 WARN("Cannot find a \"modules\" section in the configuration file!");
1768 * Load dictionaries.
1770 for (cs = cf_subsection_find_next(config, NULL, "server");
1772 cs = cf_subsection_find_next(config, cs, "server")) {
1773 CONF_SECTION *subcs;
1774 DICT_ATTR const *da;
1778 * Auto-load the VMPS/VQP dictionary.
1780 subcs = cf_section_sub_find(cs, "vmps");
1782 da = dict_attrbyname("VQP-Packet-Type");
1784 if (dict_read(main_config.dictionary_dir, "dictionary.vqp") < 0) {
1785 ERROR("Failed reading dictionary.vqp: %s",
1789 cf_log_module(cs, "Loading dictionary.vqp");
1791 da = dict_attrbyname("VQP-Packet-Type");
1793 ERROR("No VQP-Packet-Type in dictionary.vqp");
1802 * Auto-load the DHCP dictionary.
1804 subcs = cf_subsection_find_next(cs, NULL, "dhcp");
1806 da = dict_attrbyname("DHCP-Message-Type");
1808 cf_log_module(cs, "Loading dictionary.dhcp");
1809 if (dict_read(main_config.dictionary_dir, "dictionary.dhcp") < 0) {
1810 ERROR("Failed reading dictionary.dhcp: %s",
1815 da = dict_attrbyname("DHCP-Message-Type");
1817 ERROR("No DHCP-Message-Type in dictionary.dhcp");
1824 * Else it's a RADIUS virtual server, and the
1825 * dictionaries are already loaded.
1829 * Root through each virtual server, defining
1830 * Autz-Type and Auth-Type. This is so that the
1831 * modules can reference a particular type.
1833 if (!server_define_types(cs)) return -1;
1836 DEBUG2("%s: #### Instantiating modules ####", main_config.name);
1839 * Loop over module definitions, looking for duplicates.
1841 * This is O(N^2) in the number of modules, but most
1842 * systems should have less than 100 modules.
1844 for (ci = cf_item_find_next(modules, NULL);
1848 CONF_SECTION *subcs;
1849 module_instance_t *node;
1851 next = cf_item_find_next(modules, ci);
1853 if (!cf_item_is_section(ci)) continue;
1855 subcs = cf_item_to_section(ci);
1857 node = module_bootstrap(subcs);
1858 if (!node) return -1;
1860 if (!next || !cf_item_is_section(next)) continue;
1862 name1 = cf_section_name1(subcs);
1864 if (is_reserved_word(name1)) {
1865 cf_log_err_cs(subcs, "Module cannot be named for an 'unlang' keyword");
1871 * Look for the 'instantiate' section, which tells us
1872 * the instantiation order of the modules, and also allows
1873 * us to load modules with no authorize/authenticate/etc.
1876 cs = cf_section_sub_find(config, "instantiate");
1879 module_instance_t *module;
1882 cf_log_info(cs, " instantiate {");
1885 * Loop over the items in the 'instantiate' section.
1887 for (ci=cf_item_find_next(cs, NULL);
1889 ci=cf_item_find_next(cs, ci)) {
1892 * Skip sections and "other" stuff.
1893 * Sections will be handled later, if
1894 * they're referenced at all...
1896 if (cf_item_is_pair(ci)) {
1897 cp = cf_item_to_pair(ci);
1898 name = cf_pair_attr(cp);
1900 module = module_instantiate(modules, name);
1901 if (!module && (name[0] != '-')) {
1907 * Can only be "redundant" or
1909 * "redundant-load-balance"
1911 if (cf_item_is_section(ci)) {
1912 bool all_same = true;
1913 module_t const *last = NULL;
1914 CONF_SECTION *subcs;
1917 subcs = cf_item_to_section(ci);
1918 name = cf_section_name1(subcs);
1921 * Groups, etc. must have a name.
1923 if (((strcmp(name, "group") == 0) ||
1924 (strcmp(name, "redundant") == 0) ||
1925 (strcmp(name, "redundant-load-balance") == 0) ||
1926 strcmp(name, "load-balance") == 0)) {
1927 name = cf_section_name2(subcs);
1929 cf_log_err_cs(subcs, "Subsection must have a name");
1933 if (is_reserved_word(name)) {
1934 cf_log_err_cs(subcs, "Instantiate sections cannot be named for an 'unlang' keyword");
1938 if (is_reserved_word(name)) {
1939 cf_log_err_cs(subcs, "Instantiate sections cannot be named for an 'unlang' keyword");
1945 * Ensure that the modules we reference here exist.
1947 for (subci=cf_item_find_next(subcs, NULL);
1949 subci=cf_item_find_next(subcs, subci)) {
1950 if (cf_item_is_pair(subci)) {
1951 cp = cf_item_to_pair(subci);
1952 if (cf_pair_value(cp)) {
1953 cf_log_err(subci, "Cannot set return codes in a %s block",
1954 cf_section_name1(subcs));
1958 module = module_instantiate(modules, cf_pair_attr(cp));
1965 last = module->entry->module;
1966 } else if (last != module->entry->module) {
1976 * Don't check subsections for now.
1978 } /* loop over modules in a "redundant foo" section */
1981 * Register a redundant xlat
1984 if (!xlat_register_redundant(cf_item_to_section(ci))) {
1985 WARN("%s[%d] Not registering expansions for %s",
1986 cf_section_filename(subcs), cf_section_lineno(subcs),
1987 cf_section_name2(subcs));
1990 } /* handle subsections */
1991 } /* loop over the "instantiate" section */
1993 cf_log_info(cs, " }");
1994 } /* if there's an 'instantiate' section. */
1997 * Now that we've loaded the explicitly ordered modules,
1998 * load everything in the "modules" section. This is
1999 * because we've now split up the modules into
2002 cf_log_info(cs, " modules {");
2003 for (ci=cf_item_find_next(modules, NULL);
2007 module_instance_t *module;
2008 CONF_SECTION *subcs;
2010 next = cf_item_find_next(modules, ci);
2012 if (!cf_item_is_section(ci)) continue;
2014 subcs = cf_item_to_section(ci);
2015 name = cf_section_name2(subcs);
2016 if (!name) name = cf_section_name1(subcs);
2018 module = module_instantiate(modules, name);
2019 if (!module) return -1;
2021 cf_log_info(cs, " } # modules");
2023 if (virtual_servers_load(config) < 0) return -1;
2029 * Call all authorization modules until one returns
2030 * somethings else than RLM_MODULE_OK
2032 rlm_rcode_t process_authorize(int autz_type, REQUEST *request)
2034 return indexed_modcall(MOD_AUTHORIZE, autz_type, request);
2038 * Authenticate a user/password with various methods.
2040 rlm_rcode_t process_authenticate(int auth_type, REQUEST *request)
2042 return indexed_modcall(MOD_AUTHENTICATE, auth_type, request);
2045 #ifdef WITH_ACCOUNTING
2047 * Do pre-accounting for ALL configured sessions
2049 rlm_rcode_t module_preacct(REQUEST *request)
2051 return indexed_modcall(MOD_PREACCT, 0, request);
2055 * Do accounting for ALL configured sessions
2057 rlm_rcode_t process_accounting(int acct_type, REQUEST *request)
2059 return indexed_modcall(MOD_ACCOUNTING, acct_type, request);
2063 #ifdef WITH_SESSION_MGMT
2065 * See if a user is already logged in.
2067 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
2069 int process_checksimul(int sess_type, REQUEST *request, int maxsimul)
2073 if(!request->username)
2076 request->simul_count = 0;
2077 request->simul_max = maxsimul;
2078 request->simul_mpp = 1;
2080 rcode = indexed_modcall(MOD_SESSION, sess_type, request);
2082 if (rcode != RLM_MODULE_OK) {
2083 /* FIXME: Good spot for a *rate-limited* warning to the log */
2087 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
2093 * Do pre-proxying for ALL configured sessions
2095 rlm_rcode_t process_pre_proxy(int type, REQUEST *request)
2097 return indexed_modcall(MOD_PRE_PROXY, type, request);
2101 * Do post-proxying for ALL configured sessions
2103 rlm_rcode_t process_post_proxy(int type, REQUEST *request)
2105 return indexed_modcall(MOD_POST_PROXY, type, request);
2110 * Do post-authentication for ALL configured sessions
2112 rlm_rcode_t process_post_auth(int postauth_type, REQUEST *request)
2114 return indexed_modcall(MOD_POST_AUTH, postauth_type, request);
2118 rlm_rcode_t process_recv_coa(int recv_coa_type, REQUEST *request)
2120 return indexed_modcall(MOD_RECV_COA, recv_coa_type, request);
2123 rlm_rcode_t process_send_coa(int send_coa_type, REQUEST *request)
2125 return indexed_modcall(MOD_SEND_COA, send_coa_type, request);