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 #define VMPS_SPACE "vmps"
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->space && !b->space) return -1;
85 if (!a->space && b->space) return +1;
86 if (a->space && b->space) {
87 rcode = strcmp(a->space, b->space);
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,
161 const char *cffilename, int cflineno)
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 radlog(L_ERR|L_CONS, "%s[%d]: Failed to link to module '%s':"
180 " %s\n", cffilename, cflineno, module_name, lt_dlerror());
185 * Link to the module's rlm_FOO{} module structure.
187 * The module_name variable has the version number
188 * embedded in it, and we don't want that here.
190 strcpy(module_struct, module_name);
191 p = strrchr(module_struct, '-');
194 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
197 * libltld MAY core here, if the handle it gives us contains
200 module = lt_dlsym(handle, module_struct);
202 radlog(L_ERR|L_CONS, "%s[%d]: Failed linking to "
203 "%s structure in %s: %s\n",
204 cffilename, cflineno,
205 module_name, cffilename, lt_dlerror());
210 * Before doing anything else, check if it's sane.
212 if ((*(const uint32_t *) module) != RLM_MODULE_MAGIC_NUMBER) {
214 radlog(L_ERR|L_CONS, "%s[%d]: Invalid version in module '%s'",
215 cffilename, cflineno, module_name);
220 /* make room for the module type */
221 node = rad_malloc(sizeof(*node));
222 memset(node, 0, sizeof(*node));
223 strlcpy(node->name, module_name, sizeof(node->name));
224 node->module = module;
225 node->handle = handle;
227 DEBUG(" Module: Linked to module %s", module_name);
230 * Add the module as "rlm_foo-version" to the configuration
233 if (!rbtree_insert(module_tree, node)) {
234 radlog(L_ERR, "Failed to cache module %s", module_name);
244 * Find a module instance.
246 module_instance_t *find_module_instance(CONF_SECTION *modules,
247 const char *instname)
250 const char *name1, *name2;
251 module_instance_t *node;
252 char module_name[256];
254 if (!modules) return NULL;
257 * Module instances are declared in the modules{} block
258 * and referenced later by their name, which is the
259 * name2 from the config section, or name1 if there was
262 cs = cf_section_sub_find_name2(modules, NULL, instname);
264 radlog(L_ERR|L_CONS, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
269 * If there's already a module instance, return it.
271 node = cf_data_find(cs, "instance");
272 if (node) return node;
274 name1 = cf_section_name1(cs);
275 name2 = cf_section_name2(cs);
278 * Found the configuration entry.
280 node = rad_malloc(sizeof(*node));
281 memset(node, 0, sizeof(*node));
283 node->insthandle = NULL;
286 * Names in the "modules" section aren't prefixed
287 * with "rlm_", so we add it here.
289 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
291 node->entry = linkto_module(module_name,
292 mainconfig.radiusd_conf,
293 cf_section_lineno(cs));
296 /* linkto_module logs any errors */
300 DEBUG2(" Module: Instantiating %s", instname);
303 * Call the module's instantiation routine.
305 if ((node->entry->module->instantiate) &&
306 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
308 "%s[%d]: %s: Module instantiation failed.\n",
309 mainconfig.radiusd_conf, cf_section_lineno(cs),
316 * We're done. Fill in the rest of the data structure,
317 * and link it to the module instance list.
319 strlcpy(node->name, instname, sizeof(node->name));
321 #ifdef HAVE_PTHREAD_H
323 * If we're threaded, check if the module is thread-safe.
325 * If it isn't, we create a mutex.
327 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
328 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
330 * Initialize the mutex.
332 pthread_mutex_init(node->mutex, NULL);
335 * The module is thread-safe. Don't give it a mutex.
341 cf_data_add(cs, "instance", node, module_instance_free);
346 static indexed_modcallable *lookup_by_index(const char *space, int comp,
349 indexed_modcallable myc;
355 return rbtree_finddata(components, &myc);
359 * Create a new sublist.
361 static indexed_modcallable *new_sublist(const char *space, int comp, int idx)
363 indexed_modcallable *c;
365 c = lookup_by_index(space, comp, idx);
367 /* It is an error to try to create a sublist that already
368 * exists. It would almost certainly be caused by accidental
369 * duplication in the config file.
371 * index 0 is the exception, because it is used when we want
372 * to collect _all_ listed modules under a single index by
373 * default, which is currently the case in all components
374 * except authenticate. */
382 c = rad_malloc(sizeof(*c));
383 c->modulelist = NULL;
388 if (!rbtree_insert(components, c)) {
396 static int indexed_modcall(const char *space, int comp, int idx,
400 indexed_modcallable *this;
401 modcallable *list = NULL;
403 this = lookup_by_index(space, comp, idx);
405 if (idx != 0) DEBUG2(" WARNING: Unknown value specified for %s. Cannot perform requested action.",
406 section_type_value[comp].typename);
408 list = this->modulelist;
411 request->component = section_type_value[comp].section;
413 rcode = modcall(comp, list, request);
415 request->module = "<server-core>";
416 request->component = "<server-core>";
421 * Load a sub-module list, as found inside an Auth-Type foo {}
424 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
425 const char *space, int comp)
427 indexed_modcallable *subcomp;
430 const char *name2 = cf_section_name2(cs);
432 rad_assert(comp >= RLM_COMPONENT_AUTH);
433 rad_assert(comp <= RLM_COMPONENT_COUNT);
440 "%s[%d]: No name specified for %s block",
441 cf_section_filename(cs), cf_section_lineno(cs),
442 section_type_value[comp].typename);
449 ml = compile_modgroup(parent, comp, cs);
455 * We must assign a numeric index to this subcomponent.
456 * It is generated and placed in the dictionary by
457 * setup_modules(), when it loads the sections. If it
458 * isn't found, it's a serious error.
460 dval = dict_valbyname(section_type_value[comp].attr, name2);
463 "%s[%d]: %s %s Not previously configured",
464 cf_section_filename(cs), cf_section_lineno(cs),
465 section_type_value[comp].typename, name2);
466 modcallable_free(&ml);
470 subcomp = new_sublist(space, comp, dval->value);
472 modcallable_free(&ml);
476 subcomp->modulelist = ml;
480 static int load_component_section(modcallable *parent, CONF_SECTION *cs,
481 const char *space, int comp)
486 indexed_modcallable *subcomp;
488 const char *visiblename;
491 * Loop over the entries in the named section.
493 for (modref = cf_item_find_next(cs, NULL);
495 modref = cf_item_find_next(cs, modref)) {
496 CONF_PAIR *cp = NULL;
497 CONF_SECTION *scs = NULL;
500 * Look for Auth-Type foo {}, which are special
501 * cases of named sections, and allowable ONLY
504 * i.e. They're not allowed in a "group" or "redundant"
507 if (cf_item_is_section(modref)) {
508 const char *sec_name;
509 scs = cf_itemtosection(modref);
511 sec_name = cf_section_name1(scs);
514 section_type_value[comp].typename) == 0) {
515 if (!load_subcomponent_section(parent, scs,
517 return -1; /* FIXME: memleak? */
523 } else if (cf_item_is_pair(modref)) {
524 cp = cf_itemtopair(modref);
526 continue; /* ignore it */
530 * Try to compile one entry.
532 this = compile_modsingle(parent, comp, modref, &modname);
535 "%s[%d]: Failed to parse %s section.\n",
536 cf_section_filename(cs), cf_section_lineno(cs),
537 cf_section_name1(cs));
541 if (comp == RLM_COMPONENT_AUTH) {
543 const char *modrefname = NULL;
545 modrefname = cf_pair_attr(cp);
547 modrefname = cf_section_name2(scs);
550 "%s[%d]: Failed to parse %s sub-section.\n",
551 cf_section_filename(cs),
552 cf_section_lineno(cs),
553 cf_section_name1(scs));
558 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
561 * It's a section, but nothing we
564 radlog(L_ERR|L_CONS, "%s[%d]: Unknown Auth-Type \"%s\" in %s sub-section.",
565 cf_section_filename(cs), cf_section_lineno(cs),
566 modrefname, section_type_value[comp].section);
571 /* See the comment in new_sublist() for explanation
572 * of the special index 0 */
576 subcomp = new_sublist(space, comp, idx);
577 if (subcomp == NULL) {
578 modcallable_free(&this);
582 /* If subcomp->modulelist is NULL, add_to_modcallable will
584 visiblename = cf_section_name2(cs);
585 if (visiblename == NULL)
586 visiblename = cf_section_name1(cs);
587 add_to_modcallable(&subcomp->modulelist, this,
594 static int load_byspace(CONF_SECTION *cs, const char *space,
600 * Create any DICT_VALUE's for the types. See
601 * 'doc/configurable_failover' for examples of 'authtype'
602 * used to create new Auth-Type values. In order to
603 * let the user create new names, we've got to look for
604 * those names, and create DICT_VALUE's for them.
606 for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
615 * Not needed, don't load it.
617 if (!do_component[comp]) {
620 subcs = cf_section_sub_find(cs,
621 section_type_value[comp].section);
624 * FIXME: This is probably an error.
626 if (!subcs) continue;
628 for (ci = cf_item_find_next(subcs, NULL);
630 ci = cf_item_find_next(subcs, ci)) {
631 if (cf_item_is_section(ci)) {
635 type = cf_itemtosection(ci);
636 name1 = cf_section_name1(type);
639 * Not Autz-Type, Auth-Type, etc.
641 if (strcmp(name1, section_type_value[comp].typename) != 0) {
645 name2 = cf_section_name2(cf_itemtosection(ci));
648 * FIXME: This is probably an error.
650 if (!name2) continue;
652 } else if (section_type_value[comp].attr != PW_AUTH_TYPE) {
654 * Create types for names only when
655 * parsing the authenticate section.
660 name2 = cf_pair_attr(cf_itemtopair(ci));
664 * If the value already exists, don't
667 dval = dict_valbyname(section_type_value[comp].attr,
672 * Find the attribute for the value.
674 dattr = dict_attrbyvalue(section_type_value[comp].attr);
676 radlog(L_ERR, "%s[%d]: No such attribute %s",
677 mainconfig.radiusd_conf,
678 cf_section_lineno(subcs),
679 section_type_value[comp].typename);
684 * Create a new unique value with a
685 * meaningless number. You can't look at
686 * it from outside of this code, so it
687 * doesn't matter. The only requirement
688 * is that it's unique.
691 value = lrad_rand() & 0x00ffffff;
692 } while (dict_valbyattr(dattr->attr, value));
694 if (dict_addvalue(name2, dattr->name, value) < 0) {
695 radlog(L_ERR, "%s", librad_errstr);
699 } /* over the sections which can have redundent sub-sections */
701 DEBUG2(" modules {");
704 * Loop over all of the known components, finding their
705 * configuration section, and loading it.
707 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
710 if (!do_component[comp]) continue;
712 subcs = cf_section_sub_find(cs,
713 section_type_value[comp].section);
714 if (!subcs) continue;
716 if (cf_item_find_next(subcs, NULL) == NULL) continue;
718 DEBUG2(" Module: Checking %s {...} for more modules to load",
719 section_type_value[comp].section);
721 if (load_component_section(NULL, subcs, space, comp) < 0) {
725 } /* loop over components */
733 * Parse the module config sections, and load
734 * and call each module's init() function.
736 * Libtool makes your life a LOT easier, especially with libltdl.
737 * see: http://www.gnu.org/software/libtool/
739 int setup_modules(int reload, CONF_SECTION *config)
742 CONF_SECTION *cs, *modules;
743 int do_component[RLM_COMPONENT_COUNT];
744 rad_listen_t *listener;
747 * If necessary, initialize libltdl.
751 * Set the default list of preloaded symbols.
752 * This is used to initialize libltdl's list of
755 * i.e. Static modules.
757 LTDL_SET_PRELOADED_SYMBOLS();
759 if (lt_dlinit() != 0) {
760 radlog(L_ERR|L_CONS, "Failed to initialize libraries: %s\n",
766 * Set the search path to ONLY our library directory.
767 * This prevents the modules from being found from
768 * any location on the disk.
770 lt_dlsetsearchpath(radlib_dir);
772 DEBUG2("radiusd: Library search path is %s",
773 lt_dlgetsearchpath());
776 * Set up the internal module struct.
778 module_tree = rbtree_create(module_entry_cmp,
779 module_entry_free, 0);
781 radlog(L_ERR|L_CONS, "Failed to initialize modules\n");
785 rbtree_free(components);
788 components = rbtree_create(indexed_modcallable_cmp,
789 indexed_modcallable_free, 0);
791 radlog(L_ERR|L_CONS, "Failed to initialize components\n");
796 * Figure out which sections to load.
798 memset(do_component, 0, sizeof(do_component));
799 for (listener = mainconfig.listen;
801 listener = listener->next) {
802 switch (listener->type) {
803 case RAD_LISTEN_AUTH:
804 do_component[RLM_COMPONENT_AUTZ] = 1;
805 do_component[RLM_COMPONENT_AUTH] = 1;
806 do_component[RLM_COMPONENT_POST_AUTH] = 1;
807 do_component[RLM_COMPONENT_SESS] = 1;
810 case RAD_LISTEN_DETAIL: /* just like acct */
811 case RAD_LISTEN_ACCT:
812 do_component[RLM_COMPONENT_PREACCT] = 1;
813 do_component[RLM_COMPONENT_ACCT] = 1;
816 case RAD_LISTEN_PROXY:
817 do_component[RLM_COMPONENT_PRE_PROXY] = 1;
818 do_component[RLM_COMPONENT_POST_PROXY] = 1;
822 do_component[RLM_COMPONENT_POST_AUTH] = 1;
827 case RAD_LISTEN_SNMP:
836 for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
838 * Have the debugging messages all in one place.
840 if (!do_component[comp]) {
841 DEBUG2("modules: Not loading %s{} section",
842 section_type_value[comp].section);
847 * Remember where the modules were stored.
849 modules = cf_section_sub_find(config, "modules");
851 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
856 * Look for the 'instantiate' section, which tells us
857 * the instantiation order of the modules, and also allows
858 * us to load modules with no authorize/authenticate/etc.
861 cs = cf_section_sub_find(config, "instantiate");
865 module_instance_t *module;
868 DEBUG2(" instantiate {");
871 * Loop over the items in the 'instantiate' section.
873 for (ci=cf_item_find_next(cs, NULL);
875 ci=cf_item_find_next(cs, ci)) {
878 * Skip sections. They'll be handled
879 * later, if they're referenced at all...
881 if (cf_item_is_section(ci)) {
885 cp = cf_itemtopair(ci);
886 name = cf_pair_attr(cp);
887 module = find_module_instance(modules, name);
891 } /* loop over items in the subsection */
894 } /* if there's an 'instantiate' section. */
897 * Load *all*. If you don't want one loaded, don't list
898 * it in sites-enabled/
900 for (listener = mainconfig.listen;
902 listener = listener->next) {
903 if (listener->type != RAD_LISTEN_VQP) continue;
905 cs = cf_section_sub_find(config, "vmps");
907 radlog(L_ERR, "Listening on vmps socket, but no vmps section");
911 if (cf_item_find_next(cs, NULL) == NULL) {
912 radlog(L_ERR, "Listening on vmps socket, vmps section is empty");
916 DEBUG2(" Module: Checking vmps {...} for more modules to load");
918 if (load_component_section(NULL, cs, VMPS_SPACE,
919 RLM_COMPONENT_POST_AUTH) < 0) {
925 * Load all of the virtual servers.
927 for (cs = cf_subsection_find_next(config, NULL, "server");
929 cs = cf_subsection_find_next(config, cs, "server")) {
930 const char *name2 = cf_section_name2(cs);
932 DEBUG2("server %s {", name2);
933 if (load_byspace(cs, name2, do_component) < 0) {
940 if (load_byspace(config, NULL, do_component) < 0) {
949 * Call all authorization modules until one returns
950 * somethings else than RLM_MODULE_OK
952 int module_authorize(int autz_type, REQUEST *request)
954 return indexed_modcall(request->server,
955 RLM_COMPONENT_AUTZ, autz_type, request);
959 * Authenticate a user/password with various methods.
961 int module_authenticate(int auth_type, REQUEST *request)
963 return indexed_modcall(request->server,
964 RLM_COMPONENT_AUTH, auth_type, request);
968 * Do pre-accounting for ALL configured sessions
970 int module_preacct(REQUEST *request)
972 return indexed_modcall(request->server,
973 RLM_COMPONENT_PREACCT, 0, request);
977 * Do accounting for ALL configured sessions
979 int module_accounting(int acct_type, REQUEST *request)
981 return indexed_modcall(request->server,
982 RLM_COMPONENT_ACCT, acct_type, request);
986 * See if a user is already logged in.
988 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
990 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
994 if(!request->username)
997 request->simul_count = 0;
998 request->simul_max = maxsimul;
999 request->simul_mpp = 1;
1001 rcode = indexed_modcall(request->server,
1002 RLM_COMPONENT_SESS, sess_type, request);
1004 if (rcode != RLM_MODULE_OK) {
1005 /* FIXME: Good spot for a *rate-limited* warning to the log */
1009 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1013 * Do pre-proxying for ALL configured sessions
1015 int module_pre_proxy(int type, REQUEST *request)
1017 return indexed_modcall(request->server,
1018 RLM_COMPONENT_PRE_PROXY, type, request);
1022 * Do post-proxying for ALL configured sessions
1024 int module_post_proxy(int type, REQUEST *request)
1026 return indexed_modcall(request->server,
1027 RLM_COMPONENT_POST_PROXY, type, request);
1031 * Do post-authentication for ALL configured sessions
1033 int module_post_auth(int postauth_type, REQUEST *request)
1035 return indexed_modcall(request->server,
1036 RLM_COMPONENT_POST_AUTH, postauth_type, request);
1042 int module_vmps(REQUEST *request)
1044 return indexed_modcall(VMPS_SPACE, RLM_COMPONENT_POST_AUTH, 0,