/
modules.c
1871 lines (1571 loc) · 41.8 KB
/
modules.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* modules.c Radius module support.
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2003,2006 The FreeRADIUS server project
* Copyright 2000 Alan DeKok <aland@ox.org>
* Copyright 2000 Alan Curry <pacman@world.std.com>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modpriv.h>
#include <freeradius-devel/modcall.h>
#include <freeradius-devel/parser.h>
#include <freeradius-devel/rad_assert.h>
extern bool check_config;
typedef struct indexed_modcallable {
rlm_components_t comp;
int idx;
modcallable *modulelist;
} indexed_modcallable;
typedef struct virtual_server_t {
char const *name;
time_t created;
int can_free;
CONF_SECTION *cs;
rbtree_t *components;
modcallable *mc[RLM_COMPONENT_COUNT];
CONF_SECTION *subcs[RLM_COMPONENT_COUNT];
struct virtual_server_t *next;
} virtual_server_t;
/*
* Keep a hash of virtual servers, so that we can reload them.
*/
#define VIRTUAL_SERVER_HASH_SIZE (256)
static virtual_server_t *virtual_servers[VIRTUAL_SERVER_HASH_SIZE];
static rbtree_t *module_tree = NULL;
static rbtree_t *instance_tree = NULL;
struct fr_module_hup_t {
module_instance_t *mi;
time_t when;
void *insthandle;
fr_module_hup_t *next;
};
/*
* Ordered by component
*/
const section_type_value_t section_type_value[RLM_COMPONENT_COUNT] = {
{ "authenticate", "Auth-Type", PW_AUTH_TYPE },
{ "authorize", "Autz-Type", PW_AUTZ_TYPE },
{ "preacct", "Pre-Acct-Type", PW_PRE_ACCT_TYPE },
{ "accounting", "Acct-Type", PW_ACCT_TYPE },
{ "session", "Session-Type", PW_SESSION_TYPE },
{ "pre-proxy", "Pre-Proxy-Type", PW_PRE_PROXY_TYPE },
{ "post-proxy", "Post-Proxy-Type", PW_POST_PROXY_TYPE },
{ "post-auth", "Post-Auth-Type", PW_POST_AUTH_TYPE }
#ifdef WITH_COA
,
{ "recv-coa", "Recv-CoA-Type", PW_RECV_COA_TYPE },
{ "send-coa", "Send-CoA-Type", PW_SEND_COA_TYPE }
#endif
};
#ifndef RTLD_NOW
#define RTLD_NOW (0)
#endif
#ifndef RTLD_LOCAL
#define RTLD_LOCAL (0)
#endif
#ifdef __APPLE__
#define LT_SHREXT ".dylib"
#define LD_LIBRARY_PATH "DYLD_FALLBACK_LIBRARY_PATH"
#elif defined (WIN32)
#define LT_SHREXT ".dll"
#else
#define LT_SHREXT ".so"
#define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
#endif
/** Check if the magic number in the module matches the one in the library
*
* This is used to detect potential ABI issues caused by running with modules which
* were built for a different version of the server.
*
* @param cs being parsed.
* @param module being loaded.
* @returns 0 on success, -1 if prefix mismatch, -2 if version mismatch, -3 if commit mismatch.
*/
static int check_module_magic(CONF_SECTION *cs, module_t const *module)
{
if (MAGIC_PREFIX(module->magic) != MAGIC_PREFIX(RADIUSD_MAGIC_NUMBER)) {
cf_log_err_cs(cs, "Application and rlm_%s magic number (prefix) mismatch."
" application: %x module: %x", module->name,
MAGIC_PREFIX(RADIUSD_MAGIC_NUMBER),
MAGIC_PREFIX(module->magic));
return -1;
}
if (MAGIC_VERSION(module->magic) != MAGIC_VERSION(RADIUSD_MAGIC_NUMBER)) {
cf_log_err_cs(cs, "Application and rlm_%s magic number (version) mismatch."
" application: %lx module: %lx", module->name,
(unsigned long) MAGIC_VERSION(RADIUSD_MAGIC_NUMBER),
(unsigned long) MAGIC_VERSION(module->magic));
return -2;
}
if (MAGIC_COMMIT(module->magic) != MAGIC_COMMIT(RADIUSD_MAGIC_NUMBER)) {
cf_log_err_cs(cs, "Application and rlm_%s magic number (commit) mismatch."
" application: %lx module: %lx", module->name,
(unsigned long) MAGIC_COMMIT(RADIUSD_MAGIC_NUMBER),
(unsigned long) MAGIC_COMMIT(module->magic));
return -3;
}
return 0;
}
/*
* Because dlopen produces really shitty and inaccurate error messages
*/
static void check_lib_access(char const *name)
{
if (access(name, R_OK) < 0) switch (errno) {
case EACCES:
WDEBUG("Library \"%s\" exists, but we don't have permission to read", name);
break;
case ENOENT:
DEBUG4("Library not found at path \"%s\"", name);
break;
default:
DEBUG4("Possible issue accessing Library \"%s\": %s", name, fr_syserror(errno));
break;
}
}
lt_dlhandle lt_dlopenext(char const *name)
{
int flags = RTLD_NOW;
void *handle;
char buffer[2048];
char *env;
#ifdef RTLD_GLOBAL
if (strcmp(name, "rlm_perl") == 0) {
flags |= RTLD_GLOBAL;
} else
#endif
flags |= RTLD_LOCAL;
#ifndef NDEBUG
/*
* Bind all the symbols *NOW* so we don't hit errors later
*/
flags |= RTLD_NOW;
#endif
/*
* Prefer loading our libraries by absolute path.
*/
snprintf(buffer, sizeof(buffer), "%s/%s%s", radlib_dir, name, LT_SHREXT);
DEBUG4("Loading library using absolute path");
handle = dlopen(buffer, flags);
if (handle) {
return handle;
}
check_lib_access(buffer);
DEBUG4("Falling back to linker search path(s)");
if (DEBUG_ENABLED4) {
#ifdef __APPLE__
env = getenv("LD_LIBRARY_PATH");
if (env) {
DEBUG4("LD_LIBRARY_PATH : %s", env);
}
env = getenv("DYLD_LIBRARY_PATH");
if (env) {
DEBUG4("DYLB_LIBRARY_PATH : %s", env);
}
env = getenv("DYLD_FALLBACK_LIBRARY_PATH");
if (env) {
DEBUG4("DYLD_FALLBACK_LIBRARY_PATH : %s", env);
}
env = getcwd(buffer, sizeof(buffer));
if (env) {
DEBUG4("Current directory : %s", env);
}
#else
env = getenv("LD_LIBRARY_PATH");
if (env) {
DEBUG4("LD_LIBRARY_PATH : %s", env);
}
DEBUG4("Defaults : /lib:/usr/lib");
#endif
}
strlcpy(buffer, name, sizeof(buffer));
/*
* FIXME: Make this configurable...
*/
strlcat(buffer, LT_SHREXT, sizeof(buffer));
return dlopen(buffer, flags);
}
void *lt_dlsym(lt_dlhandle handle, UNUSED char const *symbol)
{
return dlsym(handle, symbol);
}
int lt_dlclose(lt_dlhandle handle)
{
if (!handle) return 0;
return dlclose(handle);
}
char const *lt_dlerror(void)
{
return dlerror();
}
static int virtual_server_idx(char const *name)
{
uint32_t hash;
if (!name) return 0;
hash = fr_hash_string(name);
return hash & (VIRTUAL_SERVER_HASH_SIZE - 1);
}
static virtual_server_t *virtual_server_find(char const *name)
{
rlm_rcode_t rcode;
virtual_server_t *server;
rcode = virtual_server_idx(name);
for (server = virtual_servers[rcode];
server != NULL;
server = server->next) {
if (!name && !server->name) break;
if ((name && server->name) &&
(strcmp(name, server->name) == 0)) break;
}
return server;
}
static int _virtual_server_free(virtual_server_t *server)
{
server = talloc_get_type_abort(server, virtual_server_t);
if (server->components) rbtree_free(server->components);
return 0;
}
void virtual_servers_free(time_t when)
{
int i;
virtual_server_t **last;
for (i = 0; i < VIRTUAL_SERVER_HASH_SIZE; i++) {
virtual_server_t *server, *next;
last = &virtual_servers[i];
for (server = virtual_servers[i];
server != NULL;
server = next) {
next = server->next;
/*
* If we delete it, fix the links so that
* we don't orphan anything. Also,
* delete it if it's old, AND a newer one
* was defined.
*
* Otherwise, the last pointer gets set to
* the one we didn't delete.
*/
if ((when == 0) ||
((server->created < when) && server->can_free)) {
*last = server->next;
talloc_free(server);
} else {
last = &(server->next);
}
}
}
}
static int indexed_modcallable_cmp(void const *one, void const *two)
{
indexed_modcallable const *a = one;
indexed_modcallable const *b = two;
if (a->comp < b->comp) return -1;
if (a->comp > b->comp) return +1;
return a->idx - b->idx;
}
/*
* Compare two module entries
*/
static int module_instance_cmp(void const *one, void const *two)
{
module_instance_t const *a = one;
module_instance_t const *b = two;
return strcmp(a->name, b->name);
}
static void module_instance_free_old(UNUSED CONF_SECTION *cs, module_instance_t *node,
time_t when)
{
fr_module_hup_t *mh, **last;
/*
* Walk the list, freeing up old instances.
*/
last = &(node->mh);
while (*last) {
mh = *last;
/*
* Free only every 60 seconds.
*/
if ((when - mh->when) < 60) {
last = &(mh->next);
continue;
}
talloc_free(mh->insthandle);
*last = mh->next;
talloc_free(mh);
}
}
/*
* Free a module instance.
*/
static void module_instance_free(void *data)
{
module_instance_t *this = data;
module_instance_free_old(this->cs, this, time(NULL) + 100);
#ifdef HAVE_PTHREAD_H
if (this->mutex) {
/*
* FIXME
* The mutex MIGHT be locked...
* we'll check for that later, I guess.
*/
pthread_mutex_destroy(this->mutex);
talloc_free(this->mutex);
}
#endif
/*
* Remove any registered paircompares.
*/
paircompare_unregister_instance(this->insthandle);
xlat_unregister(this->name, NULL, this->insthandle);
#ifndef NDEBUG
memset(this, 0, sizeof(*this));
#endif
talloc_free(this);
}
/*
* Compare two module entries
*/
static int module_entry_cmp(void const *one, void const *two)
{
module_entry_t const *a = one;
module_entry_t const *b = two;
return strcmp(a->name, b->name);
}
/*
* Free a module entry.
*/
static int _module_entry_free(module_entry_t *this)
{
this = talloc_get_type_abort(this, module_entry_t);
#ifndef NDEBUG
/*
* Don't dlclose() modules if we're doing memory
* debugging. This removes the symbols needed by
* valgrind.
*/
if (!mainconfig.debug_memory)
#endif
dlclose(this->handle); /* ignore any errors */
return 0;
}
/*
* Remove the module lists.
*/
int modules_free(void)
{
rbtree_free(instance_tree);
rbtree_free(module_tree);
return 0;
}
/*
* Find a module on disk or in memory, and link to it.
*/
static module_entry_t *linkto_module(char const *module_name,
CONF_SECTION *cs)
{
module_entry_t myentry;
module_entry_t *node;
void *handle = NULL;
module_t const *module;
strlcpy(myentry.name, module_name, sizeof(myentry.name));
node = rbtree_finddata(module_tree, &myentry);
if (node) return node;
/*
* Link to the module's rlm_FOO{} structure, the same as
* the module name.
*/
#if !defined(WITH_LIBLTDL) && defined(HAVE_DLFCN_H) && defined(RTLD_SELF)
module = dlsym(RTLD_SELF, module_name);
if (module) goto open_self;
#endif
/*
* Keep the handle around so we can dlclose() it.
*/
handle = lt_dlopenext(module_name);
if (!handle) {
cf_log_err_cs(cs,
"Failed to link to module '%s': %s\n",
module_name, dlerror());
return NULL;
}
DEBUG3(" (Loaded %s, checking if it's valid)", module_name);
/*
* libltld MAY core here, if the handle it gives us contains
* garbage data.
*/
module = dlsym(handle, module_name);
if (!module) {
cf_log_err_cs(cs,
"Failed linking to %s structure: %s\n",
module_name, dlerror());
dlclose(handle);
return NULL;
}
#if !defined(WITH_LIBLTDL) && defined (HAVE_DLFCN_H) && defined(RTLD_SELF)
open_self:
#endif
/*
* Before doing anything else, check if it's sane.
*/
if (check_module_magic(cs, module) < 0) {
dlclose(handle);
return NULL;
}
/* make room for the module type */
node = talloc_zero(cs, module_entry_t);
talloc_set_destructor(node, _module_entry_free);
strlcpy(node->name, module_name, sizeof(node->name));
node->module = module;
node->handle = handle;
cf_log_module(cs, "Loaded module %s", module_name);
/*
* Add the module as "rlm_foo-version" to the configuration
* section.
*/
if (!rbtree_insert(module_tree, node)) {
ERROR("Failed to cache module %s", module_name);
dlclose(handle);
talloc_free(node);
return NULL;
}
return node;
}
/** Parse module's configuration section and setup destructors
*
*/
static int module_conf_parse(module_instance_t *node, void **handle)
{
*handle = NULL;
/*
* If there is supposed to be instance data, allocate it now.
* Also parse the configuration data, if required.
*/
if (node->entry->module->inst_size) {
/* FIXME: make this rlm_config_t ?? */
*handle = talloc_zero_array(node, uint8_t, node->entry->module->inst_size);
rad_assert(handle);
/*
* So we can see where this configuration is from
* FIXME: set it to rlm_NAME_t, or some such thing
*/
talloc_set_name(*handle, "rlm_config_t");
if (node->entry->module->config &&
(cf_section_parse(node->cs, *handle, node->entry->module->config) < 0)) {
cf_log_err_cs(node->cs,"Invalid configuration for module \"%s\"", node->name);
talloc_free(*handle);
return -1;
}
/*
* Set the destructor.
*/
if (node->entry->module->detach) {
talloc_set_destructor((void *) *handle, node->entry->module->detach);
}
}
return 0;
}
/*
* Find a module instance.
*/
module_instance_t *find_module_instance(CONF_SECTION *modules,
char const *askedname, int do_link)
{
bool check_config_safe = false;
CONF_SECTION *cs;
char const *name1, *instname;
module_instance_t *node, myNode;
char module_name[256];
if (!modules) return NULL;
/*
* Look for the real name. Ignore the first character,
* which tells the server "it's OK for this module to not
* exist."
*/
instname = askedname;
if (instname[0] == '-') {
instname++;
}
/*
* Module instances are declared in the modules{} block
* and referenced later by their name, which is the
* name2 from the config section, or name1 if there was
* no name2.
*/
cs = cf_section_sub_find_name2(modules, NULL, instname);
if (!cs) {
ERROR("Cannot find a configuration entry for module \"%s\"", instname);
return NULL;
}
/*
* If there's already a module instance, return it.
*/
strlcpy(myNode.name, instname, sizeof(myNode.name));
node = rbtree_finddata(instance_tree, &myNode);
if (node) {
return node;
}
if (!do_link) {
return NULL;
}
name1 = cf_section_name1(cs);
/*
* Found the configuration entry, hang the node struct off of the
* configuration section. If the CS is free'd the instance will
* be too.
*/
node = talloc_zero(cs, module_instance_t);
node->cs = cs;
/*
* Names in the "modules" section aren't prefixed
* with "rlm_", so we add it here.
*/
snprintf(module_name, sizeof(module_name), "rlm_%s", name1);
/*
* Pull in the module object
*/
node->entry = linkto_module(module_name, cs);
if (!node->entry) {
talloc_free(node);
/* linkto_module logs any errors */
return NULL;
}
if (check_config && (node->entry->module->instantiate) &&
(node->entry->module->type & RLM_TYPE_CHECK_CONFIG_UNSAFE) != 0) {
char const *value = NULL;
CONF_PAIR *cp;
cp = cf_pair_find(cs, "force_check_config");
if (cp) {
value = cf_pair_value(cp);
}
if (value && (strcmp(value, "yes") == 0)) goto print_inst;
cf_log_module(cs, "Skipping instantiation of %s", instname);
} else {
print_inst:
check_config_safe = true;
cf_log_module(cs, "Instantiating module \"%s\" from file %s", instname,
cf_section_filename(cs));
}
strlcpy(node->name, instname, sizeof(node->name));
/*
* Parse the module configuration, and setup destructors so the
* module's detach method is called when it's instance data is
* about to be freed.
*/
if (module_conf_parse(node, &node->insthandle) < 0) {
talloc_free(node);
return NULL;
}
/*
* Call the module's instantiation routine.
*/
if ((node->entry->module->instantiate) &&
(!check_config || check_config_safe) &&
((node->entry->module->instantiate)(cs, node->insthandle) < 0)) {
cf_log_err_cs(cs, "Instantiation failed for module \"%s\"", node->name);
talloc_free(node);
return NULL;
}
#ifdef HAVE_PTHREAD_H
/*
* If we're threaded, check if the module is thread-safe.
*
* If it isn't, we create a mutex.
*/
if ((node->entry->module->type & RLM_TYPE_THREAD_UNSAFE) != 0) {
node->mutex = talloc_zero(node, pthread_mutex_t);
/*
* Initialize the mutex.
*/
pthread_mutex_init(node->mutex, NULL);
} else {
/*
* The module is thread-safe. Don't give it a mutex.
*/
node->mutex = NULL;
}
#endif
rbtree_insert(instance_tree, node);
return node;
}
static indexed_modcallable *lookup_by_index(rbtree_t *components,
rlm_components_t comp, int idx)
{
indexed_modcallable myc;
myc.comp = comp;
myc.idx = idx;
return rbtree_finddata(components, &myc);
}
/*
* Create a new sublist.
*/
static indexed_modcallable *new_sublist(CONF_SECTION *cs,
rbtree_t *components, rlm_components_t comp, int idx)
{
indexed_modcallable *c;
c = lookup_by_index(components, comp, idx);
/* It is an error to try to create a sublist that already
* exists. It would almost certainly be caused by accidental
* duplication in the config file.
*
* index 0 is the exception, because it is used when we want
* to collect _all_ listed modules under a single index by
* default, which is currently the case in all components
* except authenticate. */
if (c) {
if (idx == 0) {
return c;
}
return NULL;
}
c = talloc_zero(cs, indexed_modcallable);
c->modulelist = NULL;
c->comp = comp;
c->idx = idx;
if (!rbtree_insert(components, c)) {
talloc_free(c);
return NULL;
}
return c;
}
rlm_rcode_t indexed_modcall(rlm_components_t comp, int idx, REQUEST *request)
{
rlm_rcode_t rcode;
modcallable *list = NULL;
virtual_server_t *server;
/*
* Hack to find the correct virtual server.
*/
server = virtual_server_find(request->server);
if (!server) {
RDEBUG("No such virtual server \"%s\"", request->server);
return RLM_MODULE_FAIL;
}
if (idx == 0) {
list = server->mc[comp];
if (!list) RDEBUG3("Empty %s section. Using default return values.", section_type_value[comp].section);
} else {
indexed_modcallable *this;
this = lookup_by_index(server->components, comp, idx);
if (this) {
list = this->modulelist;
} else {
RWDEBUG2("Unknown value specified for %s. Cannot perform requested action.",
section_type_value[comp].typename);
}
}
if (server->subcs[comp]) {
if (idx == 0) {
RDEBUG("# Executing section %s from file %s",
section_type_value[comp].section,
cf_section_filename(server->subcs[comp]));
} else {
RDEBUG("# Executing group from file %s",
cf_section_filename(server->subcs[comp]));
}
}
request->component = section_type_value[comp].section;
rcode = modcall(comp, list, request);
request->module = "";
request->component = "<core>";
return rcode;
}
/*
* Load a sub-module list, as found inside an Auth-Type foo {}
* block
*/
static int load_subcomponent_section(modcallable *parent, CONF_SECTION *cs,
rbtree_t *components,
DICT_ATTR const *da, rlm_components_t comp)
{
indexed_modcallable *subcomp;
modcallable *ml;
DICT_VALUE *dval;
char const *name2 = cf_section_name2(cs);
/*
* Sanity check.
*/
if (!name2) {
return 1;
}
/*
* Compile the group.
*/
ml = compile_modgroup(parent, comp, cs);
if (!ml) {
return 0;
}
/*
* We must assign a numeric index to this subcomponent.
* It is generated and placed in the dictionary
* automatically. If it isn't found, it's a serious
* error.
*/
dval = dict_valbyname(da->attr, da->vendor, name2);
if (!dval) {
cf_log_err_cs(cs, "%s %s Not previously configured", section_type_value[comp].typename, name2);
talloc_free(ml);
return 0;
}
subcomp = new_sublist(cs, components, comp, dval->value);
if (!subcomp) {
talloc_free(ml);
return 1;
}
subcomp->modulelist = talloc_steal(subcomp, ml);
return 1; /* OK */
}
static int define_type(CONF_SECTION *cs, DICT_ATTR const *da, char const *name)
{
uint32_t value;
DICT_VALUE *dval;
/*
* If the value already exists, don't
* create it again.
*/
dval = dict_valbyname(da->attr, da->vendor, name);
if (dval) return 1;
/*
* Create a new unique value with a
* meaningless number. You can't look at
* it from outside of this code, so it
* doesn't matter. The only requirement
* is that it's unique.
*/
do {
value = fr_rand() & 0x00ffffff;
} while (dict_valbyattr(da->attr, da->vendor, value));
cf_log_module(cs, "Creating %s = %s", da->name, name);
if (dict_addvalue(name, da->name, value) < 0) {
ERROR("%s", fr_strerror());
return 0;
}
return 1;
}
/*
* Don't complain too often.
*/
#define MAX_IGNORED (32)
static int last_ignored = -1;
static char const *ignored[MAX_IGNORED];
static int load_component_section(CONF_SECTION *cs,
rbtree_t *components, rlm_components_t comp)
{
modcallable *this;
CONF_ITEM *modref;
int idx;
indexed_modcallable *subcomp;
char const *modname;
DICT_ATTR const *da;
/*
* Find the attribute used to store VALUEs for this section.
*/
da = dict_attrbyvalue(section_type_value[comp].attr, 0);
if (!da) {
cf_log_err_cs(cs,
"No such attribute %s",
section_type_value[comp].typename);
return -1;
}
/*
* Loop over the entries in the named section, loading
* the sections this time.
*/
for (modref = cf_item_find_next(cs, NULL);
modref != NULL;
modref = cf_item_find_next(cs, modref)) {
char const *name1;
CONF_PAIR *cp = NULL;
CONF_SECTION *scs = NULL;
if (cf_item_is_section(modref)) {
scs = cf_itemtosection(modref);
name1 = cf_section_name1(scs);
if (strcmp(name1,
section_type_value[comp].typename) == 0) {
if (!load_subcomponent_section(NULL, scs,
components,
da,
comp)) {
return -1; /* FIXME: memleak? */
}
continue;
}
cp = NULL;
} else if (cf_item_is_pair(modref)) {
cp = cf_itemtopair(modref);
} else {
continue; /* ignore it */
}
/*
* Look for Auth-Type foo {}, which are special
* cases of named sections, and allowable ONLY
* at the top-level.
*
* i.e. They're not allowed in a "group" or "redundant"
* subsection.
*/
if (comp == RLM_COMPONENT_AUTH) {
DICT_VALUE *dval;
char const *modrefname = NULL;
if (cp) {
modrefname = cf_pair_attr(cp);
} else {
modrefname = cf_section_name2(scs);
if (!modrefname) {
cf_log_err_cs(cs,
"Errors parsing %s sub-section.\n",
cf_section_name1(scs));
return -1;
}
}
dval = dict_valbyname(PW_AUTH_TYPE, 0, modrefname);
if (!dval) {
/*
* It's a section, but nothing we
* recognize. Die!
*/
cf_log_err_cs(cs,
"Unknown Auth-Type \"%s\" in %s sub-section.",
modrefname, section_type_value[comp].section);