replace strNcpy strlcpy -- `cat file_list`
[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/autoconf.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <freeradius-devel/radiusd.h>
35 #include <freeradius-devel/modpriv.h>
36 #include <freeradius-devel/modcall.h>
37 #include <freeradius-devel/rad_assert.h>
38
39 typedef struct indexed_modcallable {
40         int comp;
41         int idx;
42         modcallable *modulelist;
43 } indexed_modcallable;
44
45 /*
46  *      For each component, keep an ordered list of ones to call.
47  */
48 static lrad_hash_table_t *components;
49
50 static rbtree_t *module_tree = NULL;
51
52 typedef struct section_type_value_t {
53         const char      *section;
54         const char      *typename;
55         int             attr;
56 } section_type_value_t;
57
58
59 /*
60  *      Ordered by component
61  */
62 static const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
63         { "authenticate", "Auth-Type",       PW_AUTH_TYPE },
64         { "authorize",    "Autz-Type",       PW_AUTZ_TYPE },
65         { "preacct",      "Pre-Acct-Type",   PW_PRE_ACCT_TYPE },
66         { "accounting",   "Acct-Type",       PW_ACCT_TYPE },
67         { "session",      "Session-Type",    PW_SESSION_TYPE },
68         { "pre-proxy",    "Pre-Proxy-Type",  PW_PRE_PROXY_TYPE },
69         { "post-proxy",   "Post-Proxy-Type", PW_POST_PROXY_TYPE },
70         { "post-auth",    "Post-Auth-Type",  PW_POST_AUTH_TYPE },
71 };
72
73 /*
74  *      Delete ASAP.
75  */
76 static const section_type_value_t old_section_type_value[] = {
77         { "authenticate", "authtype", PW_AUTH_TYPE },
78         { "authorize",    "autztype", PW_AUTZ_TYPE },
79         { "preacct",      "Pre-Acct-Type",   PW_PRE_ACCT_TYPE },/* unused */
80         { "accounting",   "acctype", PW_ACCT_TYPE },
81         { "session",      "sesstype", PW_SESSION_TYPE },
82         { "pre-proxy",    "Pre-Proxy-Type",  PW_PRE_PROXY_TYPE }, /* unused */
83         { "post-proxy",   "Post-Proxy-Type", PW_POST_PROXY_TYPE }, /* unused */
84         { "post-auth",    "post-authtype", PW_POST_AUTH_TYPE }
85 };
86
87
88 static void indexed_modcallable_free(void *data)
89 {
90         indexed_modcallable *c = data;
91
92         modcallable_free(&c->modulelist);
93         free(c);
94 }
95
96 static uint32_t indexed_modcallable_hash(const void *data)
97 {
98         uint32_t hash;
99         const indexed_modcallable *c = data;
100
101         hash = lrad_hash(&c->comp, sizeof(c->comp));
102         return lrad_hash_update(&c->idx, sizeof(c->idx), hash);
103 }
104
105 static int indexed_modcallable_cmp(const void *one, const void *two)
106 {
107         const indexed_modcallable *a = one;
108         const indexed_modcallable *b = two;
109
110         if (a->comp < b->comp) return -1;
111         if (a->comp >  b->comp) return +1;
112
113         return a->idx - b->idx;
114 }
115
116
117 /*
118  *      Free a module instance.
119  */
120 static void module_instance_free(void *data)
121 {
122         module_instance_t *this = data;
123
124         if (this->entry->module->detach)
125                 (this->entry->module->detach)(this->insthandle);
126 #ifdef HAVE_PTHREAD_H
127         if (this->mutex) {
128                 /*
129                  *      FIXME
130                  *      The mutex MIGHT be locked...
131                  *      we'll check for that later, I guess.
132                  */
133                 pthread_mutex_destroy(this->mutex);
134                 free(this->mutex);
135         }
136 #endif
137         free(this);
138 }
139
140
141 /*
142  *      Compare two module entries
143  */
144 static int module_entry_cmp(const void *one, const void *two)
145 {
146         const module_entry_t *a = one;
147         const module_entry_t *b = two;
148
149         return strcmp(a->name, b->name);
150 }
151
152 /*
153  *      Free a module entry.
154  */
155 static void module_entry_free(void *data)
156 {
157         module_entry_t *this = data;
158
159         lt_dlclose(this->handle);       /* ignore any errors */
160         free(this);
161 }
162
163
164 /*
165  *      Remove the module lists.
166  */
167 int detach_modules(void)
168 {
169         lrad_hash_table_free(components);
170         rbtree_free(module_tree);
171
172         return 0;
173 }
174
175
176 /*
177  *      Find a module on disk or in memory, and link to it.
178  */
179 static module_entry_t *linkto_module(const char *module_name,
180                                      const char *cffilename, int cflineno)
181 {
182         module_entry_t myentry;
183         module_entry_t *node;
184         lt_dlhandle handle;
185         char module_struct[256];
186         char *p;
187         const void *module;
188
189         strlcpy(myentry.name, module_name, sizeof(myentry.name));
190         node = rbtree_finddata(module_tree, &myentry);
191         if (node) return node;
192
193         /*
194          *      Keep the handle around so we can dlclose() it.
195          */
196         handle = lt_dlopenext(module_name);
197         if (handle == NULL) {
198                 radlog(L_ERR|L_CONS, "%s[%d] Failed to link to module '%s':"
199                        " %s\n", cffilename, cflineno, module_name, lt_dlerror());
200                 return NULL;
201         }
202
203         /*
204          *      Link to the module's rlm_FOO{} module structure.
205          *
206          *      The module_name variable has the version number
207          *      embedded in it, and we don't want that here.
208          */
209         strcpy(module_struct, module_name);
210         p = strrchr(module_struct, '-');
211         if (p) *p = '\0';
212
213         DEBUG3("    (Loaded %s, checking if it's valid)", module_name);
214
215         /*
216          *      libltld MAY core here, if the handle it gives us contains
217          *      garbage data.
218          */
219         module = lt_dlsym(handle, module_struct);
220         if (!module) {
221                 radlog(L_ERR|L_CONS, "%s[%d] Failed linking to "
222                                 "%s structure in %s: %s\n",
223                                 cffilename, cflineno,
224                                 module_name, cffilename, lt_dlerror());
225                 lt_dlclose(handle);
226                 return NULL;
227         }
228         /*
229          *      Before doing anything else, check if it's sane.
230          */
231         if ((*(const uint32_t *) module) != RLM_MODULE_MAGIC_NUMBER) {
232                 lt_dlclose(handle);
233                 radlog(L_ERR|L_CONS, "%s[%d] Invalid version in module '%s'",
234                        cffilename, cflineno, module_name);
235                 return NULL;
236                 
237         }
238
239         /* make room for the module type */
240         node = rad_malloc(sizeof(*node));
241         memset(node, 0, sizeof(*node));
242         strlcpy(node->name, module_name, sizeof(node->name));
243         node->module = module;
244         node->handle = handle;
245
246         DEBUG("Module: Loaded %s ", node->module->name);
247
248         /*
249          *      Add the module as "rlm_foo-version" to the configuration
250          *      section.
251          */
252         if (!rbtree_insert(module_tree, node)) {
253                 radlog(L_ERR, "Failed to cache module %s", module_name);
254                 lt_dlclose(handle);
255                 free(node);
256                 return NULL;
257         }
258
259         return node;
260 }
261
262 /*
263  *      Find a module instance.
264  */
265 module_instance_t *find_module_instance(CONF_SECTION *modules,
266                                         const char *instname)
267 {
268         CONF_SECTION *cs;
269         const char *name1, *name2;
270         module_instance_t *node;
271         char module_name[256];
272
273         if (!modules) return NULL;
274
275         /*
276          *      Module instances are declared in the modules{} block
277          *      and referenced later by their name, which is the
278          *      name2 from the config section, or name1 if there was
279          *      no name2.
280          */
281         cs = cf_section_sub_find_name2(modules, NULL, instname);
282         if (cs == NULL) {
283                 radlog(L_ERR|L_CONS, "ERROR: Cannot find a configuration entry for module \"%s\".\n", instname);
284                 return NULL;
285         }
286
287         /*
288          *      If there's already a module instance, return it.
289          */
290         node = cf_data_find(cs, "instance");
291         if (node) return node;
292
293         name1 = cf_section_name1(cs);
294         name2 = cf_section_name2(cs);
295
296         /*
297          *      Found the configuration entry.
298          */
299         node = rad_malloc(sizeof(*node));
300         memset(node, 0, sizeof(*node));
301
302         node->insthandle = NULL;
303
304         /*
305          *      Names in the "modules" section aren't prefixed
306          *      with "rlm_", so we add it here.
307          */
308         snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
309
310         node->entry = linkto_module(module_name,
311                                     mainconfig.radiusd_conf,
312                                     cf_section_lineno(cs));
313         if (!node->entry) {
314                 free(node);
315                 /* linkto_module logs any errors */
316                 return NULL;
317         }
318
319         /*
320          *      Call the module's instantiation routine.
321          */
322         if ((node->entry->module->instantiate) &&
323             ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
324                 radlog(L_ERR|L_CONS,
325                                 "%s[%d]: %s: Module instantiation failed.\n",
326                        mainconfig.radiusd_conf, cf_section_lineno(cs),
327                        instname);
328                 free(node);
329                 return NULL;
330         }
331
332         /*
333          *      We're done.  Fill in the rest of the data structure,
334          *      and link it to the module instance list.
335          */
336         strlcpy(node->name, instname, sizeof(node->name));
337
338 #ifdef HAVE_PTHREAD_H
339         /*
340          *      If we're threaded, check if the module is thread-safe.
341          *
342          *      If it isn't, we create a mutex.
343          */
344         if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
345                 node->mutex = (pthread_mutex_t *) rad_malloc(sizeof(pthread_mutex_t));
346                 /*
347                  *      Initialize the mutex.
348                  */
349                 pthread_mutex_init(node->mutex, NULL);
350         } else {
351                 /*
352                  *      The module is thread-safe.  Don't give it a mutex.
353                  */
354                 node->mutex = NULL;
355         }
356
357 #endif
358         cf_data_add(cs, "instance", node, module_instance_free);
359
360         DEBUG("Module: Instantiated %s (%s) ", name1, node->name);
361
362         return node;
363 }
364
365 static indexed_modcallable *lookup_by_index(int comp, int idx)
366 {
367         indexed_modcallable myc;
368
369         myc.comp = comp;
370         myc.idx = idx;
371
372         return lrad_hash_table_finddata(components, &myc);
373 }
374
375 /*
376  *      Create a new sublist.
377  */
378 static indexed_modcallable *new_sublist(int comp, int idx)
379 {
380         indexed_modcallable *c;
381
382         c = lookup_by_index(comp, idx);
383
384         /* It is an error to try to create a sublist that already
385          * exists. It would almost certainly be caused by accidental
386          * duplication in the config file.
387          *
388          * index 0 is the exception, because it is used when we want
389          * to collect _all_ listed modules under a single index by
390          * default, which is currently the case in all components
391          * except authenticate. */
392         if (c) {
393                 if (idx == 0) {
394                         return c;
395                 }
396                 return NULL;
397         }
398
399         c = rad_malloc(sizeof(*c));
400         c->modulelist = NULL;
401         c->comp = comp;
402         c->idx = idx;
403
404         if (!lrad_hash_table_insert(components, c)) {
405                 free(c);
406                 return NULL;
407         }
408
409         return c;
410 }
411
412 static int indexed_modcall(int comp, int idx, REQUEST *request)
413 {
414         int rcode;
415         indexed_modcallable *this;
416
417         this = lookup_by_index(comp, idx);
418         if (!this) {
419                 if (idx != 0) DEBUG2("  ERROR: Unknown value specified for %s.  Cannot perform requested action.",
420                                      section_type_value[comp].typename);
421                 request->component = section_type_value[comp].typename;
422                 rcode = modcall(comp, NULL, request); /* does default action */
423         } else {
424                 DEBUG2("  Processing the %s section of %s",
425                        section_type_value[comp].section,
426                        mainconfig.radiusd_conf);
427                 request->component = section_type_value[comp].typename;
428                 rcode = modcall(comp, this->modulelist, request);
429         }
430         request->module = "<server-core>";
431         request->component = "<server-core>";
432         return rcode;
433 }
434
435 /*
436  *      Load a sub-module list, as found inside an Auth-Type foo {}
437  *      block
438  */
439 static int load_subcomponent_section(modcallable *parent,
440                                      CONF_SECTION *cs, int comp,
441                                      const char *filename)
442 {
443         indexed_modcallable *subcomp;
444         modcallable *ml;
445         DICT_VALUE *dval;
446         const char *name2 = cf_section_name2(cs);
447
448         rad_assert(comp >= RLM_COMPONENT_AUTH);
449         rad_assert(comp <= RLM_COMPONENT_COUNT);
450
451         /*
452          *      Sanity check.
453          */
454         if (!name2) {
455                 radlog(L_ERR|L_CONS,
456                        "%s[%d]: No name specified for %s block",
457                        filename, cf_section_lineno(cs),
458                        section_type_value[comp].typename);
459                 return 1;
460         }
461
462         /*
463          *      Compile the group.
464          */
465         ml = compile_modgroup(parent, comp, cs, filename);
466         if (!ml) {
467                 return 0;
468         }       
469
470         /*
471          *      We must assign a numeric index to this subcomponent.
472          *      It is generated and placed in the dictionary by
473          *      setup_modules(), when it loads the sections.  If it
474          *      isn't found, it's a serious error.
475          */
476         dval = dict_valbyname(section_type_value[comp].attr, name2);
477         if (!dval) {
478                 radlog(L_ERR|L_CONS,
479                        "%s[%d] %s %s Not previously configured",
480                        filename, cf_section_lineno(cs),
481                        section_type_value[comp].typename, name2);
482                 modcallable_free(&ml);
483                 return 0;
484         }
485
486         subcomp = new_sublist(comp, dval->value);
487         if (!subcomp) {
488                 radlog(L_ERR|L_CONS,
489                        "%s[%d] %s %s already configured - skipping",
490                        filename, cf_section_lineno(cs),
491                        section_type_value[comp].typename, name2);
492                 modcallable_free(&ml);
493                 return 1;
494         }
495
496         subcomp->modulelist = ml;
497         return 1;               /* OK */
498 }
499
500 static int load_component_section(modcallable *parent,
501                                   CONF_SECTION *cs, int comp,
502                                   const char *filename)
503 {
504         modcallable *this;
505         CONF_ITEM *modref;
506         int idx;
507         indexed_modcallable *subcomp;
508         const char *modname;
509         const char *visiblename;
510
511         /*
512          *      Loop over the entries in the named section.
513          */
514         for (modref = cf_item_find_next(cs, NULL);
515              modref != NULL;
516              modref = cf_item_find_next(cs, modref)) {
517                 CONF_PAIR *cp = NULL;
518                 CONF_SECTION *scs = NULL;
519
520                 /*
521                  *      Look for Auth-Type foo {}, which are special
522                  *      cases of named sections, and allowable ONLY
523                  *      at the top-level.
524                  *
525                  *      i.e. They're not allowed in a "group" or "redundant"
526                  *      subsection.
527                  */
528                 if (cf_item_is_section(modref)) {
529                         const char *sec_name;
530                         scs = cf_itemtosection(modref);
531
532                         sec_name = cf_section_name1(scs);
533
534                         if (strcmp(sec_name,
535                                    section_type_value[comp].typename) == 0) {
536                                 if (!load_subcomponent_section(parent, scs,
537                                                                comp,
538                                                                filename)) {
539                                         return -1; /* FIXME: memleak? */
540                                 }
541                                 continue;
542                         }
543
544                         /*
545                          *      Allow old names, too.
546                          */
547                         if (strcmp(sec_name,
548                                    old_section_type_value[comp].typename) == 0) {
549                                 if (!load_subcomponent_section(parent, scs,
550                                                                comp,
551                                                                filename)) {
552                                         return -1; /* FIXME: memleak? */
553                                 }
554                                 continue;
555                         }
556                         cp = NULL;
557                 } else if (cf_item_is_pair(modref)) {
558                         cp = cf_itemtopair(modref);
559                 } else {
560                         continue; /* ignore it */
561                 }
562
563                 /*
564                  *      Try to compile one entry.
565                  */
566                 this = compile_modsingle(parent, comp, modref, filename,
567                                          &modname);
568                 if (!this) {
569                         radlog(L_ERR|L_CONS,
570                                "%s[%d] Failed to parse %s section.\n",
571                                filename, cf_section_lineno(cs),
572                                cf_section_name1(cs));
573                         return -1;
574                 }
575
576                 if (comp == RLM_COMPONENT_AUTH) {
577                         DICT_VALUE *dval;
578                         const char *modrefname = NULL;
579                         int lineno = 0;
580
581                         if (cp) {
582                                 modrefname = cf_pair_attr(cp);
583                                 lineno = cf_pair_lineno(cp);
584                         } else {
585                                 modrefname = cf_section_name2(scs);
586                                 lineno = cf_section_lineno(scs);
587                                 if (!modrefname) {
588                                         radlog(L_ERR|L_CONS,
589                                                "%s[%d] Failed to parse %s sub-section.\n",
590                                                filename, lineno,
591                                                cf_section_name1(scs));
592                                         return -1;
593                                 }
594                         }
595
596                         dval = dict_valbyname(PW_AUTH_TYPE, modrefname);
597                         if (!dval) {
598                                 /*
599                                  *      It's a section, but nothing we
600                                  *      recognize.  Die!
601                                  */
602                                 radlog(L_ERR|L_CONS, "%s[%d] Unknown Auth-Type \"%s\" in %s sub-section.",
603                                        filename, lineno,
604                                        modrefname, section_type_value[comp].section);
605                                 return -1;
606                         }
607                         idx = dval->value;
608                 } else {
609                         /* See the comment in new_sublist() for explanation
610                          * of the special index 0 */
611                         idx = 0;
612                 }
613
614                 subcomp = new_sublist(comp, idx);
615                 if (subcomp == NULL) {
616                         radlog(L_INFO|L_CONS,
617                                         "%s %s %s already configured - skipping",
618                                         filename, section_type_value[comp].typename,
619                                         modname);
620                         modcallable_free(&this);
621                         continue;
622                 }
623
624                 /* If subcomp->modulelist is NULL, add_to_modcallable will
625                  * create it */
626                 visiblename = cf_section_name2(cs);
627                 if (visiblename == NULL)
628                         visiblename = cf_section_name1(cs);
629                 add_to_modcallable(&subcomp->modulelist, this,
630                                    comp, visiblename);
631         }
632
633         return 0;
634 }
635
636
637 /*
638  *      Parse the module config sections, and load
639  *      and call each module's init() function.
640  *
641  *      Libtool makes your life a LOT easier, especially with libltdl.
642  *      see: http://www.gnu.org/software/libtool/
643  */
644 int setup_modules(int reload)
645 {
646         int             comp;
647         CONF_SECTION    *cs, *modules;
648         int             do_component[RLM_COMPONENT_COUNT];
649         rad_listen_t    *listener;
650
651         /*
652          *      If necessary, initialize libltdl.
653          */
654         if (!reload) {
655                 /*
656                  *      Set the default list of preloaded symbols.
657                  *      This is used to initialize libltdl's list of
658                  *      preloaded modules.
659                  *
660                  *      i.e. Static modules.
661                  */
662                 LTDL_SET_PRELOADED_SYMBOLS();
663
664                 if (lt_dlinit() != 0) {
665                         radlog(L_ERR|L_CONS, "Failed to initialize libraries: %s\n",
666                                         lt_dlerror());
667                         return -1;
668                 }
669
670                 /*
671                  *      Set the search path to ONLY our library directory.
672                  *      This prevents the modules from being found from
673                  *      any location on the disk.
674                  */
675                 lt_dlsetsearchpath(radlib_dir);
676
677                 DEBUG2("Module: Library search path is %s",
678                                 lt_dlgetsearchpath());
679
680                 /*
681                  *      Set up the internal module struct.
682                  */
683                 module_tree = rbtree_create(module_entry_cmp,
684                                             module_entry_free, 0);
685                 if (!module_tree) {
686                         radlog(L_ERR|L_CONS, "Failed to initialize modules\n");
687                         return -1;
688                 }
689         } else {
690                 lrad_hash_table_free(components);
691         }
692
693         components = lrad_hash_table_create(indexed_modcallable_hash,
694                                             indexed_modcallable_cmp,
695                                             indexed_modcallable_free);
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                 default:
728                         rad_assert(0 == 1);
729                         break;
730                 }
731         }
732
733         for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
734                 /*
735                  *      Have the debugging messages all in one place.
736                  */
737                 if (!do_component[comp]) {
738                         DEBUG2("modules: Not loading %s{} section",
739                                section_type_value[comp].section);
740                 }
741         }
742
743         /*
744          *      Create any DICT_VALUE's for the types.  See
745          *      'doc/configurable_failover' for examples of 'authtype'
746          *      used to create new Auth-Type values.  In order to
747          *      let the user create new names, we've got to look for
748          *      those names, and create DICT_VALUE's for them.
749          */
750         for (comp = RLM_COMPONENT_AUTH; comp < RLM_COMPONENT_COUNT; comp++) {
751                 int             value;
752                 const char      *name2;
753                 DICT_ATTR       *dattr;
754                 DICT_VALUE      *dval;
755                 CONF_SECTION    *sub, *next;
756                 CONF_PAIR       *cp;
757
758                 /*
759                  *      Not needed, don't load it.
760                  */
761                 if (!do_component[comp]) {
762                         continue;
763                 }
764                 cs = cf_section_find(section_type_value[comp].section);
765
766                 if (!cs) continue;
767
768                 sub = NULL;
769                 do {
770                         /*
771                          *      See if there's a sub-section by that
772                          *      name.
773                          */
774                         next = cf_subsection_find_next(cs, sub,
775                                                        section_type_value[comp].typename);
776
777                         /*
778                          *      Allow some old names, too.
779                          */
780                         if (!next && (comp <= 4)) {
781                                 next = cf_subsection_find_next(cs, sub,
782                                                                old_section_type_value[comp].typename);
783                         }
784                         sub = next;
785
786                         /*
787                          *      If so, look for it to define a new
788                          *      value.
789                          */
790                         name2 = cf_section_name2(sub);
791                         if (!name2) continue;
792
793
794                         /*
795                          *      If the value already exists, don't
796                          *      create it again.
797                          */
798                         dval = dict_valbyname(section_type_value[comp].attr,
799                                               name2);
800                         if (dval) continue;
801
802                         /*
803                          *      Find the attribute for the value.
804                          */
805                         dattr = dict_attrbyvalue(section_type_value[comp].attr);
806                         if (!dattr) {
807                                 radlog(L_ERR, "%s[%d]: No such attribute %s",
808                                        mainconfig.radiusd_conf,
809                                        cf_section_lineno(sub),
810                                        section_type_value[comp].typename);
811                                 continue;
812                         }
813
814                         /*
815                          *      Create a new unique value with a
816                          *      meaningless number.  You can't look at
817                          *      it from outside of this code, so it
818                          *      doesn't matter.  The only requirement
819                          *      is that it's unique.
820                          */
821                         do {
822                                 value = lrad_rand() & 0x00ffffff;
823                         } while (dict_valbyattr(dattr->attr, value));
824
825                         if (dict_addvalue(name2, dattr->name, value) < 0) {
826                                 radlog(L_ERR, "%s", librad_errstr);
827                                 return -1;
828                         }
829                 } while (sub != NULL);
830
831                 /*
832                  *      Loop over the non-sub-sections, too.
833                  */
834                 cp = NULL;
835                 do {
836                         /*
837                          *      See if there's a conf-pair by that
838                          *      name.
839                          */
840                         cp = cf_pair_find_next(cs, cp, NULL);
841                         if (!cp) break;
842
843
844                         /*
845                          *      If the value already exists, don't
846                          *      create it again.
847                          */
848                         name2 = cf_pair_attr(cp);
849                         dval = dict_valbyname(section_type_value[comp].attr,
850                                               name2);
851                         if (dval) continue;
852
853                         /*
854                          *      Find the attribute for the value.
855                          */
856                         dattr = dict_attrbyvalue(section_type_value[comp].attr);
857                         if (!dattr) {
858                                 radlog(L_ERR, "%s[%d]: No such attribute %s",
859                                        mainconfig.radiusd_conf,
860                                        cf_section_lineno(sub),
861                                        section_type_value[comp].typename);
862                                 continue;
863                         }
864
865                         /*
866                          *      Finally, create the new attribute.
867                          */
868                         do {
869                                 value = lrad_rand() & 0x00ffffff;
870                         } while (dict_valbyattr(dattr->attr, value));
871                         if (dict_addvalue(name2, dattr->name, value) < 0) {
872                                 radlog(L_ERR, "%s", librad_errstr);
873                                 return -1;
874                         }
875                 } while (cp != NULL);
876         } /* over the sections which can have redundent sub-sections */
877
878         /*
879          *      Remember where the modules were stored.
880          */
881         modules = cf_section_find("modules");
882         if (!modules) {
883                 radlog(L_ERR, "Cannot find a \"modules\" section in the configuration file!");
884                 return -1;
885         }
886
887         /*
888          *  Look for the 'instantiate' section, which tells us
889          *  the instantiation order of the modules, and also allows
890          *  us to load modules with no authorize/authenticate/etc.
891          *  sections.
892          */
893         cs = cf_section_find("instantiate");
894         if (cs != NULL) {
895                 CONF_ITEM *ci;
896                 CONF_PAIR *cp;
897                 module_instance_t *module;
898                 const char *name;
899
900                 /*
901                  *  Loop over the items in the 'instantiate' section.
902                  */
903                 for (ci=cf_item_find_next(cs, NULL);
904                      ci != NULL;
905                      ci=cf_item_find_next(cs, ci)) {
906
907                         /*
908                          *      Skip sections.  They'll be handled
909                          *      later, if they're referenced at all...
910                          */
911                         if (cf_item_is_section(ci)) {
912                                 continue;
913                         }
914
915                         cp = cf_itemtopair(ci);
916                         name = cf_pair_attr(cp);
917                         module = find_module_instance(modules, name);
918                         if (!module) {
919                                 return -1;
920                         }
921                 } /* loop over items in the subsection */
922         } /* if there's an 'instantiate' section. */
923
924         /*
925          *      Loop over all of the known components, finding their
926          *      configuration section, and loading it.
927          */
928         for (comp = 0; comp < RLM_COMPONENT_COUNT; ++comp) {
929                 cs = cf_section_find(section_type_value[comp].section);
930                 if (cs == NULL)
931                         continue;
932
933                 if (!do_component[comp]) {
934                         continue;
935                 }
936
937                 if (load_component_section(NULL, cs, comp, mainconfig.radiusd_conf) < 0) {
938                         return -1;
939                 }
940         }
941
942         return 0;
943 }
944
945 /*
946  *      Call all authorization modules until one returns
947  *      somethings else than RLM_MODULE_OK
948  */
949 int module_authorize(int autz_type, REQUEST *request)
950 {
951         /*
952          *      Older versions of the server would pass proxy requests
953          *      through the 'authorize' sections twice; once when the
954          *      packet was received from the NAS, and again after the
955          *      reply was received from the home server.  Now that we
956          *      have a 'post_proxy' section, the replies from the home
957          *      server should be sent through that, instead of through
958          *      the 'authorize' section again.
959          */
960         if (request->proxy != NULL) {
961                 DEBUG2(" authorize: Skipping authorize in post-proxy stage");
962                 return RLM_MODULE_NOOP;
963         }
964
965         return indexed_modcall(RLM_COMPONENT_AUTZ, autz_type, request);
966 }
967
968 /*
969  *      Authenticate a user/password with various methods.
970  */
971 int module_authenticate(int auth_type, REQUEST *request)
972 {
973         return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
974 }
975
976 /*
977  *      Do pre-accounting for ALL configured sessions
978  */
979 int module_preacct(REQUEST *request)
980 {
981         return indexed_modcall(RLM_COMPONENT_PREACCT, 0, request);
982 }
983
984 /*
985  *      Do accounting for ALL configured sessions
986  */
987 int module_accounting(int acct_type, REQUEST *request)
988 {
989         return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
990 }
991
992 /*
993  *      See if a user is already logged in.
994  *
995  *      Returns: 0 == OK, 1 == double logins, 2 == multilink attempt
996  */
997 int module_checksimul(int sess_type, REQUEST *request, int maxsimul)
998 {
999         int rcode;
1000
1001         if(!request->username)
1002                 return 0;
1003
1004         request->simul_count = 0;
1005         request->simul_max = maxsimul;
1006         request->simul_mpp = 1;
1007
1008         rcode = indexed_modcall(RLM_COMPONENT_SESS, sess_type, request);
1009
1010         if (rcode != RLM_MODULE_OK) {
1011                 /* FIXME: Good spot for a *rate-limited* warning to the log */
1012                 return 0;
1013         }
1014
1015         return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1016 }
1017
1018 /*
1019  *      Do pre-proxying for ALL configured sessions
1020  */
1021 int module_pre_proxy(int type, REQUEST *request)
1022 {
1023         return indexed_modcall(RLM_COMPONENT_PRE_PROXY, type, request);
1024 }
1025
1026 /*
1027  *      Do post-proxying for ALL configured sessions
1028  */
1029 int module_post_proxy(int type, REQUEST *request)
1030 {
1031         return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1032 }
1033
1034 /*
1035  *      Do post-authentication for ALL configured sessions
1036  */
1037 int module_post_auth(int postauth_type, REQUEST *request)
1038 {
1039         return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1040 }
1041