The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
map.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: cdd9fe9420a50c7da9f48333824f45605ca66168 $
19 *
20 * @brief map and unlang integration.
21 * @brief Unlang "map" keyword evaluation.
22 *
23 * @ingroup AVP
24 *
25 * @copyright 2018 The FreeRADIUS server project
26 * @copyright 2018 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
27 */
28RCSID("$Id: cdd9fe9420a50c7da9f48333824f45605ca66168 $")
29
30#include <freeradius-devel/server/base.h>
31#include <freeradius-devel/unlang/tmpl.h>
32
33
34#include "map_priv.h"
35
36typedef enum {
37 UNLANG_UPDATE_MAP_INIT = 0, //!< Start processing a map.
38 UNLANG_UPDATE_MAP_EXPANDED_LHS, //!< Expand the LHS xlat or exec (if needed).
39 UNLANG_UPDATE_MAP_EXPANDED_RHS //!< Expand the RHS xlat or exec (if needed).
41
42/** State of an update block
43 *
44 */
45typedef struct {
46 fr_dcursor_t maps; //!< Cursor of maps to evaluate.
47
48 fr_dlist_head_t vlm_head; //!< Head of list of VP List Mod.
49
50 fr_value_box_list_t lhs_result; //!< Result of expanding the LHS
51 fr_value_box_list_t rhs_result; //!< Result of expanding the RHS.
52
53 unlang_update_state_t state; //!< What we're currently doing.
55
56/** State of a map block
57 *
58 */
59typedef struct {
60 fr_value_box_list_t src_result; //!< Result of expanding the map source.
62
63/** Apply a list of modifications on one or more fr_pair_t lists.
64 *
65 * @param[in] request The current request.
66 * @param[out] p_result The rcode indicating what the result
67 * of the operation was.
68 * @return
69 * - UNLANG_ACTION_CALCULATE_RESULT changes were applied.
70 * - UNLANG_ACTION_PUSHED_CHILD async execution of an expansion is required.
71 */
73{
74 unlang_stack_t *stack = request->stack;
75 unlang_stack_frame_t *frame = &stack->frame[stack->depth];
76 unlang_frame_state_update_t *update_state = frame->state;
77 vp_list_mod_t const *vlm = NULL;
78
79 /*
80 * No modifications...
81 */
82 if (fr_dlist_empty(&update_state->vlm_head)) {
83 RDEBUG2("Nothing to update");
84 goto done;
85 }
86
87 /*
88 * Apply the list of modifications. This should not fail
89 * except on memory allocation error.
90 */
91 while ((vlm = fr_dlist_next(&update_state->vlm_head, vlm))) {
92 int ret;
93
94 ret = map_list_mod_apply(request, vlm);
95 if (!fr_cond_assert(ret == 0)) {
96 TALLOC_FREE(frame->state);
97
98 *p_result = RLM_MODULE_FAIL;
100 }
101 }
102
103done:
104 *p_result = RLM_MODULE_NOOP;
106}
107
108/** Create a list of modifications to apply to one or more fr_pair_t lists
109 *
110 * @param[out] p_result The rcode indicating what the result
111 * of the operation was.
112 * @param[in] request The current request.
113 * @param[in] frame Current stack frame.
114 * @return
115 * - UNLANG_ACTION_CALCULATE_RESULT changes were applied.
116 * - UNLANG_ACTION_PUSHED_CHILD async execution of an expansion is required.
117 */
119{
120 unlang_frame_state_update_t *update_state = talloc_get_type_abort(frame->state, unlang_frame_state_update_t);
121 map_t *map;
122
123 /*
124 * Iterate over the maps producing a set of modifications to apply.
125 */
126 for (map = fr_dcursor_current(&update_state->maps);
127 map;
128 map = fr_dcursor_next(&update_state->maps)) {
129 repeatable_set(frame); /* Call us again when done */
130
131 switch (update_state->state) {
134
135 fr_assert(fr_value_box_list_empty(&update_state->lhs_result)); /* Should have been consumed */
136 fr_assert(fr_value_box_list_empty(&update_state->rhs_result)); /* Should have been consumed */
137
138 switch (map->lhs->type) {
139 default:
140 break;
141
142 case TMPL_TYPE_EXEC:
143 if (unlang_tmpl_push(update_state, &update_state->lhs_result,
144 request, map->lhs,
145 NULL) < 0) {
146 *p_result = RLM_MODULE_FAIL;
148 }
150
151 case TMPL_TYPE_XLAT:
152 if (unlang_xlat_push(update_state, NULL, &update_state->lhs_result,
153 request, tmpl_xlat(map->lhs), false) < 0) {
154 *p_result = RLM_MODULE_FAIL;
156 }
158
160 case TMPL_TYPE_REGEX:
164 fr_assert(0);
165 error:
166 TALLOC_FREE(frame->state);
167 repeatable_clear(frame);
168 *p_result = RLM_MODULE_FAIL;
169
171 }
173
175 /*
176 * map_to_list_mod() already concatenates the LHS, so we don't need to do it here.
177 */
178 if (!map->rhs) goto next;
179
181
182 switch (map->rhs->type) {
183 default:
184 break;
185
186 case TMPL_TYPE_EXEC:
187 if (unlang_tmpl_push(update_state, &update_state->rhs_result,
188 request, map->rhs, NULL) < 0) {
189 *p_result = RLM_MODULE_FAIL;
191 }
193
194 case TMPL_TYPE_XLAT:
195 if (unlang_xlat_push(update_state, NULL, &update_state->rhs_result,
196 request, tmpl_xlat(map->rhs), false) < 0) {
197 *p_result = RLM_MODULE_FAIL;
199 }
201
202 case TMPL_TYPE_REGEX:
207 fr_assert(0);
208 goto error;
209 }
211
213 {
214 vp_list_mod_t *new_mod;
215 /*
216 * Concat the top level results together
217 */
218 if (!fr_value_box_list_empty(&update_state->rhs_result) &&
220 fr_value_box_list_head(&update_state->rhs_result), &update_state->rhs_result, FR_TYPE_STRING,
222 SIZE_MAX) < 0)) {
223 RPEDEBUG("Failed concatenating RHS expansion results");
224 goto error;
225 }
226
227 if (map_to_list_mod(update_state, &new_mod,
228 request, map,
229 &update_state->lhs_result, &update_state->rhs_result) < 0) goto error;
230 if (new_mod) fr_dlist_insert_tail(&update_state->vlm_head, new_mod);
231
232 fr_value_box_list_talloc_free(&update_state->rhs_result);
233 }
234
235 next:
236 update_state->state = UNLANG_UPDATE_MAP_INIT;
237 fr_value_box_list_talloc_free(&update_state->lhs_result);
238
239 break;
240 }
241 }
242
243 return list_mod_apply(p_result, request);
244}
245
246
247/** Execute an update block
248 *
249 * Update blocks execute in two phases, first there's an evaluation phase where
250 * each input map is evaluated, outputting one or more modification maps. The modification
251 * maps detail a change that should be made to a list in the current request.
252 * The request is not modified during this phase.
253 *
254 * The second phase applies those modification maps to the current request.
255 * This re-enables the atomic functionality of update blocks provided in v2.x.x.
256 * If one map fails in the evaluation phase, no more maps are processed, and the current
257 * result is discarded.
258 */
260{
263 unlang_frame_state_update_t *update_state;
264
265 /*
266 * Initialise the frame state
267 */
268 MEM(frame->state = update_state = talloc_zero_pooled_object(request->stack, unlang_frame_state_update_t,
269 (sizeof(map_t) +
270 (sizeof(tmpl_t) * 2) + 128),
271 g->num_children)); /* 128 is for string buffers */
272
273 fr_dcursor_init(&update_state->maps, &gext->map.head);
274 fr_value_box_list_init(&update_state->lhs_result);
275 fr_value_box_list_init(&update_state->rhs_result);
276 fr_dlist_init(&update_state->vlm_head, vp_list_mod_t, entry);
277
278 /*
279 * Call list_mod_create
280 */
282 return list_mod_create(p_result, request, frame);
283}
284
286#ifdef WITH_VERIFY_PTR
288#else
290#endif
291 )
292{
293#ifdef WITH_VERIFY_PTR
294 unlang_frame_state_map_proc_t *map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
295 VALUE_BOX_LIST_VERIFY(&map_proc_state->src_result);
296#endif
298}
299
301{
304
306 unlang_frame_state_map_proc_t *map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
307
308 RDEBUG2("MAP %s \"%pM\"", inst->proc->name, &map_proc_state->src_result);
309
310 VALUE_BOX_LIST_VERIFY(&map_proc_state->src_result);
312 return map_proc(p_result, request, gext->proc_inst, &map_proc_state->src_result);
313}
314
316{
320 unlang_frame_state_map_proc_t *map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
321
322 /*
323 * Initialise the frame state
324 */
325 repeatable_set(frame);
326
327 fr_value_box_list_init(&map_proc_state->src_result);
328 /*
329 * Set this BEFORE doing anything else, as we will be
330 * called again after unlang_xlat_push() returns.
331 */
332 frame->process = map_proc_apply;
333
334 /*
335 * Expand the map source
336 */
337 if (inst->src) switch (inst->src->type) {
338 default:
339 {
340 fr_value_box_t *src_result = NULL;
341 if (tmpl_aexpand(frame->state, &src_result,
342 request, inst->src, NULL, NULL) < 0) {
343 REDEBUG("Failed expanding map src");
344 error:
345 *p_result = RLM_MODULE_FAIL;
347 }
348 fr_value_box_list_insert_head(&map_proc_state->src_result, src_result);
349 break;
350 }
351 case TMPL_TYPE_EXEC:
352 if (unlang_tmpl_push(map_proc_state, &map_proc_state->src_result,
353 request, inst->src, NULL) < 0) {
354 *p_result = RLM_MODULE_FAIL;
356 }
358
359 case TMPL_TYPE_XLAT:
360 if (unlang_xlat_push(map_proc_state, NULL, &map_proc_state->src_result,
361 request, tmpl_xlat(inst->src), false) < 0) {
362 *p_result = RLM_MODULE_FAIL;
364 }
366
367
368 case TMPL_TYPE_REGEX:
373 fr_assert(0);
374 goto error;
375 }
376
377 return map_proc_apply(p_result, request, frame);
378}
379
381{
383 &(unlang_op_t){
384 .name = "update",
385 .interpret = unlang_update_state_init,
386 .debug_braces = true
387 });
388
390 &(unlang_op_t){
391 .name = "map",
392 .rcode_set = true,
393 .interpret = unlang_map_state_init,
394 .frame_state_size = sizeof(unlang_frame_state_map_proc_t),
395 .frame_state_type = "unlang_frame_state_map_proc_t",
396 });
397}
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:43
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:37
#define RCSID(id)
Definition build.h:485
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define UNUSED
Definition build.h:317
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition dcursor.h:732
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:139
#define MEM(x)
Definition debug.h:36
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:260
static bool fr_dlist_empty(fr_dlist_head_t const *list_head)
Check whether a list has any items.
Definition dlist.h:501
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:378
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:555
Head of a doubly linked list.
Definition dlist.h:51
#define RPEDEBUG(fmt,...)
Definition log.h:376
void unlang_register(int type, unlang_op_t *op)
Register an operation with the interpreter.
Definition base.c:63
static unlang_action_t list_mod_create(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Create a list of modifications to apply to one or more fr_pair_t lists.
Definition map.c:118
static unlang_action_t map_proc_resume(UNUSED rlm_rcode_t *p_result, UNUSED request_t *request, UNUSED unlang_stack_frame_t *frame)
Definition map.c:285
fr_value_box_list_t lhs_result
Result of expanding the LHS.
Definition map.c:50
unlang_update_state_t state
What we're currently doing.
Definition map.c:53
static unlang_action_t map_proc_apply(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition map.c:300
static unlang_action_t unlang_update_state_init(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Execute an update block.
Definition map.c:259
void unlang_map_init(void)
Definition map.c:380
static unlang_action_t unlang_map_state_init(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition map.c:315
fr_dlist_head_t vlm_head
Head of list of VP List Mod.
Definition map.c:48
static unlang_action_t list_mod_apply(rlm_rcode_t *p_result, request_t *request)
Apply a list of modifications on one or more fr_pair_t lists.
Definition map.c:72
unlang_update_state_t
map and unlang integration.
Definition map.c:36
@ UNLANG_UPDATE_MAP_EXPANDED_RHS
Expand the RHS xlat or exec (if needed).
Definition map.c:39
@ UNLANG_UPDATE_MAP_INIT
Start processing a map.
Definition map.c:37
@ UNLANG_UPDATE_MAP_EXPANDED_LHS
Expand the LHS xlat or exec (if needed).
Definition map.c:38
fr_dcursor_t maps
Cursor of maps to evaluate.
Definition map.c:46
fr_value_box_list_t rhs_result
Result of expanding the RHS.
Definition map.c:51
fr_value_box_list_t src_result
Result of expanding the map source.
Definition map.c:60
State of a map block.
Definition map.c:59
State of an update block.
Definition map.c:45
static char * stack[MAX_STACK]
Definition radmin.c:158
int map_list_mod_apply(request_t *request, vp_list_mod_t const *vlm)
Apply the output of map_to_list_mod to a request.
Definition map_async.c:914
int map_to_list_mod(TALLOC_CTX *ctx, vp_list_mod_t **out, request_t *request, map_t const *map, fr_value_box_list_t *lhs_result, fr_value_box_list_t *rhs_result)
Evaluate a map creating a new map with TMPL_TYPE_ATTR LHS and TMPL_TYPE_DATA RHS.
Definition map_async.c:248
A list modification.
Definition map.h:99
map_proc_inst_t * proc_inst
Definition map_priv.h:35
static unlang_map_t * unlang_group_to_map(unlang_group_t *g)
Cast a group structure to the map keyword extension.
Definition map_priv.h:41
map_list_t map
Head of the map list.
Definition map_priv.h:34
Map processor registration.
Map processor instance.
@ FR_TYPE_STRING
String of printable characters.
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:81
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG2(fmt,...)
Definition radclient.h:54
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
@ RLM_MODULE_FAIL
Module failed, don't reply.
Definition rcode.h:42
@ RLM_MODULE_NOOP
Module succeeded without doing anything.
Definition rcode.h:48
#define tmpl_xlat(_tmpl)
Definition tmpl.h:930
@ TMPL_TYPE_REGEX_UNCOMPILED
Regex where compilation is possible but hasn't been performed yet.
Definition tmpl.h:158
@ TMPL_TYPE_XLAT
Pre-parsed xlat expansion.
Definition tmpl.h:146
@ TMPL_TYPE_EXEC
Callout to an external script or program.
Definition tmpl.h:150
@ TMPL_TYPE_REGEX_XLAT_UNRESOLVED
A regular expression with unresolved xlat functions or attribute references.
Definition tmpl.h:197
@ TMPL_TYPE_REGEX
Compiled (and possibly JIT'd) regular expression.
Definition tmpl.h:154
@ TMPL_TYPE_XLAT_UNRESOLVED
A xlat expansion with unresolved xlat functions or attribute references.
Definition tmpl.h:193
@ TMPL_TYPE_REGEX_XLAT
A regex containing xlat expansions.
Definition tmpl.h:162
#define tmpl_aexpand(_ctx, _out, _request, _vpt, _escape, _escape_ctx)
Expand a tmpl to a C type, allocing a new buffer to hold the string.
Definition tmpl.h:1062
eap_aka_sim_process_conf_t * inst
eap_type_t type
The preferred EAP-Type of this instance of the EAP-SIM/AKA/AKA' state machine.
Value pair map.
Definition map.h:77
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition map.h:78
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition map.h:79
#define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:177
int unlang_tmpl_push(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *tmpl, unlang_tmpl_args_t *args)
Push a tmpl onto the stack for evaluation.
Definition tmpl.c:261
int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:282
static void repeatable_clear(unlang_stack_frame_t *frame)
void * state
Stack frame specialisations.
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
@ UNLANG_TYPE_UPDATE
Update block.
Definition unlang_priv.h:56
@ UNLANG_TYPE_MAP
Mapping section (like UNLANG_TYPE_UPDATE, but uses values from a map_proc_t call).
Definition unlang_priv.h:62
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 void repeatable_set(unlang_stack_frame_t *frame)
unlang_process_t process
function to call for interpreting this stack frame
Generic representation of a grouping.
An unlang operation.
Our interpreter stack, as distinct from the C stack.
An unlang stack associated with a request.
int fr_value_box_list_concat_in_place(TALLOC_CTX *ctx, fr_value_box_t *out, fr_value_box_list_t *list, fr_type_t type, fr_value_box_list_action_t proc_action, bool flatten, size_t max_size)
Concatenate a list of value boxes.
Definition value.c:5934
@ FR_VALUE_BOX_LIST_FREE
Definition value.h:232
#define VALUE_BOX_LIST_VERIFY(_x)
Definition value.h:1305