The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
xlat.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: 1073c8a7bd467c2910720bef8efcd49622c4d882 $
19 *
20 * @file unlang/xlat.c
21 * @brief Integration between the unlang interpreter and xlats
22 *
23 * @copyright 2018 The FreeRADIUS server project
24 * @copyright 2018 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25 */
26RCSID("$Id: 1073c8a7bd467c2910720bef8efcd49622c4d882 $")
27
28#include <freeradius-devel/server/base.h>
29
30#include <ctype.h>
31#include <freeradius-devel/unlang/xlat_priv.h>
32#include <freeradius-devel/util/debug.h>
33#include "unlang_priv.h" /* Fixme - Should create a proper semi-public interface for the interpret */
34
35/** State of an xlat expansion
36 *
37 * State of one level of nesting within an xlat expansion.
38 */
39typedef struct {
40 TALLOC_CTX *ctx; //!< to allocate boxes and values in.
41 TALLOC_CTX *event_ctx; //!< for temporary events
42 xlat_exp_head_t const *head; //!< of the xlat list
43 xlat_exp_t const *exp; //!< current one we're evaluating
44 fr_dcursor_t values; //!< Values aggregated so far.
45
46 rindent_t indent; //!< indentation
47
48 void *env_data; //!< Expanded per call environment tmpls.
49 /*
50 * For func and alternate
51 */
52 fr_value_box_list_t out; //!< Head of the result of a nested
53 ///< expansion.
54 xlat_func_t resume; //!< called on resume
55 xlat_func_signal_t signal; //!< called on signal
56 fr_signal_t sigmask; //!< Signals to block
57 void *rctx; //!< for resume / signal
58
59 unlang_result_t *p_result; //!< If set, where to record the result
60 ///< of the execution.
62
63/** Wrap an #fr_timer_t providing data needed for unlang events
64 *
65 */
66typedef struct {
67 request_t *request; //!< Request this event pertains to.
68 int fd; //!< File descriptor to wait on.
69 fr_unlang_xlat_timeout_t timeout; //!< Function to call on timeout.
70 fr_unlang_xlat_fd_event_t fd_read; //!< Function to call when FD is readable.
71 fr_unlang_xlat_fd_event_t fd_write; //!< Function to call when FD is writable.
72 fr_unlang_xlat_fd_event_t fd_error; //!< Function to call when FD has errored.
73 xlat_inst_t *inst; //!< xlat instance data.
74 xlat_thread_inst_t *thread; //!< Thread specific xlat instance.
75 void const *rctx; //!< rctx data to pass to callbacks.
76 fr_timer_t *ev; //!< Event in this worker's event heap.
78
79typedef struct {
81 xlat_inst_t *inst; //!< xlat instance data.
82 xlat_thread_inst_t *thread; //!< Thread specific xlat instance.
83
84 fr_unlang_xlat_retry_t retry_cb; //!< callback to run on timeout
85 void *rctx; //!< rctx data to pass to timeout callback
86
87 fr_timer_t *ev; //!< retry timer just for this xlat
88 fr_retry_t retry; //!< retry timers, etc.
90
91/** Frees an unlang event, removing it from the request's event loop
92 *
93 * @param[in] ev The event to free.
94 *
95 * @return 0
96 */
98{
99 FR_TIMER_DELETE(&(ev->ev));
100
101 if (ev->fd >= 0) {
103 }
104
105 return 0;
106}
107
108/** Call the callback registered for a timeout event
109 *
110 * @param[in] tl the event timer was inserted into.
111 * @param[in] now The current time, as held by the event_list.
112 * @param[in] uctx unlang_module_event_t structure holding callbacks.
113 *
114 */
116{
117 unlang_xlat_event_t *ev = talloc_get_type_abort(uctx, unlang_xlat_event_t);
118
119 /*
120 * If the timeout's fired then the xlat must necessarily
121 * be yielded, so it's fine to pass in its rctx.
122 *
123 * It should be able to free the rctx if it wants to.
124 * We never free it explicitly, and instead rely on
125 * talloc parenting.
126 */
127 ev->timeout(XLAT_CTX(ev->inst->data,
128 ev->thread->data,
129 NULL,
130 ev->thread->mctx, NULL,
131 UNCONST(void *, ev->rctx)),
132 ev->request, now);
133
134 /* Remove old references from the request */
135 talloc_free(ev);
136}
137
138/** Add a timeout for an xlat handler
139 *
140 * @note The timeout is automatically removed when the xlat is cancelled or resumed.
141 *
142 * @param[in] request the request
143 * @param[in] callback to run when the timeout hits
144 * @param[in] rctx passed to the callback
145 * @param[in] when when the timeout fires
146 * @return
147 * - <0 on error
148 * - 0 on success
149 */
151 fr_unlang_xlat_timeout_t callback, void const *rctx, fr_time_t when)
152{
153 unlang_stack_t *stack = request->stack;
154 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
156 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
157
158 fr_assert(stack->depth > 0);
160
161 if (!state->event_ctx) MEM(state->event_ctx = talloc_zero(state, bool));
162
163 MEM(ev = talloc_zero(state->event_ctx, unlang_xlat_event_t));
164 ev->request = request;
165 ev->fd = -1;
166 ev->timeout = callback;
167 fr_assert(state->exp->type == XLAT_FUNC);
168 ev->inst = state->exp->call.inst;
170 ev->rctx = rctx;
171
172 if (fr_timer_at(request, unlang_interpret_event_list(request)->tl,
173 &ev->ev, when,
174 false, unlang_xlat_event_timeout_handler, ev) < 0) {
175 RPEDEBUG("Failed inserting event");
176 talloc_free(ev);
177 return -1;
178 }
179
180 talloc_set_destructor(ev, _unlang_xlat_event_free);
181
182 return 0;
183}
184
185/** Push a pre-compiled xlat onto the stack for evaluation
186 *
187 * @param[in] ctx To allocate value boxes and values in.
188 * @param[out] p_result If set, rcodes and priorities will be written here and
189 * not evaluated by the unlang interpreter.
190 * @param[out] out Where to write the result of the expansion.
191 * @param[in] request to push xlat onto.
192 * @param[in] xlat head of list
193 * @param[in] node to evaluate.
194 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
195 * Set to UNLANG_SUB_FRAME if the interpreter should continue.
196 * @return
197 * - 0 on success.
198 * - -1 on failure.
199 */
200static int unlang_xlat_push_internal(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out,
201 request_t *request, xlat_exp_head_t const *xlat, xlat_exp_t *node, bool top_frame)
202{
203 /** Static instruction for performing xlat evaluations
204 *
205 */
206 static unlang_t xlat_instruction = {
208 .name = "xlat",
209 .debug_name = "xlat",
211 };
212
214 unlang_stack_t *stack = request->stack;
216
217 /*
218 * Push a new xlat eval frame onto the stack
219 */
220 if (unlang_interpret_push(p_result, request, &xlat_instruction,
221 FRAME_CONF(RLM_MODULE_NOT_SET, top_frame), UNLANG_NEXT_STOP) < 0) return -1;
222 frame = &stack->frame[stack->depth];
223
224 /*
225 * Allocate its state, and setup a cursor for the xlat nodes
226 */
227 MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_xlat_t));
228 state->head = xlat;
229 state->exp = node;
230 state->p_result = p_result;
231 state->ctx = ctx;
232
233 if (node) switch (node->type) {
234 case XLAT_GROUP:
235 case XLAT_BOX:
236 break;
237
238 case XLAT_TMPL:
239 if (tmpl_is_data(node->vpt)) break;
241
242 default:
243 RDEBUG("| %s", node->fmt);
244 break;
245 }
246
247 /*
248 * Initialise the input and output lists
249 */
250 fr_dcursor_init(&state->values, fr_value_box_list_dlist_head(out));
251 fr_value_box_list_init(&state->out);
252
253 return 0;
254}
255
256/** Push a pre-compiled xlat onto the stack for evaluation
257 *
258 * @param[in] ctx To allocate value boxes and values in.
259 * @param[out] p_result The frame result
260 * @param[out] out Where to write the result of the expansion.
261 * @param[in] request to push xlat onto.
262 * @param[in] xlat to evaluate.
263 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
264 * Set to UNLANG_SUB_FRAME if the interpreter should continue.
265 * @return
266 * - 0 on success.
267 * - -1 on failure.
268 */
269int unlang_xlat_push(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out,
270 request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
271{
273
274 return unlang_xlat_push_internal(ctx, p_result, out, request, xlat, xlat_exp_head(xlat), top_frame);
275}
276
277/** Push a pre-compiled xlat onto the stack for evaluation
278 *
279 * @param[in] ctx To allocate value boxes and values in.
280 * @param[out] p_result If set, and execution succeeds, true will be written
281 * here. If execution fails, false will be written.
282 * @param[out] out Where to write the result of the expansion.
283 * @param[in] request to push xlat onto.
284 * @param[in] node to evaluate. Only this node will be evaluated.
285 * @return
286 * - 0 on success.
287 * - -1 on failure.
288 */
289int unlang_xlat_push_node(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out,
290 request_t *request, xlat_exp_t *node)
291{
292 return unlang_xlat_push_internal(ctx, p_result, out, request, NULL, node, UNLANG_TOP_FRAME);
293}
294
296{
297 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
298 xlat_action_t xa;
299 xlat_exp_head_t const *child = NULL;
300
301 /*
302 * If the xlat is a function with a method_env, expand it before calling the function.
303 */
304 if ((state->exp->type == XLAT_FUNC) && state->exp->call.inst->call_env && !state->env_data) {
305 unlang_action_t ua = call_env_expand(state, request, NULL, &state->env_data,
306 state->exp->call.inst->call_env);
307 switch (ua) {
309 goto fail;
310
314
315 default:
316 break;
317 }
318 }
319
320 xa = xlat_frame_eval_repeat(state->ctx, &state->values, &child,
321 request, state->head, &state->exp, state->env_data, &state->out);
322 switch (xa) {
324 fr_assert(child);
325
326 repeatable_set(frame); /* Was cleared by the interpreter */
327
328 /*
329 * Clear out the results of any previous expansions
330 * at this level. A frame may be used to evaluate
331 * multiple sibling nodes.
332 */
333 fr_value_box_list_talloc_free(&state->out);
334 if (unlang_xlat_push(state->ctx, p_result, &state->out, request, child, false) < 0) {
335 REXDENT();
337 }
339
341 repeatable_set(frame); /* Call the xlat code on the way back down */
343
345 if (!state->resume) {
346 RWDEBUG("Missing call to unlang_xlat_yield()");
347 goto fail;
348 }
349 repeatable_set(frame);
350 return UNLANG_ACTION_YIELD;
351
352 case XLAT_ACTION_DONE:
354 REXDENT();
356
357 case XLAT_ACTION_FAIL:
358 fail:
359 REXDENT();
360 return UNLANG_ACTION_FAIL;
361
362 default:
363 fr_assert(0);
364 goto fail;
365 }
366}
367
368/** Stub function for calling the xlat interpreter
369 *
370 * Calls the xlat interpreter and translates its wants and needs into
371 * unlang_action_t codes.
372 */
374{
375 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
376 xlat_action_t xa;
377 xlat_exp_head_t const *child = NULL;
378
379 RINDENT_SAVE(state, request);
380 RINDENT();
381
382 xa = xlat_frame_eval(state->ctx, &state->values, &child, request, state->head, &state->exp);
383 switch (xa) {
385 fr_assert(child);
386
388
389 /*
390 * Clear out the results of any previous expansions
391 * at this level. A frame may be used to evaluate
392 * multiple sibling nodes.
393 */
394 fr_value_box_list_talloc_free(&state->out);
395 if (unlang_xlat_push(state->ctx, p_result, &state->out, request, child, false) < 0) {
396 RINDENT_RESTORE(request, state);
398 }
400
402 repeatable_set(frame); /* Call the xlat code on the way back down */
404
406 if (!state->resume) {
407 RWDEBUG("Missing call to unlang_xlat_yield()");
408 goto fail;
409 }
410 repeatable_set(frame);
411 return UNLANG_ACTION_YIELD;
412
413 case XLAT_ACTION_DONE:
415 RINDENT_RESTORE(request, state);
417
418 case XLAT_ACTION_FAIL:
419 fail:
420 RINDENT_RESTORE(request, state);
421 return UNLANG_ACTION_FAIL;
422
423 default:
424 fr_assert(0);
425 goto fail;
426 }
427}
428
429/** Send a signal (usually stop) to a request that's running an xlat expansions
430 *
431 * This is typically called via an "async" action, i.e. an action
432 * outside of the normal processing of the request.
433 *
434 * If there is no #xlat_func_signal_t callback defined, the action is ignored.
435 *
436 * @param[in] request The current request.
437 * @param[in] frame The current stack frame.
438 * @param[in] action What the request should do (the type of signal).
439 */
441{
442 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
443
444 /*
445 * Delete timers, etc. when the xlat is cancelled.
446 */
447 if (action == FR_SIGNAL_CANCEL) {
448 TALLOC_FREE(state->event_ctx);
449 }
450
451 if (!state->signal || (state->sigmask & action)) return;
452
453 xlat_signal(state->signal, state->exp, request, state->rctx, action);
454}
455
456/** Called when we're ready to resume processing the request
457 *
458 * @param[in] p_result the result of the xlat function.
459 * - RLM_MODULE_OK on success.
460 * - RLM_MODULE_FAIL on failure.
461 * @param[in] request to resume processing.
462 * @param[in] frame the current stack frame.
463 * @return
464 * - UNLANG_ACTION_YIELD if additional asynchronous
465 * operations need to be performed.
466 * - UNLANG_ACTION_CALCULATE_RESULT if done.
467 */
469{
470 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
471 xlat_action_t xa;
472 xlat_exp_head_t const *child = NULL;
473
474 fr_assert(state->resume != NULL);
475
476 /*
477 * Delete timers, etc. when the xlat is resumed.
478 */
479 TALLOC_FREE(state->event_ctx);
480
481 xa = xlat_frame_eval_resume(state->ctx, &state->values, &child, request, state->head, &state->exp,
482 &state->out, state->resume, state->rctx);
483 switch (xa) {
485 repeatable_set(frame);
486 return UNLANG_ACTION_YIELD;
487
488 case XLAT_ACTION_DONE:
490 RINDENT_RESTORE(request, state);
492
494 repeatable_set(frame);
496
498 fr_assert(child);
499
500 repeatable_set(frame); /* Was cleared by the interpreter */
501
502 /*
503 * Clear out the results of any previous expansions
504 * at this level. A frame may be used to evaluate
505 * multiple sibling nodes.
506 */
507 fr_value_box_list_talloc_free(&state->out);
508 if (unlang_xlat_push(state->ctx, state->p_result, &state->out, request, child, false) < 0) {
509 RINDENT_RESTORE(request, state);
511 }
513
514 case XLAT_ACTION_FAIL:
515 RINDENT_RESTORE(request, state);
516 return UNLANG_ACTION_FAIL;
517 /* DON'T SET DEFAULT */
518 }
519
520 fr_assert(0); /* Garbage xlat action */
521
522 RINDENT_RESTORE(request, state);
523 return UNLANG_ACTION_FAIL;
524}
525
526/** Yield a request back to the interpreter from within a module
527 *
528 * This passes control of the request back to the unlang interpreter, setting
529 * callbacks to execute when the request is 'signalled' asynchronously, or whatever
530 * timer or I/O event the module was waiting for occurs.
531 *
532 * @note The module function which calls #unlang_module_yield should return control
533 * of the C stack to the unlang interpreter immediately after calling #unlang_xlat_yield.
534 * A common pattern is to use ``return unlang_xlat_yield(...)``.
535 *
536 * @param[in] request The current request.
537 * @param[in] resume Called on unlang_interpret_mark_runnable().
538 * @param[in] signal Called on unlang_action().
539 * @param[in] sigmask Signals to block.
540 * @param[in] rctx to pass to the callbacks.
541 * @return always returns XLAT_ACTION_YIELD
542 */
544 xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask,
545 void *rctx)
546{
547 unlang_stack_t *stack = request->stack;
548 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
549 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
550
552
553 /*
554 * Over-ride whatever functions were there before.
555 */
556 state->resume = resume;
557 state->signal = signal;
558 state->sigmask = sigmask;
559 state->rctx = rctx;
560
561 return XLAT_ACTION_YIELD;
562}
563
564/** Frees an unlang event, removing it from the request's event loop
565 *
566 * @param[in] ev The event to free.
567 *
568 * @return 0
569 */
571{
572 FR_TIMER_DELETE(&(ev->ev));
573
574 return 0;
575}
576
577/** Call the callback registered for a timeout event
578 *
579 * @param[in] tl the event timer was inserted into.
580 * @param[in] now The current time, as held by the event_list.
581 * @param[in] uctx unlang_module_event_t structure holding callbacks.
582 *
583 */
585{
586 unlang_xlat_retry_t *ev = talloc_get_type_abort(uctx, unlang_xlat_retry_t);
587 request_t *request = ev->request;
588
589 switch (fr_retry_next(&ev->retry, now)) {
591 /*
592 * Call the module retry handler, with the state of the retry. On MRD / MRC, the
593 * module is made runnable again, and the "resume" function is called.
594 */
595 ev->retry_cb(XLAT_CTX(ev->inst->data,
596 ev->thread->data,
597 NULL,
598 ev->thread->mctx, NULL,
599 UNCONST(void *, ev->rctx)),
600 ev->request, &ev->retry);
601
602 /*
603 * Reset the timer.
604 */
605 if (fr_timer_at(ev, unlang_interpret_event_list(request)->tl, &ev->ev, ev->retry.next,
606 false, unlang_xlat_event_retry_handler, ev) < 0) {
607 RPEDEBUG("Failed inserting event");
608 talloc_free(ev);
610 }
611 return;
612
613 case FR_RETRY_MRD:
614 RDEBUG("Reached max_rtx_duration (%pVs > %pVs) - sending timeout",
616 break;
617
618 case FR_RETRY_MRC:
619 RDEBUG("Reached max_rtx_count %u- sending timeout",
620 ev->retry.config->mrc);
621 break;
622 }
623
624 /*
625 * Run the retry handler on MRD / MRC, too.
626 */
627 ev->retry_cb(XLAT_CTX(ev->inst->data,
628 ev->thread->data,
629 NULL,
630 ev->thread->mctx, NULL,
631 UNCONST(void *, ev->rctx)),
632 ev->request, &ev->retry);
633
634 /*
635 * On final timeout, always mark the request as runnable.
636 */
637 talloc_free(ev);
639}
640
641
642/** Yield a request back to the interpreter, with retries
643 *
644 * This passes control of the request back to the unlang interpreter, setting
645 * callbacks to execute when the request is 'signalled' asynchronously, or when
646 * the retry timer hits.
647 *
648 * @note The module function which calls #unlang_module_yield_to_retry should return control
649 * of the C stack to the unlang interpreter immediately after calling #unlang_module_yield_to_retry.
650 * A common pattern is to use ``return unlang_module_yield_to_retry(...)``.
651 *
652 * @param[in] request The current request.
653 * @param[in] resume Called on unlang_interpret_mark_runnable().
654 * @param[in] retry Called on when a retry timer hits
655 * @param[in] signal Called on unlang_action().
656 * @param[in] sigmask Set of signals to block.
657 * @param[in] rctx to pass to the callbacks.
658 * @param[in] retry_cfg to set up the retries
659 * @return
660 * - XLAT_ACTION_YIELD on success
661 * - XLAT_ACTION_FAIL on failure
662 */
664 xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx,
665 fr_retry_config_t const *retry_cfg)
666{
667 unlang_stack_t *stack = request->stack;
668 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
670 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
671
672 fr_assert(stack->depth > 0);
674
675 if (!state->event_ctx) MEM(state->event_ctx = talloc_zero(state, bool));
676
677 MEM(ev = talloc_zero(state->event_ctx, unlang_xlat_retry_t));
678
679 ev->request = request;
680 fr_assert(state->exp->type == XLAT_FUNC);
681 ev->inst = state->exp->call.inst;
683 ev->retry_cb = retry;
684 ev->rctx = rctx;
685
686 fr_retry_init(&ev->retry, fr_time(), retry_cfg);
687
688 if (fr_timer_at(request, unlang_interpret_event_list(request)->tl,
689 &ev->ev, ev->retry.next,
690 false, unlang_xlat_event_retry_handler, ev) < 0) {
691 RPEDEBUG("Failed inserting event");
692 talloc_free(ev);
693 return XLAT_ACTION_FAIL;
694 }
695
696 talloc_set_destructor(ev, _unlang_xlat_retry_free);
697
698 return unlang_xlat_yield(request, resume, signal, sigmask, rctx);
699}
700
701/** Evaluate a "pure" (or not impure) xlat
702 *
703 * @param[in] ctx To allocate value boxes and values in.
704 * @param[out] out Where to write the result of the expansion.
705 * @param[in] request to push xlat onto.
706 * @param[in] xlat to evaluate.
707 * @return
708 * - 0 on success.
709 * - -1 on failure.
710 */
711int unlang_xlat_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat)
712{
714
715 if (xlat->flags.impure_func) {
716 fr_strerror_const("Expansion requires async operations");
717 return -1;
718 }
719
720 if (unlang_xlat_push(ctx, &result, out, request, xlat, UNLANG_TOP_FRAME) < 0) return -1;
721
723
724 if (!XLAT_RESULT_SUCCESS(&result)) return -1;
725
726 return 0;
727}
728
729/** Evaluate a "pure" (or not impure) xlat
730 *
731 * @param[in] ctx To allocate value boxes and values in.
732 * @param[out] vb output value-box
733 * @param[in] type expected type
734 * @param[in] enumv enum for type
735 * @param[in] request to push xlat onto.
736 * @param[in] xlat to evaluate.
737 * @return
738 * - 0 on success.
739 * - -1 on failure.
740 */
741int unlang_xlat_eval_type(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t type, fr_dict_attr_t const *enumv, request_t *request, xlat_exp_head_t const *xlat)
742{
743 fr_value_box_t *src;
744 fr_value_box_list_t list;
745
747 fr_strerror_const("Invalid type for output of evaluation");
748 return -1;
749 }
750
751 fr_value_box_list_init(&list);
752
753 if (unlang_xlat_eval(ctx, &list, request, xlat) < 0) return -1;
754
755 fr_value_box_init(vb, type, NULL, false);
756
757 switch (type) {
758 default:
759 /*
760 * Take only the first entry from the list.
761 */
762 src = fr_value_box_list_head(&list);
763 if (!src) {
764 fr_strerror_const("Expression returned no results");
765 fail:
766 fr_value_box_list_talloc_free(&list);
767 return -1;
768 }
769
770 if (fr_value_box_cast(ctx, vb, type, enumv, src) < 0) goto fail;
771 fr_value_box_list_talloc_free(&list);
772 break;
773
774 case FR_TYPE_STRING:
775 case FR_TYPE_OCTETS:
776 /*
777 * No output: create an empty string.
778 *
779 * The "concat in place" function returns an error for empty input, which is arguably not
780 * what we want to do here.
781 */
782 if (fr_value_box_list_empty(&list)) {
783 break;
784 }
785
786 if (fr_value_box_list_concat_in_place(ctx, vb, &list, type, FR_VALUE_BOX_LIST_FREE_BOX, false, SIZE_MAX) < 0) {
787 goto fail;
788 }
789 break;
790 }
791
792 return 0;
793}
794
796{
797 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
798 xlat_exp_t const *exp = state->exp;
799
800 if (exp) RDEBUG("expression %s", exp->fmt);
801}
802/** Register xlat operation with the interpreter
803 *
804 */
806{
808 .name = "xlat",
809 .type = UNLANG_TYPE_XLAT,
811
812 .interpret = unlang_xlat,
813 .signal = unlang_xlat_signal,
814 .dump = unlang_xlat_dump,
815
816 .frame_state_size = sizeof(unlang_frame_state_xlat_t),
817 .frame_state_type = "unlang_frame_state_xlat_t",
818 });
819}
#define RETURN_UNLANG_ACTION_FATAL
Definition action.h:44
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_PUSHED_CHILD
unlang_t pushed a new child onto the stack, execute it instead of continuing.
Definition action.h:39
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:506
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:343
#define UNUSED
Definition build.h:336
unlang_action_t call_env_expand(TALLOC_CTX *ctx, request_t *request, call_env_result_t *env_result, void **env_data, call_env_t const *call_env)
Initialise the expansion of a call environment.
Definition call_env.c:326
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition dcursor.h:708
#define MEM(x)
Definition debug.h:46
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:83
talloc_free(hp)
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:940
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1636
int unlang_interpret_push(unlang_result_t *p_result, request_t *request, unlang_t const *instruction, unlang_frame_conf_t const *conf, bool do_next_sibling)
Push a new frame onto the stack.
Definition interpret.c:278
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2053
#define UNLANG_RESULT_NOT_SET
Definition interpret.h:139
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:152
#define UNLANG_TOP_FRAME
Definition interpret.h:36
#define UNLANG_REQUEST_RUNNING
Definition interpret.h:42
#define UNLANG_RESULT_RCODE(_x)
Definition interpret.h:140
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:455
#define RWDEBUG(fmt,...)
Definition log.h:373
#define RINDENT_SAVE(_x, _request)
Save indentation for later restoral.
Definition log.h:400
#define RINDENT_RESTORE(_request, _x)
Definition log.h:404
#define RPEDEBUG(fmt,...)
Definition log.h:388
#define RINDENT()
Indent R* messages by one level.
Definition log.h:442
void unlang_register(unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:56
#define fr_time()
Definition event.c:60
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1203
static char * stack[MAX_STACK]
Definition radmin.c:158
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_OCTETS
Raw octets.
#define MOD_ACTIONS_FAIL_TIMEOUT_RETURN
Definition mod_action.h:74
#define fr_assert(_expr)
Definition rad_assert.h:37
#define RDEBUG(fmt,...)
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:45
#define tmpl_is_data(vpt)
Definition tmpl.h:206
fr_signal_t
Signals that can be generated/processed by request signal handlers.
Definition signal.h:38
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
fr_aka_sim_id_type_t type
#define talloc_get_type_abort_const
Definition talloc.h:110
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define FR_TIMER_DELETE(_ev_p)
Definition timer.h:103
#define fr_timer_at(...)
Definition timer.h:81
fr_timer_t * ev
retry timer just for this xlat
Definition xlat.c:87
int unlang_xlat_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat)
Evaluate a "pure" (or not impure) xlat.
Definition xlat.c:711
xlat_exp_head_t const * head
of the xlat list
Definition xlat.c:42
TALLOC_CTX * ctx
to allocate boxes and values in.
Definition xlat.c:40
fr_unlang_xlat_fd_event_t fd_read
Function to call when FD is readable.
Definition xlat.c:70
int unlang_xlat_eval_type(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t type, fr_dict_attr_t const *enumv, request_t *request, xlat_exp_head_t const *xlat)
Evaluate a "pure" (or not impure) xlat.
Definition xlat.c:741
static void unlang_xlat_dump(request_t *request, unlang_stack_frame_t *frame)
Definition xlat.c:795
TALLOC_CTX * event_ctx
for temporary events
Definition xlat.c:41
xlat_func_signal_t signal
called on signal
Definition xlat.c:55
static void unlang_xlat_event_retry_handler(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
Call the callback registered for a timeout event.
Definition xlat.c:584
unlang_result_t * p_result
If set, where to record the result of the execution.
Definition xlat.c:59
xlat_thread_inst_t * thread
Thread specific xlat instance.
Definition xlat.c:82
static int _unlang_xlat_event_free(unlang_xlat_event_t *ev)
Frees an unlang event, removing it from the request's event loop.
Definition xlat.c:97
void * rctx
for resume / signal
Definition xlat.c:57
int unlang_xlat_push_node(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, xlat_exp_t *node)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:289
xlat_action_t unlang_xlat_yield_to_retry(request_t *request, xlat_func_t resume, fr_unlang_xlat_retry_t retry, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx, fr_retry_config_t const *retry_cfg)
Yield a request back to the interpreter, with retries.
Definition xlat.c:663
fr_dcursor_t values
Values aggregated so far.
Definition xlat.c:44
fr_value_box_list_t out
Head of the result of a nested expansion.
Definition xlat.c:52
fr_timer_t * ev
Event in this worker's event heap.
Definition xlat.c:76
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:543
xlat_func_t resume
called on resume
Definition xlat.c:54
fr_unlang_xlat_retry_t retry_cb
callback to run on timeout
Definition xlat.c:84
fr_retry_t retry
retry timers, etc.
Definition xlat.c:88
request_t * request
Request this event pertains to.
Definition xlat.c:67
int fd
File descriptor to wait on.
Definition xlat.c:68
static unlang_action_t unlang_xlat(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Stub function for calling the xlat interpreter.
Definition xlat.c:373
fr_unlang_xlat_timeout_t timeout
Function to call on timeout.
Definition xlat.c:69
request_t * request
Definition xlat.c:80
static void unlang_xlat_event_timeout_handler(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
Call the callback registered for a timeout event.
Definition xlat.c:115
static int unlang_xlat_push_internal(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, xlat_exp_t *node, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:200
xlat_inst_t * inst
xlat instance data.
Definition xlat.c:73
int unlang_xlat_push(TALLOC_CTX *ctx, unlang_result_t *p_result, 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:269
xlat_inst_t * inst
xlat instance data.
Definition xlat.c:81
void * rctx
rctx data to pass to timeout callback
Definition xlat.c:85
fr_unlang_xlat_fd_event_t fd_write
Function to call when FD is writable.
Definition xlat.c:71
static unlang_action_t unlang_xlat_repeat(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition xlat.c:295
fr_signal_t sigmask
Signals to block.
Definition xlat.c:56
static int _unlang_xlat_retry_free(unlang_xlat_retry_t *ev)
Frees an unlang event, removing it from the request's event loop.
Definition xlat.c:570
int unlang_xlat_timeout_add(request_t *request, fr_unlang_xlat_timeout_t callback, void const *rctx, fr_time_t when)
Add a timeout for an xlat handler.
Definition xlat.c:150
rindent_t indent
indentation
Definition xlat.c:46
void * env_data
Expanded per call environment tmpls.
Definition xlat.c:48
static unlang_action_t unlang_xlat_resume(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Called when we're ready to resume processing the request.
Definition xlat.c:468
void const * rctx
rctx data to pass to callbacks.
Definition xlat.c:75
static void unlang_xlat_signal(request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Send a signal (usually stop) to a request that's running an xlat expansions.
Definition xlat.c:440
void unlang_xlat_init(void)
Register xlat operation with the interpreter.
Definition xlat.c:805
xlat_exp_t const * exp
current one we're evaluating
Definition xlat.c:43
fr_unlang_xlat_fd_event_t fd_error
Function to call when FD has errored.
Definition xlat.c:72
xlat_thread_inst_t * thread
Thread specific xlat instance.
Definition xlat.c:74
State of an xlat expansion.
Definition xlat.c:39
Wrap an fr_timer_t providing data needed for unlang events.
Definition xlat.c:66
void * data
Thread specific instance data.
Definition xlat.h:94
xlat_thread_inst_t * xlat_thread_instance_find(xlat_exp_t const *node)
Retrieve xlat/thread specific instance data.
Definition xlat_inst.c:404
void(* fr_unlang_xlat_fd_event_t)(xlat_ctx_t const *xctx, request_t *request, int fd)
A callback when the FD is ready for reading.
Definition xlat.h:212
unsigned int impure_func
xlat contains an impure function
Definition xlat.h:111
#define XLAT_RESULT_SUCCESS(_p_result)
Definition xlat.h:500
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:243
void(* fr_unlang_xlat_retry_t)(xlat_ctx_t const *xctx, request_t *request, fr_retry_t const *retry)
A callback when the timeout occurs.
Definition xlat.h:199
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:232
void * data
xlat node specific instance data.
Definition xlat.h:78
void(* fr_unlang_xlat_timeout_t)(xlat_ctx_t const *xctx, request_t *request, fr_time_t fired)
A callback when the timeout occurs.
Definition xlat.h:185
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_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
@ XLAT_ACTION_PUSH_CHILD
A deeper level of nesting needs to be evaluated.
Definition xlat.h:38
module_ctx_t const * mctx
A synthesised module calling ctx containing module global and thread instance data.
Definition xlat.h:96
Instance data for an xlat expansion node.
Definition xlat.h:71
Thread specific instance data for xlat expansion node.
Definition xlat.h:85
Private interpreter structures and functions.
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:98
@ UNLANG_TYPE_XLAT
Represents one level of an xlat expansion.
Definition unlang_priv.h:80
static void frame_repeat(unlang_stack_frame_t *frame, unlang_process_t process)
Mark the current stack frame up for repeat, and set a new process function.
unlang_t const * instruction
The unlang node we're evaluating.
@ UNLANG_OP_FLAG_INTERNAL
it's not a real keyword
static void repeatable_set(unlang_stack_frame_t *frame)
unlang_process_t process
function to call for interpreting this stack frame
unlang_type_t type
The specialisation of this node.
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.
An unlang stack associated with a request.
fr_retry_state_t fr_retry_next(fr_retry_t *r, fr_time_t now)
Initialize a retransmission counter.
Definition retry.c:110
void fr_retry_init(fr_retry_t *r, fr_time_t now, fr_retry_config_t const *config)
Initialize a retransmission counter.
Definition retry.c:36
fr_time_t start
when we started the retransmission
Definition retry.h:53
uint32_t mrc
Maximum retransmission count.
Definition retry.h:36
fr_retry_config_t const * config
master configuration
Definition retry.h:52
@ FR_RETRY_MRC
reached maximum retransmission count
Definition retry.h:47
@ FR_RETRY_CONTINUE
Definition retry.h:46
@ FR_RETRY_MRD
reached maximum retransmission duration
Definition retry.h:48
fr_time_delta_t mrd
Maximum retransmission duration.
Definition retry.h:35
fr_time_t next
when the next timer should be set
Definition retry.h:55
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_type_is_structural(_x)
Definition types.h:392
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3931
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:6589
@ FR_VALUE_BOX_LIST_FREE_BOX
Free each processed box.
Definition value.h:235
#define fr_box_time_delta(_val)
Definition value.h:366
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:610
static size_t char ** out
Definition value.h:1030
#define XLAT_CTX(_inst, _thread, _ex, _mctx, _env_data, _rctx)
Wrapper to create a xlat_ctx_t as a compound literal.
Definition xlat_ctx.h:95
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:1163
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:1055
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:1089
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:1355
xlat_flags_t flags
Flags that control resolution and evaluation.
Definition xlat_priv.h:190
@ XLAT_BOX
fr_value_box_t
Definition xlat_priv.h:108
@ XLAT_TMPL
xlat attribute
Definition xlat_priv.h:112
@ XLAT_FUNC
xlat module
Definition xlat_priv.h:110
@ XLAT_GROUP
encapsulated string of xlats
Definition xlat_priv.h:116
char const *_CONST fmt
The original format string (a talloced buffer).
Definition xlat_priv.h:151
xlat_type_t _CONST type
type of this expansion.
Definition xlat_priv.h:155
static xlat_exp_t * xlat_exp_head(xlat_exp_head_t const *head)
Definition xlat_priv.h:210
An xlat expansion node.
Definition xlat_priv.h:148