Added HUP support. As it happens, it's also thread-safe.
[freeradius.git] / src / main / modules.c
1 /*
2  * modules.c    Radius module support.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
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>
23  */
24
25 #include <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
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>
32
33 extern int check_config;
34
35 typedef struct indexed_modcallable {
36         const           char *server;
37         int             comp;
38         int             idx;
39         modcallable     *modulelist;
40 } indexed_modcallable;
41
42 /*
43  *      For each component, keep an ordered list of ones to call.
44  */
45 static rbtree_t *components;
46
47 static rbtree_t *module_tree = NULL;
48
49 typedef struct section_type_value_t {
50         const char      *section;
51         const char      *typename;
52         int             attr;
53 } section_type_value_t;
54
55
56 /*
57  *      Ordered by component
58  */
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 },
68 };
69
70 static void indexed_modcallable_free(void *data)
71 {
72         indexed_modcallable *c = data;
73
74         modcallable_free(&c->modulelist);
75         free(c);
76 }
77
78 static int indexed_modcallable_cmp(const void *one, const void *two)
79 {
80         int rcode;
81         const indexed_modcallable *a = one;
82         const indexed_modcallable *b = two;
83
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;
89         }
90
91         if (a->comp < b->comp) return -1;
92         if (a->comp >  b->comp) return +1;
93
94         return a->idx - b->idx;
95 }
96
97
98 /*
99  *      Free a module instance.
100  */
101 static void module_instance_free(void *data)
102 {
103         module_instance_t *this = data;
104
105         if (this->entry->module->detach)
106                 (this->entry->module->detach)(this->insthandle);
107 #ifdef HAVE_PTHREAD_H
108         if (this->mutex) {
109                 /*
110                  *      FIXME
111                  *      The mutex MIGHT be locked...
112                  *      we'll check for that later, I guess.
113                  */
114                 pthread_mutex_destroy(this->mutex);
115                 free(this->mutex);
116         }
117 #endif
118         free(this);
119 }
120
121
122 /*
123  *      Compare two module entries
124  */
125 static int module_entry_cmp(const void *one, const void *two)
126 {
127         const module_entry_t *a = one;
128         const module_entry_t *b = two;
129
130         return strcmp(a->name, b->name);
131 }
132
133 /*
134  *      Free a module entry.
135  */
136 static void module_entry_free(void *data)
137 {
138         module_entry_t *this = data;
139
140         lt_dlclose(this->handle);       /* ignore any errors */
141         free(this);
142 }
143
144
145 /*
146  *      Remove the module lists.
147  */
148 int detach_modules(void)
149 {
150         rbtree_free(components);
151         rbtree_free(module_tree);
152
153         return 0;
154 }
155
156
157 /*
158  *      Find a module on disk or in memory, and link to it.
159  */
160 static module_entry_t *linkto_module(const char *module_name,
161                                      CONF_SECTION *cs)
162 {
163         module_entry_t myentry;
164         module_entry_t *node;
165         lt_dlhandle handle;
166         char module_struct[256];
167         char *p;
168         const void *module;
169
170         strlcpy(myentry.name, module_name, sizeof(myentry.name));
171         node = rbtree_finddata(module_tree, &myentry);
172         if (node) return node;
173
174         /*
175          *      Keep the handle around so we can dlclose() it.
176          */
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());
182                 return NULL;
183         }
184
185         /*
186          *      Link to the module's rlm_FOO{} module structure.
187          *
188          *      The module_name variable has the version number
189          *      embedded in it, and we don't want that here.
190          */
191         strcpy(module_struct, module_name);
192         p = strrchr(module_struct, '-');
193         if (p) *p = '\0';
194
195         DEBUG3("    (Loaded %s, checking if it's valid)", module_name);
196
197         /*
198          *      libltld MAY core here, if the handle it gives us contains
199          *      garbage data.
200          */
201         module = lt_dlsym(handle, module_struct);
202         if (!module) {
203                 cf_log_err(cf_sectiontoitem(cs),
204                            "Failed linking to %s structure: %s\n",
205                            module_name, lt_dlerror());
206                 lt_dlclose(handle);
207                 return NULL;
208         }
209         /*
210          *      Before doing anything else, check if it's sane.
211          */
212         if ((*(const uint32_t *) module) != RLM_MODULE_MAGIC_NUMBER) {
213                 lt_dlclose(handle);
214                 cf_log_err(cf_sectiontoitem(cs),
215                            "Invalid version in module '%s'",
216                            module_name);
217                 return NULL;
218
219         }
220
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;
227
228         DEBUG(" Module: Linked to module %s", module_name);
229
230         /*
231          *      Add the module as "rlm_foo-version" to the configuration
232          *      section.
233          */
234         if (!rbtree_insert(module_tree, node)) {
235                 radlog(L_ERR, "Failed to cache module %s", module_name);
236                 lt_dlclose(handle);
237                 free(node);
238                 return NULL;
239         }
240
241         return node;
242 }
243
244 /*
245  *      Find a module instance.
246  */
247 module_instance_t *find_module_instance(CONF_SECTION *modules,
248                                         const char *instname)
249 {
250         CONF_SECTION *cs;
251         const char *name1, *name2;
252         module_instance_t *node;
253         char module_name[256];
254
255         if (!modules) return NULL;
256
257         /*
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
261          *      no name2.
262          */
263         cs = cf_section_sub_find_name2(modules, NULL, instname);
264         if (cs == NULL) {
265                 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
266                 return NULL;
267         }
268
269         /*
270          *      If there's already a module instance, return it.
271          */
272         node = cf_data_find(cs, "instance");
273         if (node) return node;
274
275         name1 = cf_section_name1(cs);
276         name2 = cf_section_name2(cs);
277
278         /*
279          *      Found the configuration entry.
280          */
281         node = rad_malloc(sizeof(*node));
282         memset(node, 0, sizeof(*node));
283
284         node->insthandle = NULL;
285
286         /*
287          *      Names in the "modules" section aren't prefixed
288          *      with "rlm_", so we add it here.
289          */
290         snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
291
292         node->entry = linkto_module(module_name, cs);
293         if (!node->entry) {
294                 free(node);
295                 /* linkto_module logs any errors */
296                 return NULL;
297         }
298
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);
302         } else {
303                 DEBUG2(" Module: Instantiating %s", instname);
304         }
305
306         /*
307          *      Call the module's instantiation routine.
308          */
309         if ((node->entry->module->instantiate) &&
310             (!check_config ||
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\"",
315                            instname);
316                 free(node);
317                 return NULL;
318         }
319
320         /*
321          *      We're done.  Fill in the rest of the data structure,
322          *      and link it to the module instance list.
323          */
324         strlcpy(node->name, instname, sizeof(node->name));
325
326 #ifdef HAVE_PTHREAD_H
327         /*
328          *      If we're threaded, check if the module is thread-safe.
329          *
330          *      If it isn't, we create a mutex.
331          */
332         if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
333                 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
334                 /*
335                  *      Initialize the mutex.
336                  */
337                 pthread_mutex_init(node->mutex, NULL);
338         } else {
339                 /*
340                  *      The module is thread-safe.  Don't give it a mutex.
341                  */
342                 node->mutex = NULL;
343         }
344
345 #endif
346         cf_data_add(cs, "instance", node, module_instance_free);
347
348         return node;
349 }
350
351 static indexed_modcallable *lookup_by_index(const char *server, int comp,
352                                             int idx)
353 {
354         indexed_modcallable myc;
355         
356         myc.comp = comp;
357         myc.idx = idx;
358         myc.server = server;
359
360         return rbtree_finddata(components, &myc);
361 }
362
363 /*
364  *      Create a new sublist.
365  */
366 static indexed_modcallable *new_sublist(const char *server, int comp, int idx)
367 {
368         indexed_modcallable *c;
369
370         c = lookup_by_index(server, comp, idx);
371
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.
375          *
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. */
380         if (c) {
381                 if (idx == 0) {
382                         return c;
383                 }
384                 return NULL;
385         }
386
387         c = rad_malloc(sizeof(*c));
388         c->modulelist = NULL;
389         c->server = server;
390         c->comp = comp;
391         c->idx = idx;
392
393         if (!rbtree_insert(components, c)) {
394                 free(c);
395                 return NULL;
396         }
397
398         return c;
399 }
400
401 static int indexed_modcall(int comp, int idx, REQUEST *request)
402 {
403         int rcode;
404         indexed_modcallable *this;
405         modcallable *list = NULL;
406
407         this = lookup_by_index(request->server, comp, idx);
408         if (!this) {
409                 if (idx != 0) DEBUG2("  WARNING: Unknown value specified for %s.  Cannot perform requested action.",
410                                      section_type_value[comp].typename);
411         } else {
412                 list = this->modulelist;
413         }
414
415         request->component = section_type_value[comp].section;
416
417         rcode = modcall(comp, list, request);
418
419         request->module = "<server-core>";
420         request->component = "<server-core>";
421         return rcode;
422 }
423
424 /*
425  *      Load a sub-module list, as found inside an Auth-Type foo {}
426  *      block
427  */
428 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
429                                      const char *server, int comp)
430 {
431         indexed_modcallable *subcomp;
432         modcallable *ml;
433         DICT_VALUE *dval;
434         const char *name2 = cf_section_name2(cs);
435
436         rad_assert(comp >= RLM_COMPONENT_AUTH);
437         rad_assert(comp <= RLM_COMPONENT_COUNT);
438
439         /*
440          *      Sanity check.
441          */
442         if (!name2) {
443                 cf_log_err(cf_sectiontoitem(cs),
444                            "No name specified for %s block",
445                            section_type_value[comp].typename);
446                 return 1;
447         }
448
449         /*
450          *      Compile the group.
451          */
452         ml = compile_modgroup(parent, comp, cs);
453         if (!ml) {
454                 return 0;
455         }
456
457         /*
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
461          *      error.
462          */
463         dval = dict_valbyname(section_type_value[comp].attr, name2);
464         if (!dval) {
465                 cf_log_err(cf_sectiontoitem(cs),
466                            "%s %s Not previously configured",
467                            section_type_value[comp].typename, name2);
468                 modcallable_free(&ml);
469                 return 0;
470         }
471
472         subcomp = new_sublist(server, comp, dval->value);
473         if (!subcomp) {
474                 modcallable_free(&ml);
475                 return 1;
476         }
477
478         subcomp->modulelist = ml;
479         return 1;               /* OK */
480 }
481
482 static int define_type(const DICT_ATTR *dattr, const char *name)
483 {
484         uint32_t value;
485         DICT_VALUE *dval;
486
487         /*
488          *      If the value already exists, don't
489          *      create it again.
490          */
491         dval = dict_valbyname(dattr->attr, name);
492         if (dval) return 1;
493
494         /*
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.
500          */
501         do {
502                 value = lrad_rand() & 0x00ffffff;
503         } while (dict_valbyattr(dattr->attr, value));
504
505         if (dict_addvalue(name, dattr->name, value) < 0) {
506                 radlog(L_ERR, "%s", librad_errstr);
507                 return 0;
508         }
509
510         return 1;
511 }
512
513 static int load_component_section(CONF_SECTION *cs,
514                                   const char *server, int comp)
515 {
516         modcallable *this;
517         CONF_ITEM *modref;
518         int idx;
519         indexed_modcallable *subcomp;
520         const char *modname;
521         const char *visiblename;
522         const DICT_ATTR *dattr;
523
524         /*
525          *      Find the attribute used to store VALUEs for this section.
526          */
527         dattr = dict_attrbyvalue(section_type_value[comp].attr);
528         if (!dattr) {
529                 cf_log_err(cf_sectiontoitem(cs),
530                            "No such attribute %s",
531                            section_type_value[comp].typename);
532                 return -1;
533         }
534
535         /*
536          *      Loop over the entries in the named section, loading
537          *      the sections this time.
538          */
539         for (modref = cf_item_find_next(cs, NULL);
540              modref != NULL;
541              modref = cf_item_find_next(cs, modref)) {
542                 const char *name1;
543                 CONF_PAIR *cp = NULL;
544                 CONF_SECTION *scs = NULL;
545
546                 if (cf_item_is_section(modref)) {
547                         scs = cf_itemtosection(modref);
548
549                         name1 = cf_section_name1(scs);
550
551                         if (strcmp(name1,
552                                    section_type_value[comp].typename) == 0) {
553                                 if (!load_subcomponent_section(NULL, scs,
554                                                                server, comp)) {
555                                         return -1; /* FIXME: memleak? */
556                                 }
557                                 continue;
558                         }
559
560                         cp = NULL;
561
562                 } else if (cf_item_is_pair(modref)) {
563                         cp = cf_itemtopair(modref);
564
565                 } else {
566                         continue; /* ignore it */
567                 }
568
569                 /*
570                  *      Try to compile one entry.
571                  */
572                 this = compile_modsingle(NULL, comp, modref, &modname);
573                 if (!this) {
574                         cf_log_err(cf_sectiontoitem(cs),
575                                    "Errors parsing %s section.\n",
576                                    cf_section_name1(cs));
577                         return -1;
578                 }
579
580                 /*
581                  *      Look for Auth-Type foo {}, which are special
582                  *      cases of named sections, and allowable ONLY
583                  *      at the top-level.
584                  *
585                  *      i.e. They're not allowed in a "group" or "redundant"
586                  *      subsection.
587                  */
588                 if (comp == RLM_COMPONENT_AUTH) {
589                         DICT_VALUE *dval;
590                         const char *modrefname = NULL;
591                         if (cp) {
592                                 modrefname = cf_pair_attr(cp);
593                         } else {
594                                 modrefname = cf_section_name2(scs);
595                                 if (!modrefname) {
596                                         cf_log_err(cf_sectiontoitem(cs),
597                                                    "Errors parsing %s sub-section.\n",
598                                                    cf_section_name1(scs));
599                                         return -1;
600                                 }
601                         }
602
603                         dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
604                         if (!dval) {
605                                 /*
606                                  *      It's a section, but nothing we
607                                  *      recognize.  Die!
608                                  */
609                                 cf_log_err(cf_sectiontoitem(cs),
610                                            "Unknown Auth-Type \"%s\" in %s sub-section.",
611                                            modrefname, section_type_value[comp].section);
612                                 return -1;
613                         }
614                         idx = dval->value;
615                 } else {
616                         /* See the comment in new_sublist() for explanation
617                          * of the special index 0 */
618                         idx = 0;
619                 }
620
621                 subcomp = new_sublist(server, comp, idx);
622                 if (subcomp == NULL) {
623                         modcallable_free(&this);
624                         continue;
625                 }
626
627                 /* If subcomp->modulelist is NULL, add_to_modcallable will
628                  * create it */
629                 visiblename = cf_section_name2(cs);
630                 if (visiblename == NULL)
631                         visiblename = cf_section_name1(cs);
632                 add_to_modcallable(&subcomp->modulelist, this,
633                                    comp, visiblename);
634         }
635
636         return 0;
637 }
638
639 static int load_byserver(CONF_SECTION *cs)
640 {
641         int comp, flag;
642         const char *server = cf_section_name2(cs);
643
644         DEBUG2(" modules {");
645
646         /*
647          *      Define types first.
648          */
649         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
650                 CONF_SECTION *subcs;
651                 CONF_ITEM *modref;
652                 DICT_ATTR *dattr;
653
654                 subcs = cf_section_sub_find(cs,
655                                             section_type_value[comp].section);
656                 if (!subcs) continue;
657                         
658                 if (cf_item_find_next(subcs, NULL) == NULL) continue;
659
660                 /*
661                  *      Find the attribute used to store VALUEs for this section.
662                  */
663                 dattr = dict_attrbyvalue(section_type_value[comp].attr);
664                 if (!dattr) {
665                         cf_log_err(cf_sectiontoitem(subcs),
666                                    "No such attribute %s",
667                                    section_type_value[comp].typename);
668                         DEBUG2(" }");
669                         return -1;
670                 }
671
672                 /*
673                  *      Define dynamic types, so that others can reference
674                  *      them.
675                  */
676                 for (modref = cf_item_find_next(subcs, NULL);
677                      modref != NULL;
678                      modref = cf_item_find_next(subcs, modref)) {
679                         const char *name1;
680                         CONF_SECTION *subsubcs;
681
682                         /*
683                          *      Create types for simple references
684                          *      only when parsing the authenticate
685                          *      section.
686                          */
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))) {
691                                         return -1;
692                                 }
693
694                                 continue;
695                         }
696
697                         if (!cf_item_is_section(modref)) continue;
698                         
699                         subsubcs = cf_itemtosection(modref);
700                         name1 = cf_section_name1(subsubcs);
701                 
702                         if (strcmp(name1, section_type_value[comp].typename) == 0) {
703                                 if (!define_type(dattr,
704                                                  cf_section_name2(subsubcs))) {
705                                         DEBUG2(" }");
706                                         return -1;
707                                 }
708                         }
709                 }
710         } /* loop over components */
711
712         /*
713          *      Loop over all of the known components, finding their
714          *      configuration section, and loading it.
715          */
716         flag = 0;
717         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
718                 CONF_SECTION *subcs;
719
720                 subcs = cf_section_sub_find(cs,
721                                             section_type_value[comp].section);
722                 if (!subcs) continue;
723                         
724                 if (cf_item_find_next(subcs, NULL) == NULL) continue;
725                         
726                 DEBUG2(" Module: Checking %s {...} for more modules to load",
727                        section_type_value[comp].section);
728
729                 if (load_component_section(subcs, server, comp) < 0) {
730                         DEBUG2(" }");
731                         return -1;
732                 }
733                 flag = 1;
734         } /* loop over components */
735
736         /*
737          *      We haven't loaded any of the normal sections.  Maybe we're
738          *      supposed to load the vmps section.
739          *
740          *      This is a bit of a hack...
741          */
742         if (!flag) {
743                 CONF_SECTION *subcs;
744
745                 subcs = cf_section_sub_find(cs, "vmps");
746                 if (subcs) {
747                         DEBUG2(" Module: Checking vmps {...} for more modules to load");                
748                         if (load_component_section(subcs, server,
749                                                    RLM_COMPONENT_POST_AUTH) < 0) {
750                                 return -1;
751                         }
752                         flag = 1;
753                 }
754         }
755
756         DEBUG2(" }");
757
758         if (!flag && server) {
759                 DEBUG("WARNING: Server %s is empty, and will do nothing!",
760                       server);
761         }
762
763         return 0;
764 }
765
766
767 int module_hup(CONF_SECTION *modules)
768 {
769         CONF_ITEM *ci;
770         CONF_SECTION *cs;
771         module_instance_t *node;
772
773         if (!modules) return 0;
774
775         /*
776          *      Loop over the modules
777          */
778         for (ci=cf_item_find_next(modules, NULL);
779              ci != NULL;
780              ci=cf_item_find_next(modules, ci)) {
781                 void *insthandle = NULL;
782                 void *old_insthandle = NULL;
783
784                 /*
785                  *      If it's not a section, ignore it.
786                  */
787                 if (!cf_item_is_section(ci)) continue;
788
789                 cs = cf_itemtosection(ci);
790
791                 node = cf_data_find(cs, "instance");
792                 if (!node ||
793                     ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
794                         continue;
795                 }
796
797                 DEBUG2(" Module: Trying to reload module \"%s\"", node->name);
798
799                 if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
800                         cf_log_err(cf_sectiontoitem(cs),
801                                    "HUP failed for module \"%s\"",
802                                    node->name);
803                         continue;
804                 }
805
806                 radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
807
808                 old_insthandle = node->insthandle;
809                 node->insthandle = insthandle;
810
811                 cf_section_parse_free(cs, old_insthandle);
812
813                 if (node->entry->module->detach) {
814                         (node->entry->module->detach)(old_insthandle);
815                 }
816         }
817
818         return 1;
819 }
820
821
822 /*
823  *      Parse the module config sections, and load
824  *      and call each module's init() function.
825  *
826  *      Libtool makes your life a LOT easier, especially with libltdl.
827  *      see: http://www.gnu.org/software/libtool/
828  */
829 int setup_modules(int reload, CONF_SECTION *config)
830 {
831         CONF_SECTION    *cs, *modules;
832         rad_listen_t    *listener;
833         int             null_server = FALSE;
834
835         /*
836          *      If necessary, initialize libltdl.
837          */
838         if (!reload) {
839                 /*
840                  *      Set the default list of preloaded symbols.
841                  *      This is used to initialize libltdl's list of
842                  *      preloaded modules.
843                  *
844                  *      i.e. Static modules.
845                  */
846                 LTDL_SET_PRELOADED_SYMBOLS();
847
848                 if (lt_dlinit() != 0) {
849                         radlog(L_ERR, "Failed to initialize libraries: %s\n",
850                                         lt_dlerror());
851                         return -1;
852                 }
853
854                 /*
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.
858                  */
859                 lt_dlsetsearchpath(radlib_dir);
860
861                 /*
862                  *      Set up the internal module struct.
863                  */
864                 module_tree = rbtree_create(module_entry_cmp,
865                                             module_entry_free, 0);
866                 if (!module_tree) {
867                         radlog(L_ERR, "Failed to initialize modules\n");
868                         return -1;
869                 }
870         } else {
871                 rbtree_free(components);
872         }
873
874         components = rbtree_create(indexed_modcallable_cmp,
875                                    indexed_modcallable_free, 0);
876         if (!components) {
877                 radlog(L_ERR, "Failed to initialize components\n");
878                 return -1;
879         }
880
881         /*
882          *      Remember where the modules were stored.
883          */
884         modules = cf_section_sub_find(config, "modules");
885         if (!modules) {
886                 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
887                 return -1;
888         }
889
890         DEBUG2("radiusd: #### Instantiating modules ####");
891
892         /*
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.
896          *  sections.
897          */
898         cs = cf_section_sub_find(config, "instantiate");
899         if (cs != NULL) {
900                 CONF_ITEM *ci;
901                 CONF_PAIR *cp;
902                 module_instance_t *module;
903                 const char *name;
904
905                 DEBUG2(" instantiate {");
906
907                 /*
908                  *  Loop over the items in the 'instantiate' section.
909                  */
910                 for (ci=cf_item_find_next(cs, NULL);
911                      ci != NULL;
912                      ci=cf_item_find_next(cs, ci)) {
913
914                         /*
915                          *      Skip sections.  They'll be handled
916                          *      later, if they're referenced at all...
917                          */
918                         if (cf_item_is_section(ci)) {
919                                 continue;
920                         }
921
922                         cp = cf_itemtopair(ci);
923                         name = cf_pair_attr(cp);
924                         module = find_module_instance(modules, name);
925                         if (!module) {
926                                 return -1;
927                         }
928                 } /* loop over items in the subsection */
929
930                 DEBUG2(" }");
931         } /* if there's an 'instantiate' section. */
932
933         /*
934          *      Loop over the listeners, figuring out which sections
935          *      to load.
936          */
937         for (listener = mainconfig.listen;
938              listener != NULL;
939              listener = listener->next) {
940                 char buffer[256];
941
942                 if (listener->type == RAD_LISTEN_PROXY) continue;
943
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));
948
949                         radlog(L_ERR, "No server has been defined for %s", buffer);
950                         return -1;
951                 }
952         }
953
954         DEBUG2("radiusd: #### Loading Virtual Servers ####");
955
956         /*
957          *      Load all of the virtual servers.
958          */
959         for (cs = cf_subsection_find_next(config, NULL, "server");
960              cs != NULL;
961              cs = cf_subsection_find_next(config, cs, "server")) {
962                 const char *name2 = cf_section_name2(cs);
963
964                 if (name2) {
965                         DEBUG2("server %s {", name2);
966                 } else {
967                         DEBUG2("server {");
968                         null_server = TRUE;
969                 }
970                 if (load_byserver(cs) < 0) {
971                         DEBUG2("}");
972                         return -1;
973                 }
974                 DEBUG2("}");
975         }
976
977         /*
978          *      No empty server defined.  Try to load an old-style
979          *      one for backwards compatibility.
980          */
981         if (!null_server) {
982                 DEBUG2("server {");
983                 if (load_byserver(config) < 0) {
984                         DEBUG2("}");
985                         return -1;
986                 }
987                 DEBUG2("}");
988         }
989
990         return 0;
991 }
992
993 /*
994  *      Call all authorization modules until one returns
995  *      somethings else than RLM_MODULE_OK
996  */
997 int module_authorize(int autz_type, REQUEST *request)
998 {
999         return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1000 }
1001
1002 /*
1003  *      Authenticate a user/password with various methods.
1004  */
1005 int module_authenticate(int auth_type, REQUEST *request)
1006 {
1007         return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1008 }
1009
1010 /*
1011  *      Do pre-accounting for ALL configured sessions
1012  */
1013 int module_preacct(REQUEST *request)
1014 {
1015         return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1016 }
1017
1018 /*
1019  *      Do accounting for ALL configured sessions
1020  */
1021 int module_accounting(int acct_type, REQUEST *request)
1022 {
1023         return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1024 }
1025
1026 /*
1027  *      See if a user is already logged in.
1028  *
1029  *      Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1030  */
1031 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1032 {
1033         int rcode;
1034
1035         if(!request->username)
1036                 return 0;
1037
1038         request->simul_count = 0;
1039         request->simul_max = maxsimul;
1040         request->simul_mpp = 1;
1041
1042         rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1043
1044         if (rcode != RLM_MODULE_OK) {
1045                 /* FIXME: Good spot for a *rate-limited* warning to the log */
1046                 return 0;
1047         }
1048
1049         return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1050 }
1051
1052 /*
1053  *      Do pre-proxying for ALL configured sessions
1054  */
1055 int module_pre_proxy(int type, REQUEST *request)
1056 {
1057         return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1058 }
1059
1060 /*
1061  *      Do post-proxying for ALL configured sessions
1062  */
1063 int module_post_proxy(int type, REQUEST *request)
1064 {
1065         return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1066 }
1067
1068 /*
1069  *      Do post-authentication for ALL configured sessions
1070  */
1071 int module_post_auth(int postauth_type, REQUEST *request)
1072 {
1073         return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1074 }