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: 3e587370c688a1835d416a16b572e67c0b90e943 $
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: 3e587370c688a1835d416a16b572e67c0b90e943 $")
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("else"), 1 },
140 { L("elsif"), 1 },
141 { L("foreach"), 1 },
142 { L("group"), 1 },
143 { L("if"), 1 },
144 { L("limit"), 1 },
145 { L("load-balance"), 1 },
146 { L("redundant"), 1 },
147 { L("redundant-load-balance"), 1 },
148 { L("switch"), 1 },
149 { L("timeout"), 1 },
150 { L("transaction"), 1 },
151};
153
154/** Limit the operations which can appear in a transaction.
155 */
157{
158 CONF_ITEM *ci = NULL;
159
160 while ((ci = cf_item_next(cs, ci)) != NULL) {
161 char const *name;
162
163 if (cf_item_is_section(ci)) {
164 CONF_SECTION *subcs;
165
166 subcs = cf_item_to_section(ci);
167 name = cf_section_name1(subcs);
168
169 if (strcmp(name, "actions") == 0) continue;
170
171 /*
172 * Definitely an attribute editing thing.
173 */
174 if (*name == '&') continue;
175
177
179 cf_log_err(ci, "Invalid keyword in 'transaction'");
180 return false;
181 }
182
183 if (!transaction_ok(subcs)) return false;
184
185 continue;
186
187 } else if (cf_item_is_pair(ci)) {
188 CONF_PAIR *cp;
189
190 cp = cf_item_to_pair(ci);
191 name = cf_pair_attr(cp);
192
193 /*
194 * If there's a value then it's not a module call.
195 */
196 if (cf_pair_value(cp)) continue;
197
198 if (*name == '&') continue;
199
200 /*
201 * Allow rcodes via the "always" module.
202 */
204 continue;
205 }
206
207 cf_log_err(ci, "Invalid module reference in 'transaction'");
208 return false;
209
210 } else {
211 continue;
212 }
213 }
214
215 return true;
216}
217
219{
222 unlang_t *c;
223 unlang_compile_ctx_t unlang_ctx2;
224
225 if (cf_section_name2(cs) != NULL) {
226 cf_log_err(cs, "Unexpected argument to 'transaction' section");
227 cf_log_err(ci, DOC_KEYWORD_REF(transaction));
228 return NULL;
229 }
230
231 /*
232 * The transaction is empty, ignore it.
233 */
234 if (!cf_item_next(cs, NULL)) return UNLANG_IGNORE;
235
236 if (!transaction_ok(cs)) return NULL;
237
239 if (!g) return NULL;
240
242 c->debug_name = c->name = cf_section_name1(cs);
243
244 /*
245 * The default for a failed transaction is to continue to
246 * the next instruction on failure.
247 */
251
252 /*
253 * For the children of this keyword, any failure is
254 * return, not continue.
255 */
256 unlang_compile_ctx_copy(&unlang_ctx2, unlang_ctx);
257
262
263 return unlang_compile_children(g, &unlang_ctx2);
264}
265
267{
269 .name = "transaction",
272
274 .interpret = unlang_transaction,
276
277 .unlang_size = sizeof(unlang_transaction_t),
278 .unlang_name = "unlang_transaction_t",
279
280 .frame_state_size = sizeof(unlang_frame_state_transaction_t),
281 .frame_state_type = "unlang_frame_state_transaction_t",
282 });
283}
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:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:70
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:631
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_section(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_SECTION.
Definition cf_util.c:617
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
fr_token_t cf_section_name2_quote(CONF_SECTION const *cs)
Return the quoting of the name2 identifier.
Definition cf_util.c:1229
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1593
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1577
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:289
#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: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
#define MEM(x)
Definition debug.h:36
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
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:159
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
@ MOD_ACTION_RETURN
Definition mod_action.h:43
unlang_mod_action_t actions[RLM_MODULE_NUMCODES]
Definition mod_action.h:64
#define fr_assert(_expr)
Definition rad_assert.h:38
@ 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_NUMCODES
How many valid return codes there are.
Definition rcode.h:51
@ RLM_MODULE_HANDLED
The module handled the request, so stop.
Definition rcode.h:44
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:186
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:98
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:74
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:845
void fr_edit_list_abort(fr_edit_list_t *el)
Abort the entries in an edit list.
Definition edit.c:193
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:794
Track a series of edits.
Definition edit.c:101
static fr_slen_t parent
Definition pair.h:839
#define DOC_KEYWORD_REF(_x)
Definition version.h:89