/
modcall.c
3956 lines (3379 loc) · 92.6 KB
/
modcall.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
/*
* @name modcall.c
*
* 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 2000,2006 The FreeRADIUS server project
*/
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>
/* mutually-recursive static functions need a prototype up front */
static modcallable *do_compile_modgroup(modcallable *,
rlm_components_t, CONF_SECTION *,
int, int, int);
/* Actions may be a positive integer (the highest one returned in the group
* will be returned), or the keyword "return", represented here by
* MOD_ACTION_RETURN, to cause an immediate return.
* There's also the keyword "reject", represented here by MOD_ACTION_REJECT
* to cause an immediate reject. */
#define MOD_ACTION_RETURN (-1)
#define MOD_ACTION_REJECT (-2)
/* Here are our basic types: modcallable, modgroup, and modsingle. For an
* explanation of what they are all about, see doc/configurable_failover.rst */
struct modcallable {
modcallable *parent;
struct modcallable *next;
char const *name;
char const *debug_name;
enum { MOD_SINGLE = 1, MOD_GROUP, MOD_LOAD_BALANCE, MOD_REDUNDANT_LOAD_BALANCE,
#ifdef WITH_UNLANG
MOD_IF, MOD_ELSE, MOD_ELSIF, MOD_UPDATE, MOD_SWITCH, MOD_CASE,
MOD_FOREACH, MOD_BREAK, MOD_RETURN,
#endif
MOD_POLICY, MOD_REFERENCE, MOD_XLAT } type;
rlm_components_t method;
int actions[RLM_MODULE_NUMCODES];
};
#define MOD_LOG_OPEN_BRACE RDEBUG2("%s {", c->debug_name)
#define MOD_LOG_CLOSE_BRACE RDEBUG2("} # %s = %s", c->debug_name, fr_int2str(mod_rcode_table, result, "<invalid>"))
typedef struct {
modcallable mc; /* self */
enum {
GROUPTYPE_SIMPLE = 0,
GROUPTYPE_REDUNDANT,
GROUPTYPE_COUNT
} grouptype; /* after mc */
modcallable *children;
modcallable *tail; /* of the children list */
CONF_SECTION *cs;
vp_map_t *map; /* update */
vp_tmpl_t *vpt; /* switch */
fr_cond_t *cond; /* if/elsif */
bool done_pass2;
} modgroup;
typedef struct {
modcallable mc;
module_instance_t *modinst;
} modsingle;
typedef struct {
modcallable mc;
char const *ref_name;
CONF_SECTION *ref_cs;
} modref;
typedef struct {
modcallable mc;
int exec;
char *xlat_name;
} modxlat;
/* Simple conversions: modsingle and modgroup are subclasses of modcallable,
* so we often want to go back and forth between them. */
static modsingle *mod_callabletosingle(modcallable *p)
{
rad_assert(p->type==MOD_SINGLE);
return (modsingle *)p;
}
static modgroup *mod_callabletogroup(modcallable *p)
{
rad_assert((p->type > MOD_SINGLE) && (p->type <= MOD_POLICY));
return (modgroup *)p;
}
static modcallable *mod_singletocallable(modsingle *p)
{
return (modcallable *)p;
}
static modcallable *mod_grouptocallable(modgroup *p)
{
return (modcallable *)p;
}
static modref *mod_callabletoref(modcallable *p)
{
rad_assert(p->type==MOD_REFERENCE);
return (modref *)p;
}
static modcallable *mod_reftocallable(modref *p)
{
return (modcallable *)p;
}
static modxlat *mod_callabletoxlat(modcallable *p)
{
rad_assert(p->type==MOD_XLAT);
return (modxlat *)p;
}
static modcallable *mod_xlattocallable(modxlat *p)
{
return (modcallable *)p;
}
/* modgroups are grown by adding a modcallable to the end */
static void add_child(modgroup *g, modcallable *c)
{
if (!c) return;
(void) talloc_steal(g, c);
if (!g->children) {
g->children = g->tail = c;
} else {
rad_assert(g->tail->next == NULL);
g->tail->next = c;
g->tail = c;
}
c->parent = mod_grouptocallable(g);
}
/* Here's where we recognize all of our keywords: first the rcodes, then the
* actions */
const FR_NAME_NUMBER mod_rcode_table[] = {
{ "reject", RLM_MODULE_REJECT },
{ "fail", RLM_MODULE_FAIL },
{ "ok", RLM_MODULE_OK },
{ "handled", RLM_MODULE_HANDLED },
{ "invalid", RLM_MODULE_INVALID },
{ "userlock", RLM_MODULE_USERLOCK },
{ "notfound", RLM_MODULE_NOTFOUND },
{ "noop", RLM_MODULE_NOOP },
{ "updated", RLM_MODULE_UPDATED },
{ NULL, 0 }
};
/*
* Compile action && rcode for later use.
*/
static int compile_action(modcallable *c, CONF_PAIR *cp)
{
int action;
char const *attr, *value;
attr = cf_pair_attr(cp);
value = cf_pair_value(cp);
if (!value) return 0;
if (!strcasecmp(value, "return"))
action = MOD_ACTION_RETURN;
else if (!strcasecmp(value, "break"))
action = MOD_ACTION_RETURN;
else if (!strcasecmp(value, "reject"))
action = MOD_ACTION_REJECT;
else if (strspn(value, "0123456789")==strlen(value)) {
action = atoi(value);
/*
* Don't allow priority zero, for future use.
*/
if (action == 0) return 0;
} else {
cf_log_err_cp(cp, "Unknown action '%s'.\n",
value);
return 0;
}
if (strcasecmp(attr, "default") != 0) {
int rcode;
rcode = fr_str2int(mod_rcode_table, attr, -1);
if (rcode < 0) {
cf_log_err_cp(cp,
"Unknown module rcode '%s'.\n",
attr);
return 0;
}
c->actions[rcode] = action;
} else { /* set all unset values to the default */
int i;
for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
if (!c->actions[i]) c->actions[i] = action;
}
}
return 1;
}
/* Some short names for debugging output */
static char const * const comp2str[] = {
"authenticate",
"authorize",
"preacct",
"accounting",
"session",
"pre-proxy",
"post-proxy",
"post-auth"
#ifdef WITH_COA
,
"recv-coa",
"send-coa"
#endif
};
#ifdef HAVE_PTHREAD_H
/*
* Lock the mutex for the module
*/
static void safe_lock(module_instance_t *instance)
{
if (instance->mutex)
pthread_mutex_lock(instance->mutex);
}
/*
* Unlock the mutex for the module
*/
static void safe_unlock(module_instance_t *instance)
{
if (instance->mutex)
pthread_mutex_unlock(instance->mutex);
}
#else
/*
* No threads: these functions become NULL's.
*/
#define safe_lock(foo)
#define safe_unlock(foo)
#endif
static rlm_rcode_t CC_HINT(nonnull) call_modsingle(rlm_components_t component, modsingle *sp, REQUEST *request)
{
int blocked;
int indent = request->log.indent;
/*
* If the request should stop, refuse to do anything.
*/
blocked = (request->master_state == REQUEST_STOP_PROCESSING);
if (blocked) return RLM_MODULE_NOOP;
RDEBUG3("modsingle[%s]: calling %s (%s) for request %d",
comp2str[component], sp->modinst->name,
sp->modinst->entry->name, request->number);
request->log.indent = 0;
if (sp->modinst->force) {
request->rcode = sp->modinst->code;
goto fail;
}
/*
* For logging unresponsive children.
*/
request->module = sp->modinst->name;
safe_lock(sp->modinst);
request->rcode = sp->modinst->entry->module->methods[component](sp->modinst->insthandle, request);
safe_unlock(sp->modinst);
request->module = "";
/*
* Wasn't blocked, and now is. Complain!
*/
blocked = (request->master_state == REQUEST_STOP_PROCESSING);
if (blocked) {
RWARN("Module %s became unblocked for request %u", sp->modinst->entry->name, request->number);
}
fail:
request->log.indent = indent;
RDEBUG3("modsingle[%s]: returned from %s (%s) for request %d",
comp2str[component], sp->modinst->name,
sp->modinst->entry->name, request->number);
return request->rcode;
}
static int default_component_results[MOD_COUNT] = {
RLM_MODULE_REJECT, /* AUTH */
RLM_MODULE_NOTFOUND, /* AUTZ */
RLM_MODULE_NOOP, /* PREACCT */
RLM_MODULE_NOOP, /* ACCT */
RLM_MODULE_FAIL, /* SESS */
RLM_MODULE_NOOP, /* PRE_PROXY */
RLM_MODULE_NOOP, /* POST_PROXY */
RLM_MODULE_NOOP /* POST_AUTH */
#ifdef WITH_COA
,
RLM_MODULE_NOOP, /* RECV_COA_TYPE */
RLM_MODULE_NOOP /* SEND_COA_TYPE */
#endif
};
extern char const *unlang_keyword[];
char const *unlang_keyword[] = {
"",
"single",
"group",
"load-balance group",
"redundant-load-balance group",
#ifdef WITH_UNLANG
"if",
"else",
"elsif",
"update",
"switch",
"case",
"foreach",
"break",
"return",
#endif
"policy",
"reference",
"xlat",
NULL
};
static char const modcall_spaces[] = " ";
#define MODCALL_STACK_MAX (32)
/*
* Don't call the modules recursively. Instead, do them
* iteratively, and manage the call stack ourselves.
*/
typedef struct modcall_stack_entry_t {
rlm_rcode_t result;
int priority;
int unwind; /* unwind to this one if it exists */
modcallable *c;
} modcall_stack_entry_t;
static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
modcall_stack_entry_t *entry, bool do_next_sibling);
/*
* Call a child of a block.
*/
static void modcall_child(REQUEST *request, rlm_components_t component, int depth,
modcall_stack_entry_t *entry, modcallable *c,
rlm_rcode_t *result, bool do_next_sibling)
{
modcall_stack_entry_t *next;
if (depth >= MODCALL_STACK_MAX) {
ERROR("Internal sanity check failed: module stack is too deep");
fr_exit(1);
}
/*
* Initialize the childs stack frame.
*/
next = entry + 1;
next->c = c;
next->result = entry->result;
next->priority = 0;
next->unwind = 0;
if (!modcall_recurse(request, component,
depth, next, do_next_sibling)) {
*result = RLM_MODULE_FAIL;
return;
}
/*
* Unwind back up the stack
*/
if (next->unwind != 0) {
entry->unwind = next->unwind;
}
*result = next->result;
return;
}
/*
* Interpret the various types of blocks.
*/
static bool modcall_recurse(REQUEST *request, rlm_components_t component, int depth,
modcall_stack_entry_t *entry, bool do_next_sibling)
{
bool if_taken, was_if;
modcallable *c;
int priority;
rlm_rcode_t result;
was_if = if_taken = false;
result = RLM_MODULE_UNKNOWN;
RINDENT();
redo:
priority = -1;
c = entry->c;
/*
* Nothing more to do. Return the code and priority
* which was set by the caller.
*/
if (!c) goto finish;
rad_assert(c->debug_name != NULL); /* if this happens, all bets are off. */
/*
* We've been asked to stop. Do so.
*/
if ((request->master_state == REQUEST_STOP_PROCESSING) ||
(request->parent &&
(request->parent->master_state == REQUEST_STOP_PROCESSING))) {
entry->result = RLM_MODULE_FAIL;
entry->priority = 9999;
goto finish;
}
#ifdef WITH_UNLANG
/*
* Handle "if" conditions.
*/
if (c->type == MOD_IF) {
int condition;
modgroup *g;
mod_if:
g = mod_callabletogroup(c);
rad_assert(g->cond != NULL);
RDEBUG2("%s %s{", unlang_keyword[c->type], c->name);
condition = radius_evaluate_cond(request, result, 0, g->cond);
if (condition < 0) {
condition = false;
REDEBUG("Failed retrieving values required to evaluate condition");
} else {
RDEBUG2("%s %s -> %s",
unlang_keyword[c->type],
c->name, condition ? "TRUE" : "FALSE");
}
/*
* Didn't pass. Remember that.
*/
if (!condition) {
was_if = true;
if_taken = false;
goto next_sibling;
}
/*
* We took the "if". Go recurse into its' children.
*/
was_if = true;
if_taken = true;
goto do_children;
} /* MOD_IF */
/*
* "else" if the previous "if" was taken.
* "if" if the previous if wasn't taken.
*/
if (c->type == MOD_ELSIF) {
if (!was_if) goto elsif_error;
/*
* Like MOD_ELSE, but allow for a later "else"
*/
if (if_taken) {
RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
unlang_keyword[c->type], request->number);
was_if = true;
if_taken = true;
goto next_sibling;
}
/*
* Check the "if" condition.
*/
goto mod_if;
} /* MOD_ELSIF */
/*
* "else" for a preceding "if".
*/
if (c->type == MOD_ELSE) {
if (!was_if) { /* error */
elsif_error:
RDEBUG2("... skipping %s for request %d: No preceding \"if\"",
unlang_keyword[c->type], request->number);
goto next_sibling;
}
if (if_taken) {
RDEBUG2("... skipping %s for request %d: Preceding \"if\" was taken",
unlang_keyword[c->type], request->number);
was_if = false;
if_taken = false;
goto next_sibling;
}
/*
* We need to process it. Go do that.
*/
was_if = false;
if_taken = false;
goto do_children;
} /* MOD_ELSE */
/*
* We're no longer processing if/else/elsif. Reset the
* trackers for those conditions.
*/
was_if = false;
if_taken = false;
#endif /* WITH_UNLANG */
if (c->type == MOD_SINGLE) {
modsingle *sp;
/*
* Process a stand-alone child, and fall through
* to dealing with it's parent.
*/
sp = mod_callabletosingle(c);
result = call_modsingle(c->method, sp, request);
RDEBUG2("[%s] = %s", c->name ? c->name : "",
fr_int2str(mod_rcode_table, result, "<invalid>"));
goto calculate_result;
} /* MOD_SINGLE */
#ifdef WITH_UNLANG
/*
* Update attribute(s)
*/
if (c->type == MOD_UPDATE) {
int rcode;
modgroup *g = mod_callabletogroup(c);
vp_map_t *map;
MOD_LOG_OPEN_BRACE;
RINDENT();
for (map = g->map; map != NULL; map = map->next) {
rcode = map_to_request(request, map, map_to_vp, NULL);
if (rcode < 0) {
result = (rcode == -2) ? RLM_MODULE_INVALID : RLM_MODULE_FAIL;
REXDENT();
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
}
}
REXDENT();
result = RLM_MODULE_NOOP;
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
} /* MOD_IF */
/*
* Loop over a set of attributes.
*/
if (c->type == MOD_FOREACH) {
int i, foreach_depth = -1;
VALUE_PAIR *vps, *vp;
modcall_stack_entry_t *next = NULL;
vp_cursor_t copy;
modgroup *g = mod_callabletogroup(c);
if (depth >= MODCALL_STACK_MAX) {
ERROR("Internal sanity check failed: module stack is too deep");
fr_exit(1);
}
/*
* Figure out how deep we are in nesting by looking at request_data
* stored previously.
*/
for (i = 0; i < 8; i++) {
if (!request_data_reference(request, (void *)radius_get_vp, i)) {
foreach_depth = i;
break;
}
}
if (foreach_depth < 0) {
REDEBUG("foreach Nesting too deep!");
result = RLM_MODULE_FAIL;
goto calculate_result;
}
/*
* Copy the VPs from the original request, this ensures deterministic
* behaviour if someone decides to add or remove VPs in the set were
* iterating over.
*/
if (tmpl_copy_vps(request, &vps, request, g->vpt) < 0) { /* nothing to loop over */
MOD_LOG_OPEN_BRACE;
result = RLM_MODULE_NOOP;
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
}
rad_assert(vps != NULL);
fr_cursor_init(©, &vps);
RDEBUG2("foreach %s ", c->name);
/*
* This is the actual body of the foreach loop
*/
for (vp = fr_cursor_first(©);
vp != NULL;
vp = fr_cursor_next(©)) {
#ifndef NDEBUG
if (fr_debug_lvl >= 2) {
char buffer[1024];
vp_prints_value(buffer, sizeof(buffer), vp, '"');
RDEBUG2("# Foreach-Variable-%d = %s", foreach_depth, buffer);
}
#endif
/*
* Add the vp to the request, so that
* xlat.c, xlat_foreach() can find it.
*/
request_data_add(request, (void *)radius_get_vp, foreach_depth, &vp, false);
/*
* Initialize the childs stack frame.
*/
next = entry + 1;
next->c = g->children;
next->result = entry->result;
next->priority = 0;
next->unwind = 0;
if (!modcall_recurse(request, component, depth + 1, next, true)) {
break;
}
/*
* We've been asked to unwind to the
* enclosing "foreach". We're here, so
* we can stop unwinding.
*/
if (next->unwind == MOD_BREAK) {
entry->unwind = 0;
break;
}
/*
* Unwind all the way.
*/
if (next->unwind == MOD_RETURN) {
entry->unwind = MOD_RETURN;
break;
}
} /* loop over VPs */
/*
* Free the copied vps and the request data
* If we don't remove the request data, something could call
* the xlat outside of a foreach loop and trigger a segv.
*/
fr_pair_list_free(&vps);
request_data_get(request, (void *)radius_get_vp, foreach_depth);
rad_assert(next != NULL);
result = next->result;
priority = next->priority;
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
} /* MOD_FOREACH */
/*
* Break out of a "foreach" loop, or return from a nested
* group.
*/
if ((c->type == MOD_BREAK) || (c->type == MOD_RETURN)) {
int i;
VALUE_PAIR **copy_p;
RDEBUG2("%s", unlang_keyword[c->type]);
for (i = 8; i >= 0; i--) {
copy_p = request_data_get(request, (void *)radius_get_vp, i);
if (copy_p) {
if (c->type == MOD_BREAK) {
RDEBUG2("# break Foreach-Variable-%d", i);
break;
}
}
}
/*
* Leave result / priority on the stack, and stop processing the section.
*/
entry->unwind = c->type;
goto finish;
} /* MOD_BREAK */
#endif /* WITH_UNLANG */
/*
* Child is a group that has children of it's own.
*/
if ((c->type == MOD_GROUP) || (c->type == MOD_POLICY)
#ifdef WITH_UNLANG
|| (c->type == MOD_CASE)
#endif
) {
modgroup *g;
#ifdef WITH_UNLANG
do_children:
#endif
g = mod_callabletogroup(c);
/*
* This should really have been caught in the
* compiler, and the node never generated. But
* doing that requires changing it's API so that
* it returns a flag instead of the compiled
* MOD_GROUP.
*/
if (!g->children) {
RDEBUG2("%s { ... } # empty sub-section is ignored", c->name);
goto next_sibling;
}
MOD_LOG_OPEN_BRACE;
modcall_child(request, component,
depth + 1, entry, g->children,
&result, true);
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
} /* MOD_GROUP */
#ifdef WITH_UNLANG
if (c->type == MOD_SWITCH) {
modcallable *this, *found, *null_case;
modgroup *g, *h;
fr_cond_t cond;
value_data_t data;
vp_map_t map;
vp_tmpl_t vpt;
MOD_LOG_OPEN_BRACE;
g = mod_callabletogroup(c);
memset(&cond, 0, sizeof(cond));
memset(&map, 0, sizeof(map));
cond.type = COND_TYPE_MAP;
cond.data.map = ↦
map.op = T_OP_CMP_EQ;
map.ci = cf_section_to_item(g->cs);
rad_assert(g->vpt != NULL);
null_case = found = NULL;
data.ptr = NULL;
/*
* The attribute doesn't exist. We can skip
* directly to the default 'case' statement.
*/
if ((g->vpt->type == TMPL_TYPE_ATTR) && (tmpl_find_vp(NULL, request, g->vpt) < 0)) {
find_null_case:
for (this = g->children; this; this = this->next) {
rad_assert(this->type == MOD_CASE);
h = mod_callabletogroup(this);
if (h->vpt) continue;
found = this;
break;
}
goto do_null_case;
}
/*
* Expand the template if necessary, so that it
* is evaluated once instead of for each 'case'
* statement.
*/
if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
(g->vpt->type == TMPL_TYPE_XLAT) ||
(g->vpt->type == TMPL_TYPE_EXEC)) {
char *p;
ssize_t len;
len = tmpl_aexpand(request, &p, request, g->vpt, NULL, NULL);
if (len < 0) goto find_null_case;
data.strvalue = p;
tmpl_init(&vpt, TMPL_TYPE_LITERAL, data.strvalue, len);
}
/*
* Find either the exact matching name, or the
* "case {...}" statement.
*/
for (this = g->children; this; this = this->next) {
rad_assert(this->type == MOD_CASE);
h = mod_callabletogroup(this);
/*
* Remember the default case
*/
if (!h->vpt) {
if (!null_case) null_case = this;
continue;
}
/*
* If we're switching over an attribute
* AND we haven't pre-parsed the data for
* the case statement, then cast the data
* to the type of the attribute.
*/
if ((g->vpt->type == TMPL_TYPE_ATTR) &&
(h->vpt->type != TMPL_TYPE_DATA)) {
map.rhs = g->vpt;
map.lhs = h->vpt;
cond.cast = g->vpt->tmpl_da;
/*
* Remove unnecessary casting.
*/
if ((h->vpt->type == TMPL_TYPE_ATTR) &&
(g->vpt->tmpl_da->type == h->vpt->tmpl_da->type)) {
cond.cast = NULL;
}
/*
* Use the pre-expanded string.
*/
} else if ((g->vpt->type == TMPL_TYPE_XLAT_STRUCT) ||
(g->vpt->type == TMPL_TYPE_XLAT) ||
(g->vpt->type == TMPL_TYPE_EXEC)) {
map.rhs = h->vpt;
map.lhs = &vpt;
cond.cast = NULL;
/*
* Else evaluate the 'switch' statement.
*/
} else {
map.rhs = h->vpt;
map.lhs = g->vpt;
cond.cast = NULL;
}
if (radius_evaluate_map(request, RLM_MODULE_UNKNOWN, 0,
&cond) == 1) {
found = this;
break;
}
}
if (!found) found = null_case;
do_null_case:
talloc_free(data.ptr);
modcall_child(request, component, depth + 1, entry, found, &result, true);
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
} /* MOD_SWITCH */
#endif
if ((c->type == MOD_LOAD_BALANCE) ||
(c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
uint32_t count = 0;
modcallable *this, *found;
modgroup *g;
MOD_LOG_OPEN_BRACE;
g = mod_callabletogroup(c);
found = g->children;
rad_assert(g->children != NULL);
/*
* Choose a child at random.
*/
for (this = g->children; this; this = this->next) {
count++;
if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
found = this;
}
}
if (c->type == MOD_LOAD_BALANCE) {
modcall_child(request, component,
depth + 1, entry, found,
&result, false);
} else {
this = found;
do {
modcall_child(request, component,
depth + 1, entry, this,
&result, false);
if (this->actions[result] == MOD_ACTION_RETURN) {
priority = -1;
break;
}
this = this->next;
if (!this) this = g->children;
} while (this != found);
}
MOD_LOG_CLOSE_BRACE;
goto calculate_result;
} /* MOD_LOAD_BALANCE */
/*
* Reference another virtual server.
*
* This should really be deleted, and replaced with a
* more abstracted / functional version.
*/
if (c->type == MOD_REFERENCE) {
modref *mr = mod_callabletoref(c);
char const *server = request->server;
if (server == mr->ref_name) {
RWDEBUG("Suppressing recursive call to server %s", server);
goto next_sibling;
}
request->server = mr->ref_name;
RDEBUG("server %s { # nested call", mr->ref_name);
result = indexed_modcall(component, 0, request);
RDEBUG("} # server %s with nested call", mr->ref_name);
request->server = server;
goto calculate_result;
} /* MOD_REFERENCE */
/*
* xlat a string without doing anything else
*
* This should really be deleted, and replaced with a
* more abstracted / functional version.
*/
if (c->type == MOD_XLAT) {