The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
timeout.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: bdc07ef5282bf445410591536b2900242529c2e9 $
19 *
20 * @file unlang/timeout.c
21 * @brief Unlang "timeout" keyword evaluation.
22 *
23 * @copyright 2022 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: bdc07ef5282bf445410591536b2900242529c2e9 $")
26
27#include <freeradius-devel/unlang/timeout.h>
28#include <freeradius-devel/unlang/interpret.h>
29#include <freeradius-devel/server/rcode.h>
30#include "group_priv.h"
31#include "timeout_priv.h"
32#include "mod_action.h"
33#include "unlang_priv.h"
34
35typedef struct {
36 bool fired;
37 int depth;
42
43 fr_value_box_list_t result;
44
45 unlang_t *instruction; //!< to run on timeout
47
48/** Immediately cancel the timeout if the frame is cancelled
49 */
51{
52 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
53
54 if (action == FR_SIGNAL_CANCEL) {
55 TALLOC_FREE(state->ev);
56 }
57}
58
60{
61 unlang_frame_state_timeout_t *state = talloc_get_type_abort(ctx, unlang_frame_state_timeout_t);
62 request_t *request = talloc_get_type_abort(state->request, request_t);
63 unlang_stack_t *stack = request->stack;
64 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
65 char const *module;
66
67 /*
68 * Don't log in the context of the request
69 */
70 module = request->module;
71 request->module = NULL;
72 RDEBUG("Timeout reached, signalling interpreter to cancel child section.");
73 request->module = module;
74
75 /*
76 * Signal all the frames upto, but not including the timeout
77 * frame to cancel.
78 *
79 * unlang_timeout_resume_done then runs, and returns "timeout"
80 */
82
83 /*
84 * If the frame is yielded (needs to be resumed), but was cancelled
85 * we now need to mark it runnable again so it's unwound.
86 *
87 * If the frame _isn't_ cancelled, then it's non-cancellable and
88 * something else will run it to completion, and mark
89 * the request as complete.
90 */
92 state->fired = true;
93
94 RINDENT_RESTORE(request, state);
95
96 if (!state->instruction) return;
97
98 /*
99 * Push something else onto the stack to execute.
100 */
102 FRAME_CONF(RLM_MODULE_TIMEOUT, true)) < 0)) {
103 unlang_interpret_signal(request, FR_SIGNAL_CANCEL); /* also stops the request and does cleanups */
104 }
105}
106
108{
109 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
110
111 /*
112 * No timeout, we go to the next instruction.
113 *
114 * Unless the next instruction is a "catch timeout", in which case we skip it.
115 */
116 if (!state->fired) return UNLANG_ACTION_EXECUTE_NEXT; /* Don't modify the return code*/
117
119}
120
122{
123 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
124
125 /*
126 * Save current indentation for the error path.
127 */
128 RINDENT_SAVE(state, request);
129
130 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, state->timeout,
131 false, unlang_timeout_handler, state) < 0) {
132 RPEDEBUG("Failed inserting event");
134 }
135
137
139}
140
142{
143 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
144 fr_value_box_t *box = fr_value_box_list_head(&state->result);
145
146 /*
147 * compile_timeout() ensures that the tmpl is cast to time_delta, so we don't have to do any more work here.
148 */
149 state->timeout = box->vb_time_delta;
150
151 return unlang_timeout_set(p_result, request, frame);
152}
153
155{
157 unlang_timeout_t *gext;
158 unlang_frame_state_timeout_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_timeout_t);
159 unlang_stack_t *stack = request->stack;
160
162 gext = unlang_group_to_timeout(g);
163
164 /*
165 * +1 so we don't mark the timeout frame as cancelled,
166 * we want unlang_timeout_resume_done to be called.
167 */
168 state->depth = stack->depth + 1;
169 state->request = request;
170
171 if (!gext->vpt) {
172 state->timeout = gext->timeout;
173 return unlang_timeout_set(p_result, request, frame);
174 }
175
176 fr_value_box_list_init(&state->result);
177
178 if (unlang_tmpl_push(state, &state->result, request, gext->vpt, NULL) < 0) return UNLANG_ACTION_FAIL;
179
181
183}
184
185/** When a timeout fires, run the given section.
186 *
187 * @param[in] request to push timeout onto
188 * @param[in] timeout when to run the timeout
189 * @param[in] cs section to run when the timeout fires.
190 * @param[in] top_frame Set to UNLANG_TOP_FRAME if the interpreter should return.
191 * Set to UNLANG_SUB_FRAME if the interprer should continue.
192 *
193 * @return
194 * - 0 on success.
195 * - -1 on failure.
196 */
197int unlang_timeout_section_push(request_t *request, CONF_SECTION *cs, fr_time_delta_t timeout, bool top_frame)
198{
199 /** Static instruction for performing xlat evaluations
200 *
201 */
202 static unlang_t timeout_instruction = {
204 .name = "timeout",
205 .debug_name = "timeout",
206 .actions = {
207 .actions = {
208 [RLM_MODULE_REJECT] = 0,
209 [RLM_MODULE_FAIL] = MOD_ACTION_RETURN, /* Exit out of nested levels */
210 [RLM_MODULE_OK] = 0,
211 [RLM_MODULE_HANDLED] = 0,
212 [RLM_MODULE_INVALID] = 0,
215 [RLM_MODULE_NOOP] = 0,
216 [RLM_MODULE_TIMEOUT] = MOD_ACTION_RETURN, /* Exit out of nested levels */
218 },
219 .retry = RETRY_INIT,
220 },
221 };
222
224 unlang_stack_t *stack = request->stack;
226 unlang_t *instruction;
227
228 /*
229 * Get the instruction we are supposed to run on timeout.
230 */
231 instruction = (unlang_t *)cf_data_value(cf_data_find(cs, unlang_group_t, NULL));
232 if (!instruction) {
233 REDEBUG("Failed to find pre-compiled unlang for section %s { ... }",
234 cf_section_name1(cs));
235 return -1;
236 }
237
238 /*
239 * Push a new timeout frame onto the stack
240 */
241 if (unlang_interpret_push(NULL, request, &timeout_instruction,
242 FRAME_CONF(RLM_MODULE_NOT_SET, top_frame), UNLANG_NEXT_STOP) < 0) return -1;
243 frame = &stack->frame[stack->depth];
244
245 /*
246 * Allocate its state, and set the timeout.
247 */
248 MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_timeout_t));
249
250 RINDENT_SAVE(state, request);
251 state->depth = stack->depth;
252 state->request = request;
253 state->timeout = timeout;
254 state->instruction = instruction;
255
256 if (fr_timer_in(state, unlang_interpret_event_list(request)->tl, &state->ev, timeout,
257 false, unlang_timeout_handler, state) < 0) {
258 RPEDEBUG("Failed setting timeout for section %s", cf_section_name1(cs));
259 return -1;
260 }
261
263
264 return 0;
265
266}
267
269{
271 char const *name2;
272 unlang_t *c;
274 unlang_timeout_t *gext;
276 tmpl_t *vpt = NULL;
277 fr_token_t token;
278
279 /*
280 * Timeout <time ref>
281 */
282 name2 = cf_section_name2(cs);
283 if (!name2) {
284 cf_log_err(cs, "You must specify a time value for 'timeout'");
285 print_url:
286 cf_log_err(ci, DOC_KEYWORD_REF(timeout));
287 return NULL;
288 }
289
290 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
291
293 if (!g) return NULL;
294
295 gext = unlang_group_to_timeout(g);
296
297 token = cf_section_name2_quote(cs);
298
299 if ((token == T_BARE_WORD) && isdigit((uint8_t) *name2)) {
300 if (fr_time_delta_from_str(&timeout, name2, strlen(name2), FR_TIME_RES_SEC) < 0) {
301 cf_log_err(cs, "Failed parsing time delta %s - %s",
302 name2, fr_strerror());
303 return NULL;
304 }
305 } else {
306 ssize_t slen;
307 tmpl_rules_t t_rules;
308
309 /*
310 * We don't allow unknown attributes here.
311 */
312 t_rules = *(unlang_ctx->rules);
313 t_rules.attr.allow_unknown = false;
314 RULES_VERIFY(&t_rules);
315
316 slen = tmpl_afrom_substr(gext, &vpt,
317 &FR_SBUFF_IN(name2, strlen(name2)),
318 token,
319 NULL,
320 &t_rules);
321 if (!vpt) {
322 cf_canonicalize_error(cs, slen, "Failed parsing argument to 'timeout'", name2);
323 talloc_free(g);
324 return NULL;
325 }
326
327 /*
328 * Fixup the tmpl so that we know it's somewhat sane.
329 */
330 if (!pass2_fixup_tmpl(gext, &vpt, cf_section_to_item(cs), unlang_ctx->rules->attr.dict_def)) {
331 talloc_free(g);
332 return NULL;
333 }
334
335 if (tmpl_is_list(vpt)) {
336 cf_log_err(cs, "Cannot use list as argument for 'timeout' statement");
337 error:
338 talloc_free(g);
339 goto print_url;
340 }
341
343 cf_log_err(cs, "Cannot use regular expression as argument for 'timeout' statement");
344 goto error;
345 }
346
347 /*
348 * Attribute or data MUST be cast to TIME_DELTA.
349 */
351 cf_log_perr(cs, "Failed setting cast type");
352 goto error;
353 }
354 }
355
356 /*
357 * Compile the contents of a "timeout".
358 */
360 if (!c) return NULL;
361
363 gext = unlang_group_to_timeout(g);
364 gext->timeout = timeout;
365 gext->vpt = vpt;
366
367 return c;
368}
369
371{
373 .name = "timeout",
374 .type = UNLANG_TYPE_TIMEOUT,
376
377 .compile = unlang_compile_timeout,
378 .interpret = unlang_timeout,
379 .signal = unlang_timeout_signal,
380
381 .unlang_size = sizeof(unlang_timeout_t),
382 .unlang_name = "unlang_timeout_t",
383
384 .frame_state_size = sizeof(unlang_frame_state_timeout_t),
385 .frame_state_type = "unlang_frame_state_timeout_t",
386 });
387}
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_EXECUTE_NEXT
Execute the next unlang_t.
Definition action.h:38
@ UNLANG_ACTION_STOP_PROCESSING
Break out of processing the current request (unwind).
Definition action.h:42
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
#define RCSID(id)
Definition build.h:485
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define RULES_VERIFY(_cs, _rules)
Definition cf_file.c:177
Common header for all CONF_* types.
Definition cf_priv.h:49
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
void * cf_data_value(CONF_DATA const *cd)
Return the user assigned value of CONF_DATA.
Definition cf_util.c:1762
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
char const * cf_section_name1(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1170
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:683
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1229
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_data_find(_cf, _type, _name)
Definition cf_util.h:244
#define cf_canonicalize_error(_ci, _slen, _msg, _str)
Definition cf_util.h:367
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:296
unlang_t * unlang_compile_section(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:1547
bool pass2_fixup_tmpl(UNUSED TALLOC_CTX *ctx, tmpl_t **vpt_p, CONF_ITEM const *ci, fr_dict_t const *dict)
Definition compile.c:104
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:456
#define MEM(x)
Definition debug.h:36
Declarations for the "group" keyword.
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:1620
bool unlang_request_is_scheduled(request_t const *request)
Return whether a request is currently scheduled.
Definition interpret.c:1573
int unlang_interpret_push_instruction(unlang_result_t *p_result, request_t *request, void *instruction, unlang_frame_conf_t const *conf)
Push an instruction onto the request stack for later interpretation.
Definition interpret.c:1166
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1396
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
void unlang_stack_signal(request_t *request, fr_signal_t action, int limit)
Delivers a frame to one or more frames in the stack.
Definition interpret.c:1336
unlang_action_t unlang_interpret_push_children(unlang_result_t *p_result, request_t *request, rlm_rcode_t default_rcode, bool do_next_sibling)
Push the children of the current frame onto a new frame onto the stack.
Definition interpret.c:389
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2017
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:153
#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
static TALLOC_CTX * unlang_ctx
Definition base.c:71
void unlang_register(unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:56
talloc_free(reap)
static char * stack[MAX_STACK]
Definition radmin.c:159
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
long int ssize_t
unsigned char uint8_t
Unlang module actions.
@ MOD_ACTION_RETURN
Definition mod_action.h:43
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG(fmt,...)
Definition radclient.h:53
@ 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_TIMEOUT
Module (or section) timed out.
Definition rcode.h:50
@ 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
#define RETURN_UNLANG_TIMEOUT
Definition rcode.h:65
#define FR_SBUFF_IN(_start, _len_or_end)
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
static bool tmpl_is_list(tmpl_t const *vpt)
Definition tmpl.h:920
static fr_slen_t vpt
Definition tmpl.h:1269
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
int tmpl_cast_set(tmpl_t *vpt, fr_type_t type)
Set a cast for a tmpl.
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
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
uint8_t allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:301
fr_slen_t fr_time_delta_from_str(fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint)
Create fr_time_delta_t from a string.
Definition time.c:412
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
@ FR_TIME_RES_SEC
Definition time.h:50
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
static unlang_action_t unlang_timeout_done(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:141
static void unlang_timeout_handler(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *ctx)
Definition timeout.c:59
void unlang_timeout_init(void)
Definition timeout.c:370
static void unlang_timeout_signal(UNUSED request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Immediately cancel the timeout if the frame is cancelled.
Definition timeout.c:50
static unlang_t * unlang_compile_timeout(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition timeout.c:268
static unlang_action_t unlang_timeout_set(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:121
static unlang_action_t unlang_timeout_resume_done(unlang_result_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:107
fr_time_delta_t timeout
Definition timeout.c:38
fr_value_box_list_t result
Definition timeout.c:43
int unlang_timeout_section_push(request_t *request, CONF_SECTION *cs, fr_time_delta_t timeout, bool top_frame)
When a timeout fires, run the given section.
Definition timeout.c:197
unlang_t * instruction
to run on timeout
Definition timeout.c:45
static unlang_action_t unlang_timeout(unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition timeout.c:154
fr_time_delta_t timeout
static unlang_timeout_t * unlang_group_to_timeout(unlang_group_t *g)
Cast a group structure to the timeout keyword extension.
An event timer list.
Definition timer.c:50
A timer event.
Definition timer.c:84
#define fr_timer_in(...)
Definition timer.h:87
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:271
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
Private interpreter structures and functions.
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:98
void * state
Stack frame specialisations.
#define UNLANG_NEXT_STOP
Definition unlang_priv.h:97
#define UNLANG_IGNORE
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_TIMEOUT
time-based timeouts.
Definition unlang_priv.h:72
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 bool is_yielded(unlang_stack_frame_t const *frame)
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
@ UNLANG_OP_FLAG_RCODE_SET
Set request->rcode to the result of this operation.
unlang_type_t type
The specialisation of this node.
static bool is_unwinding(unlang_stack_frame_t const *frame)
Generic representation of a grouping.
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.
static fr_slen_t parent
Definition pair.h:839
#define RETRY_INIT
Definition retry.h:39
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
#define DOC_KEYWORD_REF(_x)
Definition version.h:89