The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
try.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: aa1be92f8d61909a1a4d7c7280b0dd1a976d15d6 $
19 *
20 * @file unlang/try.c
21 * @brief Unlang "try" keyword evaluation.
22 *
23 * @copyright 2023 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: aa1be92f8d61909a1a4d7c7280b0dd1a976d15d6 $")
26
27#include "unlang_priv.h"
28#include "try_priv.h"
29
31{
32 rlm_rcode_t rcode = unlang_interpret_rcode(request);
34
37
38 /*
39 * Push the one "catch" section that we want to run. Once it's done, it will pop, return to us,
40 * and we will continue with the next sibling.
41 */
42 if (gext->catch[rcode]) {
43 if (unlang_interpret_push(NULL, request, gext->catch[rcode],
46 }
47
49 }
50
51 RDEBUG3("No catch section for %s",
52 fr_table_str_by_value(mod_rcode_table, rcode, "<invalid>"));
53
54 /*
55 * Go to the next sibling, which MUST NOT be a "catch".
56 */
58}
59
61{
62 /*
63 * When this frame finishes, jump ahead to the appropriate "catch".
64 *
65 * All of the magic is done in the compile phase.
66 */
68
70}
71
73{
76 unlang_t *c;
77 CONF_SECTION *parent_cs, *next;
78 int i;
79 CONF_SECTION *default_catch = NULL;
80 unlang_compile_ctx_t unlang_ctx2;
81 CONF_SECTION *catcher[RLM_MODULE_NUMCODES] = {};
82
83 /*
84 * The transaction is empty, ignore it.
85 */
86 if (!cf_item_next(cs, NULL)) {
87 cf_log_err(cs, "'try' sections cannot be empty");
88 print_url:
90 return NULL;
91 }
92
93 if (cf_section_name2(cs) != NULL) {
94 cf_log_err(cs, "Unexpected argument to 'try' section");
95 goto print_url;
96 }
97
98 parent_cs = cf_item_to_section(cf_parent(cs));
99 next = cf_section_next(parent_cs, cs);
100
101 if (!next ||
102 (strcmp(cf_section_name1(next), "catch") != 0)) {
103 cf_log_err(cs, "'try' sections must be followed by a 'catch'");
104 goto print_url;
105 }
106
108 if (!g) return NULL;
109
111 c->debug_name = c->name = cf_section_name1(cs);
112
113 /*
114 * Whatever the input compile ctx is, we over-ride the default actions.
115 */
116 unlang_compile_ctx_copy(&unlang_ctx2, unlang_ctx);
117
118 /*
119 * Loop over all of the "catch" sections, figuring out which rcodes are being "catch"ed.
120 */
121 for (default_catch = NULL;
122 next && (strcmp(cf_section_name1(next), "catch") == 0);
123 next = cf_section_next(parent_cs, next)) {
124 rlm_rcode_t rcode;
125 char const *name;
126
127 /*
128 * catch { ... } is the default, and we can't
129 * have anything after it.
130 */
131 if (default_catch) {
132 cf_log_err(default_catch, "Invalid 'catch' - cannot have another 'catch' after a default 'catch { ... }'");
133 cs = default_catch;
134 print_catch_url:
135 cf_log_err(cs, DOC_KEYWORD_REF(catch));
136 talloc_free(g);
137 return NULL;
138 }
139
140 name = cf_section_name2(next);
141 if (!name) {
142 default_catch = next;
143 continue;
144 }
145
147 if (rcode == RLM_MODULE_NOT_SET) {
148 cf_log_err(cs, "Invalid argument to 'catch' - unknown rcode '%s'.", name);
149 goto print_catch_url;
150 }
151
152 if (catcher[rcode]) {
153 cf_log_err(next, "Duplicate rcode '%s'", name);
154 cf_log_err(catcher[rcode], "First instance is here");
155 goto print_catch_url;
156 }
157 catcher[rcode] = next;
158
159 for (i = 0; (name = cf_section_argv(next, i)) != NULL; i++) {
161 if (rcode == RLM_MODULE_NOT_SET) {
162 cf_log_err(cs, "Invalid argument to 'catch' - unknown rcode '%s'.", name);
163 goto print_catch_url;
164 }
165
166 if (catcher[rcode]) {
167 cf_log_err(next, "Duplicate rcode '%s'", name);
168 cf_log_err(catcher[rcode], "First instance is here");
169 goto print_catch_url;
170 }
171
172 catcher[rcode] = next;
173 }
174 }
175
176 /*
177 * Check that the default will be used.
178 *
179 * Note that we do NOT change the priorities for the defaults.
180 */
181 if (default_catch) {
182 bool set = false;
183
184 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
185 if (!catcher[i]) {
186 set = true;
187 break;
188 }
189 }
190
191 if (!set) {
192 cf_log_err(default_catch, "Invalide 'catch { ... }' - all rcodes had previously been used");
193 goto print_catch_url;
194 }
195
196 /*
197 * Errors are still "return", even in a default "catch".
198 *
199 * Normal rcodes will run to the end of the try section, and then be "catch"ed.
200 */
201 if (!catcher[RLM_MODULE_REJECT]) catcher[RLM_MODULE_REJECT] = default_catch;
202 if (!catcher[RLM_MODULE_FAIL]) catcher[RLM_MODULE_FAIL] = default_catch;
203 if (!catcher[RLM_MODULE_INVALID]) catcher[RLM_MODULE_INVALID] = default_catch;
204 if (!catcher[RLM_MODULE_DISALLOW]) catcher[RLM_MODULE_DISALLOW] = default_catch;
205 if (!catcher[RLM_MODULE_TIMEOUT]) catcher[RLM_MODULE_TIMEOUT] = default_catch;
206 }
207
208 /*
209 * Loop again over the rcodes, setting the child actions to RETURN if necessary.
210 *
211 * If the child is returning for that action, ensure that _we_ aren't returning.
212 *
213 * Note that as above, reject / fail / invalid / disallow / timeout are errors, and cause the
214 * child to immediately return. All other rcodes
215 */
216 for (i = 0; i < RLM_MODULE_NUMCODES; i++) {
217 /*
218 * No one cares about this rcode. It can return, reject, etc.
219 */
220 if (!catcher[i]) continue;
221
222 /*
223 * Error rcodes cause the child section to bail immediately, but the "try" instruction
224 * does not bail.
225 */
226 if ((i == RLM_MODULE_REJECT) ||
227 (i == RLM_MODULE_FAIL) ||
228 (i == RLM_MODULE_INVALID) ||
229 (i == RLM_MODULE_DISALLOW) ||
230 (i == RLM_MODULE_TIMEOUT)) {
231 unlang_ctx2.actions.actions[i] = MOD_ACTION_RETURN;
233 continue;
234 }
235
236 /*
237 * Normal rcodes cause the child section to run to completion, and the "try" section does
238 * not bail.
239 */
240 if ((unlang_ctx2.actions.actions[i] > MOD_ACTION_NOT_SET) &&
241 (unlang_ctx2.actions.actions[i] < MOD_PRIORITY_MIN)) {
242 unlang_ctx2.actions.actions[i] = MOD_ACTION_NOT_SET;
244 }
245 }
246
247 /*
248 * Compile the children using the new compile ctx.
249 */
250 return unlang_compile_children(g, &unlang_ctx2);
251}
252
253
255{
257 .name = "try",
258 .type = UNLANG_TYPE_TRY,
260
261 .compile = unlang_compile_try,
262 .interpret = unlang_try,
263
264 .unlang_size = sizeof(unlang_try_t),
265 .unlang_name = "unlang_try_t",
266 });
267}
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_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
#define RCSID(id)
Definition build.h:485
#define UNUSED
Definition build.h:317
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
CONF_SECTION * cf_section_next(CONF_SECTION const *cs, CONF_SECTION const *curr)
Return the next child that's a CONF_SECTION.
Definition cf_util.c:996
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
char const * cf_section_argv(CONF_SECTION const *cs, int argc)
Return variadic argument at the specified index.
Definition cf_util.c:1212
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#define cf_parent(_cf)
Definition cf_util.h:101
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
unlang_t * unlang_compile_children(unlang_group_t *g, unlang_compile_ctx_t *unlang_ctx_in)
Definition compile.c:1288
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:447
fr_table_num_sorted_t const mod_rcode_table[]
Definition compile.c:75
int unlang_interpret_push(unlang_result_t *p_result, request_t *request, unlang_t const *instruction, unlang_frame_conf_t const *conf, bool do_next_sibling)
Push a new frame onto the stack.
Definition interpret.c:280
rlm_rcode_t unlang_interpret_rcode(request_t *request)
Get the last instruction result OR the last frame that was popped.
Definition interpret.c:1537
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:384
#define FRAME_CONF(_default_rcode, _top_frame)
Definition interpret.h:152
#define UNLANG_SUB_FRAME
Definition interpret.h:37
#define RDEBUG3(fmt,...)
Definition log.h:343
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)
@ MOD_ACTION_NOT_SET
default "not set by anything"
Definition mod_action.h:37
@ MOD_ACTION_RETURN
stop processing the section, and return the rcode with unset priority
Definition mod_action.h:40
#define MOD_PRIORITY_MIN
Definition mod_action.h:59
unlang_mod_action_t actions[RLM_MODULE_NUMCODES]
Definition mod_action.h:64
#define fr_assert(_expr)
Definition rad_assert.h:38
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:47
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:44
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:48
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:43
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:52
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:41
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:53
static char const * name
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
static unlang_action_t unlang_try(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition try.c:60
static unlang_action_t skip_to_catch(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition try.c:30
static unlang_t * unlang_compile_try(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition try.c:72
void unlang_try_init(void)
Definition try.c:254
Declaration for unlang try.
static unlang_try_t const * unlang_generic_to_try(unlang_t const *g)
Cast a generic structure to the try keyword extension.
Definition try_priv.h:41
unlang_t * catch[RLM_MODULE_NUMCODES]
Definition try_priv.h:35
#define UNLANG_NEXT_SIBLING
char const * debug_name
Printed in log messages when the node is executed.
unlang_mod_actions_t actions
unlang_mod_actions_t actions
Priorities, etc. for the various return codes.
static void unlang_compile_ctx_copy(unlang_compile_ctx_t *dst, unlang_compile_ctx_t const *src)
static unlang_t * unlang_group_to_generic(unlang_group_t const *p)
char const * name
Unknown...
@ UNLANG_TYPE_TRY
try / catch blocks
Definition unlang_priv.h:77
static void frame_repeat(unlang_stack_frame_t *frame, unlang_process_t process)
Mark the current stack frame up for repeat, and set a new process function.
unlang_t const * instruction
The unlang node we're evaluating.
@ UNLANG_OP_FLAG_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.
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.
static fr_slen_t parent
Definition pair.h:841
#define DOC_KEYWORD_REF(_x)
Definition version.h:89