~yolanda.robla/ubuntu/saucy/freeradius/dep-8-tests

« back to all changes in this revision

Viewing changes to src/main/modules.c

  • Committer: Bazaar Package Importer
  • Author(s): Josip Rodin
  • Date: 2009-11-23 03:57:37 UTC
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20091123035737-snauioz5r9tf8sdr
Tags: upstream-2.1.7+dfsg
ImportĀ upstreamĀ versionĀ 2.1.7+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * modules.c    Radius module support.
3
3
 *
4
 
 * Version:     $Id: modules.c,v 1.159 2008/04/20 15:00:07 aland Exp $
 
4
 * Version:     $Id$
5
5
 *
6
6
 *   This program is free software; you can redistribute it and/or modify
7
7
 *   it under the terms of the GNU General Public License as published by
23
23
 */
24
24
 
25
25
#include <freeradius-devel/ident.h>
26
 
RCSID("$Id: modules.c,v 1.159 2008/04/20 15:00:07 aland Exp $")
 
26
RCSID("$Id$")
27
27
 
28
28
#include <freeradius-devel/radiusd.h>
29
29
#include <freeradius-devel/modpriv.h>
33
33
extern int check_config;
34
34
 
35
35
typedef struct indexed_modcallable {
36
 
        const           char *server;
37
36
        int             comp;
38
37
        int             idx;
39
38
        modcallable     *modulelist;
40
39
} indexed_modcallable;
41
40
 
 
41
typedef struct virtual_server_t {
 
42
        const char      *name;
 
43
        time_t          created;
 
44
        int             can_free;
 
45
        CONF_SECTION    *cs;
 
46
        rbtree_t        *components;
 
47
        modcallable     *mc[RLM_COMPONENT_COUNT];
 
48
        CONF_SECTION    *subcs[RLM_COMPONENT_COUNT];
 
49
        struct virtual_server_t *next;
 
50
} virtual_server_t;
 
51
 
42
52
/*
43
 
 *      For each component, keep an ordered list of ones to call.
 
53
 *      Keep a hash of virtual servers, so that we can reload them.
44
54
 */
45
 
static rbtree_t *components = NULL;
 
55
#define VIRTUAL_SERVER_HASH_SIZE (256)
 
56
static virtual_server_t *virtual_servers[VIRTUAL_SERVER_HASH_SIZE];
46
57
 
47
58
static rbtree_t *module_tree = NULL;
48
59
 
49
60
static rbtree_t *instance_tree = NULL;
50
61
 
 
62
struct fr_module_hup_t {
 
63
        module_instance_t       *mi;
 
64
        time_t                  when;
 
65
        void                    *insthandle;
 
66
        fr_module_hup_t         *next;
 
67
};
 
68
 
 
69
 
51
70
typedef struct section_type_value_t {
52
71
        const char      *section;
53
72
        const char      *typename;
54
73
        int             attr;
55
74
} section_type_value_t;
56
75
 
57
 
 
58
76
/*
59
77
 *      Ordered by component
60
78
 */
66
84
        { "session",      "Session-Type",    PW_SESSION_TYPE },
67
85
        { "pre-proxy",    "Pre-Proxy-Type",  PW_PRE_PROXY_TYPE },
68
86
        { "post-proxy",   "Post-Proxy-Type", PW_POST_PROXY_TYPE },
69
 
        { "post-auth",    "Post-Auth-Type",  PW_POST_AUTH_TYPE },
70
 
};
 
87
        { "post-auth",    "Post-Auth-Type",  PW_POST_AUTH_TYPE }
 
88
#ifdef WITH_COA
 
89
        ,
 
90
        { "recv-coa",     "Recv-CoA-Type",   PW_RECV_COA_TYPE },
 
91
        { "send-coa",     "Send-CoA-Type",   PW_SEND_COA_TYPE }
 
92
#endif
 
93
};
 
94
 
 
95
 
 
96
#ifdef WITHOUT_LIBLTDL
 
97
#ifdef WITH_DLOPEN
 
98
#include <dlfcn.h>
 
99
 
 
100
#ifndef RTLD_NOW
 
101
#define RTLD_NOW (0)
 
102
#endif
 
103
#ifndef RTLD_LOCAL
 
104
#define RTLD_LOCAL (0)
 
105
#endif
 
106
 
 
107
lt_dlhandle lt_dlopenext(const char *name)
 
108
{
 
109
        char buffer[256];
 
110
 
 
111
        strlcpy(buffer, name, sizeof(buffer));
 
112
 
 
113
        /*
 
114
         *      FIXME: Make this configurable...
 
115
         */
 
116
        strlcat(buffer, ".so", sizeof(buffer));
 
117
 
 
118
        return dlopen(buffer, RTLD_NOW | RTLD_LOCAL);
 
119
}
 
120
 
 
121
void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
 
122
{
 
123
        return dlsym(handle, symbol);
 
124
}
 
125
 
 
126
int lt_dlclose(lt_dlhandle handle)
 
127
{
 
128
        return dlclose(handle);
 
129
}
 
130
 
 
131
const char *lt_dlerror(void)
 
132
{
 
133
        return dlerror();
 
134
}
 
135
 
 
136
 
 
137
#else  /* without dlopen */
 
138
typedef struct lt_dlmodule_t {
 
139
  const char    *name;
 
140
  void          *ref;
 
141
} lt_dlmodule_t;
 
142
 
 
143
typedef struct eap_type_t EAP_TYPE;
 
144
typedef struct rlm_sql_module_t rlm_sql_module_t;
 
145
 
 
146
/*
 
147
 *      FIXME: Write hackery to auto-generate this data.
 
148
 *      We only need to do this on systems that don't have dlopen.
 
149
 */
 
150
extern module_t rlm_pap;
 
151
extern module_t rlm_chap;
 
152
extern module_t rlm_eap;
 
153
extern module_t rlm_sql;
 
154
/* and so on ... */
 
155
 
 
156
extern EAP_TYPE rlm_eap_md5;
 
157
extern rlm_sql_module_t rlm_sql_mysql;
 
158
/* and so on ... */
 
159
 
 
160
static const lt_dlmodule_t lt_dlmodules[] = {
 
161
        { "rlm_pap", &rlm_pap },
 
162
        { "rlm_chap", &rlm_chap },
 
163
        { "rlm_eap", &rlm_eap },
 
164
        /* and so on ... */
 
165
 
 
166
        { "rlm_eap_md5", &rlm_eap_md5 },
 
167
        /* and so on ... */
 
168
                
 
169
        { "rlm_sql_mysql", &rlm_sql_mysql },
 
170
        /* and so on ... */
 
171
                
 
172
        { NULL, NULL }
 
173
};
 
174
 
 
175
lt_dlhandle lt_dlopenext(const char *name)
 
176
{
 
177
        int i;
 
178
 
 
179
        for (i = 0; lt_dlmodules[i].name != NULL; i++) {
 
180
                if (strcmp(name, lt_dlmodules[i].name) == 0) {
 
181
                        return lt_dlmodules[i].ref;
 
182
                }
 
183
        }
 
184
 
 
185
        return NULL;
 
186
}
 
187
 
 
188
void *lt_dlsym(lt_dlhandle handle, UNUSED const char *symbol)
 
189
{
 
190
        return handle;
 
191
}
 
192
 
 
193
int lt_dlclose(lt_dlhandle handle)
 
194
{
 
195
        return 0;
 
196
}
 
197
 
 
198
const char *lt_dlerror(void)
 
199
{
 
200
        return "Unspecified error";
 
201
}
 
202
 
 
203
#endif  /* WITH_DLOPEN */
 
204
#endif /* WITHOUT_LIBLTDL */
 
205
 
 
206
static int virtual_server_idx(const char *name)
 
207
{
 
208
        uint32_t hash;
 
209
 
 
210
        if (!name) return 0;
 
211
 
 
212
        hash = fr_hash_string(name);
 
213
                
 
214
        return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
 
215
}
 
216
 
 
217
static void virtual_server_free(virtual_server_t *server)
 
218
{
 
219
        if (!server) return;
 
220
 
 
221
        if (server->components) rbtree_free(server->components);
 
222
        server->components = NULL;
 
223
 
 
224
        free(server);
 
225
}
 
226
 
 
227
void virtual_servers_free(time_t when)
 
228
{
 
229
        int i;
 
230
        virtual_server_t **last;
 
231
        
 
232
        for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
 
233
                virtual_server_t *server, *next;
 
234
 
 
235
                last = &virtual_servers[i];
 
236
                for (server = virtual_servers[i];
 
237
                     server != NULL;
 
238
                     server = next) {
 
239
                        next = server->next;
 
240
 
 
241
                        /*
 
242
                         *      If we delete it, fix the links so that
 
243
                         *      we don't orphan anything.  Also,
 
244
                         *      delete it if it's old, AND a newer one
 
245
                         *      was defined.
 
246
                         *
 
247
                         *      Otherwise, the last pointer gets set to
 
248
                         *      the one we didn't delete.
 
249
                         */
 
250
                        if ((when == 0) ||
 
251
                            ((server->created < when) && server->can_free)) {
 
252
                                *last = server->next;
 
253
                                virtual_server_free(server);
 
254
                        } else {
 
255
                                last = &(server->next);
 
256
                        }
 
257
                }
 
258
        }
 
259
}
71
260
 
72
261
static void indexed_modcallable_free(void *data)
73
262
{
79
268
 
80
269
static int indexed_modcallable_cmp(const void *one, const void *two)
81
270
{
82
 
        int rcode;
83
271
        const indexed_modcallable *a = one;
84
272
        const indexed_modcallable *b = two;
85
273
 
86
 
        if (a->server && !b->server) return -1;
87
 
        if (!a->server && b->server) return +1;
88
 
        if (a->server && b->server) {
89
 
                rcode = strcmp(a->server, b->server);
90
 
                if (rcode != 0) return rcode;
91
 
        }
92
 
 
93
274
        if (a->comp < b->comp) return -1;
94
275
        if (a->comp >  b->comp) return +1;
95
276
 
109
290
}
110
291
 
111
292
 
 
293
static void module_instance_free_old(CONF_SECTION *cs, module_instance_t *node,
 
294
                                     time_t when)
 
295
{
 
296
        fr_module_hup_t *mh, **last;
 
297
 
 
298
        /*
 
299
         *      Walk the list, freeing up old instances.
 
300
         */
 
301
        last = &(node->mh);
 
302
        while (*last) {
 
303
                mh = *last;
 
304
 
 
305
                /*
 
306
                 *      Free only every 60 seconds.
 
307
                 */
 
308
                if ((when - mh->when) < 60) {
 
309
                        last = &(mh->next);
 
310
                        continue;
 
311
                }
 
312
 
 
313
                cf_section_parse_free(cs, mh->insthandle);
 
314
                
 
315
                if (node->entry->module->detach) {
 
316
                        (node->entry->module->detach)(mh->insthandle);
 
317
                } else {
 
318
                        free(mh->insthandle);
 
319
                }
 
320
 
 
321
                *last = mh->next;
 
322
                free(mh);
 
323
        }
 
324
}
 
325
 
 
326
 
112
327
/*
113
328
 *      Free a module instance.
114
329
 */
116
331
{
117
332
        module_instance_t *this = data;
118
333
 
 
334
        module_instance_free_old(this->cs, this, time(NULL) + 100);
 
335
 
119
336
        if (this->entry->module->detach) {
120
 
                int i;
121
 
 
122
337
                (this->entry->module->detach)(this->insthandle);
123
 
                for (i = 0; i < 16; i++) {
124
 
                        if (this->old_insthandle[i]) {
125
 
                                (this->entry->module->detach)(this->old_insthandle[i]);
126
 
                        }
127
 
                }
128
338
        }
129
339
 
130
340
#ifdef HAVE_PTHREAD_H
173
383
int detach_modules(void)
174
384
{
175
385
        rbtree_free(instance_tree);
176
 
        rbtree_free(components);
177
386
        rbtree_free(module_tree);
178
387
 
179
388
        lt_dlexit();
273
482
 *      Find a module instance.
274
483
 */
275
484
module_instance_t *find_module_instance(CONF_SECTION *modules,
276
 
                                        const char *instname)
 
485
                                        const char *instname, int do_link)
277
486
{
 
487
        int check_config_safe = FALSE;
278
488
        CONF_SECTION *cs;
279
 
        const char *name1, *name2;
 
489
        const char *name1;
280
490
        module_instance_t *node, myNode;
281
491
        char module_name[256];
282
492
 
301
511
        node = rbtree_finddata(instance_tree, &myNode);
302
512
        if (node) return node;
303
513
 
 
514
        if (!do_link) return NULL;
 
515
 
304
516
        name1 = cf_section_name1(cs);
305
 
        name2 = cf_section_name2(cs);
306
517
 
307
518
        /*
308
519
         *      Found the configuration entry.
311
522
        memset(node, 0, sizeof(*node));
312
523
 
313
524
        node->insthandle = NULL;
 
525
        node->cs = cs;
314
526
 
315
527
        /*
316
528
         *      Names in the "modules" section aren't prefixed
327
539
 
328
540
        if (check_config && (node->entry->module->instantiate) &&
329
541
            (node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) == 0) {
 
542
                const char *value = NULL;
 
543
                CONF_PAIR *cp;
 
544
 
 
545
                cp = cf_pair_find(cs, "force_check_config");
 
546
                if (cp) value = cf_pair_value(cp);
 
547
 
 
548
                if (value && (strcmp(value, "yes") == 0)) goto print_inst;
 
549
 
330
550
                cf_log_module(cs, "Skipping instantiation of %s", instname);
331
551
        } else {
 
552
        print_inst:
 
553
                check_config_safe = TRUE;
332
554
                cf_log_module(cs, "Instantiating %s", instname);
333
555
        }
334
556
 
336
558
         *      Call the module's instantiation routine.
337
559
         */
338
560
        if ((node->entry->module->instantiate) &&
339
 
            (!check_config ||
340
 
             ((node->entry->module->type & RLM_TYPE_CHECK_CONFIG_SAFE) != 0)) &&
 
561
            (!check_config || check_config_safe) &&
341
562
            ((node->entry->module->instantiate)(cs, &node->insthandle) < 0)) {
342
563
                cf_log_err(cf_sectiontoitem(cs),
343
564
                           "Instantiation failed for module \"%s\"",
377
598
        return node;
378
599
}
379
600
 
380
 
static indexed_modcallable *lookup_by_index(const char *server, int comp,
381
 
                                            int idx)
 
601
static indexed_modcallable *lookup_by_index(rbtree_t *components,
 
602
                                            int comp, int idx)
382
603
{
383
604
        indexed_modcallable myc;
384
605
        
385
606
        myc.comp = comp;
386
607
        myc.idx = idx;
387
 
        myc.server = server;
388
608
 
389
609
        return rbtree_finddata(components, &myc);
390
610
}
392
612
/*
393
613
 *      Create a new sublist.
394
614
 */
395
 
static indexed_modcallable *new_sublist(const char *server, int comp, int idx)
 
615
static indexed_modcallable *new_sublist(rbtree_t *components, int comp, int idx)
396
616
{
397
617
        indexed_modcallable *c;
398
618
 
399
 
        c = lookup_by_index(server, comp, idx);
 
619
        c = lookup_by_index(components, comp, idx);
400
620
 
401
621
        /* It is an error to try to create a sublist that already
402
622
         * exists. It would almost certainly be caused by accidental
415
635
 
416
636
        c = rad_malloc(sizeof(*c));
417
637
        c->modulelist = NULL;
418
 
        c->server = server;
419
638
        c->comp = comp;
420
639
        c->idx = idx;
421
640
 
430
649
int indexed_modcall(int comp, int idx, REQUEST *request)
431
650
{
432
651
        int rcode;
433
 
        indexed_modcallable *this;
434
652
        modcallable *list = NULL;
435
 
 
436
 
        this = lookup_by_index(request->server, comp, idx);
437
 
        if (!this) {
438
 
                if (idx != 0) DEBUG2("  WARNING: Unknown value specified for %s.  Cannot perform requested action.",
439
 
                                     section_type_value[comp].typename);
 
653
        virtual_server_t *server;
 
654
 
 
655
        /*
 
656
         *      Hack to find the correct virtual server.
 
657
         */
 
658
        rcode = virtual_server_idx(request->server);
 
659
        for (server = virtual_servers[rcode];
 
660
             server != NULL;
 
661
             server = server->next) {
 
662
                if (!request->server && !server->name) break;
 
663
 
 
664
                if ((request->server && server->name) &&
 
665
                    (strcmp(request->server, server->name) == 0)) break;
 
666
        }
 
667
 
 
668
        if (!server) {
 
669
                RDEBUG("No such virtual server %s", request->server);
 
670
                return RLM_MODULE_FAIL;
 
671
        }
 
672
 
 
673
        if (idx == 0) {
 
674
                list = server->mc[comp];
 
675
                if (!list) RDEBUG2("  WARNING: Empty section.  Using default return values.");
 
676
 
440
677
        } else {
441
 
                list = this->modulelist;
 
678
                indexed_modcallable *this;
 
679
 
 
680
                this = lookup_by_index(server->components, comp, idx);
 
681
                if (this) {
 
682
                        list = this->modulelist;
 
683
                } else {
 
684
                        RDEBUG2("  WARNING: Unknown value specified for %s.  Cannot perform requested action.",
 
685
                                section_type_value[comp].typename);
 
686
                }
442
687
        }
443
688
 
444
689
        request->component = section_type_value[comp].section;
445
690
 
446
691
        rcode = modcall(comp, list, request);
447
692
 
448
 
        request->module = "<server-core>";
449
 
        request->component = "<server-core>";
 
693
        request->module = "";
 
694
        request->component = "";
450
695
        return rcode;
451
696
}
452
697
 
455
700
 *      block
456
701
 */
457
702
static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
458
 
                                     const char *server, int attr, int comp)
 
703
                                     rbtree_t *components, int attr, int comp)
459
704
{
460
705
        indexed_modcallable *subcomp;
461
706
        modcallable *ml;
463
708
        const char *name2 = cf_section_name2(cs);
464
709
 
465
710
        rad_assert(comp >= RLM_COMPONENT_AUTH);
466
 
        rad_assert(comp <= RLM_COMPONENT_COUNT);
 
711
        rad_assert(comp < RLM_COMPONENT_COUNT);
467
712
 
468
713
        /*
469
714
         *      Sanity check.
498
743
                return 0;
499
744
        }
500
745
 
501
 
        subcomp = new_sublist(server, comp, dval->value);
 
746
        subcomp = new_sublist(components, comp, dval->value);
502
747
        if (!subcomp) {
503
748
                modcallable_free(&ml);
504
749
                return 1;
532
777
        } while (dict_valbyattr(dattr->attr, value));
533
778
 
534
779
        if (dict_addvalue(name, dattr->name, value) < 0) {
535
 
                radlog(L_ERR, "%s", librad_errstr);
 
780
                radlog(L_ERR, "%s", fr_strerror());
536
781
                return 0;
537
782
        }
538
783
 
540
785
}
541
786
 
542
787
static int load_component_section(CONF_SECTION *cs,
543
 
                                  const char *server, int comp)
 
788
                                  rbtree_t *components, int comp)
544
789
{
545
790
        modcallable *this;
546
791
        CONF_ITEM *modref;
580
825
                        if (strcmp(name1,
581
826
                                   section_type_value[comp].typename) == 0) {
582
827
                                if (!load_subcomponent_section(NULL, scs,
583
 
                                                               server,
 
828
                                                               components,
584
829
                                                               dattr->attr,
585
830
                                                               comp)) {
586
831
                                        return -1; /* FIXME: memleak? */
651
896
                        idx = 0;
652
897
                }
653
898
 
654
 
                subcomp = new_sublist(server, comp, idx);
 
899
                subcomp = new_sublist(components, comp, idx);
655
900
                if (subcomp == NULL) {
656
901
                        modcallable_free(&this);
657
902
                        continue;
672
917
static int load_byserver(CONF_SECTION *cs)
673
918
{
674
919
        int comp, flag;
675
 
        const char *server = cf_section_name2(cs);
 
920
        const char *name = cf_section_name2(cs);
 
921
        rbtree_t *components;
 
922
        virtual_server_t *server = NULL;
 
923
        indexed_modcallable *c;
 
924
 
 
925
        if (name) {
 
926
                cf_log_info(cs, "server %s {", name);
 
927
        } else {
 
928
                cf_log_info(cs, "server {");
 
929
        }
676
930
 
677
931
        cf_log_info(cs, " modules {");
678
932
 
 
933
        components = rbtree_create(indexed_modcallable_cmp,
 
934
                                   indexed_modcallable_free, 0);
 
935
        if (!components) {
 
936
                radlog(L_ERR, "Failed to initialize components\n");
 
937
                goto error;
 
938
        }
 
939
 
 
940
        server = rad_malloc(sizeof(*server));
 
941
        memset(server, 0, sizeof(*server));
 
942
 
 
943
        server->name = name;
 
944
        server->created = time(NULL);
 
945
        server->cs = cs;
 
946
        server->components = components;
 
947
 
679
948
        /*
680
949
         *      Define types first.
681
950
         */
698
967
                        cf_log_err(cf_sectiontoitem(subcs),
699
968
                                   "No such attribute %s",
700
969
                                   section_type_value[comp].typename);
701
 
                        cf_log_info(cs, " }");
 
970
                error:
 
971
                        if (debug_flag == 0) {
 
972
                                radlog(L_ERR, "Failed to load virtual server %s",
 
973
                                       (name != NULL) ? name : "<default>");
 
974
                        }
 
975
                        virtual_server_free(server);
702
976
                        return -1;
703
977
                }
704
978
 
721
995
                            cf_item_is_pair(modref)) {
722
996
                                CONF_PAIR *cp = cf_itemtopair(modref);
723
997
                                if (!define_type(dattr, cf_pair_attr(cp))) {
724
 
                                        return -1;
 
998
                                        goto error;
725
999
                                }
726
1000
 
727
1001
                                continue;
735
1009
                        if (strcmp(name1, section_type_value[comp].typename) == 0) {
736
1010
                                if (!define_type(dattr,
737
1011
                                                 cf_section_name2(subsubcs))) {
738
 
                                        cf_log_info(cs, " }");
739
 
                                        return -1;
 
1012
                                        goto error;
740
1013
                                }
741
1014
                        }
742
1015
                }
759
1032
                cf_log_module(cs, "Checking %s {...} for more modules to load",
760
1033
                       section_type_value[comp].section);
761
1034
 
762
 
                if (load_component_section(subcs, server, comp) < 0) {
763
 
                        cf_log_info(cs, " }");
764
 
                        return -1;
765
 
                }
 
1035
#ifdef WITH_PROXY
 
1036
                /*
 
1037
                 *      Skip pre/post-proxy sections if we're not
 
1038
                 *      proxying.
 
1039
                 */
 
1040
                if (!mainconfig.proxy_requests &&
 
1041
                    ((comp == PW_PRE_PROXY_TYPE) ||
 
1042
                     (comp == PW_PRE_PROXY_TYPE))) {
 
1043
                        continue;
 
1044
                }
 
1045
#endif
 
1046
 
 
1047
                if (load_component_section(subcs, components, comp) < 0) {
 
1048
                        goto error;
 
1049
                }
 
1050
 
 
1051
                /*
 
1052
                 *      Cache a default, if it exists.  Some people
 
1053
                 *      put empty sections for some reason...
 
1054
                 */
 
1055
                c = lookup_by_index(components, comp, 0);
 
1056
                if (c) server->mc[comp] = c->modulelist;
 
1057
 
 
1058
                server->subcs[comp] = subcs;
 
1059
 
766
1060
                flag = 1;
767
1061
        } /* loop over components */
768
1062
 
778
1072
                subcs = cf_section_sub_find(cs, "vmps");
779
1073
                if (subcs) {
780
1074
                        cf_log_module(cs, "Checking vmps {...} for more modules to load");              
781
 
                        if (load_component_section(subcs, server,
 
1075
                        if (load_component_section(subcs, components,
782
1076
                                                   RLM_COMPONENT_POST_AUTH) < 0) {
783
 
                                return -1;
 
1077
                                goto error;
784
1078
                        }
 
1079
                        c = lookup_by_index(components,
 
1080
                                            RLM_COMPONENT_POST_AUTH, 0);
 
1081
                        if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
785
1082
                        flag = 1;
786
1083
                }
787
1084
 
790
1087
                        const DICT_ATTR *dattr;
791
1088
 
792
1089
                        dattr = dict_attrbyname("DHCP-Message-Type");
793
 
                        if (!dattr) {
794
 
                                radlog(L_ERR, "No DHCP-Message-Type attribute");
795
 
                                return -1;
796
 
                        }
797
1090
 
798
1091
                        /*
799
1092
                         *      Handle each DHCP Message type separately.
800
1093
                         */
801
 
                        for (subcs = cf_subsection_find_next(cs, NULL,
802
 
                                                             "dhcp");
803
 
                             subcs != NULL;
804
 
                             subcs = cf_subsection_find_next(cs, subcs,
805
 
                                                             "dhcp")) {
 
1094
                        if (dattr) for (subcs = cf_subsection_find_next(cs, NULL, "dhcp");
 
1095
                                        subcs != NULL;
 
1096
                                        subcs = cf_subsection_find_next(cs, subcs,
 
1097
                                                                        "dhcp")) {
806
1098
                                const char *name2 = cf_section_name2(subcs);
807
1099
 
808
1100
                                DEBUG2(" Module: Checking dhcp %s {...} for more modules to load", name2);
809
1101
                                if (!load_subcomponent_section(NULL, subcs,
810
 
                                                               server,
 
1102
                                                               components,
811
1103
                                                               dattr->attr,
812
1104
                                                               RLM_COMPONENT_POST_AUTH)) {
813
 
                                        return -1; /* FIXME: memleak? */
 
1105
                                        goto error; /* FIXME: memleak? */
814
1106
                                }
 
1107
                                c = lookup_by_index(components,
 
1108
                                                    RLM_COMPONENT_POST_AUTH, 0);
 
1109
                                if (c) server->mc[RLM_COMPONENT_POST_AUTH] = c->modulelist;
815
1110
                                flag = 1;
816
1111
                        }
817
1112
                }
818
1113
#endif
819
1114
        }
820
1115
 
821
 
        cf_log_info(cs, " }");
 
1116
        cf_log_info(cs, " } # modules");
 
1117
        cf_log_info(cs, "} # server");
822
1118
 
823
 
        if (!flag && server) {
 
1119
        if (!flag && name) {
824
1120
                DEBUG("WARNING: Server %s is empty, and will do nothing!",
825
 
                      server);
826
 
        }
827
 
 
828
 
        return 0;
 
1121
                      name);
 
1122
        }
 
1123
 
 
1124
        if (debug_flag == 0) {
 
1125
                radlog(L_INFO, "Loaded virtual server %s",
 
1126
                       (name != NULL) ? name : "<default>");
 
1127
        }
 
1128
 
 
1129
        /*
 
1130
         *      Now that it is OK, insert it into the list.
 
1131
         *
 
1132
         *      This is thread-safe...
 
1133
         */
 
1134
        comp = virtual_server_idx(name);
 
1135
        server->next = virtual_servers[comp];
 
1136
        virtual_servers[comp] = server;
 
1137
 
 
1138
        /*
 
1139
         *      Mark OLDER ones of the same name as being unused.
 
1140
         */
 
1141
        server = server->next;
 
1142
        while (server) {
 
1143
                if ((!name && !server->name) ||
 
1144
                    (name && server->name &&
 
1145
                     (strcmp(server->name, name) == 0))) {
 
1146
                        server->can_free = TRUE;
 
1147
                        break;
 
1148
                }
 
1149
                server = server->next;
 
1150
        }
 
1151
 
 
1152
        return 0;
 
1153
}
 
1154
 
 
1155
 
 
1156
/*
 
1157
 *      Load all of the virtual servers.
 
1158
 */
 
1159
int virtual_servers_load(CONF_SECTION *config)
 
1160
{
 
1161
        int null_server = FALSE;
 
1162
        CONF_SECTION *cs;
 
1163
        static int first_time = TRUE;
 
1164
 
 
1165
        DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
 
1166
 
 
1167
        /*
 
1168
         *      Load all of the virtual servers.
 
1169
         */
 
1170
        for (cs = cf_subsection_find_next(config, NULL, "server");
 
1171
             cs != NULL;
 
1172
             cs = cf_subsection_find_next(config, cs, "server")) {
 
1173
                if (!cf_section_name2(cs)) null_server = TRUE;
 
1174
 
 
1175
                if (load_byserver(cs) < 0) {
 
1176
                        /*
 
1177
                         *      Once we successfully staryed once,
 
1178
                         *      continue loading the OTHER servers,
 
1179
                         *      even if one fails.
 
1180
                         */
 
1181
                        if (!first_time) continue;
 
1182
                        return -1;
 
1183
                }
 
1184
        }
 
1185
 
 
1186
        /*
 
1187
         *      No empty server defined.  Try to load an old-style
 
1188
         *      one for backwards compatibility.
 
1189
         */
 
1190
        if (!null_server) {
 
1191
                if (load_byserver(config) < 0) {
 
1192
                        return -1;
 
1193
                }
 
1194
        }
 
1195
 
 
1196
        /*
 
1197
         *      If we succeed the first time around, remember that.
 
1198
         */
 
1199
        first_time = FALSE;
 
1200
 
 
1201
        return 0;
 
1202
}
 
1203
 
 
1204
int module_hup_module(CONF_SECTION *cs, module_instance_t *node, time_t when)
 
1205
{
 
1206
        void *insthandle = NULL;
 
1207
        fr_module_hup_t *mh;
 
1208
 
 
1209
        if (!node ||
 
1210
            !node->entry->module->instantiate ||
 
1211
            ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
 
1212
                return 1;
 
1213
        }
 
1214
 
 
1215
        cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
 
1216
        
 
1217
        if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
 
1218
                cf_log_err(cf_sectiontoitem(cs),
 
1219
                           "HUP failed for module \"%s\".  Using old configuration.",
 
1220
                           node->name);
 
1221
                return 0;
 
1222
        }
 
1223
 
 
1224
        radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
 
1225
 
 
1226
        module_instance_free_old(cs, node, when);
 
1227
 
 
1228
        /*
 
1229
         *      Save the old instance handle for later deletion.
 
1230
         */
 
1231
        mh = rad_malloc(sizeof(*mh));
 
1232
        mh->mi = node;
 
1233
        mh->when = when;
 
1234
        mh->insthandle = node->insthandle;
 
1235
        mh->next = node->mh;
 
1236
        node->mh = mh;
 
1237
 
 
1238
        node->insthandle = insthandle;
 
1239
        
 
1240
        /*
 
1241
         *      FIXME: Set a timeout to come back in 60s, so that
 
1242
         *      we can pro-actively clean up the old instances.
 
1243
         */
 
1244
 
 
1245
        return 1;
829
1246
}
830
1247
 
831
1248
 
836
1253
        CONF_SECTION *cs;
837
1254
        module_instance_t *node;
838
1255
 
839
 
        static int insthandle_counter = -1;
840
 
        static time_t hup_times[16];
841
 
 
842
1256
        if (!modules) return 0;
843
1257
 
844
 
        if (insthandle_counter < 0) {
845
 
                memset(hup_times, 0, sizeof(hup_times));
846
 
                insthandle_counter = 0;
847
 
        }
848
 
 
849
1258
        when = time(NULL);
850
1259
 
851
1260
        /*
854
1263
        for (ci=cf_item_find_next(modules, NULL);
855
1264
             ci != NULL;
856
1265
             ci=cf_item_find_next(modules, ci)) {
857
 
                int i;
858
 
                void *insthandle = NULL;
859
1266
                const char *instname;
860
1267
                module_instance_t myNode;
861
1268
 
870
1277
 
871
1278
                strlcpy(myNode.name, instname, sizeof(myNode.name));
872
1279
                node = rbtree_finddata(instance_tree, &myNode);
873
 
                if (!node ||
874
 
                    !node->entry->module->instantiate ||
875
 
                    ((node->entry->module->type & RLM_TYPE_HUP_SAFE) == 0)) {
876
 
                        continue;
877
 
                }
878
 
 
879
 
                cf_log_module(cs, "Trying to reload module \"%s\"", node->name);
880
 
 
881
 
                if ((node->entry->module->instantiate)(cs, &insthandle) < 0) {
882
 
                        cf_log_err(cf_sectiontoitem(cs),
883
 
                                   "HUP failed for module \"%s\".  Using old configuration.",
884
 
                                   node->name);
885
 
                        continue;
886
 
                }
887
 
 
888
 
                radlog(L_INFO, " Module: Reloaded module \"%s\"", node->name);
889
 
 
890
 
                /*
891
 
                 *      Free all configurations old enough to be
892
 
                 *      deleted.  This is slightly wasteful, but easy
893
 
                 *      to do.
894
 
                 *
895
 
                 *      We permit HUPs every 5s, and we cache the last
896
 
                 *      16 configurations.  So the current entry at
897
 
                 *      insthandle_counter will either be empty, OR will
898
 
                 *      be at least (5*16) = 80s old.  The following check
899
 
                 *      ensures that it will be deleted.
900
 
                 */
901
 
                for (i = 0; i < 16; i++) {
902
 
                        if ((int) (when - hup_times[insthandle_counter]) < 60)
903
 
                                continue;
904
 
 
905
 
                        if (!node->old_insthandle[i]) continue;
906
 
 
907
 
                        cf_section_parse_free(cs, node->old_insthandle[i]);
908
 
                        
909
 
                        if (node->entry->module->detach) {
910
 
                                (node->entry->module->detach)(node->old_insthandle[i]);
911
 
                        }
912
 
                        node->old_insthandle[i] = NULL;
913
 
                }
914
 
 
915
 
                /*
916
 
                 *      Save the old instance handle for later deletion.
917
 
                 */
918
 
                node->old_insthandle[insthandle_counter] = node->insthandle;
919
 
                node->insthandle = insthandle;
920
 
 
921
 
                /*
922
 
                 *      FIXME: Set a timeout to come back in 60s, so that
923
 
                 *      we can pro-actively clean up the old instances.
924
 
                 */
 
1280
 
 
1281
                module_hup_module(cs, node, when);
925
1282
        }
926
1283
 
927
 
        /*
928
 
         *      Remember when we were last HUP'd.
929
 
         */
930
 
        hup_times[insthandle_counter] = when;
931
 
        insthandle_counter++;
932
 
        insthandle_counter &= 0x0f;
933
 
 
934
1284
        return 1;
935
1285
}
936
1286
 
946
1296
{
947
1297
        CONF_SECTION    *cs, *modules;
948
1298
        rad_listen_t    *listener;
949
 
        int             null_server = FALSE;
950
1299
 
951
1300
        if (reload) return 0;
952
1301
 
955
1304
         */
956
1305
        if (!reload) {
957
1306
                /*
 
1307
                 *      This line works around a completely
 
1308
                 *
 
1309
                 *              RIDICULOUS INSANE IDIOTIC
 
1310
                 *
 
1311
                 *      bug in libltdl on certain systems.  The "set
 
1312
                 *      preloaded symbols" macro below ends up
 
1313
                 *      referencing this name, but it isn't defined
 
1314
                 *      anywhere in the libltdl source.  As a result,
 
1315
                 *      any program STUPID enough to rely on libltdl
 
1316
                 *      fails to link, because the symbol isn't
 
1317
                 *      defined anywhere.
 
1318
                 *
 
1319
                 *      It's like libtool and libltdl are some kind
 
1320
                 *      of sick joke.
 
1321
                 */
 
1322
#ifdef IE_LIBTOOL_DIE
 
1323
#define lt__PROGRAM__LTX_preloaded_symbols lt_libltdl_LTX_preloaded_symbols
 
1324
#endif
 
1325
 
 
1326
                /*
958
1327
                 *      Set the default list of preloaded symbols.
959
1328
                 *      This is used to initialize libltdl's list of
960
1329
                 *      preloaded modules.
994
1363
                }
995
1364
        }
996
1365
 
997
 
        components = rbtree_create(indexed_modcallable_cmp,
998
 
                                   indexed_modcallable_free, 0);
999
 
        if (!components) {
1000
 
                radlog(L_ERR, "Failed to initialize components\n");
1001
 
                return -1;
1002
 
        }
 
1366
        memset(virtual_servers, 0, sizeof(virtual_servers));
1003
1367
 
1004
1368
        /*
1005
1369
         *      Remember where the modules were stored.
1045
1409
 
1046
1410
                        cp = cf_itemtopair(ci);
1047
1411
                        name = cf_pair_attr(cp);
1048
 
                        module = find_module_instance(modules, name);
 
1412
                        module = find_module_instance(modules, name, 1);
1049
1413
                        if (!module) {
1050
1414
                                return -1;
1051
1415
                        }
1075
1439
                }
1076
1440
        }
1077
1441
 
1078
 
        DEBUG2("%s: #### Loading Virtual Servers ####", mainconfig.name);
1079
 
 
1080
 
        /*
1081
 
         *      Load all of the virtual servers.
1082
 
         */
1083
 
        for (cs = cf_subsection_find_next(config, NULL, "server");
1084
 
             cs != NULL;
1085
 
             cs = cf_subsection_find_next(config, cs, "server")) {
1086
 
                const char *name2 = cf_section_name2(cs);
1087
 
 
1088
 
                if (name2) {
1089
 
                        cf_log_info(cs, "server %s {", name2);
1090
 
                } else {
1091
 
                        cf_log_info(cs, "server {");
1092
 
                        null_server = TRUE;
1093
 
                }
1094
 
                if (load_byserver(cs) < 0) {
1095
 
                        cf_log_info(cs, "}");
1096
 
                        return -1;
1097
 
                }
1098
 
                cf_log_info(cs, "}");
1099
 
        }
1100
 
 
1101
 
        /*
1102
 
         *      No empty server defined.  Try to load an old-style
1103
 
         *      one for backwards compatibility.
1104
 
         */
1105
 
        if (!null_server) {
1106
 
                cf_log_info(cs, "server {");
1107
 
                if (load_byserver(config) < 0) {
1108
 
                        cf_log_info(cs, "}");
1109
 
                        return -1;
1110
 
                }
1111
 
                cf_log_info(cs, "}");
1112
 
        }
 
1442
        if (virtual_servers_load(config) < 0) return -1;
1113
1443
 
1114
1444
        return 0;
1115
1445
}
1131
1461
        return indexed_modcall(RLM_COMPONENT_AUTH, auth_type, request);
1132
1462
}
1133
1463
 
 
1464
#ifdef WITH_ACCOUNTING
1134
1465
/*
1135
1466
 *      Do pre-accounting for ALL configured sessions
1136
1467
 */
1146
1477
{
1147
1478
        return indexed_modcall(RLM_COMPONENT_ACCT, acct_type, request);
1148
1479
}
 
1480
#endif
1149
1481
 
 
1482
#ifdef WITH_SESSION_MGMT
1150
1483
/*
1151
1484
 *      See if a user is already logged in.
1152
1485
 *
1172
1505
 
1173
1506
        return (request->simul_count < maxsimul) ? 0 : request->simul_mpp;
1174
1507
}
 
1508
#endif
1175
1509
 
 
1510
#ifdef WITH_PROXY
1176
1511
/*
1177
1512
 *      Do pre-proxying for ALL configured sessions
1178
1513
 */
1188
1523
{
1189
1524
        return indexed_modcall(RLM_COMPONENT_POST_PROXY, type, request);
1190
1525
}
 
1526
#endif
1191
1527
 
1192
1528
/*
1193
1529
 *      Do post-authentication for ALL configured sessions
1196
1532
{
1197
1533
        return indexed_modcall(RLM_COMPONENT_POST_AUTH, postauth_type, request);
1198
1534
}
 
1535
 
 
1536
#ifdef WITH_COA
 
1537
int module_recv_coa(int recv_coa_type, REQUEST *request)
 
1538
{
 
1539
        return indexed_modcall(RLM_COMPONENT_RECV_COA, recv_coa_type, request);
 
1540
}
 
1541
 
 
1542
int module_send_coa(int send_coa_type, REQUEST *request)
 
1543
{
 
1544
        return indexed_modcall(RLM_COMPONENT_SEND_COA, send_coa_type, request);
 
1545
}
 
1546
#endif