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: 6cd9d72798c756d72827dd3b7c8a084edceee5cb $
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: 6cd9d72798c756d72827dd3b7c8a084edceee5cb $")
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 bool *success; //!< 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 ev->thread->mctx, NULL,
130 UNCONST(void *, ev->rctx)),
131 ev->request, now);
132
133 /* Remove old references from the request */
134 talloc_free(ev);
135}
136
137/** Add a timeout for an xlat handler
138 *
139 * @note The timeout is automatically removed when the xlat is cancelled or resumed.
140 *
141 * @param[in] request the request
142 * @param[in] callback to run when the timeout hits
143 * @param[in] rctx passed to the callback
144 * @param[in] when when the timeout fires
145 * @return
146 * - <0 on error
147 * - 0 on success
148 */
150 fr_unlang_xlat_timeout_t callback, void const *rctx, fr_time_t when)
151{
152 unlang_stack_t *stack = request->stack;
153 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
155 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
156
157 fr_assert(stack->depth > 0);
159
160 if (!state->event_ctx) MEM(state->event_ctx = talloc_zero(state, bool));
161
162 MEM(ev = talloc_zero(state->event_ctx, unlang_xlat_event_t));
163 ev->request = request;
164 ev->fd = -1;
165 ev->timeout = callback;
166 fr_assert(state->exp->type == XLAT_FUNC);
167 ev->inst = state->exp->call.inst;
169 ev->rctx = rctx;
170
171 if (fr_timer_at(request, unlang_interpret_event_list(request)->tl,
172 &ev->ev, when,
173 false, unlang_xlat_event_timeout_handler, ev) < 0) {
174 RPEDEBUG("Failed inserting event");
175 talloc_free(ev);
176 return -1;
177 }
178
179 talloc_set_destructor(ev, _unlang_xlat_event_free);
180
181 return 0;
182}
183
184/** Push a pre-compiled xlat onto the stack for evaluation
185 *
186 * @param[in] ctx To allocate value boxes and values in.
187 * @param[out] p_success If set, and execution succeeds, true will be written
188 * here. If execution fails, false will be written.
189 * @param[out] out Where to write the result of the expansion.
190 * @param[in] request to push xlat onto.
191 * @param[in] xlat head of list
192 * @param[in] node to evaluate.
193 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
194 * Set to UNLANG_SUB_FRAME if the interprer should continue.
195 * @return
196 * - 0 on success.
197 * - -1 on failure.
198 */
199static int unlang_xlat_push_internal(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out,
200 request_t *request, xlat_exp_head_t const *xlat, xlat_exp_t *node, bool top_frame)
201{
202 /** Static instruction for performing xlat evaluations
203 *
204 */
205 static unlang_t xlat_instruction = {
207 .name = "xlat",
208 .debug_name = "xlat",
209 .actions = {
210 .actions = {
211 [RLM_MODULE_REJECT] = 0,
212 [RLM_MODULE_FAIL] = MOD_ACTION_RETURN, /* Exit out of nested levels */
213 [RLM_MODULE_OK] = 0,
214 [RLM_MODULE_HANDLED] = 0,
215 [RLM_MODULE_INVALID] = 0,
218 [RLM_MODULE_NOOP] = 0,
220 },
221 .retry = RETRY_INIT,
222 },
223 };
224
226 unlang_stack_t *stack = request->stack;
228
229 /*
230 * Push a new xlat eval frame onto the stack
231 */
232 if (unlang_interpret_push(request, &xlat_instruction,
233 RLM_MODULE_NOT_SET, UNLANG_NEXT_STOP, top_frame) < 0) return -1;
234 frame = &stack->frame[stack->depth];
235
236 /*
237 * Allocate its state, and setup a cursor for the xlat nodes
238 */
239 MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_xlat_t));
240 state->head = xlat;
241 state->exp = node;
242 state->success = p_success;
243 state->ctx = ctx;
244
245 if (node) switch (node->type) {
246 case XLAT_GROUP:
247 case XLAT_BOX:
248 break;
249
250 case XLAT_TMPL:
251 if (tmpl_is_data(node->vpt)) break;
253
254 default:
255 RDEBUG("| %s", node->fmt);
256 break;
257 }
258
259 /*
260 * Initialise the input and output lists
261 */
262 fr_dcursor_init(&state->values, fr_value_box_list_dlist_head(out));
263 fr_value_box_list_init(&state->out);
264
265 return 0;
266}
267
268/** Push a pre-compiled xlat onto the stack for evaluation
269 *
270 * @param[in] ctx To allocate value boxes and values in.
271 * @param[out] p_success If set, and execution succeeds, true will be written
272 * here. If execution fails, false will be written.
273 * @param[out] out Where to write the result of the expansion.
274 * @param[in] request to push xlat onto.
275 * @param[in] xlat to evaluate.
276 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
277 * Set to UNLANG_SUB_FRAME if the interprer should continue.
278 * @return
279 * - 0 on success.
280 * - -1 on failure.
281 */
282int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out,
283 request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
284{
286
287 return unlang_xlat_push_internal(ctx, p_success, out, request, xlat, xlat_exp_head(xlat), top_frame);
288}
289
290/** Push a pre-compiled xlat onto the stack for evaluation
291 *
292 * @param[in] ctx To allocate value boxes and values in.
293 * @param[out] p_success If set, and execution succeeds, true will be written
294 * here. If execution fails, false will be written.
295 * @param[out] out Where to write the result of the expansion.
296 * @param[in] request to push xlat onto.
297 * @param[in] node to evaluate. Only this node will be evaluated.
298 * @return
299 * - 0 on success.
300 * - -1 on failure.
301 */
302int unlang_xlat_push_node(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out,
303 request_t *request, xlat_exp_t *node)
304{
305 return unlang_xlat_push_internal(ctx, p_success, out, request, NULL, node, UNLANG_TOP_FRAME);
306}
307
309{
310 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
311 xlat_action_t xa;
312 xlat_exp_head_t const *child = NULL;
313
314 /*
315 * If the xlat is a function with a method_env, expand it before calling the function.
316 */
317 if ((state->exp->type == XLAT_FUNC) && state->exp->call.inst->call_env && !state->env_data) {
318 unlang_action_t ua = call_env_expand(state, request, NULL, &state->env_data,
319 state->exp->call.inst->call_env);
320 switch (ua) {
322 goto fail;
323
327
328 default:
329 break;
330 }
331 }
332
333 xa = xlat_frame_eval_repeat(state->ctx, &state->values, &child,
334 request, state->head, &state->exp, state->env_data, &state->out);
335 switch (xa) {
337 fr_assert(child);
338
339 repeatable_set(frame); /* Was cleared by the interpreter */
340
341 /*
342 * Clear out the results of any previous expansions
343 * at this level. A frame may be used to evaluate
344 * multiple sibling nodes.
345 */
346 fr_value_box_list_talloc_free(&state->out);
347 if (unlang_xlat_push(state->ctx, state->success, &state->out, request, child, false) < 0) {
348 *p_result = RLM_MODULE_FAIL;
349 REXDENT();
351 }
353
355 repeatable_set(frame); /* Call the xlat code on the way back down */
357
359 if (!state->resume) {
360 RWDEBUG("Missing call to unlang_xlat_yield()");
361 goto fail;
362 }
363 repeatable_set(frame);
364 return UNLANG_ACTION_YIELD;
365
366 case XLAT_ACTION_DONE:
367 if (state->success) *state->success = true;
368 *p_result = RLM_MODULE_OK;
369 REXDENT();
371
372 case XLAT_ACTION_FAIL:
373 fail:
374 if (state->success) *state->success = false;
375 *p_result = RLM_MODULE_FAIL;
376 REXDENT();
378
379 default:
380 fr_assert(0);
381 goto fail;
382 }
383}
384
385/** Stub function for calling the xlat interpreter
386 *
387 * Calls the xlat interpreter and translates its wants and needs into
388 * unlang_action_t codes.
389 */
391{
392 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
393 xlat_action_t xa;
394 xlat_exp_head_t const *child = NULL;
395
396 RINDENT_SAVE(state, request);
397 RINDENT();
398
399 xa = xlat_frame_eval(state->ctx, &state->values, &child, request, state->head, &state->exp);
400 switch (xa) {
402 fr_assert(child);
403
405
406 /*
407 * Clear out the results of any previous expansions
408 * at this level. A frame may be used to evaluate
409 * multiple sibling nodes.
410 */
411 fr_value_box_list_talloc_free(&state->out);
412 if (unlang_xlat_push(state->ctx, state->success, &state->out, request, child, false) < 0) {
413 *p_result = RLM_MODULE_FAIL;
414 RINDENT_RESTORE(request, state);
416 }
418
420 repeatable_set(frame); /* Call the xlat code on the way back down */
422
424 if (!state->resume) {
425 RWDEBUG("Missing call to unlang_xlat_yield()");
426 goto fail;
427 }
428 repeatable_set(frame);
429 return UNLANG_ACTION_YIELD;
430
431 case XLAT_ACTION_DONE:
432 if (state->success) *state->success = true;
433 *p_result = RLM_MODULE_OK;
434 RINDENT_RESTORE(request, state);
436
437 case XLAT_ACTION_FAIL:
438 fail:
439 if (state->success) *state->success = false;
440 *p_result = RLM_MODULE_FAIL;
441 RINDENT_RESTORE(request, state);
443
444 default:
445 fr_assert(0);
446 goto fail;
447 }
448}
449
450/** Send a signal (usually stop) to a request that's running an xlat expansions
451 *
452 * This is typically called via an "async" action, i.e. an action
453 * outside of the normal processing of the request.
454 *
455 * If there is no #xlat_func_signal_t callback defined, the action is ignored.
456 *
457 * @param[in] request The current request.
458 * @param[in] frame The current stack frame.
459 * @param[in] action What the request should do (the type of signal).
460 */
462{
463 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
464
465 /*
466 * Delete timers, etc. when the xlat is cancelled.
467 */
468 if (action == FR_SIGNAL_CANCEL) {
469 TALLOC_FREE(state->event_ctx);
470 }
471
472 if (!state->signal || (state->sigmask & action)) return;
473
474 xlat_signal(state->signal, state->exp, request, state->rctx, action);
475}
476
477/** Called when we're ready to resume processing the request
478 *
479 * @param[in] p_result the result of the xlat function.
480 * - RLM_MODULE_OK on success.
481 * - RLM_MODULE_FAIL on failure.
482 * @param[in] request to resume processing.
483 * @param[in] frame the current stack frame.
484 * @return
485 * - UNLANG_ACTION_YIELD if additional asynchronous
486 * operations need to be performed.
487 * - UNLANG_ACTION_CALCULATE_RESULT if done.
488 */
490{
491 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
492 xlat_action_t xa;
493 xlat_exp_head_t const *child = NULL;
494
495 fr_assert(state->resume != NULL);
496
497 /*
498 * Delete timers, etc. when the xlat is resumed.
499 */
500 TALLOC_FREE(state->event_ctx);
501
502 xa = xlat_frame_eval_resume(state->ctx, &state->values, &child, request, state->head, &state->exp,
503 &state->out, state->resume, state->rctx);
504 switch (xa) {
506 repeatable_set(frame);
507 return UNLANG_ACTION_YIELD;
508
509 case XLAT_ACTION_DONE:
510 if (state->success) *state->success = true;
511 *p_result = RLM_MODULE_OK;
512 RINDENT_RESTORE(request, state);
514
516 repeatable_set(frame);
518
520 fr_assert(child);
521
522 repeatable_set(frame); /* Was cleared by the interpreter */
523
524 /*
525 * Clear out the results of any previous expansions
526 * at this level. A frame may be used to evaluate
527 * multiple sibling nodes.
528 */
529 fr_value_box_list_talloc_free(&state->out);
530 if (unlang_xlat_push(state->ctx, state->success, &state->out, request, child, false) < 0) {
531 *p_result = RLM_MODULE_FAIL;
532 RINDENT_RESTORE(request, state);
534 }
536
537 case XLAT_ACTION_FAIL:
538 if (state->success) *state->success = false;
539 *p_result = RLM_MODULE_FAIL;
540 RINDENT_RESTORE(request, state);
542 /* DON'T SET DEFAULT */
543 }
544
545 fr_assert(0); /* Garbage xlat action */
546
547 *p_result = RLM_MODULE_FAIL;
548 RINDENT_RESTORE(request, state);
550}
551
552/** Yield a request back to the interpreter from within a module
553 *
554 * This passes control of the request back to the unlang interpreter, setting
555 * callbacks to execute when the request is 'signalled' asynchronously, or whatever
556 * timer or I/O event the module was waiting for occurs.
557 *
558 * @note The module function which calls #unlang_module_yield should return control
559 * of the C stack to the unlang interpreter immediately after calling #unlang_xlat_yield.
560 * A common pattern is to use ``return unlang_xlat_yield(...)``.
561 *
562 * @param[in] request The current request.
563 * @param[in] resume Called on unlang_interpret_mark_runnable().
564 * @param[in] signal Called on unlang_action().
565 * @param[in] sigmask Signals to block.
566 * @param[in] rctx to pass to the callbacks.
567 * @return always returns XLAT_ACTION_YIELD
568 */
570 xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask,
571 void *rctx)
572{
573 unlang_stack_t *stack = request->stack;
574 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
575 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
576
578
579 /*
580 * Over-ride whatever functions were there before.
581 */
582 state->resume = resume;
583 state->signal = signal;
584 state->sigmask = sigmask;
585 state->rctx = rctx;
586
587 return XLAT_ACTION_YIELD;
588}
589
590/** Frees an unlang event, removing it from the request's event loop
591 *
592 * @param[in] ev The event to free.
593 *
594 * @return 0
595 */
597{
598 FR_TIMER_DELETE(&(ev->ev));
599
600 return 0;
601}
602
603/** Call the callback registered for a timeout event
604 *
605 * @param[in] tl the event timer was inserted into.
606 * @param[in] now The current time, as held by the event_list.
607 * @param[in] uctx unlang_module_event_t structure holding callbacks.
608 *
609 */
611{
612 unlang_xlat_retry_t *ev = talloc_get_type_abort(uctx, unlang_xlat_retry_t);
613 request_t *request = ev->request;
614
615 switch (fr_retry_next(&ev->retry, now)) {
617 /*
618 * Call the module retry handler, with the state of the retry. On MRD / MRC, the
619 * module is made runnable again, and the "resume" function is called.
620 */
621 ev->retry_cb(XLAT_CTX(ev->inst->data,
622 ev->thread->data,
623 ev->thread->mctx, NULL,
624 UNCONST(void *, ev->rctx)),
625 ev->request, &ev->retry);
626
627 /*
628 * Reset the timer.
629 */
630 if (fr_timer_at(ev, unlang_interpret_event_list(request)->tl, &ev->ev, ev->retry.next,
631 false, unlang_xlat_event_retry_handler, request) < 0) {
632 RPEDEBUG("Failed inserting event");
633 talloc_free(ev);
635 }
636 return;
637
638 case FR_RETRY_MRD:
639 RDEBUG("Reached max_rtx_duration (%pVs > %pVs) - sending timeout",
641 break;
642
643 case FR_RETRY_MRC:
644 RDEBUG("Reached max_rtx_count %u- sending timeout",
645 ev->retry.config->mrc);
646 break;
647 }
648
649 /*
650 * Run the retry handler on MRD / MRC, too.
651 */
652 ev->retry_cb(XLAT_CTX(ev->inst->data,
653 ev->thread->data,
654 ev->thread->mctx, NULL,
655 UNCONST(void *, ev->rctx)),
656 ev->request, &ev->retry);
657
658 /*
659 * On final timeout, always mark the request as runnable.
660 */
661 talloc_free(ev);
663}
664
665
666/** Yield a request back to the interpreter, with retries
667 *
668 * This passes control of the request back to the unlang interpreter, setting
669 * callbacks to execute when the request is 'signalled' asynchronously, or when
670 * the retry timer hits.
671 *
672 * @note The module function which calls #unlang_module_yield_to_retry should return control
673 * of the C stack to the unlang interpreter immediately after calling #unlang_module_yield_to_retry.
674 * A common pattern is to use ``return unlang_module_yield_to_retry(...)``.
675 *
676 * @param[in] request The current request.
677 * @param[in] resume Called on unlang_interpret_mark_runnable().
678 * @param[in] retry Called on when a retry timer hits
679 * @param[in] signal Called on unlang_action().
680 * @param[in] sigmask Set of signals to block.
681 * @param[in] rctx to pass to the callbacks.
682 * @param[in] retry_cfg to set up the retries
683 * @return
684 * - XLAT_ACTION_YIELD on success
685 * - XLAT_ACTION_FAIL on failure
686 */
688 xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx,
689 fr_retry_config_t const *retry_cfg)
690{
691 unlang_stack_t *stack = request->stack;
692 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
694 unlang_frame_state_xlat_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_xlat_t);
695
696 fr_assert(stack->depth > 0);
698
699 if (!state->event_ctx) MEM(state->event_ctx = talloc_zero(state, bool));
700
701 MEM(ev = talloc_zero(state->event_ctx, unlang_xlat_retry_t));
702
703 ev->request = request;
704 fr_assert(state->exp->type == XLAT_FUNC);
705 ev->inst = state->exp->call.inst;
707 ev->retry_cb = retry;
708 ev->rctx = rctx;
709
710 fr_retry_init(&ev->retry, fr_time(), retry_cfg);
711
712 if (fr_timer_at(request, unlang_interpret_event_list(request)->tl,
713 &ev->ev, ev->retry.next,
714 false, unlang_xlat_event_retry_handler, ev) < 0) {
715 RPEDEBUG("Failed inserting event");
716 talloc_free(ev);
717 return XLAT_ACTION_FAIL;
718 }
719
720 talloc_set_destructor(ev, _unlang_xlat_retry_free);
721
722 return unlang_xlat_yield(request, resume, signal, sigmask, rctx);
723}
724
725/** Evaluate a "pure" (or not impure) xlat
726 *
727 * @param[in] ctx To allocate value boxes and values in.
728 * @param[out] out Where to write the result of the expansion.
729 * @param[in] request to push xlat onto.
730 * @param[in] xlat to evaluate.
731 * @return
732 * - 0 on success.
733 * - -1 on failure.
734 */
735int unlang_xlat_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat)
736{
737 bool success = false;
738
739 if (xlat->flags.impure_func) {
740 fr_strerror_const("Expansion requires async operations");
741 return -1;
742 }
743
744 if (unlang_xlat_push(ctx, &success, out, request, xlat, UNLANG_TOP_FRAME) < 0) return -1;
745
747
748 if (!success) return -1;
749
750 return 0;
751}
752
753/** Evaluate a "pure" (or not impure) xlat
754 *
755 * @param[in] ctx To allocate value boxes and values in.
756 * @param[out] vb output value-box
757 * @param[in] type expected type
758 * @param[in] enumv enum for type
759 * @param[in] request to push xlat onto.
760 * @param[in] xlat to evaluate.
761 * @return
762 * - 0 on success.
763 * - -1 on failure.
764 */
765int 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)
766{
767 fr_value_box_t *src;
768 fr_value_box_list_t list;
769
771 fr_strerror_const("Invalid type for output of evaluation");
772 return -1;
773 }
774
775 fr_value_box_list_init(&list);
776
777 if (unlang_xlat_eval(ctx, &list, request, xlat) < 0) return -1;
778
779 fr_value_box_init(vb, type, NULL, false);
780
781 switch (type) {
782 default:
783 /*
784 * Take only the first entry from the list.
785 */
786 src = fr_value_box_list_head(&list);
787 if (!src) {
788 fr_strerror_const("Expression returned no results");
789 fail:
790 fr_value_box_list_talloc_free(&list);
791 return -1;
792 }
793
794 if (fr_value_box_cast(ctx, vb, type, enumv, src) < 0) goto fail;
795 fr_value_box_list_talloc_free(&list);
796 break;
797
798 case FR_TYPE_STRING:
799 case FR_TYPE_OCTETS:
800 /*
801 * No output: create an empty string.
802 *
803 * The "concat in place" function returns an error for empty input, which is arguably not
804 * what we want to do here.
805 */
806 if (fr_value_box_list_empty(&list)) {
807 break;
808 }
809
810 if (fr_value_box_list_concat_in_place(ctx, vb, &list, type, FR_VALUE_BOX_LIST_FREE_BOX, false, SIZE_MAX) < 0) {
811 goto fail;
812 }
813 break;
814 }
815
816 return 0;
817}
818
819
820/** Register xlat operation with the interpreter
821 *
822 */
824{
826 &(unlang_op_t){
827 .name = "xlat_eval",
828 .interpret = unlang_xlat,
829 .signal = unlang_xlat_signal,
830 .debug_braces = false,
831 .frame_state_size = sizeof(unlang_frame_state_xlat_t),
832 .frame_state_type = "unlang_frame_state_xlat_t",
833 });
834}
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_STOP_PROCESSING
Break out of processing the current request (unwind).
Definition action.h:43
@ 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:42
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:485
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define UNUSED
Definition build.h:317
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:294
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition dcursor.h:732
#define MEM(x)
Definition debug.h:36
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:84
rlm_rcode_t unlang_interpret(request_t *request, bool running)
Run the interpreter for a current request.
Definition interpret.c:784
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1418
int unlang_interpret_push(request_t *request, unlang_t const *instruction, rlm_rcode_t default_rcode, bool do_next_sibling, bool top_frame)
Push a new frame onto the stack.
Definition interpret.c:159
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:1822
#define UNLANG_TOP_FRAME
Definition interpret.h:35
#define UNLANG_REQUEST_RUNNING
Definition interpret.h:41
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:443
#define RWDEBUG(fmt,...)
Definition log.h:361
#define RINDENT_SAVE(_x, _request)
Save indentation for later restoral.
Definition log.h:388
#define RINDENT_RESTORE(_request, _x)
Definition log.h:392
#define RPEDEBUG(fmt,...)
Definition log.h:376
#define RINDENT()
Indent R* messages by one level.
Definition log.h:430
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
talloc_free(reap)
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.
@ MOD_ACTION_RETURN
Definition mod_action.h:40
#define fr_assert(_expr)
Definition rad_assert.h:38
#define RDEBUG(fmt,...)
Definition radclient.h:53
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:45
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:43
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:46
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:41
@ RLM_MODULE_NOTFOUND
User not found.
Definition rcode.h:47
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:49
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:51
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:48
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:44
#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 fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
#define talloc_get_type_abort_const
Definition talloc.h:282
#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:75
#define FR_TIMER_DELETE(_ev_p)
Definition timer.h:102
#define fr_timer_at(...)
Definition timer.h:80
goto success
Definition tmpl_eval.c:1443
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:735
static int unlang_xlat_push_internal(TALLOC_CTX *ctx, bool *p_success, 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:199
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:765
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:610
xlat_thread_inst_t * thread
Thread specific xlat instance.
Definition xlat.c:82
static unlang_action_t unlang_xlat_resume(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Called when we're ready to resume processing the request.
Definition xlat.c:489
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
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:687
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:569
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
bool * success
If set, where to record the result of the execution.
Definition xlat.c:59
int fd
File descriptor to wait on.
Definition xlat.c:68
fr_unlang_xlat_timeout_t timeout
Function to call on timeout.
Definition xlat.c:69
request_t * request
Definition xlat.c:80
int unlang_xlat_push_node(TALLOC_CTX *ctx, bool *p_success, 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:302
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
xlat_inst_t * inst
xlat instance data.
Definition xlat.c:73
static unlang_action_t unlang_xlat(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Stub function for calling the xlat interpreter.
Definition xlat.c:390
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
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:596
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:149
rindent_t indent
indentation
Definition xlat.c:46
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:282
void * env_data
Expanded per call environment tmpls.
Definition xlat.c:48
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:461
static unlang_action_t unlang_xlat_repeat(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition xlat.c:308
void unlang_xlat_init(void)
Register xlat operation with the interpreter.
Definition xlat.c:823
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:93
xlat_thread_inst_t * xlat_thread_instance_find(xlat_exp_t const *node)
Retrieve xlat/thread specific instance data.
Definition xlat_inst.c:405
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:208
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:239
void(* fr_unlang_xlat_retry_t)(xlat_ctx_t const *xctx, request_t *request, fr_retry_t const *retry)
A callback when the the timeout occurs.
Definition xlat.h:195
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:228
void * data
xlat node specific instance data.
Definition xlat.h:77
void(* fr_unlang_xlat_timeout_t)(xlat_ctx_t const *xctx, request_t *request, fr_time_t fired)
A callback when the the timeout occurs.
Definition xlat.h:181
uint8_t impure_func
xlat contains an impure function
Definition xlat.h:110
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
module_ctx_t const * mctx
A synthesised module calling ctx containing module global and thread instance data.
Definition xlat.h:95
Instance data for an xlat expansion node.
Definition xlat.h:70
Thread specific instance data for xlat expansion node.
Definition xlat.h:84
Private interpreter structures and functions.
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:92
@ UNLANG_TYPE_XLAT
Represents one level of an xlat expansion.
Definition unlang_priv.h:74
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.
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:108
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
#define RETRY_INIT
Definition retry.h:39
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:371
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:3574
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:5934
@ FR_VALUE_BOX_LIST_FREE_BOX
Free each processed box.
Definition value.h:229
#define fr_box_time_delta(_val)
Definition value.h:354
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:598
static size_t char ** out
Definition value.h:1012
#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
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:1035
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:927
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:961
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:1240
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:208
An xlat expansion node.
Definition xlat_priv.h:148