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 {
39 modcallable *modulelist;
40 } indexed_modcallable;
43 * For each component, keep an ordered list of ones to call.
45 static rbtree_t *components;
47 static rbtree_t *module_tree = NULL;
49 typedef struct section_type_value_t {
53 } section_type_value_t;
57 * Ordered by component
59 static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
60 { "authenticate", "Auth-Type", PW_AUTH_TYPE },
61 { "authorize", "Autz-Type", PW_AUTZ_TYPE },
62 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
63 { "accounting", "Acct-Type", PW_ACCT_TYPE },
64 { "session", "Session-Type", PW_SESSION_TYPE },
65 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
66 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
67 { "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE },
70 static void indexed_modcallable_free(void *data)
72 indexed_modcallable *c = data;
74 modcallable_free(&c->modulelist);
78 static int indexed_modcallable_cmp(const void *one, const void *two)
81 const indexed_modcallable *a = one;
82 const indexed_modcallable *b = two;
84 if (a->server && !b->server) return -1;
85 if (!a->server && b->server) return +1;
86 if (a->server && b->server) {
87 rcode = strcmp(a->server, b->server);
88 if (rcode != 0) return rcode;
91 if (a->comp < b->comp) return -1;
92 if (a->comp > b->comp) return +1;
94 return a->idx - b->idx;
99 * Free a module instance.
101 static void module_instance_free(void *data)
103 module_instance_t *this = data;
105 if (this->entry->module->detach)
106 (this->entry->module->detach)(this->insthandle);
107 #ifdef HAVE_PTHREAD_H
111 * The mutex MIGHT be locked...
112 * we'll check for that later, I guess.
114 pthread_mutex_destroy(this->mutex);
123 * Compare two module entries
125 static int module_entry_cmp(const void *one, const void *two)
127 const module_entry_t *a = one;
128 const module_entry_t *b = two;
130 return strcmp(a->name, b->name);
134 * Free a module entry.
136 static void module_entry_free(void *data)
138 module_entry_t *this = data;
140 lt_dlclose(this->handle); /* ignore any errors */
146 * Remove the module lists.
148 int detach_modules(void)
150 rbtree_free(components);
151 rbtree_free(module_tree);
158 * Find a module on disk or in memory, and link to it.
160 static module_entry_t *linkto_module(const char *module_name,
163 module_entry_t myentry;
164 module_entry_t *node;
166 char module_struct[256];
170 strlcpy(myentry.name, module_name, sizeof(myentry.name));
171 node = rbtree_finddata(module_tree, &myentry);
172 if (node) return node;
175 * Keep the handle around so we can dlclose() it.
177 handle = lt_dlopenext(module_name);
178 if (handle == NULL) {
179 cf_log_err(cf_sectiontoitem(cs),
180 "Failed to link to module '%s': %s\n",
181 module_name, lt_dlerror());
186 * Link to the module's rlm_FOO{} module structure.
188 * The module_name variable has the version number
189 * embedded in it, and we don't want that here.
191 strcpy(module_struct, module_name);
192 p = strrchr(module_struct, '-');
195 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
198 * libltld MAY core here, if the handle it gives us contains
201 module = lt_dlsym(handle, module_struct);
203 cf_log_err(cf_sectiontoitem(cs),
204 "Failed linking to %s structure: %s\n",
205 module_name, lt_dlerror());
210 * Before doing anything else, check if it's sane.
212 if ((*(const uint32_t *) module) != RLM_MODULE_MAGIC_NUMBER) {
214 cf_log_err(cf_sectiontoitem(cs),
215 "Invalid version in module '%s'",
221 /* make room for the module type */
222 node = rad_malloc(sizeof(*node));
223 memset(node, 0, sizeof(*node));
224 strlcpy(node->name, module_name, sizeof(node->name));
225 node->module = module;
226 node->handle = handle;
228 DEBUG(" Module: Linked to module %s", module_name);
231 * Add the module as "rlm_foo-version" to the configuration
234 if (!rbtree_insert(module_tree, node)) {
235 radlog(L_ERR, "Failed to cache module %s", module_name);
245 * Find a module instance.
247 module_instance_t *find_module_instance(CONF_SECTION *modules,
248 const char *instname)
251 const char *name1, *name2;
252 module_instance_t *node;
253 char module_name[256];
255 if (!modules) return NULL;
258 * Module instances are declared in the modules{} block
259 * and referenced later by their name, which is the
260 * name2 from the config section, or name1 if there was
263 cs = cf_section_sub_find_name2(modules, NULL, instname);
265 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
270 * If there's already a module instance, return it.
272 node = cf_data_find(cs, "instance");
273 if (node) return node;
275 name1 = cf_section_name1(cs);
276 name2 = cf_section_name2(cs);
279 * Found the configuration entry.
281 node = rad_malloc(sizeof(*node));
282 memset(node, 0, sizeof(*node));
284 node->insthandle = NULL;
287 * Names in the "modules" section aren't prefixed
288 * with "rlm_", so we add it here.
290 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
292 node->entry = linkto_module(module_name, cs);
295 /* linkto_module logs any errors */
299 if (check_config && (node->entry->module->instantiate) &&
300 (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
301 DEBUG2(" Module: Skipping instantiation of %s", instname);
303 DEBUG2(" Module: Instantiating %s", instname);
307 * Call the module's instantiation routine.
309 if ((node->entry->module->instantiate) &&
311 ((node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) != 0)) &&
312 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
313 cf_log_err(cf_sectiontoitem(cs),
314 "Instantiation failed for module \"%s\"",
321 * We're done. Fill in the rest of the data structure,
322 * and link it to the module instance list.
324 strlcpy(node->name, instname, sizeof(node->name));
326 #ifdef HAVE_PTHREAD_H
328 * If we're threaded, check if the module is thread-safe.
330 * If it isn't, we create a mutex.
332 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
333 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
335 * Initialize the mutex.
337 pthread_mutex_init(node->mutex, NULL);
340 * The module is thread-safe. Don't give it a mutex.
346 cf_data_add(cs, "instance", node, module_instance_free);
351 static indexed_modcallable *lookup_by_index(const char *server, int comp,
354 indexed_modcallable myc;
360 return rbtree_finddata(components, &myc);
364 * Create a new sublist.
366 static indexed_modcallable *new_sublist(const char *server, int comp, int idx)
368 indexed_modcallable *c;
370 c = lookup_by_index(server, comp, idx);
372 /* It is an error to try to create a sublist that already
373 * exists. It would almost certainly be caused by accidental
374 * duplication in the config file.
376 * index 0 is the exception, because it is used when we want
377 * to collect _all_ listed modules under a single index by
378 * default, which is currently the case in all components
379 * except authenticate. */
387 c = rad_malloc(sizeof(*c));
388 c->modulelist = NULL;
393 if (!rbtree_insert(components, c)) {
401 static int indexed_modcall(int comp, int idx, REQUEST *request)
404 indexed_modcallable *this;
405 modcallable *list = NULL;
407 this = lookup_by_index(request->server, comp, idx);
409 if (idx != 0) DEBUG2(" WARNING: Unknown value specified for %s. Cannot perform requested action.",
410 section_type_value[comp].typename);
412 list = this->modulelist;
415 request->component = section_type_value[comp].section;
417 rcode = modcall(comp, list, request);
419 request->module = "<server-core>";
420 request->component = "<server-core>";
425 * Load a sub-module list, as found inside an Auth-Type foo {}
428 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
429 const char *server, int comp)
431 indexed_modcallable *subcomp;
434 const char *name2 = cf_section_name2(cs);
436 rad_assert(comp >= RLM_COMPONENT_AUTH);
437 rad_assert(comp <= RLM_COMPONENT_COUNT);
443 cf_log_err(cf_sectiontoitem(cs),
444 "No name specified for %s block",
445 section_type_value[comp].typename);
452 ml = compile_modgroup(parent, comp, cs);
458 * We must assign a numeric index to this subcomponent.
459 * It is generated and placed in the dictionary
460 * automatically. If it isn't found, it's a serious
463 dval = dict_valbyname(section_type_value[comp].attr, name2);
465 cf_log_err(cf_sectiontoitem(cs),
466 "%s %s Not previously configured",
467 section_type_value[comp].typename, name2);
468 modcallable_free(&ml);
472 subcomp = new_sublist(server, comp, dval->value);
474 modcallable_free(&ml);
478 subcomp->modulelist = ml;
482 static int define_type(const DICT_ATTR *dattr, const char *name)
488 * If the value already exists, don't
491 dval = dict_valbyname(dattr->attr, name);
495 * Create a new unique value with a
496 * meaningless number. You can't look at
497 * it from outside of this code, so it
498 * doesn't matter. The only requirement
499 * is that it's unique.
502 value = lrad_rand() & 0x00ffffff;
503 } while (dict_valbyattr(dattr->attr, value));
505 if (dict_addvalue(name, dattr->name, value) < 0) {
506 radlog(L_ERR, "%s", librad_errstr);
513 static int load_component_section(CONF_SECTION *cs,
514 const char *server, int comp)
519 indexed_modcallable *subcomp;
521 const char *visiblename;
522 const DICT_ATTR *dattr;
525 * Find the attribute used to store VALUEs for this section.
527 dattr = dict_attrbyvalue(section_type_value[comp].attr);
529 cf_log_err(cf_sectiontoitem(cs),
530 "No such attribute %s",
531 section_type_value[comp].typename);
536 * Loop over the entries in the named section, loading
537 * the sections this time.
539 for (modref = cf_item_find_next(cs, NULL);
541 modref = cf_item_find_next(cs, modref)) {
543 CONF_PAIR *cp = NULL;
544 CONF_SECTION *scs = NULL;
546 if (cf_item_is_section(modref)) {
547 scs = cf_itemtosection(modref);
549 name1 = cf_section_name1(scs);
552 section_type_value[comp].typename) == 0) {
553 if (!load_subcomponent_section(NULL, scs,
555 return -1; /* FIXME: memleak? */
562 } else if (cf_item_is_pair(modref)) {
563 cp = cf_itemtopair(modref);
566 continue; /* ignore it */
570 * Try to compile one entry.
572 this = compile_modsingle(NULL, comp, modref, &modname);
574 cf_log_err(cf_sectiontoitem(cs),
575 "Errors parsing %s section.\n",
576 cf_section_name1(cs));
581 * Look for Auth-Type foo {}, which are special
582 * cases of named sections, and allowable ONLY
585 * i.e. They're not allowed in a "group" or "redundant"
588 if (comp == RLM_COMPONENT_AUTH) {
590 const char *modrefname = NULL;
592 modrefname = cf_pair_attr(cp);
594 modrefname = cf_section_name2(scs);
596 cf_log_err(cf_sectiontoitem(cs),
597 "Errors parsing %s sub-section.\n",
598 cf_section_name1(scs));
603 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
606 * It's a section, but nothing we
609 cf_log_err(cf_sectiontoitem(cs),
610 "Unknown Auth-Type \"%s\" in %s sub-section.",
611 modrefname, section_type_value[comp].section);
616 /* See the comment in new_sublist() for explanation
617 * of the special index 0 */
621 subcomp = new_sublist(server, comp, idx);
622 if (subcomp == NULL) {
623 modcallable_free(&this);
627 /* If subcomp->modulelist is NULL, add_to_modcallable will
629 visiblename = cf_section_name2(cs);
630 if (visiblename == NULL)
631 visiblename = cf_section_name1(cs);
632 add_to_modcallable(&subcomp->modulelist, this,
639 static int load_byserver(CONF_SECTION *cs)
642 const char *server = cf_section_name2(cs);
644 DEBUG2(" modules {");
647 * Define types first.
649 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
654 subcs = cf_section_sub_find(cs,
655 section_type_value[comp].section);
656 if (!subcs) continue;
658 if (cf_item_find_next(subcs, NULL) == NULL) continue;
661 * Find the attribute used to store VALUEs for this section.
663 dattr = dict_attrbyvalue(section_type_value[comp].attr);
665 cf_log_err(cf_sectiontoitem(subcs),
666 "No such attribute %s",
667 section_type_value[comp].typename);
673 * Define dynamic types, so that others can reference
676 for (modref = cf_item_find_next(subcs, NULL);
678 modref = cf_item_find_next(subcs, modref)) {
680 CONF_SECTION *subsubcs;
683 * Create types for simple references
684 * only when parsing the authenticate
687 if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
688 cf_item_is_pair(modref)) {
689 CONF_PAIR *cp = cf_itemtopair(modref);
690 if (!define_type(dattr, cf_pair_attr(cp))) {
697 if (!cf_item_is_section(modref)) continue;
699 subsubcs = cf_itemtosection(modref);
700 name1 = cf_section_name1(subsubcs);
702 if (strcmp(name1, section_type_value[comp].typename) == 0) {
703 if (!define_type(dattr,
704 cf_section_name2(subsubcs))) {
710 } /* loop over components */
713 * Loop over all of the known components, finding their
714 * configuration section, and loading it.
717 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
720 subcs = cf_section_sub_find(cs,
721 section_type_value[comp].section);
722 if (!subcs) continue;
724 if (cf_item_find_next(subcs, NULL) == NULL) continue;
726 DEBUG2(" Module: Checking %s {...} for more modules to load",
727 section_type_value[comp].section);
729 if (load_component_section(subcs, server, comp) < 0) {
734 } /* loop over components */
737 * We haven't loaded any of the normal sections. Maybe we're
738 * supposed to load the vmps section.
740 * This is a bit of a hack...
745 subcs = cf_section_sub_find(cs, "vmps");
747 DEBUG2(" Module: Checking vmps {...} for more modules to load");
748 if (load_component_section(subcs, server,
749 RLM_COMPONENT_POST_AUTH) < 0) {
758 if (!flag && server) {
759 DEBUG("WARNING: Server %s is empty, and will do nothing!",
767 int module_hup(CONF_SECTION *modules)
771 module_instance_t *node;
773 if (!modules) return 0;
776 * Loop over the modules
778 for (ci=cf_item_find_next(modules, NULL);
780 ci=cf_item_find_next(modules, ci)) {
781 void *insthandle = NULL;
782 void *old_insthandle = NULL;
785 * If it's not a section, ignore it.
787 if (!cf_item_is_section(ci)) continue;
789 cs = cf_itemtosection(ci);
791 node = cf_data_find(cs, "instance");
793 ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
797 DEBUG2(" Module: Trying to reload module \"%s\"", node->name);
799 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
800 cf_log_err(cf_sectiontoitem(cs),
801 "HUP failed for module \"%s\"",
806 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
808 old_insthandle = node->insthandle;
809 node->insthandle = insthandle;
811 cf_section_parse_free(cs, old_insthandle);
813 if (node->entry->module->detach) {
814 (node->entry->module->detach)(old_insthandle);
823 * Parse the module config sections, and load
824 * and call each module's init() function.
826 * Libtool makes your life a LOT easier, especially with libltdl.
827 * see: http://www.gnu.org/software/libtool/
829 int setup_modules(int reload, CONF_SECTION *config)
831 CONF_SECTION *cs, *modules;
832 rad_listen_t *listener;
833 int null_server = FALSE;
836 * If necessary, initialize libltdl.
840 * Set the default list of preloaded symbols.
841 * This is used to initialize libltdl's list of
844 * i.e. Static modules.
846 LTDL_SET_PRELOADED_SYMBOLS();
848 if (lt_dlinit() != 0) {
849 radlog(L_ERR, "Failed to initialize libraries: %s\n",
855 * Set the search path to ONLY our library directory.
856 * This prevents the modules from being found from
857 * any location on the disk.
859 lt_dlsetsearchpath(radlib_dir);
862 * Set up the internal module struct.
864 module_tree = rbtree_create(module_entry_cmp,
865 module_entry_free, 0);
867 radlog(L_ERR, "Failed to initialize modules\n");
871 rbtree_free(components);
874 components = rbtree_create(indexed_modcallable_cmp,
875 indexed_modcallable_free, 0);
877 radlog(L_ERR, "Failed to initialize components\n");
882 * Remember where the modules were stored.
884 modules = cf_section_sub_find(config, "modules");
886 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
890 DEBUG2("radiusd: #### Instantiating modules ####");
893 * Look for the 'instantiate' section, which tells us
894 * the instantiation order of the modules, and also allows
895 * us to load modules with no authorize/authenticate/etc.
898 cs = cf_section_sub_find(config, "instantiate");
902 module_instance_t *module;
905 DEBUG2(" instantiate {");
908 * Loop over the items in the 'instantiate' section.
910 for (ci=cf_item_find_next(cs, NULL);
912 ci=cf_item_find_next(cs, ci)) {
915 * Skip sections. They'll be handled
916 * later, if they're referenced at all...
918 if (cf_item_is_section(ci)) {
922 cp = cf_itemtopair(ci);
923 name = cf_pair_attr(cp);
924 module = find_module_instance(modules, name);
928 } /* loop over items in the subsection */
931 } /* if there's an 'instantiate' section. */
934 * Loop over the listeners, figuring out which sections
937 for (listener = mainconfig.listen;
939 listener = listener->next) {
942 if (listener->type == RAD_LISTEN_PROXY) continue;
944 cs = cf_section_sub_find_name2(config,
945 "server", listener->server);
946 if (!cs && (listener->server != NULL)) {
947 listener->print(listener, buffer, sizeof(buffer));
949 radlog(L_ERR, "No server has been defined for %s", buffer);
954 DEBUG2("radiusd: #### Loading Virtual Servers ####");
957 * Load all of the virtual servers.
959 for (cs = cf_subsection_find_next(config, NULL, "server");
961 cs = cf_subsection_find_next(config, cs, "server")) {
962 const char *name2 = cf_section_name2(cs);
965 DEBUG2("server %s {", name2);
970 if (load_byserver(cs) < 0) {
978 * No empty server defined. Try to load an old-style
979 * one for backwards compatibility.
983 if (load_byserver(config) < 0) {
994 * Call all authorization modules until one returns
995 * somethings else than RLM_MODULE_OK
997 int module_authorize(int autz_type, REQUEST *request)
999 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1003 * Authenticate a user/password with various methods.
1005 int module_authenticate(int auth_type, REQUEST *request)
1007 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1011 * Do pre-accounting for ALL configured sessions
1013 int module_preacct(REQUEST *request)
1015 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1019 * Do accounting for ALL configured sessions
1021 int module_accounting(int acct_type, REQUEST *request)
1023 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1027 * See if a user is already logged in.
1029 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1031 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1035 if(!request->username)
1038 request->simul_count = 0;
1039 request->simul_max = maxsimul;
1040 request->simul_mpp = 1;
1042 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1044 if (rcode != RLM_MODULE_OK) {
1045 /* FIXME: Good spot for a *rate-limited* warning to the log */
1049 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1053 * Do pre-proxying for ALL configured sessions
1055 int module_pre_proxy(int type, REQUEST *request)
1057 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1061 * Do post-proxying for ALL configured sessions
1063 int module_post_proxy(int type, REQUEST *request)
1065 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1069 * Do post-authentication for ALL configured sessions
1071 int module_post_auth(int postauth_type, REQUEST *request)
1073 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);