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 typedef struct indexed_modcallable {
36 modcallable *modulelist;
37 } indexed_modcallable;
40 * For each component, keep an ordered list of ones to call.
42 static lrad_hash_table_t *components;
44 static rbtree_t *module_tree = NULL;
46 typedef struct section_type_value_t {
50 } section_type_value_t;
54 * Ordered by component
56 static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
57 { "authenticate", "Auth-Type", PW_AUTH_TYPE },
58 { "authorize", "Autz-Type", PW_AUTZ_TYPE },
59 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
60 { "accounting", "Acct-Type", PW_ACCT_TYPE },
61 { "session", "Session-Type", PW_SESSION_TYPE },
62 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
63 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
64 { "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE },
70 static const section_type_value_t old_section_type_value[] = {
71 { "authenticate", "authtype", PW_AUTH_TYPE },
72 { "authorize", "autztype", PW_AUTZ_TYPE },
73 { "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },/* unused */
74 { "accounting", "acctype", PW_ACCT_TYPE },
75 { "session", "sesstype", PW_SESSION_TYPE },
76 { "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE }, /* unused */
77 { "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE }, /* unused */
78 { "post-auth", "post-authtype", PW_POST_AUTH_TYPE }
82 static void indexed_modcallable_free(void *data)
84 indexed_modcallable *c = data;
86 modcallable_free(&c->modulelist);
90 static uint32_t indexed_modcallable_hash(const void *data)
93 const indexed_modcallable *c = data;
95 hash = lrad_hash(&c->comp, sizeof(c->comp));
96 return lrad_hash_update(&c->idx, sizeof(c->idx), hash);
99 static int indexed_modcallable_cmp(const void *one, const void *two)
101 const indexed_modcallable *a = one;
102 const indexed_modcallable *b = two;
104 if (a->comp < b->comp) return -1;
105 if (a->comp > b->comp) return +1;
107 return a->idx - b->idx;
112 * Free a module instance.
114 static void module_instance_free(void *data)
116 module_instance_t *this = data;
118 if (this->entry->module->detach)
119 (this->entry->module->detach)(this->insthandle);
120 #ifdef HAVE_PTHREAD_H
124 * The mutex MIGHT be locked...
125 * we'll check for that later, I guess.
127 pthread_mutex_destroy(this->mutex);
136 * Compare two module entries
138 static int module_entry_cmp(const void *one, const void *two)
140 const module_entry_t *a = one;
141 const module_entry_t *b = two;
143 return strcmp(a->name, b->name);
147 * Free a module entry.
149 static void module_entry_free(void *data)
151 module_entry_t *this = data;
153 lt_dlclose(this->handle); /* ignore any errors */
159 * Remove the module lists.
161 int detach_modules(void)
163 lrad_hash_table_free(components);
164 rbtree_free(module_tree);
171 * Find a module on disk or in memory, and link to it.
173 static module_entry_t *linkto_module(const char *module_name,
174 const char *cffilename, int cflineno)
176 module_entry_t myentry;
177 module_entry_t *node;
179 char module_struct[256];
183 strlcpy(myentry.name, module_name, sizeof(myentry.name));
184 node = rbtree_finddata(module_tree, &myentry);
185 if (node) return node;
188 * Keep the handle around so we can dlclose() it.
190 handle = lt_dlopenext(module_name);
191 if (handle == NULL) {
192 radlog(L_ERR|L_CONS, "%s[%d] Failed to link to module '%s':"
193 " %s\n", cffilename, cflineno, module_name, lt_dlerror());
198 * Link to the module's rlm_FOO{} module structure.
200 * The module_name variable has the version number
201 * embedded in it, and we don't want that here.
203 strcpy(module_struct, module_name);
204 p = strrchr(module_struct, '-');
207 DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
210 * libltld MAY core here, if the handle it gives us contains
213 module = lt_dlsym(handle, module_struct);
215 radlog(L_ERR|L_CONS, "%s[%d] Failed linking to "
216 "%s structure in %s: %s\n",
217 cffilename, cflineno,
218 module_name, cffilename, lt_dlerror());
223 * Before doing anything else, check if it's sane.
225 if ((*(const uint32_t *) module) != RLM_MODULE_MAGIC_NUMBER) {
227 radlog(L_ERR|L_CONS, "%s[%d] Invalid version in module '%s'",
228 cffilename, cflineno, module_name);
233 /* make room for the module type */
234 node = rad_malloc(sizeof(*node));
235 memset(node, 0, sizeof(*node));
236 strlcpy(node->name, module_name, sizeof(node->name));
237 node->module = module;
238 node->handle = handle;
240 DEBUG("Module: Loaded %s ", node->module->name);
243 * Add the module as "rlm_foo-version" to the configuration
246 if (!rbtree_insert(module_tree, node)) {
247 radlog(L_ERR, "Failed to cache module %s", module_name);
257 * Find a module instance.
259 module_instance_t *find_module_instance(CONF_SECTION *modules,
260 const char *instname)
263 const char *name1, *name2;
264 module_instance_t *node;
265 char module_name[256];
267 if (!modules) return NULL;
270 * Module instances are declared in the modules{} block
271 * and referenced later by their name, which is the
272 * name2 from the config section, or name1 if there was
275 cs = cf_section_sub_find_name2(modules, NULL, instname);
277 radlog(L_ERR|L_CONS, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
282 * If there's already a module instance, return it.
284 node = cf_data_find(cs, "instance");
285 if (node) return node;
287 name1 = cf_section_name1(cs);
288 name2 = cf_section_name2(cs);
291 * Found the configuration entry.
293 node = rad_malloc(sizeof(*node));
294 memset(node, 0, sizeof(*node));
296 node->insthandle = NULL;
299 * Names in the "modules" section aren't prefixed
300 * with "rlm_", so we add it here.
302 snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
304 node->entry = linkto_module(module_name,
305 mainconfig.radiusd_conf,
306 cf_section_lineno(cs));
309 /* linkto_module logs any errors */
314 * Call the module's instantiation routine.
316 if ((node->entry->module->instantiate) &&
317 ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
319 "%s[%d]: %s: Module instantiation failed.\n",
320 mainconfig.radiusd_conf, cf_section_lineno(cs),
327 * We're done. Fill in the rest of the data structure,
328 * and link it to the module instance list.
330 strlcpy(node->name, instname, sizeof(node->name));
332 #ifdef HAVE_PTHREAD_H
334 * If we're threaded, check if the module is thread-safe.
336 * If it isn't, we create a mutex.
338 if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
339 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
341 * Initialize the mutex.
343 pthread_mutex_init(node->mutex, NULL);
346 * The module is thread-safe. Don't give it a mutex.
352 cf_data_add(cs, "instance", node, module_instance_free);
354 DEBUG("Module: Instantiated %s (%s) ", name1, node->name);
359 static indexed_modcallable *lookup_by_index(int comp, int idx)
361 indexed_modcallable myc;
366 return lrad_hash_table_finddata(components, &myc);
370 * Create a new sublist.
372 static indexed_modcallable *new_sublist(int comp, int idx)
374 indexed_modcallable *c;
376 c = lookup_by_index(comp, idx);
378 /* It is an error to try to create a sublist that already
379 * exists. It would almost certainly be caused by accidental
380 * duplication in the config file.
382 * index 0 is the exception, because it is used when we want
383 * to collect _all_ listed modules under a single index by
384 * default, which is currently the case in all components
385 * except authenticate. */
393 c = rad_malloc(sizeof(*c));
394 c->modulelist = NULL;
398 if (!lrad_hash_table_insert(components, c)) {
406 static int indexed_modcall(int comp, int idx, REQUEST *request)
409 indexed_modcallable *this;
411 this = lookup_by_index(comp, idx);
413 if (idx != 0) DEBUG2(" ERROR: Unknown value specified for %s. Cannot perform requested action.",
414 section_type_value[comp].typename);
415 request->component = section_type_value[comp].typename;
416 rcode = modcall(comp, NULL, request); /* does default action */
418 DEBUG2(" Processing the %s section of %s",
419 section_type_value[comp].section,
420 mainconfig.radiusd_conf);
421 request->component = section_type_value[comp].typename;
422 rcode = modcall(comp, this->modulelist, request);
424 request->module = "<server-core>";
425 request->component = "<server-core>";
430 * Load a sub-module list, as found inside an Auth-Type foo {}
433 static int load_subcomponent_section(modcallable *parent,
434 CONF_SECTION *cs, int comp,
435 const char *filename)
437 indexed_modcallable *subcomp;
440 const char *name2 = cf_section_name2(cs);
442 rad_assert(comp >= RLM_COMPONENT_AUTH);
443 rad_assert(comp <= RLM_COMPONENT_COUNT);
450 "%s[%d]: No name specified for %s block",
451 filename, cf_section_lineno(cs),
452 section_type_value[comp].typename);
459 ml = compile_modgroup(parent, comp, cs, filename);
465 * We must assign a numeric index to this subcomponent.
466 * It is generated and placed in the dictionary by
467 * setup_modules(), when it loads the sections. If it
468 * isn't found, it's a serious error.
470 dval = dict_valbyname(section_type_value[comp].attr, name2);
473 "%s[%d] %s %s Not previously configured",
474 filename, cf_section_lineno(cs),
475 section_type_value[comp].typename, name2);
476 modcallable_free(&ml);
480 subcomp = new_sublist(comp, dval->value);
483 "%s[%d] %s %s already configured - skipping",
484 filename, cf_section_lineno(cs),
485 section_type_value[comp].typename, name2);
486 modcallable_free(&ml);
490 subcomp->modulelist = ml;
494 static int load_component_section(modcallable *parent,
495 CONF_SECTION *cs, int comp,
496 const char *filename)
501 indexed_modcallable *subcomp;
503 const char *visiblename;
506 * Loop over the entries in the named section.
508 for (modref = cf_item_find_next(cs, NULL);
510 modref = cf_item_find_next(cs, modref)) {
511 CONF_PAIR *cp = NULL;
512 CONF_SECTION *scs = NULL;
515 * Look for Auth-Type foo {}, which are special
516 * cases of named sections, and allowable ONLY
519 * i.e. They're not allowed in a "group" or "redundant"
522 if (cf_item_is_section(modref)) {
523 const char *sec_name;
524 scs = cf_itemtosection(modref);
526 sec_name = cf_section_name1(scs);
529 section_type_value[comp].typename) == 0) {
530 if (!load_subcomponent_section(parent, scs,
533 return -1; /* FIXME: memleak? */
539 * Allow old names, too.
542 old_section_type_value[comp].typename) == 0) {
543 if (!load_subcomponent_section(parent, scs,
546 return -1; /* FIXME: memleak? */
551 } else if (cf_item_is_pair(modref)) {
552 cp = cf_itemtopair(modref);
554 continue; /* ignore it */
558 * Try to compile one entry.
560 this = compile_modsingle(parent, comp, modref, filename,
564 "%s[%d] Failed to parse %s section.\n",
565 filename, cf_section_lineno(cs),
566 cf_section_name1(cs));
570 if (comp == RLM_COMPONENT_AUTH) {
572 const char *modrefname = NULL;
576 modrefname = cf_pair_attr(cp);
577 lineno = cf_pair_lineno(cp);
579 modrefname = cf_section_name2(scs);
580 lineno = cf_section_lineno(scs);
583 "%s[%d] Failed to parse %s sub-section.\n",
585 cf_section_name1(scs));
590 dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
593 * It's a section, but nothing we
596 radlog(L_ERR|L_CONS, "%s[%d] Unknown Auth-Type \"%s\" in %s sub-section.",
598 modrefname, section_type_value[comp].section);
603 /* See the comment in new_sublist() for explanation
604 * of the special index 0 */
608 subcomp = new_sublist(comp, idx);
609 if (subcomp == NULL) {
610 radlog(L_INFO|L_CONS,
611 "%s %s %s already configured - skipping",
612 filename, section_type_value[comp].typename,
614 modcallable_free(&this);
618 /* If subcomp->modulelist is NULL, add_to_modcallable will
620 visiblename = cf_section_name2(cs);
621 if (visiblename == NULL)
622 visiblename = cf_section_name1(cs);
623 add_to_modcallable(&subcomp->modulelist, this,
632 * Parse the module config sections, and load
633 * and call each module's init() function.
635 * Libtool makes your life a LOT easier, especially with libltdl.
636 * see: http://www.gnu.org/software/libtool/
638 int setup_modules(int reload)
641 CONF_SECTION *cs, *modules;
642 int do_component[RLM_COMPONENT_COUNT];
643 rad_listen_t *listener;
646 * If necessary, initialize libltdl.
650 * Set the default list of preloaded symbols.
651 * This is used to initialize libltdl's list of
654 * i.e. Static modules.
656 LTDL_SET_PRELOADED_SYMBOLS();
658 if (lt_dlinit() != 0) {
659 radlog(L_ERR|L_CONS, "Failed to initialize libraries: %s\n",
665 * Set the search path to ONLY our library directory.
666 * This prevents the modules from being found from
667 * any location on the disk.
669 lt_dlsetsearchpath(radlib_dir);
671 DEBUG2("Module: Library search path is %s",
672 lt_dlgetsearchpath());
675 * Set up the internal module struct.
677 module_tree = rbtree_create(module_entry_cmp,
678 module_entry_free, 0);
680 radlog(L_ERR|L_CONS, "Failed to initialize modules\n");
684 lrad_hash_table_free(components);
687 components = lrad_hash_table_create(indexed_modcallable_hash,
688 indexed_modcallable_cmp,
689 indexed_modcallable_free);
691 radlog(L_ERR|L_CONS, "Failed to initialize components\n");
696 * Figure out which sections to load.
698 memset(do_component, 0, sizeof(do_component));
699 for (listener = mainconfig.listen;
701 listener = listener->next) {
702 switch (listener->type) {
703 case RAD_LISTEN_AUTH:
704 do_component[RLM_COMPONENT_AUTZ] = 1;
705 do_component[RLM_COMPONENT_AUTH] = 1;
706 do_component[RLM_COMPONENT_POST_AUTH] = 1;
707 do_component[RLM_COMPONENT_SESS] = 1;
710 case RAD_LISTEN_DETAIL: /* just like acct */
711 case RAD_LISTEN_ACCT:
712 do_component[RLM_COMPONENT_PREACCT] = 1;
713 do_component[RLM_COMPONENT_ACCT] = 1;
716 case RAD_LISTEN_PROXY:
717 do_component[RLM_COMPONENT_PRE_PROXY] = 1;
718 do_component[RLM_COMPONENT_POST_PROXY] = 1;
728 for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
730 * Have the debugging messages all in one place.
732 if (!do_component[comp]) {
733 DEBUG2("modules: Not loading %s{} section",
734 section_type_value[comp].section);
739 * Create any DICT_VALUE's for the types. See
740 * 'doc/configurable_failover' for examples of 'authtype'
741 * used to create new Auth-Type values. In order to
742 * let the user create new names, we've got to look for
743 * those names, and create DICT_VALUE's for them.
745 for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
750 CONF_SECTION *sub, *next;
754 * Not needed, don't load it.
756 if (!do_component[comp]) {
759 cs = cf_section_find(section_type_value[comp].section);
766 * See if there's a sub-section by that
769 next = cf_subsection_find_next(cs, sub,
770 section_type_value[comp].typename);
773 * Allow some old names, too.
775 if (!next && (comp <= 4)) {
776 next = cf_subsection_find_next(cs, sub,
777 old_section_type_value[comp].typename);
782 * If so, look for it to define a new
785 name2 = cf_section_name2(sub);
786 if (!name2) continue;
790 * If the value already exists, don't
793 dval = dict_valbyname(section_type_value[comp].attr,
798 * Find the attribute for the value.
800 dattr = dict_attrbyvalue(section_type_value[comp].attr);
802 radlog(L_ERR, "%s[%d]: No such attribute %s",
803 mainconfig.radiusd_conf,
804 cf_section_lineno(sub),
805 section_type_value[comp].typename);
810 * Create a new unique value with a
811 * meaningless number. You can't look at
812 * it from outside of this code, so it
813 * doesn't matter. The only requirement
814 * is that it's unique.
817 value = lrad_rand() & 0x00ffffff;
818 } while (dict_valbyattr(dattr->attr, value));
820 if (dict_addvalue(name2, dattr->name, value) < 0) {
821 radlog(L_ERR, "%s", librad_errstr);
824 } while (sub != NULL);
827 * Loop over the non-sub-sections, too.
832 * See if there's a conf-pair by that
835 cp = cf_pair_find_next(cs, cp, NULL);
840 * If the value already exists, don't
843 name2 = cf_pair_attr(cp);
844 dval = dict_valbyname(section_type_value[comp].attr,
849 * Find the attribute for the value.
851 dattr = dict_attrbyvalue(section_type_value[comp].attr);
853 radlog(L_ERR, "%s[%d]: No such attribute %s",
854 mainconfig.radiusd_conf,
855 cf_section_lineno(sub),
856 section_type_value[comp].typename);
861 * Finally, create the new attribute.
864 value = lrad_rand() & 0x00ffffff;
865 } while (dict_valbyattr(dattr->attr, value));
866 if (dict_addvalue(name2, dattr->name, value) < 0) {
867 radlog(L_ERR, "%s", librad_errstr);
870 } while (cp != NULL);
871 } /* over the sections which can have redundent sub-sections */
874 * Remember where the modules were stored.
876 modules = cf_section_find("modules");
878 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
883 * Look for the 'instantiate' section, which tells us
884 * the instantiation order of the modules, and also allows
885 * us to load modules with no authorize/authenticate/etc.
888 cs = cf_section_find("instantiate");
892 module_instance_t *module;
896 * Loop over the items in the 'instantiate' section.
898 for (ci=cf_item_find_next(cs, NULL);
900 ci=cf_item_find_next(cs, ci)) {
903 * Skip sections. They'll be handled
904 * later, if they're referenced at all...
906 if (cf_item_is_section(ci)) {
910 cp = cf_itemtopair(ci);
911 name = cf_pair_attr(cp);
912 module = find_module_instance(modules, name);
916 } /* loop over items in the subsection */
917 } /* if there's an 'instantiate' section. */
920 * Loop over all of the known components, finding their
921 * configuration section, and loading it.
923 for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
924 cs = cf_section_find(section_type_value[comp].section);
928 if (!do_component[comp]) {
932 if (load_component_section(NULL, cs, comp, mainconfig.radiusd_conf) < 0) {
941 * Call all authorization modules until one returns
942 * somethings else than RLM_MODULE_OK
944 int module_authorize(int autz_type, REQUEST *request)
946 return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
950 * Authenticate a user/password with various methods.
952 int module_authenticate(int auth_type, REQUEST *request)
954 return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
958 * Do pre-accounting for ALL configured sessions
960 int module_preacct(REQUEST *request)
962 return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
966 * Do accounting for ALL configured sessions
968 int module_accounting(int acct_type, REQUEST *request)
970 return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
974 * See if a user is already logged in.
976 * Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
978 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
982 if(!request->username)
985 request->simul_count = 0;
986 request->simul_max = maxsimul;
987 request->simul_mpp = 1;
989 rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
991 if (rcode != RLM_MODULE_OK) {
992 /* FIXME: Good spot for a *rate-limited* warning to the log */
996 return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1000 * Do pre-proxying for ALL configured sessions
1002 int module_pre_proxy(int type, REQUEST *request)
1004 return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1008 * Do post-proxying for ALL configured sessions
1010 int module_post_proxy(int type, REQUEST *request)
1012 return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1016 * Do post-authentication for ALL configured sessions
1018 int module_post_auth(int postauth_type, REQUEST *request)
1020 return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);