space may be NULL
[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 #define VMPS_SPACE "vmps"
34
35 typedef struct indexed_modcallable {
36         const           char *space;
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->space && !b->space) return -1;
85         if (!a->space && b->space) return +1;
86         if (a->space && b->space) {
87                 rcode = strcmp(a->space, b->space);
88                 if (rcode != 0) return rcode;
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                                      const char *cffilename, int cflineno)
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                 radlog(L_ERR|L_CONS, "%s[%d] Failed to link to module '%s':"
180                        " %s\n", cffilename, cflineno, module_name, lt_dlerror());
181                 return NULL;
182         }
183
184         /*
185          *      Link to the module's rlm_FOO{} module structure.
186          *
187          *      The module_name variable has the version number
188          *      embedded in it, and we don't want that here.
189          */
190         strcpy(module_struct, module_name);
191         p = strrchr(module_struct, '-');
192         if (p) *p = '\0';
193
194         DEBUG3("    (Loaded %s, checking if it's valid)", module_name);
195
196         /*
197          *      libltld MAY core here, if the handle it gives us contains
198          *      garbage data.
199          */
200         module = lt_dlsym(handle, module_struct);
201         if (!module) {
202                 radlog(L_ERR|L_CONS, "%s[%d] Failed linking to "
203                                 "%s structure in %s: %s\n",
204                                 cffilename, cflineno,
205                                 module_name, cffilename, lt_dlerror());
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                 radlog(L_ERR|L_CONS, "%s[%d] Invalid version in module '%s'",
215                        cffilename, cflineno, module_name);
216                 return NULL;
217
218         }
219
220         /* make room for the module type */
221         node = rad_malloc(sizeof(*node));
222         memset(node, 0, sizeof(*node));
223         strlcpy(node->name, module_name, sizeof(node->name));
224         node->module = module;
225         node->handle = handle;
226
227         DEBUG(" Module: Linked to module %s", module_name);
228
229         /*
230          *      Add the module as "rlm_foo-version" to the configuration
231          *      section.
232          */
233         if (!rbtree_insert(module_tree, node)) {
234                 radlog(L_ERR, "Failed to cache module %s", module_name);
235                 lt_dlclose(handle);
236                 free(node);
237                 return NULL;
238         }
239
240         return node;
241 }
242
243 /*
244  *      Find a module instance.
245  */
246 module_instance_t *find_module_instance(CONF_SECTION *modules,
247                                         const char *instname)
248 {
249         CONF_SECTION *cs;
250         const char *name1, *name2;
251         module_instance_t *node;
252         char module_name[256];
253
254         if (!modules) return NULL;
255
256         /*
257          *      Module instances are declared in the modules{} block
258          *      and referenced later by their name, which is the
259          *      name2 from the config section, or name1 if there was
260          *      no name2.
261          */
262         cs = cf_section_sub_find_name2(modules, NULL, instname);
263         if (cs == NULL) {
264                 radlog(L_ERR|L_CONS, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
265                 return NULL;
266         }
267
268         /*
269          *      If there's already a module instance, return it.
270          */
271         node = cf_data_find(cs, "instance");
272         if (node) return node;
273
274         name1 = cf_section_name1(cs);
275         name2 = cf_section_name2(cs);
276
277         /*
278          *      Found the configuration entry.
279          */
280         node = rad_malloc(sizeof(*node));
281         memset(node, 0, sizeof(*node));
282
283         node->insthandle = NULL;
284
285         /*
286          *      Names in the "modules" section aren't prefixed
287          *      with "rlm_", so we add it here.
288          */
289         snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
290
291         node->entry = linkto_module(module_name,
292                                     mainconfig.radiusd_conf,
293                                     cf_section_lineno(cs));
294         if (!node->entry) {
295                 free(node);
296                 /* linkto_module logs any errors */
297                 return NULL;
298         }
299
300         DEBUG2(" Module: Instantiating %s", instname);
301
302         /*
303          *      Call the module's instantiation routine.
304          */
305         if ((node->entry->module->instantiate) &&
306             ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
307                 radlog(L_ERR|L_CONS,
308                                 "%s[%d]: %s: Module instantiation failed.\n",
309                        mainconfig.radiusd_conf, cf_section_lineno(cs),
310                        instname);
311                 free(node);
312                 return NULL;
313         }
314
315         /*
316          *      We're done.  Fill in the rest of the data structure,
317          *      and link it to the module instance list.
318          */
319         strlcpy(node->name, instname, sizeof(node->name));
320
321 #ifdef HAVE_PTHREAD_H
322         /*
323          *      If we're threaded, check if the module is thread-safe.
324          *
325          *      If it isn't, we create a mutex.
326          */
327         if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
328                 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
329                 /*
330                  *      Initialize the mutex.
331                  */
332                 pthread_mutex_init(node->mutex, NULL);
333         } else {
334                 /*
335                  *      The module is thread-safe.  Don't give it a mutex.
336                  */
337                 node->mutex = NULL;
338         }
339
340 #endif
341         cf_data_add(cs, "instance", node, module_instance_free);
342
343         return node;
344 }
345
346 static indexed_modcallable *lookup_by_index(const char *space, int comp,
347                                             int idx)
348 {
349         indexed_modcallable myc;
350         
351         myc.comp = comp;
352         myc.idx = idx;
353         myc.space = space;
354
355         return rbtree_finddata(components, &myc);
356 }
357
358 /*
359  *      Create a new sublist.
360  */
361 static indexed_modcallable *new_sublist(const char *space, int comp, int idx)
362 {
363         indexed_modcallable *c;
364
365         c = lookup_by_index(space, comp, idx);
366
367         /* It is an error to try to create a sublist that already
368          * exists. It would almost certainly be caused by accidental
369          * duplication in the config file.
370          *
371          * index 0 is the exception, because it is used when we want
372          * to collect _all_ listed modules under a single index by
373          * default, which is currently the case in all components
374          * except authenticate. */
375         if (c) {
376                 if (idx == 0) {
377                         return c;
378                 }
379                 return NULL;
380         }
381
382         c = rad_malloc(sizeof(*c));
383         c->modulelist = NULL;
384         c->space = space;
385         c->comp = comp;
386         c->idx = idx;
387
388         if (!rbtree_insert(components, c)) {
389                 free(c);
390                 return NULL;
391         }
392
393         return c;
394 }
395
396 static int indexed_modcall(const char *space, int comp, int idx,
397                            REQUEST *request)
398 {
399         int rcode;
400         indexed_modcallable *this;
401         modcallable *list = NULL;
402
403         this = lookup_by_index(space, comp, idx);
404         if (!this) {
405                 if (idx != 0) DEBUG2("  ERROR: Unknown value specified for %s.  Cannot perform requested action.",
406                                      section_type_value[comp].typename);
407         } else {
408                 list = this->modulelist;
409         }
410
411         request->component = section_type_value[comp].section;
412
413         rcode = modcall(comp, list, request);
414
415         request->module = "<server-core>";
416         request->component = "<server-core>";
417         return rcode;
418 }
419
420 /*
421  *      Load a sub-module list, as found inside an Auth-Type foo {}
422  *      block
423  */
424 static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
425                                      const char *space, 
426                                      int comp, const char *filename)
427 {
428         indexed_modcallable *subcomp;
429         modcallable *ml;
430         DICT_VALUE *dval;
431         const char *name2 = cf_section_name2(cs);
432
433         rad_assert(comp >= RLM_COMPONENT_AUTH);
434         rad_assert(comp <= RLM_COMPONENT_COUNT);
435
436         /*
437          *      Sanity check.
438          */
439         if (!name2) {
440                 radlog(L_ERR|L_CONS,
441                        "%s[%d]: No name specified for %s block",
442                        filename, cf_section_lineno(cs),
443                        section_type_value[comp].typename);
444                 return 1;
445         }
446
447         /*
448          *      Compile the group.
449          */
450         ml = compile_modgroup(parent, comp, cs, filename);
451         if (!ml) {
452                 return 0;
453         }
454
455         /*
456          *      We must assign a numeric index to this subcomponent.
457          *      It is generated and placed in the dictionary by
458          *      setup_modules(), when it loads the sections.  If it
459          *      isn't found, it's a serious error.
460          */
461         dval = dict_valbyname(section_type_value[comp].attr, name2);
462         if (!dval) {
463                 radlog(L_ERR|L_CONS,
464                        "%s[%d] %s %s Not previously configured",
465                        filename, cf_section_lineno(cs),
466                        section_type_value[comp].typename, name2);
467                 modcallable_free(&ml);
468                 return 0;
469         }
470
471         subcomp = new_sublist(space, comp, dval->value);
472         if (!subcomp) {
473                 modcallable_free(&ml);
474                 return 1;
475         }
476
477         subcomp->modulelist = ml;
478         return 1;               /* OK */
479 }
480
481 static int load_component_section(modcallable *parent, CONF_SECTION *cs,
482                                   const char *space, int comp,
483                                   const char *filename)
484 {
485         modcallable *this;
486         CONF_ITEM *modref;
487         int idx;
488         indexed_modcallable *subcomp;
489         const char *modname;
490         const char *visiblename;
491
492         /*
493          *      Loop over the entries in the named section.
494          */
495         for (modref = cf_item_find_next(cs, NULL);
496              modref != NULL;
497              modref = cf_item_find_next(cs, modref)) {
498                 CONF_PAIR *cp = NULL;
499                 CONF_SECTION *scs = NULL;
500
501                 /*
502                  *      Look for Auth-Type foo {}, which are special
503                  *      cases of named sections, and allowable ONLY
504                  *      at the top-level.
505                  *
506                  *      i.e. They're not allowed in a "group" or "redundant"
507                  *      subsection.
508                  */
509                 if (cf_item_is_section(modref)) {
510                         const char *sec_name;
511                         scs = cf_itemtosection(modref);
512
513                         sec_name = cf_section_name1(scs);
514
515                         if (strcmp(sec_name,
516                                    section_type_value[comp].typename) == 0) {
517                                 if (!load_subcomponent_section(parent, scs,
518                                                                space, comp,
519                                                                filename)) {
520                                         return -1; /* FIXME: memleak? */
521                                 }
522                                 continue;
523                         }
524
525                         cp = NULL;
526                 } else if (cf_item_is_pair(modref)) {
527                         cp = cf_itemtopair(modref);
528                 } else {
529                         continue; /* ignore it */
530                 }
531
532                 /*
533                  *      Try to compile one entry.
534                  */
535                 this = compile_modsingle(parent, comp, modref, filename,
536                                          &modname);
537                 if (!this) {
538                         radlog(L_ERR|L_CONS,
539                                "%s[%d] Failed to parse %s section.\n",
540                                filename, cf_section_lineno(cs),
541                                cf_section_name1(cs));
542                         return -1;
543                 }
544
545                 if (comp == RLM_COMPONENT_AUTH) {
546                         DICT_VALUE *dval;
547                         const char *modrefname = NULL;
548                         int lineno = 0;
549
550                         if (cp) {
551                                 modrefname = cf_pair_attr(cp);
552                                 lineno = cf_pair_lineno(cp);
553                         } else {
554                                 modrefname = cf_section_name2(scs);
555                                 lineno = cf_section_lineno(scs);
556                                 if (!modrefname) {
557                                         radlog(L_ERR|L_CONS,
558                                                "%s[%d] Failed to parse %s sub-section.\n",
559                                                filename, lineno,
560                                                cf_section_name1(scs));
561                                         return -1;
562                                 }
563                         }
564
565                         dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
566                         if (!dval) {
567                                 /*
568                                  *      It's a section, but nothing we
569                                  *      recognize.  Die!
570                                  */
571                                 radlog(L_ERR|L_CONS, "%s[%d] Unknown Auth-Type \"%s\" in %s sub-section.",
572                                        filename, lineno,
573                                        modrefname, section_type_value[comp].section);
574                                 return -1;
575                         }
576                         idx = dval->value;
577                 } else {
578                         /* See the comment in new_sublist() for explanation
579                          * of the special index 0 */
580                         idx = 0;
581                 }
582
583                 subcomp = new_sublist(space, comp, idx);
584                 if (subcomp == NULL) {
585                         modcallable_free(&this);
586                         continue;
587                 }
588
589                 /* If subcomp->modulelist is NULL, add_to_modcallable will
590                  * create it */
591                 visiblename = cf_section_name2(cs);
592                 if (visiblename == NULL)
593                         visiblename = cf_section_name1(cs);
594                 add_to_modcallable(&subcomp->modulelist, this,
595                                    comp, visiblename);
596         }
597
598         return 0;
599 }
600
601 static int load_byspace(CONF_SECTION *cs, const char *space,
602                         int *do_component)
603 {
604         int comp;
605
606         DEBUG2(" modules {");
607         
608         /*
609          *      Loop over all of the known components, finding their
610          *      configuration section, and loading it.
611          */
612         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
613                 CONF_SECTION *subcs;
614
615                 if (!do_component[comp]) continue;
616
617                 subcs = cf_section_sub_find(cs,
618                                             section_type_value[comp].section);
619                 if (!subcs) continue;
620                         
621                 if (cf_item_find_next(subcs, NULL) == NULL) continue;
622                         
623                 DEBUG2(" Module: Checking %s {...} for more modules to load",
624                        section_type_value[comp].section);
625
626                 if (load_component_section(NULL, subcs, space, comp,
627                                            mainconfig.radiusd_conf) < 0) {
628                         DEBUG2(" }");
629                         return -1;
630                 }
631         } /* loop over components */
632
633         DEBUG2(" }");
634
635         return 0;
636 }
637
638 /*
639  *      Parse the module config sections, and load
640  *      and call each module's init() function.
641  *
642  *      Libtool makes your life a LOT easier, especially with libltdl.
643  *      see: http://www.gnu.org/software/libtool/
644  */
645 int setup_modules(int reload)
646 {
647         int             comp;
648         CONF_SECTION    *cs, *modules;
649         int             do_component[RLM_COMPONENT_COUNT];
650         rad_listen_t    *listener;
651
652         /*
653          *      If necessary, initialize libltdl.
654          */
655         if (!reload) {
656                 /*
657                  *      Set the default list of preloaded symbols.
658                  *      This is used to initialize libltdl's list of
659                  *      preloaded modules.
660                  *
661                  *      i.e. Static modules.
662                  */
663                 LTDL_SET_PRELOADED_SYMBOLS();
664
665                 if (lt_dlinit() != 0) {
666                         radlog(L_ERR|L_CONS, "Failed to initialize libraries: %s\n",
667                                         lt_dlerror());
668                         return -1;
669                 }
670
671                 /*
672                  *      Set the search path to ONLY our library directory.
673                  *      This prevents the modules from being found from
674                  *      any location on the disk.
675                  */
676                 lt_dlsetsearchpath(radlib_dir);
677
678                 DEBUG2("radiusd: Library search path is %s",
679                        lt_dlgetsearchpath());
680
681                 /*
682                  *      Set up the internal module struct.
683                  */
684                 module_tree = rbtree_create(module_entry_cmp,
685                                             module_entry_free, 0);
686                 if (!module_tree) {
687                         radlog(L_ERR|L_CONS, "Failed to initialize modules\n");
688                         return -1;
689                 }
690         } else {
691                 rbtree_free(components);
692         }
693
694         components = rbtree_create(indexed_modcallable_cmp,
695                                    indexed_modcallable_free, 0);
696         if (!components) {
697                 radlog(L_ERR|L_CONS, "Failed to initialize components\n");
698                 return -1;
699         }
700
701         /*
702          *      Figure out which sections to load.
703          */
704         memset(do_component, 0, sizeof(do_component));
705         for (listener = mainconfig.listen;
706              listener != NULL;
707              listener = listener->next) {
708                 switch (listener->type) {
709                 case RAD_LISTEN_AUTH:
710                         do_component[RLM_COMPONENT_AUTZ] = 1;
711                         do_component[RLM_COMPONENT_AUTH] = 1;
712                         do_component[RLM_COMPONENT_POST_AUTH] = 1;
713                         do_component[RLM_COMPONENT_SESS] = 1;
714                         break;
715
716                 case RAD_LISTEN_DETAIL: /* just like acct */
717                 case RAD_LISTEN_ACCT:
718                         do_component[RLM_COMPONENT_PREACCT] = 1;
719                         do_component[RLM_COMPONENT_ACCT] = 1;
720                         break;
721
722                 case RAD_LISTEN_PROXY:
723                         do_component[RLM_COMPONENT_PRE_PROXY] = 1;
724                         do_component[RLM_COMPONENT_POST_PROXY] = 1;
725                         break;
726
727                 case RAD_LISTEN_VQP:
728                         do_component[RLM_COMPONENT_POST_AUTH] = 1;
729                         break;
730                         /*
731                          *      Ignore this.
732                          */
733                 case RAD_LISTEN_SNMP:
734                         break;
735
736                 default:
737                         rad_assert(0 == 1);
738                         break;
739                 }
740         }
741
742         for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
743                 /*
744                  *      Have the debugging messages all in one place.
745                  */
746                 if (!do_component[comp]) {
747                         DEBUG2("modules: Not loading %s{} section",
748                                section_type_value[comp].section);
749                 }
750         }
751
752         /*
753          *      Create any DICT_VALUE's for the types.  See
754          *      'doc/configurable_failover' for examples of 'authtype'
755          *      used to create new Auth-Type values.  In order to
756          *      let the user create new names, we've got to look for
757          *      those names, and create DICT_VALUE's for them.
758          */
759         for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
760                 int             value;
761                 const char      *name2;
762                 DICT_ATTR       *dattr;
763                 DICT_VALUE      *dval;
764                 CONF_SECTION    *sub, *next;
765                 CONF_PAIR       *cp;
766
767                 /*
768                  *      Not needed, don't load it.
769                  */
770                 if (!do_component[comp]) {
771                         continue;
772                 }
773                 cs = cf_section_find(section_type_value[comp].section);
774
775                 if (!cs) continue;
776
777                 sub = NULL;
778                 do {
779                         /*
780                          *      See if there's a sub-section by that
781                          *      name.
782                          */
783                         next = cf_subsection_find_next(cs, sub,
784                                                        section_type_value[comp].typename);
785                         sub = next;
786
787                         /*
788                          *      If so, look for it to define a new
789                          *      value.
790                          */
791                         name2 = cf_section_name2(sub);
792                         if (!name2) continue;
793
794
795                         /*
796                          *      If the value already exists, don't
797                          *      create it again.
798                          */
799                         dval = dict_valbyname(section_type_value[comp].attr,
800                                               name2);
801                         if (dval) continue;
802
803                         /*
804                          *      Find the attribute for the value.
805                          */
806                         dattr = dict_attrbyvalue(section_type_value[comp].attr);
807                         if (!dattr) {
808                                 radlog(L_ERR, "%s[%d]: No such attribute %s",
809                                        mainconfig.radiusd_conf,
810                                        cf_section_lineno(sub),
811                                        section_type_value[comp].typename);
812                                 continue;
813                         }
814
815                         /*
816                          *      Create a new unique value with a
817                          *      meaningless number.  You can't look at
818                          *      it from outside of this code, so it
819                          *      doesn't matter.  The only requirement
820                          *      is that it's unique.
821                          */
822                         do {
823                                 value = lrad_rand() & 0x00ffffff;
824                         } while (dict_valbyattr(dattr->attr, value));
825
826                         if (dict_addvalue(name2, dattr->name, value) < 0) {
827                                 radlog(L_ERR, "%s", librad_errstr);
828                                 return -1;
829                         }
830                 } while (sub != NULL);
831
832                 /*
833                  *      Loop over the non-sub-sections, too.
834                  */
835                 cp = NULL;
836                 do {
837                         /*
838                          *      See if there's a conf-pair by that
839                          *      name.
840                          */
841                         cp = cf_pair_find_next(cs, cp, NULL);
842                         if (!cp) break;
843
844
845                         /*
846                          *      If the value already exists, don't
847                          *      create it again.
848                          */
849                         name2 = cf_pair_attr(cp);
850                         dval = dict_valbyname(section_type_value[comp].attr,
851                                               name2);
852                         if (dval) continue;
853
854                         /*
855                          *      Find the attribute for the value.
856                          */
857                         dattr = dict_attrbyvalue(section_type_value[comp].attr);
858                         if (!dattr) {
859                                 radlog(L_ERR, "%s[%d]: No such attribute %s",
860                                        mainconfig.radiusd_conf,
861                                        cf_section_lineno(sub),
862                                        section_type_value[comp].typename);
863                                 continue;
864                         }
865
866                         /*
867                          *      Finally, create the new attribute.
868                          */
869                         do {
870                                 value = lrad_rand() & 0x00ffffff;
871                         } while (dict_valbyattr(dattr->attr, value));
872                         if (dict_addvalue(name2, dattr->name, value) < 0) {
873                                 radlog(L_ERR, "%s", librad_errstr);
874                                 return -1;
875                         }
876                 } while (cp != NULL);
877         } /* over the sections which can have redundent sub-sections */
878
879         /*
880          *      Remember where the modules were stored.
881          */
882         modules = cf_section_find("modules");
883         if (!modules) {
884                 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
885                 return -1;
886         }
887
888         /*
889          *  Look for the 'instantiate' section, which tells us
890          *  the instantiation order of the modules, and also allows
891          *  us to load modules with no authorize/authenticate/etc.
892          *  sections.
893          */
894         cs = cf_section_find("instantiate");
895         if (cs != NULL) {
896                 CONF_ITEM *ci;
897                 CONF_PAIR *cp;
898                 module_instance_t *module;
899                 const char *name;
900
901                 DEBUG2(" instantiate {");
902
903                 /*
904                  *  Loop over the items in the 'instantiate' section.
905                  */
906                 for (ci=cf_item_find_next(cs, NULL);
907                      ci != NULL;
908                      ci=cf_item_find_next(cs, ci)) {
909
910                         /*
911                          *      Skip sections.  They'll be handled
912                          *      later, if they're referenced at all...
913                          */
914                         if (cf_item_is_section(ci)) {
915                                 continue;
916                         }
917
918                         cp = cf_itemtopair(ci);
919                         name = cf_pair_attr(cp);
920                         module = find_module_instance(modules, name);
921                         if (!module) {
922                                 return -1;
923                         }
924                 } /* loop over items in the subsection */
925
926                 DEBUG2(" }");
927         } /* if there's an 'instantiate' section. */
928
929         if (load_byspace(mainconfig.config, NULL, do_component) < 0) {
930                 return -1;
931         }
932
933         /*
934          *      Load by identities, and by vmps.
935          */
936         for (listener = mainconfig.listen;
937              listener != NULL;
938              listener = listener->next) {
939                 if (listener->type == RAD_LISTEN_VQP) {
940                         cs = cf_section_find("vmps");
941                         if (!cs) {
942                                 radlog(L_ERR, "Listening on vmps socket, but no vmps section");
943                                 return -1;
944                         }
945                         
946                         if (cf_item_find_next(cs, NULL) == NULL) {
947                                 radlog(L_ERR, "Listening on vmps socket, vmps section is empty");
948                                 return -1;
949                         }
950                         
951                         DEBUG2(" Module: Checking vmps {...} for more modules to load");
952                         
953                         if (load_component_section(NULL, cs, VMPS_SPACE,
954                                                    RLM_COMPONENT_POST_AUTH,
955                                                    mainconfig.radiusd_conf) < 0) {
956                                 return -1;
957                         }
958                         
959                         continue;
960                 }
961
962                 if (!listener->identity) continue;
963
964                 /*
965                  *      Load by identity
966                  */
967                 cs = cf_section_sub_find_name2(mainconfig.config,
968                                                "identity", listener->identity);
969                 if (!cs) continue;
970
971                 DEBUG2("identity %s {", listener->identity);
972                 if (load_byspace(cs, listener->identity, do_component) < 0) {
973                         DEBUG2("}");
974                         return -1;
975                 }
976                 DEBUG2("}");
977         }
978
979         return 0;
980 }
981
982 /*
983  *      Call all authorization modules until one returns
984  *      somethings else than RLM_MODULE_OK
985  */
986 int module_authorize(int autz_type, REQUEST *request)
987 {
988         return indexed_modcall(request->listener->identity,
989                                RLM_COMPONENT_AUTZ, autz_type, request);
990 }
991
992 /*
993  *      Authenticate a user/password with various methods.
994  */
995 int module_authenticate(int auth_type, REQUEST *request)
996 {
997         return indexed_modcall(request->listener->identity,
998                                RLM_COMPONENT_AUTH, auth_type, request);
999 }
1000
1001 /*
1002  *      Do pre-accounting for ALL configured sessions
1003  */
1004 int module_preacct(REQUEST *request)
1005 {
1006         return indexed_modcall(request->listener->identity,
1007                                RLM_COMPONENT_PREACCT, 0, request);
1008 }
1009
1010 /*
1011  *      Do accounting for ALL configured sessions
1012  */
1013 int module_accounting(int acct_type, REQUEST *request)
1014 {
1015         return indexed_modcall(request->listener->identity,
1016                                RLM_COMPONENT_ACCT, acct_type, request);
1017 }
1018
1019 /*
1020  *      See if a user is already logged in.
1021  *
1022  *      Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
1023  */
1024 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
1025 {
1026         int rcode;
1027
1028         if(!request->username)
1029                 return 0;
1030
1031         request->simul_count = 0;
1032         request->simul_max = maxsimul;
1033         request->simul_mpp = 1;
1034
1035         rcode = indexed_modcall(request->listener->identity,
1036                                 RLM_COMPONENT_SESS, sess_type, request);
1037
1038         if (rcode != RLM_MODULE_OK) {
1039                 /* FIXME: Good spot for a *rate-limited* warning to the log */
1040                 return 0;
1041         }
1042
1043         return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1044 }
1045
1046 /*
1047  *      Do pre-proxying for ALL configured sessions
1048  */
1049 int module_pre_proxy(int type, REQUEST *request)
1050 {
1051         return indexed_modcall(request->listener->identity,
1052                                RLM_COMPONENT_PRE_PROXY, type, request);
1053 }
1054
1055 /*
1056  *      Do post-proxying for ALL configured sessions
1057  */
1058 int module_post_proxy(int type, REQUEST *request)
1059 {
1060         return indexed_modcall(request->listener->identity,
1061                                RLM_COMPONENT_POST_PROXY, type, request);
1062 }
1063
1064 /*
1065  *      Do post-authentication for ALL configured sessions
1066  */
1067 int module_post_auth(int postauth_type, REQUEST *request)
1068 {
1069         return indexed_modcall(request->listener->identity,
1070                                RLM_COMPONENT_POST_AUTH, postauth_type, request);
1071 }
1072
1073 /*
1074  *      Do VMPS
1075  */
1076 int module_vmps(REQUEST *request)
1077 {
1078         return indexed_modcall(VMPS_SPACE, RLM_COMPONENT_POST_AUTH, 0,
1079                                request);
1080 }