The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
call_env.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: ae1b4b76a3061bc268067b1beca269380b2a6f38 $
19 *
20 * @file unlang/call_env.c
21 * @brief Call environment parsing functions
22 *
23 * @copyright 2023 Network RADIUS SAS (legal@networkradius.com)
24 */
25
26RCSID("$Id: ae1b4b76a3061bc268067b1beca269380b2a6f38 $")
27
28#include <freeradius-devel/server/log.h>
29#include <freeradius-devel/server/cf_util.h>
30#include <freeradius-devel/server/tmpl.h>
31#include <freeradius-devel/server/section.h>
32#include <freeradius-devel/server/main_config.h>
33#include <freeradius-devel/unlang/tmpl.h>
34#include <freeradius-devel/unlang/function.h>
35#include <freeradius-devel/unlang/interpret.h>
36#include <freeradius-devel/unlang/call_env.h>
37
38#include <talloc.h>
39#include "call_env.h"
40
42 call_env_parsed_entry_t entry; //!< Entry in list of parsed call_env_parsers.
43
44 union {
45 tmpl_t const *tmpl; //!< Tmpl produced from parsing conf pair.
46 fr_value_box_t const *vb; //!< Value box produced from parsing conf pair.
47 void const *ptr; //!< Data produced from parsing conf pair.
49
50 size_t count; //!< Number of CONF_PAIRs found, matching the #call_env_parser_t.
51 size_t multi_index; //!< Array index for this instance.
52 call_env_parser_t const *rule; //!< Used to produce this.
53};
54FR_DLIST_FUNCS(call_env_parsed, call_env_parsed_t, entry)
55
56#if defined(DEBUG_CALL_ENV)
57# define CALL_ENV_DEBUG(_ci, fmt, ...) cf_log_debug(_ci, fmt, ##__VA_ARGS__)
58#else
59# define CALL_ENV_DEBUG(_ci, ...)
60#endif
61
62/** Parse the result of call_env tmpl expansion
63 */
64static inline CC_HINT(always_inline)
65call_env_result_t call_env_result(TALLOC_CTX *ctx, request_t *request, void *out,
66 void **tmpl_out, call_env_parsed_t const *env,
67 fr_value_box_list_t *tmpl_expanded)
68{
70
71 if (tmpl_out) *tmpl_out = UNCONST(tmpl_t *, env->data.tmpl);
72 if (call_env_parse_only(env->rule->flags)) return CALL_ENV_SUCCESS;
73
74 vb = fr_value_box_list_head(tmpl_expanded);
75 if (!vb) {
76 if (!call_env_nullable(env->rule->flags)) {
77 RPEDEBUG("Failed to evaluate required module option %s = %s", env->rule->name, env->data.tmpl->name);
78 return CALL_ENV_MISSING;
79 }
80 return CALL_ENV_SUCCESS;
81 }
82
83 /*
84 * Concatenate multiple boxes if needed
85 */
86 if ((call_env_concat(env->rule->flags) || call_env_attribute(env->rule->flags)) &&
87 (env->rule->pair.cast_type != FR_TYPE_VOID) &&
88 fr_value_box_list_concat_in_place(vb, vb, tmpl_expanded, env->rule->pair.cast_type,
89 FR_VALUE_BOX_LIST_FREE, true, SIZE_MAX) < 0 ) {
90 RPEDEBUG("Failed concatenating values for %s", env->rule->name);
91 return CALL_ENV_INVALID;
92 }
93
94 if (call_env_single(env->rule->flags) && (fr_value_box_list_num_elements(tmpl_expanded) > 1)) {
95 RPEDEBUG("%u values found for %s. Only one is allowed",
96 fr_value_box_list_num_elements(tmpl_expanded), env->rule->name);
97 return CALL_ENV_INVALID;
98 }
99
100 while ((vb = fr_value_box_list_pop_head(tmpl_expanded))) {
101 switch (env->rule->pair.type) {
104 break;
105
107 if (!fr_value_box_list_initialised((fr_value_box_list_t *)out)) fr_value_box_list_init((fr_value_box_list_t *)out);
108 fr_value_box_list_insert_tail((fr_value_box_list_t *)out, vb);
109 break;
110
111 default:
112 fr_assert(0);
113 break;
114 }
115 }
116
117 return CALL_ENV_SUCCESS;
118}
119
120/** Context to keep track of expansion of call environments
121 *
122 */
123typedef struct {
124 call_env_result_t *result; //!< Where to write the return code of callenv expansion.
125 call_env_t const *call_env; //!< Call env being expanded.
126 call_env_parsed_t const *last_expanded; //!< The last expanded tmpl.
127 fr_value_box_list_t tmpl_expanded; //!< List to write value boxes to as tmpls are expanded.
128 void **data; //!< Final destination structure for value boxes.
130
131static unlang_action_t call_env_expand_repeat(rlm_rcode_t *p_result, int *priority, request_t *request, void *uctx);
132
133/** Start the expansion of a call environment tmpl.
134 *
135 */
136static unlang_action_t call_env_expand_start(UNUSED rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
137{
138 call_env_rctx_t *call_env_rctx = talloc_get_type_abort(uctx, call_env_rctx_t);
139 TALLOC_CTX *ctx;
140 call_env_parsed_t const *env = NULL;
141 void **out;
142
143 while ((call_env_rctx->last_expanded = call_env_parsed_next(&call_env_rctx->call_env->parsed, call_env_rctx->last_expanded))) {
144 env = call_env_rctx->last_expanded;
145 fr_assert(env != NULL);
146
147 /*
148 * Subsections are expanded during parsing to produce a list of
149 * call_env_parsed_t. They are not expanded at runtime.
150 */
151 fr_assert_msg(call_env_is_subsection(env->rule->flags) == false, "Subsections cannot be expanded at runtime");
152
153 /*
154 * If there's an offset to copy the output to, do that.
155 * We may also need to expand the tmpl_t and write out the result
156 * to the pair offset.
157 */
158 if (env->rule->pair.parsed.offset >= 0) {
159 /*
160 * If we only need the tmpl or data, just set the pointer and move the next.
161 */
162 out = (void **)((uint8_t *)*call_env_rctx->data + env->rule->pair.parsed.offset);
163
164 /*
165 * For multi pair options, the pointers need to go into a new array.
166 * When processing the first expansion, allocate the array, and for
167 * all expansions adjust the `out` pointer to write to.
168 */
169 if (call_env_multi(env->rule->flags)) {
170 void **array;
171 if (env->multi_index == 0) {
172 /*
173 * Coverity thinks talloc_zero_array being called with the type `void *`
174 * is a size mismatch. This works round the false positive.
175 */
176 MEM(array = _talloc_zero_array((*call_env_rctx->data), sizeof(uint8_t *),
177 env->count, "void *"));
178 *out = array;
179 }
180 array = (void **)(*out);
181 out = (void **)((uint8_t *)array + sizeof(void *) * env->multi_index);
182 }
183
184 switch (env->rule->pair.parsed.type) {
186 *out = UNCONST(tmpl_t *, env->data.tmpl);
187 break;
188
190 *out = UNCONST(fr_value_box_t *, env->data.vb);
191 continue; /* Can't evaluate these */
192
194 *out = UNCONST(void *, env->data.ptr);
195 continue; /* Can't evaluate these */
196 }
197 }
198
199 /*
200 * If this is not parse_only, we need to expand the tmpl.
201 */
202 if ((env->rule->pair.parsed.type == CALL_ENV_PARSE_TYPE_TMPL) && !call_env_parse_only(env->rule->flags)) break;
203 }
204
205 if (!call_env_rctx->last_expanded) { /* No more! */
206 if (call_env_rctx->result) *call_env_rctx->result = CALL_ENV_SUCCESS;
208 }
209
210 ctx = *call_env_rctx->data;
211
212 fr_assert(env != NULL);
213
214 /*
215 * Multi pair options should allocate boxes in the context of the array
216 */
217 if (call_env_multi(env->rule->flags)) {
218 out = (void **)((uint8_t *)(*call_env_rctx->data) + env->rule->pair.offset);
219
220 /*
221 * For multi pair options, allocate the array before expanding the first entry.
222 */
223 if (env->multi_index == 0) {
224 void *array;
225 MEM(array = _talloc_zero_array((*call_env_rctx->data), env->rule->pair.size,
226 env->count, env->rule->pair.type_name));
227 *out = array;
228 }
229 ctx = *out;
230 }
231
232 if (unlang_tmpl_push(ctx, &call_env_rctx->tmpl_expanded, request, call_env_rctx->last_expanded->data.tmpl,
233 NULL) < 0) return UNLANG_ACTION_FAIL;
234
236}
237
238/** Extract expanded call environment tmpl and store in env_data
239 *
240 * If there are more tmpls to expand, push the next expansion.
241 */
243 request_t *request, void *uctx)
244{
245 void *out = NULL, *tmpl_out = NULL;
246 call_env_rctx_t *call_env_rctx = talloc_get_type_abort(uctx, call_env_rctx_t);
247 call_env_parsed_t const *env;
248 call_env_result_t result;
249
250 env = call_env_rctx->last_expanded;
251 if (!env) return UNLANG_ACTION_CALCULATE_RESULT;
252
253 if (call_env_parse_only(env->rule->flags)) goto parse_only;
254 /*
255 * Find the location of the output
256 */
257 out = ((uint8_t*)(*call_env_rctx->data)) + env->rule->pair.offset;
258
259 /*
260 * If this is a multi pair option, the output is an array.
261 * Find the correct offset in the array
262 */
263 if (call_env_multi(env->rule->flags)) {
264 void *array = *(void **)out;
265 out = ((uint8_t *)array) + env->rule->pair.size * env->multi_index;
266 }
267
268parse_only:
269 if (env->rule->pair.parsed.offset >= 0) tmpl_out = ((uint8_t *)*call_env_rctx->data) + env->rule->pair.parsed.offset;
270
271 /* coverity[var_deref_model] */
272 result = call_env_result(*call_env_rctx->data, request, out, tmpl_out, env, &call_env_rctx->tmpl_expanded);
273 if (result != CALL_ENV_SUCCESS) {
274 if (call_env_rctx->result) *call_env_rctx->result = result;
275 return UNLANG_ACTION_FAIL;
276 }
277
278 if (!call_env_parsed_next(&call_env_rctx->call_env->parsed, env)) {
279 if (call_env_rctx->result) *call_env_rctx->result = CALL_ENV_SUCCESS;
281 }
282
284 call_env_rctx);
285}
286
287/** Initialise the expansion of a call environment
288 *
289 * @param[in] ctx in which to allocate destination structure for resulting value boxes.
290 * @param[in] request Current request.
291 * @param[out] env_result Where to write the result of the callenv expansion. May be NULL
292 * @param[in,out] env_data Where the destination structure should be created.
293 * @param[in] call_env Call environment being expanded.
294 */
295unlang_action_t call_env_expand(TALLOC_CTX *ctx, request_t *request, call_env_result_t *env_result, void **env_data,
296 call_env_t const *call_env)
297{
298 call_env_rctx_t *call_env_rctx;
299
300 MEM(call_env_rctx = talloc_zero(ctx, call_env_rctx_t));
301 MEM(*env_data = talloc_zero_array(ctx, uint8_t, call_env->method->inst_size));
302 talloc_set_name_const(*env_data, call_env->method->inst_type);
303 call_env_rctx->result = env_result;
304 if (env_result) *env_result = CALL_ENV_INVALID; /* Make sure we ran to completion*/
305 call_env_rctx->data = env_data;
306 call_env_rctx->call_env = call_env;
307 fr_value_box_list_init(&call_env_rctx->tmpl_expanded);
308
310 call_env_rctx);
311}
312
313/** Allocates a new call env parsed struct
314 *
315 */
316static inline CC_HINT(always_inline)
318{
319 call_env_parsed_t *call_env_parsed;
320
321 MEM(call_env_parsed = talloc_zero(ctx, call_env_parsed_t));
322 call_env_parsed->rule = rule;
323 call_env_parsed->count = 1;
324 call_env_parsed->multi_index = 0;
325
326 return call_env_parsed;
327}
328
329static inline CC_HINT(always_inline)
330int call_env_parsed_valid(call_env_parsed_t const *parsed, CONF_ITEM const *ci, call_env_parser_t const *rule)
331{
332 tmpl_t const *tmpl;
333
334 if (rule->pair.parsed.type != CALL_ENV_PARSE_TYPE_TMPL) return 0;
335
336 tmpl = parsed->data.tmpl;
337 switch (tmpl->type) {
338 /*
339 * These can't be created from a call_env flag which is marked as an attribute.
340 */
341 case TMPL_TYPE_DATA:
342 case TMPL_TYPE_EXEC:
343 case TMPL_TYPE_XLAT:
344 fr_assert(!call_env_attribute(rule->flags));
345 break;
346
347 /*
348 * This can be created from multiple types of flags, not just an attribute one.
349 */
350 case TMPL_TYPE_ATTR:
351 break;
352
353 default:
354 cf_log_err(ci, "'%s' expands to invalid tmpl type %s", tmpl->name,
355 tmpl_type_to_str(tmpl->type));
356 return -1;
357 }
358
359 return 0;
360}
361
362/** Standard function we use for parsing call env pairs
363 *
364 * @note This is called where no custom pair parsing function is provided, but may be called by custom functions to avoid
365 * duplicating the standard parsing code.
366 *
367 * @param[in] ctx to allocate any data in.
368 * @param[out] out Where to write the result of parsing.
369 * @param[in] t_rules we're parsing attributes with. Contains the default dictionary and nested 'caller' tmpl_rules_t.
370 * @param[in] ci The #CONF_SECTION or #CONF_PAIR to parse.
371 * @param[in] cec information about the call.
372 * @param[in] rule Parse rules - How the #CONF_PAIR or #CONF_SECTION should be converted.
373 * @return
374 * - 0 on success.
375 * - -1 on failure.
376 */
377int call_env_parse_pair(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci,
378 UNUSED call_env_ctx_t const *cec, call_env_parser_t const *rule)
379{
380 CONF_PAIR const *to_parse = cf_item_to_pair(ci);
381 tmpl_t *parsed_tmpl;
382 fr_token_t quote = cf_pair_value_quote(to_parse);
383
384 /*
385 * If it's marked as containing an attribute reference,
386 * then always parse it as an attribute reference.
387 */
388 if (call_env_attribute(rule->flags) ||
389 ((quote == T_BARE_WORD) && call_env_bare_word_attribute(rule->flags))) {
390 if (tmpl_afrom_attr_str(ctx, NULL, &parsed_tmpl, cf_pair_value(to_parse), t_rules) <= 0) {
391 return -1;
392 }
393 } else {
394 if (tmpl_afrom_substr(ctx, &parsed_tmpl,
397 t_rules) < 0) {
398 return -1;
399 }
400 }
401 *(void **)out = parsed_tmpl;
402
403 /*
404 * All attributes and functions should be resolved at this point
405 */
406 return tmpl_resolve(parsed_tmpl, NULL);
407}
408
409/** Parse per call env
410 *
411 * Used for config options which must be parsed in the context in which
412 * the module is being called.
413 *
414 * @param[in] ctx To allocate parsed environment in.
415 * @param[out] parsed Where to write parsed environment.
416 * @param[in] name Module name for error messages.
417 * @param[in] t_rules controlling how the call env is parsed.
418 * @param[in] cs Module config.
419 * @param[in] cec information about the call.
420 * @param[in] rule to parse.
421 * @return
422 * - 0 on success;
423 * - <0 on failure;
424 */
425static int call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *parsed, char const *name, tmpl_rules_t const *t_rules,
426 CONF_SECTION const *cs,
427 call_env_ctx_t const *cec, call_env_parser_t const *rule) {
428 CONF_PAIR const *cp, *next;
429 call_env_parsed_t *call_env_parsed = NULL;
430 ssize_t count, multi_index;
431 call_env_parser_t const *rule_p = rule;
432
433 while (rule_p->name) {
434 CALL_ENV_DEBUG(cs, "%s: Parsing call env data for %s", name, section_name_str(rule_p->name));
435
436 if (call_env_is_subsection(rule_p->flags)) {
437 CONF_SECTION const *subcs;
438 subcs = cf_section_find(cs, rule_p->name, rule_p->section.name2);
439 if (!subcs && !call_env_parse_missing(rule_p->flags)) {
440 if (!call_env_required(rule_p->flags)) goto next;
441 cf_log_err(cs, "Module %s missing required section \"%s\"", name, rule_p->name);
442 return -1;
443 }
444
445 /*
446 * Hand off to custom parsing function if there is one...
447 */
448 if (rule_p->section.func) {
449 /*
450 * Record our position so we can process any new entries
451 * after the callback returns.
452 */
453 call_env_parsed_t *last = call_env_parsed_tail(parsed);
454
455 CALL_ENV_DEBUG(cs, "%s: Calling subsection callback %p", name, rule_p->section.func);
456
457 if (rule_p->section.func(ctx, parsed, t_rules, cf_section_to_item(subcs), cec, rule_p) < 0) {
458 cf_log_perr(cs, "Failed parsing configuration section %s",
459 rule_p->name == CF_IDENT_ANY ? cf_section_name(cs) : rule_p->name);
460 talloc_free(call_env_parsed);
461 return -1;
462 }
463
464 CALL_ENV_DEBUG(subcs, "%s: Callback returned %u parsed call envs", name,
465 call_env_parsed_num_elements(parsed));
466
467 /*
468 * We _could_ fix up count and multi_index on behalf of
469 * the callback, but there's no guarantee that all call_env_parsed_t
470 * are related to each other, so we don't.
471 */
472 call_env_parsed = last;
473 while ((call_env_parsed = call_env_parsed_next(parsed, call_env_parsed))) {
474 CALL_ENV_DEBUG(subcs, "%s: Checking parsed env", name, rule_p->section.func);
475 if (call_env_parsed_valid(call_env_parsed, cf_section_to_item(subcs), rule_p) < 0) {
476 cf_log_err(cf_section_to_item(subcs), "Invalid data produced by %s",
477 rule_p->name == CF_IDENT_ANY ? cf_section_name(cs) : rule_p->name);
478 return -1;
479 }
480 }
481 goto next;
482 }
483
484 if (call_env_parse(ctx, parsed, name, t_rules, subcs, cec, rule_p->section.subcs) < 0) {
485 CALL_ENV_DEBUG(cs, "%s: Recursive call failed", name);
486 return -1;
487 }
488 goto next;
489 }
490
491 cp = cf_pair_find(cs, rule_p->name);
492
493 if (!cp && !rule_p->pair.dflt) {
494 if (!call_env_required(rule_p->flags)) goto next;
495
496 cf_log_err(cs, "Missing required config item '%s'", rule_p->name);
497 return -1;
498 }
499
500 /*
501 * Check for additional conf pairs and error
502 * if there is one and multi is not allowed.
503 */
504 if (!call_env_multi(rule_p->flags) && ((next = cf_pair_find_next(cs, cp, rule_p->name)))) {
505 cf_log_err(cf_pair_to_item(next), "Invalid duplicate configuration item '%s'", rule_p->name);
506 return -1;
507 }
508
509 count = cf_pair_count(cs, rule_p->name);
510 if (count == 0) count = 1;
511
512 for (multi_index = 0; multi_index < count; multi_index++) {
513 CONF_PAIR *tmp_cp = NULL;
514 CONF_PAIR const *to_parse;
515 tmpl_rules_t our_rules = {};
516 fr_type_t type = rule_p->pair.cast_type;
517 call_env_parse_pair_t func = rule_p->pair.func ? rule_p->pair.func : call_env_parse_pair;
518
519 if (t_rules) {
520 our_rules.parent = t_rules->parent;
521 our_rules.attr.dict_def = t_rules->attr.dict_def;
522 our_rules.escape = rule_p->pair.escape; /* Escape rules will now get embedded in the tmpl_t and used at evaluation */
523 }
524
526 our_rules.cast = ((type == FR_TYPE_VOID) ? FR_TYPE_NULL : type);
527 our_rules.literals_safe_for = rule_p->pair.literals_safe_for;
528
529 call_env_parsed = call_env_parsed_alloc(ctx, rule_p);
530 call_env_parsed->count = count;
531 call_env_parsed->multi_index = multi_index;
532
533 /*
534 * With the conf_parser code we can add default pairs
535 * if they don't exist, but as the same CONF_SECTIONs
536 * are evaluated multiple times for each module call
537 * we can't do that here.
538 */
539 if (cp) {
540 if (call_env_force_quote(rule_p->flags)) {
541 to_parse = tmp_cp = cf_pair_alloc(NULL,
544 call_env_force_quote(rule_p->flags) ? rule_p->pair.dflt_quote : cf_pair_value_quote(cp));
545 } else {
546 to_parse = cp;
547 }
548 } else {
549 to_parse = tmp_cp = cf_pair_alloc(NULL,
550 rule_p->name, rule_p->pair.dflt, T_OP_EQ,
551 T_BARE_WORD, rule_p->pair.dflt_quote);
552 }
553
554 /*
555 * The parsing function can either produce a tmpl_t as tmpl_afrom_substr
556 * would, or produce a custom structure, which will be copied into the
557 * result structure.
558 */
559 if (unlikely(func(ctx, &call_env_parsed->data, &our_rules, cf_pair_to_item(to_parse), cec, rule_p) < 0)) {
560 error:
561 cf_log_perr(to_parse, "Failed to parse configuration item '%s = %s'", rule_p->name, cf_pair_value(to_parse));
562 talloc_free(call_env_parsed);
563 talloc_free(tmp_cp);
564 return -1;
565 }
566 if (!call_env_parsed->data.ptr) {
567 talloc_free(call_env_parsed);
568 goto next_pair;
569 }
570
571 /*
572 * Ensure only valid data is produced.
573 */
574 if (call_env_parsed_valid(call_env_parsed, cf_pair_to_item(to_parse), rule_p) < 0) goto error;
575
576 call_env_parsed_insert_tail(parsed, call_env_parsed);
577 next_pair:
578 talloc_free(tmp_cp);
579 cp = cf_pair_find_next(cs, cp, rule_p->name);
580 }
581 next:
582 rule_p++;
583 }
584
585 CALL_ENV_DEBUG(cs, "Returning afer processing %u rules", (unsigned int)(rule_p - rule));
586
587 return 0;
588}
589
590/** Perform a quick assessment of how many parsed call env will be produced.
591 *
592 * @param[in,out] names_len Where to write the sum of bytes required to represent
593 * the strings which will be parsed as tmpls. This is required
594 * to pre-allocate space for the tmpl name buffers.
595 * @param[in] cs Conf section to search for pairs.
596 * @param[in] call_env to parse.
597 * @return Number of parsed_call_env expected to be required.
598 */
599static size_t call_env_count(size_t *names_len, CONF_SECTION const *cs, call_env_parser_t const *call_env)
600{
601 size_t pair_count, tmpl_count = 0;
602 CONF_PAIR const *cp;
603
604 *names_len = 0;
605
606 while (call_env->name) {
607 if (call_env_is_subsection(call_env->flags)) {
608 CONF_SECTION const *subcs;
609 subcs = cf_section_find(cs, call_env->name, call_env->section.name2);
610 if (!subcs) goto next;
611
612 /*
613 * May only be a callback...
614 */
615 if (call_env->section.subcs) tmpl_count += call_env_count(names_len, subcs, call_env->section.subcs);
616 goto next;
617 }
618 pair_count = 0;
619 cp = NULL;
620 while ((cp = cf_pair_find_next(cs, cp, call_env->name))) {
621 pair_count++;
622 *names_len += talloc_array_length(cf_pair_value(cp));
623 }
624 if (!pair_count && call_env->pair.dflt) {
625 pair_count = 1;
626 *names_len += strlen(call_env->pair.dflt);
627 }
628 tmpl_count += pair_count;
629 next:
630 call_env++;
631 }
632
633 return tmpl_count;
634}
635
636/** Allocate a new call_env_parsed_t structure and add it to the list of parsed call envs
637 *
638 * @note tmpl_t and void * should be allocated in the context of the call_env_parsed_t
639 *
640 * @param[in] ctx to allocate the new call_env_parsed_t in.
641 * @param[out] head to add the new call_env_parsed_t to.
642 * @param[in] rule to base call_env_parsed_t around. MUST NOT BE THE RULE PASSED TO THE CALLBACK.
643 * The rule passed to the callback describes how to parse a subsection, but the
644 * subsection callback is adding rules describing how to parse its children.
645 * @return The new call_env_parsed_t.
646 */
647call_env_parsed_t *call_env_parsed_add(TALLOC_CTX *ctx, call_env_parsed_head_t *head, call_env_parser_t const *rule)
648{
649 call_env_parsed_t *call_env_parsed;
650 call_env_parser_t *our_rules;
651
652 fr_assert_msg(call_env_is_subsection(rule->flags) == false, "Rules added by subsection callbacks cannot be subsections themselves");
653
654 MEM(call_env_parsed = call_env_parsed_alloc(ctx, rule));
655
656 /*
657 * Copy the rule the callback provided, there's no guarantee
658 * it's not stack allocated, or in some way ephemeral.
659 */
660 MEM(our_rules = talloc(call_env_parsed, call_env_parser_t));
661 memcpy(our_rules, rule, sizeof(*our_rules));
662 call_env_parsed->rule = our_rules;
663 call_env_parsed_insert_tail(head, call_env_parsed);
664
665 return call_env_parsed;
666}
667
668/** Assign a tmpl to a call_env_parsed_t
669 *
670 * @note Intended to be used by subsection callbacks to add a tmpl to be
671 * evaluated during the call.
672 *
673 * @param[in] parsed to assign the tmpl to.
674 * @param[in] tmpl to assign.
675 */
677{
678 fr_assert_msg(parsed->rule->pair.parsed.type == CALL_ENV_PARSE_TYPE_TMPL, "Rule must indicate parsed output is a tmpl_t");
679 parsed->data.tmpl = tmpl;
680}
681
682/** Assign a value box to a call_env_parsed_t
683 *
684 * @note Intended to be used by subsection callbacks to set a static boxed
685 * value to be written out to the result structure.
686 *
687 * @param[in] parsed to assign the tmpl to.
688 * @param[in] vb to assign.
689 */
691{
692 fr_assert_msg(parsed->rule->pair.parsed.type == CALL_ENV_PARSE_TYPE_VALUE_BOX, "Rule must indicate parsed output is a value box");
693 parsed->data.vb = vb;
694}
695
696/** Assign data to a call_env_parsed_t
697 *
698 * @note Intended to be used by subsection callbacks to set arbitrary data
699 * to be written out to the result structure.
700 *
701 * @param[in] parsed to assign the tmpl to.
702 * @param[in] data to assign.
703 */
705{
706 fr_assert_msg(parsed->rule->pair.parsed.type == CALL_ENV_PARSE_TYPE_VOID, "Rule must indicate parsed output is a void *");
707 parsed->data.ptr = data;
708}
709
710/** Assign a count and index to a call_env_parsed_t
711 *
712 * @note Intended to be used by subsection callbacks to indicate related
713 * call_env_parsed_t.
714 *
715 * @param[in] parsed to modify metadata of.
716 * @param[in] count to assign.
717 * @param[in] index to assign.
718 */
719void call_env_parsed_set_multi_index(call_env_parsed_t *parsed, size_t count, size_t index)
720{
721 fr_assert_msg(call_env_multi(parsed->rule->flags), "Rule must indicate parsed output is a multi pair");
722 parsed->multi_index = index;
723 parsed->count = count;
724}
725
726/** Remove a call_env_parsed_t from the list of parsed call envs
727 *
728 * @note Intended to be used by subsection callbacks to remove a call_env_parsed_t
729 * from the list of parsed call envs (typically on error).
730 *
731 * @param[in] parsed to remove parsed data from.
732 * @param[in] ptr to remove.
733 */
734void call_env_parsed_free(call_env_parsed_head_t *parsed, call_env_parsed_t *ptr)
735{
736 call_env_parsed_remove(parsed, ptr);
737 talloc_free(ptr);
738}
739
740/** Given a call_env_method, parse all call_env_pair_t in the context of a specific call to an xlat or module method
741 *
742 * @param[in] ctx to allocate the call_env_t in.
743 * @param[in] name Module name for error messages.
744 * @param[in] call_env_method containing the call_env_pair_t to evaluate against the specified CONF_SECTION.
745 * @param[in] t_rules that control how call_env_pair_t are parsed.
746 * @param[in] cs to parse in the context of the call.
747 * @param[in] cec information about how the call is being made.
748 * @return
749 * - A new call_env_t on success.
750 * - NULL on failure.
751 */
752call_env_t *call_env_alloc(TALLOC_CTX *ctx, char const *name, call_env_method_t const *call_env_method,
753 tmpl_rules_t const *t_rules, CONF_SECTION *cs, call_env_ctx_t const *cec)
754{
755 unsigned int count;
756 size_t names_len;
757 call_env_t *call_env;
758
759 /*
760 * Only used if caller doesn't use a more specific assert
761 */
762 fr_assert_msg(call_env_method->inst_size, "inst_size 0 for %s, method_env (%p)", name, call_env_method);
763
764 /*
765 * Firstly assess how many parsed env there will be and create a talloc pool to hold them.
766 * The pool size is a rough estimate based on each tmpl also allocating at least two children,
767 * for which we allow twice the length of the value to be parsed.
768 */
769 count = call_env_count(&names_len, cs, call_env_method->env);
770
771 /*
772 * Pre-allocated headers:
773 * 1 header for the call_env_pair_parsed_t, 1 header for the tmpl_t, 1 header for the name,
774 * one header for the value.
775 *
776 * Pre-allocated memory:
777 * ((sizeof(call_env_pair_parsed_t) + sizeof(tmpl_t)) * count) + (names of tmpls * 2)... Not sure what
778 * the * 2 is for, maybe for slop?
779 */
780 MEM(call_env = talloc_pooled_object(ctx, call_env_t, count * 4, (sizeof(call_env_parser_t) + sizeof(tmpl_t)) * count + names_len * 2));
781 call_env->method = call_env_method;
782 call_env_parsed_init(&call_env->parsed);
783 if (call_env_parse(call_env, &call_env->parsed, name, t_rules, cs, cec, call_env_method->env) < 0) {
784 talloc_free(call_env);
785 return NULL;
786 }
787
788 return call_env;
789}
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_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 UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:485
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
static unlang_action_t call_env_expand_repeat(rlm_rcode_t *p_result, int *priority, request_t *request, void *uctx)
call_env_parser_t const * rule
Used to produce this.
Definition call_env.c:52
size_t count
Number of CONF_PAIRs found, matching the call_env_parser_t.
Definition call_env.c:50
static unlang_action_t call_env_expand_start(UNUSED rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx)
Start the expansion of a call environment tmpl.
Definition call_env.c:136
call_env_parsed_t const * last_expanded
The last expanded tmpl.
Definition call_env.c:126
static call_env_result_t call_env_result(TALLOC_CTX *ctx, request_t *request, void *out, void **tmpl_out, call_env_parsed_t const *env, fr_value_box_list_t *tmpl_expanded)
Parse the result of call_env tmpl expansion.
Definition call_env.c:65
void call_env_parsed_free(call_env_parsed_head_t *parsed, call_env_parsed_t *ptr)
Remove a call_env_parsed_t from the list of parsed call envs.
Definition call_env.c:734
#define CALL_ENV_DEBUG(_ci,...)
Definition call_env.c:59
static int call_env_parsed_valid(call_env_parsed_t const *parsed, CONF_ITEM const *ci, call_env_parser_t const *rule)
Definition call_env.c:330
unlang_action_t call_env_expand(TALLOC_CTX *ctx, request_t *request, call_env_result_t *env_result, void **env_data, call_env_t const *call_env)
Initialise the expansion of a call environment.
Definition call_env.c:295
static call_env_parsed_t * call_env_parsed_alloc(TALLOC_CTX *ctx, call_env_parser_t const *rule)
Allocates a new call env parsed struct.
Definition call_env.c:317
call_env_parsed_t * call_env_parsed_add(TALLOC_CTX *ctx, call_env_parsed_head_t *head, call_env_parser_t const *rule)
Allocate a new call_env_parsed_t structure and add it to the list of parsed call envs.
Definition call_env.c:647
call_env_t const * call_env
Call env being expanded.
Definition call_env.c:125
static size_t call_env_count(size_t *names_len, CONF_SECTION const *cs, call_env_parser_t const *call_env)
Perform a quick assessment of how many parsed call env will be produced.
Definition call_env.c:599
size_t multi_index
Array index for this instance.
Definition call_env.c:51
void call_env_parsed_set_multi_index(call_env_parsed_t *parsed, size_t count, size_t index)
Assign a count and index to a call_env_parsed_t.
Definition call_env.c:719
call_env_parsed_entry_t entry
Entry in list of parsed call_env_parsers.
Definition call_env.c:42
call_env_result_t * result
Where to write the return code of callenv expansion.
Definition call_env.c:124
static int call_env_parse(TALLOC_CTX *ctx, call_env_parsed_head_t *parsed, char const *name, tmpl_rules_t const *t_rules, CONF_SECTION const *cs, call_env_ctx_t const *cec, call_env_parser_t const *rule)
Parse per call env.
Definition call_env.c:425
void ** data
Final destination structure for value boxes.
Definition call_env.c:128
fr_value_box_list_t tmpl_expanded
List to write value boxes to as tmpls are expanded.
Definition call_env.c:127
void call_env_parsed_set_data(call_env_parsed_t *parsed, void const *data)
Assign data to a call_env_parsed_t.
Definition call_env.c:704
union call_env_parsed_s::@99 data
call_env_t * call_env_alloc(TALLOC_CTX *ctx, char const *name, call_env_method_t const *call_env_method, tmpl_rules_t const *t_rules, CONF_SECTION *cs, call_env_ctx_t const *cec)
Given a call_env_method, parse all call_env_pair_t in the context of a specific call to an xlat or mo...
Definition call_env.c:752
void call_env_parsed_set_value(call_env_parsed_t *parsed, fr_value_box_t const *vb)
Assign a value box to a call_env_parsed_t.
Definition call_env.c:690
void call_env_parsed_set_tmpl(call_env_parsed_t *parsed, tmpl_t const *tmpl)
Assign a tmpl to a call_env_parsed_t.
Definition call_env.c:676
int call_env_parse_pair(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, UNUSED call_env_ctx_t const *cec, call_env_parser_t const *rule)
Standard function we use for parsing call env pairs.
Definition call_env.c:377
Context to keep track of expansion of call environments.
Definition call_env.c:123
Structures and functions for handling call environments.
#define call_env_parse_missing(_flags)
Definition call_env.h:132
call_env_flags_t flags
Flags controlling parser behaviour.
Definition call_env.h:182
#define call_env_is_subsection(_flags)
Definition call_env.h:130
#define call_env_required(_flags)
Definition call_env.h:114
char const * name
Of conf pair to pass to tmpl_tokenizer.
Definition call_env.h:181
#define call_env_nullable(_flags)
Definition call_env.h:122
#define call_env_multi(_flags)
Definition call_env.h:120
#define call_env_single(_flags)
Definition call_env.h:118
#define call_env_concat(_flags)
Definition call_env.h:116
@ CALL_ENV_RESULT_TYPE_VALUE_BOX_LIST
Output of the evaluation phase is a list of value boxes.
Definition call_env.h:69
@ CALL_ENV_RESULT_TYPE_VALUE_BOX
Output of the evaluation phase is a single value box.
Definition call_env.h:68
size_t inst_size
Size of per call env.
Definition call_env.h:245
#define call_env_attribute(_flags)
Definition call_env.h:128
call_env_result_t
Definition call_env.h:51
@ CALL_ENV_INVALID
Definition call_env.h:54
@ CALL_ENV_MISSING
Definition call_env.h:53
@ CALL_ENV_SUCCESS
Definition call_env.h:52
call_env_parser_t const * env
Parsing rules for call method env.
Definition call_env.h:247
char const * inst_type
Type of per call env.
Definition call_env.h:246
#define call_env_parse_only(_flags)
Definition call_env.h:126
#define call_env_force_quote(_flags)
Definition call_env.h:124
call_env_parsed_head_t parsed
The per call parsed call environment.
Definition call_env.h:253
@ CALL_ENV_PARSE_TYPE_VALUE_BOX
Output of the parsing phase is a single value box (static data).
Definition call_env.h:61
@ CALL_ENV_PARSE_TYPE_TMPL
Output of the parsing phase is a tmpl_t.
Definition call_env.h:60
@ CALL_ENV_PARSE_TYPE_VOID
Output of the parsing phase is undefined (a custom structure).
Definition call_env.h:62
#define call_env_bare_word_attribute(_flags)
Definition call_env.h:136
call_env_method_t const * method
The method this call env is for.
Definition call_env.h:254
int(* call_env_parse_pair_t)(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, call_env_ctx_t const *cec, call_env_parser_t const *rule)
Callback for performing custom parsing of a CONF_PAIR.
Definition call_env.h:151
Per method call config.
Definition call_env.h:180
Structure containing both a talloc pool, a list of parsed call_env_pairs.
Definition call_env.h:252
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
fr_token_t cf_pair_attr_quote(CONF_PAIR const *pair)
Return the value (lhs) quoting of a pair.
Definition cf_util.c:1622
CONF_PAIR * cf_pair_find_next(CONF_SECTION const *cs, CONF_PAIR const *prev, char const *attr)
Find a pair with a name matching attr, after specified pair.
Definition cf_util.c:1452
unsigned int cf_pair_count(CONF_SECTION const *cs, char const *attr)
Count the number of times an attribute occurs in a parent section.
Definition cf_util.c:1519
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
CONF_PAIR * cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value, fr_token_t op, fr_token_t lhs_quote, fr_token_t rhs_quote)
Allocate a CONF_PAIR.
Definition cf_util.c:1278
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1027
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition cf_util.c:1438
char const * cf_section_name(CONF_SECTION const *cs)
Return name2 if set, else name1.
Definition cf_util.c:1196
fr_token_t cf_pair_operator(CONF_PAIR const *pair)
Return the operator of a pair.
Definition cf_util.c:1607
fr_token_t cf_pair_value_quote(CONF_PAIR const *pair)
Return the value (rhs) quoting of a pair.
Definition cf_util.c:1637
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1593
CONF_ITEM * cf_pair_to_item(CONF_PAIR const *cp)
Cast a CONF_PAIR to a CONF_ITEM.
Definition cf_util.c:721
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_log_perr(_cf, _fmt,...)
Definition cf_util.h:296
#define CF_IDENT_ANY
Definition cf_util.h:78
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:210
#define MEM(x)
Definition debug.h:36
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
Definition dlist.h:1152
#define unlang_function_push(_request, _func, _repeat, _signal, _sigmask, _top_frame, _uctx)
Push a generic function onto the unlang stack.
Definition function.h:111
#define UNLANG_SUB_FRAME
Definition interpret.h:36
#define RPEDEBUG(fmt,...)
Definition log.h:376
talloc_free(reap)
fr_type_t
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_VOID
User data.
long int ssize_t
unsigned char uint8_t
#define fr_assert(_expr)
Definition rad_assert.h:38
rlm_rcode_t
Return codes indicating the result of the module call.
Definition rcode.h:40
fr_dict_attr_t const * request_attr_request
Definition request.c:43
static char const * name
#define FR_SBUFF_IN(_start, _len_or_end)
static char const * section_name_str(char const *name)
Return a printable string for the section name.
Definition section.h:98
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition tmpl.h:634
tmpl_escape_t escape
How escaping should be handled during evaluation.
Definition tmpl.h:349
int tmpl_resolve(tmpl_t *vpt, tmpl_res_rules_t const *tr_rules))
Attempt to resolve functions and attributes in xlats and attribute references.
fr_value_box_safe_for_t literals_safe_for
safe_for value assigned to literal values in xlats, execs, and data.
Definition tmpl.h:347
tmpl_rules_t const * parent
for parent / child relationships
Definition tmpl.h:333
ssize_t tmpl_afrom_attr_str(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, char const *name, tmpl_rules_t const *rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:142
@ 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_DATA
Value in native boxed format.
Definition tmpl.h:138
ssize_t tmpl_afrom_substr(TALLOC_CTX *ctx, tmpl_t **out, fr_sbuff_t *in, fr_token_t quote, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Convert an arbitrary string into a tmpl_t.
fr_type_t cast
Whether there was an explicit cast.
Definition tmpl.h:340
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
return count
Definition module.c:155
fr_aka_sim_id_type_t type
fr_dict_attr_t const * list_def
Default list to use with unqualified attribute reference.
Definition tmpl.h:295
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
Functions which we wish were included in the standard talloc distribution.
#define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:180
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:294
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:260
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
static fr_slen_t head
Definition xlat.h:419
fr_sbuff_parse_rules_t const * value_parse_rules_quoted[T_TOKEN_LAST]
Parse rules for quoted strings.
Definition value.c:605
void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
Perform a shallow copy of a value_box.
Definition value.c:4065
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:5949
@ FR_VALUE_BOX_LIST_FREE
Definition value.h:237
static fr_slen_t data
Definition value.h:1288
static size_t char ** out
Definition value.h:1020