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
97 typedef struct lt_dlmodule_t {
103 * Define modules here.
105 extern module_t rlm_pap;
106 extern module_t rlm_chap;
107 extern module_t rlm_eap;
110 * EAP structures are defined elsewhere.
112 typedef struct eap_type_t EAP_TYPE;
115 * And so on for other EAP types.
117 extern EAP_TYPE rlm_eap_md5;
119 static const lt_dlmodule_t lt_dlmodules[] = {
120 { "rlm_pap", &rlm_pap },
121 { "rlm_chap", &rlm_chap },
122 { "rlm_eap", &rlm_eap },
123 { "rlm_eap_md5", &rlm_eap_md5 },
126 * Add other modules here.
133 lt_dlhandle lt_dlopenext(const char *name)
137 for (i = 0; lt_dlmodules[i].name != NULL; i++) {
138 if (strcmp(name, lt_dlmodules[i].name) == 0) {
139 return lt_dlmodules[i].ref;
146 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
150 #endif /* WITHOUT_LIBLTDL */
152 static int virtual_server_idx(const char *name)
158 hash = fr_hash_string(name);
160 return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
163 static void virtual_server_free(virtual_server_t *server)
167 if (server->components) rbtree_free(server->components);
168 server->components = NULL;
173 void virtual_servers_free(time_t when)
176 virtual_server_t **last;
178 for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
179 virtual_server_t *server, *next;
181 last = &virtual_servers[i];
182 for (server = virtual_servers[i];
188 * If we delete it, fix the links so that
189 * we don't orphan anything. Also,
190 * delete it if it's old, AND a newer one
193 * Otherwise, the last pointer gets set to
194 * the one we didn't delete.
197 ((server->created < when) && server->can_free)) {
198 *last = server->next;
199 virtual_server_free(server);
201 last = &(server->next);
207 static void indexed_modcallable_free(void *data)
209 indexed_modcallable *c = data;
211 modcallable_free(&c->modulelist);
215 static int indexed_modcallable_cmp(const void *one, const void *two)
217 const indexed_modcallable *a = one;
218 const indexed_modcallable *b = two;
220 if (a->comp < b->comp) return -1;
221 if (a->comp > b->comp) return +1;
223 return a->idx - b->idx;
228 * Compare two module entries
230 static int module_instance_cmp(const void *one, const void *two)
232 const module_instance_t *a = one;
233 const module_instance_t *b = two;
235 return strcmp(a->name, b->name);
239 static void module_instance_free_old(CONF_SECTION *cs, module_instance_t *node,
242 fr_module_hup_t *mh, **last;
245 * Walk the list, freeing up old instances.
252 * Free only every 60 seconds.
254 if ((when - mh->when) < 60) {
259 cf_section_parse_free(cs, mh->insthandle);
261 if (node->entry->module->detach) {
262 (node->entry->module->detach)(mh->insthandle);
264 free(mh->insthandle);
274 * Free a module instance.
276 static void module_instance_free(void *data)
278 module_instance_t *this = data;
280 module_instance_free_old(this->cs, this, time(NULL) + 100);
282 if (this->entry->module->detach) {
283 (this->entry->module->detach)(this->insthandle);
286 #ifdef HAVE_PTHREAD_H
290 * The mutex MIGHT be locked...
291 * we'll check for that later, I guess.
293 pthread_mutex_destroy(this->mutex);
297 memset(this, 0, sizeof(*this));
303 * Compare two module entries
305 static int module_entry_cmp(const void *one, const void *two)
307 const module_entry_t *a = one;
308 const module_entry_t *b = two;
310 return strcmp(a->name, b->name);
314 * Free a module entry.
316 static void module_entry_free(void *data)
318 module_entry_t *this = data;
320 lt_dlclose(this->handle); /* ignore any errors */
321 memset(this, 0, sizeof(*this));
327 * Remove the module lists.
329 int detach_modules(void)
331 rbtree_free(instance_tree);
332 rbtree_free(module_tree);
341 * Find a module on disk or in memory, and link to it.
343 static module_entry_t *linkto_module(const char *module_name,
346 module_entry_t myentry;
347 module_entry_t *node;
349 char module_struct[256];
351 const module_t *module;
353 strlcpy(myentry.name, module_name, sizeof(myentry.name));
354 node = rbtree_finddata(module_tree, &myentry);
355 if (node) return node;
358 * Keep the handle around so we can dlclose() it.
360 handle = lt_dlopenext(module_name);
361 if (handle == NULL) {
362 cf_log_err(cf_sectiontoitem(cs),
363 "Failed to link to module '%s': %s\n",
364 module_name, lt_dlerror());
369 * Link to the module's rlm_FOO{} module structure.
371 * The module_name variable has the version number
372 * embedded in it, and we don't want that here.
374 strcpy(module_struct, module_name);
375 p = strrchr(module_struct, '-');
378 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
381 * libltld MAY core here, if the handle it gives us contains
384 module = lt_dlsym(handle, module_struct);
386 cf_log_err(cf_sectiontoitem(cs),
387 "Failed linking to %s structure: %s\n",
388 module_name, lt_dlerror());
393 * Before doing anything else, check if it's sane.
395 if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
397 cf_log_err(cf_sectiontoitem(cs),
398 "Invalid version in module '%s'",
404 /* make room for the module type */
405 node = rad_malloc(sizeof(*node));
406 memset(node, 0, sizeof(*node));
407 strlcpy(node->name, module_name, sizeof(node->name));
408 node->module = module;
409 node->handle = handle;
411 cf_log_module(cs, "Linked to module %s", module_name);
414 * Add the module as "rlm_foo-version" to the configuration
417 if (!rbtree_insert(module_tree, node)) {
418 radlog(L_ERR, "Failed to cache module %s", module_name);
428 * Find a module instance.
430 module_instance_t *find_module_instance(CONF_SECTION *modules,
431 const char *instname, int do_link)
433 int check_config_safe = FALSE;
435 const char *name1, *name2;
436 module_instance_t *node, myNode;
437 char module_name[256];
439 if (!modules) return NULL;
442 * Module instances are declared in the modules{} block
443 * and referenced later by their name, which is the
444 * name2 from the config section, or name1 if there was
447 cs = cf_section_sub_find_name2(modules, NULL, instname);
449 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
454 * If there's already a module instance, return it.
456 strlcpy(myNode.name, instname, sizeof(myNode.name));
457 node = rbtree_finddata(instance_tree, &myNode);
458 if (node) return node;
460 if (!do_link) return NULL;
462 name1 = cf_section_name1(cs);
465 * Found the configuration entry.
467 node = rad_malloc(sizeof(*node));
468 memset(node, 0, sizeof(*node));
470 node->insthandle = NULL;
474 * Names in the "modules" section aren't prefixed
475 * with "rlm_", so we add it here.
477 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
479 node->entry = linkto_module(module_name, cs);
482 /* linkto_module logs any errors */
486 if (check_config && (node->entry->module->instantiate) &&
487 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
488 const char *value = NULL;
491 cp = cf_pair_find(cs, "force_check_config");
492 if (cp) value = cf_pair_value(cp);
494 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
496 cf_log_module(cs, "Skipping instantiation of %s", instname);
499 check_config_safe = TRUE;
500 cf_log_module(cs, "Instantiating %s", instname);
504 * Call the module's instantiation routine.
506 if ((node->entry->module->instantiate) &&
507 (!check_config || check_config_safe) &&
508 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
509 cf_log_err(cf_sectiontoitem(cs),
510 "Instantiation failed for module \"%s\"",
517 * We're done. Fill in the rest of the data structure,
518 * and link it to the module instance list.
520 strlcpy(node->name, instname, sizeof(node->name));
522 #ifdef HAVE_PTHREAD_H
524 * If we're threaded, check if the module is thread-safe.
526 * If it isn't, we create a mutex.
528 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
529 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
531 * Initialize the mutex.
533 pthread_mutex_init(node->mutex, NULL);
536 * The module is thread-safe. Don't give it a mutex.
542 rbtree_insert(instance_tree, node);
547 static indexed_modcallable *lookup_by_index(rbtree_t *components,
550 indexed_modcallable myc;
555 return rbtree_finddata(components, &myc);
559 * Create a new sublist.
561 static indexed_modcallable *new_sublist(rbtree_t *components, int comp, int idx)
563 indexed_modcallable *c;
565 c = lookup_by_index(components, comp, idx);
567 /* It is an error to try to create a sublist that already
568 * exists. It would almost certainly be caused by accidental
569 * duplication in the config file.
571 * index 0 is the exception, because it is used when we want
572 * to collect _all_ listed modules under a single index by
573 * default, which is currently the case in all components
574 * except authenticate. */
582 c = rad_malloc(sizeof(*c));
583 c->modulelist = NULL;
587 if (!rbtree_insert(components, c)) {
595 int indexed_modcall(int comp, int idx, REQUEST *request)
598 modcallable *list = NULL;
599 virtual_server_t *server;
602 * Hack to find the correct virtual server.
604 rcode = virtual_server_idx(request->server);
605 for (server = virtual_servers[rcode];
607 server = server->next) {
608 if (!request->server && !server->name) break;
610 if ((request->server && server->name) &&
611 (strcmp(request->server, server->name) == 0)) break;
615 RDEBUG("No such virtual server %s", request->server);
616 return RLM_MODULE_FAIL;
620 list = server->mc[comp];
623 indexed_modcallable *this;
625 this = lookup_by_index(server->components, comp, idx);
627 list = this->modulelist;
629 RDEBUG2(" WARNING: Unknown value specified for %s. Cannot perform requested action.",
630 section_type_value[comp].typename);
634 request->component = section_type_value[comp].section;
636 rcode = modcall(comp, list, request);
638 request->module = "";
639 request->component = "";
644 * Load a sub-module list, as found inside an Auth-Type foo {}
647 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
648 rbtree_t *components, int attr, int comp)
650 indexed_modcallable *subcomp;
653 const char *name2 = cf_section_name2(cs);
655 rad_assert(comp >= RLM_COMPONENT_AUTH);
656 rad_assert(comp < RLM_COMPONENT_COUNT);
662 cf_log_err(cf_sectiontoitem(cs),
663 "No name specified for %s block",
664 section_type_value[comp].typename);
671 ml = compile_modgroup(parent, comp, cs);
677 * We must assign a numeric index to this subcomponent.
678 * It is generated and placed in the dictionary
679 * automatically. If it isn't found, it's a serious
682 dval = dict_valbyname(attr, name2);
684 cf_log_err(cf_sectiontoitem(cs),
685 "%s %s Not previously configured",
686 section_type_value[comp].typename, name2);
687 modcallable_free(&ml);
691 subcomp = new_sublist(components, comp, dval->value);
693 modcallable_free(&ml);
697 subcomp->modulelist = ml;
701 static int define_type(const DICT_ATTR *dattr, const char *name)
707 * If the value already exists, don't
710 dval = dict_valbyname(dattr->attr, name);
714 * Create a new unique value with a
715 * meaningless number. You can't look at
716 * it from outside of this code, so it
717 * doesn't matter. The only requirement
718 * is that it's unique.
721 value = fr_rand() & 0x00ffffff;
722 } while (dict_valbyattr(dattr->attr, value));
724 if (dict_addvalue(name, dattr->name, value) < 0) {
725 radlog(L_ERR, "%s", fr_strerror());
732 static int load_component_section(CONF_SECTION *cs,
733 rbtree_t *components, int comp)
738 indexed_modcallable *subcomp;
740 const char *visiblename;
741 const DICT_ATTR *dattr;
744 * Find the attribute used to store VALUEs for this section.
746 dattr = dict_attrbyvalue(section_type_value[comp].attr);
748 cf_log_err(cf_sectiontoitem(cs),
749 "No such attribute %s",
750 section_type_value[comp].typename);
755 * Loop over the entries in the named section, loading
756 * the sections this time.
758 for (modref = cf_item_find_next(cs, NULL);
760 modref = cf_item_find_next(cs, modref)) {
762 CONF_PAIR *cp = NULL;
763 CONF_SECTION *scs = NULL;
765 if (cf_item_is_section(modref)) {
766 scs = cf_itemtosection(modref);
768 name1 = cf_section_name1(scs);
771 section_type_value[comp].typename) == 0) {
772 if (!load_subcomponent_section(NULL, scs,
776 return -1; /* FIXME: memleak? */
783 } else if (cf_item_is_pair(modref)) {
784 cp = cf_itemtopair(modref);
787 continue; /* ignore it */
791 * Try to compile one entry.
793 this = compile_modsingle(NULL, comp, modref, &modname);
795 cf_log_err(cf_sectiontoitem(cs),
796 "Errors parsing %s section.\n",
797 cf_section_name1(cs));
802 * Look for Auth-Type foo {}, which are special
803 * cases of named sections, and allowable ONLY
806 * i.e. They're not allowed in a "group" or "redundant"
809 if (comp == RLM_COMPONENT_AUTH) {
811 const char *modrefname = NULL;
813 modrefname = cf_pair_attr(cp);
815 modrefname = cf_section_name2(scs);
817 modcallable_free(&this);
818 cf_log_err(cf_sectiontoitem(cs),
819 "Errors parsing %s sub-section.\n",
820 cf_section_name1(scs));
825 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
828 * It's a section, but nothing we
831 modcallable_free(&this);
832 cf_log_err(cf_sectiontoitem(cs),
833 "Unknown Auth-Type \"%s\" in %s sub-section.",
834 modrefname, section_type_value[comp].section);
839 /* See the comment in new_sublist() for explanation
840 * of the special index 0 */
844 subcomp = new_sublist(components, comp, idx);
845 if (subcomp == NULL) {
846 modcallable_free(&this);
850 /* If subcomp->modulelist is NULL, add_to_modcallable will
852 visiblename = cf_section_name2(cs);
853 if (visiblename == NULL)
854 visiblename = cf_section_name1(cs);
855 add_to_modcallable(&subcomp->modulelist, this,
862 static int load_byserver(CONF_SECTION *cs)
865 const char *name = cf_section_name2(cs);
866 rbtree_t *components;
867 virtual_server_t *server = NULL;
870 cf_log_info(cs, "server %s {", name);
872 cf_log_info(cs, "server {");
875 cf_log_info(cs, " modules {");
877 components = rbtree_create(indexed_modcallable_cmp,
878 indexed_modcallable_free, 0);
880 radlog(L_ERR, "Failed to initialize components\n");
884 server = rad_malloc(sizeof(*server));
885 memset(server, 0, sizeof(*server));
888 server->created = time(NULL);
890 server->components = components;
893 * Define types first.
895 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
900 subcs = cf_section_sub_find(cs,
901 section_type_value[comp].section);
902 if (!subcs) continue;
904 if (cf_item_find_next(subcs, NULL) == NULL) continue;
907 * Find the attribute used to store VALUEs for this section.
909 dattr = dict_attrbyvalue(section_type_value[comp].attr);
911 cf_log_err(cf_sectiontoitem(subcs),
912 "No such attribute %s",
913 section_type_value[comp].typename);
915 if (debug_flag == 0) {
916 radlog(L_ERR, "Failed to load virtual server %s",
917 (name != NULL) ? name : "<default>");
919 virtual_server_free(server);
924 * Define dynamic types, so that others can reference
927 for (modref = cf_item_find_next(subcs, NULL);
929 modref = cf_item_find_next(subcs, modref)) {
931 CONF_SECTION *subsubcs;
934 * Create types for simple references
935 * only when parsing the authenticate
938 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
939 cf_item_is_pair(modref)) {
940 CONF_PAIR *cp = cf_itemtopair(modref);
941 if (!define_type(dattr, cf_pair_attr(cp))) {
948 if (!cf_item_is_section(modref)) continue;
950 subsubcs = cf_itemtosection(modref);
951 name1 = cf_section_name1(subsubcs);
953 if (strcmp(name1, section_type_value[comp].typename) == 0) {
954 if (!define_type(dattr,
955 cf_section_name2(subsubcs))) {
960 } /* loop over components */
963 * Loop over all of the known components, finding their
964 * configuration section, and loading it.
967 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
969 indexed_modcallable *c;
971 subcs = cf_section_sub_find(cs,
972 section_type_value[comp].section);
973 if (!subcs) continue;
975 if (cf_item_find_next(subcs, NULL) == NULL) continue;
977 cf_log_module(cs, "Checking %s {...} for more modules to load",
978 section_type_value[comp].section);
982 * Skip pre/post-proxy sections if we're not
985 if (!mainconfig.proxy_requests &&
986 ((comp == PW_PRE_PROXY_TYPE) ||
987 (comp == PW_PRE_PROXY_TYPE))) {
992 if (load_component_section(subcs, components, comp) < 0) {
997 * Cache a default, if it exists. Some people
998 * put empty sections for some reason...
1000 c = lookup_by_index(components, comp, 0);
1001 if (c) server->mc[comp] = c->modulelist;
1003 server->subcs[comp] = subcs;
1006 } /* loop over components */
1009 * We haven't loaded any of the normal sections. Maybe we're
1010 * supposed to load the vmps section.
1012 * This is a bit of a hack...
1015 CONF_SECTION *subcs;
1017 subcs = cf_section_sub_find(cs, "vmps");
1019 cf_log_module(cs, "Checking vmps {...} for more modules to load");
1020 if (load_component_section(subcs, components,
1021 RLM_COMPONENT_POST_AUTH) < 0) {
1029 const DICT_ATTR *dattr;
1031 dattr = dict_attrbyname("DHCP-Message-Type");
1033 radlog(L_ERR, "No DHCP-Message-Type attribute");
1038 * Handle each DHCP Message type separately.
1040 for (subcs = cf_subsection_find_next(cs, NULL,
1043 subcs = cf_subsection_find_next(cs, subcs,
1045 const char *name2 = cf_section_name2(subcs);
1047 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
1048 if (!load_subcomponent_section(NULL, subcs,
1051 RLM_COMPONENT_POST_AUTH)) {
1052 goto error; /* FIXME: memleak? */
1060 cf_log_info(cs, " } # modules");
1061 cf_log_info(cs, "} # server");
1063 if (!flag && name) {
1064 DEBUG("WARNING: Server %s is empty, and will do nothing!",
1068 if (debug_flag == 0) {
1069 radlog(L_INFO, "Loaded virtual server %s",
1070 (name != NULL) ? name : "<default>");
1074 * Now that it is OK, insert it into the list.
1076 * This is thread-safe...
1078 comp = virtual_server_idx(name);
1079 server->next = virtual_servers[comp];
1080 virtual_servers[comp] = server;
1083 * Mark OLDER ones of the same name as being unused.
1085 server = server->next;
1087 if ((!name && !server->name) ||
1088 (name && server->name &&
1089 (strcmp(server->name, name) == 0))) {
1090 server->can_free = TRUE;
1093 server = server->next;
1101 * Load all of the virtual servers.
1103 int virtual_servers_load(CONF_SECTION *config)
1105 int null_server = FALSE;
1107 static int first_time = TRUE;
1109 DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1112 * Load all of the virtual servers.
1114 for (cs = cf_subsection_find_next(config, NULL, "server");
1116 cs = cf_subsection_find_next(config, cs, "server")) {
1117 if (!cf_section_name2(cs)) null_server = TRUE;
1119 if (load_byserver(cs) < 0) {
1121 * Once we successfully staryed once,
1122 * continue loading the OTHER servers,
1123 * even if one fails.
1125 if (!first_time) continue;
1131 * No empty server defined. Try to load an old-style
1132 * one for backwards compatibility.
1135 if (load_byserver(config) < 0) {
1141 * If we succeed the first time around, remember that.
1148 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
1150 void *insthandle = NULL;
1151 fr_module_hup_t *mh;
1154 !node->entry->module->instantiate ||
1155 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
1159 cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
1161 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
1162 cf_log_err(cf_sectiontoitem(cs),
1163 "HUP failed for module \"%s\". Using old configuration.",
1168 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
1170 module_instance_free_old(cs, node, when);
1173 * Save the old instance handle for later deletion.
1175 mh = rad_malloc(sizeof(*mh));
1178 mh->insthandle = node->insthandle;
1179 mh->next = node->mh;
1182 node->insthandle = insthandle;
1185 * FIXME: Set a timeout to come back in 60s, so that
1186 * we can pro-actively clean up the old instances.
1193 int module_hup(CONF_SECTION *modules)
1198 module_instance_t *node;
1200 if (!modules) return 0;
1205 * Loop over the modules
1207 for (ci=cf_item_find_next(modules, NULL);
1209 ci=cf_item_find_next(modules, ci)) {
1210 const char *instname;
1211 module_instance_t myNode;
1214 * If it's not a section, ignore it.
1216 if (!cf_item_is_section(ci)) continue;
1218 cs = cf_itemtosection(ci);
1219 instname = cf_section_name2(cs);
1220 if (!instname) instname = cf_section_name1(cs);
1222 strlcpy(myNode.name, instname, sizeof(myNode.name));
1223 node = rbtree_finddata(instance_tree, &myNode);
1225 module_hup_module(cs, node, when);
1233 * Parse the module config sections, and load
1234 * and call each module's init() function.
1236 * Libtool makes your life a LOT easier, especially with libltdl.
1237 * see: http://www.gnu.org/software/libtool/
1239 int setup_modules(int reload, CONF_SECTION *config)
1241 CONF_SECTION *cs, *modules;
1242 rad_listen_t *listener;
1244 if (reload) return 0;
1247 * If necessary, initialize libltdl.
1251 * Set the default list of preloaded symbols.
1252 * This is used to initialize libltdl's list of
1253 * preloaded modules.
1255 * i.e. Static modules.
1257 LTDL_SET_PRELOADED_SYMBOLS();
1259 if (lt_dlinit() != 0) {
1260 radlog(L_ERR, "Failed to initialize libraries: %s\n",
1266 * Set the search path to ONLY our library directory.
1267 * This prevents the modules from being found from
1268 * any location on the disk.
1270 lt_dlsetsearchpath(radlib_dir);
1273 * Set up the internal module struct.
1275 module_tree = rbtree_create(module_entry_cmp,
1276 module_entry_free, 0);
1278 radlog(L_ERR, "Failed to initialize modules\n");
1282 instance_tree = rbtree_create(module_instance_cmp,
1283 module_instance_free, 0);
1284 if (!instance_tree) {
1285 radlog(L_ERR, "Failed to initialize modules\n");
1290 memset(virtual_servers, 0, sizeof(virtual_servers));
1293 * Remember where the modules were stored.
1295 modules = cf_section_sub_find(config, "modules");
1297 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
1301 DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1304 * Look for the 'instantiate' section, which tells us
1305 * the instantiation order of the modules, and also allows
1306 * us to load modules with no authorize/authenticate/etc.
1309 cs = cf_section_sub_find(config, "instantiate");
1313 module_instance_t *module;
1316 cf_log_info(cs, " instantiate {");
1319 * Loop over the items in the 'instantiate' section.
1321 for (ci=cf_item_find_next(cs, NULL);
1323 ci=cf_item_find_next(cs, ci)) {
1326 * Skip sections and "other" stuff.
1327 * Sections will be handled later, if
1328 * they're referenced at all...
1330 if (!cf_item_is_pair(ci)) {
1334 cp = cf_itemtopair(ci);
1335 name = cf_pair_attr(cp);
1336 module = find_module_instance(modules, name, 1);
1340 } /* loop over items in the subsection */
1342 cf_log_info(cs, " }");
1343 } /* if there's an 'instantiate' section. */
1346 * Loop over the listeners, figuring out which sections
1349 for (listener = mainconfig.listen;
1351 listener = listener->next) {
1354 if (listener->type == RAD_LISTEN_PROXY) continue;
1356 cs = cf_section_sub_find_name2(config,
1357 "server", listener->server);
1358 if (!cs && (listener->server != NULL)) {
1359 listener->print(listener, buffer, sizeof(buffer));
1361 radlog(L_ERR, "No server has been defined for %s", buffer);
1366 if (virtual_servers_load(config) < 0) return -1;
1372 * Call all authorization modules until one returns
1373 * somethings else than RLM_MODULE_OK
1375 int module_authorize(int autz_type, REQUEST *request)
1377 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1381 * Authenticate a user/password with various methods.
1383 int module_authenticate(int auth_type, REQUEST *request)
1385 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1388 #ifdef WITH_ACCOUNTING
1390 * Do pre-accounting for ALL configured sessions
1392 int module_preacct(REQUEST *request)
1394 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1398 * Do accounting for ALL configured sessions
1400 int module_accounting(int acct_type, REQUEST *request)
1402 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1406 #ifdef WITH_SESSION_MGMT
1408 * See if a user is already logged in.
1410 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1412 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1416 if(!request->username)
1419 request->simul_count = 0;
1420 request->simul_max = maxsimul;
1421 request->simul_mpp = 1;
1423 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1425 if (rcode != RLM_MODULE_OK) {
1426 /* FIXME: Good spot for a *rate-limited* warning to the log */
1430 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1436 * Do pre-proxying for ALL configured sessions
1438 int module_pre_proxy(int type, REQUEST *request)
1440 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1444 * Do post-proxying for ALL configured sessions
1446 int module_post_proxy(int type, REQUEST *request)
1448 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1453 * Do post-authentication for ALL configured sessions
1455 int module_post_auth(int postauth_type, REQUEST *request)
1457 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1461 int module_recv_coa(int recv_coa_type, REQUEST *request)
1463 return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
1466 int module_send_coa(int send_coa_type, REQUEST *request)
1468 return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);