The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
compile.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 95e207e3d6147b9dc9dadfbbccced77f623a73cf $
19 *
20 * @file unlang/compile.c
21 * @brief Functions to convert configuration sections into unlang structures.
22 *
23 * @copyright 2006-2016 The FreeRADIUS server project
24 */
25RCSID("$Id: 95e207e3d6147b9dc9dadfbbccced77f623a73cf $")
26
27#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
28
29#include <freeradius-devel/server/virtual_servers.h>
30
31#include <freeradius-devel/server/cf_file.h>
32#include <freeradius-devel/server/main_config.h>
33#include <freeradius-devel/server/map_proc.h>
34#include <freeradius-devel/server/modpriv.h>
35#include <freeradius-devel/server/module_rlm.h>
36
37
38#include <freeradius-devel/unlang/xlat_priv.h>
39
40#include "call_priv.h"
41#include "caller_priv.h"
42#include "condition_priv.h"
43#include "foreach_priv.h"
44#include "load_balance_priv.h"
45#include "map_priv.h"
46#include "module_priv.h"
47#include "parallel_priv.h"
48#include "subrequest_priv.h"
49#include "switch_priv.h"
50#include "edit_priv.h"
51#include "timeout_priv.h"
52#include "limit_priv.h"
53#include "transaction_priv.h"
54#include "try_priv.h"
55#include "mod_action.h"
56
57static unsigned int unlang_number = 1;
58
59/*
60 * For simplicity, this is just array[unlang_number]. Once we
61 * call unlang_thread_instantiate(), the "unlang_number" above MUST
62 * NOT change.
63 */
65
66/*
67 * Until we know how many instructions there are, we can't
68 * allocate an array. So we have to put the instructions into an
69 * RB tree.
70 */
72
73/* Here's where we recognize all of our keywords: first the rcodes, then the
74 * actions */
76 { L("..."), RLM_MODULE_NOT_SET },
77 { L("disallow"), RLM_MODULE_DISALLOW },
78 { L("fail"), RLM_MODULE_FAIL },
79 { L("handled"), RLM_MODULE_HANDLED },
80 { L("invalid"), RLM_MODULE_INVALID },
81 { L("noop"), RLM_MODULE_NOOP },
82 { L("notfound"), RLM_MODULE_NOTFOUND },
83 { L("ok"), RLM_MODULE_OK },
84 { L("reject"), RLM_MODULE_REJECT },
85 { L("timeout"), RLM_MODULE_TIMEOUT },
86 { L("updated"), RLM_MODULE_UPDATED }
87};
89
90#define UPDATE_CTX2 unlang_compile_ctx_copy(&unlang_ctx2, unlang_ctx)
91
92
93static char const unlang_spaces[] = " ";
94
95bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
96{
97 tmpl_t *vpt = *vpt_p;
98
100
101 /*
102 * We may now know the correct dictionary
103 * where we didn't before...
104 */
105 if (!vpt->rules.attr.dict_def) tmpl_set_dict_def(vpt, dict);
106
107 /*
108 * Fixup any other tmpl types
109 */
110 if (tmpl_resolve(vpt, &(tmpl_res_rules_t){ .dict_def = dict, .force_dict_def = (dict != NULL)}) < 0) {
111 cf_log_perr(ci, NULL);
112 return false;
113 }
114
115 return true;
116}
117
118/** Fixup ONE map (recursively)
119 *
120 * This function resolves most things. Most notable it CAN leave the
121 * RHS unresolved, for use in `map` sections.
122 */
124{
125 RULES_VERIFY(rules);
126
127 if (tmpl_is_data_unresolved(map->lhs)) {
128 if (!pass2_fixup_tmpl(map, &map->lhs, map->ci, rules->attr.dict_def)) {
129 return false;
130 }
131 }
132
133 /*
134 * Enforce parent-child relationships in nested maps.
135 */
136 if (parent) {
137 if ((map->op != T_OP_EQ) && (!map->parent || (map->parent->op != T_OP_SUB_EQ))) {
138 cf_log_err(map->ci, "Invalid operator \"%s\" in nested map section. "
139 "Only '=' is allowed",
140 fr_table_str_by_value(fr_tokens_table, map->op, "<INVALID>"));
141 return false;
142 }
143 }
144
145 if (map->rhs) {
146 if (tmpl_is_data_unresolved(map->rhs)) {
148
149 if (!pass2_fixup_tmpl(map, &map->rhs, map->ci, rules->attr.dict_def)) {
150 return false;
151 }
152 }
153 }
154
155 /*
156 * Sanity check sublists.
157 */
158 if (!map_list_empty(&map->child)) {
159 fr_dict_attr_t const *da;
160 map_t *child;
161
162 if (!tmpl_is_attr(map->lhs)) {
163 cf_log_err(map->ci, "Sublists can only be assigned to a known attribute");
164 return false;
165 }
166
167 da = tmpl_attr_tail_da(map->lhs);
168
169 /*
170 * Resolve all children.
171 */
172 for (child = map_list_next(&map->child, NULL);
173 child != NULL;
174 child = map_list_next(&map->child, child)) {
175 if (!pass2_fixup_map(child, rules, da)) {
176 return false;
177 }
178 }
179 }
180
181 return true;
182}
183
184/*
185 * Do all kinds of fixups and checks for update sections.
186 */
188{
190 map_t *map = NULL;
191
192 RULES_VERIFY(rules);
193
194 while ((map = map_list_next(&gext->map, map))) {
195 /*
196 * Mostly fixup the map, but maybe leave the RHS
197 * unresolved.
198 */
199 if (!pass2_fixup_map(map, rules, NULL)) return false;
200
201 /*
202 * Check allowed operators, and ensure that the
203 * RHS is resolved.
204 */
205 if (cf_item_is_pair(map->ci) && (unlang_fixup_update(map, NULL) < 0)) return false;
206 }
207
208 return true;
209}
210
211/*
212 * Compile the RHS of map sections to xlat_exp_t
213 */
215{
217 map_t *map = NULL;
218
219 RULES_VERIFY(rules);
220
221 /*
222 * Do most fixups on the maps. Leaving the RHS as
223 * unresolved, so that the `map` function can interpret
224 * the RHS as a reference to a json string, SQL column
225 * name, etc.
226 */
227 while ((map = map_list_next(&gext->map, map))) {
228 if (!pass2_fixup_map(map, rules, NULL)) return false;
229 }
230
231 /*
232 * Map sections don't need a VPT.
233 */
234 if (!gext->vpt) return true;
235
236 if (map_list_num_elements(&gext->map) == 0) return true;
237
238 return pass2_fixup_tmpl(map_list_head(&gext->map)->ci, &gext->vpt,
239 cf_section_to_item(g->cs), rules->attr.dict_def);
240}
241
242static void unlang_dump(unlang_t *c, int depth)
243{
245 map_t *map;
246 char buffer[1024];
247
248 switch (c->type) {
249 case UNLANG_TYPE_NULL:
251 case UNLANG_TYPE_MAX:
252 fr_assert(0);
253 break;
254
256 DEBUG("%.*s%s", depth, unlang_spaces, c->debug_name);
257 break;
258
260 {
262
263 DEBUG("%.*s%s", depth, unlang_spaces, m->mmc.mi->name);
264 }
265 break;
266
267 case UNLANG_TYPE_MAP:
268 {
269 unlang_map_t *gext;
270
271 DEBUG("%.*s%s {", depth, unlang_spaces, c->debug_name);
272
274 gext = unlang_group_to_map(g);
275 map = NULL;
276 while ((map = map_list_next(&gext->map, map))) {
277 map_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), map);
278 DEBUG("%.*s%s", depth + 1, unlang_spaces, buffer);
279 }
280
281 DEBUG("%.*s}", depth, unlang_spaces);
282 }
283 break;
284
285 case UNLANG_TYPE_EDIT:
286 {
287 unlang_edit_t *edit;
288
289 edit = unlang_generic_to_edit(c);
290 map = NULL;
291 while ((map = map_list_next(&edit->maps, map))) {
292 if (!map->rhs) continue; /* @todo - fixme */
293
294 map_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), map);
295 DEBUG("%.*s%s", depth + 1, unlang_spaces, buffer);
296 }
297
298 DEBUG("%.*s}", depth, unlang_spaces);
299 }
300 break;
301
302 case UNLANG_TYPE_CALL:
304 case UNLANG_TYPE_CASE:
307 case UNLANG_TYPE_ELSE:
310 case UNLANG_TYPE_IF:
321 case UNLANG_TYPE_TRY:
322 case UNLANG_TYPE_CATCH: /* @todo - print out things we catch, too */
324
325 DEBUG("%.*s%s {", depth, unlang_spaces, c->debug_name);
326 unlang_list_foreach(&g->children, child) {
327 unlang_dump(child, depth + 1);
328 }
329 DEBUG("%.*s}", depth, unlang_spaces);
330 break;
331
336 case UNLANG_TYPE_TMPL:
337 case UNLANG_TYPE_XLAT:
338 DEBUG("%.*s%s", depth, unlang_spaces, c->debug_name);
339 break;
340 }
341}
342
343
344/** Validate and fixup a map that's part of an update section.
345 *
346 * @param map to validate.
347 * @param ctx data to pass to fixup function (currently unused).
348 * @return
349 * - 0 if valid.
350 * - -1 not valid.
351 */
352int unlang_fixup_update(map_t *map, void *ctx)
353{
354 CONF_PAIR *cp = cf_item_to_pair(map->ci);
355
356 if (!ctx) {
357 /*
358 * Fixup RHS attribute references to change NUM_UNSPEC to NUM_ALL.
359 *
360 * RHS may be NULL for T_OP_CMP_FALSE.
361 */
362 if (map->rhs) {
363 switch (map->rhs->type) {
364 case TMPL_TYPE_ATTR:
366 break;
367
368 default:
369 break;
370 }
371 }
372 }
373
374 /*
375 * Lots of sanity checks for insane people...
376 */
377
378 /*
379 * Depending on the attribute type, some operators are disallowed.
380 */
381 if (tmpl_is_attr(map->lhs)) {
382 /*
383 * What exactly where you expecting to happen here?
384 */
385 if (tmpl_attr_tail_da_is_leaf(map->lhs) &&
386 map->rhs && tmpl_is_list(map->rhs)) {
387 cf_log_err(map->ci, "Can't copy list into an attribute");
388 return -1;
389 }
390
391 if (!fr_assignment_op[map->op] && !fr_comparison_op[map->op] && !fr_binary_op[map->op]) {
392 cf_log_err(map->ci, "Invalid operator \"%s\" in update section. "
393 "Only assignment or filter operators are allowed",
394 fr_table_str_by_value(fr_tokens_table, map->op, "<INVALID>"));
395 return -1;
396 }
397
398 if (fr_comparison_op[map->op] && (map->op != T_OP_CMP_FALSE)) {
399 cf_log_warn(cp, "Please use the 'filter' keyword for attribute filtering");
400 }
401 }
402
403 /*
404 * If the map has a unary operator there's no further
405 * processing we need to, as RHS is unused.
406 */
407 if (map->op == T_OP_CMP_FALSE) return 0;
408
409 if (unlikely(!map->rhs)) {
410 cf_log_err(map->ci, "Missing rhs");
411 return -1;
412 }
413
414 if (!tmpl_is_data_unresolved(map->rhs)) return 0;
415
416 /*
417 * If LHS is an attribute, and RHS is a literal, we can
418 * preparse the information into a TMPL_TYPE_DATA.
419 *
420 * Unless it's a unary operator in which case we
421 * ignore map->rhs.
422 */
423 if (tmpl_is_attr(map->lhs) && tmpl_is_data_unresolved(map->rhs)) {
424 fr_type_t type = tmpl_attr_tail_da(map->lhs)->type;
425
426 /*
427 * @todo - allow passing octets to
428 * FR_TYPE_STRUCT, which can then decode them as
429 * data? That would be rather powerful.
430 */
432
433 /*
434 * It's a literal string, just copy it.
435 * Don't escape anything.
436 */
437 if (tmpl_cast_in_place(map->rhs, type, tmpl_attr_tail_da(map->lhs)) < 0) {
438 cf_log_perr(map->ci, "Cannot convert RHS value (%s) to LHS attribute type (%s)",
441 return -1;
442 }
443
444 return 0;
445 } /* else we can't precompile the data */
446
447 if (!tmpl_is_xlat(map->lhs)) {
448 fr_assert(0);
449 cf_log_err(map->ci, "Cannot determine what update action to perform");
450 return -1;
451 }
452
453 return 0;
454}
455
456
458{
460 unlang_t *c;
461 TALLOC_CTX *ctx;
462 unlang_op_t const *op = &unlang_ops[type];
463
464 ctx = parent;
465 if (!ctx) ctx = cs;
466
467 fr_assert(op->unlang_size > 0);
468
469 /*
470 * All the groups have a common header
471 */
472 g = (unlang_group_t *)_talloc_zero_pooled_object(ctx, op->unlang_size, op->unlang_name,
473 op->pool_headers, op->pool_len);
474 if (!g) return NULL;
475
476 g->cs = cs;
477
479 c->ci = CF_TO_ITEM(cs);
480
482
483 return g;
484}
485
486/** Update a compiled unlang_t with the default actions.
487 *
488 * Don't over-ride any actions which have been set.
489 */
491{
492 int i;
493
494 /*
495 * Note that we do NOT copy over the default retries, as
496 * that would result in every subsection doing it's own
497 * retries. That is not what we want. Instead, we want
498 * the retries to apply only to the _current_ section.
499 */
500
501 /*
502 * Set the default actions if they haven't already been
503 * set.
504 */
505 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
506 if (!c->actions.actions[i]) {
507 c->actions.actions[i] = unlang_ctx->actions.actions[i];
508 }
509 }
510}
511
512#define T(_x) [T_OP_ ## _x] = true
513
514static const bool edit_list_sub_op[T_TOKEN_LAST] = {
515 T(NE),
516 T(GE),
517 T(GT),
518 T(LE),
519 T(LT),
520 T(CMP_EQ),
521};
522
523/** Validate and fixup a map that's part of an edit section.
524 *
525 * @param map to validate.
526 * @param ctx data to pass to fixup function (currently unused).
527 * @return 0 if valid else -1.
528 *
529 * @todo - this is only called for CONF_PAIR maps, not for
530 * CONF_SECTION. So when we parse nested maps, there's no validation
531 * done of the CONF_SECTION. In order to fix this, we need to have
532 * map_afrom_cs() call the validation function for the CONF_SECTION
533 * *before* recursing.
534 */
535static int unlang_fixup_edit(map_t *map, void *ctx)
536{
537 CONF_PAIR *cp = cf_item_to_pair(map->ci);
538 fr_dict_attr_t const *da;
539 fr_dict_attr_t const *parent = NULL;
540 map_t *parent_map = ctx;
541
542 fr_assert(parent_map);
543#ifdef STATIC_ANALYZER
544 if (!parent_map) return -1;
545#endif
546
547 fr_assert(tmpl_is_attr(parent_map->lhs));
548
549 if (parent_map && (parent_map->op == T_OP_SUB_EQ)) {
550 if (!edit_list_sub_op[map->op]) {
551 cf_log_err(cp, "Invalid operator '%s' for right-hand side list. It must be a comparison operator", fr_tokens[map->op]);
552 return -1;
553 }
554
555 } else if (map->op != T_OP_EQ) {
556 cf_log_err(cp, "Invalid operator '%s' for right-hand side list. It must be '='", fr_tokens[map->op]);
557 return -1;
558 }
559
560 /*
561 * map_afrom_cs() will build its tree recursively, and call us for each child map.
562 */
563 if (map->parent && (map->parent != parent_map)) parent_map = map->parent;
564
565 parent = tmpl_attr_tail_da(parent_map->lhs);
566
567 switch (map->lhs->type) {
568 case TMPL_TYPE_ATTR:
569 da = tmpl_attr_tail_da(map->lhs);
570 if (!da->flags.internal && parent && (parent->type != FR_TYPE_GROUP) &&
571 (da->parent != parent)) {
572 /* FIXME - Broken check, doesn't work for key attributes */
573 cf_log_err(cp, "Invalid location for %s - it is not a child of %s",
574 da->name, parent->name);
575 return -1;
576 }
577 break;
578
580 case TMPL_TYPE_XLAT:
581 break;
582
583 default:
584 cf_log_err(map->ci, "Left side of map must be an attribute "
585 "or an xlat (that expands to an attribute), not a %s",
586 tmpl_type_to_str(map->lhs->type));
587 return -1;
588 }
589
590 fr_assert(map->rhs);
591
592 switch (map->rhs->type) {
595 case TMPL_TYPE_XLAT:
596 case TMPL_TYPE_DATA:
597 case TMPL_TYPE_ATTR:
598 case TMPL_TYPE_EXEC:
599 break;
600
601 default:
602 cf_log_err(map->ci, "Right side of map must be an attribute, literal, xlat or exec, got type %s",
603 tmpl_type_to_str(map->rhs->type));
604 return -1;
605 }
606
607 return 0;
608}
609
610/** Compile one edit section.
611 */
613{
614 unlang_edit_t *edit;
616 map_t *map;
617 char const *name;
618 fr_token_t op;
619 ssize_t slen;
620 fr_dict_attr_t const *parent_da;
621 int num;
622
623 tmpl_rules_t t_rules;
624
626 if (name) {
627 cf_log_err(cs, "Unexpected name2 '%s' for editing list %s ", name, cf_section_name1(cs));
628 return NULL;
629 }
630
631 op = cf_section_name2_quote(cs);
632 if ((op == T_INVALID) || !fr_list_assignment_op[op]) {
633 cf_log_err(cs, "Invalid operator '%s' for editing list %s.", fr_tokens[op], cf_section_name1(cs));
634 return NULL;
635 }
636
637 if ((op == T_OP_CMP_TRUE) || (op == T_OP_CMP_FALSE)) {
638 cf_log_err(cs, "Invalid operator \"%s\".",
639 fr_table_str_by_value(fr_tokens_table, op, "<INVALID>"));
640 return NULL;
641 }
642
643 /*
644 * We allow unknown attributes here.
645 */
646 t_rules = *(unlang_ctx->rules);
647 t_rules.attr.allow_unknown = true;
648 RULES_VERIFY(&t_rules);
649
650 edit = talloc_zero(parent, unlang_edit_t);
651 if (!edit) return NULL;
652
653 c = out = unlang_edit_to_generic(edit);
655 c->name = cf_section_name1(cs);
656 c->debug_name = c->name;
657 c->ci = CF_TO_ITEM(cs);
658
659 map_list_init(&edit->maps);
660
661 /*
662 * Allocate the map and initialize it.
663 */
664 MEM(map = talloc_zero(edit, map_t));
665 map->op = op;
666 map->ci = cf_section_to_item(cs);
667 map_list_init(&map->child);
668
670
671 slen = tmpl_afrom_attr_str(map, NULL, &map->lhs, name, &t_rules);
672 if (slen <= 0) {
673 cf_log_err(cs, "Failed parsing list reference %s - %s", name, fr_strerror());
674 fail:
675 talloc_free(edit);
676 return NULL;
677 }
678
679 /*
680 * Can't assign to [*] or [#]
681 */
682 num = tmpl_attr_tail_num(map->lhs);
683 if ((num == NUM_ALL) || (num == NUM_COUNT)) {
684 cf_log_err(cs, "Invalid array reference in %s", name);
685 goto fail;
686 }
687
688 /*
689 * If the DA isn't structural, then it can't have children.
690 */
691 parent_da = tmpl_attr_tail_da(map->lhs);
692 if (fr_type_is_structural(parent_da->type)) {
693 map_t *child;
694
695 /*
696 * Reset the namespace to be this attribute. The tmpl tokenizer will take care of
697 * figuring out if this is a group, TLV, dictionary switch, etc.
698 */
699 t_rules.attr.namespace = parent_da;
700
701 if (map_afrom_cs_edit(map, &map->child, cs, &t_rules, &t_rules, unlang_fixup_edit, map, 256) < 0) {
702 goto fail;
703 }
704
705 /*
706 * As a set of fixups... we can't do array references in -=
707 */
708 if (map->op == T_OP_SUB_EQ) {
709 for (child = map_list_head(&map->child); child != NULL; child = map_list_next(&map->child, child)) {
710 if (!tmpl_is_attr(child->lhs)) continue;
711
712 if (tmpl_attr_tail_num(child->lhs) != NUM_UNSPEC) {
713 cf_log_err(child->ci, "Cannot use array references and values when deleting from a list");
714 goto fail;
715 }
716
717 /*
718 * The edit code doesn't do this correctly, so we just forbid it.
719 */
720 if ((tmpl_attr_num_elements(child->lhs) - tmpl_attr_num_elements(map->lhs)) > 1) {
721 cf_log_err(child->ci, "List deletion must operate directly on the final child");
722 goto fail;
723 }
724
725 /*
726 * We don't do list comparisons either.
727 */
728 if (fr_type_is_structural(tmpl_attr_tail_da(child->lhs)->type)) {
729 cf_log_err(child->ci, "List deletion cannot operate on lists");
730 goto fail;
731 }
732 }
733 }
734 } else {
735 /*
736 * foo := { a, b, c }
737 */
738 if (map_list_afrom_cs(map, &map->child, cs, &t_rules, NULL, NULL, 256) < 0) {
739 goto fail;
740 }
741
742 if ((map->op != T_OP_SET) && !map_list_num_elements(&map->child)) {
743 cf_log_err(cs, "Cannot use operator '%s' for assigning empty list to '%s' data type.",
744 fr_tokens[map->op], fr_type_to_str(parent_da->type));
745 goto fail;
746 }
747 }
748 /*
749 * Do basic sanity checks and resolving.
750 */
751 if (!pass2_fixup_map(map, unlang_ctx->rules, NULL)) goto fail;
752
753 /*
754 * Check operators, and ensure that the RHS has been
755 * resolved.
756 */
757// if (unlang_fixup_update(map, NULL) < 0) goto fail;
758
759 map_list_insert_tail(&edit->maps, map);
760
761 return out;
762}
763
764/** Compile one edit pair
765 *
766 */
768{
769 unlang_edit_t *edit;
770 unlang_t *c = NULL, *out = UNLANG_IGNORE;
771 map_t *map;
772 int num;
773
774 tmpl_rules_t t_rules;
775 fr_token_t op;
776
777 /*
778 * We allow unknown attributes here.
779 */
780 t_rules = *(unlang_ctx->rules);
781 t_rules.attr.allow_unknown = true;
782 fr_assert(t_rules.attr.ci == cf_pair_to_item(cp));
783 RULES_VERIFY(&t_rules);
784
785 edit = talloc_zero(parent, unlang_edit_t);
786 if (!edit) return NULL;
787
788 c = out = unlang_edit_to_generic(edit);
790 c->name = cf_pair_attr(cp);
791 c->debug_name = c->name;
792 c->ci = CF_TO_ITEM(cp);
793
794 map_list_init(&edit->maps);
795
796 op = cf_pair_operator(cp);
797 if ((op == T_OP_CMP_TRUE) || (op == T_OP_CMP_FALSE)) {
798 cf_log_err(cp, "Invalid operator \"%s\".",
799 fr_table_str_by_value(fr_tokens_table, op, "<INVALID>"));
800 fail:
801 talloc_free(edit);
802 return NULL;
803 }
804
805 /*
806 * Convert this particular map.
807 */
808 if (map_afrom_cp(edit, &map, map_list_tail(&edit->maps), cp, &t_rules, NULL, true) < 0) {
809 goto fail;
810 }
811
812 /*
813 * @todo - we still want to do fixups on the RHS?
814 */
815 if (tmpl_is_attr(map->lhs)) {
816 /*
817 * Can't assign to [*] or [#]
818 */
819 num = tmpl_attr_tail_num(map->lhs);
820 if ((num == NUM_ALL) || (num == NUM_COUNT)) {
821 cf_log_err(cp, "Invalid array reference in %s", map->lhs->name);
822 goto fail;
823 }
824
825 if ((map->op == T_OP_SUB_EQ) && fr_type_is_structural(tmpl_attr_tail_da(map->lhs)->type) &&
826 tmpl_is_attr(map->rhs) && tmpl_attr_tail_da(map->rhs)->flags.local) {
827 cf_log_err(cp, "Cannot delete local variable %s", map->rhs->name);
828 goto fail;
829 }
830 }
831
832 /*
833 * Do basic sanity checks and resolving.
834 */
835 if (!pass2_fixup_map(map, unlang_ctx->rules, NULL)) goto fail;
836
837 /*
838 * Check operators, and ensure that the RHS has been
839 * resolved.
840 */
841 if (unlang_fixup_update(map, c) < 0) goto fail;
842
843 map_list_insert_tail(&edit->maps, map);
844
845 return out;
846}
847
848#define debug_braces(_type) (unlang_ops[_type].flag & UNLANG_OP_FLAG_DEBUG_BRACES)
849
850/** Compile a variable definition.
851 *
852 * Definitions which are adjacent to one another are automatically merged
853 * into one larger variable definition.
854 */
856{
859 char const *attr, *value;
860 unlang_group_t *group;
861
863
864 /*
865 * Enforce locations for local variables.
866 */
867 switch (parent->type) {
868 case UNLANG_TYPE_CASE:
869 case UNLANG_TYPE_ELSE:
873 case UNLANG_TYPE_IF:
881 break;
882
883 default:
884 cf_log_err(cp, "Local variables cannot be used here");
885 return -1;
886 }
887
888 /*
889 * The variables exist in the parent block.
890 */
892 if (group->variables) {
893 var = group->variables;
894
895 } else {
896 group->variables = var = talloc_zero(parent, unlang_variable_t);
897 if (!var) return -1;
898
899 var->dict = fr_dict_protocol_alloc(unlang_ctx->rules->attr.dict_def);
900 if (!var->dict) {
901 group->variables = NULL;
902 talloc_free(var);
903 return -1;
904 }
905 var->root = fr_dict_root(var->dict);
906
907 var->max_attr = 1;
908
909 /*
910 * Initialize the new rules, and point them to the parent rules.
911 *
912 * Then replace the parse rules with our rules, and our dictionary.
913 */
914 *t_rules = *unlang_ctx->rules;
915 t_rules->parent = unlang_ctx->rules;
916
917 t_rules->attr.dict_def = var->dict;
918 t_rules->attr.namespace = NULL;
919
920 unlang_ctx->rules = t_rules;
921 }
922
923 attr = cf_pair_attr(cp); /* data type */
924 value = cf_pair_value(cp); /* variable name */
925
927 if (type == FR_TYPE_NULL) {
929 cf_log_err(cp, "Invalid data type '%s'", attr);
930 return -1;
931 }
932
933 /*
934 * Leaf and group are OK. TLV, Vendor, Struct, VSA, etc. are not.
935 */
936 if (!(fr_type_is_leaf(type) || (type == FR_TYPE_GROUP))) goto invalid_type;
937
938 return unlang_define_local_variable(cf_pair_to_item(cp), var, t_rules, type, value, NULL);
939}
940
941/*
942 * Compile action && rcode for later use.
943 */
945{
946 int action;
947 char const *attr, *value;
948
949 attr = cf_pair_attr(cp);
950 value = cf_pair_value(cp);
951 if (!value) return true;
952
953 if (!strcasecmp(value, "return"))
954 action = MOD_ACTION_RETURN;
955
956 else if (!strcasecmp(value, "break"))
957 action = MOD_ACTION_RETURN;
958
959 else if (!strcasecmp(value, "reject"))
960 action = MOD_ACTION_REJECT;
961
962 else if (!strcasecmp(value, "retry"))
963 action = MOD_ACTION_RETRY;
964
965 else if (strspn(value, "0123456789") == strlen(value)) {
966 if (strlen(value) > 2) {
967 invalid_action:
968 cf_log_err(cp, "Priorities MUST be between 1 and 64.");
969 return false;
970 }
971
972 action = MOD_PRIORITY(atoi(value));
973
974 if (!MOD_ACTION_VALID_SET(action)) goto invalid_action;
975
976 } else {
977 cf_log_err(cp, "Unknown action '%s'.\n",
978 value);
979 return false;
980 }
981
982 if (strcasecmp(attr, "default") != 0) {
983 int rcode;
984
985 rcode = fr_table_value_by_str(mod_rcode_table, attr, -1);
986 if (rcode < 0) {
987 cf_log_err(cp,
988 "Unknown module rcode '%s'.",
989 attr);
990 return false;
991 }
992 actions->actions[rcode] = action;
993
994 } else { /* set all unset values to the default */
995 int i;
996
997 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
998 if (!actions->actions[i]) actions->actions[i] = action;
999 }
1000 }
1001
1002 return true;
1003}
1004
1006{
1007 CONF_ITEM *csi;
1008 CONF_SECTION *cs;
1009
1010 cs = cf_item_to_section(ci);
1011 for (csi=cf_item_next(cs, NULL);
1012 csi != NULL;
1013 csi=cf_item_next(cs, csi)) {
1014 CONF_PAIR *cp;
1015 char const *name, *value;
1016
1017 if (cf_item_is_section(csi)) {
1018 cf_log_err(csi, "Invalid subsection in 'retry' configuration.");
1019 return false;
1020 }
1021
1022 if (!cf_item_is_pair(csi)) continue;
1023
1024 cp = cf_item_to_pair(csi);
1025 name = cf_pair_attr(cp);
1026 value = cf_pair_value(cp);
1027
1028 if (!value) {
1029 cf_log_err(csi, "Retry configuration must specify a value");
1030 return false;
1031 }
1032
1033#define CLAMP(_name, _field, _limit) do { \
1034 if (!fr_time_delta_ispos(actions->retry._field)) { \
1035 cf_log_err(csi, "Invalid value for '" STRINGIFY(_name) " = %s' - value must be positive", \
1036 value); \
1037 return false; \
1038 } \
1039 if (fr_time_delta_cmp(actions->retry._field, fr_time_delta_from_sec(_limit)) > 0) { \
1040 cf_log_err(csi, "Invalid value for '" STRINGIFY(_name) " = %s' - value must be less than " STRINGIFY(_limit) "s", \
1041 value); \
1042 return false; \
1043 } \
1044 } while (0)
1045
1046 /*
1047 * We don't use conf_parser_t here for various
1048 * magical reasons.
1049 */
1050 if (strcmp(name, "initial_rtx_time") == 0) {
1051 if (fr_time_delta_from_str(&actions->retry.irt, value, strlen(value), FR_TIME_RES_SEC) < 0) {
1052 error:
1053 cf_log_err(csi, "Failed parsing '%s = %s' - %s",
1054 name, value, fr_strerror());
1055 return false;
1056 }
1057 CLAMP(initial_rtx_time, irt, 2);
1058
1059 } else if (strcmp(name, "max_rtx_time") == 0) {
1060 if (fr_time_delta_from_str(&actions->retry.mrt, value, strlen(value), FR_TIME_RES_SEC) < 0) goto error;
1061
1062 CLAMP(max_rtx_time, mrt, 10);
1063
1064 } else if (strcmp(name, "max_rtx_count") == 0) {
1065 char *end;
1066 unsigned long v = strtoul(value, &end, 10);
1067
1068 if (*end || (end == value) || (v > 10)) {
1069 cf_log_err(csi, "Invalid value for 'max_rtx_count = %s' - value must be between 0 and 10",
1070 value);
1071 return false;
1072 }
1073
1074 actions->retry.mrc = v;
1075
1076 } else if (strcmp(name, "max_rtx_duration") == 0) {
1077 if (fr_time_delta_from_str(&actions->retry.mrd, value, strlen(value), FR_TIME_RES_SEC) < 0) goto error;
1078
1079 CLAMP(max_rtx_duration, mrd, 20);
1080
1081 } else {
1082 cf_log_err(csi, "Invalid item '%s' in 'retry' configuration.", name);
1083 return false;
1084 }
1085 }
1086
1087 return true;
1088}
1089
1090bool unlang_compile_actions(unlang_mod_actions_t *actions, CONF_SECTION *action_cs, bool module_retry)
1091{
1092 int i;
1093 bool disallow_retry_action = false;
1094 CONF_ITEM *csi;
1095 CONF_SECTION *cs;
1096
1097 /*
1098 * Over-ride the default return codes of the module.
1099 */
1100 cs = cf_item_to_section(cf_section_to_item(action_cs));
1101 for (csi=cf_item_next(cs, NULL);
1102 csi != NULL;
1103 csi=cf_item_next(cs, csi)) {
1104 char const *name;
1105 CONF_PAIR *cp;
1106
1107 if (cf_item_is_section(csi)) {
1108 CONF_SECTION *subcs = cf_item_to_section(csi);
1109
1110 name = cf_section_name1(subcs);
1111
1112 /*
1113 * Look for a "retry" section.
1114 */
1115 if (name && (strcmp(name, "retry") == 0) && !cf_section_name2(subcs)) {
1116 if (!compile_retry_section(actions, csi)) return false;
1117 continue;
1118 }
1119
1120 cf_log_err(csi, "Invalid subsection. Expected 'action = value'");
1121 return false;
1122 }
1123
1124 if (!cf_item_is_pair(csi)) continue;
1125
1126 cp = cf_item_to_pair(csi);
1127
1128 /*
1129 * Allow 'retry = path.to.retry.config'
1130 */
1131 name = cf_pair_attr(cp);
1132 if (strcmp(name, "retry") == 0) {
1133 CONF_ITEM *subci;
1134 char const *value = cf_pair_value(cp);
1135
1136 if (!value) {
1137 cf_log_err(csi, "Missing reference string");
1138 return false;
1139 }
1140
1141 subci = cf_reference_item(cs, cf_root(cf_section_to_item(action_cs)), value);
1142 if (!subci) {
1143 cf_log_perr(csi, "Failed finding reference '%s'", value);
1144 return false;
1145 }
1146
1147 if (!compile_retry_section(actions, subci)) return false;
1148 continue;
1149 }
1150
1151 if (!compile_action_pair(actions, cp)) {
1152 return false;
1153 }
1154 }
1155
1156 if (module_retry) {
1157 if (!fr_time_delta_ispos(actions->retry.irt)) {
1158 cf_log_err(action_cs, "initial_rtx_time MUST be non-zero for modules which support retries.");
1159 return false;
1160 }
1161 } else {
1162 if (fr_time_delta_ispos(actions->retry.irt)) {
1163 cf_log_err(action_cs, "initial_rtx_time MUST be zero, as only max_rtx_count and max_rtx_duration are used.");
1164 return false;
1165 }
1166
1167 if (!actions->retry.mrc && !fr_time_delta_ispos(actions->retry.mrd)) {
1168 disallow_retry_action = true;
1169 }
1170 }
1171
1172 /*
1173 * Sanity check that "fail = retry", we actually have a
1174 * retry section.
1175 */
1176 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
1177 if (actions->actions[i] != MOD_ACTION_RETRY) continue;
1178
1179 if (module_retry) {
1180 cf_log_err(action_cs, "Cannot use a '%s = retry' action for a module which has its own retries",
1181 fr_table_str_by_value(mod_rcode_table, i, "<INVALID>"));
1182 return false;
1183 }
1184
1185 if (disallow_retry_action) {
1186 cf_log_err(action_cs, "max_rtx_count and max_rtx_duration cannot both be zero when using '%s = retry'",
1187 fr_table_str_by_value(mod_rcode_table, i, "<INVALID>"));
1188 return false;
1189 }
1190
1191 if (!fr_time_delta_ispos(actions->retry.irt) &&
1192 !actions->retry.mrc &&
1193 !fr_time_delta_ispos(actions->retry.mrd)) {
1194 cf_log_err(action_cs, "Cannot use a '%s = retry' action without a 'retry { ... }' section.",
1195 fr_table_str_by_value(mod_rcode_table, i, "<INVALID>"));
1196 return false;
1197 }
1198 }
1199
1200 return true;
1201}
1202
1204{
1205 unlang_group_t *g;
1206 unlang_t *c;
1207
1208 /*
1209 * If we're compiling an empty section, then the
1210 * *interpreter* type is GROUP, even if the *debug names*
1211 * are something else.
1212 */
1214 if (!g) return NULL;
1215
1217 if (!cs) {
1218 c->name = unlang_ops[type].name;
1219 c->debug_name = c->name;
1220
1221 } else {
1222 char const *name2;
1223
1224 name2 = cf_section_name2(cs);
1225 if (!name2) {
1226 c->name = cf_section_name1(cs);
1227 c->debug_name = c->name;
1228 } else {
1229 c->name = name2;
1230 c->debug_name = talloc_typed_asprintf(c, "%s %s", cf_section_name1(cs), name2);
1231 }
1232 }
1233
1234 return c;
1235}
1236
1237
1239
1240/*
1241 * compile 'actions { ... }' inside of another group.
1242 */
1244{
1245 CONF_ITEM *ci, *next;
1246
1247 ci = cf_section_to_item(subcs);
1248
1249 next = cf_item_next(cs, ci);
1250 if (next && (cf_item_is_pair(next) || cf_item_is_section(next))) {
1251 cf_log_err(ci, "'actions' MUST be the last block in a section");
1252 return false;
1253 }
1254
1255 if (cf_section_name2(subcs) != NULL) {
1256 cf_log_err(ci, "Invalid name for 'actions' section");
1257 return false;
1258 }
1259
1260 /*
1261 * Over-riding the actions can be done in certain limited
1262 * situations. In other situations (e.g. "redundant",
1263 * "load-balance"), it doesn't make sense.
1264 *
1265 * Note that this limitation also applies to "retry"
1266 * timers. We can do retries of a "group". We cannot do
1267 * retries of "load-balance", as the "load-balance"
1268 * section already takes care of redundancy.
1269 *
1270 * We may need to loosen that limitation in the future.
1271 */
1272 switch (c->type) {
1273 case UNLANG_TYPE_CASE:
1274 case UNLANG_TYPE_CATCH:
1275 case UNLANG_TYPE_IF:
1276 case UNLANG_TYPE_ELSE:
1277 case UNLANG_TYPE_ELSIF:
1279 case UNLANG_TYPE_GROUP:
1280 case UNLANG_TYPE_LIMIT:
1281 case UNLANG_TYPE_SWITCH:
1284 break;
1285
1286 default:
1287 cf_log_err(ci, "'actions' MUST NOT be in a '%s' block", unlang_ops[c->type].name);
1288 return false;
1289 }
1290
1291 return unlang_compile_actions(&c->actions, subcs, false);
1292}
1293
1294
1296{
1297 CONF_ITEM *ci = NULL;
1298 unlang_t *c, *single;
1299 bool was_if = false;
1300 char const *skip_else = NULL;
1302 unlang_compile_ctx_t unlang_ctx2;
1303 tmpl_rules_t t_rules, t2_rules; /* yes, it does */
1304
1306
1307 /*
1308 * Create our own compilation context which can be edited
1309 * by a variable definition.
1310 */
1311 unlang_compile_ctx_copy(&unlang_ctx2, unlang_ctx_in);
1312 t2_rules = *(unlang_ctx_in->rules);
1313
1314 unlang_ctx = &unlang_ctx2;
1315 unlang_ctx2.rules = &t2_rules;
1316
1317 t_rules = *unlang_ctx_in->rules;
1318
1319 /*
1320 * Loop over the children of this group.
1321 */
1322 while ((ci = cf_item_next(g->cs, ci))) {
1323 if (cf_item_is_data(ci)) continue;
1324
1325 t_rules.attr.ci = ci;
1326 t2_rules.attr.ci = ci;
1327
1328 /*
1329 * Sections are keywords, or references to
1330 * modules with updated return codes.
1331 */
1332 if (cf_item_is_section(ci)) {
1333 char const *name = NULL;
1334 CONF_SECTION *subcs = cf_item_to_section(ci);
1335
1336 /*
1337 * Skip precompiled blocks. This is
1338 * mainly for policies.
1339 */
1340 if (cf_data_find(subcs, unlang_group_t, NULL)) continue;
1341
1342 /*
1343 * "actions" apply to the current group.
1344 * It's not a subgroup.
1345 */
1346 name = cf_section_name1(subcs);
1347
1348 /*
1349 * In-line attribute editing. Nothing else in the parse has list assignments, so this must be it.
1350 */
1352 single = compile_edit_section(c, unlang_ctx, subcs);
1353 if (!single) {
1354 fail:
1355 talloc_free(c);
1356 return NULL;
1357 }
1358
1359 goto add_child;
1360 }
1361
1362 if (strcmp(name, "actions") == 0) {
1363 if (!compile_action_subsection(c, g->cs, subcs)) goto fail;
1364 continue;
1365 }
1366
1367 /*
1368 * Special checks for "else" and "elsif".
1369 */
1370 if ((strcmp(name, "else") == 0) || (strcmp(name, "elsif") == 0)) {
1371 /*
1372 * We ran into one without a preceding "if" or "elsif".
1373 * That's not allowed.
1374 */
1375 if (!was_if) {
1376 cf_log_err(ci, "Invalid location for '%s'. There is no preceding "
1377 "'if' or 'elsif' statement", name);
1378 goto fail;
1379 }
1380
1381 /*
1382 * There was a previous "if" or "elsif" which was always taken.
1383 * So we skip this "elsif" or "else".
1384 */
1385 if (skip_else) {
1386 void *ptr;
1387
1388 /*
1389 * And manually free this.
1390 */
1391 ptr = cf_data_remove(subcs, xlat_exp_head_t, NULL);
1392 talloc_free(ptr);
1393
1395
1396 cf_log_debug_prefix(ci, "Skipping contents of '%s' due to previous "
1397 "'%s' being always being taken.",
1398 name, skip_else);
1399 continue;
1400 }
1401 }
1402
1403 /*
1404 * Otherwise it's a real keyword.
1405 */
1406 single = compile_item(c, unlang_ctx, ci);
1407 if (!single) {
1408 cf_log_err(ci, "Failed to parse \"%s\" subsection", cf_section_name1(subcs));
1409 goto fail;
1410 }
1411
1412 goto add_child;
1413
1414 } else if (cf_item_is_pair(ci)) {
1415 CONF_PAIR *cp = cf_item_to_pair(ci);
1416
1417 /*
1418 * Variable definition.
1419 */
1420 if (cf_pair_operator(cp) == T_OP_CMP_TRUE) {
1421 if (compile_variable(c, unlang_ctx, cp, &t_rules) < 0) goto fail;
1422
1423 single = UNLANG_IGNORE;
1424 goto add_child;
1425 }
1426
1427 if (!cf_pair_value(cp)) {
1428 single = compile_item(c, unlang_ctx, ci);
1429 if (!single) {
1430 cf_log_err(ci, "Invalid keyword \"%s\".", cf_pair_attr(cp));
1431 goto fail;
1432 }
1433
1434 goto add_child;
1435 }
1436
1437 /*
1438 * What remains MUST be an edit pair. At this point, the state of the compiler
1439 * tells us what it is, and we don't really care if there's a leading '&'.
1440 */
1441 single = compile_edit_pair(c, unlang_ctx, cp);
1442 if (!single) goto fail;
1443
1444 goto add_child;
1445 } else {
1446 cf_log_err(ci, "Asked to compile unknown conf type");
1447 goto fail;
1448 }
1449
1450 add_child:
1451 if (single == UNLANG_IGNORE) continue;
1452
1453 /*
1454 * Do optimizations for "if" and "elsif"
1455 * conditions.
1456 */
1457 switch (single->type) {
1458 case UNLANG_TYPE_ELSIF:
1459 case UNLANG_TYPE_IF:
1460 was_if = true;
1461
1462 {
1463 unlang_group_t *f;
1464 unlang_cond_t *gext;
1465
1466 /*
1467 * Skip else, and/or omit things which will never be run.
1468 */
1469 f = unlang_generic_to_group(single);
1470 gext = unlang_group_to_cond(f);
1471
1472 if (gext->is_truthy) {
1473 if (gext->value) {
1474 skip_else = single->debug_name;
1475 } else {
1476 /*
1477 * The condition never
1478 * matches, so we can
1479 * avoid putting it into
1480 * the unlang tree.
1481 */
1482 talloc_free(single);
1483 continue;
1484 }
1485 }
1486 }
1487 break;
1488
1489 default:
1490 was_if = false;
1491 skip_else = NULL;
1492 break;
1493 }
1494
1495 /*
1496 * unlang_group_t is grown by adding a unlang_t to the end
1497 */
1498 fr_assert(g == talloc_parent(single));
1500 unlang_list_insert_tail(&g->children, single);
1501 single->list = &g->children;
1502
1503 /*
1504 * If it's not possible to execute statement
1505 * after the current one, then just stop
1506 * processing the children.
1507 */
1508 if (g->self.closed) {
1509 cf_log_warn(ci, "Skipping remaining instructions due to '%s'",
1510 single->name);
1511 break;
1512 }
1513 }
1514
1515 return c;
1516}
1517
1518
1519/*
1520 * Generic "compile a section with more unlang inside of it".
1521 */
1523{
1524 unlang_group_t *g;
1525 unlang_t *c;
1526 char const *name1, *name2;
1527
1528 fr_assert(unlang_ctx->rules != NULL);
1529 fr_assert(unlang_ctx->rules->attr.list_def);
1530
1531 /*
1532 * We always create a group, even if the section is empty.
1533 */
1535 if (!g) return NULL;
1536
1538
1539 /*
1540 * Remember the name for printing, etc.
1541 */
1542 name1 = cf_section_name1(cs);
1543 name2 = cf_section_name2(cs);
1544 c->name = name1;
1545
1546 /*
1547 * Make sure to tell the user that we're running a
1548 * policy, and not anything else.
1549 */
1550 if (type == UNLANG_TYPE_POLICY) {
1551 MEM(c->debug_name = talloc_typed_asprintf(c, "policy %s", name1));
1552
1553 } else if (!name2) {
1554 c->debug_name = c->name;
1555
1556 } else {
1557 MEM(c->debug_name = talloc_typed_asprintf(c, "%s %s", name1, name2));
1558 }
1559
1561}
1562
1563
1565{
1566 CONF_PAIR *cp = cf_item_to_pair(ci);
1567 unlang_t *c;
1568 unlang_tmpl_t *ut;
1569 ssize_t slen;
1570 char const *p = cf_pair_attr(cp);
1571 tmpl_t *vpt;
1572
1573 MEM(ut = talloc_zero(parent, unlang_tmpl_t));
1574 c = unlang_tmpl_to_generic(ut);
1576 c->name = p;
1577 c->debug_name = c->name;
1578 c->ci = CF_TO_ITEM(cp);
1579
1580 RULES_VERIFY(unlang_ctx->rules);
1581 slen = tmpl_afrom_substr(ut, &vpt,
1582 &FR_SBUFF_IN(p, talloc_strlen(p)),
1584 NULL,
1585 unlang_ctx->rules);
1586 if (!vpt) {
1587 cf_canonicalize_error(cp, slen, "Failed parsing expansion", p);
1588 talloc_free(ut);
1589 return NULL;
1590 }
1591 ut->tmpl = vpt; /* const issues */
1592
1593 return c;
1594}
1595
1596/*
1597 * redundant, load-balance and parallel have limits on what can
1598 * go in them.
1599 */
1601{
1602 CONF_ITEM *ci;
1603
1604 for (ci=cf_item_next(cs, NULL);
1605 ci != NULL;
1606 ci=cf_item_next(cs, ci)) {
1607 /*
1608 * If we're a redundant, etc. group, then the
1609 * intention is to call modules, rather than
1610 * processing logic. These checks aren't
1611 * *strictly* necessary, but they keep the users
1612 * from doing crazy things.
1613 */
1614 if (cf_item_is_section(ci)) {
1615 CONF_SECTION *subcs = cf_item_to_section(ci);
1616 char const *name1 = cf_section_name1(subcs);
1617
1618 /*
1619 * Allow almost anything except "else"
1620 * statements. The normal processing
1621 * falls through from "if" to "else", and
1622 * we can't do that for redundant and
1623 * load-balance sections.
1624 */
1625 if ((strcmp(name1, "else") == 0) ||
1626 (strcmp(name1, "elsif") == 0)) {
1627 cf_log_err(ci, "%s sections cannot contain a \"%s\" statement",
1628 name, name1);
1629 return false;
1630 }
1631 continue;
1632 }
1633
1634 if (cf_item_is_pair(ci)) {
1635 CONF_PAIR *cp = cf_item_to_pair(ci);
1636
1637 if (cf_pair_operator(cp) == T_OP_CMP_TRUE) continue;
1638
1639 if (cf_pair_value(cp) != NULL) {
1640 cf_log_err(cp, "Unknown keyword '%s', or invalid location", cf_pair_attr(cp));
1641 return false;
1642 }
1643 }
1644 }
1645
1646 return true;
1647}
1648
1649
1651 CONF_SECTION *subcs,
1652 bool policy)
1653{
1654 unlang_compile_ctx_t unlang_ctx2;
1655 unlang_t *c;
1656
1658
1659 /*
1660 * module.c takes care of ensuring that this is:
1661 *
1662 * group foo { ...
1663 * load-balance foo { ...
1664 * redundant foo { ...
1665 * redundant-load-balance foo { ...
1666 *
1667 * We can just recurse to compile the section as
1668 * if it was found here.
1669 */
1670 if (cf_section_name2(subcs)) {
1671 if (policy) {
1672 cf_log_err(subcs, "Unexpected second name in policy");
1673 return NULL;
1674 }
1675
1676 c = compile_item(parent, &unlang_ctx2, cf_section_to_item(subcs));
1677
1678 } else {
1679 /*
1680 * We have:
1681 *
1682 * foo { ...
1683 *
1684 * So we compile it like it was:
1685 *
1686 * group foo { ...
1687 */
1688 c = unlang_compile_section(parent, &unlang_ctx2, subcs,
1690 }
1691 if (!c) return NULL;
1693
1694 /*
1695 * Return the compiled thing if we can.
1696 */
1697 if (!cf_item_is_section(ci)) return c;
1698
1699 /*
1700 * Else we have a reference to a policy, and that reference
1701 * over-rides the return codes for the policy!
1702 */
1703 if (!unlang_compile_actions(&c->actions, cf_item_to_section(ci), false)) {
1704 talloc_free(c);
1705 return NULL;
1706 }
1707
1708 return c;
1709}
1710
1711/** Load a named module from the virtual module list, or from the "policy" subsection.
1712 *
1713 * If it's "foo.method", look for "foo", and return "method" as the method
1714 * we wish to use, instead of the input component.
1715 *
1716 * @param[in] ci Configuration item to check
1717 * @param[in] real_name Complete name string e.g. foo.authorize.
1718 * @param[in] virtual_name Virtual module name e.g. foo.
1719 * @param[in] method_name Method override (may be NULL) or the method
1720 * name e.g. authorize.
1721 * @param[in] unlang_ctx Unlang context this call is being compiled in.
1722 * @param[out] policy whether or not this thing was a policy
1723 * @return the CONF_SECTION specifying the virtual module.
1724 */
1725static CONF_SECTION *virtual_module_find_cs(CONF_ITEM *ci, UNUSED char const *real_name, char const *virtual_name,
1726 char const *method_name, unlang_compile_ctx_t *unlang_ctx, bool *policy)
1727{
1728 CONF_SECTION *cs, *subcs, *conf_root;
1729 CONF_ITEM *loop;
1730 char buffer[256];
1731
1732 *policy = false;
1733 conf_root = cf_root(ci);
1734
1735 /*
1736 * Look for "foo" as a virtual server. If we find it,
1737 * AND there's no method name, we've found the right
1738 * thing.
1739 *
1740 * Found "foo". Load it as "foo", or "foo.method".
1741 *
1742 * Return it to the caller, with the updated method.
1743 */
1744 subcs = module_rlm_virtual_by_name(virtual_name);
1745 if (subcs) goto check_for_loop;
1746
1747 /*
1748 * Look for it in "policy".
1749 *
1750 * If there's no policy section, we can't do anything else.
1751 */
1752 cs = cf_section_find(conf_root, "policy", NULL);
1753 if (!cs) return NULL;
1754
1755 *policy = true;
1756
1757 /*
1758 * "foo.authorize" means "load policy 'foo.authorize' or 'foo'"
1759 * as method "authorize".
1760 *
1761 * And bail out if there's no policy "foo.authorize" or "foo".
1762 */
1763 if (method_name) {
1764 snprintf(buffer, sizeof(buffer), "%s.%s", virtual_name, method_name);
1765 subcs = cf_section_find(cs, buffer, NULL);
1766 if (!subcs) subcs = cf_section_find(cs, virtual_name, NULL);
1767 if (!subcs) return NULL;
1768
1769 goto check_for_loop;
1770 }
1771
1772 /*
1773 * "foo" means "look for foo.name1.name2" first, to allow
1774 * method overrides. If that's not found, look for
1775 * "foo.name1" and if that's not found just look for
1776 * a policy "foo".
1777 */
1778 if (unlang_ctx->section_name2) {
1779 snprintf(buffer, sizeof(buffer), "%s.%s.%s", virtual_name, unlang_ctx->section_name1, unlang_ctx->section_name2);
1780 subcs = cf_section_find(cs, buffer, NULL);
1781 } else {
1782 subcs = NULL;
1783 }
1784
1785 if (!subcs) {
1786 snprintf(buffer, sizeof(buffer), "%s.%s", virtual_name, unlang_ctx->section_name1);
1787 subcs = cf_section_find(cs, buffer, NULL);
1788 }
1789
1790 if (!subcs) subcs = cf_section_find(cs, virtual_name, NULL);
1791 if (!subcs) return NULL;
1792
1793check_for_loop:
1794 /*
1795 * Check that we're not creating a loop. We may
1796 * be compiling an "sql" module reference inside
1797 * of an "sql" policy. If so, we allow the
1798 * second "sql" to refer to the module.
1799 */
1800 for (loop = cf_parent(ci);
1801 loop && subcs;
1802 loop = cf_parent(loop)) {
1803 if (loop == cf_section_to_item(subcs)) {
1804 return NULL;
1805 }
1806 }
1807
1808 return subcs;
1809}
1810
1812{
1813 unlang_t *c;
1814 unlang_module_t *m;
1815 fr_slen_t slen;
1816
1817 MEM(m = talloc_zero(parent, unlang_module_t));
1818 slen = module_rlm_by_name_and_method(m, &m->mmc,
1819 unlang_ctx->vs,
1820 &(section_name_t){ .name1 = unlang_ctx->section_name1, .name2 = unlang_ctx->section_name2 },
1822 unlang_ctx->rules);
1823 if (slen < 0) {
1824 cf_log_perr(ci, "Failed compiling module call");
1825 talloc_free(m);
1826 return NULL;
1827 }
1828
1829 /*
1830 * We parsed a string, but we were told to ignore it. Don't do anything.
1831 */
1832 if (!m->mmc.rlm) return UNLANG_IGNORE;
1833
1834 if (m->mmc.rlm->common.dict &&
1835 !fr_dict_compatible(*m->mmc.rlm->common.dict, unlang_ctx->rules->attr.dict_def)) {
1836 cf_log_err(ci, "The '%s' module can only be used within a '%s' namespace.",
1837 m->mmc.rlm->common.name, fr_dict_root(*m->mmc.rlm->common.dict)->name);
1838 cf_log_err(ci, "Please use the 'subrequest' keyword to change namespaces");
1839 cf_log_err(ci, DOC_KEYWORD_REF(subrequest));
1840 talloc_free(m);
1841 return NULL;
1842 }
1843
1846 c->name = talloc_strdup(c, name);
1847 c->debug_name = c->name;
1848 c->ci = ci;
1849
1850 /*
1851 * Set the default actions for this module.
1852 */
1853 c->actions = m->mmc.mi->actions;
1854
1855 /*
1856 * Parse the method environment for this module / method
1857 */
1858 if (m->mmc.mmb.method_env) {
1860
1861 fr_assert_msg(method_env->inst_size, "Method environment for module %s, method %s %s declared, "
1862 "but no inst_size set",
1863 m->mmc.mi->name, unlang_ctx->section_name1, unlang_ctx->section_name2);
1864
1865 if (!unlang_ctx->rules) {
1866 cf_log_err(ci, "Failed compiling %s - no rules", m->mmc.mi->name);
1867 goto error;
1868 }
1870 unlang_ctx->rules, m->mmc.mi->conf,
1871 &(call_env_ctx_t){
1872 .type = CALL_ENV_CTX_TYPE_MODULE,
1873 .mi = m->mmc.mi,
1874 .asked = &m->mmc.asked
1875 });
1876 if (!m->call_env) {
1877 error:
1878 talloc_free(m);
1879 return NULL;
1880 }
1881 }
1882
1883 /*
1884 * If a module reference is a section, then the section
1885 * should contain action over-rides. We add those here.
1886 */
1887 if (cf_item_is_section(ci) &&
1889 (m->mmc.mi->exported->flags & MODULE_TYPE_RETRY) != 0)) goto error;
1890
1891 return c;
1892}
1893
1894extern int dict_attr_acopy_children(fr_dict_t *dict, fr_dict_attr_t *dst, fr_dict_attr_t const *src);
1895
1896static inline CC_HINT(always_inline) unlang_op_t const *name_to_op(char const *name)
1897{
1898 unlang_op_t const *op;
1899
1900 op = fr_hash_table_find(unlang_op_table, &(unlang_op_t) { .name = name });
1901 if (op) return op;
1902
1903 return NULL;
1904}
1905
1907 fr_dict_attr_t const *ref)
1908{
1909 fr_dict_attr_t const *da;
1910 fr_slen_t len;
1911 unlang_op_t const *op;
1912
1913 fr_dict_attr_flags_t flags = {
1914 .internal = true,
1915 .local = true,
1916 };
1917
1918 /*
1919 * No overlap with list names.
1920 */
1922 fail_list:
1923 cf_log_err(ci, "Local variable '%s' cannot be a list reference.", name);
1924 return -1;
1925 }
1926
1927 len = strlen(name);
1928 if (tmpl_attr_list_from_substr(&da, &FR_SBUFF_IN(name, len)) == len) goto fail_list;
1929
1930 /*
1931 * No keyword section names.
1932 */
1933 op = name_to_op(name);
1934 if (op) {
1935 cf_log_err(ci, "Local variable '%s' cannot be an unlang keyword.", name);
1936 return -1;
1937 }
1938
1939 /*
1940 * No protocol names.
1941 */
1942 if (fr_dict_by_protocol_name(name) != NULL) {
1943 cf_log_err(ci, "Local variable '%s' cannot be an existing protocol name.", name);
1944 return -1;
1945 }
1946
1947 /*
1948 * No overlap with attributes in the current dictionary. The lookup in var->root will also check
1949 * the current dictionary, so the check here is really only for better error messages.
1950 */
1951 if (t_rules && t_rules->parent && t_rules->parent->attr.dict_def) {
1952 da = fr_dict_attr_by_name(NULL, fr_dict_root(t_rules->parent->attr.dict_def), name);
1953 if (da) {
1954 cf_log_err(ci, "Local variable '%s' duplicates a dictionary attribute.", name);
1955 return -1;
1956 }
1957 }
1958
1959 /*
1960 * No data types.
1961 */
1963 cf_log_err(ci, "Invalid variable name '%s'.", name);
1964 return -1;
1965 }
1966
1967 /*
1968 * No dups of local variables.
1969 */
1970 da = fr_dict_attr_by_name(NULL, var->root, name);
1971 if (da) {
1972 cf_log_err(ci, "Duplicate variable name '%s'.", name);
1973 return -1;
1974 }
1975
1976 if (fr_dict_attr_add(var->dict, var->root, name, var->max_attr, type, &flags) < 0) {
1977 fail:
1978 cf_log_err(ci, "Failed adding variable '%s' - %s", name, fr_strerror());
1979 return -1;
1980 }
1981 da = fr_dict_attr_by_name(NULL, var->root, name);
1982 fr_assert(da != NULL);
1983
1984 /*
1985 * Copy the children over.
1986 */
1988 fr_fatal_assert(ref != NULL);
1989
1990 if (fr_dict_attr_acopy_local(da, ref) < 0) goto fail;
1991 }
1992
1993 var->max_attr++;
1994
1995 return 0;
1996}
1997
1998/*
1999 * Compile one unlang instruction
2000 */
2002{
2003 char const *name, *p;
2004 CONF_SECTION *cs, *subcs, *modules;
2005 unlang_compile_ctx_t unlang_ctx2;
2006 bool policy;
2007 unlang_t *c;
2008 unlang_op_t const *op;
2009
2010 if (cf_item_is_section(ci)) {
2011 cs = cf_item_to_section(ci);
2012 name = cf_section_name1(cs);
2013 op = name_to_op(name);
2014
2015 if (op) {
2016 /*
2017 * Forbid pair keywords as section names,
2018 * e.g. "break { ... }"
2019 */
2020 if ((op->flag & UNLANG_OP_FLAG_SINGLE_WORD) != 0) {
2021 cf_log_err(ci, "Syntax error after keyword '%s' - unexpected '{'", name);
2022 return NULL;
2023 }
2024
2025 c = op->compile(parent, unlang_ctx, ci);
2026 goto allocate_number;
2027 }
2028
2029 /* else it's something like sql { fail = 1 ...} */
2030 goto check_for_module;
2031
2032 } else if (cf_item_is_pair(ci)) {
2033
2034 /*
2035 * Else it's a module reference such as "sql", OR
2036 * one of the few bare keywords that we allow.
2037 */
2038 CONF_PAIR *cp = cf_item_to_pair(ci);
2039
2040 /*
2041 * We cannot have assignments or actions here.
2042 */
2043 if (cf_pair_value(cp) != NULL) {
2044 cf_log_err(ci, "Invalid assignment");
2045 return NULL;
2046 }
2047
2048 name = cf_pair_attr(cp);
2049 op = name_to_op(name);
2050
2051 if (op) {
2052 /*
2053 * Forbid section keywords as pair names, e.g. "switch = foo"
2054 */
2055 if ((op->flag & UNLANG_OP_FLAG_SINGLE_WORD) == 0) {
2056 cf_log_err(ci, "Syntax error after keyword '%s' - missing '{'", name);
2057 return NULL;
2058 }
2059
2060 c = op->compile(parent, unlang_ctx, ci);
2061 goto allocate_number;
2062 }
2063
2064 /*
2065 * In-place expansions.
2066 *
2067 * @todo - allow only function calls, not %{...}
2068 *
2069 * @todo don't create a tmpl. Instead, create an
2070 * xlat. This functionality is needed for the in-place language functions via
2071 *
2072 * language {{{
2073 * ...
2074 * }}}
2075 */
2076 if (name[0] == '%') {
2078 goto allocate_number;
2079 }
2080
2081 goto check_for_module;
2082
2083 } else {
2084 cf_log_err(ci, "Asked to compile unknown configuration item");
2085 return NULL; /* who knows what it is... */
2086 }
2087
2088check_for_module:
2089 /*
2090 * We now have a name. It can be one of two forms. A
2091 * bare module name, or a section named for the module,
2092 * with over-rides for the return codes.
2093 *
2094 * The name can refer to a real module, in the "modules"
2095 * section. In that case, the name will be either the
2096 * first or second name of the sub-section of "modules".
2097 *
2098 * Or, the name can refer to a policy, in the "policy"
2099 * section. In that case, the name will be first of the
2100 * sub-section of "policy".
2101 *
2102 * Or, the name can refer to a "module.method", in which
2103 * case we're calling a different method than normal for
2104 * this section.
2105 *
2106 * Or, the name can refer to a virtual module, in the
2107 * "modules" section. In that case, the name will be
2108 * name2 of the CONF_SECTION.
2109 *
2110 * We try these in sequence, from the bottom up. This is
2111 * so that virtual modules and things in "policy" can
2112 * over-ride calls to real modules.
2113 */
2114
2115
2116 /*
2117 * Try:
2118 *
2119 * policy { ... name { .. } .. }
2120 * policy { ... name.method { .. } .. }
2121 */
2122 p = strrchr(name, '.');
2123 if (!p) {
2124 subcs = virtual_module_find_cs(ci, name, name, NULL, unlang_ctx, &policy);
2125 } else {
2126 char buffer[256];
2127
2128 strlcpy(buffer, name, sizeof(buffer));
2129 buffer[p - name] = '\0';
2130
2131 subcs = virtual_module_find_cs(ci, name,
2132 buffer, buffer + (p - name) + 1, unlang_ctx, &policy);
2133 }
2134
2135 /*
2136 * We've found the thing which defines this "function".
2137 * It MUST be a sub-section.
2138 *
2139 * i.e. it refers to a a subsection in "policy".
2140 */
2141 if (subcs) {
2142 c = compile_function(parent, unlang_ctx, ci, subcs, policy);
2143 goto allocate_number;
2144 }
2145
2146 /*
2147 * Not a function. It must be a real module.
2148 */
2149 modules = cf_section_find(cf_root(ci), "modules", NULL);
2150 if (!modules) {
2151 cf_log_err(ci, "Failed compiling \"%s\" as a module or policy as no modules are enabled", name);
2152 cf_log_err(ci, "Please verify that modules { ... } section is present in the server configuration");
2153 return NULL;
2154 }
2155
2156 /*
2157 * Set the child compilation context BEFORE parsing the
2158 * module name and method. The lookup function will take
2159 * care of returning the appropriate component, name1,
2160 * name2, etc.
2161 */
2163 c = compile_module(parent, &unlang_ctx2, ci, name);
2164
2165allocate_number:
2166 if (!c) return NULL;
2167 if (c == UNLANG_IGNORE) return UNLANG_IGNORE;
2168
2169 c->number = unlang_number++;
2171
2172 /*
2173 * Only insert the per-thread allocation && instantiation if it's used.
2174 */
2175 op = &unlang_ops[c->type];
2176 if (!op->thread_inst_size) return c;
2177
2179 cf_log_err(ci, "Instruction \"%s\" number %u has conflict with previous one.",
2180 c->debug_name, c->number);
2181 talloc_free(c);
2182 return NULL;
2183 }
2184
2185 return c;
2186}
2187
2188/** Compile an unlang section for a virtual server
2189 *
2190 * @param[in] vs Virtual server to compile section for.
2191 * @param[in] cs containing the unlang calls to compile.
2192 * @param[in] actions Actions to use for the unlang section.
2193 * @param[in] rules Rules to use for the unlang section.
2194 * @param[out] instruction Pointer to store the compiled unlang section.
2195 * @return
2196 * - 0 on success.
2197 * - -1 on error.
2198 */
2200 CONF_SECTION *cs, unlang_mod_actions_t const *actions, tmpl_rules_t const *rules, void **instruction)
2201{
2202 unlang_t *c;
2203 char const *name1, *name2;
2204 CONF_DATA const *cd;
2205
2206 /*
2207 * Don't compile it twice, and don't print out debug
2208 * messages twice.
2209 */
2210 cd = cf_data_find(cs, unlang_group_t, NULL);
2211 if (cd) {
2212 if (instruction) *instruction = cf_data_value(cd);
2213 return 1;
2214 }
2215
2216 /*
2217 * Ensure that all compile functions get valid rules.
2218 */
2219 if (!rules) {
2220 cf_log_err(cs, "Failed compiling section - no namespace rules passed");
2221 return -1;
2222 }
2223
2224 name1 = cf_section_name1(cs);
2225 name2 = cf_section_name2(cs);
2226
2227 if (!name2) name2 = "";
2228
2229 cf_log_debug(cs, "Compiling policies in - %s %s {...}", name1, name2);
2230
2231 c = unlang_compile_section(NULL,
2233 .vs = vs,
2234 .section_name1 = cf_section_name1(cs),
2235 .section_name2 = cf_section_name2(cs),
2236 .actions = *actions,
2237 .rules = rules
2238 },
2239 cs, UNLANG_TYPE_GROUP);
2240 if (!c) return -1;
2241
2243
2244 if (DEBUG_ENABLED4) unlang_dump(c, 2);
2245
2246 /*
2247 * Associate the unlang with the configuration section,
2248 * and free the unlang code when the configuration
2249 * section is freed.
2250 */
2251 cf_data_add(cs, c, NULL, true);
2253 if (instruction) *instruction = c;
2254
2255 return 0;
2256}
2257
2258
2259/** Check if name is an unlang keyword
2260 *
2261 * @param[in] name to check.
2262 * @return
2263 * - true if it is a keyword.
2264 * - false if it's not a keyword.
2265 */
2267{
2268 if (!name || !*name) return false;
2269
2270 return (name_to_op(name) != 0);
2271}
2272
2273/*
2274 * These are really unlang_foo_t, but that's fine...
2275 */
2276static int8_t instruction_cmp(void const *one, void const *two)
2277{
2278 unlang_t const *a = one;
2279 unlang_t const *b = two;
2280
2281 return CMP(a->number, b->number);
2282}
2283
2284
2285void unlang_compile_init(TALLOC_CTX *ctx)
2286{
2288}
2289
2290
2291/** Create thread-specific data structures for unlang
2292 *
2293 */
2294int unlang_thread_instantiate(TALLOC_CTX *ctx)
2295{
2297 unlang_t *instruction;
2298
2299 if (unlang_thread_array) {
2300 fr_strerror_const("already initialized");
2301 return -1;
2302 }
2303
2304 MEM(unlang_thread_array = talloc_zero_array(ctx, unlang_thread_t, unlang_number + 1));
2305// talloc_set_destructor(unlang_thread_array, _unlang_thread_array_free);
2306
2307 /*
2308 * Instantiate each instruction with thread-specific data.
2309 */
2310 for (instruction = fr_rb_iter_init_inorder(unlang_instruction_tree, &iter);
2311 instruction;
2312 instruction = fr_rb_iter_next_inorder(unlang_instruction_tree, &iter)) {
2313 unlang_op_t *op;
2314
2315 unlang_thread_array[instruction->number].instruction = instruction;
2316
2317 op = &unlang_ops[instruction->type];
2318
2320
2321 /*
2322 * Allocate any thread-specific instance data.
2323 */
2324 MEM(unlang_thread_array[instruction->number].thread_inst = talloc_zero_array(unlang_thread_array, uint8_t, op->thread_inst_size));
2325 talloc_set_name_const(unlang_thread_array[instruction->number].thread_inst, op->thread_inst_type);
2326
2327 if (op->thread_instantiate && (op->thread_instantiate(instruction, unlang_thread_array[instruction->number].thread_inst) < 0)) {
2328 return -1;
2329 }
2330 }
2331
2332 return 0;
2333}
2334
2335/** Get the thread-instance data for an instruction.
2336 *
2337 * @param[in] instruction the instruction to use
2338 * @return a pointer to thread-local data
2339 */
2340void *unlang_thread_instance(unlang_t const *instruction)
2341{
2342 if (!instruction->number || !unlang_thread_array) return NULL;
2343
2344 fr_assert(instruction->number <= unlang_number);
2345
2346 return unlang_thread_array[instruction->number].thread_inst;
2347}
2348
2349#ifdef WITH_PERF
2351{
2352 unlang_thread_t *t;
2353 fr_time_t now;
2354 unlang_t const *instruction = frame->instruction;
2355
2356 if (!instruction->number || !unlang_thread_array) return;
2357
2358 fr_assert(instruction->number <= unlang_number);
2359
2360 t = &unlang_thread_array[instruction->number];
2361
2362 t->use_count++;
2363 t->yielded++; // everything starts off as yielded
2364 now = fr_time();
2365
2366 fr_time_tracking_start(NULL, &frame->tracking, now);
2367 fr_time_tracking_yield(&frame->tracking, fr_time());
2368}
2369
2371{
2372 unlang_t const *instruction = frame->instruction;
2373 unlang_thread_t *t;
2374
2375 if (!instruction->number || !unlang_thread_array) return;
2376
2377 t = &unlang_thread_array[instruction->number];
2378 t->yielded++;
2379 t->running--;
2380
2381 fr_time_tracking_yield(&frame->tracking, fr_time());
2382}
2383
2385{
2386 unlang_t const *instruction = frame->instruction;
2387 unlang_thread_t *t;
2388
2389 if (!instruction->number || !unlang_thread_array) return;
2390
2391 if (frame->tracking.state != FR_TIME_TRACKING_YIELDED) return;
2392
2393 t = &unlang_thread_array[instruction->number];
2394 t->running++;
2395 t->yielded--;
2396
2397 fr_time_tracking_resume(&frame->tracking, fr_time());
2398}
2399
2401{
2402 unlang_t const *instruction = frame->instruction;
2403 unlang_thread_t *t;
2404
2405 if (!instruction || !instruction->number || !unlang_thread_array) return;
2406
2407 fr_assert(instruction->number <= unlang_number);
2408
2409 t = &unlang_thread_array[instruction->number];
2410
2411 if (frame->tracking.state == FR_TIME_TRACKING_YIELDED) {
2412 t->yielded--;
2413 fr_time_tracking_resume(&frame->tracking, fr_time());
2414 } else {
2415 t->running--;
2416 }
2417
2418 fr_time_tracking_end(NULL, &frame->tracking, fr_time());
2419 t->tracking.running_total = fr_time_delta_add(t->tracking.running_total, frame->tracking.running_total);
2420 t->tracking.waiting_total = fr_time_delta_add(t->tracking.waiting_total, frame->tracking.waiting_total);
2421}
2422
2423
2424static void unlang_perf_dump(fr_log_t *log, unlang_t const *instruction, int depth)
2425{
2426 unlang_group_t const *g;
2427 unlang_thread_t *t;
2428 char const *file;
2429 int line;
2430
2431 if (!instruction || !instruction->number) return;
2432
2433 /*
2434 * Ignore any non-group instruction.
2435 */
2436 if (!((instruction->type > UNLANG_TYPE_MODULE) && (instruction->type <= UNLANG_TYPE_POLICY))) return;
2437
2438 /*
2439 * Everything else is an unlang_group_t;
2440 */
2441 g = unlang_generic_to_group(instruction);
2442
2443 if (!g->cs) return;
2444
2445 file = cf_filename(g->cs);
2446 line = cf_lineno(g->cs);
2447
2448 if (depth) {
2449 fr_log(log, L_DBG, file, line, "%.*s", depth, unlang_spaces);
2450 }
2451
2452 if (debug_braces(instruction->type)) {
2453 fr_log(log, L_DBG, file, line, "%s { #", instruction->debug_name);
2454 } else {
2455 fr_log(log, L_DBG, file, line, "%s #", instruction->debug_name);
2456 }
2457
2458 t = &unlang_thread_array[instruction->number];
2459
2460 fr_log(log, L_DBG, file, line, "count=%" PRIu64 " cpu_time=%" PRId64 " yielded_time=%" PRId64 ,
2461 t->use_count, fr_time_delta_unwrap(t->tracking.running_total), fr_time_delta_unwrap(t->tracking.waiting_total));
2462
2463 if (!unlang_list_empty(&g->children)) {
2464 unlang_list_foreach(&g->children, child) {
2465 unlang_perf_dump(log, child, depth + 1);
2466 }
2467 }
2468
2469 if (debug_braces(instruction->type)) {
2470 if (depth) {
2471 fr_log(log, L_DBG, file, line, "%.*s", depth, unlang_spaces);
2472 }
2473
2474 fr_log(log, L_DBG, file, line, "}");
2475 }
2476}
2477
2478void unlang_perf_virtual_server(fr_log_t *log, char const *name)
2479{
2480
2482 CONF_SECTION *cs;
2483 CONF_ITEM *ci;
2484 char const *file;
2485 int line;
2486
2487 if (!vs) return;
2488
2489 cs = virtual_server_cs(vs);
2490
2491 file = cf_filename(cs);
2492 line = cf_lineno(cs);
2493
2494 fr_log(log, L_DBG, file, line, " server %s {\n", name);
2495
2496 /*
2497 * Loop over the children of the virtual server, checking for unlang_t;
2498 */
2499 for (ci = cf_item_next(cs, NULL);
2500 ci != NULL;
2501 ci = cf_item_next(cs, ci)) {
2502 char const *name1, *name2;
2503 unlang_t *instruction;
2504 CONF_SECTION *subcs;
2505
2506 if (!cf_item_is_section(ci)) continue;
2507
2508 instruction = (unlang_t *)cf_data_value(cf_data_find(ci, unlang_group_t, NULL));
2509 if (!instruction) continue;
2510
2511 subcs = cf_item_to_section(ci);
2512 name1 = cf_section_name1(subcs);
2513 name2 = cf_section_name2(subcs);
2514 file = cf_filename(ci);
2515 line = cf_lineno(ci);
2516
2517 if (!name2) {
2518 fr_log(log, L_DBG, file, line, " %s {\n", name1);
2519 } else {
2520 fr_log(log, L_DBG, file, line, " %s %s {\n", name1, name2);
2521 }
2522
2523 unlang_perf_dump(log, instruction, 2);
2524
2525 fr_log(log, L_DBG, file, line, " }\n");
2526 }
2527
2528 fr_log(log, L_DBG, file, line, "}\n");
2529}
2530#endif
static int const char char buffer[256]
Definition acutest.h:578
int const char * file
Definition acutest.h:704
int const char int line
Definition acutest.h:704
#define RCSID(id)
Definition build.h:487
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:112
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
static int invalid_type(fr_type_t type)
Definition calc.c:698
call_env_t * call_env_alloc(TALLOC_CTX *ctx, char const *name, call_env_method_t const *call_env_method, tmpl_rules_t const *t_rules, CONF_SECTION *cs, call_env_ctx_t const *cec)
Given a call_env_method, parse all call_env_pair_t in the context of a specific call to an xlat or mo...
Definition call_env.c:802
size_t inst_size
Size of per call env.
Definition call_env.h:245
#define RULES_VERIFY(_cs, _rules)
Definition cf_file.c:180
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
Definition cf_file.c:3832
Internal data that is associated with a configuration section.
Definition cf_priv.h:125
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:72
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition cf_util.c:631
fr_token_t cf_pair_attr_quote(CONF_PAIR const *pair)
Return the value (lhs) quoting of a pair.
Definition cf_util.c:1603
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1743
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1027
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:683
bool cf_item_is_data(CONF_ITEM const *ci)
Determine if CONF_ITEM is CONF_DATA.
Definition cf_util.c:645
fr_token_t cf_pair_operator(CONF_PAIR const *pair)
Return the operator of a pair.
Definition cf_util.c:1588
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:617
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1229
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1574
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
Definition cf_util.c:721
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1558
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:288
#define cf_lineno(_cf)
Definition cf_util.h:104
#define cf_data_add(_cf, _data, _name, _free)
Definition cf_util.h:254
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:243
#define cf_log_debug_prefix(_cf, _fmt,...)
Definition cf_util.h:305
#define cf_data_remove(_cf, _type, _name)
Remove an item from a parent by type and name.
Definition cf_util.h:266
#define cf_root(_cf)
Definition cf_util.h:98
#define cf_section_free_children(_x)
Definition cf_util.h:199
#define cf_parent(_cf)
Definition cf_util.h:101
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:366
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:295
#define CF_TO_ITEM(_cf)
Auto cast from the input type to CONF_ITEM (which is the base type)
Definition cf_util.h:65
#define cf_filename(_cf)
Definition cf_util.h:107
#define cf_log_warn(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_log_debug(_cf, _fmt,...)
Definition cf_util.h:291
#define cf_item_mark_parsed(_cf)
Definition cf_util.h:136
unlang_t * unlang_compile_children(unlang_group_t *g, unlang_compile_ctx_t *unlang_ctx_in)
Definition compile.c:1295
static unlang_t * compile_module(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM *ci, char const *name)
Definition compile.c:1811
static unlang_t * compile_edit_pair(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_PAIR *cp)
Compile one edit pair.
Definition compile.c:767
bool unlang_compile_is_keyword(const char *name)
Check if name is an unlang keyword.
Definition compile.c:2266
void unlang_compile_init(TALLOC_CTX *ctx)
Definition compile.c:2285
static bool compile_action_pair(unlang_mod_actions_t *actions, CONF_PAIR *cp)
Definition compile.c:944
unlang_t * unlang_compile_section(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:1522
bool unlang_compile_actions(unlang_mod_actions_t *actions, CONF_SECTION *action_cs, bool module_retry)
Definition compile.c:1090
bool pass2_fixup_map_rhs(unlang_group_t *g, tmpl_rules_t const *rules)
Definition compile.c:214
bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
Definition compile.c:95
static unlang_t * compile_edit_section(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs)
Compile one edit section.
Definition compile.c:612
bool unlang_compile_limit_subsection(CONF_SECTION *cs, char const *name)
Definition compile.c:1600
int unlang_thread_instantiate(TALLOC_CTX *ctx)
Create thread-specific data structures for unlang.
Definition compile.c:2294
static void unlang_dump(unlang_t *c, int depth)
Definition compile.c:242
static int compile_variable(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_PAIR *cp, tmpl_rules_t *t_rules)
Compile a variable definition.
Definition compile.c:855
static char const unlang_spaces[]
Definition compile.c:93
#define UPDATE_CTX2
Definition compile.c:90
static int8_t instruction_cmp(void const *one, void const *two)
Definition compile.c:2276
int unlang_fixup_update(map_t *map, void *ctx)
Validate and fixup a map that's part of an update section.
Definition compile.c:352
static bool compile_retry_section(unlang_mod_actions_t *actions, CONF_ITEM *ci)
Definition compile.c:1005
static CONF_SECTION * virtual_module_find_cs(CONF_ITEM *ci, UNUSED char const *real_name, char const *virtual_name, char const *method_name, unlang_compile_ctx_t *unlang_ctx, bool *policy)
Load a named module from the virtual module list, or from the "policy" subsection.
Definition compile.c:1725
static unlang_t * compile_item(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM *ci)
Definition compile.c:2001
static const bool edit_list_sub_op[T_TOKEN_LAST]
Definition compile.c:514
size_t mod_rcode_table_len
Definition compile.c:88
static unlang_t * compile_tmpl(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM *ci)
Definition compile.c:1564
static void compile_set_default_actions(unlang_t *c, unlang_compile_ctx_t *unlang_ctx)
Update a compiled unlang_t with the default actions.
Definition compile.c:490
static unsigned int unlang_number
Definition compile.c:57
unlang_t * unlang_compile_empty(unlang_t *parent, UNUSED unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:1203
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:457
#define debug_braces(_type)
Definition compile.c:848
bool pass2_fixup_map(map_t *map, tmpl_rules_t const *rules, fr_dict_attr_t const *parent)
Fixup ONE map (recursively)
Definition compile.c:123
static fr_rb_tree_t * unlang_instruction_tree
Definition compile.c:71
bool pass2_fixup_update(unlang_group_t *g, tmpl_rules_t const *rules)
Definition compile.c:187
static unlang_t * compile_function(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM *ci, CONF_SECTION *subcs, bool policy)
Definition compile.c:1650
int dict_attr_acopy_children(fr_dict_t *dict, fr_dict_attr_t *dst, fr_dict_attr_t const *src)
Copy the children of an existing attribute.
Definition dict_util.c:1192
fr_table_num_sorted_t const mod_rcode_table[]
Definition compile.c:75
void * unlang_thread_instance(unlang_t const *instruction)
Get the thread-instance data for an instruction.
Definition compile.c:2340
static int unlang_fixup_edit(map_t *map, void *ctx)
Validate and fixup a map that's part of an edit section.
Definition compile.c:535
int unlang_compile(virtual_server_t const *vs, CONF_SECTION *cs, unlang_mod_actions_t const *actions, tmpl_rules_t const *rules, void **instruction)
Compile an unlang section for a virtual server.
Definition compile.c:2199
static _Thread_local unlang_thread_t * unlang_thread_array
Definition compile.c:64
static unlang_op_t const * name_to_op(char const *name)
Definition compile.c:1896
#define CLAMP(_name, _field, _limit)
#define T(_x)
Definition compile.c:512
int unlang_define_local_variable(CONF_ITEM *ci, unlang_variable_t *var, tmpl_rules_t *t_rules, fr_type_t type, char const *name, fr_dict_attr_t const *ref)
Definition compile.c:1906
static bool compile_action_subsection(unlang_t *c, CONF_SECTION *cs, CONF_SECTION *subcs)
Definition compile.c:1243
static unlang_cond_t * unlang_group_to_cond(unlang_group_t *g)
Cast a group structure to the cond keyword extension.
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:202
#define fr_fatal_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:159
#define MEM(x)
Definition debug.h:36
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
int fr_dict_attr_acopy_local(fr_dict_attr_t const *dst, fr_dict_attr_t const *src)
Definition dict_util.c:1130
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3529
bool fr_dict_compatible(fr_dict_t const *dict1, fr_dict_t const *dict2)
See if two dictionaries have the same end parent.
Definition dict_util.c:2885
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2666
unsigned int internal
Internal attribute, should not be received in protocol packets, should not be encoded.
Definition dict.h:90
fr_dict_t const * fr_dict_by_protocol_name(char const *name)
Lookup a protocol by its name.
Definition dict_util.c:2843
int fr_dict_attr_add(fr_dict_t *dict, fr_dict_attr_t const *parent, char const *name, unsigned int attr, fr_type_t type, fr_dict_attr_flags_t const *flags))
Add an attribute to the dictionary.
Definition dict_util.c:1968
fr_dict_t * fr_dict_protocol_alloc(fr_dict_t const *parent)
Allocate a new local dictionary.
Definition dict_util.c:4267
Values of the encryption flags.
Test enumeration values.
Definition dict_test.h:92
map_list_t maps
Head of the map list.
Definition edit_priv.h:33
static unlang_t * unlang_edit_to_generic(unlang_edit_t const *p)
Definition edit_priv.h:45
static unlang_edit_t * unlang_generic_to_edit(unlang_t const *p)
Cast a generic structure to the edit extension.
Definition edit_priv.h:39
void * fr_hash_table_find(fr_hash_table_t *ht, void const *data)
Find data in a hash table.
Definition hash.c:450
talloc_free(hp)
#define DEBUG_ENABLED4
True if global debug level 1-3 messages are enabled.
Definition log.h:260
int map_afrom_cs_edit(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules, map_validate_t validate, void *uctx, unsigned int max)
Convert a config section into an attribute map for editing.
Definition map.c:1170
int map_afrom_cp(TALLOC_CTX *ctx, map_t **out, map_t *parent, CONF_PAIR *cp, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *input_rhs_rules, bool edit)
Convert CONFIG_PAIR (which may contain refs) to map_t.
Definition map.c:110
int map_list_afrom_cs(TALLOC_CTX *ctx, map_list_t *out, CONF_SECTION *cs, tmpl_rules_t const *t_rules, map_validate_t validate, void *uctx, unsigned int max)
Convert a config section into a list of { a, b, c, d, ... }.
Definition map.c:1355
ssize_t map_print(fr_sbuff_t *out, map_t const *map)
Print a map to a string.
Definition map.c:2407
unlang_op_t unlang_ops[UNLANG_TYPE_MAX]
Different operations the interpreter can execute.
Definition base.c:31
static TALLOC_CTX * unlang_ctx
Definition base.c:71
fr_hash_table_t * unlang_op_table
Definition base.c:45
#define fr_time()
Definition event.c:60
void fr_log(fr_log_t const *log, fr_log_type_t type, char const *file, int line, char const *fmt,...)
Send a server log message to its destination.
Definition log.c:577
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:59
static unlang_map_t * unlang_group_to_map(unlang_group_t *g)
Cast a group structure to the map keyword extension.
Definition map_priv.h:41
map_list_t map
Head of the map list.
Definition map_priv.h:34
tmpl_t * vpt
Definition map_priv.h:33
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
int strcasecmp(char *s1, char *s2)
Definition missing.c:66
Unlang module actions.
@ MOD_ACTION_RETURN
stop processing the section, and return the rcode with unset priority
Definition mod_action.h:41
@ MOD_ACTION_REJECT
change the rcode to REJECT, with unset priority
Definition mod_action.h:40
@ MOD_ACTION_RETRY
retry the instruction, MUST also set a retry config
Definition mod_action.h:39
#define MOD_ACTION_VALID_SET(_x)
Definition mod_action.h:66
fr_retry_config_t retry
Definition mod_action.h:70
#define MOD_PRIORITY(_x)
Definition mod_action.h:34
unlang_mod_action_t actions[RLM_MODULE_NUMCODES]
Definition mod_action.h:69
Declarations for the unlang module interface.
static unlang_t * unlang_module_to_generic(unlang_module_t *p)
Definition module_priv.h:92
static unlang_module_t * unlang_generic_to_module(unlang_t const *p)
Definition module_priv.h:86
module_method_call_t mmc
Everything needed to call a module method.
Definition module_priv.h:38
unlang_t self
Common fields in all unlang_t tree nodes.
Definition module_priv.h:36
call_env_t const * call_env
The per call parsed call environment.
Definition module_priv.h:37
A call to a module method.
Definition module_priv.h:35
fr_slen_t module_rlm_by_name_and_method(TALLOC_CTX *ctx, module_method_call_t *mmc_out, virtual_server_t const *vs, section_name_t const *section, fr_sbuff_t *name, tmpl_rules_t const *t_rules)
Find an existing module instance and verify it implements the specified method.
Definition module_rlm.c:563
CONF_SECTION * module_rlm_virtual_by_name(char const *asked_name)
Definition module_rlm.c:807
module_instance_t * mi
The process modules also push module calls onto the stack for execution.
Definition module_rlm.h:63
module_t common
Common fields presented by all modules.
Definition module_rlm.h:39
module_method_binding_t mmb
Method we're calling.
Definition module_rlm.h:70
module_rlm_t const * rlm
Cached module_rlm_t.
Definition module_rlm.h:66
Declarations for the unlang "parallel" keyword.
#define fr_assert(_expr)
Definition rad_assert.h:38
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:824
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
void * fr_rb_iter_next_inorder(UNUSED fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:850
#define fr_rb_alloc(_ctx, _data_cmp, _data_free)
Allocs a red black tree.
Definition rb.h:223
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:321
The main red black tree structure.
Definition rb.h:73
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:51
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:52
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:47
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:56
@ RLM_MODULE_NOTFOUND
User not found.
Definition rcode.h:53
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:55
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:45
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:54
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:57
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:50
static const call_env_method_t method_env
Definition rlm_detail.c:480
static char const * name
#define FR_SBUFF_IN(_start, _len_or_end)
#define FR_SBUFF_IN_STR(_start)
#define FR_SBUFF_OUT(_start, _len_or_end)
Section name identifier.
Definition section.h:44
char const * name
Instance name e.g. user_database.
Definition module.h:355
@ MODULE_TYPE_RETRY
can handle retries
Definition module.h:51
module_flags_t flags
Flags that control how a module starts up and how a module is called.
Definition module.h:236
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:349
unlang_mod_actions_t actions
default actions and retries.
Definition module.h:323
fr_dict_t const ** dict
required dictionary for this module.
Definition module.h:207
call_env_method_t const * method_env
Method specific call_env.
Definition module.h:178
module_t * exported
Public module structure.
Definition module.h:296
static int16_t tmpl_attr_tail_num(tmpl_t const *vpt)
Return the last attribute reference's attribute number.
Definition tmpl.h:885
#define TMPL_VERIFY(_vpt)
Definition tmpl.h:961
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition tmpl.h:638
#define tmpl_is_xlat(vpt)
Definition tmpl.h:210
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules))
Attempt to resolve functions and attributes in xlats and attribute references.
fr_table_num_sorted_t const tmpl_request_ref_table[]
Map keywords to tmpl_request_ref_t values.
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define NUM_ALL
Definition tmpl.h:395
tmpl_rules_t const * parent
for parent / child relationships
Definition tmpl.h:337
ssize_t tmpl_afrom_attr_str(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, char const *name, tmpl_rules_t const *rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
@ TMPL_TYPE_XLAT
Pre-parsed xlat expansion.
Definition tmpl.h:146
@ TMPL_TYPE_EXEC
Callout to an external script or program.
Definition tmpl.h:150
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:138
@ TMPL_TYPE_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:179
@ TMPL_TYPE_XLAT_UNRESOLVED
A xlat expansion with unresolved xlat functions or attribute references.
Definition tmpl.h:193
static bool tmpl_attr_tail_da_is_leaf(tmpl_t const *vpt)
Return true if the the last attribute reference is a leaf attribute.
Definition tmpl.h:817
#define NUM_COUNT
Definition tmpl.h:396
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
int tmpl_cast_in_place(tmpl_t *vpt, fr_type_t type, fr_dict_attr_t const *enumv))
Convert tmpl_t of type TMPL_TYPE_DATA_UNRESOLVED or TMPL_TYPE_DATA to TMPL_TYPE_DATA of type specifie...
static fr_slen_t vpt
Definition tmpl.h:1269
#define NUM_UNSPEC
Definition tmpl.h:394
static size_t tmpl_attr_num_elements(tmpl_t const *vpt)
The number of attribute references contained within a tmpl.
Definition tmpl.h:896
void tmpl_attr_rewrite_leaf_num(tmpl_t *vpt, int16_t num)
Rewrite the leaf's instance number.
#define tmpl_is_data_unresolved(vpt)
Definition tmpl.h:217
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:339
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
@ REQUEST_UNKNOWN
Unknown request.
Definition tmpl.h:97
#define tmpl_is_regex_xlat_unresolved(vpt)
Definition tmpl.h:221
void tmpl_set_dict_def(tmpl_t *vpt, fr_dict_t const *dict)
Change the default dictionary in the tmpl's resolution rules.
fr_slen_t tmpl_attr_list_from_substr(fr_dict_attr_t const **da_p, fr_sbuff_t *in)
Parse one a single list reference.
Similar to tmpl_rules_t, but used to specify parameters that may change during subsequent resolution ...
Definition tmpl.h:368
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:336
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
fr_aka_sim_id_type_t type
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
Definition log.h:96
Value pair map.
Definition map.h:77
fr_token_t op
The operator that controls insertion of the dst attribute.
Definition map.h:82
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition map.h:78
map_list_t child
parent map, for nested ones
Definition map.h:89
map_t * parent
Definition map.h:88
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition map.h:79
CONF_ITEM * ci
Config item that the map was created from.
Definition map.h:85
unsigned int allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:303
CONF_ITEM * ci
for migration support and various warnings
Definition tmpl.h:301
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:545
#define talloc_strdup(_ctx, _str)
Definition talloc.h:145
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:139
fr_slen_t fr_time_delta_from_str(fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint)
Create fr_time_delta_t from a string.
Definition time.c:412
static fr_time_delta_t fr_time_delta_add(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:255
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
#define fr_time_delta_ispos(_a)
Definition time.h:290
@ FR_TIME_RES_SEC
Definition time.h:50
"server local" time.
Definition time.h:69
@ FR_TIME_TRACKING_YIELDED
We're currently tracking time in the yielded state.
static void fr_time_tracking_yield(fr_time_tracking_t *tt, fr_time_t now)
Transition to the yielded state, recording the time we just spent running.
static void fr_time_tracking_end(fr_time_delta_t *predicted, fr_time_tracking_t *tt, fr_time_t now)
End time tracking for this entity.
static void fr_time_tracking_start(fr_time_tracking_t *parent, fr_time_tracking_t *tt, fr_time_t now)
Start time tracking for a tracked entity.
static void fr_time_tracking_resume(fr_time_tracking_t *tt, fr_time_t now)
Track that a request resumed.
const bool fr_assignment_op[T_TOKEN_LAST]
Definition token.c:171
const bool fr_list_assignment_op[T_TOKEN_LAST]
Definition token.c:188
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:34
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:81
const bool fr_comparison_op[T_TOKEN_LAST]
Definition token.c:201
const bool fr_binary_op[T_TOKEN_LAST]
Definition token.c:219
enum fr_token fr_token_t
@ T_OP_SUB_EQ
Definition token.h:70
@ T_INVALID
Definition token.h:39
@ T_OP_CMP_TRUE
Definition token.h:104
@ T_OP_EQ
Definition token.h:83
@ T_OP_SET
Definition token.h:84
@ T_OP_CMP_FALSE
Definition token.h:105
#define T_TOKEN_LAST
Definition token.h:129
Declarations for unlang transactions.
Declaration for unlang try.
#define unlang_frame_perf_resume(_x)
CONF_SECTION * cs
char const * debug_name
Printed in log messages when the node is executed.
void * state
Stack frame specialisations.
unlang_mod_actions_t actions
Priorities, etc. for the various return codes.
tmpl_rules_t const * rules
unlang_t * parent
Previous node.
static void unlang_type_init(unlang_t *unlang, unlang_t *parent, unlang_type_t type)
char const * thread_inst_type
fr_dict_attr_t const * root
the root of our dictionary
size_t pool_len
How much additional space to allocate for chunks.
static void unlang_compile_ctx_copy(unlang_compile_ctx_t *dst, unlang_compile_ctx_t const *src)
#define unlang_frame_perf_init(_x)
bool closed
whether or not this section is closed to new statements
static unlang_t * unlang_group_to_generic(unlang_group_t const *p)
#define unlang_list_foreach(_list_head, _iter)
char const * unlang_name
Talloc type name for the unlang_t.
#define UNLANG_IGNORE
static unlang_t * unlang_tmpl_to_generic(unlang_tmpl_t const *p)
CONF_ITEM * ci
used to generate this item
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
unsigned int number
unique node number
size_t unlang_size
Total length of the unlang_t + specialisation struct.
unlang_list_t children
char const * name
Unknown...
unlang_type_t
Types of unlang_t nodes.
Definition unlang_priv.h:47
@ UNLANG_TYPE_SWITCH
Switch section.
Definition unlang_priv.h:59
@ UNLANG_TYPE_TRANSACTION
transactions for editing lists
Definition unlang_priv.h:75
@ UNLANG_TYPE_FINALLY
run at the end of a virtual server.
Definition unlang_priv.h:78
@ UNLANG_TYPE_SUBREQUEST
create a child subrequest
Definition unlang_priv.h:67
@ UNLANG_TYPE_CONTINUE
Break statement (within a UNLANG_TYPE_FOREACH).
Definition unlang_priv.h:63
@ UNLANG_TYPE_ELSIF
!Condition && Condition.
Definition unlang_priv.h:58
@ UNLANG_TYPE_ELSE
!Condition.
Definition unlang_priv.h:57
@ UNLANG_TYPE_LOAD_BALANCE
Load balance section.
Definition unlang_priv.h:53
@ UNLANG_TYPE_DETACH
detach a child
Definition unlang_priv.h:70
@ UNLANG_TYPE_GROUP
Grouping section.
Definition unlang_priv.h:51
@ UNLANG_TYPE_POLICY
Policy section.
Definition unlang_priv.h:79
@ UNLANG_TYPE_TMPL
asynchronously expand a tmpl_t
Definition unlang_priv.h:81
@ UNLANG_TYPE_CASE
Case section (within a UNLANG_TYPE_SWITCH).
Definition unlang_priv.h:60
@ UNLANG_TYPE_LIMIT
limit number of requests in a section
Definition unlang_priv.h:74
@ UNLANG_TYPE_BREAK
Break statement (within a UNLANG_TYPE_FOREACH or UNLANG_TYPE_CASE).
Definition unlang_priv.h:62
@ UNLANG_TYPE_TRY
try / catch blocks
Definition unlang_priv.h:76
@ UNLANG_TYPE_CALL
call another virtual server
Definition unlang_priv.h:71
@ UNLANG_TYPE_RETURN
Return statement.
Definition unlang_priv.h:64
@ UNLANG_TYPE_REDUNDANT
exactly like group, but with different default return codes
Definition unlang_priv.h:52
@ UNLANG_TYPE_MAX
Definition unlang_priv.h:83
@ UNLANG_TYPE_IF
Condition.
Definition unlang_priv.h:56
@ UNLANG_TYPE_XLAT
Represents one level of an xlat expansion.
Definition unlang_priv.h:80
@ UNLANG_TYPE_NULL
unlang type not set.
Definition unlang_priv.h:48
@ UNLANG_TYPE_MAP
Mapping section (like #UNLANG_TYPE_UPDATE, but uses values from a map_proc_t call).
Definition unlang_priv.h:65
@ UNLANG_TYPE_CALLER
conditionally check parent dictionary type
Definition unlang_priv.h:72
@ UNLANG_TYPE_TIMEOUT
time-based timeouts.
Definition unlang_priv.h:73
@ UNLANG_TYPE_MODULE
Module method.
Definition unlang_priv.h:49
@ UNLANG_TYPE_REDUNDANT_LOAD_BALANCE
Redundant load balance section.
Definition unlang_priv.h:54
@ UNLANG_TYPE_CHILD_REQUEST
a frame at the top of a child's request stack used to signal the parent when the child is complete.
Definition unlang_priv.h:68
@ UNLANG_TYPE_CATCH
catch a previous try
Definition unlang_priv.h:77
@ UNLANG_TYPE_FUNCTION
Internal call to a function or submodule.
Definition unlang_priv.h:50
@ UNLANG_TYPE_EDIT
edit VPs in place. After 20 years!
Definition unlang_priv.h:82
@ UNLANG_TYPE_FOREACH
Foreach section.
Definition unlang_priv.h:61
@ UNLANG_TYPE_PARALLEL
execute statements in parallel
Definition unlang_priv.h:55
unlang_t const * instruction
The unlang node we're evaluating.
unsigned pool_headers
How much additional space to allocate for chunk headers.
unlang_compile_t compile
compile the keyword
unlang_variable_t * variables
rarely used, so we don't usually need it
unlang_t const * instruction
instruction which we're executing
char const * name
Name of the keyword.
#define unlang_frame_perf_yield(_x)
#define unlang_frame_perf_cleanup(_x)
int max_attr
1..N local attributes have been defined
unlang_thread_instantiate_t thread_instantiate
per-thread instantiation function
fr_dict_t * dict
our dictionary
static void unlang_group_type_init(unlang_t *unlang, unlang_t *parent, unlang_type_t type)
tmpl_t const * tmpl
@ UNLANG_OP_FLAG_SINGLE_WORD
the operation is parsed and compiled as a single word
unlang_type_t type
The specialisation of this node.
size_t thread_inst_size
unlang_op_flag_t flag
Interpreter flags for this operation.
unlang_list_t * list
so we have fewer run-time dereferences
void * thread_inst
thread-specific instance data
Generic representation of a grouping.
An unlang operation.
A node in a graph of unlang_op_t (s) that we execute.
Our interpreter stack, as distinct from the C stack.
A naked xlat.
static fr_slen_t parent
Definition pair.h:858
fr_time_delta_t irt
Initial transmission time.
Definition retry.h:33
fr_time_delta_t mrt
Maximum retransmission time.
Definition retry.h:34
uint32_t mrc
Maximum retransmission count.
Definition retry.h:36
fr_time_delta_t mrd
Maximum retransmission duration.
Definition retry.h:35
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
#define fr_type_is_structural(_x)
Definition types.h:393
#define fr_type_is_leaf(_x)
Definition types.h:394
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:455
#define DOC_KEYWORD_REF(_x)
Definition version.h:89
static size_t char ** out
Definition value.h:1030
virtual_server_t const * virtual_server_find(char const *name)
Return virtual server matching the specified name.
CONF_SECTION * virtual_server_cs(virtual_server_t const *vs)
Return the configuration section for a virtual server.