The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
catch.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: 70ef78fb7dd8c6b5878ad275e4acd2d4737c6714 $
19 *
20 * @file unlang/catch.c
21 * @brief Unlang "catch" keyword evaluation.
22 *
23 * @copyright 2023 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: 70ef78fb7dd8c6b5878ad275e4acd2d4737c6714 $")
26
27#include <freeradius-devel/server/rcode.h>
28#include "unlang_priv.h"
29#include "catch_priv.h"
30
32{
33 unlang_t *unlang;
34
36
37 for (unlang = frame->instruction->next;
38 unlang != NULL;
39 unlang = unlang->next) {
40 if (unlang->type == UNLANG_TYPE_CATCH) continue;
41
42 break;
43 }
44
45 return frame_set_next(frame, unlang);
46}
47
49{
50#ifndef NDEBUG
52
53 fr_assert(!c->catching[p_result->rcode]);
54#endif
55
56 /*
57 * Skip over any "catch" statementa after this one.
58 */
60
62}
63
64
65/** Skip ahead to a particular "catch" instruction.
66 *
67 */
69{
70 unlang_t *unlang;
71 rlm_rcode_t rcode = unlang_interpret_rcode(request);
72
74
75 /*
76 * 'try' at the end of a block without 'catch' should have been caught by the compiler.
77 */
78 fr_assert(frame->instruction->next);
79
80 for (unlang = frame->instruction->next;
81 unlang != NULL;
82 unlang = unlang->next) {
83 unlang_catch_t const *c;
84
85 if (unlang->type != UNLANG_TYPE_CATCH) {
86 not_caught:
87 RDEBUG3("No catch section for %s",
88 fr_table_str_by_value(mod_rcode_table, rcode, "<invalid>"));
89 return frame_set_next(frame, unlang);
90 }
91
92 if (rcode >= RLM_MODULE_NUMCODES) continue;
93
94 c = unlang_generic_to_catch(unlang);
95 if (c->catching[rcode]) break;
96 }
97 if (!unlang) goto not_caught;
98
99 return frame_set_next(frame, unlang);
100}
101
102static int catch_argv(CONF_SECTION *cs, unlang_catch_t *ca, char const *name)
103{
104 int rcode;
105
107 if (rcode < 0) {
108 cf_log_err(cs, "Unknown rcode '%s'.", name);
109 return -1;
110 }
111
112 if (ca->catching[rcode]) {
113 cf_log_err(cs, "Duplicate rcode '%s'.", name);
114 return -1;
115 }
116
117 ca->catching[rcode] = true;
118
119 return 0;
120}
121
123{
126 unlang_t *c;
127 unlang_catch_t *ca;
128 CONF_ITEM *prev;
129 char const *name;
130
131 prev = cf_item_prev(cf_parent(ci), ci);
132 while (prev && cf_item_is_data(prev)) prev = cf_item_prev(cf_parent(ci), prev);
133
134 if (!prev || !cf_item_is_section(prev)) {
135 fail:
136 cf_log_err(cs, "Found 'catch' section with no previous 'try'");
137 cf_log_err(ci, DOC_KEYWORD_REF(catch));
138 return NULL;
139 }
140
142 fr_assert(name != NULL);
143
144 if ((strcmp(name, "try") != 0) && (strcmp(name, "catch") != 0)) {
145 /*
146 * The previous thing has to be a section. And it has to
147 * be either a "try" or a "catch".
148 */
149 goto fail;
150 }
151
153 if (!g) return NULL;
154
156
157 /*
158 * Want to log what we caught
159 */
161
162 ca = unlang_group_to_catch(g);
163 if (!cf_section_name2(cs)) {
164 /*
165 * No arg2: catch errors
166 */
167 ca->catching[RLM_MODULE_REJECT] = true;
168 ca->catching[RLM_MODULE_FAIL] = true;
169 ca->catching[RLM_MODULE_INVALID] = true;
170 ca->catching[RLM_MODULE_DISALLOW] = true;
171
172 } else {
173 int i;
174
176
177 if (catch_argv(cs, ca, name) < 0) {
178 talloc_free(c);
179 return NULL;
180 }
181
182 for (i = 0; (name = cf_section_argv(cs, i)) != NULL; i++) {
183 if (catch_argv(cs, ca, name) < 0) {
184 talloc_free(c);
185 return NULL;
186 }
187 }
188 }
189
190 /*
191 * @todo - Else parse and limit the things we catch
192 */
194}
195
197{
199 .name = "catch",
200 .type = UNLANG_TYPE_CATCH,
202
203 .compile = unlang_compile_catch,
204 .interpret = unlang_catch,
205
206 .unlang_size = sizeof(unlang_catch_t),
207 .unlang_name = "unlang_catch_t",
208 });
209}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
#define RCSID(id)
Definition build.h:485
#define UNUSED
Definition build.h:317
static unlang_action_t unlang_catch(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition catch.c:48
static int catch_argv(CONF_SECTION *cs, unlang_catch_t *ca, char const *name)
Definition catch.c:102
unlang_action_t unlang_interpret_skip_to_catch(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Skip ahead to a particular "catch" instruction.
Definition catch.c:68
void unlang_catch_init(void)
Definition catch.c:196
static unlang_t * unlang_compile_catch(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
Definition catch.c:122
static unlang_action_t catch_skip_to_next(UNUSED unlang_result_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Definition catch.c:31
Declarations for the "catch" keyword.
static unlang_catch_t * unlang_group_to_catch(unlang_group_t *g)
Cast a group structure to the transaction keyword extension.
Definition catch_priv.h:42
static unlang_catch_t const * unlang_generic_to_catch(unlang_t const *g)
Cast a generic structure to the catch keyword extension.
Definition catch_priv.h:50
bool catching[RLM_MODULE_NUMCODES]
Definition catch_priv.h:34
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
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
bool cf_item_is_data(CONF_ITEM const *ci)
Determine if CONF_ITEM is CONF_DATA.
Definition cf_util.c:645
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:617
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_item_prev(_parent, _curr)
Definition cf_util.h:95
#define cf_parent(_cf)
Definition cf_util.h:101
unlang_t * unlang_compile_children(unlang_group_t *g, unlang_compile_ctx_t *unlang_ctx_in)
Definition compile.c:1324
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:456
fr_table_num_sorted_t const mod_rcode_table[]
Definition compile.c:76
rlm_rcode_t unlang_interpret_rcode(request_t *request)
Get the last instruction result OR the last frame that was popped.
Definition interpret.c:1545
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
#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)
#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:45
@ 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_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:52
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:51
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
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:514
Private interpreter structures and functions.
unlang_t * next
Next node (executed on UNLANG_ACTION_EXECUTE_NEXT et al).
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:98
char const * debug_name
Printed in log messages when the node is executed.
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:75
@ UNLANG_TYPE_CATCH
catch a previous try
Definition unlang_priv.h:76
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_action_t frame_set_next(unlang_stack_frame_t *frame, unlang_t *unlang)
@ UNLANG_OP_FLAG_DEBUG_BRACES
Print debug braces.
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:839
#define DOC_KEYWORD_REF(_x)
Definition version.h:89