/
module.c
1801 lines (1498 loc) · 44.6 KB
/
module.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
/*
* 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
*/
/**
* $Id$
*
* @file src/lib/server/module.c
* @brief Defines functions for module (re-)initialisation.
*
* @copyright 2003,2006,2016 The FreeRADIUS server project
* @copyright 2016 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
* @copyright 2000 Alan DeKok <aland@freeradius.org>
* @copyright 2000 Alan Curry <pacman@world.std.com>
*/
RCSID("$Id$")
#include <freeradius-devel/server/base.h>
#include <freeradius-devel/server/modpriv.h>
#include <freeradius-devel/server/parser.h>
#include <freeradius-devel/unlang/base.h>
#include <freeradius-devel/server/radmin.h>
#include <freeradius-devel/server/cf_file.h>
static TALLOC_CTX *instance_ctx = NULL;
static size_t instance_num = 0;
/*
* For simplicity, this is just array[instance_num]. Once we
* finish with modules_bootstrap(), the "instance_num" above MUST
* NOT change.
*/
static _Thread_local module_thread_instance_t **module_thread_inst_array;
/** Lookup module instances by name and lineage
*/
static rbtree_t *module_instance_name_tree;
/** Lookup module by instance data
*/
static rbtree_t *module_instance_data_tree;
/** Module command table
*/
static fr_cmd_table_t cmd_module_table[];
static int _module_instantiate(void *instance, UNUSED void *ctx);
/*
* Ordered by component
*/
const char *section_type_value[MOD_COUNT] = {
"authenticate",
"authorize",
"preacct",
"accounting",
"pre-proxy",
"post-proxy",
"post-auth"
#ifdef WITH_COA
,
"recv-coa",
"send-coa"
#endif
};
static int cmd_show_module_config(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
{
module_instance_t *mi = ctx;
rad_assert(mi->dl_inst->conf != NULL);
(void) cf_section_write(fp, mi->dl_inst->conf, 0);
return 0;
}
typedef struct {
char const *text;
int count;
int max_expansions;
char const **expansions;
} module_tab_expand_t;
static int _module_tab_expand(void *instance, void *ctx)
{
module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t);
module_tab_expand_t *mt = ctx;
if (mt->count >= mt->max_expansions) return 1;
if (fr_command_strncmp(mt->text, mi->name)) {
mt->expansions[mt->count] = strdup(mi->name);
mt->count++;
}
return 0;
}
static int module_name_tab_expand(UNUSED TALLOC_CTX *talloc_ctx, UNUSED void *uctx, fr_cmd_info_t *info, int max_expansions, char const **expansions)
{
module_tab_expand_t mt;
if (info->argc <= 0) return 0;
mt.text = info->argv[info->argc - 1];
mt.count = 0;
mt.max_expansions = max_expansions;
mt.expansions = expansions;
(void) rbtree_walk(module_instance_name_tree, RBTREE_IN_ORDER, _module_tab_expand, &mt);
return mt.count;
}
static int _module_list(void *instance, void *uctx)
{
module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t);
FILE *fp = uctx;
fprintf(fp, "\t%s\n", mi->name);
return 0;
}
static int cmd_show_module_list(FILE *fp, UNUSED FILE *fp_err, UNUSED void *uctx, UNUSED fr_cmd_info_t const *info)
{
(void) rbtree_walk(module_instance_name_tree, RBTREE_IN_ORDER, _module_list, fp);
return 0;
}
static int cmd_show_module_status(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info)
{
module_instance_t *mi = ctx;
if (!mi->force) {
fprintf(fp, "alive\n");
return 0;
}
fprintf(fp, "%s\n", fr_int2str(rcode_table, mi->code, "<invalid>"));
return 0;
}
static int cmd_set_module_status(UNUSED FILE *fp, UNUSED FILE *fp_err, void *ctx, fr_cmd_info_t const *info)
{
module_instance_t *mi = ctx;
rlm_rcode_t rcode;
if (strcmp(info->argv[1], "alive") == 0) {
mi->force = false;
return 0;
}
rcode = fr_str2int(rcode_table, info->argv[1], RLM_MODULE_UNKNOWN);
rad_assert(rcode != RLM_MODULE_UNKNOWN);
mi->code = rcode;
mi->force = true;
return 0;
}
static fr_cmd_table_t cmd_module_table[] = {
{
.parent = "show module",
.add_name = true,
.name = "status",
.func = cmd_show_module_status,
.help = "Show the status of a particular module.",
.read_only = true,
},
{
.parent = "show module",
.add_name = true,
.name = "config",
.func = cmd_show_module_config,
.help = "Show configuration for a module",
// @todo - do tab expand, by walking over the whole module list...
.read_only = true,
},
{
.parent = "set module",
.add_name = true,
.name = "status",
.syntax = "(alive|ok|fail|reject|handled|invalid|userlock|notfound|noop|updated)",
.func = cmd_set_module_status,
.help = "Change module status to fixed value.",
.read_only = false,
},
CMD_TABLE_END
};
static fr_cmd_table_t cmd_table[] = {
{
.parent = "show",
.name = "module",
.help = "Show information about modules.",
.tab_expand = module_name_tab_expand,
.read_only = true,
},
// @todo - what if there's a module called "list" ?
{
.parent = "show module",
.name = "list",
.func = cmd_show_module_list,
.help = "Show the list of modules loaded in the server.",
.read_only = true,
},
{
.parent = "set",
.name = "module",
.help = "Change module settings.",
.tab_expand = module_name_tab_expand,
.read_only = false,
},
CMD_TABLE_END
};
/** Compare module instances by parent and name
*
* The reason why we need parent, is because we could have submodules with names
* that conflict with their parent.
*/
static int module_instance_name_cmp(void const *one, void const *two)
{
module_instance_t const *a = one;
module_instance_t const *b = two;
dl_module_inst_t const *dl_inst;
int a_depth = 0, b_depth = 0;
int ret;
/*
* Sort by depth, so for tree walking we start
* at the shallowest node, and finish with
* the deepest child.
*/
for (dl_inst = a->dl_inst; dl_inst; dl_inst = dl_inst->parent) a_depth++;
for (dl_inst = b->dl_inst; dl_inst; dl_inst = dl_inst->parent) b_depth++;
ret = (a_depth > b_depth) - (a_depth < b_depth);
if (ret != 0) return ret;
/*
* This happens, as dl_inst is is used in
* as the loop condition above.
*/
#ifdef __clang_analyzer__
if (!fr_cond_assert(a->dl_inst)) return +1;
if (!fr_cond_assert(b->dl_inst)) return -1;
#endif
ret = (a->dl_inst->parent > b->dl_inst->parent) - (a->dl_inst->parent < b->dl_inst->parent);
if (ret != 0) return ret;
return strcmp(a->name, b->name);
}
/** Compare module's by their private instance data
*
*/
static int module_instance_data_cmp(void const *one, void const *two)
{
void const *a = (((module_instance_t const *)one)->dl_inst)->data;
void const *b = (((module_instance_t const *)two)->dl_inst)->data;
return (a > b) - (a < b);
}
/** Initialise a module specific exfile handle
*
* @see exfile_init
*
* @param[in] ctx to bind the lifetime of the exfile handle to.
* @param[in] module section.
* @param[in] max_entries Max file descriptors to cache, and manage locks for.
* @param[in] max_idle Maximum time a file descriptor can be idle before it's closed.
* @param[in] locking Whether or not to lock the files.
* @param[in] trigger_prefix if NULL will be set automatically from the module CONF_SECTION.
* @param[in] trigger_args to make available in any triggers executed by the connection pool.
* @return
* - New connection pool.
* - NULL on error.
*/
exfile_t *module_exfile_init(TALLOC_CTX *ctx,
CONF_SECTION *module,
uint32_t max_entries,
uint32_t max_idle,
bool locking,
char const *trigger_prefix,
VALUE_PAIR *trigger_args)
{
char trigger_prefix_buff[128];
exfile_t *handle;
if (!trigger_prefix) {
snprintf(trigger_prefix_buff, sizeof(trigger_prefix_buff), "modules.%s.file", cf_section_name1(module));
trigger_prefix = trigger_prefix_buff;
}
handle = exfile_init(ctx, max_entries, max_idle, locking);
if (!handle) return NULL;
exfile_enable_triggers(handle, cf_section_find(module, "file", NULL), trigger_prefix, trigger_args);
return handle;
}
/** Resolve polymorphic item's from a module's #CONF_SECTION to a subsection in another module
*
* This allows certain module sections to reference module sections in other instances
* of the same module and share #CONF_DATA associated with them.
*
* @verbatim
example {
data {
...
}
}
example inst {
data = example
}
* @endverbatim
*
* @param[out] out where to write the pointer to a module's config section. May be NULL on success,
* indicating the config item was not found within the module #CONF_SECTION
* or the chain of module references was followed and the module at the end of the chain
* did not a subsection.
* @param[in] module #CONF_SECTION.
* @param[in] name of the polymorphic sub-section.
* @return
* - 0 on success with referenced section.
* - 1 on success with local section.
* - -1 on failure.
*/
int module_sibling_section_find(CONF_SECTION **out, CONF_SECTION *module, char const *name)
{
CONF_PAIR *cp;
CONF_SECTION *cs;
CONF_DATA const *cd;
module_instance_t *mi;
char const *inst_name;
#define FIND_SIBLING_CF_KEY "find_sibling"
*out = NULL;
/*
* Is a real section (not referencing sibling module).
*/
cs = cf_section_find(module, name, NULL);
if (cs) {
*out = cs;
return 0;
}
/*
* Item omitted completely from module config.
*/
cp = cf_pair_find(module, name);
if (!cp) return 0;
if (cf_data_find(module, CONF_SECTION, FIND_SIBLING_CF_KEY)) {
cf_log_err(cp, "Module reference loop found");
return -1;
}
cd = cf_data_add(module, module, FIND_SIBLING_CF_KEY, false);
/*
* Item found, resolve it to a module instance.
* This triggers module loading, so we don't have
* instantiation order issues.
*/
inst_name = cf_pair_value(cp);
mi = module_by_name(NULL, inst_name);
if (!mi) {
cf_log_err(cp, "Unknown module instance \"%s\"", inst_name);
return -1;
}
if (!mi->instantiated) {
CONF_SECTION *parent = module;
/*
* Find the root of the config...
*/
do {
CONF_SECTION *tmp;
tmp = cf_item_to_section(cf_parent(parent));
if (!tmp) break;
parent = tmp;
} while (true);
_module_instantiate(module_by_name(NULL, inst_name), NULL);
}
/*
* Remove the config data we added for loop
* detection.
*/
cf_data_remove(module, cd);
/*
* Check the module instances are of the same type.
*/
if (strcmp(cf_section_name1(mi->dl_inst->conf), cf_section_name1(module)) != 0) {
cf_log_err(cp, "Referenced module is a rlm_%s instance, must be a rlm_%s instance",
cf_section_name1(mi->dl_inst->conf), cf_section_name1(module));
return -1;
}
*out = cf_section_find(mi->dl_inst->conf, name, NULL);
return 1;
}
/** Initialise a module specific connection pool
*
* @see fr_pool_init
*
* @param[in] module section.
* @param[in] opaque data pointer to pass to callbacks.
* @param[in] c Callback to create new connections.
* @param[in] a Callback to check the status of connections.
* @param[in] log_prefix override, if NULL will be set automatically from the module CONF_SECTION.
* @param[in] trigger_prefix if NULL will be set automatically from the module CONF_SECTION.
* @param[in] trigger_args to make available in any triggers executed by the connection pool.
* @return
* - New connection pool.
* - NULL on error.
*/
fr_pool_t *module_connection_pool_init(CONF_SECTION *module,
void *opaque,
fr_pool_connection_create_t c,
fr_pool_connection_alive_t a,
char const *log_prefix,
char const *trigger_prefix,
VALUE_PAIR *trigger_args)
{
CONF_SECTION *cs, *mycs;
char log_prefix_buff[128];
char trigger_prefix_buff[128];
fr_pool_t *pool;
char const *cs_name1, *cs_name2;
int ret;
#define parent_name(_x) cf_section_name(cf_item_to_section(cf_parent(_x)))
cs_name1 = cf_section_name1(module);
cs_name2 = cf_section_name2(module);
if (!cs_name2) cs_name2 = cs_name1;
if (!trigger_prefix) {
snprintf(trigger_prefix_buff, sizeof(trigger_prefix_buff), "modules.%s.pool", cs_name1);
trigger_prefix = trigger_prefix_buff;
}
if (!log_prefix) {
snprintf(log_prefix_buff, sizeof(log_prefix_buff), "rlm_%s (%s)", cs_name1, cs_name2);
log_prefix = log_prefix_buff;
}
/*
* Get sibling's pool config section
*/
ret = module_sibling_section_find(&cs, module, "pool");
switch (ret) {
case -1:
return NULL;
case 1:
DEBUG4("%s: Using pool section from \"%s\"", log_prefix, parent_name(cs));
break;
case 0:
DEBUG4("%s: Using local pool section", log_prefix);
break;
}
/*
* Get our pool config section
*/
mycs = cf_section_find(module, "pool", NULL);
if (!mycs) {
DEBUG4("%s: Adding pool section to config item \"%s\" to store pool references", log_prefix,
cf_section_name(module));
mycs = cf_section_alloc(module, module, "pool", NULL);
}
/*
* Sibling didn't have a pool config section
* Use our own local pool.
*/
if (!cs) {
DEBUG4("%s: \"%s.pool\" section not found, using \"%s.pool\"", log_prefix,
parent_name(cs), parent_name(mycs));
cs = mycs;
}
/*
* If fr_pool_init has already been called
* for this config section, reuse the previous instance.
*
* This allows modules to pass in the config sections
* they would like to use the connection pool from.
*/
pool = cf_data_value(cf_data_find(cs, fr_pool_t, NULL));
if (!pool) {
DEBUG4("%s: No pool reference found for config item \"%s.pool\"", log_prefix, parent_name(cs));
pool = fr_pool_init(cs, cs, opaque, c, a, log_prefix);
if (!pool) return NULL;
fr_pool_enable_triggers(pool, trigger_prefix, trigger_args);
if (fr_pool_start(pool) < 0) {
ERROR("%s: Starting initial connections failed", log_prefix);
return NULL;
}
DEBUG4("%s: Adding pool reference %p to config item \"%s.pool\"", log_prefix, pool, parent_name(cs));
cf_data_add(cs, pool, NULL, false);
return pool;
}
fr_pool_ref(pool);
DEBUG4("%s: Found pool reference %p in config item \"%s.pool\"", log_prefix, pool, parent_name(cs));
/*
* We're reusing pool data add it to our local config
* section. This allows other modules to transitively
* re-use a pool through this module.
*/
if (mycs != cs) {
DEBUG4("%s: Copying pool reference %p from config item \"%s.pool\" to config item \"%s.pool\"",
log_prefix, pool, parent_name(cs), parent_name(mycs));
cf_data_add(mycs, pool, NULL, false);
}
return pool;
}
/*
* Convert a string to an integer
*/
module_method_t module_state_str_to_method(module_state_func_table_t const *table,
char const *name, module_method_t def)
{
module_state_func_table_t const *this;
if (!name) return def;
for (this = table; this->name != NULL; this++) {
if (strcasecmp(this->name, name) == 0) return this->func;
}
return def;
}
/*
* Convert an integer to a string.
*/
char const *module_state_method_to_str(module_state_func_table_t const *table,
module_method_t method, char const *def)
{
module_state_func_table_t const *this;
for (this = table; this->name != NULL; this++) if (this->func == method) return this->name;
return def;
}
/** Set the next section type if it's not already set
*
* @param[in] request The current request.
* @param[in] type_da to use. Usually attr_auth_type.
* @param[in] enumv Enumeration value of the specified type_da.
*/
bool module_section_type_set(REQUEST *request, fr_dict_attr_t const *type_da, fr_dict_enum_t const *enumv)
{
VALUE_PAIR *vp;
switch (pair_update_control(&vp, type_da)) {
case 0:
fr_value_box_copy(vp, &vp->data, enumv->value);
vp->data.enumv = vp->da; /* So we get the correct string alias */
RDEBUG2("Setting &control:%pP", vp);
return true;
case 1:
RDEBUG2("&control:%s already set. Not setting to %s", vp->da->name, enumv->alias);
return false;
default:
MEM(0);
return false;
}
}
/** Mark module instance data as being read only
*
* This still allows memory to be modified, but not allocated
*/
int module_instance_read_only(void *inst, char const *name)
{
int rcode;
size_t size;
size = talloc_total_size(inst);
rcode = talloc_set_memlimit(inst, size);
if (rcode < 0) {
ERROR("Failed setting memory limit for module %s", name);
} else {
DEBUG3("Memory limit for module %s is set to %zd bytes", name, size);
}
return rcode;
}
/** Find an existing module instance by its name and parent
*
* @param[in] parent to qualify search with.
* @param[in] asked_name The name of the module we're attempting to find.
* May include '-' which indicates that it's ok for
* the module not to be loaded.
* @return
* - Module instance matching name.
* - NULL if no such module exists.
*/
module_instance_t *module_by_name(module_instance_t const *parent, char const *asked_name)
{
char const *inst_name;
void *inst;
if (!module_instance_name_tree) return NULL;
/*
* Look for the real name. Ignore the first character,
* which tells the server "it's OK for this module to not
* exist."
*/
inst_name = asked_name;
if (inst_name[0] == '-') inst_name++;
inst = rbtree_finddata(module_instance_name_tree,
&(module_instance_t){
.dl_inst = &(dl_module_inst_t){ .parent = parent ? parent->dl_inst : NULL },
.name = inst_name
});
if (!inst) return NULL;
return talloc_get_type_abort(inst, module_instance_t);
}
/** Find an existing module instance and verify it implements the specified method
*
* Extracts the method from the module name where the format is @verbatim <module>.<method> @endverbatim
* and ensures the module implements the specified method.
*
* @param[out] method the method function we will call
* @param[in,out] component the default component to use. Updated to be the found component
* @param[out] name1 name1 of the method being called
* @param[out] name2 name2 of the method being called
* @param[in] name The name of the module we're attempting to find, possibly concatenated with the method
* @return
* - The module instance on success.
* - NULL on not found
*
* If the module exists but the method doesn't exist, then `method` is set to NULL.
*/
module_instance_t *module_by_name_and_method(module_method_t *method, rlm_components_t *component,
char const **name1, char const **name2,
char const *name)
{
char *p, *q, *inst_name;
size_t len;
int j;
rlm_components_t i;
module_instance_t *mi;
module_method_names_t const *methods;
if (method) *method = NULL;
*name1 = *name2 = NULL;
/*
* Module names are allowed to contain '.'
* so we search for the bare module name first.
*/
mi = module_by_name(NULL, name);
if (mi) {
virtual_server_method_t *allowed_list;
if (!method) return mi;
/*
* For now, prefer existing methods over named
* sections. That is because modules can now
* export both old-style methods and new
* "wildcard" methods, which may do different
* things.
*/
if (component && mi->module->methods[*component]) {
*method = mi->module->methods[*component];
return mi;
}
/*
* We weren't asked to search for specific names,
* OR the module has no specific names, return.
*/
if (!name1 || !*name1 || !name2 || !mi->module->method_names) {
return mi;
}
/*
* Walk through the module, finding a matching
* method.
*/
for (j = 0; mi->module->method_names[j].name1 != NULL; j++) {
methods = &mi->module->method_names[j];
/*
* Wildcard match name1, we're
* done.
*/
if (methods->name1 == CF_IDENT_ANY) {
found:
*method = methods->method;
return mi;
}
/*
* If name1 doesn't match, skip it.
*/
if (strcmp(methods->name1, *name1) != 0) continue;
/*
* The module can declare a
* wildcard for name2, in which
* case it's a match.
*/
if (methods->name2 == CF_IDENT_ANY) goto found;
/*
* No name2 is also a match to no name2.
*/
if (!methods->name2 && !*name2) goto found;
/*
* Don't do strcmp on NULLs
*/
if (!methods->name2 || !*name2) continue;
if (strcmp(methods->name2, *name2) == 0) goto found;
}
/*
* No match for "recv Access-Request", or
* whatever else the section is. Let's see if
* the section has a list of allowed methods.
*/
allowed_list = virtual_server_section_methods(*name1, *name2);
if (!allowed_list) return mi;
/*
* Walk over allowed methods for this section,
* (implicitly ordered by priority), and see if
* the allowed method matches any of the module
* methods.
*
* Unfortunately, this process is O(N*M).
* Luckily, we only do it if all else fails, so
* it's mostly OK.
*
* Note that the "allowed" list CANNOT include
* CF_IDENT_ANY. Only the module can do that.
* If the "allowed" list exported CF_IDENT_ANY,
* then any module method would match, which is
* bad.
*/
for (j = 0; allowed_list[j].name != NULL; j++) {
int k;
virtual_server_method_t *allowed = &allowed_list[j];
for (k = 0; mi->module->method_names[k].name1 != NULL; k++) {
methods = &mi->module->method_names[k];
rad_assert(methods->name1 != CF_IDENT_ANY); /* should have been caught above */
if (strcmp(methods->name1, allowed->name) != 0) continue;
/*
* The module matches "recv *",
* call this method.
*/
if (methods->name2 == CF_IDENT_ANY) {
found_allowed:
*method = methods->method;
return mi;
}
/*
* No name2 is also a match to no name2.
*/
if (!methods->name2 && !allowed->name2) goto found_allowed;
/*
* Don't do strcmp on NULLs
*/
if (!methods->name2 || !allowed->name2) continue;
if (strcmp(methods->name2, allowed->name2) == 0) goto found_allowed;
}
}
/*
* Didn't find a matching method. Just return
* the module.
*/
return mi;
}
/*
* Find out if the instance name contains
* a method, if it doesn't, then the module
* doesn't exist.
*/
p = strchr(name, '.');
if (!p) return NULL;
/*
* The module name may have a '.' in it, AND it may have
* a method <sigh> So we try to find out which is which.
*/
inst_name = talloc_strdup(NULL, name);
p = inst_name + (p - name);
/*
* Loop over the '.' portions, gradually looking up a
* longer string, in order to find the full module name.
*/
do {
*p = '\0';
mi = module_by_name(NULL, inst_name);
if (mi) break;
/*
* Find the next '.'
*/
*p = '.';
p = strchr(p + 1, '.');
} while (p);
/*
* No such module, we're done.
*/
if (!mi) {
talloc_free(inst_name);
return NULL;
}
/*
* We have a module, but the caller doesn't care about
* method or names, so just return the module.
*/
if (!name1 || !name2 || !method) {
talloc_free(inst_name);
return mi;
}
/*
* We MAY have two names.
*/
p++;
q = strchr(p, '.');
/*
* If there's only one component, look for it in the
* "authorize", etc. list first.
*/
if (!q) {
for (i = MOD_AUTHENTICATE; i < MOD_COUNT; i++) {
if (strcmp(section_type_value[i], p) != 0) continue;
/*
* Tell the caller which component was
* referenced, and set the method to the found
* function.
*/
if (component) {
*component = i;
if (method) *method = mi->module->methods[*component];
}
/*
* The string matched. Return it. Also set the
* names so that the caller gets told the method
* name being used.
*/
*name1 = name + (p - inst_name);
*name2 = NULL;
talloc_free(inst_name);
return mi;
}
}
/*
* We've found the module, but it has no named methods.
*/
if (!mi->module->method_names) {
talloc_free(inst_name);
return mi;
}
/*
* We have "module.METHOD", but METHOD doesn't match
* "authorize", "authenticate", etc. Let's see if it
* matches anything else.
*/
if (!q) {
for (j = 0; mi->module->method_names[j].name1 != NULL; j++) {
methods = &mi->module->method_names[j];
/*
* If we do not have the second $method, then ignore it!
*/
if (methods->name2 != CF_IDENT_ANY) continue;
/*
* Wildcard match name1, we're
* done.
*/
if (!methods->name1 || (methods->name1 == CF_IDENT_ANY)) goto found_name1;
/*
* If name1 doesn't match, skip it.
*/
if (strcmp(methods->name1, p) != 0) continue;
found_name1:
/*
* We've matched "*", or "name1" or
* "name1 *". Return that.
*/
*name1 = p;
*name2 = NULL;
*method = methods->method;
break;
}
/*
* Return the found module.
*/
talloc_free(inst_name);
return mi;
}
/*
* We CANNOT have '.' in method names.
*/
if (strchr(q + 1, '.') != 0) {