Allow administrators to force_check_config
[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 = NULL;
46
47 static rbtree_t *module_tree = NULL;
48
49 static rbtree_t *instance_tree = NULL;
50
51 typedef struct section_type_value_t {
52         const char      *section;
53         const char      *typename;
54         int             attr;
55 } section_type_value_t;
56
57 struct fr_module_hup_t {
58         module_instance_t       *mi;
59         time_t                  when;
60         void                    *insthandle;
61         fr_module_hup_t         *next;
62 };
63
64
65 /*
66  *      Ordered by component
67  */
68 static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
69         { "authenticate", "Auth-Type",       PW_AUTH_TYPE },
70         { "authorize",    "Autz-Type",       PW_AUTZ_TYPE },
71         { "preacct",      "Pre-Acct-Type",   PW_PRE_ACCT_TYPE },
72         { "accounting",   "Acct-Type",       PW_ACCT_TYPE },
73         { "session",      "Session-Type",    PW_SESSION_TYPE },
74         { "pre-proxy",    "Pre-Proxy-Type",  PW_PRE_PROXY_TYPE },
75         { "post-proxy",   "Post-Proxy-Type", PW_POST_PROXY_TYPE },
76         { "post-auth",    "Post-Auth-Type",  PW_POST_AUTH_TYPE },
77 };
78
79
80 #ifdef WITHOUT_LIBLTDL
81 typedef struct lt_dlmodule_t {
82   const char    *name;
83   void          *ref;
84 } lt_dlmodule_t;
85
86 /*
87  *      Define modules here.
88  */
89 extern module_t rlm_pap;
90 extern module_t rlm_chap;
91 extern module_t rlm_eap;
92
93 /*
94  *      EAP structures are defined elsewhere.
95  */
96 typedef struct eap_type_t EAP_TYPE;
97
98 /*
99  *      And so on for other EAP types.
100  */
101 extern EAP_TYPE rlm_eap_md5;
102
103 static const lt_dlmodule_t lt_dlmodules[] = {
104         { "rlm_pap", &rlm_pap },
105         { "rlm_chap", &rlm_chap },
106         { "rlm_eap", &rlm_eap },
107         { "rlm_eap_md5", &rlm_eap_md5 },
108         
109         /*
110          *      Add other modules here.
111          */
112                 
113         { NULL, NULL }
114 };
115
116
117 lt_dlhandle lt_dlopenext(const char *name)
118 {
119         int i;
120
121         for (i = 0; lt_dlmodules[i].name != NULL; i++) {
122                 if (strcmp(name, lt_dlmodules[i].name) == 0) {
123                         return lt_dlmodules[i].ref;
124                 }
125         }
126
127         return NULL;
128 }
129
130 void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
131 {
132         return handle;
133 }
134 #endif /* WITHOUT_LIBLTDL */
135
136
137 static void indexed_modcallable_free(void *data)
138 {
139         indexed_modcallable *c = data;
140
141         modcallable_free(&c->modulelist);
142         free(c);
143 }
144
145 static int indexed_modcallable_cmp(const void *one, const void *two)
146 {
147         int rcode;
148         const indexed_modcallable *a = one;
149         const indexed_modcallable *b = two;
150
151         if (a->server && !b->server) return -1;
152         if (!a->server && b->server) return +1;
153         if (a->server && b->server) {
154                 rcode = strcmp(a->server, b->server);
155                 if (rcode != 0) return rcode;
156         }
157
158         if (a->comp < b->comp) return -1;
159         if (a->comp >  b->comp) return +1;
160
161         return a->idx - b->idx;
162 }
163
164
165 /*
166  *      Compare two module entries
167  */
168 static int module_instance_cmp(const void *one, const void *two)
169 {
170         const module_instance_t *a = one;
171         const module_instance_t *b = two;
172
173         return strcmp(a->name, b->name);
174 }
175
176
177 static void module_instance_free_old(CONF_SECTION *cs, module_instance_t *node,
178                                      time_t when)
179 {
180         fr_module_hup_t *mh, **last;
181
182         /*
183          *      Walk the list, freeing up old instances.
184          */
185         last = &(node->mh);
186         while (*last) {
187                 mh = *last;
188
189                 /*
190                  *      Free only every 60 seconds.
191                  */
192                 if ((when - mh->when) < 60) {
193                         last = &(mh->next);
194                         continue;
195                 }
196
197                 cf_section_parse_free(cs, mh->insthandle);
198                 
199                 if (node->entry->module->detach) {
200                         (node->entry->module->detach)(mh->insthandle);
201                 } else {
202                         free(mh->insthandle);
203                 }
204
205                 *last = mh->next;
206                 free(mh);
207         }
208 }
209
210
211 /*
212  *      Free a module instance.
213  */
214 static void module_instance_free(void *data)
215 {
216         module_instance_t *this = data;
217
218         module_instance_free_old(this->cs, this, time(NULL) + 100);
219
220         if (this->entry->module->detach) {
221                 (this->entry->module->detach)(this->insthandle);
222         }
223
224 #ifdef HAVE_PTHREAD_H
225         if (this->mutex) {
226                 /*
227                  *      FIXME
228                  *      The mutex MIGHT be locked...
229                  *      we'll check for that later, I guess.
230                  */
231                 pthread_mutex_destroy(this->mutex);
232                 free(this->mutex);
233         }
234 #endif
235         memset(this, 0, sizeof(*this));
236         free(this);
237 }
238
239
240 /*
241  *      Compare two module entries
242  */
243 static int module_entry_cmp(const void *one, const void *two)
244 {
245         const module_entry_t *a = one;
246         const module_entry_t *b = two;
247
248         return strcmp(a->name, b->name);
249 }
250
251 /*
252  *      Free a module entry.
253  */
254 static void module_entry_free(void *data)
255 {
256         module_entry_t *this = data;
257
258         lt_dlclose(this->handle);       /* ignore any errors */
259         memset(this, 0, sizeof(*this));
260         free(this);
261 }
262
263
264 /*
265  *      Remove the module lists.
266  */
267 int detach_modules(void)
268 {
269         rbtree_free(instance_tree);
270         rbtree_free(components);
271         rbtree_free(module_tree);
272
273         lt_dlexit();
274
275         return 0;
276 }
277
278
279 /*
280  *      Find a module on disk or in memory, and link to it.
281  */
282 static module_entry_t *linkto_module(const char *module_name,
283                                      CONF_SECTION *cs)
284 {
285         module_entry_t myentry;
286         module_entry_t *node;
287         lt_dlhandle handle;
288         char module_struct[256];
289         char *p;
290         const module_t *module;
291
292         strlcpy(myentry.name, module_name, sizeof(myentry.name));
293         node = rbtree_finddata(module_tree, &myentry);
294         if (node) return node;
295
296         /*
297          *      Keep the handle around so we can dlclose() it.
298          */
299         handle = lt_dlopenext(module_name);
300         if (handle == NULL) {
301                 cf_log_err(cf_sectiontoitem(cs),
302                            "Failed to link to module '%s': %s\n",
303                            module_name, lt_dlerror());
304                 return NULL;
305         }
306
307         /*
308          *      Link to the module's rlm_FOO{} module structure.
309          *
310          *      The module_name variable has the version number
311          *      embedded in it, and we don't want that here.
312          */
313         strcpy(module_struct, module_name);
314         p = strrchr(module_struct, '-');
315         if (p) *p = '\0';
316
317         DEBUG3("    (Loaded %s, checking if it's valid)", module_name);
318
319         /*
320          *      libltld MAY core here, if the handle it gives us contains
321          *      garbage data.
322          */
323         module = lt_dlsym(handle, module_struct);
324         if (!module) {
325                 cf_log_err(cf_sectiontoitem(cs),
326                            "Failed linking to %s structure: %s\n",
327                            module_name, lt_dlerror());
328                 lt_dlclose(handle);
329                 return NULL;
330         }
331         /*
332          *      Before doing anything else, check if it's sane.
333          */
334         if (module->magic != RLM_MODULE_MAGIC_NUMBER) {
335                 lt_dlclose(handle);
336                 cf_log_err(cf_sectiontoitem(cs),
337                            "Invalid version in module '%s'",
338                            module_name);
339                 return NULL;
340
341         }
342
343         /* make room for the module type */
344         node = rad_malloc(sizeof(*node));
345         memset(node, 0, sizeof(*node));
346         strlcpy(node->name, module_name, sizeof(node->name));
347         node->module = module;
348         node->handle = handle;
349
350         cf_log_module(cs, "Linked to module %s", module_name);
351
352         /*
353          *      Add the module as "rlm_foo-version" to the configuration
354          *      section.
355          */
356         if (!rbtree_insert(module_tree, node)) {
357                 radlog(L_ERR, "Failed to cache module %s", module_name);
358                 lt_dlclose(handle);
359                 free(node);
360                 return NULL;
361         }
362
363         return node;
364 }
365
366 /*
367  *      Find a module instance.
368  */
369 module_instance_t *find_module_instance(CONF_SECTION *modules,
370                                         const char *instname, int do_link)
371 {
372         int check_config_safe = FALSE;
373         CONF_SECTION *cs;
374         const char *name1, *name2;
375         module_instance_t *node, myNode;
376         char module_name[256];
377
378         if (!modules) return NULL;
379
380         /*
381          *      Module instances are declared in the modules{} block
382          *      and referenced later by their name, which is the
383          *      name2 from the config section, or name1 if there was
384          *      no name2.
385          */
386         cs = cf_section_sub_find_name2(modules, NULL, instname);
387         if (cs == NULL) {
388                 radlog(L_ERR, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
389                 return NULL;
390         }
391
392         /*
393          *      If there's already a module instance, return it.
394          */
395         strlcpy(myNode.name, instname, sizeof(myNode.name));
396         node = rbtree_finddata(instance_tree, &myNode);
397         if (node) return node;
398
399         if (!do_link) return NULL;
400
401         name1 = cf_section_name1(cs);
402         name2 = cf_section_name2(cs);
403
404         /*
405          *      Found the configuration entry.
406          */
407         node = rad_malloc(sizeof(*node));
408         memset(node, 0, sizeof(*node));
409
410         node->insthandle = NULL;
411         node->cs = cs;
412
413         /*
414          *      Names in the "modules" section aren't prefixed
415          *      with "rlm_", so we add it here.
416          */
417         snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
418
419         node->entry = linkto_module(module_name, cs);
420         if (!node->entry) {
421                 free(node);
422                 /* linkto_module logs any errors */
423                 return NULL;
424         }
425
426         if (check_config && (node->entry->module->instantiate) &&
427             (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
428                 const char *value = NULL;
429                 CONF_PAIR *cp;
430
431                 cp = cf_pair_find(cs, "force_check_config");
432                 if (cp) value = cf_pair_value(cp);
433
434                 if (value && (strcmp(value, "yes") == 0)) goto print_inst;
435
436                 cf_log_module(cs, "Skipping instantiation of %s", instname);
437         } else {
438         print_inst:
439                 check_config_safe = TRUE;
440                 cf_log_module(cs, "Instantiating %s", instname);
441         }
442
443         /*
444          *      Call the module's instantiation routine.
445          */
446         if ((node->entry->module->instantiate) &&
447             (!check_config || check_config_safe) &&
448             ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
449                 cf_log_err(cf_sectiontoitem(cs),
450                            "Instantiation failed for module \"%s\"",
451                            instname);
452                 free(node);
453                 return NULL;
454         }
455
456         /*
457          *      We're done.  Fill in the rest of the data structure,
458          *      and link it to the module instance list.
459          */
460         strlcpy(node->name, instname, sizeof(node->name));
461
462 #ifdef HAVE_PTHREAD_H
463         /*
464          *      If we're threaded, check if the module is thread-safe.
465          *
466          *      If it isn't, we create a mutex.
467          */
468         if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
469                 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
470                 /*
471                  *      Initialize the mutex.
472                  */
473                 pthread_mutex_init(node->mutex, NULL);
474         } else {
475                 /*
476                  *      The module is thread-safe.  Don't give it a mutex.
477                  */
478                 node->mutex = NULL;
479         }
480
481 #endif
482         rbtree_insert(instance_tree, node);
483
484         return node;
485 }
486
487 static indexed_modcallable *lookup_by_index(const char *server, int comp,
488                                             int idx)
489 {
490         indexed_modcallable myc;
491         
492         myc.comp = comp;
493         myc.idx = idx;
494         myc.server = server;
495
496         return rbtree_finddata(components, &myc);
497 }
498
499 /*
500  *      Create a new sublist.
501  */
502 static indexed_modcallable *new_sublist(const char *server, int comp, int idx)
503 {
504         indexed_modcallable *c;
505
506         c = lookup_by_index(server, comp, idx);
507
508         /* It is an error to try to create a sublist that already
509          * exists. It would almost certainly be caused by accidental
510          * duplication in the config file.
511          *
512          * index 0 is the exception, because it is used when we want
513          * to collect _all_ listed modules under a single index by
514          * default, which is currently the case in all components
515          * except authenticate. */
516         if (c) {
517                 if (idx == 0) {
518                         return c;
519                 }
520                 return NULL;
521         }
522
523         c = rad_malloc(sizeof(*c));
524         c->modulelist = NULL;
525         c->server = server;
526         c->comp = comp;
527         c->idx = idx;
528
529         if (!rbtree_insert(components, c)) {
530                 free(c);
531                 return NULL;
532         }
533
534         return c;
535 }
536
537 int indexed_modcall(int comp, int idx, REQUEST *request)
538 {
539         int rcode;
540         indexed_modcallable *this;
541         modcallable *list = NULL;
542
543         this = lookup_by_index(request->server, comp, idx);
544         if (!this) {
545                 if (idx != 0) DEBUG2("  WARNING: Unknown value specified for %s.  Cannot perform requested action.",
546                                      section_type_value[comp].typename);
547         } else {
548                 list = this->modulelist;
549         }
550
551         request->component = section_type_value[comp].section;
552
553         rcode = modcall(comp, list, request);
554
555         request->module = "";
556         request->component = "";
557         return rcode;
558 }
559
560 /*
561  *      Load a sub-module list, as found inside an Auth-Type foo {}
562  *      block
563  */
564 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
565                                      const char *server, int attr, int comp)
566 {
567         indexed_modcallable *subcomp;
568         modcallable *ml;
569         DICT_VALUE *dval;
570         const char *name2 = cf_section_name2(cs);
571
572         rad_assert(comp >= RLM_COMPONENT_AUTH);
573         rad_assert(comp < RLM_COMPONENT_COUNT);
574
575         /*
576          *      Sanity check.
577          */
578         if (!name2) {
579                 cf_log_err(cf_sectiontoitem(cs),
580                            "No name specified for %s block",
581                            section_type_value[comp].typename);
582                 return 1;
583         }
584
585         /*
586          *      Compile the group.
587          */
588         ml = compile_modgroup(parent, comp, cs);
589         if (!ml) {
590                 return 0;
591         }
592
593         /*
594          *      We must assign a numeric index to this subcomponent.
595          *      It is generated and placed in the dictionary
596          *      automatically.  If it isn't found, it's a serious
597          *      error.
598          */
599         dval = dict_valbyname(attr, name2);
600         if (!dval) {
601                 cf_log_err(cf_sectiontoitem(cs),
602                            "%s %s Not previously configured",
603                            section_type_value[comp].typename, name2);
604                 modcallable_free(&ml);
605                 return 0;
606         }
607
608         subcomp = new_sublist(server, comp, dval->value);
609         if (!subcomp) {
610                 modcallable_free(&ml);
611                 return 1;
612         }
613
614         subcomp->modulelist = ml;
615         return 1;               /* OK */
616 }
617
618 static int define_type(const DICT_ATTR *dattr, const char *name)
619 {
620         uint32_t value;
621         DICT_VALUE *dval;
622
623         /*
624          *      If the value already exists, don't
625          *      create it again.
626          */
627         dval = dict_valbyname(dattr->attr, name);
628         if (dval) return 1;
629
630         /*
631          *      Create a new unique value with a
632          *      meaningless number.  You can't look at
633          *      it from outside of this code, so it
634          *      doesn't matter.  The only requirement
635          *      is that it's unique.
636          */
637         do {
638                 value = fr_rand() & 0x00ffffff;
639         } while (dict_valbyattr(dattr->attr, value));
640
641         if (dict_addvalue(name, dattr->name, value) < 0) {
642                 radlog(L_ERR, "%s", fr_strerror());
643                 return 0;
644         }
645
646         return 1;
647 }
648
649 static int load_component_section(CONF_SECTION *cs,
650                                   const char *server, int comp)
651 {
652         modcallable *this;
653         CONF_ITEM *modref;
654         int idx;
655         indexed_modcallable *subcomp;
656         const char *modname;
657         const char *visiblename;
658         const DICT_ATTR *dattr;
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(cs),
666                            "No such attribute %s",
667                            section_type_value[comp].typename);
668                 return -1;
669         }
670
671         /*
672          *      Loop over the entries in the named section, loading
673          *      the sections this time.
674          */
675         for (modref = cf_item_find_next(cs, NULL);
676              modref != NULL;
677              modref = cf_item_find_next(cs, modref)) {
678                 const char *name1;
679                 CONF_PAIR *cp = NULL;
680                 CONF_SECTION *scs = NULL;
681
682                 if (cf_item_is_section(modref)) {
683                         scs = cf_itemtosection(modref);
684
685                         name1 = cf_section_name1(scs);
686
687                         if (strcmp(name1,
688                                    section_type_value[comp].typename) == 0) {
689                                 if (!load_subcomponent_section(NULL, scs,
690                                                                server,
691                                                                dattr->attr,
692                                                                comp)) {
693                                         return -1; /* FIXME: memleak? */
694                                 }
695                                 continue;
696                         }
697
698                         cp = NULL;
699
700                 } else if (cf_item_is_pair(modref)) {
701                         cp = cf_itemtopair(modref);
702
703                 } else {
704                         continue; /* ignore it */
705                 }
706
707                 /*
708                  *      Try to compile one entry.
709                  */
710                 this = compile_modsingle(NULL, comp, modref, &modname);
711                 if (!this) {
712                         cf_log_err(cf_sectiontoitem(cs),
713                                    "Errors parsing %s section.\n",
714                                    cf_section_name1(cs));
715                         return -1;
716                 }
717
718                 /*
719                  *      Look for Auth-Type foo {}, which are special
720                  *      cases of named sections, and allowable ONLY
721                  *      at the top-level.
722                  *
723                  *      i.e. They're not allowed in a "group" or "redundant"
724                  *      subsection.
725                  */
726                 if (comp == RLM_COMPONENT_AUTH) {
727                         DICT_VALUE *dval;
728                         const char *modrefname = NULL;
729                         if (cp) {
730                                 modrefname = cf_pair_attr(cp);
731                         } else {
732                                 modrefname = cf_section_name2(scs);
733                                 if (!modrefname) {
734                                         modcallable_free(&this);
735                                         cf_log_err(cf_sectiontoitem(cs),
736                                                    "Errors parsing %s sub-section.\n",
737                                                    cf_section_name1(scs));
738                                         return -1;
739                                 }
740                         }
741
742                         dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
743                         if (!dval) {
744                                 /*
745                                  *      It's a section, but nothing we
746                                  *      recognize.  Die!
747                                  */
748                                 modcallable_free(&this);
749                                 cf_log_err(cf_sectiontoitem(cs),
750                                            "Unknown Auth-Type \"%s\" in %s sub-section.",
751                                            modrefname, section_type_value[comp].section);
752                                 return -1;
753                         }
754                         idx = dval->value;
755                 } else {
756                         /* See the comment in new_sublist() for explanation
757                          * of the special index 0 */
758                         idx = 0;
759                 }
760
761                 subcomp = new_sublist(server, comp, idx);
762                 if (subcomp == NULL) {
763                         modcallable_free(&this);
764                         continue;
765                 }
766
767                 /* If subcomp->modulelist is NULL, add_to_modcallable will
768                  * create it */
769                 visiblename = cf_section_name2(cs);
770                 if (visiblename == NULL)
771                         visiblename = cf_section_name1(cs);
772                 add_to_modcallable(&subcomp->modulelist, this,
773                                    comp, visiblename);
774         }
775
776         return 0;
777 }
778
779 static int load_byserver(CONF_SECTION *cs)
780 {
781         int comp, flag;
782         const char *server = cf_section_name2(cs);
783
784         cf_log_info(cs, " modules {");
785
786         /*
787          *      Define types first.
788          */
789         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
790                 CONF_SECTION *subcs;
791                 CONF_ITEM *modref;
792                 DICT_ATTR *dattr;
793
794                 subcs = cf_section_sub_find(cs,
795                                             section_type_value[comp].section);
796                 if (!subcs) continue;
797                         
798                 if (cf_item_find_next(subcs, NULL) == NULL) continue;
799
800                 /*
801                  *      Find the attribute used to store VALUEs for this section.
802                  */
803                 dattr = dict_attrbyvalue(section_type_value[comp].attr);
804                 if (!dattr) {
805                         cf_log_err(cf_sectiontoitem(subcs),
806                                    "No such attribute %s",
807                                    section_type_value[comp].typename);
808                         cf_log_info(cs, " }");
809                         return -1;
810                 }
811
812                 /*
813                  *      Define dynamic types, so that others can reference
814                  *      them.
815                  */
816                 for (modref = cf_item_find_next(subcs, NULL);
817                      modref != NULL;
818                      modref = cf_item_find_next(subcs, modref)) {
819                         const char *name1;
820                         CONF_SECTION *subsubcs;
821
822                         /*
823                          *      Create types for simple references
824                          *      only when parsing the authenticate
825                          *      section.
826                          */
827                         if ((section_type_value[comp].attr == PW_AUTH_TYPE) &&
828                             cf_item_is_pair(modref)) {
829                                 CONF_PAIR *cp = cf_itemtopair(modref);
830                                 if (!define_type(dattr, cf_pair_attr(cp))) {
831                                         return -1;
832                                 }
833
834                                 continue;
835                         }
836
837                         if (!cf_item_is_section(modref)) continue;
838                         
839                         subsubcs = cf_itemtosection(modref);
840                         name1 = cf_section_name1(subsubcs);
841                 
842                         if (strcmp(name1, section_type_value[comp].typename) == 0) {
843                                 if (!define_type(dattr,
844                                                  cf_section_name2(subsubcs))) {
845                                         cf_log_info(cs, " }");
846                                         return -1;
847                                 }
848                         }
849                 }
850         } /* loop over components */
851
852         /*
853          *      Loop over all of the known components, finding their
854          *      configuration section, and loading it.
855          */
856         flag = 0;
857         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
858                 CONF_SECTION *subcs;
859
860                 subcs = cf_section_sub_find(cs,
861                                             section_type_value[comp].section);
862                 if (!subcs) continue;
863                         
864                 if (cf_item_find_next(subcs, NULL) == NULL) continue;
865                         
866                 cf_log_module(cs, "Checking %s {...} for more modules to load",
867                        section_type_value[comp].section);
868
869 #ifdef WITH_PROXY
870                 /*
871                  *      Skip pre/post-proxy sections if we're not
872                  *      proxying.
873                  */
874                 if (!mainconfig.proxy_requests &&
875                     ((comp == PW_PRE_PROXY_TYPE) ||
876                      (comp == PW_PRE_PROXY_TYPE))) {
877                         continue;
878                 }
879 #endif
880
881                 if (load_component_section(subcs, server, comp) < 0) {
882                         cf_log_info(cs, " }");
883                         return -1;
884                 }
885                 flag = 1;
886         } /* loop over components */
887
888         /*
889          *      We haven't loaded any of the normal sections.  Maybe we're
890          *      supposed to load the vmps section.
891          *
892          *      This is a bit of a hack...
893          */
894         if (!flag) {
895                 CONF_SECTION *subcs;
896
897                 subcs = cf_section_sub_find(cs, "vmps");
898                 if (subcs) {
899                         cf_log_module(cs, "Checking vmps {...} for more modules to load");              
900                         if (load_component_section(subcs, server,
901                                                    RLM_COMPONENT_POST_AUTH) < 0) {
902                                 return -1;
903                         }
904                         flag = 1;
905                 }
906
907 #ifdef WITH_DHCP
908                 if (!flag) {
909                         const DICT_ATTR *dattr;
910
911                         dattr = dict_attrbyname("DHCP-Message-Type");
912                         if (!dattr) {
913                                 radlog(L_ERR, "No DHCP-Message-Type attribute");
914                                 return -1;
915                         }
916
917                         /*
918                          *      Handle each DHCP Message type separately.
919                          */
920                         for (subcs = cf_subsection_find_next(cs, NULL,
921                                                              "dhcp");
922                              subcs != NULL;
923                              subcs = cf_subsection_find_next(cs, subcs,
924                                                              "dhcp")) {
925                                 const char *name2 = cf_section_name2(subcs);
926
927                                 DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
928                                 if (!load_subcomponent_section(NULL, subcs,
929                                                                server,
930                                                                dattr->attr,
931                                                                RLM_COMPONENT_POST_AUTH)) {
932                                         return -1; /* FIXME: memleak? */
933                                 }
934                                 flag = 1;
935                         }
936                 }
937 #endif
938         }
939
940         cf_log_info(cs, " }");
941
942         if (!flag && server) {
943                 DEBUG("WARNING: Server %s is empty, and will do nothing!",
944                       server);
945         }
946
947         return 0;
948 }
949
950
951 int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
952 {
953         void *insthandle = NULL;
954         fr_module_hup_t *mh;
955
956         if (!node ||
957             !node->entry->module->instantiate ||
958             ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
959                 return 1;
960         }
961
962         cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
963         
964         if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
965                 cf_log_err(cf_sectiontoitem(cs),
966                            "HUP failed for module \"%s\".  Using old configuration.",
967                            node->name);
968                 return 0;
969         }
970
971         radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
972
973         module_instance_free_old(cs, node, when);
974
975         /*
976          *      Save the old instance handle for later deletion.
977          */
978         mh = rad_malloc(sizeof(*mh));
979         mh->mi = node;
980         mh->when = when;
981         mh->insthandle = node->insthandle;
982         mh->next = node->mh;
983         node->mh = mh;
984
985         node->insthandle = insthandle;
986         
987         /*
988          *      FIXME: Set a timeout to come back in 60s, so that
989          *      we can pro-actively clean up the old instances.
990          */
991
992         return 1;
993 }
994
995
996 int module_hup(CONF_SECTION *modules)
997 {
998         time_t when;
999         CONF_ITEM *ci;
1000         CONF_SECTION *cs;
1001         module_instance_t *node;
1002
1003         if (!modules) return 0;
1004
1005         when = time(NULL);
1006
1007         /*
1008          *      Loop over the modules
1009          */
1010         for (ci=cf_item_find_next(modules, NULL);
1011              ci != NULL;
1012              ci=cf_item_find_next(modules, ci)) {
1013                 const char *instname;
1014                 module_instance_t myNode;
1015
1016                 /*
1017                  *      If it's not a section, ignore it.
1018                  */
1019                 if (!cf_item_is_section(ci)) continue;
1020
1021                 cs = cf_itemtosection(ci);
1022                 instname = cf_section_name2(cs);
1023                 if (!instname) instname = cf_section_name1(cs);
1024
1025                 strlcpy(myNode.name, instname, sizeof(myNode.name));
1026                 node = rbtree_finddata(instance_tree, &myNode);
1027
1028                 module_hup_module(cs, node, when);
1029         }
1030
1031         return 1;
1032 }
1033
1034
1035 /*
1036  *      Parse the module config sections, and load
1037  *      and call each module's init() function.
1038  *
1039  *      Libtool makes your life a LOT easier, especially with libltdl.
1040  *      see: http://www.gnu.org/software/libtool/
1041  */
1042 int setup_modules(int reload, CONF_SECTION *config)
1043 {
1044         CONF_SECTION    *cs, *modules;
1045         rad_listen_t    *listener;
1046         int             null_server = FALSE;
1047
1048         if (reload) return 0;
1049
1050         /*
1051          *      If necessary, initialize libltdl.
1052          */
1053         if (!reload) {
1054                 /*
1055                  *      Set the default list of preloaded symbols.
1056                  *      This is used to initialize libltdl's list of
1057                  *      preloaded modules.
1058                  *
1059                  *      i.e. Static modules.
1060                  */
1061                 LTDL_SET_PRELOADED_SYMBOLS();
1062
1063                 if (lt_dlinit() != 0) {
1064                         radlog(L_ERR, "Failed to initialize libraries: %s\n",
1065                                         lt_dlerror());
1066                         return -1;
1067                 }
1068
1069                 /*
1070                  *      Set the search path to ONLY our library directory.
1071                  *      This prevents the modules from being found from
1072                  *      any location on the disk.
1073                  */
1074                 lt_dlsetsearchpath(radlib_dir);
1075
1076                 /*
1077                  *      Set up the internal module struct.
1078                  */
1079                 module_tree = rbtree_create(module_entry_cmp,
1080                                             module_entry_free, 0);
1081                 if (!module_tree) {
1082                         radlog(L_ERR, "Failed to initialize modules\n");
1083                         return -1;
1084                 }
1085
1086                 instance_tree = rbtree_create(module_instance_cmp,
1087                                               module_instance_free, 0);
1088                 if (!instance_tree) {
1089                         radlog(L_ERR, "Failed to initialize modules\n");
1090                         return -1;
1091                 }
1092         }
1093
1094         components = rbtree_create(indexed_modcallable_cmp,
1095                                    indexed_modcallable_free, 0);
1096         if (!components) {
1097                 radlog(L_ERR, "Failed to initialize components\n");
1098                 return -1;
1099         }
1100
1101         /*
1102          *      Remember where the modules were stored.
1103          */
1104         modules = cf_section_sub_find(config, "modules");
1105         if (!modules) {
1106                 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
1107                 return -1;
1108         }
1109
1110         DEBUG2("%s: #### Instantiating modules ####", mainconfig.name);
1111
1112         /*
1113          *  Look for the 'instantiate' section, which tells us
1114          *  the instantiation order of the modules, and also allows
1115          *  us to load modules with no authorize/authenticate/etc.
1116          *  sections.
1117          */
1118         cs = cf_section_sub_find(config, "instantiate");
1119         if (cs != NULL) {
1120                 CONF_ITEM *ci;
1121                 CONF_PAIR *cp;
1122                 module_instance_t *module;
1123                 const char *name;
1124
1125                 cf_log_info(cs, " instantiate {");
1126
1127                 /*
1128                  *  Loop over the items in the 'instantiate' section.
1129                  */
1130                 for (ci=cf_item_find_next(cs, NULL);
1131                      ci != NULL;
1132                      ci=cf_item_find_next(cs, ci)) {
1133
1134                         /*
1135                          *      Skip sections and "other" stuff.
1136                          *      Sections will be handled later, if
1137                          *      they're referenced at all...
1138                          */
1139                         if (!cf_item_is_pair(ci)) {
1140                                 continue;
1141                         }
1142
1143                         cp = cf_itemtopair(ci);
1144                         name = cf_pair_attr(cp);
1145                         module = find_module_instance(modules, name, 1);
1146                         if (!module) {
1147                                 return -1;
1148                         }
1149                 } /* loop over items in the subsection */
1150
1151                 cf_log_info(cs, " }");
1152         } /* if there's an 'instantiate' section. */
1153
1154         /*
1155          *      Loop over the listeners, figuring out which sections
1156          *      to load.
1157          */
1158         for (listener = mainconfig.listen;
1159              listener != NULL;
1160              listener = listener->next) {
1161                 char buffer[256];
1162
1163                 if (listener->type == RAD_LISTEN_PROXY) continue;
1164
1165                 cs = cf_section_sub_find_name2(config,
1166                                                "server", listener->server);
1167                 if (!cs && (listener->server != NULL)) {
1168                         listener->print(listener, buffer, sizeof(buffer));
1169
1170                         radlog(L_ERR, "No server has been defined for %s", buffer);
1171                         return -1;
1172                 }
1173         }
1174
1175         DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1176
1177         /*
1178          *      Load all of the virtual servers.
1179          */
1180         for (cs = cf_subsection_find_next(config, NULL, "server");
1181              cs != NULL;
1182              cs = cf_subsection_find_next(config, cs, "server")) {
1183                 const char *name2 = cf_section_name2(cs);
1184
1185                 if (name2) {
1186                         cf_log_info(cs, "server %s {", name2);
1187                 } else {
1188                         cf_log_info(cs, "server {");
1189                         null_server = TRUE;
1190                 }
1191                 if (load_byserver(cs) < 0) {
1192                         cf_log_info(cs, "}");
1193                         return -1;
1194                 }
1195                 cf_log_info(cs, "}");
1196         }
1197
1198         /*
1199          *      No empty server defined.  Try to load an old-style
1200          *      one for backwards compatibility.
1201          */
1202         if (!null_server) {
1203                 cf_log_info(cs, "server {");
1204                 if (load_byserver(config) < 0) {
1205                         cf_log_info(cs, "}");
1206                         return -1;
1207                 }
1208                 cf_log_info(cs, "}");
1209         }
1210
1211         return 0;
1212 }
1213
1214 /*
1215  *      Call all authorization modules until one returns
1216  *      somethings else than RLM_MODULE_OK
1217  */
1218 int module_authorize(int autz_type, REQUEST *request)
1219 {
1220         return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
1221 }
1222
1223 /*
1224  *      Authenticate a user/password with various methods.
1225  */
1226 int module_authenticate(int auth_type, REQUEST *request)
1227 {
1228         return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1229 }
1230
1231 #ifdef WITH_ACCOUNTING
1232 /*
1233  *      Do pre-accounting for ALL configured sessions
1234  */
1235 int module_preacct(REQUEST *request)
1236 {
1237         return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
1238 }
1239
1240 /*
1241  *      Do accounting for ALL configured sessions
1242  */
1243 int module_accounting(int acct_type, REQUEST *request)
1244 {
1245         return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1246 }
1247 #endif
1248
1249 #ifdef WITH_SESSION_MGMT
1250 /*
1251  *      See if a user is already logged in.
1252  *
1253  *      Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1254  */
1255 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1256 {
1257         int rcode;
1258
1259         if(!request->username)
1260                 return 0;
1261
1262         request->simul_count = 0;
1263         request->simul_max = maxsimul;
1264         request->simul_mpp = 1;
1265
1266         rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1267
1268         if (rcode != RLM_MODULE_OK) {
1269                 /* FIXME: Good spot for a *rate-limited* warning to the log */
1270                 return 0;
1271         }
1272
1273         return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1274 }
1275 #endif
1276
1277 #ifdef WITH_PROXY
1278 /*
1279  *      Do pre-proxying for ALL configured sessions
1280  */
1281 int module_pre_proxy(int type, REQUEST *request)
1282 {
1283         return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1284 }
1285
1286 /*
1287  *      Do post-proxying for ALL configured sessions
1288  */
1289 int module_post_proxy(int type, REQUEST *request)
1290 {
1291         return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1292 }
1293 #endif
1294
1295 /*
1296  *      Do post-authentication for ALL configured sessions
1297  */
1298 int module_post_auth(int postauth_type, REQUEST *request)
1299 {
1300         return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1301 }