backport from HEAD
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2003  The FreeRADIUS server project
21  * Copyright 2000  Alan DeKok <aland@ox.org>
22  * Copyright 2000  Alan Curry <pacman@world.std.com>
23  */
24
25 static const char rcsid[] = "$Id$";
26
27 #include "autoconf.h"
28 #include "libradius.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "radiusd.h"
35 #include "modpriv.h"
36 #include "modules.h"
37 #include "modcall.h"
38 #include "conffile.h"
39 #include "ltdl.h"
40 #include "rad_assert.h"
41
42 /*
43  *      Internal list of all of the modules we have loaded.
44  */
45 static module_list_t *module_list = NULL;
46
47 /*
48  *      Internal list of each module instance.
49  */
50 static module_instance_t *module_instance_list = NULL;
51
52 typedef struct indexed_modcallable {
53         struct indexed_modcallable *next;
54         int idx;
55         modcallable *modulelist;
56 } indexed_modcallable;
57
58 /*
59  *      For each component, keep an ordered list of ones to call.
60  */
61 static indexed_modcallable *components[RLM_COMPONENT_COUNT];
62
63 /*
64  *      The component names.
65  *
66  *      Hmm... we probably should be getting these from the configuration
67  *      file, too.
68  */
69 const char *component_names[RLM_COMPONENT_COUNT] =
70 {
71         "authenticate",
72         "authorize",
73         "preacct",
74         "accounting",
75         "session",
76         "pre-proxy",
77         "post-proxy",
78         "post-auth"
79 };
80
81 /*
82  *      Delete ASAP.
83  */
84 static const char *old_subcomponent_names[RLM_COMPONENT_COUNT] =
85 {
86         "authtype",
87         "autztype",
88         "preacctype",
89         "acctype",
90         "sesstype",
91         "pre-proxytype",
92         "post-proxytype",
93         "post-authtype"
94 };
95
96 static const char *subcomponent_names[RLM_COMPONENT_COUNT] =
97 {
98         "Auth-Type",
99         "Autz-Type",
100         "Pre-Acct-Type",
101         "Acct-Type",
102         "Session-Type",
103         "Pre-Proxy-Type",
104         "Post-Proxy-Type",
105         "Post-Auth-Type"
106 };
107
108 static void indexed_modcallable_free(indexed_modcallable **cf)
109 {
110         indexed_modcallable     *c, *next;
111
112         c = *cf;
113         while (c) {
114                 next = c->next;
115                 modcallable_free(&c->modulelist);
116                 free(c);
117                 c = next;
118         }
119         *cf = NULL;
120 }
121
122 static void instance_list_free(module_instance_t **i)
123 {
124         module_instance_t       *c, *next;
125
126         c = *i;
127         while (c) {
128                 next = c->next;
129                 if(c->entry->module->detach)
130                         (c->entry->module->detach)(c->insthandle);
131 #ifdef HAVE_PTHREAD_H
132                 if (c->mutex) {
133                         /*
134                          *      FIXME
135                          *      The mutex MIGHT be locked...
136                          *      we'll check for that later, I guess.
137                          */
138                         pthread_mutex_destroy(c->mutex);
139                         free(c->mutex);
140                 }
141 #endif
142                 free(c);
143                 c = next;
144         }
145         *i = NULL;
146 }
147
148 /*
149  *      Remove all of the modules.
150  */
151 int detach_modules(void)
152 {
153         module_list_t *ml, *next;
154         int i;
155
156         /*
157          *      Delete the internal component pointers.
158          */
159         for (i = 0; i < RLM_COMPONENT_COUNT; i++) {
160                 indexed_modcallable_free(&components[i]);
161         }
162
163         instance_list_free(&module_instance_list);
164
165         ml = module_list;
166         while (ml) {
167                 next = ml->next;
168                 if (ml->module->destroy)
169                         (ml->module->destroy)();
170                 lt_dlclose(ml->handle); /* ignore any errors */
171                 free(ml);
172                 ml = next;
173         }
174
175         module_list = NULL;
176
177         return 0;
178 }
179
180 /*
181  *      Find a module on disk or in memory, and link to it.
182  */
183 static module_list_t *linkto_module(const char *module_name,
184                 const char *cffilename, int cflineno)
185 {
186         module_list_t *node;
187         lt_dlhandle handle;
188         char module_struct[256];
189         char *p;
190
191         /*
192          *      Look through the global module library list for the
193          *      named module.
194          */
195         for (node = module_list; node != NULL; node = node->next) {
196                 /*
197                  *      Found the named module.  Return it.
198                  */
199                 if (strcmp(node->name, module_name) == 0)
200                         return node;
201
202         }
203
204         /*
205          *      Keep the handle around so we can dlclose() it.
206          */
207         handle = lt_dlopenext(module_name);
208         if (handle == NULL) {
209                 radlog(L_ERR|L_CONS, "%s[%d] Failed to link to module '%s':"
210                                 " %s\n", cffilename, cflineno, module_name, lt_dlerror());
211                 return NULL;
212         }
213
214         /* make room for the module type */
215         node = (module_list_t *) rad_malloc(sizeof(module_list_t));
216
217         /* fill in the module structure */
218         node->next = NULL;
219         node->handle = handle;
220         strNcpy(node->name, module_name, sizeof(node->name));
221
222         /*
223          *      Link to the module's rlm_FOO{} module structure.
224          */
225         /* module_name has the version embedded; strip it. */
226         strcpy(module_struct, module_name);
227         p = strrchr(module_struct, '-');
228         if (p)
229                 *p = '\0';
230         node->module = (module_t *) lt_dlsym(node->handle, module_struct);
231         if (!node->module) {
232                 radlog(L_ERR|L_CONS, "%s[%d] Failed linking to "
233                                 "%s structure in %s: %s\n",
234                                 cffilename, cflineno,
235                                 module_name, cffilename, lt_dlerror());
236                 lt_dlclose(node->handle);       /* ignore any errors */
237                 free(node);
238                 return NULL;
239         }
240
241         /* call the modules initialization */
242         if (node->module->init && (node->module->init)() < 0) {
243                 radlog(L_ERR|L_CONS, "%s[%d] Module initialization failed.\n",
244                                 cffilename, cflineno);
245                 lt_dlclose(node->handle);       /* ignore any errors */
246                 free(node);
247                 return NULL;
248         }
249
250         DEBUG("Module: Loaded %s ", node->module->name);
251
252         node->next = module_list;
253         module_list = node;
254
255         return node;
256 }
257
258 /*
259  *      Find a module instance.
260  */
261 module_instance_t *find_module_instance(const char *instname)
262 {
263         CONF_SECTION *cs, *inst_cs;
264         const char *name1, *name2;
265         module_instance_t *node, **last;
266         char module_name[256];
267
268         /*
269          *      Look through the global module instance list for the
270          *      named module.
271          */
272         last = &module_instance_list;
273         for (node = module_instance_list; node != NULL; node = node->next) {
274                 /*
275                  *      Found the named instance.  Return it.
276                  */
277                 if (strcmp(node->name, instname) == 0)
278                         return node;
279
280                 /*
281                  *      Keep a pointer to the last entry to update...
282                  */
283                 last = &node->next;
284         }
285
286         /*
287          *      Instance doesn't exist yet. Try to find the
288          *      corresponding configuration section and create it.
289          */
290
291         /*
292          *      Look for the 'modules' configuration section.
293          */
294         cs = cf_section_find("modules");
295         if (cs == NULL) {
296                 radlog(L_ERR|L_CONS, "ERROR: Cannot find a 'modules' section in the configuration file.\n");
297                 return NULL;
298         }
299
300         /*
301          *      Module instances are declared in the modules{} block
302          *      and referenced later by their name, which is the
303          *      name2 from the config section, or name1 if there was
304          *      no name2.
305          */
306         name1 = name2 = NULL;
307         for(inst_cs=cf_subsection_find_next(cs, NULL, NULL);
308                         inst_cs != NULL;
309                         inst_cs=cf_subsection_find_next(cs, inst_cs, NULL)) {
310                 name1 = cf_section_name1(inst_cs);
311                 name2 = cf_section_name2(inst_cs);
312                 if ( (name2 && !strcmp(name2, instname)) ||
313                      (!name2 && !strcmp(name1, instname)) )
314                         break;
315         }
316         if (inst_cs == NULL) {
317                 radlog(L_ERR|L_CONS, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
318                 return NULL;
319         }
320
321         /*
322          *      Found the configuration entry.
323          */
324         node = rad_malloc(sizeof(*node));
325         node->next = NULL;
326         node->insthandle = NULL;
327
328         /*
329          *      Link to the module by name: rlm_FOO-major.minor
330          */
331         if (strncmp(name1, "rlm_", 4)) {
332 #if 0
333                 snprintf(module_name, sizeof(module_name), "rlm_%s-%d.%d",
334                          name1, RADIUSD_MAJOR_VERSION, RADIUSD_MINOR_VERSION);
335 #else
336                 snprintf(module_name, sizeof(module_name), "rlm_%s",
337                          name1);
338 #endif
339         } else {
340                 strNcpy(module_name, name1, sizeof(module_name));
341
342         }
343
344         /*
345          *  FIXME: "radiusd.conf" is wrong here; must find cf filename
346          */
347         node->entry = linkto_module(module_name, "radiusd.conf",
348                                     cf_section_lineno(inst_cs));
349         if (!node->entry) {
350                 free(node);
351                 /* linkto_module logs any errors */
352                 return NULL;
353         }
354
355         /*
356          *      Call the module's instantiation routine.
357          */
358         if ((node->entry->module->instantiate) &&
359             ((node->entry->module->instantiate)(inst_cs,
360                         &node->insthandle) < 0)) {
361                 radlog(L_ERR|L_CONS,
362                                 "radiusd.conf[%d]: %s: Module instantiation failed.\n",
363                                 cf_section_lineno(inst_cs), instname);
364                 free(node);
365                 return NULL;
366         }
367
368         /*
369          *      We're done.  Fill in the rest of the data structure,
370          *      and link it to the module instance list.
371          */
372         strNcpy(node->name, instname, sizeof(node->name));
373
374 #ifdef HAVE_PTHREAD_H
375         /*
376          *      If we're threaded, check if the module is thread-safe.
377          *
378          *      If it isn't, we create a mutex.
379          */
380         if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
381                 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
382                 /*
383                  *      Initialize the mutex.
384                  */
385                 pthread_mutex_init(node->mutex, NULL);
386         } else {
387                 /*
388                  *      The module is thread-safe.  Don't give it a mutex.
389                  */
390                 node->mutex = NULL;
391         }
392
393 #endif
394         *last = node;
395
396         DEBUG("Module: Instantiated %s (%s) ", name1, node->name);
397
398         return node;
399 }
400
401 static indexed_modcallable *lookup_by_index(indexed_modcallable *head, int idx)
402 {
403         indexed_modcallable *p;
404
405         for (p = head; p != NULL; p = p->next) {
406                 if( p->idx == idx)
407                         return p;
408         }
409         return NULL;
410 }
411
412 static indexed_modcallable *new_sublist(int comp, int idx)
413 {
414         indexed_modcallable **head = &components[comp];
415         indexed_modcallable *node = *head;
416         indexed_modcallable **last = head;
417
418         while (node) {
419                 /* It is an error to try to create a sublist that already
420                  * exists. It would almost certainly be caused by accidental
421                  * duplication in the config file.
422                  *
423                  * index 0 is the exception, because it is used when we want
424                  * to collect _all_ listed modules under a single index by
425                  * default, which is currently the case in all components
426                  * except authenticate. */
427                 if (node->idx == idx) {
428                         if (idx == 0)
429                                 return node;
430                         else
431                                 return NULL;
432                 }
433                 last = &node->next;
434                 node = node->next;
435         }
436
437         node = rad_malloc(sizeof *node);
438         node->next = NULL;
439         node->modulelist = NULL;
440         node->idx = idx;
441         *last = node;
442         return node;
443 }
444
445 static int indexed_modcall(int comp, int idx, REQUEST *request)
446 {
447         indexed_modcallable *this;
448
449         this = lookup_by_index(components[comp], idx);
450         if (!this) {
451                 if (idx != 0) DEBUG2("  ERROR: Unknown value specified for %s.  Cannot perform requested action.",
452                                      subcomponent_names[comp]);
453                 /* Return a default value appropriate for the component */
454                 switch(comp) {
455                         case RLM_COMPONENT_AUTZ:    return RLM_MODULE_NOTFOUND;
456                         case RLM_COMPONENT_AUTH:    return RLM_MODULE_REJECT;
457                         case RLM_COMPONENT_PREACCT: return RLM_MODULE_NOOP;
458                         case RLM_COMPONENT_ACCT:    return RLM_MODULE_NOOP;
459                         case RLM_COMPONENT_SESS:    return RLM_MODULE_FAIL;
460                         case RLM_COMPONENT_PRE_PROXY:  return RLM_MODULE_NOOP;
461                         case RLM_COMPONENT_POST_PROXY: return RLM_MODULE_NOOP;
462                         case RLM_COMPONENT_POST_AUTH:  return RLM_MODULE_NOOP;
463                         default:                    return RLM_MODULE_FAIL;
464                 }
465         }
466
467         DEBUG2("  Processing the %s section of radiusd.conf",
468                component_names[comp]);
469         return modcall(comp, this->modulelist, request);
470 }
471
472 /* Load a flat module list, as found inside an authtype{} block */
473 static void load_subcomponent_section(CONF_SECTION *cs, int comp,
474                                       const char *filename)
475 {
476         int idx;
477         indexed_modcallable *subcomp;
478         modcallable *ml;
479         DICT_VALUE *dval;
480
481         static int meaningless_counter = 1;
482
483         ml = compile_modgroup(comp, cs, filename);
484
485         /* We must assign a numeric index to this subcomponent. For
486          * auth, it is generated and placed in the dictionary by
487          * new_sectiontype_value(). The others are just numbers that are pulled
488          * out of thin air, and the names are neither put into the dictionary
489          * nor checked for uniqueness, but all that could be fixed in a few
490          * minutes, if anyone finds a real use for indexed config of
491          * components other than auth. */
492         dval = NULL;
493         if (comp==RLM_COMPONENT_AUTH) {
494                 dval = dict_valbyname(PW_AUTH_TYPE, cf_section_name2(cs));
495         } else if (comp == RLM_COMPONENT_AUTZ) {
496                 dval = dict_valbyname(PW_AUTZ_TYPE, cf_section_name2(cs));
497         } else if (comp == RLM_COMPONENT_ACCT) {
498                 dval = dict_valbyname(PW_ACCT_TYPE, cf_section_name2(cs));
499         } else if (comp == RLM_COMPONENT_SESS) {
500                 dval = dict_valbyname(PW_SESSION_TYPE, cf_section_name2(cs));
501         } else if (comp == RLM_COMPONENT_POST_AUTH) {
502                 dval = dict_valbyname(PW_POST_AUTH_TYPE, cf_section_name2(cs));
503         }
504
505         if (dval) {
506                 idx = dval->value;
507         } else {
508                 idx = meaningless_counter++;
509         }
510
511         subcomp = new_sublist(comp, idx);
512         if (!subcomp) {
513                 radlog(L_ERR|L_CONS,
514                                 "%s[%d] %s %s already configured - skipping",
515                                 filename, cf_section_lineno(cs),
516                                 subcomponent_names[comp], cf_section_name2(cs));
517                 modcallable_free(&ml);
518                 return;
519         }
520
521         subcomp->modulelist = ml;
522 }
523
524 static int load_component_section(CONF_SECTION *cs, int comp,
525                                   const char *filename)
526 {
527         modcallable *this;
528         CONF_ITEM *modref;
529         int idx;
530         indexed_modcallable *subcomp;
531         const char *modname;
532         char *visiblename;
533
534         for (modref=cf_item_find_next(cs, NULL);
535                         modref != NULL;
536                         modref=cf_item_find_next(cs, modref)) {
537
538                 if (cf_item_is_section(modref)) {
539                         const char *sec_name;
540                         CONF_SECTION *scs;
541                         scs = cf_itemtosection(modref);
542
543                         sec_name = cf_section_name1(scs);
544                         if (strcmp(sec_name,
545                                    subcomponent_names[comp]) == 0) {
546                                 load_subcomponent_section(scs, comp, filename);
547                                 continue;
548                         }
549
550                         /*
551                          *      Allow old names, too.
552                          */
553                         if (strcmp(sec_name,
554                                    old_subcomponent_names[comp]) == 0) {
555                                 load_subcomponent_section(scs, comp, filename);
556                                 continue;
557                         }
558                 } else {
559                         CONF_PAIR *cp;
560                         cp = cf_itemtopair(modref);
561                 }
562
563                 /*
564                  *      FIXME: This calls exit if the reference can't be
565                  *      found.  We should instead print a better error,
566                  *      and return the failure code up the stack.
567                  */
568                 this = compile_modsingle(comp, modref, filename, &modname);
569
570                 if (comp == RLM_COMPONENT_AUTH) {
571                         DICT_VALUE *dval;
572
573                         dval = dict_valbyname(PW_AUTH_TYPE, modname);
574                         if (!dval) {
575                                 /*
576                                  *      It's a section, but nothing we
577                                  *      recognize.  Die!
578                                  */
579                                 radlog(L_ERR|L_CONS, "%s[%d] Unknown Auth-Type \"%s\" in %s section.",
580                                        filename, cf_section_lineno(cs),
581                                        modname, component_names[comp]);
582                                 return -1;
583                         }
584                         idx = dval->value;
585                 } else {
586                         /* See the comment in new_sublist() for explanation
587                          * of the special index 0 */
588                         idx = 0;
589                 }
590
591                 subcomp = new_sublist(comp, idx);
592                 if (subcomp == NULL) {
593                         radlog(L_INFO|L_CONS,
594                                         "%s %s %s already configured - skipping",
595                                         filename, subcomponent_names[comp],
596                                         modname);
597                         modcallable_free(&this);
598                         continue;
599                 }
600
601                 /* If subcomp->modulelist is NULL, add_to_modcallable will
602                  * create it */
603                 visiblename = cf_section_name2(cs);
604                 if (visiblename == NULL)
605                         visiblename = cf_section_name1(cs);
606                 add_to_modcallable(&subcomp->modulelist, this,
607                                 comp, visiblename);
608         }
609
610         return 0;
611 }
612
613 typedef struct section_type_value_t {
614         const char      *section;
615         const char      *typename;
616         int             attr;
617 } section_type_value_t;
618
619 static const section_type_value_t section_type_value[] = {
620         { "authorize",    "Autz-Type",       PW_AUTZ_TYPE },
621         { "authenticate", "Auth-Type",       PW_AUTH_TYPE },
622         { "accounting",   "Acct-Type",       PW_ACCT_TYPE },
623         { "session",      "Session-Type",    PW_SESSION_TYPE },
624         { "post-auth",    "Post-Auth-Type",  PW_POST_AUTH_TYPE },
625         { "preacct",      "Pre-Acct-Type",   PW_PRE_ACCT_TYPE },
626         { "post-proxy",   "Post-Proxy-Type", PW_POST_PROXY_TYPE },
627         { "pre-proxy",    "Pre-Proxy-Type",  PW_PRE_PROXY_TYPE },
628         { NULL, NULL, 0 }
629 };
630
631 /*
632  *      Delete ASAP.
633  */
634 static const section_type_value_t old_section_type_value[] = {
635         { "authorize",    "autztype", PW_AUTZ_TYPE },
636         { "authenticate", "authtype", PW_AUTH_TYPE },
637         { "accounting",   "acctype", PW_ACCT_TYPE },
638         { "session",      "sesstype", PW_SESSION_TYPE },
639         { "post-auth",    "post-authtype", PW_POST_AUTH_TYPE },
640         { NULL, NULL, 0 }
641 };
642
643 /*
644  *      Parse the module config sections, and load
645  *      and call each module's init() function.
646  *
647  *      Libtool makes your life a LOT easier, especially with libltdl.
648  *      see: http://www.gnu.org/software/libtool/
649  */
650 int setup_modules(void)
651 {
652         int comp;
653         CONF_SECTION *cs;
654
655         /*
656          *  FIXME: This should be pulled from somewhere else.
657          */
658         const char *filename="radiusd.conf";
659
660         /*
661          *      No current list of modules: Go initialize libltdl.
662          */
663         if (!module_list) {
664                 /*
665                  *      Set the default list of preloaded symbols.
666                  *      This is used to initialize libltdl's list of
667                  *      preloaded modules.
668                  *
669                  *      i.e. Static modules.
670                  */
671                 LTDL_SET_PRELOADED_SYMBOLS();
672
673                 if (lt_dlinit() != 0) {
674                         radlog(L_ERR|L_CONS, "Failed to initialize libraries: %s\n",
675                                         lt_dlerror());
676                         exit(1); /* FIXME */
677
678                 }
679
680                 /*
681                  *      Set the search path to ONLY our library directory.
682                  *      This prevents the modules from being found from
683                  *      any location on the disk.
684                  */
685                 lt_dlsetsearchpath(radlib_dir);
686
687                 DEBUG2("Module: Library search path is %s",
688                                 lt_dlgetsearchpath());
689
690                 /*
691                  *      Initialize the components.
692                  */
693                 for (comp = 0; comp < RLM_COMPONENT_COUNT; comp++) {
694                         components[comp] = NULL;
695                 }
696
697         } else {
698                 detach_modules();
699         }
700
701         /*
702          *      Create any DICT_VALUE's for the types.  See
703          *      'doc/configurable_failover' for examples of 'authtype'
704          *      used to create new Auth-Type values.  In order to
705          *      let the user create new names, we've got to look for
706          *      those names, and create DICT_VALUE's for them.
707          */
708         for (comp = 0; section_type_value[comp].section != NULL; comp++) {
709                 const char      *name2;
710                 DICT_ATTR       *dattr;
711                 DICT_VALUE      *dval;
712                 CONF_SECTION    *sub, *next;
713                 CONF_PAIR       *cp;
714
715                 /*
716                  *  Big-time YUCK
717                  */
718                 static int my_value = 32767;
719
720                 cs = cf_section_find(section_type_value[comp].section);
721
722                 if (!cs) continue;
723
724                 sub = NULL;
725                 do {
726                         /*
727                          *      See if there's a sub-section by that
728                          *      name.
729                          */
730                         next = cf_subsection_find_next(cs, sub,
731                                                       section_type_value[comp].typename);
732
733                         /*
734                          *      Allow some old names, too.
735                          */
736                         if (!next && (comp <= 4)) {
737
738                                 next = cf_subsection_find_next(cs, sub,
739                                                                old_section_type_value[comp].typename);
740                         }
741                         sub = next;
742
743                         /*
744                          *      If so, look for it to define a new
745                          *      value.
746                          */
747                         name2 = cf_section_name2(sub);
748                         if (!name2) continue;
749
750
751                         /*
752                          *      If the value already exists, don't
753                          *      create it again.
754                          */
755                         dval = dict_valbyname(section_type_value[comp].attr,
756                                               name2);
757                         if (dval) continue;
758
759                         /*
760                          *      Find the attribute for the value.
761                          */
762                         dattr = dict_attrbyvalue(section_type_value[comp].attr);
763                         if (!dattr) continue;
764
765                         /*
766                          *      Finally, create the new attribute.
767                          */
768                         if (dict_addvalue(name2, dattr->name, my_value++) < 0) {
769                                 radlog(L_ERR, "%s", librad_errstr);
770                                 exit(1);
771                         }
772                 } while (sub != NULL);
773
774                 /*
775                  *      Loop over the non-sub-sections, too.
776                  */
777                 cp = NULL;
778                 do {
779                         /*
780                          *      See if there's a conf-pair by that
781                          *      name.
782                          */
783                         cp = cf_pair_find_next(cs, cp, NULL);
784                         if (!cp) break;
785
786
787                         /*
788                          *      If the value already exists, don't
789                          *      create it again.
790                          */
791                         name2 = cf_pair_attr(cp);
792                         dval = dict_valbyname(section_type_value[comp].attr,
793                                               name2);
794                         if (dval) continue;
795
796                         /*
797                          *      Find the attribute for the value.
798                          */
799                         dattr = dict_attrbyvalue(section_type_value[comp].attr);
800                         if (!dattr) continue;
801
802                         /*
803                          *      Finally, create the new attribute.
804                          */
805                         if (dict_addvalue(name2, dattr->name, my_value++) < 0) {
806                                 radlog(L_ERR, "%s", librad_errstr);
807                                 exit(1);
808                         }
809                 } while (cp != NULL);
810         } /* over the sections which can have redundent sub-sections */
811
812         /*
813          *  Look for the 'instantiate' section, which tells us
814          *  the instantiation order of the modules, and also allows
815          *  us to load modules with no authorize/authenticate/etc.
816          *  sections.
817          */
818         cs = cf_section_find("instantiate");
819         if (cs != NULL) {
820                 CONF_ITEM *ci;
821                 CONF_PAIR *cp;
822                 module_instance_t *module;
823                 const char *name;
824
825                 /*
826                  *  Loop over the items in the 'instantiate' section.
827                  */
828                 for (ci=cf_item_find_next(cs, NULL);
829                      ci != NULL;
830                      ci=cf_item_find_next(cs, ci)) {
831
832                         if (cf_item_is_section(ci)) {
833                                 radlog(L_ERR|L_CONS,
834                                        "%s[%d] Subsection for module instantiate is not allowed\n", filename,
835
836                                        cf_section_lineno(cf_itemtosection(ci)));
837                                 exit(1);
838                         }
839
840                         cp = cf_itemtopair(ci);
841                         name = cf_pair_attr(cp);
842                         module = find_module_instance(name);
843                         if (!module) {
844                                 exit(1);
845                         }
846                 } /* loop over items in the subsection */
847         } /* if there's an 'instantiate' section. */
848
849         /*
850          *      Loop over all of the known components, finding their
851          *      configuration section, and loading it.
852          */
853         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
854                 cs = cf_section_find(component_names[comp]);
855                 if (cs == NULL)
856                         continue;
857
858                 if (load_component_section(cs, comp, filename) < 0) {
859                         exit(1);
860                 }
861         }
862
863         return 0;
864 }
865
866 /*
867  *      Call all authorization modules until one returns
868  *      somethings else than RLM_MODULE_OK
869  */
870 int module_authorize(int autz_type, REQUEST *request)
871 {
872         /*
873          *      We have a proxied packet, and we've been told
874          *      to NOT pass proxied packets through 'authorize'
875          *      a second time.  So stop.
876          */
877         if ((request->proxy != NULL &&
878              mainconfig.post_proxy_authorize == FALSE)) {
879                 DEBUG2(" authorize: Skipping authorize in post-proxy stage");
880                 return RLM_MODULE_NOOP;
881         }
882
883         return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
884 }
885
886 /*
887  *      Authenticate a user/password with various methods.
888  */
889 int module_authenticate(int auth_type, REQUEST *request)
890 {
891         return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
892 }
893
894 /*
895  *      Do pre-accounting for ALL configured sessions
896  */
897 int module_preacct(REQUEST *request)
898 {
899         return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
900 }
901
902 /*
903  *      Do accounting for ALL configured sessions
904  */
905 int module_accounting(int acct_type, REQUEST *request)
906 {
907         return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
908 }
909
910 /*
911  *      See if a user is already logged in.
912  *
913  *      Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
914  */
915 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
916 {
917         int rcode;
918
919         if(!components[RLM_COMPONENT_SESS])
920                 return 0;
921
922         if(!request->username)
923                 return 0;
924
925         request->simul_count = 0;
926         request->simul_max = maxsimul;
927         request->simul_mpp = 1;
928
929         rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
930
931         if (rcode != RLM_MODULE_OK) {
932                 /* FIXME: Good spot for a *rate-limited* warning to the log */
933                 return 0;
934         }
935
936         return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
937 }
938
939 /*
940  *      Do pre-proxying for ALL configured sessions
941  */
942 int module_pre_proxy(REQUEST *request)
943 {
944         return indexed_modcall(RLM_COMPONENT_PRE_PROXY, 0, request);
945 }
946
947 /*
948  *      Do post-proxying for ALL configured sessions
949  */
950 int module_post_proxy(REQUEST *request)
951 {
952         return indexed_modcall(RLM_COMPONENT_POST_PROXY, 0, request);
953 }
954
955 /*
956  *      Do post-authentication for ALL configured sessions
957  */
958 int module_post_auth(int postauth_type, REQUEST *request)
959 {
960         return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
961 }
962