The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
transaction.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: 558098858a58515da0ecf99edcdad57a6554eb9c $
19 *
20 * @file unlang/transaction.c
21 * @brief Allows for edit transactions
22 *
23 * @copyright 2023 Network RADIUS SAS (legal@networkradius.com)
24 */
25
26RCSID("$Id: 558098858a58515da0ecf99edcdad57a6554eb9c $")
27
28#include <freeradius-devel/util/syserror.h>
29#include <freeradius-devel/server/rcode.h>
30#include "transaction.h"
31#include "transaction_priv.h"
32
33/** Signal a transaction to abort.
34 *
35 * @param[in] request The current request.
36 * @param[in] frame being signalled.
37 * @param[in] action to signal.
38 */
40{
41 unlang_frame_state_transaction_t *state = talloc_get_type_abort(frame->state,
43
44 /*
45 * Ignore everything except cancel.
46 */
47 if (action != FR_SIGNAL_CANCEL) return;
48
49 fr_edit_list_abort(state->el);
50 state->el = NULL;
51}
52
53/** Commit a successful transaction.
54 *
55 */
58{
59 unlang_frame_state_transaction_t *state = talloc_get_type_abort(frame->state,
61
62 fr_assert(state->el != NULL);
63
64 /*
65 * p_result contains OUR result, we want the section
66 * result from what was just executed on the stack.
67 */
68 switch (state->result.rcode) {
70 case RLM_MODULE_FAIL:
75 fr_edit_list_abort(state->el);
76 break;
77
78 case RLM_MODULE_OK:
80 case RLM_MODULE_NOOP:
83 fr_edit_list_commit(state->el);
84 break;
85
86 case RLM_MODULE_NUMCODES: /* Do not add default: */
87 fr_assert(0);
88 return UNLANG_ACTION_FAIL;
89 }
90
91 /*
92 * Allow the interpreter to access
93 * the result of the child section
94 */
95 *p_result = state->result;
96
98}
99
113
115{
117 unlang_stack_t *stack = request->stack;
118 int i, depth = stack->depth;
119
120 if (depth == 1) return NULL;
121
122 for (i = depth - 1; i > 0; i--) {
124
125 frame = &stack->frame[i];
126 if (frame->instruction->type != UNLANG_TYPE_TRANSACTION) continue;
127
128 state = talloc_get_type_abort(frame->state, unlang_frame_state_transaction_t);
129 fr_assert(state->el != NULL);
130
131 return state->el;
132 }
133
134 return NULL;
135}
136
138 { L("case"), 1 },
139 { L("catch"), 1 },
140 { L("else"), 1 },
141 { L("elsif"), 1 },
142 { L("foreach"), 1 },
143 { L("group"), 1 },
144 { L("if"), 1 },
145 { L("limit"), 1 },
146 { L("load-balance"), 1 },
147 { L("map"), 1 },
148 { L("redundant"), 1 },
149 { L("redundant-load-balance"), 1 },
150 { L("switch"), 1 },
151 { L("timeout"), 1 },
152 { L("transaction"), 1 },
153 { L("try"), 1 },
154};
156
157/** Limit the operations which can appear in a transaction.
158 */
160{
161 CONF_ITEM *ci = NULL;
162
163 while ((ci = cf_item_next(cs, ci)) != NULL) {
164 char const *name;
165
166 if (cf_item_is_section(ci)) {
167 CONF_SECTION *subcs;
168
169 subcs = cf_item_to_section(ci);
170 name = cf_section_name1(subcs);
171
172 /*
173 * Allow limited keywords.
174 */
175 if ((strcmp(name, "actions") == 0) ||
176 (strcmp(name, "if") == 0) ||
177 (strcmp(name, "else") == 0) ||
178 (strcmp(name, "elsif") == 0)) {
179 continue;
180 }
181
182 /*
183 * Ignore edits.
184 */
185 if (fr_list_assignment_op[cf_section_name2_quote(subcs)]) continue;
186
188 cf_log_err(ci, "Invalid keyword in 'transaction'");
189 return false;
190 }
191
192 if (!transaction_ok(subcs)) return false;
193
194 continue;
195
196 } else if (cf_item_is_pair(ci)) {
197 CONF_PAIR *cp;
198
199 cp = cf_item_to_pair(ci);
200 name = cf_pair_attr(cp);
201
202 /*
203 * If there's a value then it's not a module call.
204 */
205 if (cf_pair_value(cp)) continue;
206
207 /*
208 * Allow rcodes via the "always" module.
209 */
211 continue;
212 }
213
214 cf_log_err(ci, "Invalid module reference in 'transaction'");
215 return false;
216
217 } else {
218 continue;
219 }
220 }
221
222 return true;
223}
224
226{
229 unlang_t *c;
230 unlang_compile_ctx_t unlang_ctx2;
231
232 if (cf_section_name2(cs) != NULL) {
233 cf_log_err(cs, "Unexpected argument to 'transaction' section");
234 cf_log_err(ci, DOC_KEYWORD_REF(transaction));
235 return NULL;
236 }
237
238 /*
239 * The transaction is empty, ignore it.
240 */
241 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
242
243 if (!transaction_ok(cs)) return NULL;
244
246 if (!g) return NULL;
247
249 c->debug_name = c->name = cf_section_name1(cs);
250
251 /*
252 * The default for a failed transaction is to continue to
253 * the next instruction on failure.
254 */
258
259 /*
260 * For the children of this keyword, any failure is
261 * return, not continue.
262 */
263 unlang_compile_ctx_copy(&unlang_ctx2, unlang_ctx);
264
269
270 return unlang_compile_children(g, &unlang_ctx2);
271}
272
274{
276 .name = "transaction",
279
281 .interpret = unlang_transaction,
283
284 .unlang_size = sizeof(unlang_transaction_t),
285 .unlang_name = "unlang_transaction_t",
286
287 .frame_state_size = sizeof(unlang_frame_state_transaction_t),
288 .frame_state_type = "unlang_frame_state_transaction_t",
289 });
290}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_FAIL
Encountered an unexpected error.
Definition action.h:36
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
#define RCSID(id)
Definition build.h:512
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
#define UNUSED
Definition build.h:336
#define NUM_ELEMENTS(_t)
Definition build.h:358
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:72
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition cf_util.c:633
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1187
char const * cf_section_name1(CONF_SECTION const *cs)
Return the first identifier of a CONF_SECTION.
Definition cf_util.c:1173
CONF_SECTION * cf_item_to_section(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_SECTION.
Definition cf_util.c:685
bool cf_item_is_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:619
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:665
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1232
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1581
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1565
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:285
#define cf_item_next(_parent, _curr)
Definition cf_util.h:89
unlang_t * unlang_compile_children(unlang_group_t *g, unlang_compile_ctx_t *unlang_ctx_in)
Definition compile.c:1294
unlang_group_t * unlang_group_allocate(unlang_t *parent, CONF_SECTION *cs, unlang_type_t type)
Definition compile.c:458
fr_table_num_sorted_t const mod_rcode_table[]
Definition compile.c:74
#define MEM(x)
Definition debug.h:46
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:381
rlm_rcode_t rcode
The current rcode, from executing the instruction or merging the result from a frame.
Definition interpret.h:134
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
static char * stack[MAX_STACK]
Definition radmin.c:158
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
@ MOD_ACTION_RETURN
stop processing the section, and return the rcode with unset priority
Definition mod_action.h:41
#define MOD_PRIORITY(_x)
Definition mod_action.h:34
unlang_mod_action_t actions[RLM_MODULE_NUMCODES]
Definition mod_action.h:69
#define fr_assert(_expr)
Definition rad_assert.h:37
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:51
@ RLM_MODULE_OK
The module is OK, continue.
Definition rcode.h:49
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:48
@ RLM_MODULE_DISALLOW
Reject the request (user is locked out).
Definition rcode.h:52
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:47
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:56
@ RLM_MODULE_NOTFOUND
User not found.
Definition rcode.h:53
@ RLM_MODULE_UPDATED
OK (pairs modified).
Definition rcode.h:55
@ RLM_MODULE_NOT_SET
Error resolving rcode (should not be returned by modules).
Definition rcode.h:45
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:54
@ RLM_MODULE_NUMCODES
How many valid return codes there are.
Definition rcode.h:57
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:50
static char const * name
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
#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
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
const bool fr_list_assignment_op[T_TOKEN_LAST]
Definition token.c:187
static unlang_t * unlang_compile_transaction(unlang_t *parent, unlang_compile_ctx_t *unlang_ctx, CONF_ITEM const *ci)
static bool transaction_ok(CONF_SECTION *cs)
Limit the operations which can appear in a transaction.
void unlang_transaction_init(void)
static unlang_action_t unlang_transaction(UNUSED unlang_result_t *p_result, request_t *request, unlang_stack_frame_t *frame)
static unlang_action_t unlang_transaction_final(UNUSED unlang_result_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Commit a successful transaction.
Definition transaction.c:56
static int transaction_keywords_len
fr_edit_list_t * unlang_interpret_edit_list(request_t *request)
static fr_table_num_sorted_t transaction_keywords[]
static void unlang_transaction_signal(UNUSED request_t *request, unlang_stack_frame_t *frame, fr_signal_t action)
Signal a transaction to abort.
Definition transaction.c:39
Declarations for unlang transactions.
Declarations for unlang transactions.
fr_edit_list_t * el
my edit list
A transaction stack entry.
#define UNLANG_NEXT_SIBLING
Definition unlang_priv.h:99
char const * debug_name
Printed in log messages when the node is executed.
unlang_mod_actions_t actions
void * state
Stack frame specialisations.
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)
#define UNLANG_IGNORE
char const * name
Unknown...
@ UNLANG_TYPE_TRANSACTION
transactions for editing lists
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.
@ 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.
An unlang stack associated with a request.
void fr_edit_list_commit(fr_edit_list_t *el)
Commit an edit list.
Definition edit.c:853
void fr_edit_list_abort(fr_edit_list_t *el)
Abort the entries in an edit list.
Definition edit.c:202
fr_edit_list_t * fr_edit_list_alloc(TALLOC_CTX *ctx, int hint, fr_edit_list_t *parent)
Allocate an edit list.
Definition edit.c:802
Track a series of edits.
Definition edit.c:101
static fr_slen_t parent
Definition pair.h:858
#define DOC_KEYWORD_REF(_x)
Definition version.h:89