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