The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
xlat_eval.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: 1427de2dae43213a82a7c1837a5007bf8f121b56 $
19 *
20 * @file xlat_eval.c
21 * @brief String expansion ("translation"). Evaluation of pre-parsed xlat expansions.
22 *
23 * @copyright 2018-2021 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2000,2006 The FreeRADIUS server project
25 * @copyright 2000 Alan DeKok (aland@freeradius.org)
26 */
27RCSID("$Id: 1427de2dae43213a82a7c1837a5007bf8f121b56 $")
28
29#include <freeradius-devel/server/base.h>
30#include <freeradius-devel/unlang/xlat.h>
31#include <freeradius-devel/unlang/xlat_priv.h>
32#include <freeradius-devel/util/debug.h>
33#include <freeradius-devel/util/types.h>
34#include <freeradius-devel/util/sbuff.h>
35#include <freeradius-devel/util/value.h>
36
37static int instance_count = 0;
38
40
42 { .out = &dict_freeradius, .proto = "freeradius" },
43 { NULL }
44};
45
46fr_dict_attr_t const *attr_expr_bool_enum; /* xlat_expr.c */
47fr_dict_attr_t const *attr_cast_base; /* xlat_expr.c */
48
60
62 { .out = &attr_expr_bool_enum, .name = "Expr-Bool-Enum", .type = FR_TYPE_BOOL, .dict = &dict_freeradius },
63 { .out = &attr_cast_base, .name = "Cast-Base", .type = FR_TYPE_UINT8, .dict = &dict_freeradius },
64
65 { .out = &attr_cast_time_res_sec, .name = "Cast-Time-Res-Sec", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
66 { .out = &attr_cast_time_res_min, .name = "Cast-Time-Res-Min", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
67 { .out = &attr_cast_time_res_hour, .name = "Cast-Time-Res-Hour", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
68 { .out = &attr_cast_time_res_day, .name = "Cast-Time-Res-Day", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
69 { .out = &attr_cast_time_res_week, .name = "Cast-Time-Res-Week", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
70 { .out = &attr_cast_time_res_month, .name = "Cast-Time-Res-Month", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
71 { .out = &attr_cast_time_res_year, .name = "Cast-Time-Res-Year", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
72 { .out = &attr_cast_time_res_csec, .name = "Cast-Time-Res-Centi-Sec", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
73 { .out = &attr_cast_time_res_msec, .name = "Cast-Time-Res-Milli-Sec", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
74 { .out = &attr_cast_time_res_usec, .name = "Cast-Time-Res-Micro-Sec", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
75 { .out = &attr_cast_time_res_nsec, .name = "Cast-Time-Res-Nano-Sec", .type = FR_TYPE_TIME_DELTA, .dict = &dict_freeradius },
76
77 { NULL }
78};
79
81 { L("done"), XLAT_ACTION_DONE },
82 { L("fail"), XLAT_ACTION_FAIL },
83 { L("push-child"), XLAT_ACTION_PUSH_CHILD },
84 { L("yield"), XLAT_ACTION_YIELD }
85};
87
88/*
89 * This should be updated if fr_time_precision_table[] adds more time resolutions.
90 */
92 { L("microseconds"), &attr_cast_time_res_usec },
93 { L("us"), &attr_cast_time_res_usec },
94
95 { L("nanoseconds"), &attr_cast_time_res_nsec },
96 { L("ns"), &attr_cast_time_res_nsec },
97
98 { L("milliseconds"), &attr_cast_time_res_msec },
99 { L("ms"), &attr_cast_time_res_msec },
100
101 { L("centiseconds"), &attr_cast_time_res_csec },
102 { L("cs"), &attr_cast_time_res_csec },
103
104 { L("seconds"), &attr_cast_time_res_sec },
105 { L("s"), &attr_cast_time_res_sec },
106
107 { L("minutes"), &attr_cast_time_res_min },
108 { L("m"), &attr_cast_time_res_min },
109
110 { L("hours"), &attr_cast_time_res_hour },
111 { L("h"), &attr_cast_time_res_hour },
112
113 { L("days"), &attr_cast_time_res_day },
114 { L("d"), &attr_cast_time_res_day },
115
116 { L("weeks"), &attr_cast_time_res_week },
117 { L("w"), &attr_cast_time_res_week },
118
119 /*
120 * These use special values FR_TIME_DUR_MONTH and FR_TIME_DUR_YEAR
121 */
122 { L("months"), &attr_cast_time_res_month },
123 { L("M"), &attr_cast_time_res_month },
124
125 { L("years"), &attr_cast_time_res_year },
126 { L("y"), &attr_cast_time_res_year },
127
128};
130
131fr_dict_attr_t const *xlat_time_res_attr(char const *res)
132{
133 fr_dict_attr_t const **da_p;
134
136 if (!da_p) return NULL;
137
138 return *da_p;
139}
140
141static ssize_t xlat_eval_sync(TALLOC_CTX *ctx, char **out, request_t *request, xlat_exp_head_t const * const head,
142 xlat_escape_legacy_t escape, void const *escape_ctx);
143
144/** Reconstruct the original expansion string from an xlat tree
145 *
146 * @param[in] out sbuff to print result in.
147 * @param[in] node in the tree to start printing.
148 * @return
149 * - The original expansion string on success.
150 * - NULL on error.
151 */
153{
154 switch (node->type) {
155 case XLAT_BOX:
156 case XLAT_GROUP:
157 fr_assert(node->fmt != NULL);
158 return fr_sbuff_in_sprintf(out, "%pV", fr_box_strvalue_buffer(node->fmt));
159
160 case XLAT_ONE_LETTER:
161 fr_assert(node->fmt != NULL);
162 return fr_sbuff_in_sprintf(out, "%%%s", node->fmt);
163
164 case XLAT_TMPL:
165 fr_assert(node->fmt != NULL);
166 if (tmpl_is_attr(node->vpt) && (node->fmt[0] == '&')) {
167 return fr_sbuff_in_strcpy(out, node->fmt);
168 } else {
169 return fr_sbuff_in_sprintf(out, "%%{%pV}", fr_box_strvalue_buffer(node->fmt));
170 }
171
172#ifdef HAVE_REGEX
173 case XLAT_REGEX:
174 return fr_sbuff_in_sprintf(out, "%%{%u}", node->regex_index);
175#endif
176
177 case XLAT_FUNC:
178 {
179 bool first_done = false;
180 fr_sbuff_t our_out;
181 fr_slen_t slen;
182
183 /*
184 * No arguments, just print an empty function.
185 */
186 if (!xlat_exp_head(node->call.args)) return fr_sbuff_in_sprintf(out, "%%%s()", node->call.func->name);
187
188 our_out = FR_SBUFF(out);
189 FR_SBUFF_IN_SPRINTF_RETURN(&our_out, "%%%s(", node->call.func->name);
190
191 xlat_exp_foreach(node->call.args, arg) {
192 if ((first_done) && (node->call.func->input_type == XLAT_INPUT_ARGS)) {
193 FR_SBUFF_IN_CHAR_RETURN(&our_out, ',');
194 }
195
196 slen = xlat_fmt_print(&our_out, arg);
197 if (slen < 0) return slen - fr_sbuff_used(&our_out);
198
199 first_done = true;
200 }
201
202 FR_SBUFF_IN_CHAR_RETURN(&our_out, ')');
203 return fr_sbuff_set(out, &our_out);
204 }
205
206 default:
207 return 0;
208 }
209}
210
211/** Output what we're currently expanding
212 *
213 * @param[in] request The current request.
214 * @param[in] node Being processed.
215 * @param[in] args from previous expansion.
216 * @param[in] line Unused
217 */
218static inline void xlat_debug_log_expansion(request_t *request, xlat_exp_t const *node, fr_value_box_list_t const *args, UNUSED int line)
219{
220 if (node->flags.constant) return;
221
222 if (!RDEBUG_ENABLED2) return;
223
224 /*
225 * Because it's difficult to keep track of what
226 * the function was actually called with,
227 * we print the concatenated arguments list as
228 * well as the original fmt string.
229 */
230 if ((node->type == XLAT_FUNC) && !xlat_is_literal(node->call.args)) {
231 RDEBUG2("| %%%s(%pM)", node->call.func->name, args);
232 } else {
233 fr_sbuff_t *agg;
234
235 FR_SBUFF_TALLOC_THREAD_LOCAL(&agg, 1024, SIZE_MAX);
236
237 if (xlat_fmt_print(agg, node) < 0) {
238 RERROR("Failed printing expansion");
239 return;
240 }
241 RDEBUG2("| %s", fr_sbuff_start(agg)); /* print line number here for debugging */
242 }
243}
244
245/** Output the list result of an expansion
246 *
247 * @param[in] request The current request.
248 * @param[in] node which was expanded.
249 * @param[in] result of the expansion.
250 */
251static inline void xlat_debug_log_list_result(request_t *request, xlat_exp_t const *node, fr_value_box_list_t const *result)
252{
253 if (node->flags.constant) return;
254
255 if (!RDEBUG_ENABLED2) return;
256
257 RDEBUG2("| --> %pM", result);
258}
259
260/** Output the result of an expansion
261 *
262 * @param[in] request The current request.
263 * @param[in] node which was expanded.
264 * @param[in] result of the expansion.
265 */
266static inline void xlat_debug_log_result(request_t *request, xlat_exp_t const *node, fr_value_box_t const *result)
267{
268 if (node->flags.constant) return;
269
270 if (!RDEBUG_ENABLED2) return;
271
272 RDEBUG2("| --> %pV", result);
273}
274
275/** Process an individual xlat argument value box group
276 *
277 * @param[in] ctx to allocate any additional buffers in
278 * @param[in,out] list of value boxes representing one argument
279 * @param[in] request currently being processed
280 * @param[in] name of the function being called
281 * @param[in] arg specification of current argument
282 * @param[in] arg_num number of current argument in the argument specifications
283 * @return
284 * - XLAT_ACTION_DONE on success.
285 * - XLAT_ACTION_FAIL on failure.
286 */
287static xlat_action_t xlat_process_arg_list(TALLOC_CTX *ctx, fr_value_box_list_t *list, request_t *request,
288 char const *name, xlat_arg_parser_t const *arg, unsigned int arg_num)
289{
290 fr_value_box_t *vb;
291
292#define ESCAPE(_arg, _vb, _arg_num) \
293do { \
294 if ((_arg)->func && (!(_vb)->safe_for || !fr_value_box_is_safe_for((_vb), (_arg)->safe_for) || (_arg)->always_escape)) { \
295 if ((_arg)->func(request, _vb, (_arg)->uctx) < 0) { \
296 RPEDEBUG("Function \"%s\" failed escaping argument %u", name, _arg_num); \
297 return XLAT_ACTION_FAIL; \
298 } \
299 fr_value_box_mark_safe_for((_vb), (_arg)->safe_for); \
300 } \
301} while (0)
302
303 if (fr_value_box_list_empty(list)) {
304 if (arg->required) {
305 REDEBUG("Function \"%s\" is missing required argument %u", name, arg_num);
306 return XLAT_ACTION_FAIL;
307 }
308 return XLAT_ACTION_DONE;
309 }
310
311 vb = fr_value_box_list_head(list);
312
313 /*
314 * Concatenate child boxes, casting to desired type,
315 * then replace group vb with first child vb
316 */
317 if (arg->concat) {
318 if (arg->func) {
319 do ESCAPE(arg, vb, arg_num); while ((vb = fr_value_box_list_next(list, vb)));
320
321 vb = fr_value_box_list_head(list); /* Reset */
322 }
323
325 vb, list, arg->type,
327 SIZE_MAX) < 0) {
328 RPEDEBUG("Function \"%s\" failed concatenating arguments to type %s", name, fr_type_to_str(arg->type));
329 return XLAT_ACTION_FAIL;
330 }
331 fr_assert(fr_value_box_list_num_elements(list) <= 1);
332
333 return XLAT_ACTION_DONE;
334 }
335
336 /*
337 * Only a single child box is valid here. Check there is
338 * just one, cast to the correct type
339 */
340 if (arg->single) {
341 if (fr_value_box_list_num_elements(list) > 1) {
342 RPEDEBUG("Function \"%s\" was provided an incorrect number of values at argument %u, "
343 "expected %s got %u",
344 name, arg_num,
345 arg->required ? "0-1" : "1",
346 fr_value_box_list_num_elements(list));
347 return XLAT_ACTION_FAIL;
348 }
349
350 ESCAPE(arg, vb, arg_num);
351
352 if ((arg->type != FR_TYPE_VOID) && (vb->type != arg->type)) {
353 cast_error:
354 if (fr_value_box_cast_in_place(ctx, vb,
355 arg->type, NULL) < 0) {
356 RPEDEBUG("Function \"%s\" failed to cast argument %u to type %s", name, arg_num, fr_type_to_str(arg->type));
357 return XLAT_ACTION_FAIL;
358 }
359 }
360
361 return XLAT_ACTION_DONE;
362 }
363
364 /*
365 * We're neither concatenating nor do we only expect a single value,
366 * cast all child values to the required type.
367 */
368 if (arg->type != FR_TYPE_VOID) {
369 do {
370 ESCAPE(arg, vb, arg_num);
371 if (vb->type == arg->type) continue;
372 if (fr_value_box_cast_in_place(ctx, vb,
373 arg->type, NULL) < 0) goto cast_error;
374 } while ((vb = fr_value_box_list_next(list, vb)));
375
376 /*
377 * If it's not a void type we still need to escape the values
378 */
379 } else if (arg->func) {
380 do ESCAPE(arg, vb, arg_num); while ((vb = fr_value_box_list_next(list, vb)));
381 }
382
383#undef ESCAPE
384
385 return XLAT_ACTION_DONE;
386}
387
388
389/** Process list of boxed values provided as input to an xlat
390 *
391 * Ensures that the value boxes passed to an xlat function match the
392 * requirements listed in its "args", and escapes any tainted boxes
393 * using the specified escaping routine.
394 *
395 * @param[in] ctx in which to allocate any buffers.
396 * @param[in,out] list value boxes provided as input.
397 * List will be modified in accordance to rules
398 * provided in the args array.
399 * @param[in] request being processed.
400 * @param[in] func to call
401 */
402static inline CC_HINT(always_inline)
403xlat_action_t xlat_process_args(TALLOC_CTX *ctx, fr_value_box_list_t *list,
404 request_t *request, xlat_t const *func)
405{
406 xlat_arg_parser_t const *arg_p = func->args;
407 xlat_action_t xa;
408 fr_value_box_t *vb, *next;
409
410 /*
411 * No args registered for this xlat
412 */
413 if (!func->args) return XLAT_ACTION_DONE;
414
415 /*
416 * xlat needs no input processing just return.
417 */
418 switch (func->input_type) {
420 return XLAT_ACTION_DONE;
421
422 /*
423 * xlat takes all input as a single vb.
424 */
425 case XLAT_INPUT_ARGS:
426 vb = fr_value_box_list_head(list);
427 while (arg_p->type != FR_TYPE_NULL) {
428 /*
429 * Separate check to see if the group
430 * box is there. Check in
431 * xlat_process_arg_list verifies it
432 * has a value.
433 */
434 if (!vb) {
435 if (arg_p->required) {
436 missing:
437 REDEBUG("Function \"%s\" is missing required argument %u",
438 func->name, (unsigned int)((arg_p - func->args) + 1));
439 return XLAT_ACTION_FAIL;
440 }
441
442 /*
443 * The argument isn't required. Just omit it. xlat_func_args_set() enforces
444 * that optional arguments are at the end of the argument list.
445 */
446 return XLAT_ACTION_DONE;
447 }
448
449 /*
450 * Everything in the top level list should be
451 * groups
452 */
453 if (!fr_cond_assert(vb->type == FR_TYPE_GROUP)) return XLAT_ACTION_FAIL;
454
455 /*
456 * pre-advance, in case the vb is replaced
457 * during processing.
458 */
459 next = fr_value_box_list_next(list, vb);
460 xa = xlat_process_arg_list(ctx, &vb->vb_group, request, func->name, arg_p,
461 (unsigned int)((arg_p - func->args) + 1));
462 if (xa != XLAT_ACTION_DONE) return xa;
463
464 /*
465 * This argument doesn't exist. That might be OK, or it may be a fatal error.
466 */
467 if (fr_value_box_list_empty(&vb->vb_group)) {
468 /*
469 * Variadic rules deal with empty boxes differently...
470 */
471 switch (arg_p->variadic) {
473 fr_value_box_list_talloc_free_head(list);
474 goto do_next;
475
477 goto empty_ok;
478
480 break;
481 }
482
483 /*
484 * Empty groups for optional arguments are OK, we can just stop processing the list.
485 */
486 if (!arg_p->required) {
487 /*
488 * If the caller doesn't care about the type, then we leave the
489 * empty group there.
490 */
491 if (arg_p->type == FR_TYPE_VOID) goto do_next;
492
493 /*
494 * The caller does care about the type, and we don't have any
495 * matching data. Omit this argument, and all arguments after it.
496 *
497 * i.e. if the caller has 3 optional arguments, all
498 * FR_TYPE_UINT8, and the first one is missing, then we MUST
499 * either supply boxes all of FR_TYPE_UINT8, OR we supply nothing.
500 *
501 * We can't supply a box of any other type, because the caller
502 * has declared that it wants FR_TYPE_UINT8, and is naively
503 * accessing the box as vb_uint8, hoping that it's being passed
504 * the right thing.
505 */
506 fr_value_box_list_talloc_free_head(list);
507 break;
508 }
509
510 /*
511 * If the caller is expecting a particular type, then getting nothing is
512 * an error.
513 *
514 * If the caller manually checks the input type, then we can leave it as
515 * an empty group.
516 */
517 if (arg_p->type != FR_TYPE_VOID) goto missing;
518 }
519
520 empty_ok:
521 /*
522 * In some cases we replace the current argument with the head of the group.
523 *
524 * xlat_process_arg_list() has already done concatenations for us.
525 */
526 if (arg_p->single || arg_p->concat) {
527 fr_value_box_t *head = fr_value_box_list_pop_head(&vb->vb_group);
528
529 /*
530 * If we're meant to be smashing the argument
531 * to a single box, but the group was empty,
532 * add a null box instead so ordering is maintained
533 * for subsequent boxes.
534 */
535 if (!head) head = fr_value_box_alloc_null(ctx);
536 fr_value_box_list_replace(list, vb, head);
537 talloc_free(vb);
538 }
539
540 do_next:
541 if (arg_p->variadic) {
542 if (!next) break;
543 } else {
544 arg_p++;
545 }
546 vb = next;
547 }
548 break;
549 }
550
551 return XLAT_ACTION_DONE;
552}
553
554/** Validate that the return values from an xlat function match what it registered
555 *
556 * @param[in] request The current request.
557 * @param[in] func that was called.
558 * @param[in] returned the output list of the function.
559 * @param[in] pos current position in the output list.
560 * @return
561 * - true - If return values were correct.
562 * - false - If the return values were incorrect.
563 */
564static inline CC_HINT(nonnull(1,2,3))
565bool xlat_process_return(request_t *request, xlat_t const *func, fr_value_box_list_t const *returned, fr_value_box_t *pos)
566{
567 unsigned int count = 0;
568
569 /*
570 * Nothing to validate. We don't yet enforce that functions
571 * must return at least one instance of their type.
572 */
573 if (!pos || fr_type_is_void(func->return_type)) return true;
574
575 if (fr_type_is_null(func->return_type)) {
576 /* Dynamic expansion to get the right name */
577 REDEBUG("%s return type registered as %s, but %s expansion produced data",
578 func->name, func->name, fr_type_to_str(func->return_type));
579
580 /* We are not forgiving for debug builds */
581 fr_assert_fail("Treating invalid return type as fatal");
582
583 return false;
584 }
585
586 do {
587 if (pos->type != func->return_type) {
588 REDEBUG("%s returned invalid result type at index %u. Expected type %s, got type %s",
589 func->name, count, fr_type_to_str(func->return_type), fr_type_to_str(pos->type));
590
591 /* We are not forgiving for debug builds */
592 fr_assert_fail("Treating invalid return type as fatal");
593 }
594 fr_value_box_mark_safe_for(pos, func->return_safe_for); /* Always set this */
595 count++;
596 } while ((pos = fr_value_box_list_next(returned, pos)));
597
598 return true;
599}
600
601/** One letter expansions
602 *
603 * @param[in] ctx to allocate boxed value, and buffers in.
604 * @param[out] out Where to write the boxed value.
605 * @param[in] request The current request.
606 * @param[in] letter to expand.
607 * @return
608 * - #XLAT_ACTION_FAIL on memory allocation errors.
609 * - #XLAT_ACTION_DONE if we're done processing this node.
610 *
611 */
612static inline CC_HINT(always_inline)
613xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out,
614 request_t *request, char letter)
615{
616
617 char buffer[64];
618 struct tm ts;
619 time_t now;
621
622 now = fr_time_to_sec(request->packet->timestamp);
623
624 switch (letter) {
625 case '%':
627 if (fr_value_box_strdup(value, value, NULL, "%", false) < 0) return XLAT_ACTION_FAIL;
628 break;
629
630 /*
631 * RADIUS request values
632 */
633
634 case 'I': /* Request ID */
636 value->datum.uint32 = request->packet->id;
637 break;
638
639 case 'n': /* Request number */
641 value->datum.uint64 = request->number;
642 break;
643
644 case 's': /* First request in this sequence */
646 value->datum.uint64 = request->seq_start;
647 break;
648
649 /*
650 * Current time
651 */
652
653 case 'c': /* Current epoch time seconds */
654 /*
655 * @todo - leave this as FR_TYPE_DATE, but add an enumv which changes the scale to
656 * seconds?
657 */
659 value->datum.uint64 = (uint64_t)fr_time_to_sec(fr_time());
660 break;
661
662 case 'C': /* Current epoch time microsecond component */
663 /*
664 * @todo - we probably should remove this now that we have FR_TYPE_DATE with scaling.
665 */
667 value->datum.uint64 = (uint64_t)fr_time_to_usec(fr_time()) % 1000000;
668 break;
669
670 /*
671 * Time of the current request
672 */
673
674 case 'd': /* Request day */
675 if (!localtime_r(&now, &ts)) {
676 error:
677 REDEBUG("Failed converting packet timestamp to localtime: %s", fr_syserror(errno));
678 return XLAT_ACTION_FAIL;
679 }
680
682 value->datum.uint8 = ts.tm_mday;
683 break;
684
685 case 'D': /* Request date */
686 if (!localtime_r(&now, &ts)) goto error;
687
688 strftime(buffer, sizeof(buffer), "%Y%m%d", &ts);
689
691 if (fr_value_box_strdup(value, value, NULL, buffer, false) < 0) goto error;
692 break;
693
694 case 'e': /* Request second */
695 if (!localtime_r(&now, &ts)) goto error;
696
698 value->datum.uint8 = ts.tm_sec;
699 break;
700
701 case 'G': /* Request minute */
702 if (!localtime_r(&now, &ts)) goto error;
703
705 value->datum.uint8 = ts.tm_min;
706 break;
707
708 case 'H': /* Request hour */
709 if (!localtime_r(&now, &ts)) goto error;
710
712 value->datum.uint8 = ts.tm_hour;
713 break;
714
715 case 'l': /* Request timestamp as seconds since the epoch */
716 /*
717 * @todo - leave this as FR_TYPE_DATE, but add an enumv which changes the scale to
718 * seconds?
719 */
721 value->datum.uint64 = (uint64_t ) now;
722 break;
723
724 case 'm': /* Request month */
725 if (!localtime_r(&now, &ts)) goto error;
726
728 value->datum.uint8 = ts.tm_mon + 1;
729 break;
730
731 case 'M': /* Request time microsecond component */
732 /*
733 * @todo - we probably should remove this now that we have FR_TYPE_DATE with scaling.
734 */
736 value->datum.uint32 = fr_time_to_msec(request->packet->timestamp) % 1000;
737 break;
738
739 case 'S': /* Request timestamp in SQL format */
740 if (!localtime_r(&now, &ts)) goto error;
741
742 strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &ts);
743
745 if (fr_value_box_strdup(value, value, NULL, buffer, false) < 0) goto error;
746 break;
747
748 case 't': /* Request timestamp in CTIME format */
749 {
750 char *p;
751
752 CTIME_R(&now, buffer, sizeof(buffer));
753 p = strchr(buffer, '\n');
754 if (p) *p = '\0';
755
757 if (fr_value_box_strdup(value, value, NULL, buffer, false) < 0) goto error;
758 }
759 break;
760
761 case 'T': /* Request timestamp in ISO format */
762 {
763 int len = 0;
764
765 if (!gmtime_r(&now, &ts)) goto error;
766
767 if (!(len = strftime(buffer, sizeof(buffer) - 1, "%Y-%m-%dT%H:%M:%S", &ts))) {
768 REDEBUG("Failed converting packet timestamp to gmtime: Buffer full");
769 return XLAT_ACTION_FAIL;
770 }
771 strcat(buffer, ".");
772 len++;
773 snprintf(buffer + len, sizeof(buffer) - len, "%03i",
774 (int) fr_time_to_msec(request->packet->timestamp) % 1000);
775
777 if (fr_value_box_strdup(value, value, NULL, buffer, false) < 0) goto error;
778 }
779 break;
780
781 case 'Y': /* Request year */
782 if (!localtime_r(&now, &ts)) goto error;
783
785
786 value->datum.int16 = ts.tm_year + 1900;
787 break;
788
789 default:
790 fr_assert_fail("%%%c is not a valid one letter expansion", letter);
791 return XLAT_ACTION_FAIL;
792 }
793
794 fr_value_box_list_insert_tail(out, value);
795
796 return XLAT_ACTION_DONE;
797}
798
799typedef struct {
801 fr_value_box_list_t list;
803
805 xlat_ctx_t const *xctx,
806 UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
807{
808 xlat_exec_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_exec_rctx_t);
809
810 if (rctx->status != 0) {
811 fr_strerror_printf("Program failed with status %d", rctx->status);
812 return XLAT_ACTION_FAIL;
813 }
814
815 fr_value_box_list_move((fr_value_box_list_t *)out->dlist, &rctx->list);
816
817 return XLAT_ACTION_DONE;
818}
819
820
821/** Signal an xlat function
822 *
823 * @param[in] signal function to call.
824 * @param[in] exp Xlat node that previously yielded.
825 * @param[in] request The current request.
826 * @param[in] rctx Opaque (to us), resume ctx provided by the xlat function
827 * when it yielded.
828 * @param[in] action What the request should do (the type of signal).
829 */
831 request_t *request, void *rctx, fr_signal_t action)
832{
834
835 signal(XLAT_CTX(exp->call.inst, t->data, t->mctx, NULL, rctx), request, action);
836}
837
839 UNUSED xlat_ctx_t const *xctx,
840 UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
841{
842 return XLAT_ACTION_DONE;
843}
844
845/** Call an xlat's resumption method
846 *
847 * @param[in] ctx to allocate value boxes in.
848 * @param[out] out a list of #fr_value_box_t to append to.
849 * @param[out] child to evaluate. If a child needs to be evaluated
850 * by the caller, we return XLAT_ACTION_PUSH_CHILD
851 * and place the child to be evaluated here.
852 * Once evaluation is complete, the caller
853 * should call us with the same #xlat_exp_t and the
854 * result of the nested evaluation in result.
855 * @param[in] request the current request.
856 * @param[in] head of the list to evaluate
857 * @param[in,out] in xlat node to evaluate. Advanced as we process
858 * additional #xlat_exp_t.
859 * @param[in] result Previously expanded arguments to this xlat function.
860 * @param[in] resume function to call.
861 * @param[in] rctx Opaque (to us), resume ctx provided by xlat function
862 * when it yielded.
863 */
865 xlat_exp_head_t const **child,
866 request_t *request, xlat_exp_head_t const *head, xlat_exp_t const **in,
867 fr_value_box_list_t *result, xlat_func_t resume, void *rctx)
868{
869 xlat_action_t xa;
870 xlat_exp_t const *node = *in;
871
872 /*
873 * It's important that callbacks leave the result list
874 * in a valid state, as it leads to all kinds of hard
875 * to debug problems if they free or change elements
876 * and don't remove them from the list.
877 */
878 VALUE_BOX_LIST_VERIFY(result);
879
880 if (node->type != XLAT_FUNC) {
881 xa = resume(ctx, out, XLAT_CTX(NULL, NULL, NULL, NULL, rctx), request, result);
882 } else {
885 xa = resume(ctx, out, XLAT_CTX(node->call.inst->data, t->data, t->mctx, NULL, rctx), request, result);
886 VALUE_BOX_LIST_VERIFY(result);
887
888 RDEBUG2("| %%%s(...)", node->call.func->name);
889 }
890
891 switch (xa) {
893 RDEBUG2("| (YIELD)");
894 return xa;
895
896 case XLAT_ACTION_DONE:
897 if (unlang_xlat_yield(request, xlat_null_resume, NULL, 0, NULL) != XLAT_ACTION_YIELD) return XLAT_ACTION_FAIL;
898
899 fr_dcursor_next(out); /* Wind to the start of this functions output */
900 if (node->call.func) {
901 RDEBUG2("| --> %pV", fr_dcursor_current(out));
902 if (!xlat_process_return(request, node->call.func, (fr_value_box_list_t *)out->dlist,
904 }
905
906 /*
907 * It's easier if we get xlat_frame_eval to continue evaluating the frame.
908 */
909 *in = xlat_exp_next(head, *in); /* advance */
910 return xlat_frame_eval(ctx, out, child, request, head, in);
911
914 case XLAT_ACTION_FAIL:
915 break;
916 }
917
918 return xa;
919}
920
921/** Process the result of a previous nested expansion
922 *
923 * @param[in] ctx to allocate value boxes in.
924 * @param[out] out a list of #fr_value_box_t to append to.
925 * @param[out] child to evaluate. If a child needs to be evaluated
926 * by the caller, we return XLAT_ACTION_PUSH_CHILD
927 * and place the child to be evaluated here.
928 * Once evaluation is complete, the caller
929 * should call us with the same #xlat_exp_t and the
930 * result of the nested evaluation in result.
931 * @param[in] request the current request.
932 * @param[in] head of the list to evaluate
933 * @param[in,out] in xlat node to evaluate. Advanced as we process
934 * additional #xlat_exp_t.
935 * @param[in] env_data Expanded call env.
936 * @param[in] result of a previous nested evaluation.
937 */
939 xlat_exp_head_t const **child,
940 request_t *request, xlat_exp_head_t const *head, xlat_exp_t const **in,
941 void *env_data, fr_value_box_list_t *result)
942{
943 xlat_exp_t const *node = *in;
944
945 fr_dcursor_tail(out); /* Needed for reentrant behaviour and debugging */
946
947 switch (node->type) {
948 case XLAT_FUNC:
949 {
950 xlat_action_t xa;
952 fr_value_box_list_t result_copy;
953
955 fr_assert(t);
956
957 XLAT_DEBUG("** [%i] %s(func-async) - %%%s(%pM)",
958 unlang_interpret_stack_depth(request), __FUNCTION__,
959 node->fmt, result);
960
961 VALUE_BOX_LIST_VERIFY(result);
962
963 /*
964 * Always need to init and free the
965 * copy list as debug level could change
966 * when the xlat function executes.
967 */
968 fr_value_box_list_init(&result_copy);
969
970 /*
971 * Need to copy the input list in case
972 * the async function mucks with it.
973 */
975 xa = xlat_process_args(ctx, result, request, node->call.func);
976 if (xa == XLAT_ACTION_FAIL) {
977 fr_value_box_list_talloc_free(&result_copy);
978 return xa;
979 }
980
981 VALUE_BOX_LIST_VERIFY(result);
982 xa = node->call.func->func(ctx, out,
983 XLAT_CTX(node->call.inst->data, t->data, t->mctx, env_data, NULL),
984 request, result);
985 VALUE_BOX_LIST_VERIFY(result);
986
987 if (RDEBUG_ENABLED2) {
988 REXDENT();
989 xlat_debug_log_expansion(request, *in, &result_copy, __LINE__);
990 RINDENT();
991 }
992 fr_value_box_list_talloc_free(&result_copy);
993
994 switch (xa) {
995 case XLAT_ACTION_FAIL:
996 return xa;
997
999 RDEBUG3("| -- CHILD");
1000 return xa;
1001
1003 RDEBUG3("| -- UNLANG");
1004 return xa;
1005
1006 case XLAT_ACTION_YIELD:
1007 RDEBUG3("| -- YIELD");
1008 return xa;
1009
1010 case XLAT_ACTION_DONE: /* Process the result */
1012
1013 /*
1014 * Don't print out results if there are no results.
1015 */
1016 if (!fr_type_is_void(node->call.func->return_type)) {
1017 REXDENT();
1019 if (!xlat_process_return(request, node->call.func,
1020 (fr_value_box_list_t *)out->dlist,
1022 RINDENT();
1023 return XLAT_ACTION_FAIL;
1024 }
1025 RINDENT();
1026 }
1027 break;
1028 }
1029 }
1030 break;
1031
1032 case XLAT_GROUP:
1033 {
1034 fr_value_box_t *arg;
1035
1036 /*
1037 * We'd like to do indent / exdent for groups, but that also involves fixing all of the
1038 * error paths. Which we won't do right now.
1039 */
1040 XLAT_DEBUG("** [%i] %s(child) - continuing %%{%s ...}", unlang_interpret_stack_depth(request), __FUNCTION__,
1041 node->fmt);
1042
1043 MEM(arg = fr_value_box_alloc(ctx, FR_TYPE_GROUP, NULL));
1044
1045 if (!fr_value_box_list_empty(result)) {
1046 VALUE_BOX_LIST_VERIFY(result);
1047 fr_value_box_list_move(&arg->vb_group, result);
1048 }
1049
1050// xlat_debug_log_expansion(request, *in, NULL, __LINE__);
1051// xlat_debug_log_result(request, *in, arg);
1052
1053 VALUE_BOX_VERIFY(arg);
1054
1055 fr_dcursor_insert(out, arg);
1056 }
1057 break;
1058
1059 case XLAT_TMPL:
1060 fr_assert(tmpl_is_exec(node->vpt));
1061
1062 if (tmpl_eval_cast_in_place(result, request, node->vpt) < 0) {
1063 fr_value_box_list_talloc_free(result);
1064 return XLAT_ACTION_FAIL;
1065 }
1066
1067 /*
1068 * First entry is the command to run. Subsequent entries are the options to pass to the
1069 * command.
1070 */
1071 fr_value_box_list_move((fr_value_box_list_t *)out->dlist, result);
1072 break;
1073
1074 default:
1075 fr_assert(0);
1076 return XLAT_ACTION_FAIL;
1077 }
1078
1079 /*
1080 * It's easier if we get xlat_frame_eval to continue evaluating the frame.
1081 */
1082 *in = xlat_exp_next(head, *in); /* advance */
1083 return xlat_frame_eval(ctx, out, child, request, head, in);
1084}
1085
1086/** Converts xlat nodes to value boxes
1087 *
1088 * Evaluates a single level of expansions.
1089 *
1090 * @param[in] ctx to allocate value boxes in.
1091 * @param[out] out a list of #fr_value_box_t to append to.
1092 * @param[out] child to evaluate. If a child needs to be evaluated
1093 * by the caller, we return XLAT_ACTION_PUSH_CHILD
1094 * and place the child to be evaluated here.
1095 * Once evaluation is complete, the caller
1096 * should call us with the same #xlat_exp_t and the
1097 * result of the nested evaluation in result.
1098 * @param[in] request the current request.
1099 * @param[in] head of the list to evaluate
1100 * @param[in,out] in xlat node to evaluate. Advanced as we process
1101 * additional #xlat_exp_t.
1102 * @return
1103 * - XLAT_ACTION_PUSH_CHILD if we need to evaluate a deeper level of nested.
1104 * child will be filled with the node that needs to be evaluated.
1105 * call #xlat_frame_eval_repeat on this node, once there are results
1106 * from the nested expansion.
1107 * - XLAT_ACTION_YIELD a resumption frame was pushed onto the stack by an
1108 * xlat function and we need to wait for the request to be resumed
1109 * before continuing.
1110 * - XLAT_ACTION_DONE we're done, pop the frame.
1111 * - XLAT_ACTION_FAIL an xlat module failed.
1112 */
1114 request_t *request, xlat_exp_head_t const *head, xlat_exp_t const **in)
1115{
1117 xlat_exp_t const *node;
1118 fr_value_box_list_t result; /* tmp list so debug works correctly */
1120
1121 fr_value_box_list_init(&result);
1122
1123 *child = NULL;
1124
1125 if (!*in) return XLAT_ACTION_DONE;
1126
1127 XLAT_DEBUG("** [%i] %s >> entered", unlang_interpret_stack_depth(request), __FUNCTION__);
1128
1129 for (node = *in; node; node = xlat_exp_next(head, node)) {
1130 *in = node; /* Update node in our caller */
1131 fr_dcursor_tail(out); /* Needed for debugging */
1132 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
1133
1134 fr_assert(fr_value_box_list_num_elements(&result) == 0); /* Should all have been moved */
1135
1136 switch (node->type) {
1137 case XLAT_BOX:
1138 XLAT_DEBUG("** [%i] %s(value_box) - %s", unlang_interpret_stack_depth(request), __FUNCTION__, node->fmt);
1139
1140 /*
1141 * Empty boxes are only allowed if
1142 * they're the only node in the expansion.
1143 *
1144 * If they're found anywhere else the xlat
1145 * parser has an error.
1146 */
1147 fr_assert(((node == *in) && !xlat_exp_next(head, node)) || (talloc_array_length(node->fmt) > 1));
1148
1149 /*
1150 * We unfortunately need to dup the buffer
1151 * because references aren't threadsafe.
1152 */
1154 if (fr_value_box_copy(value, value, &node->data) < 0) goto fail;
1156 continue;
1157
1158 case XLAT_ONE_LETTER:
1159 XLAT_DEBUG("** [%i] %s(one-letter) - %%%s", unlang_interpret_stack_depth(request), __FUNCTION__,
1160 node->fmt);
1161
1162 xlat_debug_log_expansion(request, node, NULL, __LINE__);
1163 if (xlat_eval_one_letter(ctx, &result, request, node->fmt[0]) == XLAT_ACTION_FAIL) {
1164 fail:
1165 fr_value_box_list_talloc_free(&result);
1166 xa = XLAT_ACTION_FAIL;
1167 goto finish;
1168 }
1169 xlat_debug_log_list_result(request, *in, &result);
1170 fr_value_box_list_move((fr_value_box_list_t *)out->dlist, &result);
1171 continue;
1172
1173 case XLAT_TMPL:
1174 /*
1175 * Everything should have been resolved.
1176 */
1177 fr_assert(!tmpl_needs_resolving(node->vpt));
1178
1179 if (tmpl_is_data(node->vpt)) {
1180 XLAT_DEBUG("** [%i] %s(value) - %s", unlang_interpret_stack_depth(request), __FUNCTION__,
1181 node->vpt->name);
1182
1183 MEM(value = fr_value_box_alloc(ctx, tmpl_value_type(node->vpt), NULL));
1184
1185 fr_value_box_copy(value, value, tmpl_value(node->vpt)); /* Also dups taint */
1186 fr_value_box_list_insert_tail(&result, value);
1187
1188 /*
1189 * Cast the results if necessary.
1190 */
1191 if (tmpl_eval_cast_in_place(&result, request, node->vpt) < 0) goto fail;
1192
1193 fr_value_box_list_move((fr_value_box_list_t *)out->dlist, &result);
1194 continue;
1195
1196 } else if (tmpl_is_attr(node->vpt)) {
1197 if (node->fmt[0] == '&') {
1198 XLAT_DEBUG("** [%i] %s(attribute) - %s", unlang_interpret_stack_depth(request), __FUNCTION__,
1199 node->fmt);
1200 } else {
1201 XLAT_DEBUG("** [%i] %s(attribute) - %%{%s}", unlang_interpret_stack_depth(request), __FUNCTION__,
1202 node->fmt);
1203 }
1204 xlat_debug_log_expansion(request, node, NULL, __LINE__);
1205
1206 if (tmpl_eval_pair(ctx, &result, request, node->vpt) < 0) goto fail;
1207
1208 } else if (tmpl_is_exec(node->vpt) || tmpl_is_xlat(node->vpt)) {
1209 xlat_exec_rctx_t *rctx;
1210
1211 /*
1212 * Allocate and initialize the output context, with value-boxes, exec status, etc.
1213 */
1214 MEM(rctx = talloc_zero(unlang_interpret_frame_talloc_ctx(request), xlat_exec_rctx_t));
1215 fr_value_box_list_init(&rctx->list);
1216
1217 xlat_debug_log_expansion(request, node, NULL, __LINE__);
1218
1219 if (unlang_xlat_yield(request, xlat_exec_resume, NULL, 0, rctx) != XLAT_ACTION_YIELD) goto fail;
1220
1221 if (unlang_tmpl_push(ctx, &rctx->list, request, node->vpt,
1223 false, &rctx->status)) < 0) goto fail;
1224
1226 goto finish;
1227
1228 } else {
1229#ifdef NDEBUG
1230 xa = XLAT_ACTION_FAIL;
1231 goto finish;
1232#endif
1233
1234 /*
1235 * Either this should have been handled previously, or we need to write
1236 * code to deal with this case.
1237 */
1238 fr_assert(0);
1239 }
1240
1241 xlat_debug_log_list_result(request, node, &result);
1242 fr_value_box_list_move((fr_value_box_list_t *)out->dlist, &result);
1243 continue;
1244
1245 case XLAT_FUNC:
1246 XLAT_DEBUG("** [%i] %s(func) - %%%s(...)", unlang_interpret_stack_depth(request), __FUNCTION__,
1247 node->fmt);
1248
1249 /*
1250 * Hand back the child node to the caller
1251 * for evaluation.
1252 */
1253 if (xlat_exp_head(node->call.args)) {
1254 *child = node->call.args;
1256 goto finish;
1257 }
1258
1259 /*
1260 * If there's no children we can just
1261 * call the function directly.
1262 */
1263 xa = xlat_frame_eval_repeat(ctx, out, child, request, head, in, NULL, &result);
1264 if (xa != XLAT_ACTION_DONE || (!*in)) goto finish;
1265 continue;
1266
1267#ifdef HAVE_REGEX
1268 case XLAT_REGEX:
1269 {
1270 char *str = NULL;
1271
1272 XLAT_DEBUG("** [%i] %s(regex) - %%{%s}", unlang_interpret_stack_depth(request), __FUNCTION__,
1273 node->fmt);
1274
1275 xlat_debug_log_expansion(request, node, NULL, __LINE__);
1277 if (regex_request_to_sub(ctx, &str, request, node->regex_index) < 0) {
1279 continue;
1280 }
1281 fr_value_box_bstrdup_buffer_shallow(NULL, value, NULL, str, false);
1282
1283 xlat_debug_log_result(request, node, value);
1285 }
1286 continue;
1287#endif
1288
1289 case XLAT_GROUP:
1290 XLAT_DEBUG("** [%i] %s(child) - %%{%s ...}", unlang_interpret_stack_depth(request), __FUNCTION__,
1291 node->fmt);
1292 if (!node->group) return XLAT_ACTION_DONE;
1293
1294 /*
1295 * Hand back the child node to the caller
1296 * for evaluation.
1297 */
1298 *child = node->group;
1300 goto finish;
1301
1302 /*
1303 * Should have been fixed up during pass2
1304 */
1305 case XLAT_INVALID:
1307 fr_assert(0);
1308 return XLAT_ACTION_FAIL;
1309 }
1310 }
1311
1312finish:
1313 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
1314 XLAT_DEBUG("** [%i] %s << %s", unlang_interpret_stack_depth(request),
1315 __FUNCTION__, fr_table_str_by_value(xlat_action_table, xa, "<INVALID>"));
1316
1317 return xa;
1318}
1319
1320static ssize_t xlat_eval_sync(TALLOC_CTX *ctx, char **out, request_t *request, xlat_exp_head_t const * const head,
1321 xlat_escape_legacy_t escape, void const *escape_ctx)
1322{
1323 fr_value_box_list_t result;
1324 bool success = false;
1325 TALLOC_CTX *pool = talloc_new(NULL);
1326 rlm_rcode_t rcode;
1327 char *str;
1328
1329 XLAT_DEBUG("xlat_eval_sync");
1330
1331 *out = NULL;
1332
1333 fr_value_box_list_init(&result);
1334
1335 /*
1336 * Use the unlang stack to evaluate the xlat.
1337 */
1338 if (unlang_xlat_push(pool, &success, &result, request, head, UNLANG_TOP_FRAME) < 0) {
1339 fail:
1340 talloc_free(pool);
1341 return -1;
1342 }
1343
1344 /*
1345 * Pure functions don't yield, and can therefore be
1346 * expanded in place. This check saves an expensive
1347 * bounce through a new synchronous interpreter.
1348 */
1349 if (!xlat_impure_func(head) && unlang_interpret_get(request)) {
1350 rcode = unlang_interpret(request, UNLANG_REQUEST_RUNNING);
1351 } else {
1353 }
1354
1355 switch (rcode) {
1356 default:
1357 if (!success) goto fail;
1358 break;
1359
1360 case RLM_MODULE_REJECT:
1361 case RLM_MODULE_FAIL:
1362 goto fail;
1363 }
1364
1365 if (!fr_value_box_list_empty(&result)) {
1366 if (escape) {
1367 fr_value_box_t *vb = NULL;
1368
1369 /*
1370 * For tainted boxes perform the requested escaping
1371 */
1372 while ((vb = fr_value_box_list_next(&result, vb))) {
1373 fr_value_box_entry_t entry;
1374 size_t len, real_len;
1375 char *escaped;
1376
1377 if (!vb->tainted) continue;
1378
1379 if (fr_value_box_cast_in_place(pool, vb, FR_TYPE_STRING, NULL) < 0) {
1380 RPEDEBUG("Failed casting result to string");
1381 goto fail;
1382 }
1383
1384 len = vb->vb_length * 3;
1385 MEM(escaped = talloc_array(pool, char, len));
1386 real_len = escape(request, escaped, len, vb->vb_strvalue, UNCONST(void *, escape_ctx));
1387
1388 entry = vb->entry;
1390 fr_value_box_bstrndup(vb, vb, NULL, escaped, real_len, false);
1391 vb->entry = entry;
1392
1393 talloc_free(escaped);
1394 }
1395 }
1396
1397 str = fr_value_box_list_aprint(ctx, &result, NULL, NULL);
1398 if (!str) goto fail;
1399 } else {
1400 str = talloc_typed_strdup(ctx, "");
1401 }
1402 talloc_free(pool); /* Memory should be in new ctx */
1403
1404 *out = str;
1405
1406 return talloc_array_length(str) - 1;
1407}
1408
1409/** Replace %whatever in a string.
1410 *
1411 * See 'doc/unlang/xlat.adoc' for more information.
1412 *
1413 * @param[in] ctx to allocate expansion buffers in.
1414 * @param[out] out Where to write pointer to output buffer.
1415 * @param[in] outlen Size of out.
1416 * @param[in] request current request.
1417 * @param[in] head the xlat structure to expand
1418 * @param[in] escape function to escape final value e.g. SQL quoting.
1419 * @param[in] escape_ctx pointer to pass to escape function.
1420 * @return length of string written @bug should really have -1 for failure.
1421 */
1422static ssize_t _xlat_eval_compiled(TALLOC_CTX *ctx, char **out, size_t outlen, request_t *request,
1423 xlat_exp_head_t const *head, xlat_escape_legacy_t escape, void const *escape_ctx)
1424{
1425 char *buff;
1426 ssize_t slen;
1427
1428 fr_assert(head != NULL);
1429
1430 slen = xlat_eval_sync(ctx, &buff, request, head, escape, escape_ctx);
1431 if (slen < 0) {
1432 fr_assert(buff == NULL);
1433 if (*out) **out = '\0';
1434 return slen;
1435 }
1436
1437 /*
1438 * If out doesn't point to an existing buffer
1439 * copy the pointer to our buffer over.
1440 */
1441 if (!*out) {
1442 *out = buff;
1443 return slen;
1444 }
1445
1446 if ((size_t)slen >= outlen) {
1447 fr_strerror_const("Insufficient output buffer space");
1448 return -1;
1449 }
1450
1451 /*
1452 * Otherwise copy the talloced buffer to the fixed one.
1453 */
1454 memcpy(*out, buff, slen);
1455 (*out)[slen] = '\0';
1457
1458 return slen;
1459}
1460
1461/** Replace %whatever in a string.
1462 *
1463 * See 'doc/unlang/xlat.adoc' for more information.
1464 *
1465 * @param[in] ctx to allocate expansion buffers in.
1466 * @param[out] out Where to write pointer to output buffer.
1467 * @param[in] outlen Size of out.
1468 * @param[in] request current request.
1469 * @param[in] fmt string to expand.
1470 * @param[in] escape function to escape final value e.g. SQL quoting.
1471 * @param[in] escape_ctx pointer to pass to escape function.
1472 * @return length of string written @bug should really have -1 for failure.
1473 */
1474static CC_HINT(nonnull (2, 4, 5))
1475ssize_t _xlat_eval(TALLOC_CTX *ctx, char **out, size_t outlen, request_t *request, char const *fmt,
1476 xlat_escape_legacy_t escape, void const *escape_ctx)
1477{
1478 ssize_t len;
1480
1481 RINDENT();
1482
1483 /*
1484 * Give better errors than the old code.
1485 */
1486 len = xlat_tokenize(ctx, &head,
1487 &FR_SBUFF_IN(fmt, strlen(fmt)),
1488 NULL,
1489 &(tmpl_rules_t){
1490 .attr = {
1491 .dict_def = request->dict,
1492 .list_def = request_attr_request,
1493 },
1494 .xlat = {
1495 .runtime_el = unlang_interpret_event_list(request),
1496 },
1497 .at_runtime = true,
1498 });
1499 if (len == 0) {
1500 if (*out) {
1501 **out = '\0';
1502 } else {
1503 *out = talloc_zero_array(ctx, char, 1);
1504 }
1505 REXDENT();
1506 return 0;
1507 }
1508
1509 if (len < 0) {
1510 REMARKER(fmt, -(len), "%s", fr_strerror());
1511 if (*out) **out = '\0';
1512 REXDENT();
1513 return -1;
1514 }
1515
1516 len = _xlat_eval_compiled(ctx, out, outlen, request, head, escape, escape_ctx);
1518
1519 REXDENT();
1520
1521 return len;
1522}
1523
1524ssize_t xlat_eval(char *out, size_t outlen, request_t *request,
1525 char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx)
1526{
1528
1529 return _xlat_eval(request, &out, outlen, request, fmt, escape, escape_ctx);
1530}
1531
1532ssize_t xlat_eval_compiled(char *out, size_t outlen, request_t *request,
1533 xlat_exp_head_t const *xlat, xlat_escape_legacy_t escape, void const *escape_ctx)
1534{
1536
1537 return _xlat_eval_compiled(request, &out, outlen, request, xlat, escape, escape_ctx);
1538}
1539
1540ssize_t xlat_aeval(TALLOC_CTX *ctx, char **out, request_t *request, char const *fmt,
1541 xlat_escape_legacy_t escape, void const *escape_ctx)
1542{
1544
1545 *out = NULL;
1546 return _xlat_eval(ctx, out, 0, request, fmt, escape, escape_ctx);
1547}
1548
1549ssize_t xlat_aeval_compiled(TALLOC_CTX *ctx, char **out, request_t *request,
1550 xlat_exp_head_t const *xlat, xlat_escape_legacy_t escape, void const *escape_ctx)
1551{
1553
1554 *out = NULL;
1555 return _xlat_eval_compiled(ctx, out, 0, request, xlat, escape, escape_ctx);
1556}
1557
1558
1559/** Turn am xlat list into an argv[] array, and nuke the input list.
1560 *
1561 * This is mostly for async use.
1562 */
1564{
1565 int i;
1566 xlat_exp_head_t **my_argv;
1567 size_t count;
1568
1569 if (head->flags.needs_resolving) {
1570 fr_strerror_printf("Cannot flatten expression with unresolved functions");
1571 return -1;
1572 }
1573
1574 count = 0;
1575 xlat_exp_foreach(head, node) {
1576 count++;
1577 }
1578
1579 MEM(my_argv = talloc_zero_array(ctx, xlat_exp_head_t *, count + 1));
1580 *argv = my_argv;
1581
1583
1584 i = 0;
1585 xlat_exp_foreach(head, node) {
1586 fr_assert(node->type == XLAT_GROUP);
1587 my_argv[i++] = talloc_steal(my_argv, node->group);
1588 }
1589
1590 fr_value_box_list_talloc_free((fr_value_box_list_t *)&head->dlist);
1591
1592 return count;
1593}
1594
1595/** Walk over all xlat nodes (depth first) in a xlat expansion, calling a callback
1596 *
1597 * @param[in] head to evaluate.
1598 * @param[in] walker callback to pass nodes to.
1599 * @param[in] type if > 0 a mask of types to call walker for.
1600 * @param[in] uctx to pass to walker.
1601 * @return
1602 * - 0 on success (walker always returned 0).
1603 * - <0 if walker returned <0.
1604 */
1606{
1607 int ret;
1608
1609 /*
1610 * Iterate over nodes at the same depth
1611 */
1612 xlat_exp_foreach(head, node) {
1613 switch (node->type){
1614 case XLAT_FUNC:
1615 /*
1616 * Evaluate the function's arguments
1617 * first, as they may get moved around
1618 * when the function is instantiated.
1619 */
1620 if (xlat_exp_head(node->call.args)) {
1621 ret = xlat_eval_walk(node->call.args, walker, type, uctx);
1622 if (ret < 0) return ret;
1623 }
1624
1625 if (!type || (type & XLAT_FUNC)) {
1626 ret = walker(node, uctx);
1627 if (ret < 0) return ret;
1628 }
1629 break;
1630
1632 if (xlat_exp_head(node->call.args)) {
1633 ret = xlat_eval_walk(node->call.args, walker, type, uctx);
1634 if (ret < 0) return ret;
1635 }
1636
1637 if (!type || (type & XLAT_FUNC_UNRESOLVED)) {
1638 ret = walker(node, uctx);
1639 if (ret < 0) return ret;
1640 }
1641 break;
1642
1643 case XLAT_GROUP:
1644 if (!type || (type & XLAT_GROUP)) {
1645 ret = walker(node, uctx);
1646 if (ret < 0) return ret;
1647 if (ret > 0) continue;
1648 }
1649
1650 /*
1651 * Evaluate the child.
1652 */
1653 ret = xlat_eval_walk(node->group, walker, type, uctx);
1654 if (ret < 0) return ret;
1655 break;
1656
1657 default:
1658 if (!type || (type & node->type)) {
1659 ret = walker(node, uctx);
1660 if (ret < 0) return ret;
1661 }
1662 break;
1663 }
1664 }
1665
1666 return 0;
1667}
1668
1670{
1672
1673 if (instance_count > 0) {
1675 return 0;
1676 }
1677
1679
1681 PERROR("%s", __FUNCTION__);
1682 return -1;
1683 }
1684
1686 PERROR("%s", __FUNCTION__);
1688 return -1;
1689 }
1690
1691 return 0;
1692}
1693
1695{
1697
1698 if (--instance_count > 0) return;
1699
1701}
static int const char char buffer[256]
Definition acutest.h:576
va_list args
Definition acutest.h:770
static int const char * fmt
Definition acutest.h:573
int const char int line
Definition acutest.h:702
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:483
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define UNUSED
Definition build.h:315
#define NUM_ELEMENTS(_t)
Definition build.h:337
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
static void * fr_dcursor_tail(fr_dcursor_t *cursor)
Wind cursor to the tail item in the list.
Definition dcursor.h:259
static int fr_dcursor_insert(fr_dcursor_t *cursor, void *v)
Insert directly after the current item.
Definition dcursor.h:435
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define fr_assert_fail(_msg,...)
Calls panic_action ifndef NDEBUG, else logs error.
Definition debug.h:216
#define MEM(x)
Definition debug.h:36
#define fr_dict_autofree(_to_free)
Definition dict.h:856
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:270
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:283
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4097
#define fr_dict_autoload(_to_load)
Definition dict.h:853
static fr_slen_t in
Definition dict.h:827
Specifies an attribute which must be present for the module to function.
Definition dict.h:269
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:282
Test enumeration values.
Definition dict_test.h:92
#define EXEC_TIMEOUT
Default wait time for exec calls (in seconds).
Definition exec.h:32
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:782
unlang_interpret_t * unlang_interpret_get(request_t *request)
Get the interpreter set for a request.
Definition interpret.c:1769
int unlang_interpret_stack_depth(request_t *request)
Return the depth of the request's stack.
Definition interpret.c:1290
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:1418
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:1779
#define UNLANG_TOP_FRAME
Definition interpret.h:35
#define UNLANG_REQUEST_RUNNING
Definition interpret.h:41
rlm_rcode_t unlang_interpret_synchronous(fr_event_list_t *el, request_t *request)
Execute an unlang section synchronously.
#define PERROR(_fmt,...)
Definition log.h:228
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:443
#define RDEBUG3(fmt,...)
Definition log.h:343
#define RERROR(fmt,...)
Definition log.h:298
#define REMARKER(_str, _marker_idx, _marker,...)
Output string with error marker, showing where format error occurred.
Definition log.h:498
#define RPEDEBUG(fmt,...)
Definition log.h:376
#define RINDENT()
Indent R* messages by one level.
Definition log.h:430
talloc_free(reap)
size_t(* xlat_escape_legacy_t)(request_t *request, char *out, size_t outlen, char const *in, void *arg)
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_GROUP
A grouping of other attributes.
long int ssize_t
unsigned char bool
ssize_t fr_slen_t
struct tm * gmtime_r(time_t const *l_clock, struct tm *result)
Definition missing.c:201
struct tm * localtime_r(time_t const *l_clock, struct tm *result)
Definition missing.c:163
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG_ENABLED2()
Definition radclient.h:50
#define RDEBUG2(fmt,...)
Definition radclient.h:54
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:41
fr_dict_attr_t const * request_attr_request
Definition request.c:45
static char const * name
ssize_t fr_sbuff_in_strcpy(fr_sbuff_t *sbuff, char const *str)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1454
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
Definition sbuff.c:1595
#define fr_sbuff_start(_sbuff_or_marker)
#define FR_SBUFF_IN_CHAR_RETURN(_sbuff,...)
#define fr_sbuff_set(_dst, _src)
#define FR_SBUFF_IN(_start, _len_or_end)
#define FR_SBUFF_IN_SPRINTF_RETURN(...)
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_used(_sbuff_or_marker)
#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
#define tmpl_is_xlat(vpt)
Definition tmpl.h:215
#define tmpl_value(_tmpl)
Definition tmpl.h:954
#define tmpl_is_attr(vpt)
Definition tmpl.h:213
#define tmpl_is_exec(vpt)
Definition tmpl.h:216
int tmpl_eval_cast_in_place(fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
Casts a value or list of values according to the tmpl.
Definition tmpl_eval.c:1349
int tmpl_eval_pair(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
Gets the value of a real or virtual attribute.
Definition tmpl_eval.c:1077
#define tmpl_is_data(vpt)
Definition tmpl.h:211
#define tmpl_value_type(_tmpl)
Definition tmpl.h:956
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:228
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:347
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
return count
Definition module.c:163
fr_aka_sim_id_type_t type
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#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
An element in an arbitrarily ordered array of name to ptr mappings.
Definition table.h:73
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:445
static int64_t fr_time_to_sec(fr_time_t when)
Convert an fr_time_t (internal time) to number of sec since the unix epoch (wallclock time)
Definition time.h:731
static int64_t fr_time_to_msec(fr_time_t when)
Convert an fr_time_t (internal time) to number of msec since the unix epoch (wallclock time)
Definition time.h:711
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
static int64_t fr_time_to_usec(fr_time_t when)
Convert an fr_time_t (internal time) to number of usec since the unix epoch (wallclock time)
Definition time.h:701
int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:262
goto success
Definition tmpl_eval.c:1455
#define TMPL_ARGS_EXEC(_env, _timeout, _stdout_on_error, _status_out)
Create a temporary argument structure for evaluating an exec type tmpl.
Definition tmpl.h:75
xlat_action_t unlang_xlat_yield(request_t *request, xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition xlat.c:573
int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:286
fr_type_t type
Type to cast argument to.
Definition xlat.h:155
void * data
Thread specific instance data.
Definition xlat.h:98
xlat_thread_inst_t * xlat_thread_instance_find(xlat_exp_t const *node)
Retrieve xlat/thread specific instance data.
Definition xlat_inst.c:407
bool xlat_is_literal(xlat_exp_head_t const *head)
Check to see if the expansion consists entirely of value-box elements.
bool required
Argument must be present, and non-empty.
Definition xlat.h:148
bool xlat_impure_func(xlat_exp_head_t const *head)
xlat_escape_func_t func
Function to handle tainted values.
Definition xlat.h:156
fr_slen_t xlat_tokenize(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules)
Tokenize an xlat expansion.
@ XLAT_ARG_VARIADIC_EMPTY_KEEP
Empty argument groups are left alone, and either passed through as empty groups or null boxes.
Definition xlat.h:139
@ XLAT_ARG_VARIADIC_EMPTY_SQUASH
Empty argument groups are removed.
Definition xlat.h:138
@ XLAT_ARG_VARIADIC_DISABLED
Definition xlat.h:137
static fr_slen_t head
Definition xlat.h:418
xlat_arg_parser_variadic_t variadic
All additional boxes should be processed using this definition.
Definition xlat.h:151
bool concat
Concat boxes together.
Definition xlat.h:149
bool single
Argument must only contain a single box.
Definition xlat.h:150
void(* xlat_func_signal_t)(xlat_ctx_t const *xctx, request_t *request, fr_signal_t action)
A callback when the request gets a fr_signal_t.
Definition xlat.h:241
@ XLAT_INPUT_ARGS
Ingests a number of arguments.
Definition xlat.h:49
@ XLAT_INPUT_UNPROCESSED
No input argument processing.
Definition xlat.h:48
xlat_action_t(* xlat_func_t)(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
xlat callback function
Definition xlat.h:230
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_YIELD
An xlat function pushed a resume frame onto the stack.
Definition xlat.h:42
@ XLAT_ACTION_PUSH_UNLANG
An xlat function pushed an unlang frame onto the unlang stack.
Definition xlat.h:39
@ XLAT_ACTION_PUSH_CHILD
A deeper level of nesting needs to be evaluated.
Definition xlat.h:38
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
bool constant
xlat is just tmpl_attr_tail_data, or XLAT_BOX
Definition xlat.h:118
module_ctx_t const * mctx
A synthesised module calling ctx containing module global and thread instance data.
Definition xlat.h:100
Definition for a single argument consumend by an xlat function.
Definition xlat.h:147
Thread specific instance data for xlat expansion node.
Definition xlat.h:89
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:554
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_type_is_void(_x)
Definition types.h:357
#define fr_type_is_null(_x)
Definition types.h:326
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
char * fr_value_box_list_aprint(TALLOC_CTX *ctx, fr_value_box_list_t const *list, char const *delim, fr_sbuff_escape_rules_t const *e_rules)
Concatenate the string representations of a list of value boxes together.
Definition value.c:6095
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:3740
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:3572
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:3681
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:3937
int fr_value_box_list_acopy(TALLOC_CTX *ctx, fr_value_box_list_t *out, fr_value_box_list_t const *in)
Do a full copy of a list of value boxes.
Definition value.c:6225
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4158
int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a talloced buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4263
int fr_value_box_list_concat_in_place(TALLOC_CTX *ctx, fr_value_box_t *out, fr_value_box_list_t *list, fr_type_t type, fr_value_box_list_action_t proc_action, bool flatten, size_t max_size)
Concatenate a list of value boxes.
Definition value.c:5833
@ FR_VALUE_BOX_LIST_FREE
Definition value.h:221
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:621
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1048
#define fr_box_strvalue_buffer(_val)
Definition value.h:289
#define VALUE_BOX_VERIFY(_x)
Definition value.h:1295
#define VALUE_BOX_LIST_VERIFY(_x)
Definition value.h:1296
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:632
static size_t char ** out
Definition value.h:997
void * rctx
Resume context.
Definition xlat_ctx.h:54
#define XLAT_CTX(_inst, _thread, _mctx, _env_data, _rctx)
Wrapper to create a xlat_ctx_t as a compound literal.
Definition xlat_ctx.h:93
An xlat calling ctx.
Definition xlat_ctx.h:49
ssize_t xlat_eval_compiled(char *out, size_t outlen, request_t *request, xlat_exp_head_t const *xlat, xlat_escape_legacy_t escape, void const *escape_ctx)
Definition xlat_eval.c:1532
static size_t xlat_time_precision_table_len
Definition xlat_eval.c:129
static fr_dict_attr_t const * attr_cast_time_res_week
Definition xlat_eval.c:53
static fr_table_ptr_ordered_t const xlat_time_precision_table[]
Definition xlat_eval.c:91
static fr_slen_t xlat_fmt_print(fr_sbuff_t *out, xlat_exp_t const *node)
Reconstruct the original expansion string from an xlat tree.
Definition xlat_eval.c:152
static void xlat_debug_log_expansion(request_t *request, xlat_exp_t const *node, fr_value_box_list_t const *args, UNUSED int line)
Output what we're currently expanding.
Definition xlat_eval.c:218
xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_head_t const **child, request_t *request, xlat_exp_head_t const *head, xlat_exp_t const **in, void *env_data, fr_value_box_list_t *result)
Process the result of a previous nested expansion.
Definition xlat_eval.c:938
void xlat_signal(xlat_func_signal_t signal, xlat_exp_t const *exp, request_t *request, void *rctx, fr_signal_t action)
Signal an xlat function.
Definition xlat_eval.c:830
fr_dict_attr_t const * xlat_time_res_attr(char const *res)
Definition xlat_eval.c:131
static void xlat_debug_log_result(request_t *request, xlat_exp_t const *node, fr_value_box_t const *result)
Output the result of an expansion.
Definition xlat_eval.c:266
int xlat_eval_init(void)
Definition xlat_eval.c:1669
fr_table_num_sorted_t const xlat_action_table[]
Definition xlat_eval.c:80
xlat_action_t xlat_frame_eval_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_head_t const **child, request_t *request, xlat_exp_head_t const *head, xlat_exp_t const **in, fr_value_box_list_t *result, xlat_func_t resume, void *rctx)
Call an xlat's resumption method.
Definition xlat_eval.c:864
static fr_dict_attr_t const * attr_cast_time_res_year
Definition xlat_eval.c:55
static fr_dict_t const * dict_freeradius
Definition xlat_eval.c:39
static fr_dict_attr_t const * attr_cast_time_res_nsec
Definition xlat_eval.c:59
static xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, char letter)
One letter expansions.
Definition xlat_eval.c:613
static fr_dict_attr_t const * attr_cast_time_res_month
Definition xlat_eval.c:54
static fr_dict_attr_t const * attr_cast_time_res_usec
Definition xlat_eval.c:58
fr_dict_attr_t const * attr_expr_bool_enum
Definition xlat_eval.c:46
static fr_dict_attr_t const * attr_cast_time_res_csec
Definition xlat_eval.c:56
static xlat_action_t xlat_process_args(TALLOC_CTX *ctx, fr_value_box_list_t *list, request_t *request, xlat_t const *func)
Process list of boxed values provided as input to an xlat.
Definition xlat_eval.c:403
size_t xlat_action_table_len
Definition xlat_eval.c:86
static ssize_t _xlat_eval(TALLOC_CTX *ctx, char **out, size_t outlen, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx)
Replace whatever in a string.
Definition xlat_eval.c:1475
static fr_dict_autoload_t xlat_eval_dict[]
Definition xlat_eval.c:41
static fr_dict_attr_t const * attr_cast_time_res_msec
Definition xlat_eval.c:57
fr_dict_attr_t const * attr_cast_base
Definition xlat_eval.c:47
fr_value_box_list_t list
Definition xlat_eval.c:801
ssize_t xlat_eval(char *out, size_t outlen, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx)
Definition xlat_eval.c:1524
xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_head_t const **child, request_t *request, xlat_exp_head_t const *head, xlat_exp_t const **in)
Converts xlat nodes to value boxes.
Definition xlat_eval.c:1113
int xlat_flatten_to_argv(TALLOC_CTX *ctx, xlat_exp_head_t ***argv, xlat_exp_head_t *head)
Turn am xlat list into an argv[] array, and nuke the input list.
Definition xlat_eval.c:1563
static fr_dict_attr_autoload_t xlat_eval_dict_attr[]
Definition xlat_eval.c:61
static bool xlat_process_return(request_t *request, xlat_t const *func, fr_value_box_list_t const *returned, fr_value_box_t *pos)
Validate that the return values from an xlat function match what it registered.
Definition xlat_eval.c:565
void xlat_eval_free(void)
Definition xlat_eval.c:1694
int xlat_eval_walk(xlat_exp_head_t *head, xlat_walker_t walker, xlat_type_t type, void *uctx)
Walk over all xlat nodes (depth first) in a xlat expansion, calling a callback.
Definition xlat_eval.c:1605
static fr_dict_attr_t const * attr_cast_time_res_sec
Definition xlat_eval.c:49
static xlat_action_t xlat_null_resume(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
Definition xlat_eval.c:838
ssize_t xlat_aeval(TALLOC_CTX *ctx, char **out, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx)
Definition xlat_eval.c:1540
static fr_dict_attr_t const * attr_cast_time_res_day
Definition xlat_eval.c:52
static ssize_t _xlat_eval_compiled(TALLOC_CTX *ctx, char **out, size_t outlen, request_t *request, xlat_exp_head_t const *head, xlat_escape_legacy_t escape, void const *escape_ctx)
Replace whatever in a string.
Definition xlat_eval.c:1422
ssize_t xlat_aeval_compiled(TALLOC_CTX *ctx, char **out, request_t *request, xlat_exp_head_t const *xlat, xlat_escape_legacy_t escape, void const *escape_ctx)
Definition xlat_eval.c:1549
static void xlat_debug_log_list_result(request_t *request, xlat_exp_t const *node, fr_value_box_list_t const *result)
Output the list result of an expansion.
Definition xlat_eval.c:251
static fr_dict_attr_t const * attr_cast_time_res_min
Definition xlat_eval.c:50
static ssize_t xlat_eval_sync(TALLOC_CTX *ctx, char **out, request_t *request, xlat_exp_head_t const *const head, xlat_escape_legacy_t escape, void const *escape_ctx)
Definition xlat_eval.c:1320
static xlat_action_t xlat_exec_resume(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
Definition xlat_eval.c:804
static xlat_action_t xlat_process_arg_list(TALLOC_CTX *ctx, fr_value_box_list_t *list, request_t *request, char const *name, xlat_arg_parser_t const *arg, unsigned int arg_num)
Process an individual xlat argument value box group.
Definition xlat_eval.c:287
static fr_dict_attr_t const * attr_cast_time_res_hour
Definition xlat_eval.c:51
#define ESCAPE(_arg, _vb, _arg_num)
static int instance_count
Definition xlat_eval.c:37
#define XLAT_DEBUG(...)
Definition xlat_expr.c:39
char const * name
Name of xlat function.
Definition xlat_priv.h:64
xlat_flags_t flags
Flags that control resolution and evaluation.
Definition xlat_priv.h:155
fr_type_t return_type
Function is guaranteed to return one or more boxes of this type.
Definition xlat_priv.h:100
static xlat_exp_t * xlat_exp_next(xlat_exp_head_t const *head, xlat_exp_t const *node)
Definition xlat_priv.h:242
int(* xlat_walker_t)(xlat_exp_t *exp, void *uctx)
Walker callback for xlat_walk()
Definition xlat_priv.h:263
xlat_type_t
Definition xlat_priv.h:105
@ XLAT_ONE_LETTER
Special "one-letter" expansion.
Definition xlat_priv.h:108
@ XLAT_BOX
fr_value_box_t
Definition xlat_priv.h:107
@ XLAT_TMPL
xlat attribute
Definition xlat_priv.h:111
@ XLAT_FUNC
xlat module
Definition xlat_priv.h:109
@ XLAT_GROUP
encapsulated string of xlats
Definition xlat_priv.h:115
@ XLAT_FUNC_UNRESOLVED
func needs resolution during pass2.
Definition xlat_priv.h:110
@ XLAT_INVALID
Bad expansion.
Definition xlat_priv.h:106
xlat_arg_parser_t const * args
Definition of args consumed.
Definition xlat_priv.h:93
xlat_input_type_t input_type
Type of input used.
Definition xlat_priv.h:92
char const *_CONST fmt
The original format string (a talloced buffer).
Definition xlat_priv.h:152
xlat_type_t _CONST type
type of this expansion.
Definition xlat_priv.h:156
fr_value_box_safe_for_t return_safe_for
Escaped value to set in output boxes.
Definition xlat_priv.h:99
#define xlat_exp_foreach(_list_head, _iter)
Iterate over the contents of a list, only one level.
Definition xlat_priv.h:218
static xlat_exp_t * xlat_exp_head(xlat_exp_head_t const *head)
Definition xlat_priv.h:205
An xlat expansion node.
Definition xlat_priv.h:149