The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 4e8118c7bb7cb93bfa7fa37fe88ed73083769f11 $
19  *
20  * @file unlang/transaction.c
21  * @brief Allows for edit transactions
22  *
23  * @copyright 2023 Network RADIUS SAS (legal@networkradius.com)
24  */
25 
26 RCSID("$Id: 4e8118c7bb7cb93bfa7fa37fe88ed73083769f11 $")
27 
28 #include <freeradius-devel/util/syserror.h>
29 #include "transaction.h"
30 #include "transaction_priv.h"
31 
32 /** Signal a transaction to abort.
33  *
34  * @param[in] request The current request.
35  * @param[in] frame being signalled.
36  * @param[in] action to signal.
37  */
39 {
40  unlang_frame_state_transaction_t *state = talloc_get_type_abort(frame->state,
42 
43  /*
44  * Ignore everything except cancel.
45  */
46  if (action != FR_SIGNAL_CANCEL) return;
47 
48  fr_edit_list_abort(state->el);
49  state->el = NULL;
50 }
51 
52 /** Commit a successful transaction.
53  *
54  */
56  unlang_stack_frame_t *frame)
57 {
58  unlang_frame_state_transaction_t *state = talloc_get_type_abort(frame->state,
60 
61  fr_assert(state->el != NULL);
62 
63  switch (*p_result) {
64  case RLM_MODULE_REJECT:
65  case RLM_MODULE_FAIL:
66  case RLM_MODULE_INVALID:
68  fr_edit_list_abort(state->el);
69  break;
70 
71  case RLM_MODULE_OK:
72  case RLM_MODULE_HANDLED:
73  case RLM_MODULE_NOOP:
74  case RLM_MODULE_UPDATED:
75  fr_edit_list_commit(state->el);
76  break;
77 
78  default:
79  fr_assert(0);
80  return UNLANG_ACTION_FAIL;
81  }
82 
84 }
85 
87 {
88  unlang_frame_state_transaction_t *state = talloc_get_type_abort(frame->state, unlang_frame_state_transaction_t);
90 
92 
93  MEM(state->el = fr_edit_list_alloc(state, 10, parent));
94 
96 
97  return unlang_interpret_push_children(p_result, request, frame->result, UNLANG_NEXT_SIBLING);
98 }
99 
101 {
102  unlang_stack_frame_t *frame;
103  unlang_stack_t *stack = request->stack;
104  int i, depth = stack->depth;
105 
106  if (depth == 1) return NULL;
107 
108  for (i = depth - 1; i > 0; i--) {
110 
111  frame = &stack->frame[i];
112  if (frame->instruction->type != UNLANG_TYPE_TRANSACTION) continue;
113 
114  state = talloc_get_type_abort(frame->state, unlang_frame_state_transaction_t);
115  fr_assert(state->el != NULL);
116 
117  return state->el;
118  }
119 
120  return NULL;
121 }
122 
124 {
126  &(unlang_op_t){
127  .name = "transaction",
128  .interpret = unlang_transaction,
129  .signal = unlang_transaction_signal,
130  .frame_state_size = sizeof(unlang_frame_state_transaction_t),
131  .frame_state_type = "unlang_frame_state_transaction_t",
132  .debug_braces = true
133  });
134 }
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:444
#define UNUSED
Definition: build.h:313
unlang_action_t unlang_interpret_push_children(rlm_rcode_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:243
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition: base.c:65
static char * stack[MAX_STACK]
Definition: radmin.c:158
static uint8_t depth(fr_minmax_heap_index_t i)
Definition: minmax_heap.c:83
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_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_UPDATED
OK (pairs modified).
Definition: rcode.h:49
@ 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
fr_signal_t
Definition: signal.h:48
fr_assert(0)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
static unlang_action_t unlang_transaction_final(rlm_rcode_t *p_result, UNUSED request_t *request, unlang_stack_frame_t *frame)
Commit a successful transaction.
Definition: transaction.c:55
static unlang_action_t unlang_transaction(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: transaction.c:86
void unlang_transaction_init(void)
Definition: transaction.c:123
fr_edit_list_t * unlang_interpret_edit_list(request_t *request)
Definition: transaction.c:100
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:38
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:103
void * state
Stack frame specialisations.
Definition: unlang_priv.h:304
@ UNLANG_TYPE_TRANSACTION
transactions for editing lists
Definition: unlang_priv.h:80
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.
Definition: unlang_priv.h:526
unlang_t const * instruction
The unlang node we're evaluating.
Definition: unlang_priv.h:289
rlm_rcode_t result
The result from executing the instruction.
Definition: unlang_priv.h:308
unlang_type_t type
The specialisation of this node.
Definition: unlang_priv.h:127
An unlang operation.
Definition: unlang_priv.h:214
Our interpreter stack, as distinct from the C stack.
Definition: unlang_priv.h:288
An unlang stack associated with a request.
Definition: unlang_priv.h:322
void fr_edit_list_commit(fr_edit_list_t *el)
Commit an edit list.
Definition: edit.c:841
void fr_edit_list_abort(fr_edit_list_t *el)
Abort the entries in an edit list.
Definition: edit.c:194
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:790
Track a series of edits.
Definition: edit.c:102
static fr_slen_t parent
Definition: pair.h:844