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: d40d72252acfe868ab25337d60776f25b995db8a $
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: d40d72252acfe868ab25337d60776f25b995db8a $")
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");
84 }
85
86 if (state->out) fr_value_box_list_move(state->out, &state->list);
87
88 if (state->resume) return state->resume(p_result, request, state->rctx);
89
91}
92
93/** Wrapper to call exec after the program has finished executing
94 *
95 */
98{
99 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state,
101
102 /*
103 * The exec failed for some internal reason. We don't
104 * care about output, and we don't care about the programs exit status.
105 */
106 if (state->exec.failed) {
107 fr_value_box_list_talloc_free(&state->list);
108 goto resume;
109 }
110
111 fr_assert(state->exec.pid < 0); /* Assert this has been cleaned up */
112
113 if (!state->args.exec.stdout_on_error && (state->exec.status != 0)) {
114 fr_assert(fr_value_box_list_empty(&state->list));
115 goto resume;
116 }
117
118 /*
119 * We might want to just get the status of the program,
120 * and not care about the output.
121 *
122 * If we do care about the output, it's unquoted, and tainted.
123 *
124 * FIXME - It would be much more efficient to just reparent
125 * the string buffer into the context of the box... but we'd
126 * need to fix talloc first.
127 */
128 if (state->out) {
130 fr_value_box_t *box;
131
132 /*
133 * Remove any trailing LF / CR
134 */
135 fr_sbuff_trim(&state->exec.stdout_buff, sbuff_char_line_endings);
136
137 fr_value_box_list_init(&state->list);
138 MEM(box = fr_value_box_alloc(state->ctx, FR_TYPE_STRING, NULL));
139 if (fr_value_box_from_str(state->ctx, box, type, NULL,
140 fr_sbuff_start(&state->exec.stdout_buff),
141 fr_sbuff_used(&state->exec.stdout_buff),
142 NULL) < 0) {
143 talloc_free(box);
145 }
146 fr_value_box_list_insert_head(&state->list, box);
147 }
148
149resume:
150 /*
151 * Inform the caller of the status if it asked for it
152 */
153 if (state->args.exec.status_out) *state->args.exec.status_out = state->exec.status;
154
155 /*
156 * Ensure that the callers resume function is called.
157 */
159 return unlang_tmpl_resume(p_result, request, frame);
160}
161
162
163/** Wrapper to call exec after a tmpl has been expanded
164 *
165 */
168{
169 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
170
171 if (fr_exec_oneshot(state->ctx, &state->exec, request,
172 &state->list,
173 state->args.exec.env, false, false,
174 false,
175 (state->out != NULL), state,
176 state->args.exec.timeout) < 0) {
177 RPEDEBUG("Failed executing program");
179 }
180
181 fr_value_box_list_talloc_free(&state->list); /* this is the xlat expansion, and not the output string we want */
183
184 return UNLANG_ACTION_YIELD;
185}
186
187
189{
190 unlang_frame_state_tmpl_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
192
193 /*
194 * If we're not called from unlang_tmpl_push(), then
195 * ensure that we clean up the resulting value boxes
196 * and that the list to write the boxes in is initialised.
197 */
198 if (!state->ctx) {
199 state->ctx = state;
200 fr_value_box_list_init(&state->list);
201 }
202
203 /*
204 * Synchronous tmpls can just be resolved immediately, and directly to the output list.
205 *
206 * However, xlat expansions (including fully synchronous function calls!) need to be expanded by
207 * the xlat framework.
208 */
209 if (!tmpl_async_required(ut->tmpl) && !tmpl_contains_xlat(ut->tmpl)) {
210 if (tmpl_eval(state->ctx, state->out, request, ut->tmpl) < 0) {
211 RPEDEBUG("Failed evaluating expansion");
212 goto fail;
213 }
214
216 }
217
218 /*
219 * XLAT structs are allowed.
220 */
221 if (tmpl_is_xlat(ut->tmpl)) {
223 goto push;
224 }
225
227
228 /*
229 * Expand the arguments to the program we're executing.
230 */
232push:
233 if (unlang_xlat_push(state->ctx, NULL, &state->list, request, tmpl_xlat(ut->tmpl), false) < 0) {
234 fail:
236 }
237
239}
240
241/** Push a tmpl onto the stack for evaluation
242 *
243 * @param[in] ctx To allocate value boxes and values in.
244 * @param[out] out The value_box created from the tmpl. May be NULL,
245 * in which case the result is discarded.
246 * @param[in] request The current request.
247 * @param[in] tmpl the tmpl to expand
248 * @param[in] args additional controls for expanding #TMPL_TYPE_EXEC,
249 * and where the status of exited programs will be stored.
250 * @return
251 * - 0 on success
252 * - -1 on failure
253 */
254int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request,
255 tmpl_t const *tmpl, unlang_tmpl_args_t *args)
256{
257 unlang_stack_t *stack = request->stack;
260
261 unlang_tmpl_t *ut;
262
263 static unlang_t tmpl_instruction = {
265 .name = "tmpl",
266 .debug_name = "tmpl",
267 .actions = {
268 .actions = {
269 [RLM_MODULE_REJECT] = 0,
271 [RLM_MODULE_OK] = 0,
272 [RLM_MODULE_HANDLED] = 0,
273 [RLM_MODULE_INVALID] = 0,
276 [RLM_MODULE_NOOP] = 0,
278 },
279 .retry = RETRY_INIT,
280 },
281 };
282
283 if (tmpl_needs_resolving(tmpl)) {
284 REDEBUG("Expansion \"%pV\" needs to be resolved before it is used", fr_box_strvalue_len(tmpl->name, tmpl->len));
285 return -1;
286 }
287
289
290 MEM(ut = talloc(stack, unlang_tmpl_t));
291 *ut = (unlang_tmpl_t){
292 .self = tmpl_instruction,
293 .tmpl = tmpl
294 };
295
296 /*
297 * Push a new tmpl frame onto the stack
298 */
299 if (unlang_interpret_push(NULL, request, unlang_tmpl_to_generic(ut),
300 FRAME_CONF(RLM_MODULE_NOT_SET, false), UNLANG_NEXT_STOP) < 0) return -1;
301
302 frame = &stack->frame[stack->depth];
303 state = talloc_get_type_abort(frame->state, unlang_frame_state_tmpl_t);
304
305 /*
306 * Set the frame as repeatable so that multiple tmpls can
307 * be pushed on the stack before returning UNLANG_ACTION_PUSHED_CHILD
308 */
309 repeatable_set(frame);
310
311 *state = (unlang_frame_state_tmpl_t) {
312 .out = out,
313 .ctx = ctx,
314 };
315 if (args) state->args = *args; /* Copy these because they're usually ephemeral/initialised as compound literal */
316
317 /*
318 * Default to something sensible
319 * instead of locking the same indefinitely.
320 */
321 if (!fr_time_delta_ispos(state->args.exec.timeout)) state->args.exec.timeout = fr_time_delta_from_sec(EXEC_TIMEOUT);
322
323 fr_value_box_list_init(&state->list);
324
325 return 0;
326}
327
329{
331 &(unlang_op_t){
332 .name = "tmpl",
333 .interpret = unlang_tmpl,
334 .signal = unlang_tmpl_signal,
335 .frame_state_size = sizeof(unlang_frame_state_tmpl_t),
336 .frame_state_type = "unlang_frame_state_tmpl_t",
337 });
338}
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:42
@ UNLANG_ACTION_YIELD
Temporarily pause execution until an event occurs.
Definition action.h:41
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(unlang_result_t *result_p, 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:283
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:153
#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:159
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ MOD_ACTION_RETURN
Definition mod_action.h:43
#define fr_assert(_expr)
Definition rad_assert.h:38
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RETURN_UNLANG_FAIL
Definition rcode.h:57
#define RETURN_UNLANG_OK
Definition rcode.h:58
@ 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:52
@ 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:2161
#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:1228
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:1100
#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
void unlang_tmpl_init(void)
Definition tmpl.c:328
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_exec_wait_final(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Wrapper to call exec after the program has finished executing.
Definition tmpl.c:96
static unlang_action_t unlang_tmpl_exec_wait_resume(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Wrapper to call exec after a tmpl has been expanded.
Definition tmpl.c:166
static unlang_action_t unlang_tmpl(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition tmpl.c:188
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:254
static unlang_action_t unlang_tmpl_resume(unlang_result_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
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::@105 exec
Exec specific arguments.
Arguments for evaluating different types of tmpls.
Definition tmpl.h:48
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:282
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:97
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: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.
static unlang_tmpl_t * unlang_generic_to_tmpl(unlang_t const *p)
tmpl_t const * tmpl
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.
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:5459
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:640
#define fr_box_strvalue_len(_val, _len)
Definition value.h:305
static size_t char ** out
Definition value.h:1020