The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
tmpl.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: 074e0e38659f39f871f79d4b233b448d4d82191f $
19 *
20 * @file unlang/tmpl.c
21 * @brief Defines functions for calling tmpl__t asynchronously
22 *
23 * @copyright 2021 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
24 * @copyright 2020 Network RADIUS SAS (legal@networkradius.com)
25 */
26RCSID("$Id: 074e0e38659f39f871f79d4b233b448d4d82191f $")
27
28#include <freeradius-devel/unlang/tmpl.h>
29#include <freeradius-devel/server/exec.h>
30#include <freeradius-devel/util/syserror.h>
31#include "tmpl_priv.h"
32#include <signal.h>
33
34#if defined(__linux__) || defined(__FreeBSD__)
35#include <sys/wait.h>
36#endif
37
38/** Send a signal (usually stop) to a request
39 *
40 * This is typically called via an "async" action, i.e. an action
41 * outside of the normal processing of the request.
42 *
43 * If there is no #fr_unlang_tmpl_signal_t callback defined, the action is ignored.
44 *
45 * @param[in] request The current request.
46 * @param[in] frame being signalled.
47 * @param[in] action to signal.
48 */
49static void unlang_tmpl_signal(request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
50{
51 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state,
53
54 /*
55 * If we're cancelled, then kill any child processes
56 */
57 if ((action == FR_SIGNAL_CANCEL) && state->exec.request) fr_exec_oneshot_cleanup(&state->exec, SIGKILL);
58
59 if (!state->signal) return;
60
61 state->signal(request, state->rctx, action);
62
63 /*
64 * If we're cancelled then disable this signal handler.
65 * fr_exec_oneshot_cleanup should handle being called spuriously.
66 */
67 if (action == FR_SIGNAL_CANCEL) state->signal = NULL;
68}
69
70/** Wrapper to call a resumption function after a tmpl has been expanded
71 *
72 * If the resumption function returns YIELD, then this function is
73 * called repeatedly until the resumption function returns a final
74 * value.
75 */
77{
78 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
80
81 if (tmpl_eval_cast_in_place(&state->list, request, ut->tmpl) < 0) {
82 RPEDEBUG("Failed casting expansion");
83 *p_result = RLM_MODULE_FAIL;
85 }
86
87 if (state->out) fr_value_box_list_move(state->out, &state->list);
88
89 if (state->resume) return state->resume(p_result, request, state->rctx);
90
91 *p_result = RLM_MODULE_OK;
92
94}
95
96/** Wrapper to call exec after the program has finished executing
97 *
98 */
101{
102 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state,
104
105 /*
106 * The exec failed for some internal reason. We don't
107 * care about output, and we don't care about the programs exit status.
108 */
109 if (state->exec.failed) {
110 fr_value_box_list_talloc_free(&state->list);
111 goto resume;
112 }
113
114 fr_assert(state->exec.pid < 0); /* Assert this has been cleaned up */
115
116 if (!state->args.exec.stdout_on_error && (state->exec.status != 0)) {
117 fr_assert(fr_value_box_list_empty(&state->list));
118 goto resume;
119 }
120
121 /*
122 * We might want to just get the status of the program,
123 * and not care about the output.
124 *
125 * If we do care about the output, it's unquoted, and tainted.
126 *
127 * FIXME - It would be much more efficient to just reparent
128 * the string buffer into the context of the box... but we'd
129 * need to fix talloc first.
130 */
131 if (state->out) {
133 fr_value_box_t *box;
134
135 /*
136 * Remove any trailing LF / CR
137 */
138 fr_sbuff_trim(&state->exec.stdout_buff, sbuff_char_line_endings);
139
140 fr_value_box_list_init(&state->list);
141 MEM(box = fr_value_box_alloc(state->ctx, FR_TYPE_STRING, NULL));
142 if (fr_value_box_from_str(state->ctx, box, type, NULL,
143 fr_sbuff_start(&state->exec.stdout_buff),
144 fr_sbuff_used(&state->exec.stdout_buff),
145 NULL) < 0) {
146 talloc_free(box);
147 *p_result = RLM_MODULE_FAIL;
149 }
150 fr_value_box_list_insert_head(&state->list, box);
151 }
152
153resume:
154 /*
155 * Inform the caller of the status if it asked for it
156 */
157 if (state->args.exec.status_out) *state->args.exec.status_out = state->exec.status;
158
159 /*
160 * Ensure that the callers resume function is called.
161 */
163 return unlang_tmpl_resume(p_result, request, frame);
164}
165
166
167/** Wrapper to call exec after a tmpl has been expanded
168 *
169 */
172{
173 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
174
175 if (fr_exec_oneshot(state->ctx, &state->exec, request,
176 &state->list,
177 state->args.exec.env, false, false,
178 false,
179 (state->out != NULL), state,
180 state->args.exec.timeout) < 0) {
181 RPEDEBUG("Failed executing program");
182 *p_result = RLM_MODULE_FAIL;
184 }
185
186 fr_value_box_list_talloc_free(&state->list); /* this is the xlat expansion, and not the output string we want */
188
189 return UNLANG_ACTION_YIELD;
190}
191
192
194{
195 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
197
198 /*
199 * If we're not called from unlang_tmpl_push(), then
200 * ensure that we clean up the resulting value boxes
201 * and that the list to write the boxes in is initialised.
202 */
203 if (!state->ctx) {
204 state->ctx = state;
205 fr_value_box_list_init(&state->list);
206 }
207
208 /*
209 * Synchronous tmpls can just be resolved immediately, and directly to the output list.
210 *
211 * However, xlat expansions (including fully synchronous function calls!) need to be expanded by
212 * the xlat framework.
213 */
214 if (!tmpl_async_required(ut->tmpl) && !tmpl_contains_xlat(ut->tmpl)) {
215 if (tmpl_eval(state->ctx, state->out, request, ut->tmpl) < 0) {
216 RPEDEBUG("Failed evaluating expansion");
217 goto fail;
218 }
219
220 *p_result = RLM_MODULE_OK;
222 }
223
224 /*
225 * XLAT structs are allowed.
226 */
227 if (tmpl_is_xlat(ut->tmpl)) {
229 goto push;
230 }
231
233
234 /*
235 * Expand the arguments to the program we're executing.
236 */
238push:
239 if (unlang_xlat_push(state->ctx, NULL, &state->list, request, tmpl_xlat(ut->tmpl), false) < 0) {
240 fail:
241 *p_result = RLM_MODULE_FAIL;
243 }
244
246}
247
248/** Push a tmpl onto the stack for evaluation
249 *
250 * @param[in] ctx To allocate value boxes and values in.
251 * @param[out] out The value_box created from the tmpl. May be NULL,
252 * in which case the result is discarded.
253 * @param[in] request The current request.
254 * @param[in] tmpl the tmpl to expand
255 * @param[in] args additional controls for expanding #TMPL_TYPE_EXEC,
256 * and where the status of exited programs will be stored.
257 * @return
258 * - 0 on success
259 * - -1 on failure
260 */
261int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request,
262 tmpl_t const *tmpl, unlang_tmpl_args_t *args)
263{
264 unlang_stack_t *stack = request->stack;
267
268 unlang_tmpl_t *ut;
269
270 static unlang_t tmpl_instruction = {
272 .name = "tmpl",
273 .debug_name = "tmpl",
274 .actions = {
275 .actions = {
276 [RLM_MODULE_REJECT] = 0,
277 [RLM_MODULE_FAIL] = 0,
278 [RLM_MODULE_OK] = 0,
279 [RLM_MODULE_HANDLED] = 0,
280 [RLM_MODULE_INVALID] = 0,
283 [RLM_MODULE_NOOP] = 0,
285 },
286 .retry = RETRY_INIT,
287 },
288 };
289
290 if (tmpl_needs_resolving(tmpl)) {
291 REDEBUG("Expansion \"%pV\" needs to be resolved before it is used", fr_box_strvalue_len(tmpl->name, tmpl->len));
292 return -1;
293 }
294
296
297 MEM(ut = talloc(stack, unlang_tmpl_t));
298 *ut = (unlang_tmpl_t){
299 .self = tmpl_instruction,
300 .tmpl = tmpl
301 };
302
303 /*
304 * Push a new tmpl frame onto the stack
305 */
307 RLM_MODULE_NOT_SET, UNLANG_NEXT_STOP, false) < 0) return -1;
308
309 frame = &stack->frame[stack->depth];
310 state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
311
312 *state = (unlang_frame_state_tmpl_t) {
313 .out = out,
314 .ctx = ctx,
315 };
316 if (args) state->args = *args; /* Copy these because they're usually ephemeral/initialised as compound literal */
317
318 /*
319 * Default to something sensible
320 * instead of locking the same indefinitely.
321 */
322 if (!fr_time_delta_ispos(state->args.exec.timeout)) state->args.exec.timeout = fr_time_delta_from_sec(EXEC_TIMEOUT);
323
324 fr_value_box_list_init(&state->list);
325
326 return 0;
327}
328
330{
332 &(unlang_op_t){
333 .name = "tmpl",
334 .interpret = unlang_tmpl,
335 .signal = unlang_tmpl_signal,
336 .frame_state_size = sizeof(unlang_frame_state_tmpl_t),
337 .frame_state_type = "unlang_frame_state_tmpl_t",
338 });
339}
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_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
va_list args
Definition acutest.h:770
#define RCSID(id)
Definition build.h:485
#define MEM(x)
Definition debug.h:36
int fr_exec_oneshot(TALLOC_CTX *ctx, fr_exec_state_t *exec, request_t *request, fr_value_box_list_t *args, fr_pair_list_t *env_pairs, bool env_escape, bool env_inherit, bool need_stdin, bool store_stdout, TALLOC_CTX *stdout_ctx, fr_time_delta_t timeout)
Call an child program, optionally reading it's output.
Definition exec.c:982
void fr_exec_oneshot_cleanup(fr_exec_state_t *exec, int signal)
Cleans up an exec'd process on error.
Definition exec.c:664
#define EXEC_TIMEOUT
Default wait time for exec calls (in seconds).
Definition exec.h:32
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
#define RPEDEBUG(fmt,...)
Definition log.h:376
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
talloc_free(reap)
static char * stack[MAX_STACK]
Definition radmin.c:158
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
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
bool const sbuff_char_line_endings[UINT8_MAX+1]
Definition sbuff.c:104
size_t fr_sbuff_trim(fr_sbuff_t *sbuff, bool const to_trim[static UINT8_MAX+1])
Trim trailing characters from a string we're composing.
Definition sbuff.c:2130
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_used(_sbuff_or_marker)
#define tmpl_contains_xlat(vpt)
Definition tmpl.h:227
#define tmpl_is_xlat(vpt)
Definition tmpl.h:210
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
#define tmpl_is_exec(vpt)
Definition tmpl.h:211
bool tmpl_async_required(tmpl_t const *vpt)
Return whether or not async is required for this tmpl.
#define tmpl_xlat(_tmpl)
Definition tmpl.h:930
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:1339
int tmpl_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
Gets the value of a tmpl.
Definition tmpl_eval.c:1211
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:223
Signals that can be sent to a request.
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
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_delta_ispos(_a)
Definition time.h:290
static unlang_action_t unlang_tmpl_exec_wait_final(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Wrapper to call exec after the program has finished executing.
Definition tmpl.c:99
void unlang_tmpl_init(void)
Definition tmpl.c:329
static void unlang_tmpl_signal(request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition tmpl.c:49
static unlang_action_t unlang_tmpl_resume(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Wrapper to call a resumption function after a tmpl has been expanded.
Definition tmpl.c:76
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:261
static unlang_action_t unlang_tmpl(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition tmpl.c:193
static unlang_action_t unlang_tmpl_exec_wait_resume(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Wrapper to call exec after a tmpl has been expanded.
Definition tmpl.c:170
Declarations for the unlang tmpl interface.
fr_value_box_list_t * out
output list if the exec succeeds
Definition tmpl_priv.h:39
unlang_tmpl_args_t args
Arguments that control how the tmpl is evaluated.
Definition tmpl_priv.h:50
TALLOC_CTX * ctx
for allocating value boxes
Definition tmpl_priv.h:38
fr_unlang_tmpl_resume_t resume
resumption handler
Definition tmpl_priv.h:43
fr_value_box_list_t list
our intermediate working list
Definition tmpl_priv.h:40
void * rctx
for resume
Definition tmpl_priv.h:42
fr_unlang_tmpl_signal_t signal
signal handler
Definition tmpl_priv.h:44
A tmpl stack entry.
Definition tmpl_priv.h:37
struct unlang_tmpl_args_t::@102 exec
Exec specific arguments.
Arguments for evaluating different types of tmpls.
Definition tmpl.h:47
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 * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:92
unlang_t self
static unlang_t * unlang_tmpl_to_generic(unlang_tmpl_t const *p)
@ UNLANG_TYPE_TMPL
asynchronously expand a tmpl_t
Definition unlang_priv.h:75
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 unlang_tmpl_t * unlang_generic_to_tmpl(unlang_t const *p)
tmpl_t const * tmpl
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.
A naked xlat.
#define RETRY_INIT
Definition retry.h:39
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:5450
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:632
#define fr_box_strvalue_len(_val, _len)
Definition value.h:297
static size_t char ** out
Definition value.h:1012