/
modcall.c
2743 lines (2382 loc) · 62.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);
/* 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;
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,
#endif
MOD_POLICY, MOD_REFERENCE, MOD_XLAT } type;
rlm_components_t method;
int actions[RLM_MODULE_NUMCODES];
};
#define MOD_LOG_OPEN_BRACE(_name) RDEBUG2("%.*s%s %s {", depth + 1, modcall_spaces, _name ? _name : "", c->name)
#define MOD_LOG_CLOSE_BRACE() RDEBUG2("%.*s} # %s %s = %s", depth + 1, modcall_spaces, \
cf_section_name1(g->cs) ? cf_section_name1(g->cs) : "", c->name ? c->name : "", \
fr_int2str(mod_rcode_table, result, "<invalid>"))
typedef struct {
modcallable mc; /* self */
enum {
GROUPTYPE_SIMPLE = 0,
GROUPTYPE_REDUNDANT,
GROUPTYPE_APPEND,
GROUPTYPE_COUNT
} grouptype; /* after mc */
modcallable *children;
CONF_SECTION *cs;
value_pair_map_t *map; /* update */
fr_cond_t *cond; /* if/elsif */
} 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;
/*
static const FR_NAME_NUMBER grouptype_table[] = {
{ "", GROUPTYPE_SIMPLE },
{ "redundant ", GROUPTYPE_REDUNDANT },
{ "append ", GROUPTYPE_APPEND },
{ NULL, -1 }
};
*/
/* 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 */
/* FIXME: This is O(N^2) */
static void add_child(modgroup *g, modcallable *c)
{
modcallable **head = &g->children;
modcallable *node = *head;
modcallable **last = head;
if (!c) return;
while (node) {
last = &node->next;
node = node->next;
}
rad_assert(c->next == NULL);
*last = 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 call_modsingle(rlm_components_t component, modsingle *sp, REQUEST *request)
{
rlm_rcode_t myresult;
int blocked;
rad_assert(request != NULL);
/*
* 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);
if (sp->modinst->force) {
myresult = sp->modinst->code;
goto fail;
}
safe_lock(sp->modinst);
/*
* For logging unresponsive children.
*/
request->module = sp->modinst->name;
myresult = sp->modinst->entry->module->methods[component](
sp->modinst->insthandle, request);
request->module = "";
safe_unlock(sp->modinst);
/*
* 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:
RDEBUG3(" modsingle[%s]: returned from %s (%s) for request %d",
comp2str[component], sp->modinst->name,
sp->modinst->entry->name, request->number);
return myresult;
}
static int default_component_results[RLM_COMPONENT_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
};
static char const *group_name[] = {
"",
"single",
"group",
"load-balance group",
"redundant-load-balance group",
#ifdef WITH_UNLANG
"if",
"else",
"elsif",
"update",
"switch",
"case",
"foreach",
"break",
#endif
"policy",
"reference",
"xlat"
};
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);
/*
* 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)
{
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)) {
*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 if_taken, was_if;
modcallable *c;
int priority;
rlm_rcode_t result;
was_if = if_taken = false;
result = RLM_MODULE_UNKNOWN;
redo:
priority = -1;
c = entry->c;
/*
* Nothing more to do. Return the code and priority
* which was set by the caller.
*/
if (!c) return true;
/*
* 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;
return true;
}
#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 %s", depth + 1, modcall_spaces,
group_name[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 -> %s", depth + 1, modcall_spaces,
group_name[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("%.*s ... skipping %s for request %d: Preceding \"if\" was taken",
depth + 1, modcall_spaces,
group_name[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("%.*s ... skipping %s for request %d: No preceding \"if\"",
depth + 1, modcall_spaces,
group_name[c->type], request->number);
goto next_sibling;
}
if (if_taken) {
RDEBUG2("%.*s ... skipping %s for request %d: Preceding \"if\" was taken",
depth + 1, modcall_spaces,
group_name[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] = %s", depth + 1, modcall_spaces, 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);
value_pair_map_t *map;
MOD_LOG_OPEN_BRACE("update");
for (map = g->map; map != NULL; map = map->next) {
rcode = radius_map2request(request, map, "update", radius_map2vp, NULL);
if (rcode < 0) {
result = (rcode == -2) ? RLM_MODULE_INVALID : RLM_MODULE_FAIL;
MOD_LOG_CLOSE_BRACE();
goto calculate_result;
}
}
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 *vp;
modcall_stack_entry_t *next = NULL;
vp_cursor_t cursor;
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,
radius_get_vp, i)) {
foreach_depth = i;
break;
}
}
if (foreach_depth < 0) {
REDEBUG("foreach Nesting too deep!");
result = RLM_MODULE_FAIL;
goto calculate_result;
}
if (radius_get_vp(&vp, request, c->name) < 0) {
RDEBUG("Unknown attribute \"%s\"", c->name);
result = RLM_MODULE_FAIL;
goto calculate_result;
}
if (!vp) { /* nothing to loop over */
MOD_LOG_OPEN_BRACE("foreach");
result = RLM_MODULE_NOOP;
MOD_LOG_CLOSE_BRACE();
goto calculate_result;
}
RDEBUG2("%.*sforeach %s ", depth + 1, modcall_spaces,
c->name);
fr_cursor_init(&cursor, &vp);
/* Prime the cursor. */
cursor.found = cursor.current;
while (vp) {
VALUE_PAIR *copy = NULL, **copy_p;
#ifndef NDEBUG
if (fr_debug_flag >= 2) {
char buffer[1024];
vp_prints_value(buffer, sizeof(buffer), vp, '"');
RDEBUG2("%.*s # Foreach-Variable-%d = %s", depth + 1,
modcall_spaces, foreach_depth, buffer);
}
#endif
/*
* Copy only the one VP we care about.
*/
copy = paircopyvp(request, vp);
copy_p = ©
/*
* @fixme: The old code freed copy on
* request_data_add or request_data_get.
* There's no way to easily do that now.
* The foreach code should be audited for
* possible memory leaks, though it's not
* a huge priority as any leaked memory
* will be freed on request free.
*/
request_data_add(request, radius_get_vp, foreach_depth, copy_p, 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)) {
request_data_get(request, radius_get_vp, foreach_depth);
pairfree(©);
break;
}
vp = fr_cursor_next_by_num(&cursor, vp->da->attr, vp->da->vendor, TAG_ANY);
/*
* Delete the cached attribute, if it exists.
*/
if (copy) {
request_data_get(request, radius_get_vp, foreach_depth);
pairfree(©);
} else {
break;
}
/*
* If we've been told to stop processing
* it, do so.
*/
if (entry->unwind == MOD_FOREACH) {
entry->unwind = 0;
break;
}
} /* loop over VPs */
result = next->result;
priority = next->priority;
MOD_LOG_CLOSE_BRACE();
goto calculate_result;
} /* MOD_FOREACH */
/*
* Break out of a "foreach" loop.
*/
if (c->type == MOD_BREAK) {
int i;
VALUE_PAIR **copy_p;
for (i = 8; i >= 0; i--) {
copy_p = request_data_get(request, radius_get_vp, i);
if (copy_p) {
RDEBUG2("%.*s # break Foreach-Variable-%d", depth + 1,
modcall_spaces, i);
pairfree(copy_p);
break;
}
}
/*
* Leave result / priority on the stack, and stop processing the section.
*/
entry->unwind = MOD_FOREACH;
return true;
} /* MOD_BREAK */
#endif /* WITH_PROXY */
/*
* 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%s %s { ... } # empty sub-section is ignored",
depth + 1, modcall_spaces, cf_section_name1(g->cs), c->name);
goto next_sibling;
}
MOD_LOG_OPEN_BRACE(cf_section_name1(g->cs));
modcall_child(request, component,
depth + 1, entry, g->children,
&result);
MOD_LOG_CLOSE_BRACE();
goto calculate_result;
} /* MOD_GROUP */
#ifdef WITH_UNLANG
if (c->type == MOD_SWITCH) {
modcallable *this, *found, *null_case;
modgroup *g;
char buffer[1024];
MOD_LOG_OPEN_BRACE("switch");
/*
* If there's no %, it refers to an attribute.
* Otherwise, expand it.
*/
if (!strchr(c->name, '%')) {
VALUE_PAIR *vp = NULL;
radius_get_vp(&vp, request, c->name);
if (vp) {
vp_prints_value(buffer,
sizeof(buffer),
vp, 0);
} else {
*buffer = '\0';
}
} else {
radius_xlat(buffer, sizeof(buffer),
request, c->name, NULL, NULL);
}
/*
* Find either the exact matching name, or the
* "case {...}" statement.
*/
g = mod_callabletogroup(c);
null_case = found = NULL;
for (this = g->children; this; this = this->next) {
if (!this->name) {
if (!null_case) null_case = this;
continue;
}
if (strcmp(buffer, this->name) == 0) {
found = this;
break;
}
}
if (!found) found = null_case;
MOD_LOG_OPEN_BRACE(group_name[c->type]);
modcall_child(request, component,
depth + 1, entry, found,
&result);
MOD_LOG_CLOSE_BRACE();
goto calculate_result;
} /* MOD_SWITCH */
#endif
if ((c->type == MOD_LOAD_BALANCE) ||
(c->type == MOD_REDUNDANT_LOAD_BALANCE)) {
int count = 0;
modcallable *this, *found;
modgroup *g;
MOD_LOG_OPEN_BRACE("load-balance");
g = mod_callabletogroup(c);
found = NULL;
for (this = g->children; this; this = this->next) {
if (!found) {
found = this;
count = 1;
continue;
}
count++;
if ((count * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
found = this;
}
}
MOD_LOG_OPEN_BRACE(group_name[c->type]);
if (c->type == MOD_LOAD_BALANCE) {
modcall_child(request, component,
depth + 1, entry, found,
&result);
} else {
int i;
/*
* Loop over all children in this
* section. If we get FAIL, then
* continue. Otherwise, stop.
*/
for (i = 1; i < count; i++) {
modcall_child(request, component,
depth + 1, entry, found,
&result);
if (c->actions[result] == MOD_ACTION_RETURN) {
priority = -1;
break;
}
}
}
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) {
modxlat *mx = mod_callabletoxlat(c);
char buffer[128];
if (!mx->exec) {
radius_xlat(buffer, sizeof(buffer), request, mx->xlat_name, NULL, NULL);
} else {
RDEBUG("`%s`", mx->xlat_name);
radius_exec_program(request, mx->xlat_name, false, true, NULL, 0,
EXEC_TIMEOUT, request->packet->vps, NULL);
}
goto next_sibling;
} /* MOD_XLAT */
/*
* Add new module types here.
*/
calculate_result:
#if 0
RDEBUG("(%s, %d) ? (%s, %d)",
fr_int2str(mod_rcode_table, result, "<invalid>"),
priority,
fr_int2str(mod_rcode_table, entry->result, "<invalid>"),
entry->priority);
#endif
rad_assert(result != RLM_MODULE_UNKNOWN);
/*
* The child's action says return. Do so.
*/
if ((c->actions[result] == MOD_ACTION_RETURN) &&
(priority <= 0)) {
entry->result = result;
return true;
}
/*
* If "reject", break out of the loop and return
* reject.
*/
if (c->actions[result] == MOD_ACTION_REJECT) {
entry->result = RLM_MODULE_REJECT;
return true;
}
/*
* The array holds a default priority for this return
* code. Grab it in preference to any unset priority.
*/
if (priority < 0) {
priority = c->actions[result];
}
/*
* We're higher than any previous priority, remember this
* return code and priority.
*/
if (priority > entry->priority) {
entry->result = result;
entry->priority = priority;
}