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/autoconf.h>
34 #include <freeradius-devel/radiusd.h>
35 #include <freeradius-devel/modpriv.h>
36 #include <freeradius-devel/modcall.h>
37 #include <freeradius-devel/rad_assert.h>
39 typedef struct indexed_modcallable {
42 modcallable *modulelist;
43 } indexed_modcallable;
46 * For each component, keep an ordered list of ones to call.
48 static lrad_hash_table_t *components;
50 static rbtree_t *module_tree = NULL;
52 typedef struct section_type_value_t {
56 } section_type_value_t;
60 * Ordered by component
62 static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
63 { "authenticate", "Auth-Type", PW_AUTH_TYPE },
64 { "authorize", "Autz-Type", PW_AUTZ_TYPE },
65 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
66 { "accounting", "Acct-Type", PW_ACCT_TYPE },
67 { "session", "Session-Type", PW_SESSION_TYPE },
68 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
69 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
70 { "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE },
76 static const section_type_value_t old_section_type_value[] = {
77 { "authenticate", "authtype", PW_AUTH_TYPE },
78 { "authorize", "autztype", PW_AUTZ_TYPE },
79 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },/* unused */
80 { "accounting", "acctype", PW_ACCT_TYPE },
81 { "session", "sesstype", PW_SESSION_TYPE },
82 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE }, /* unused */
83 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE }, /* unused */
84 { "post-auth", "post-authtype", PW_POST_AUTH_TYPE }
88 static void indexed_modcallable_free(void *data)
90 indexed_modcallable *c = data;
92 modcallable_free(&c->modulelist);
96 static uint32_t indexed_modcallable_hash(const void *data)
99 const indexed_modcallable *c = data;
101 hash = lrad_hash(&c->comp, sizeof(c->comp));
102 return lrad_hash_update(&c->idx, sizeof(c->idx), hash);
105 static int indexed_modcallable_cmp(const void *one, const void *two)
107 const indexed_modcallable *a = one;
108 const indexed_modcallable *b = two;
110 if (a->comp < b->comp) return -1;
111 if (a->comp > b->comp) return +1;
113 return a->idx - b->idx;
118 * Free a module instance.
120 static void module_instance_free(void *data)
122 module_instance_t *this = data;
124 if (this->entry->module->detach)
125 (this->entry->module->detach)(this->insthandle);
126 #ifdef HAVE_PTHREAD_H
130 * The mutex MIGHT be locked...
131 * we'll check for that later, I guess.
133 pthread_mutex_destroy(this->mutex);
142 * Compare two module entries
144 static int module_entry_cmp(const void *one, const void *two)
146 const module_entry_t *a = one;
147 const module_entry_t *b = two;
149 return strcmp(a->name, b->name);
153 * Free a module entry.
155 static void module_entry_free(void *data)
157 module_entry_t *this = data;
159 lt_dlclose(this->handle); /* ignore any errors */
165 * Remove the module lists.
167 int detach_modules(void)
169 lrad_hash_table_free(components);
176 * Find a module on disk or in memory, and link to it.
178 static module_entry_t *linkto_module(const char *module_name,
179 const char *cffilename, int cflineno)
181 module_entry_t myentry;
182 module_entry_t *node;
184 char module_struct[256];
188 strNcpy(myentry.name, module_name, sizeof(myentry.name));
189 node = rbtree_finddata(module_tree, &myentry);
190 if (node) return node;
193 * Keep the handle around so we can dlclose() it.
195 handle = lt_dlopenext(module_name);
196 if (handle == NULL) {
197 radlog(L_ERR|L_CONS, "%s[%d] Failed to link to module '%s':"
198 " %s\n", cffilename, cflineno, module_name, lt_dlerror());
203 * Link to the module's rlm_FOO{} module structure.
205 * The module_name variable has the version number
206 * embedded in it, and we don't want that here.
208 strcpy(module_struct, module_name);
209 p = strrchr(module_struct, '-');
212 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
215 * libltld MAY core here, if the handle it gives us contains
218 module = lt_dlsym(handle, module_struct);
220 radlog(L_ERR|L_CONS, "%s[%d] Failed linking to "
221 "%s structure in %s: %s\n",
222 cffilename, cflineno,
223 module_name, cffilename, lt_dlerror());
228 * Before doing anything else, check if it's sane.
230 if ((*(const uint32_t *) module) != RLM_MODULE_MAGIC_NUMBER) {
232 radlog(L_ERR|L_CONS, "%s[%d] Invalid version in module '%s'",
233 cffilename, cflineno, module_name);
238 /* make room for the module type */
239 node = rad_malloc(sizeof(*node));
240 memset(node, 0, sizeof(*node));
241 strNcpy(node->name, module_name, sizeof(node->name));
242 node->module = module;
243 node->handle = handle;
245 DEBUG("Module: Loaded %s ", node->module->name);
248 * Add the module as "rlm_foo-version" to the configuration
251 if (!rbtree_insert(module_tree, node)) {
252 radlog(L_ERR, "Failed to cache module %s", module_name);
262 * Find a module instance.
264 module_instance_t *find_module_instance(CONF_SECTION *modules,
265 const char *instname)
268 const char *name1, *name2;
269 module_instance_t *node;
270 char module_name[256];
272 if (!modules) return NULL;
275 * Module instances are declared in the modules{} block
276 * and referenced later by their name, which is the
277 * name2 from the config section, or name1 if there was
280 cs = cf_section_sub_find_name2(modules, NULL, instname);
282 radlog(L_ERR|L_CONS, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
287 * If there's already a module instance, return it.
289 node = cf_data_find(cs, "instance");
290 if (node) return node;
292 name1 = cf_section_name1(cs);
293 name2 = cf_section_name2(cs);
296 * Found the configuration entry.
298 node = rad_malloc(sizeof(*node));
299 memset(node, 0, sizeof(*node));
301 node->insthandle = NULL;
304 * Names in the "modules" section aren't prefixed
305 * with "rlm_", so we add it here.
307 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
309 node->entry = linkto_module(module_name,
310 mainconfig.radiusd_conf,
311 cf_section_lineno(cs));
314 /* linkto_module logs any errors */
319 * Call the module's instantiation routine.
321 if ((node->entry->module->instantiate) &&
322 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
324 "%s[%d]: %s: Module instantiation failed.\n",
325 mainconfig.radiusd_conf, cf_section_lineno(cs),
332 * We're done. Fill in the rest of the data structure,
333 * and link it to the module instance list.
335 strNcpy(node->name, instname, sizeof(node->name));
337 #ifdef HAVE_PTHREAD_H
339 * If we're threaded, check if the module is thread-safe.
341 * If it isn't, we create a mutex.
343 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
344 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
346 * Initialize the mutex.
348 pthread_mutex_init(node->mutex, NULL);
351 * The module is thread-safe. Don't give it a mutex.
357 cf_data_add(cs, "instance", node, module_instance_free);
359 DEBUG("Module: Instantiated %s (%s) ", name1, node->name);
364 static indexed_modcallable *lookup_by_index(int comp, int idx)
366 indexed_modcallable myc;
371 return lrad_hash_table_finddata(components, &myc);
375 * Create a new sublist.
377 static indexed_modcallable *new_sublist(int comp, int idx)
379 indexed_modcallable *c;
381 c = lookup_by_index(comp, idx);
383 /* It is an error to try to create a sublist that already
384 * exists. It would almost certainly be caused by accidental
385 * duplication in the config file.
387 * index 0 is the exception, because it is used when we want
388 * to collect _all_ listed modules under a single index by
389 * default, which is currently the case in all components
390 * except authenticate. */
398 c = rad_malloc(sizeof(*c));
399 c->modulelist = NULL;
403 if (!lrad_hash_table_insert(components, c)) {
411 static int indexed_modcall(int comp, int idx, REQUEST *request)
414 indexed_modcallable *this;
416 this = lookup_by_index(comp, idx);
418 if (idx != 0) DEBUG2(" ERROR: Unknown value specified for %s. Cannot perform requested action.",
419 section_type_value[comp].typename);
420 request->component = section_type_value[comp].typename;
421 rcode = modcall(comp, NULL, request); /* does default action */
423 DEBUG2(" Processing the %s section of %s",
424 section_type_value[comp].section,
425 mainconfig.radiusd_conf);
426 request->component = section_type_value[comp].typename;
427 rcode = modcall(comp, this->modulelist, request);
429 request->module = "<server-core>";
430 request->component = "<server-core>";
435 * Load a sub-module list, as found inside an Auth-Type foo {}
438 static int load_subcomponent_section(modcallable *parent,
439 CONF_SECTION *cs, int comp,
440 const char *filename)
442 indexed_modcallable *subcomp;
445 const char *name2 = cf_section_name2(cs);
447 rad_assert(comp >= RLM_COMPONENT_AUTH);
448 rad_assert(comp <= RLM_COMPONENT_COUNT);
455 "%s[%d]: No name specified for %s block",
456 filename, cf_section_lineno(cs),
457 section_type_value[comp].typename);
464 ml = compile_modgroup(parent, comp, cs, filename);
470 * We must assign a numeric index to this subcomponent.
471 * It is generated and placed in the dictionary by
472 * setup_modules(), when it loads the sections. If it
473 * isn't found, it's a serious error.
475 dval = dict_valbyname(section_type_value[comp].attr, name2);
478 "%s[%d] %s %s Not previously configured",
479 filename, cf_section_lineno(cs),
480 section_type_value[comp].typename, name2);
481 modcallable_free(&ml);
485 subcomp = new_sublist(comp, dval->value);
488 "%s[%d] %s %s already configured - skipping",
489 filename, cf_section_lineno(cs),
490 section_type_value[comp].typename, name2);
491 modcallable_free(&ml);
495 subcomp->modulelist = ml;
499 static int load_component_section(modcallable *parent,
500 CONF_SECTION *cs, int comp,
501 const char *filename)
506 indexed_modcallable *subcomp;
508 const char *visiblename;
511 * Loop over the entries in the named section.
513 for (modref = cf_item_find_next(cs, NULL);
515 modref = cf_item_find_next(cs, modref)) {
516 CONF_PAIR *cp = NULL;
517 CONF_SECTION *scs = NULL;
520 * Look for Auth-Type foo {}, which are special
521 * cases of named sections, and allowable ONLY
524 * i.e. They're not allowed in a "group" or "redundant"
527 if (cf_item_is_section(modref)) {
528 const char *sec_name;
529 scs = cf_itemtosection(modref);
531 sec_name = cf_section_name1(scs);
534 section_type_value[comp].typename) == 0) {
535 if (!load_subcomponent_section(parent, scs,
538 return -1; /* FIXME: memleak? */
544 * Allow old names, too.
547 old_section_type_value[comp].typename) == 0) {
548 if (!load_subcomponent_section(parent, scs,
551 return -1; /* FIXME: memleak? */
556 } else if (cf_item_is_pair(modref)) {
557 cp = cf_itemtopair(modref);
559 continue; /* ignore it */
563 * Try to compile one entry.
565 this = compile_modsingle(parent, comp, modref, filename,
569 "%s[%d] Failed to parse %s section.\n",
570 filename, cf_section_lineno(cs),
571 cf_section_name1(cs));
575 if (comp == RLM_COMPONENT_AUTH) {
577 const char *modrefname = NULL;
581 modrefname = cf_pair_attr(cp);
582 lineno = cf_pair_lineno(cp);
584 modrefname = cf_section_name2(scs);
585 lineno = cf_section_lineno(scs);
588 "%s[%d] Failed to parse %s sub-section.\n",
590 cf_section_name1(scs));
595 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
598 * It's a section, but nothing we
601 radlog(L_ERR|L_CONS, "%s[%d] Unknown Auth-Type \"%s\" in %s sub-section.",
603 modrefname, section_type_value[comp].section);
608 /* See the comment in new_sublist() for explanation
609 * of the special index 0 */
613 subcomp = new_sublist(comp, idx);
614 if (subcomp == NULL) {
615 radlog(L_INFO|L_CONS,
616 "%s %s %s already configured - skipping",
617 filename, section_type_value[comp].typename,
619 modcallable_free(&this);
623 /* If subcomp->modulelist is NULL, add_to_modcallable will
625 visiblename = cf_section_name2(cs);
626 if (visiblename == NULL)
627 visiblename = cf_section_name1(cs);
628 add_to_modcallable(&subcomp->modulelist, this,
637 * Parse the module config sections, and load
638 * and call each module's init() function.
640 * Libtool makes your life a LOT easier, especially with libltdl.
641 * see: http://www.gnu.org/software/libtool/
643 int setup_modules(int reload)
646 CONF_SECTION *cs, *modules;
647 int do_component[RLM_COMPONENT_COUNT];
648 rad_listen_t *listener;
651 * If necessary, initialize libltdl.
655 * Set the default list of preloaded symbols.
656 * This is used to initialize libltdl's list of
659 * i.e. Static modules.
661 LTDL_SET_PRELOADED_SYMBOLS();
663 if (lt_dlinit() != 0) {
664 radlog(L_ERR|L_CONS, "Failed to initialize libraries: %s\n",
670 * Set the search path to ONLY our library directory.
671 * This prevents the modules from being found from
672 * any location on the disk.
674 lt_dlsetsearchpath(radlib_dir);
676 DEBUG2("Module: Library search path is %s",
677 lt_dlgetsearchpath());
680 * Set up the internal module struct.
682 module_tree = rbtree_create(module_entry_cmp,
683 module_entry_free, 0);
685 radlog(L_ERR|L_CONS, "Failed to initialize modules\n");
689 lrad_hash_table_free(components);
692 components = lrad_hash_table_create(indexed_modcallable_hash,
693 indexed_modcallable_cmp,
694 indexed_modcallable_free);
696 radlog(L_ERR|L_CONS, "Failed to initialize components\n");
701 * Figure out which sections to load.
703 memset(do_component, 0, sizeof(do_component));
704 for (listener = mainconfig.listen;
706 listener = listener->next) {
707 switch (listener->type) {
708 case RAD_LISTEN_AUTH:
709 do_component[RLM_COMPONENT_AUTZ] = 1;
710 do_component[RLM_COMPONENT_AUTH] = 1;
711 do_component[RLM_COMPONENT_POST_AUTH] = 1;
712 do_component[RLM_COMPONENT_SESS] = 1;
715 case RAD_LISTEN_DETAIL: /* just like acct */
716 case RAD_LISTEN_ACCT:
717 do_component[RLM_COMPONENT_PREACCT] = 1;
718 do_component[RLM_COMPONENT_ACCT] = 1;
721 case RAD_LISTEN_PROXY:
722 do_component[RLM_COMPONENT_PRE_PROXY] = 1;
723 do_component[RLM_COMPONENT_POST_PROXY] = 1;
732 for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
734 * Have the debugging messages all in one place.
736 if (!do_component[comp]) {
737 DEBUG2("modules: Not loading %s{} section",
738 section_type_value[comp].section);
743 * Create any DICT_VALUE's for the types. See
744 * 'doc/configurable_failover' for examples of 'authtype'
745 * used to create new Auth-Type values. In order to
746 * let the user create new names, we've got to look for
747 * those names, and create DICT_VALUE's for them.
749 for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
754 CONF_SECTION *sub, *next;
758 * Not needed, don't load it.
760 if (!do_component[comp]) {
763 cs = cf_section_find(section_type_value[comp].section);
770 * See if there's a sub-section by that
773 next = cf_subsection_find_next(cs, sub,
774 section_type_value[comp].typename);
777 * Allow some old names, too.
779 if (!next && (comp <= 4)) {
780 next = cf_subsection_find_next(cs, sub,
781 old_section_type_value[comp].typename);
786 * If so, look for it to define a new
789 name2 = cf_section_name2(sub);
790 if (!name2) continue;
794 * If the value already exists, don't
797 dval = dict_valbyname(section_type_value[comp].attr,
802 * Find the attribute for the value.
804 dattr = dict_attrbyvalue(section_type_value[comp].attr);
806 radlog(L_ERR, "%s[%d]: No such attribute %s",
807 mainconfig.radiusd_conf,
808 cf_section_lineno(sub),
809 section_type_value[comp].typename);
814 * Create a new unique value with a
815 * meaningless number. You can't look at
816 * it from outside of this code, so it
817 * doesn't matter. The only requirement
818 * is that it's unique.
821 value = lrad_rand() & 0x00ffffff;
822 } while (dict_valbyattr(dattr->attr, value));
824 if (dict_addvalue(name2, dattr->name, value) < 0) {
825 radlog(L_ERR, "%s", librad_errstr);
828 } while (sub != NULL);
831 * Loop over the non-sub-sections, too.
836 * See if there's a conf-pair by that
839 cp = cf_pair_find_next(cs, cp, NULL);
844 * If the value already exists, don't
847 name2 = cf_pair_attr(cp);
848 dval = dict_valbyname(section_type_value[comp].attr,
853 * Find the attribute for the value.
855 dattr = dict_attrbyvalue(section_type_value[comp].attr);
857 radlog(L_ERR, "%s[%d]: No such attribute %s",
858 mainconfig.radiusd_conf,
859 cf_section_lineno(sub),
860 section_type_value[comp].typename);
865 * Finally, create the new attribute.
868 value = lrad_rand() & 0x00ffffff;
869 } while (dict_valbyattr(dattr->attr, value));
870 if (dict_addvalue(name2, dattr->name, value) < 0) {
871 radlog(L_ERR, "%s", librad_errstr);
874 } while (cp != NULL);
875 } /* over the sections which can have redundent sub-sections */
878 * Remember where the modules were stored.
880 modules = cf_section_find("modules");
882 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
887 * Look for the 'instantiate' section, which tells us
888 * the instantiation order of the modules, and also allows
889 * us to load modules with no authorize/authenticate/etc.
892 cs = cf_section_find("instantiate");
896 module_instance_t *module;
900 * Loop over the items in the 'instantiate' section.
902 for (ci=cf_item_find_next(cs, NULL);
904 ci=cf_item_find_next(cs, ci)) {
907 * Skip sections. They'll be handled
908 * later, if they're referenced at all...
910 if (cf_item_is_section(ci)) {
914 cp = cf_itemtopair(ci);
915 name = cf_pair_attr(cp);
916 module = find_module_instance(modules, name);
920 } /* loop over items in the subsection */
921 } /* if there's an 'instantiate' section. */
924 * Loop over all of the known components, finding their
925 * configuration section, and loading it.
927 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
928 cs = cf_section_find(section_type_value[comp].section);
932 if (!do_component[comp]) {
936 if (load_component_section(NULL, cs, comp, mainconfig.radiusd_conf) < 0) {
945 * Call all authorization modules until one returns
946 * somethings else than RLM_MODULE_OK
948 int module_authorize(int autz_type, REQUEST *request)
951 * Older versions of the server would pass proxy requests
952 * through the 'authorize' sections twice; once when the
953 * packet was received from the NAS, and again after the
954 * reply was received from the home server. Now that we
955 * have a 'post_proxy' section, the replies from the home
956 * server should be sent through that, instead of through
957 * the 'authorize' section again.
959 if (request->proxy != NULL) {
960 DEBUG2(" authorize: Skipping authorize in post-proxy stage");
961 return RLM_MODULE_NOOP;
964 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
968 * Authenticate a user/password with various methods.
970 int module_authenticate(int auth_type, REQUEST *request)
972 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
976 * Do pre-accounting for ALL configured sessions
978 int module_preacct(REQUEST *request)
980 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
984 * Do accounting for ALL configured sessions
986 int module_accounting(int acct_type, REQUEST *request)
988 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
992 * See if a user is already logged in.
994 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
996 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1000 if(!request->username)
1003 request->simul_count = 0;
1004 request->simul_max = maxsimul;
1005 request->simul_mpp = 1;
1007 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1009 if (rcode != RLM_MODULE_OK) {
1010 /* FIXME: Good spot for a *rate-limited* warning to the log */
1014 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1018 * Do pre-proxying for ALL configured sessions
1020 int module_pre_proxy(int type, REQUEST *request)
1022 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1026 * Do post-proxying for ALL configured sessions
1028 int module_post_proxy(int type, REQUEST *request)
1030 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1034 * Do post-authentication for ALL configured sessions
1036 int module_post_auth(int postauth_type, REQUEST *request)
1038 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);