The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 05389205ff47c86789354e8c9680b06226d150eb $
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  */
28 RCSID("$Id: 05389205ff47c86789354e8c9680b06226d150eb $")
29 
30 #include <freeradius-devel/server/base.h>
31 #include <freeradius-devel/util/debug.h>
32 #include <freeradius-devel/unlang/tmpl.h>
33 
34 
35 #include "map_priv.h"
36 
37 typedef enum {
38  UNLANG_UPDATE_MAP_INIT = 0, //!< Start processing a map.
39  UNLANG_UPDATE_MAP_EXPANDED_LHS, //!< Expand the LHS xlat or exec (if needed).
40  UNLANG_UPDATE_MAP_EXPANDED_RHS //!< Expand the RHS xlat or exec (if needed).
42 
43 /** State of an update block
44  *
45  */
46 typedef struct {
47  fr_dcursor_t maps; //!< Cursor of maps to evaluate.
48 
49  fr_dlist_head_t vlm_head; //!< Head of list of VP List Mod.
50 
51  fr_value_box_list_t lhs_result; //!< Result of expanding the LHS
52  fr_value_box_list_t rhs_result; //!< Result of expanding the RHS.
53 
54  unlang_update_state_t state; //!< What we're currently doing.
56 
57 /** State of a map block
58  *
59  */
60 typedef struct {
61  fr_value_box_list_t src_result; //!< Result of expanding the map source.
63 
64 /** Apply a list of modifications on one or more fr_pair_t lists.
65  *
66  * @param[in] request The current request.
67  * @param[out] p_result The rcode indicating what the result
68  * of the operation was.
69  * @return
70  * - UNLANG_ACTION_CALCULATE_RESULT changes were applied.
71  * - UNLANG_ACTION_PUSHED_CHILD async execution of an expansion is required.
72  */
74 {
75  unlang_stack_t *stack = request->stack;
76  unlang_stack_frame_t *frame = &stack->frame[stack->depth];
77  unlang_frame_state_update_t *update_state = frame->state;
78  vp_list_mod_t const *vlm = NULL;
79 
80  /*
81  * No modifications...
82  */
83  if (fr_dlist_empty(&update_state->vlm_head)) {
84  RDEBUG2("Nothing to update");
85  goto done;
86  }
87 
88  /*
89  * Apply the list of modifications. This should not fail
90  * except on memory allocation error.
91  */
92  while ((vlm = fr_dlist_next(&update_state->vlm_head, vlm))) {
93  int ret;
94 
95  ret = map_list_mod_apply(request, vlm);
96  if (!fr_cond_assert(ret == 0)) {
97  TALLOC_FREE(frame->state);
98 
99  *p_result = RLM_MODULE_FAIL;
101  }
102  }
103 
104 done:
105  *p_result = RLM_MODULE_NOOP;
107 }
108 
109 /** Create a list of modifications to apply to one or more fr_pair_t lists
110  *
111  * @param[out] p_result The rcode indicating what the result
112  * of the operation was.
113  * @param[in] request The current request.
114  * @param[in] frame Current stack frame.
115  * @return
116  * - UNLANG_ACTION_CALCULATE_RESULT changes were applied.
117  * - UNLANG_ACTION_PUSHED_CHILD async execution of an expansion is required.
118  */
120 {
121  unlang_frame_state_update_t *update_state = talloc_get_type_abort(frame->state, unlang_frame_state_update_t);
122  map_t *map;
123 
124  /*
125  * Iterate over the maps producing a set of modifications to apply.
126  */
127  for (map = fr_dcursor_current(&update_state->maps);
128  map;
129  map = fr_dcursor_next(&update_state->maps)) {
130  repeatable_set(frame); /* Call us again when done */
131 
132  switch (update_state->state) {
134  update_state->state = UNLANG_UPDATE_MAP_EXPANDED_LHS;
135 
136  fr_assert(fr_value_box_list_empty(&update_state->lhs_result)); /* Should have been consumed */
137  fr_assert(fr_value_box_list_empty(&update_state->rhs_result)); /* Should have been consumed */
138 
139  switch (map->lhs->type) {
140  default:
141  break;
142 
143  case TMPL_TYPE_EXEC:
144  if (unlang_tmpl_push(update_state, &update_state->lhs_result,
145  request, map->lhs,
146  NULL) < 0) {
147  *p_result = RLM_MODULE_FAIL;
149  }
151 
152  case TMPL_TYPE_XLAT:
153  if (unlang_xlat_push(update_state, NULL, &update_state->lhs_result,
154  request, tmpl_xlat(map->lhs), false) < 0) {
155  *p_result = RLM_MODULE_FAIL;
157  }
159 
161  case TMPL_TYPE_REGEX:
165  fr_assert(0);
166  error:
167  TALLOC_FREE(frame->state);
168  repeatable_clear(frame);
169  *p_result = RLM_MODULE_FAIL;
170 
172  }
173  FALL_THROUGH;
174 
176  /*
177  * map_to_list_mod() already concatenates the LHS, so we don't need to do it here.
178  */
179  if (!map->rhs) goto next;
180 
181  update_state->state = UNLANG_UPDATE_MAP_EXPANDED_RHS;
182 
183  switch (map->rhs->type) {
184  default:
185  break;
186 
187  case TMPL_TYPE_EXEC:
188  if (unlang_tmpl_push(update_state, &update_state->rhs_result,
189  request, map->rhs, NULL) < 0) {
190  *p_result = RLM_MODULE_FAIL;
192  }
194 
195  case TMPL_TYPE_XLAT:
196  if (unlang_xlat_push(update_state, NULL, &update_state->rhs_result,
197  request, tmpl_xlat(map->rhs), false) < 0) {
198  *p_result = RLM_MODULE_FAIL;
200  }
202 
203  case TMPL_TYPE_REGEX:
208  fr_assert(0);
209  goto error;
210  }
211  FALL_THROUGH;
212 
214  {
215  vp_list_mod_t *new_mod;
216  /*
217  * Concat the top level results together
218  */
219  if (!fr_value_box_list_empty(&update_state->rhs_result) &&
220  (fr_value_box_list_concat_in_place(update_state,
221  fr_value_box_list_head(&update_state->rhs_result), &update_state->rhs_result, FR_TYPE_STRING,
223  SIZE_MAX) < 0)) {
224  RPEDEBUG("Failed concatenating RHS expansion results");
225  goto error;
226  }
227 
228  if (map_to_list_mod(update_state, &new_mod,
229  request, map,
230  &update_state->lhs_result, &update_state->rhs_result) < 0) goto error;
231  if (new_mod) fr_dlist_insert_tail(&update_state->vlm_head, new_mod);
232 
233  fr_value_box_list_talloc_free(&update_state->rhs_result);
234  }
235 
236  next:
237  update_state->state = UNLANG_UPDATE_MAP_INIT;
238  fr_value_box_list_talloc_free(&update_state->lhs_result);
239 
240  break;
241  }
242  }
243 
244  return list_mod_apply(p_result, request);
245 }
246 
247 
248 /** Execute an update block
249  *
250  * Update blocks execute in two phases, first there's an evaluation phase where
251  * each input map is evaluated, outputting one or more modification maps. The modification
252  * maps detail a change that should be made to a list in the current request.
253  * The request is not modified during this phase.
254  *
255  * The second phase applies those modification maps to the current request.
256  * This re-enables the atomic functionality of update blocks provided in v2.x.x.
257  * If one map fails in the evaluation phase, no more maps are processed, and the current
258  * result is discarded.
259  */
261 {
264  unlang_frame_state_update_t *update_state;
265 
266  /*
267  * Initialise the frame state
268  */
269  MEM(frame->state = update_state = talloc_zero_pooled_object(request->stack, unlang_frame_state_update_t,
270  (sizeof(map_t) +
271  (sizeof(tmpl_t) * 2) + 128),
272  g->num_children)); /* 128 is for string buffers */
273 
274  fr_dcursor_init(&update_state->maps, &gext->map.head);
275  fr_value_box_list_init(&update_state->lhs_result);
276  fr_value_box_list_init(&update_state->rhs_result);
277  fr_dlist_init(&update_state->vlm_head, vp_list_mod_t, entry);
278 
279  /*
280  * Call list_mod_create
281  */
283  return list_mod_create(p_result, request, frame);
284 }
285 
287 #ifdef WITH_VERIFY_PTR
288  unlang_stack_frame_t *frame
289 #else
291 #endif
292  )
293 {
294 #ifdef WITH_VERIFY_PTR
295  unlang_frame_state_map_proc_t *map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
296  VALUE_BOX_LIST_VERIFY(&map_proc_state->src_result);
297 #endif
299 }
300 
302 {
305 
306  map_proc_inst_t *inst = gext->proc_inst;
307  unlang_frame_state_map_proc_t *map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
308 
309  RDEBUG2("MAP %s \"%pM\"", inst->proc->name, &map_proc_state->src_result);
310 
311  VALUE_BOX_LIST_VERIFY(&map_proc_state->src_result);
313  return map_proc(p_result, request, gext->proc_inst, &map_proc_state->src_result);
314 }
315 
317 {
320  map_proc_inst_t *inst = gext->proc_inst;
321  unlang_frame_state_map_proc_t *map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
322 
323  /*
324  * Initialise the frame state
325  */
326  repeatable_set(frame);
327 
328  fr_value_box_list_init(&map_proc_state->src_result);
329  /*
330  * Set this BEFORE doing anything else, as we will be
331  * called again after unlang_xlat_push() returns.
332  */
333  frame->process = map_proc_apply;
334 
335  /*
336  * Expand the map source
337  */
338  if (inst->src) switch (inst->src->type) {
339  default:
340  {
341  fr_value_box_t *src_result = NULL;
342  if (tmpl_aexpand(frame->state, &src_result,
343  request, inst->src, NULL, NULL) < 0) {
344  REDEBUG("Failed expanding map src");
345  error:
346  *p_result = RLM_MODULE_FAIL;
348  }
349  fr_value_box_list_insert_head(&map_proc_state->src_result, src_result);
350  break;
351  }
352  case TMPL_TYPE_EXEC:
353  if (unlang_tmpl_push(map_proc_state, &map_proc_state->src_result,
354  request, inst->src, NULL) < 0) {
355  *p_result = RLM_MODULE_FAIL;
357  }
359 
360  case TMPL_TYPE_XLAT:
361  if (unlang_xlat_push(map_proc_state, NULL, &map_proc_state->src_result,
362  request, tmpl_xlat(inst->src), false) < 0) {
363  *p_result = RLM_MODULE_FAIL;
365  }
367 
368 
369  case TMPL_TYPE_REGEX:
374  fr_assert(0);
375  goto error;
376  }
377 
378  return map_proc_apply(p_result, request, frame);
379 }
380 
381 void unlang_map_init(void)
382 {
384  &(unlang_op_t){
385  .name = "update",
386  .interpret = unlang_update_state_init,
387  .debug_braces = true
388  });
389 
391  &(unlang_op_t){
392  .name = "map",
393  .rcode_set = true,
394  .interpret = unlang_map_state_init,
395  .frame_state_size = sizeof(unlang_frame_state_map_proc_t),
396  .frame_state_type = "unlang_frame_state_map_proc_t",
397  });
398 }
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:481
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition: build.h:320
#define UNUSED
Definition: build.h:313
#define fr_dcursor_init(_cursor, _head)
Initialise a cursor.
Definition: dcursor.h:732
next
Definition: dcursor.h:178
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition: dcursor.h:288
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 fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition: dlist.h:260
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
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
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:119
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:286
fr_value_box_list_t lhs_result
Result of expanding the LHS.
Definition: map.c:51
unlang_update_state_t state
What we're currently doing.
Definition: map.c:54
static unlang_action_t map_proc_apply(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: map.c:301
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:260
void unlang_map_init(void)
Definition: map.c:381
static unlang_action_t unlang_map_state_init(rlm_rcode_t *p_result, request_t *request, unlang_stack_frame_t *frame)
Definition: map.c:316
fr_dlist_head_t vlm_head
Head of list of VP List Mod.
Definition: map.c:49
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:73
unlang_update_state_t
map and unlang integration.
Definition: map.c:37
@ UNLANG_UPDATE_MAP_EXPANDED_RHS
Expand the RHS xlat or exec (if needed).
Definition: map.c:40
@ UNLANG_UPDATE_MAP_INIT
Start processing a map.
Definition: map.c:38
@ UNLANG_UPDATE_MAP_EXPANDED_LHS
Expand the LHS xlat or exec (if needed).
Definition: map.c:39
fr_dcursor_t maps
Cursor of maps to evaluate.
Definition: map.c:47
fr_value_box_list_t rhs_result
Result of expanding the RHS.
Definition: map.c:52
fr_value_box_list_t src_result
Result of expanding the map source.
Definition: map.c:61
State of a map block.
Definition: map.c:60
State of an update block.
Definition: map.c:46
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:922
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:251
A list modification.
Definition: map.h:99
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_proc_inst_t * proc_inst
Definition: map_priv.h:35
map_list_t map
Head of the map list.
Definition: map_priv.h:34
unlang_action_t map_proc(rlm_rcode_t *p_result, request_t *request, map_proc_inst_t const *inst, fr_value_box_list_t *result)
Evaluate a set of maps using the specified map processor.
Definition: map_proc.c:247
Map processor instance.
Definition: map_proc_priv.h:53
@ FR_TYPE_STRING
String of printable characters.
Definition: merged_model.c:83
static bool done
Definition: radclient.c:80
#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:941
@ TMPL_TYPE_REGEX_UNCOMPILED
Regex where compilation is possible but hasn't been performed yet.
Definition: tmpl.h:162
@ TMPL_TYPE_XLAT
Pre-parsed xlat expansion.
Definition: tmpl.h:150
@ TMPL_TYPE_EXEC
Callout to an external script or program.
Definition: tmpl.h:154
@ TMPL_TYPE_REGEX_XLAT_UNRESOLVED
A regular expression with unresolved xlat functions or attribute references.
Definition: tmpl.h:201
@ TMPL_TYPE_REGEX
Compiled (and possibly JIT'd) regular expression.
Definition: tmpl.h:158
@ TMPL_TYPE_XLAT_UNRESOLVED
A xlat expansion with unresolved xlat functions or attribute references.
Definition: tmpl.h:197
@ TMPL_TYPE_REGEX_XLAT
A regex containing xlat expansions.
Definition: tmpl.h:166
#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:1070
fr_assert(0)
MEM(pair_append_request(&vp, attr_eap_aka_sim_identity) >=0)
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:259
VALUE_BOX_LIST_VERIFY(list)
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:274
static void repeatable_clear(unlang_stack_frame_t *frame)
Definition: unlang_priv.h:351
void * state
Stack frame specialisations.
Definition: unlang_priv.h:296
static unlang_group_t * unlang_generic_to_group(unlang_t const *p)
Definition: unlang_priv.h:531
@ 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.
Definition: unlang_priv.h:518
unlang_t const * instruction
The unlang node we're evaluating.
Definition: unlang_priv.h:281
static void repeatable_set(unlang_stack_frame_t *frame)
Definition: unlang_priv.h:345
unlang_process_t process
function to call for interpreting this stack frame
Definition: unlang_priv.h:284
Generic representation of a grouping.
Definition: unlang_priv.h:145
An unlang operation.
Definition: unlang_priv.h:204
Our interpreter stack, as distinct from the C stack.
Definition: unlang_priv.h:280
An unlang stack associated with a request.
Definition: unlang_priv.h:314
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:5777
@ FR_VALUE_BOX_LIST_FREE
Definition: value.h:221