-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
map.c
351 lines (294 loc) · 10 KB
/
map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
*
* @brief map and unlang integration.
* @brief Unlang "map" keyword evaluation.
*
* @ingroup AVP
*
* @copyright 2018 The FreeRADIUS server project
* @copyright 2018 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
*/
RCSID("$Id$")
#include <freeradius-devel/server/base.h>
#include <freeradius-devel/server/rad_assert.h>
#include <freeradius-devel/server/map.h>
#include <freeradius-devel/server/xlat.h>
#include <freeradius-devel/server/map_proc_priv.h>
#include "unlang_priv.h"
typedef enum {
UNLANG_UPDATE_MAP_INIT = 0, //!< Start processing a map.
UNLANG_UPDATE_MAP_EXPANDED_LHS, //!< Expand the LHS xlat or exec (if needed).
UNLANG_UPDATE_MAP_EXPANDED_RHS //!< Expand the RHS xlat or exec (if needed).
} unlang_update_state_t;
/** State of an update block
*
*/
typedef struct {
fr_cursor_t maps; //!< Cursor of maps to evaluate.
vp_list_mod_t **vlm_next;
vp_list_mod_t *vlm_head; //!< First VP List Mod.
fr_value_box_t *lhs_result; //!< Result of expanding the LHS
fr_value_box_t *rhs_result; //!< Result of expanding the RHS.
unlang_update_state_t state; //!< What we're currently doing.
} unlang_frame_state_update_t;
/** State of a map block
*
*/
typedef struct {
fr_value_box_t *src_result; //!< Result of expanding the map source.
} unlang_frame_state_map_proc_t;
static unlang_action_t unlang_update_apply(REQUEST *request, rlm_rcode_t *presult, int *priority)
{
unlang_stack_t *stack = request->stack;
unlang_stack_frame_t *frame = &stack->frame[stack->depth];
unlang_t *instruction = frame->instruction;
unlang_frame_state_update_t *update_state = frame->state;
vp_list_mod_t const *vlm;
/*
* No modifications...
*/
if (!update_state->vlm_head) {
RDEBUG2("Nothing to update");
goto done;
}
/*
* Apply the list of modifications. This should not fail
* except on memory allocation error.
*/
for (vlm = update_state->vlm_head;
vlm;
vlm = vlm->next) {
int ret;
ret = map_list_mod_apply(request, vlm);
if (!fr_cond_assert(ret == 0)) {
TALLOC_FREE(frame->state);
*presult = RLM_MODULE_FAIL;
*priority = instruction->actions[*presult];
return UNLANG_ACTION_CALCULATE_RESULT;
}
}
done:
*presult = RLM_MODULE_NOOP;
*priority = instruction->actions[*presult];
return UNLANG_ACTION_CALCULATE_RESULT;
}
static unlang_action_t unlang_update_create(REQUEST *request, rlm_rcode_t *presult, int *priority)
{
unlang_stack_t *stack = request->stack;
unlang_stack_frame_t *frame = &stack->frame[stack->depth];
unlang_t *instruction = frame->instruction;
unlang_frame_state_update_t *update_state = frame->state;
vp_map_t *map;
/*
* Iterate over the maps producing a set of modifications to apply.
*/
for (map = fr_cursor_current(&update_state->maps);
map;
map = fr_cursor_next(&update_state->maps)) {
switch (update_state->state) {
case UNLANG_UPDATE_MAP_INIT:
update_state->state = UNLANG_UPDATE_MAP_EXPANDED_LHS;
rad_assert(!update_state->lhs_result); /* Should have been consumed */
rad_assert(!update_state->rhs_result); /* Should have been consumed */
switch (map->lhs->type) {
default:
break;
case TMPL_TYPE_XLAT_STRUCT:
unlang_xlat_push(update_state, &update_state->lhs_result,
request, map->lhs->tmpl_xlat, false);
return UNLANG_ACTION_PUSHED_CHILD;
case TMPL_TYPE_REGEX:
case TMPL_TYPE_REGEX_STRUCT:
case TMPL_TYPE_XLAT:
rad_assert(0);
error:
TALLOC_FREE(frame->state);
*presult = RLM_MODULE_FAIL;
*priority = instruction->actions[*presult];
return UNLANG_ACTION_CALCULATE_RESULT;
}
/* FALL-THROUGH */
case UNLANG_UPDATE_MAP_EXPANDED_LHS:
/*
* Concat the top level results together
*/
if (update_state->lhs_result &&
(fr_value_box_list_concat(update_state, update_state->lhs_result, &update_state->lhs_result,
FR_TYPE_STRING, true) < 0)) {
RPEDEBUG("Failed concatenating LHS expansion results");
goto error;
}
if (!map->rhs) goto next;
update_state->state = UNLANG_UPDATE_MAP_EXPANDED_RHS;
switch (map->rhs->type) {
default:
break;
case TMPL_TYPE_XLAT_STRUCT:
unlang_xlat_push(update_state, &update_state->rhs_result,
request, map->rhs->tmpl_xlat, false);
return UNLANG_ACTION_PUSHED_CHILD;
case TMPL_TYPE_REGEX:
case TMPL_TYPE_REGEX_STRUCT:
case TMPL_TYPE_XLAT:
rad_assert(0);
goto error;
}
/* FALL-THROUGH */
case UNLANG_UPDATE_MAP_EXPANDED_RHS:
update_state->state = UNLANG_UPDATE_MAP_INIT;
/*
* Concat the top level results together
*/
if (update_state->rhs_result &&
(fr_value_box_list_concat(update_state, update_state->rhs_result, &update_state->rhs_result,
FR_TYPE_STRING, true) < 0)) {
RPEDEBUG("Failed concatenating RHS expansion results");
goto error;
}
if (map_to_list_mod(update_state, update_state->vlm_next,
request, map,
&update_state->lhs_result, &update_state->rhs_result) < 0) goto error;
talloc_list_free(&update_state->rhs_result);
next:
talloc_list_free(&update_state->lhs_result);
/*
* Wind to the end...
*/
while (*update_state->vlm_next) update_state->vlm_next = &(*(update_state->vlm_next))->next;
break;
}
};
frame->process = unlang_update_apply;
return unlang_update_apply(request, presult, priority);
}
/** Execute an update block
*
* Update blocks execute in two phases, first there's an evaluation phase where
* each input map is evaluated, outputting one or more modification maps. The modification
* maps detail a change that should be made to a list in the current request.
* The request is not modified during this phase.
*
* The second phase applies those modification maps to the current request.
* This re-enables the atomic functionality of update blocks provided in v2.x.x.
* If one map fails in the evaluation phase, no more maps are processed, and the current
* result is discarded.
*/
static unlang_action_t unlang_update(REQUEST *request, rlm_rcode_t *presult, int *priority)
{
unlang_stack_t *stack = request->stack;
unlang_stack_frame_t *frame = &stack->frame[stack->depth];
unlang_t *instruction = frame->instruction;
unlang_group_t *g = unlang_generic_to_group(instruction);
unlang_frame_state_update_t *update_state = frame->state;
/*
* Initialise the frame state
*/
#ifdef HAVE_TALLOC_POOLED_OBJECT
int cnt = 0;
for (map = g->map; map; map = map->next) cnt++;
MEM(frame->state = update_state = talloc_pooled_object(stack, unlang_frame_state_update_t,
(sizeof(vp_map_t) +
(sizeof(vp_tmpl_t) * 2) + 128),
cnt)); /* 128 is for string buffers */
#else
MEM(frame->state = update_state = talloc_zero(stack, unlang_frame_state_update_t));
#endif
fr_cursor_init(&update_state->maps, &g->map);
update_state->vlm_next = &update_state->vlm_head;
repeatable_set(frame);
frame->process = unlang_update_create;
return unlang_update_create(request, presult, priority);
}
static unlang_action_t unlang_map(REQUEST *request, rlm_rcode_t *presult, int *priority)
{
unlang_stack_t *stack = request->stack;
unlang_stack_frame_t *frame = &stack->frame[stack->depth];
unlang_t *instruction = frame->instruction;
unlang_group_t *g = unlang_generic_to_group(instruction);
map_proc_inst_t *inst = g->proc_inst;
unlang_frame_state_map_proc_t *map_proc_state;
/*
* Initialise the frame state
*/
if (!is_repeatable(frame)) {
MEM(frame->state = map_proc_state = talloc_zero(stack, unlang_frame_state_map_proc_t));
repeatable_set(frame);
/*
* Expand the map source
*/
if (inst->src) switch (inst->src->type) {
default:
if (tmpl_aexpand(frame->state, &map_proc_state->src_result,
request, inst->src, NULL, NULL) < 0) {
REDEBUG("Failed expanding map src");
error:
*presult = RLM_MODULE_FAIL;
*priority = instruction->actions[*presult];
return UNLANG_ACTION_CALCULATE_RESULT;
}
break;
case TMPL_TYPE_XLAT_STRUCT:
unlang_xlat_push(map_proc_state, &map_proc_state->src_result,
request, inst->src->tmpl_xlat, false);
return UNLANG_ACTION_PUSHED_CHILD;
case TMPL_TYPE_REGEX:
case TMPL_TYPE_REGEX_STRUCT:
case TMPL_TYPE_XLAT:
rad_assert(0);
goto error;
}
} else {
map_proc_state = talloc_get_type_abort(frame->state, unlang_frame_state_map_proc_t);
}
RDEBUG2("MAP %s \"%pM\"", inst->proc->name, map_proc_state->src_result);
/*
* FIXME - We don't yet support async LHS/RHS expansions for map procs
*/
#ifndef NDEBUG
if (map_proc_state->src_result) talloc_list_get_type_abort(map_proc_state->src_result, fr_value_box_t);
#endif
*presult = map_proc(request, g->proc_inst, &map_proc_state->src_result);
#ifndef NDEBUG
if (map_proc_state->src_result) talloc_list_get_type_abort(map_proc_state->src_result, fr_value_box_t);
#endif
return *presult == RLM_MODULE_YIELD ? UNLANG_ACTION_YIELD : UNLANG_ACTION_CALCULATE_RESULT;
}
void unlang_map_init(void)
{
/*
* For now, FILTER and UPDATE use the same processor.
*/
unlang_register(UNLANG_TYPE_FILTER,
&(unlang_op_t){
.name = "filter",
.func = unlang_update,
.debug_braces = true
});
unlang_register(UNLANG_TYPE_UPDATE,
&(unlang_op_t){
.name = "update",
.func = unlang_update,
.debug_braces = true
});
unlang_register(UNLANG_TYPE_MAP,
&(unlang_op_t){
.name = "map",
.func = unlang_map,
});
}