The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
tmpl_eval.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: d19ff0f96bae0834a160725bcc94906abd9ec367 $
19 *
20 * @brief #fr_pair_t template functions
21 * @file src/lib/server/tmpl_eval.c
22 *
23 * @ingroup AVP
24 *
25 * @copyright 2014-2020 The FreeRADIUS server project
26 */
27RCSID("$Id: d19ff0f96bae0834a160725bcc94906abd9ec367 $")
28
29#define _TMPL_PRIVATE 1
30
31#include <freeradius-devel/server/exec.h>
32#include <freeradius-devel/server/exec_legacy.h>
33#include <freeradius-devel/server/tmpl.h>
34#include <freeradius-devel/server/tmpl_dcursor.h>
35#include <freeradius-devel/server/client.h>
36#include <freeradius-devel/unlang/call.h>
37
38#include <freeradius-devel/util/atexit.h>
39#include <freeradius-devel/util/edit.h>
40
41
43static fr_dict_t const *dict_radius;
44
47 { .out = &dict_freeradius, .proto = "freeradius" },
48 { .out = &dict_radius, .proto = "radius" }, /* @todo - remove RADIUS from the server core... */
49 { NULL }
50};
51
52/** Placeholder attribute for uses of unspecified attribute references
53 */
56
57
58/** Resolve attribute #fr_pair_list_t value to an attribute list.
59 *
60 * The value returned is a pointer to the pointer of the HEAD of a #fr_pair_t list in the
61 * #request_t. If the head of the list changes, the pointer will still be valid.
62 *
63 * @param[in] request containing the target lists.
64 * @param[in] list #fr_pair_list_t value to resolve to #fr_pair_t list. Will be NULL if list
65 * name couldn't be resolved.
66 * @return a pointer to the HEAD of a list in the #request_t.
67 *
68 * @see tmpl_dcursor_init
69 */
71{
72 if (!request) return NULL;
73
74 if (list == request_attr_request) {
75 if (!request->packet) return NULL;
76 return &request->request_pairs;
77 }
78
79 if (list == request_attr_reply) {
80 if (!request->reply) return NULL;
81 return &request->reply_pairs;
82 }
83
84 if (list == request_attr_control) return &request->control_pairs;
85
86 if (list == request_attr_state) return &request->session_state_pairs;
87
88 if (list == request_attr_local) return &request->local_pairs;
89
90 RWDEBUG2("List \"%s\" is not available", tmpl_list_name(list, "<INVALID>"));
91
92 return NULL;
93}
94
95/** Return the correct TALLOC_CTX to alloc #fr_pair_t in, for a list
96 *
97 * Allocating new #fr_pair_t in the context of a #request_t is usually wrong.
98 * #fr_pair_t should be allocated in the context of a #fr_packet_t, so that if the
99 * #fr_packet_t is freed before the #request_t, the associated #fr_pair_t lists are
100 * freed too.
101 *
102 * @param[in] request containing the target lists.
103 * @param[in] list #fr_pair_list_t value to resolve to TALLOC_CTX.
104 * @return
105 * - TALLOC_CTX on success.
106 * - NULL on failure.
107 *
108 * @see tmpl_pair_list
109 */
110TALLOC_CTX *tmpl_list_ctx(request_t *request, fr_dict_attr_t const *list)
111{
112 if (!request) return NULL;
113
114 if (list == request_attr_request) return request->request_ctx;
115
116 if (list == request_attr_reply) return request->reply_ctx;
117
118 if (list == request_attr_control) return request->control_ctx;
119
120 if (list == request_attr_state) return request->session_state_ctx;
121
122 if (list == request_attr_local) return request->local_ctx;
123
124 return NULL;
125}
126
127/** Resolve a list to the #fr_packet_t holding the HEAD pointer for a #fr_pair_t list
128 *
129 * Returns a pointer to the #fr_packet_t that holds the HEAD pointer of a given list,
130 * for the current #request_t.
131 *
132 * @param[in] request To resolve list in.
133 * @param[in] list #fr_pair_list_t value to resolve to #fr_packet_t.
134 * @return
135 * - #fr_packet_t on success.
136 * - NULL on failure.
137 *
138 * @see tmpl_pair_list
139 */
141{
142 if (list == request_attr_request) return request->packet;
143
144 if (list == request_attr_reply) return request->reply;
145
146 return NULL;
147}
148
149/** Resolve a #tmpl_request_ref_t to a #request_t.
150 *
151 * Sometimes #request_t structs may be chained to each other, as is the case
152 * when internally proxying EAP. This function resolves a #tmpl_request_ref_t
153 * to a #request_t higher in the chain than the current #request_t.
154 *
155 * @see tmpl_pair_list
156 * @param[in,out] context #request_t to start resolving from, and where to write
157 * a pointer to the resolved #request_t back to.
158 * @param[in] rql list of request qualifiers to follow.
159 * @return
160 * - 0 if request is valid in this context.
161 * - -1 if request is not valid in this context.
162 */
163int tmpl_request_ptr(request_t **context, FR_DLIST_HEAD(tmpl_request_list) const *rql)
164{
165 tmpl_request_t *rr = NULL;
166 request_t *request = *context;
167
168 while ((rr = tmpl_request_list_next(rql, rr))) {
169 switch (rr->request) {
170 case REQUEST_CURRENT:
171 continue; /* noop */
172
173 case REQUEST_PARENT: /* Navigate up one level */
174 if (!request->parent) return -1;
175 request = request->parent;
176 break;
177
178 case REQUEST_OUTER: /* Navigate to the outermost request */
179 if (!request->parent) return -1;
180 while (request->parent) request = request->parent;
181 break;
182
183 case REQUEST_UNKNOWN:
184 default:
185 fr_assert(0);
186 return -1;
187 }
188 }
189
190 *context = request;
191
192 return 0;
193}
194
195/** Return the native data type of the expression
196 *
197 * @param[in] vpt to determine the type of.
198 * @return
199 * - FR_TYPE_NULL if the type of the #tmpl_t can't be determined.
200 * - The data type we'd expect the #tmpl_t to produce at runtime
201 * when expanded.
202 */
204{
205 /*
206 * Regexes can't be expanded
207 */
209
210 /*
211 * Casts take precedence over everything.
212 */
214
215 /*
216 * Anything that's not a bare word will
217 * be a string unless there's a casting
218 * operator.
219 */
220 if (vpt->quote != T_BARE_WORD) return FR_TYPE_STRING;
221
222 switch (vpt->type) {
223 case TMPL_TYPE_ATTR:
224 return tmpl_attr_tail_da(vpt)->type;
225
226 case TMPL_TYPE_DATA:
227 return tmpl_value_type(vpt);
228
229 case TMPL_TYPE_XLAT:
230 case TMPL_TYPE_EXEC:
231 return FR_TYPE_STRING;
232
233 default:
234 break;
235 }
236
237 return FR_TYPE_NULL;
238}
239
240/** Expand a #tmpl_t to a string writing the result to a buffer
241 *
242 * The intended use of #tmpl_expand and #tmpl_aexpand is for modules to easily convert a #tmpl_t
243 * provided by the conf parser, into a usable value.
244 * The value returned should be raw and undoctored for #FR_TYPE_STRING and #FR_TYPE_OCTETS types,
245 * and the printable (string) version of the data for all others.
246 *
247 * Depending what arguments are passed, either copies the value to buff, or writes a pointer
248 * to a string buffer to out. This allows the most efficient access to the value resolved by
249 * the #tmpl_t, avoiding unnecessary string copies.
250 *
251 * @note This function is used where raw string values are needed, which may mean the string
252 * returned may be binary data or contain unprintable chars. #fr_snprint or #fr_asprint
253 * should be used before using these values in debug statements. #is_printable can be used to
254 * check if the string only contains printable chars.
255 *
256 * @param[out] out Where to write a pointer to the string buffer. On return may
257 * point to buff if buff was used to store the value. Otherwise will
258 * point to a #fr_value_box_t buffer, or the name of the template.
259 * Must not be NULL.
260 * @param[out] buff Expansion buffer, may be NULL except for the following types:
261 * - #TMPL_TYPE_EXEC
262 * - #TMPL_TYPE_XLAT
263 * - input type non-string, output type string
264 * @param[in] bufflen Length of expansion buffer. Must be >= 2.
265 * @param[in] request Current request.
266 * @param[in] vpt to expand. Must be one of the following types:
267 * - #TMPL_TYPE_EXEC
268 * - #TMPL_TYPE_XLAT
269 * - #TMPL_TYPE_ATTR
270 * - #TMPL_TYPE_DATA
271 * @param dst_type FR_TYPE_* matching out pointer. @see tmpl_expand.
272 * @return
273 * - -1 on failure.
274 * - The length of data written out.
275 */
277 uint8_t *buff, size_t bufflen,
278 request_t *request,
279 tmpl_t const *vpt,
280 fr_type_t dst_type)
281{
282 fr_value_box_t value_to_cast = FR_VALUE_BOX_INITIALISER_NULL(value_to_cast);
283 fr_value_box_t value_from_cast = FR_VALUE_BOX_INITIALISER_NULL(value_from_cast);
284 fr_value_box_t const *to_cast = NULL;
285 fr_value_box_t const *from_cast = NULL;
286
287 fr_pair_t *vp = NULL;
288
289 ssize_t slen = -1; /* quiet compiler */
290
292
295
296 fr_assert(!buff || (bufflen >= 2));
297
298 switch (vpt->type) {
299 case TMPL_TYPE_EXEC:
300 RDEBUG4("EXPAND TMPL EXEC");
301 if (!buff) {
302 REDEBUG("Missing expansion buffer for exec");
303 return -1;
304 }
305
306 if (radius_exec_program_legacy((char *) buff, bufflen, request, vpt->name, NULL,
307 true, false, fr_time_delta_from_sec(EXEC_TIMEOUT)) != 0) return -1;
308
309 fr_value_box_strdup_shallow(&value_to_cast, NULL, (char *) buff, true);
310 to_cast = &value_to_cast;
311 break;
312
313 case TMPL_TYPE_XLAT:
314 RDEBUG4("EXPAND TMPL XLAT PARSED");
315
316 /* No EXPAND <xlat> here as the xlat code does it */
317
318 if (!buff) {
319 REDEBUG("Missing expansion buffer for dynamic expansion");
320 return -1;
321 }
322
323 /* Error in expansion, this is distinct from zero length expansion */
324 slen = xlat_eval_compiled((char *) buff, bufflen, request, tmpl_xlat(vpt), NULL, NULL);
325 if (slen < 0) return slen;
326
327 fr_value_box_bstrndup_shallow(&value_to_cast, NULL, (char *) buff, slen, true);
328 to_cast = &value_to_cast;
329 break;
330
331 case TMPL_TYPE_ATTR:
332 RDEBUG4("EXPAND TMPL ATTR");
333 if (tmpl_find_vp(&vp, request, vpt) < 0) {
334 REDEBUG("Failed to find attribute %s", vpt->name);
335 return -2;
336 }
337
338 to_cast = &vp->data;
339 break;
340
341 case TMPL_TYPE_DATA:
342 RDEBUG4("EXPAND TMPL DATA");
343 to_cast = tmpl_value(vpt);
344 break;
345
346 /*
347 * We should never be expanding these.
348 */
354 case TMPL_TYPE_REGEX:
358 case TMPL_TYPE_MAX:
359 fr_assert(0);
360 return -1;
361 }
362
363 /*
364 * Same type, just copy the value.
365 *
366 * If the input is exec/xlat, then we can just copy the output ptr to the caller, as it's already
367 * pointing to "buff".
368 */
369 if (to_cast->type == dst_type) {
370 from_cast = to_cast;
371 goto do_copy;
372 }
373
374 /*
375 * We need a buffer to hold ouput data which can be returned to the caller.
376 */
377 if (fr_type_is_variable_size(dst_type) && !buff) {
378 REDEBUG("Missing expansion buffer for %s -> %s cast", fr_type_to_str(to_cast->type), fr_type_to_str(dst_type));
379 return -1;
380 }
381
382 /*
383 * Convert to the correct data type.
384 */
385 if (fr_value_box_cast(request, &value_from_cast, dst_type, NULL, to_cast)) {
386 RPEDEBUG("Failed casting input %pV to data type %s", to_cast, fr_type_to_str(dst_type));
387 return -1;
388 }
389
390 from_cast = &value_from_cast;
391
392 /*
393 * If the output is a talloc'd buffer, then we have to copy it to "buff", so that we can return
394 * the pointer to the caller.
395 */
396 if (fr_type_is_variable_size(dst_type)) {
397 size_t len = from_cast->vb_length + (dst_type == FR_TYPE_STRING);
398
399 if (bufflen < len) {
400 REDEBUG("Expansion buffer is too small. Buffer is %zu bytes, and we need %zu bytes",
401 bufflen, len);
402 }
403
404 /*
405 * Copy the data to the buffer, and clear the alloc'd pointer.
406 */
407 memcpy(buff, to_cast->vb_octets, len);
408 fr_value_box_clear(&value_from_cast);
409
410 /*
411 * "out" is a pointer to a char* or uint8_t*
412 */
413 *(uint8_t **) out = buff;
414
415 return from_cast->vb_length;
416 }
417
418do_copy:
419 RDEBUG4("Copying %zu bytes to %p from offset %zu",
421
422 fr_value_box_memcpy_out(out, from_cast);
423
424 return from_cast->vb_length;
425}
426
427/** Expand a template to a string, allocing a new buffer to hold the string
428 *
429 * The intended use of #tmpl_expand and #tmpl_aexpand is for modules to easily convert a #tmpl_t
430 * provided by the conf parser, into a usable value.
431 * The value returned should be raw and undoctored for #FR_TYPE_STRING and #FR_TYPE_OCTETS types,
432 * and the printable (string) version of the data for all others.
433 *
434 * This function will always duplicate values, whereas #tmpl_expand may return a pointer to an
435 * existing buffer.
436 *
437 * @note This function is used where raw string values are needed, which may mean the string
438 * returned may be binary data or contain unprintable chars. #fr_snprint or #fr_asprint should
439 * be used before using these values in debug statements. #is_printable can be used to check
440 * if the string only contains printable chars.
441 *
442 * @note The type (char or uint8_t) can be obtained with talloc_get_type, and may be used as a
443 * hint as to how to process or print the data.
444 *
445 * @param ctx to allocate new buffer in.
446 * @param out Where to write pointer to the new buffer.
447 * @param request Current request.
448 * @param vpt to expand. Must be one of the following types:
449 * - #TMPL_TYPE_DATA_UNRESOLVED
450 * - #TMPL_TYPE_EXEC
451 * - #TMPL_TYPE_XLAT
452 * - #TMPL_TYPE_ATTR
453 * - #TMPL_TYPE_DATA
454 * @param escape xlat escape function (only used for TMPL_TYPE_XLAT_UNRESOLVED_* types).
455 * @param escape_ctx xlat escape function data (only used for TMPL_TYPE_XLAT_UNRESOLVED_* types).
456 * @param dst_type FR_TYPE_* matching out pointer. @see tmpl_aexpand.
457 * @return
458 * - -1 on failure.
459 * - The length of data written to buff, or pointed to by out.
460 */
461ssize_t _tmpl_to_atype(TALLOC_CTX *ctx, void *out,
462 request_t *request,
463 tmpl_t const *vpt,
464 xlat_escape_legacy_t escape, void const *escape_ctx,
465 fr_type_t dst_type)
466{
467 fr_value_box_t *vb_out, *vb_in = NULL;
469
470 fr_pair_t *vp = NULL;
471 bool needs_dup = false;
472
473 ssize_t slen = -1;
474 int ret;
475
476 TALLOC_CTX *tmp_ctx = NULL;
477 char *str = NULL;
478
480
483
484 switch (vpt->type) {
485 case TMPL_TYPE_EXEC:
486 RDEBUG4("EXPAND TMPL EXEC");
487
488 MEM(tmp_ctx = talloc_new(ctx));
489
490 MEM(fr_value_box_bstr_alloc(tmp_ctx, &str, &value, NULL, 1024, true));
491 if (radius_exec_program_legacy(str, 1024, request, vpt->name, NULL,
492 true, false, fr_time_delta_from_sec(EXEC_TIMEOUT)) != 0) {
493 error:
494 talloc_free(tmp_ctx);
495 return slen;
496 }
497
498 fr_value_box_strtrim(tmp_ctx, &value);
499 vb_in = &value;
500 break;
501
502 case TMPL_TYPE_XLAT:
504 RDEBUG4("EXPAND TMPL XLAT STRUCT");
505
506 MEM(tmp_ctx = talloc_new(ctx));
507
508 /*
509 * An error in expansion is distinct from zero
510 * length expansion. Zero-length strings are
511 * permitted.
512 */
513 slen = xlat_aeval_compiled(tmp_ctx, &str, request, tmpl_xlat(vpt), escape, escape_ctx);
514 if (slen < 0) goto error;
515
516 /*
517 * The output is a string which might get cast to something later.
518 */
519 fr_value_box_bstrndup_shallow(&value, NULL, str, (size_t) slen, false);
520 vb_in = &value;
521 break;
522
523 case TMPL_TYPE_ATTR:
524 RDEBUG4("EXPAND TMPL ATTR");
525
526 ret = tmpl_find_vp(&vp, request, vpt);
527 if (ret < 0) {
528 RDEBUG("Failed finding attribute %s", vpt->name);
529 talloc_free(tmp_ctx);
530 return -2;
531 }
532
533 fr_assert(vp);
534
535 needs_dup = true;
536 vb_in = &vp->data;
537 break;
538
539 case TMPL_TYPE_DATA:
540 RDEBUG4("EXPAND TMPL DATA");
541
542 needs_dup = true;
544 break;
545
546 /*
547 * We should never be expanding these.
548 */
551 case TMPL_TYPE_REGEX:
557 case TMPL_TYPE_MAX:
558 fr_assert(0);
559 goto error;
560 }
561
562 fr_assert(vb_in != NULL);
563 VALUE_BOX_VERIFY(vb_in);
564
565 /*
566 * If the output is a value-box, we might cast it using the tmpl cast. When done, we just copy
567 * the value-box.
568 */
569 if (dst_type == FR_TYPE_VALUE_BOX) {
570 fr_type_t cast_type;
571
572 MEM(vb_out = fr_value_box_alloc_null(ctx));
573 cast_type = tmpl_rules_cast(vpt);
574
575 if (cast_type == FR_TYPE_NULL) {
576 if (needs_dup) {
577 fr_value_box_copy(vb_out, vb_out, vb_in);
578 } else {
579 fr_value_box_steal(vb_out, vb_out, vb_in);
580 }
581 talloc_free(tmp_ctx);
582
583 } else {
584 ret = fr_value_box_cast(vb_out, vb_out, cast_type, NULL, vb_in);
585 talloc_free(tmp_ctx);
586
587 if (ret < 0) {
588 talloc_free(vb_out);
589 dst_type = cast_type;
590
591 failed_cast:
592 RPEDEBUG("Failed casting input %pV to data type %s", vb_in, fr_type_to_str(dst_type));
593 goto error;
594 }
595 }
596
597 VALUE_BOX_VERIFY(vb_out);
598 *(fr_value_box_t **) out = vb_out;
599 return 0;
600 }
601
602 /*
603 * Cast the data to the correct type. Which also allocates any variable sized buffers from the
604 * output ctx.
605 */
606 if (dst_type != vb_in->type) {
607 if (vb_in == &value) {
608 fr_assert(tmp_ctx != NULL);
609 fr_assert(str != NULL);
610 fr_assert(dst_type != FR_TYPE_STRING); /* exec / xlat returned string in 'str' */
611
612 slen = fr_value_box_from_str(ctx, &value, dst_type, NULL, str, (size_t) slen, NULL);
613 if (slen < 0) {
614 fr_value_box_bstrndup_shallow(&value, NULL, str, (size_t) slen, false);
615 goto failed_cast;
616 }
617
618 } else {
619 ret = fr_value_box_cast(ctx, &value, dst_type, NULL, vb_in);
620 if (ret < 0) goto failed_cast;
621 }
622
623 /*
624 * The input data has been converted, and placed into value.
625 */
626 vb_in = &value;
627
628 } else if (fr_type_is_variable_size(dst_type)) {
629 /*
630 * The output type is the same, but variable sized types need to be either duplicated, or
631 * reparented.
632 */
633 if (needs_dup) {
634 fr_assert(vb_in != &value);
635
636 ret = fr_value_box_copy(ctx, &value, vb_in);
637 if (ret < 0) goto failed_cast;
638
639 vb_in = &value;
640 } else {
641 fr_assert(dst_type == FR_TYPE_STRING);
642 fr_assert(str != NULL);
643
644 (void) talloc_steal(ctx, str); /* ensure it's parented from the right context */
645 fr_assert(vb_in == &value);
646 }
647 } /* else the output type is a leaf, and is the same data type as the input */
648
649 RDEBUG4("Copying %zu bytes to %p from offset %zu",
650 fr_value_box_field_sizes[dst_type], *((void **)out), fr_value_box_offsets[dst_type]);
651
653
654 /*
655 * Frees any memory allocated for temporary buffers
656 * in this function.
657 */
658 talloc_free(tmp_ctx);
659
660 return vb_in->vb_length;
661}
662
663/** Copy pairs matching a #tmpl_t in the current #request_t
664 *
665 * @param ctx to allocate new #fr_pair_t in.
666 * @param out Where to write the copied #fr_pair_t (s).
667 * @param request The current #request_t.
668 * @param vpt specifying the #fr_pair_t type or list to copy.
669 * Must be one of the following types:
670 * - #TMPL_TYPE_ATTR
671 * @return
672 * - -1 if no matching #fr_pair_t could be found.
673 * - -2 if list could not be found (doesn't exist in current #request_t).
674 * - -3 if context could not be found (no parent #request_t available).
675 * - -4 on memory allocation error.
676 */
677int tmpl_copy_pairs(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt)
678{
679 fr_pair_t *vp;
680 fr_dcursor_t from;
682 int err;
683
685
687
688 for (vp = tmpl_dcursor_init(&err, NULL, &cc, &from, request, vpt);
689 vp;
690 vp = fr_dcursor_next(&from)) {
691 vp = fr_pair_copy(ctx, vp);
692 if (!vp) {
694 fr_strerror_const("Out of memory");
695 err = -4;
696 break;
697 }
699 }
701
702 return err;
703}
704
705
706/** Copy children of pairs matching a #tmpl_t in the current #request_t
707 *
708 * @param ctx to allocate new #fr_pair_t in.
709 * @param out Where to write the copied #fr_pair_t (s).
710 * @param request The current #request_t.
711 * @param vpt specifying the #fr_pair_t type or list to copy.
712 * Must be one of the following types:
713 * - #TMPL_TYPE_ATTR
714 * @return
715 * - -1 if no matching #fr_pair_t could be found.
716 * - -2 if list could not be found (doesn't exist in current #request_t).
717 * - -3 if context could not be found (no parent #request_t available).
718 * - -4 on memory allocation error.
719 */
720int tmpl_copy_pair_children(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt)
721{
722 fr_pair_t *vp;
723 fr_dcursor_t from;
725 int err;
726
728
730
732
733 for (vp = tmpl_dcursor_init(&err, NULL, &cc, &from, request, vpt);
734 vp;
735 vp = fr_dcursor_next(&from)) {
736 switch (vp->vp_type) {
738 if (fr_pair_list_copy(ctx, out, &vp->vp_group) < 0) {
739 err = -4;
740 goto done;
741 }
742 break;
743
744 default:
745 continue;
746 }
747 }
748done:
750
751 return err;
752}
753
754
755/** Returns the first VP matching a #tmpl_t
756 *
757 * @param[out] out where to write the retrieved vp.
758 * @param[in] request The current #request_t.
759 * @param[in] vpt specifying the #fr_pair_t type to find.
760 * Must be one of the following types:
761 * - #TMPL_TYPE_ATTR
762 * @return
763 * - 0 on success (found matching #fr_pair_t).
764 * - -1 if no matching #fr_pair_t could be found.
765 * - -2 if list could not be found (doesn't exist in current #request_t).
766 * - -3 if context could not be found (no parent #request_t available).
767 */
769{
770 fr_dcursor_t cursor;
772 fr_pair_t *vp;
773 int err;
774
776
777 vp = tmpl_dcursor_init(&err, request, &cc, &cursor, request, vpt);
779
780 if (out) *out = vp;
781
782 return err;
783}
784
785/** Returns the first VP matching a #tmpl_t, or if no VPs match, creates a new one.
786 *
787 * @param[out] out where to write the retrieved or created vp.
788 * @param[in] request The current #request_t.
789 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create. Must be #TMPL_TYPE_ATTR.
790 * @return
791 * - 1 on success a pair was created.
792 * - 0 on success a pair was found.
793 * - -1 if a new #fr_pair_t couldn't be found or created.
794 * - -2 if list could not be found (doesn't exist in current #request_t).
795 * - -3 if context could not be found (no parent #request_t available).
796 */
798{
799 fr_dcursor_t cursor;
801 fr_pair_t *vp;
802 int err;
803
806
807 *out = NULL;
808
809 vp = tmpl_dcursor_init(&err, NULL, &cc, &cursor, request, vpt);
811
812 switch (err) {
813 case 0:
814 *out = vp;
815 return 0;
816
817 case -1:
818 {
819 TALLOC_CTX *ctx;
821
822 tmpl_pair_list_and_ctx(ctx, head, request, tmpl_request(vpt), tmpl_list(vpt));
823 if (!head) return -1;
824
825 if (pair_append_by_tmpl_parent(ctx, &vp, head, vpt, true) < 0) return -1;
826
827 *out = vp;
828 }
829 return 1;
830
831 default:
832 return err;
833 }
834}
835
836/** Allocate and insert a leaf vp from a tmpl_t, building the parent vps if needed.
837 *
838 * This is the simple case - just add a vp at the first place where
839 * the parents exist, or create the parents, with no attempt to handle filters.
840 *
841 * It is functionally equivalent to fr_pair_append_by_da_parent() but
842 * uses a tmpl_t to build the nested structure rather than a fr_dict_attr_t.
843 *
844 * @param[in] ctx to allocate new pair(s) in
845 * @param[out] out Leaf pair we allocated.
846 * @param[in] list to insert into.
847 * @param[in] vpt tmpl representing the attribute to add.
848 * @param[in] skip_list skip list attr ref at the head of the tmpl.
849 * @return
850 * - 0 on success.
851 * - -1 on failure.
852 */
853int pair_append_by_tmpl_parent(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, tmpl_t const *vpt, bool skip_list)
854{
855 fr_pair_t *vp = NULL;
856 TALLOC_CTX *pair_ctx = ctx;
857 tmpl_attr_t *ar, *leaf;
858 tmpl_attr_list_head_t const *ar_list = &vpt->data.attribute.ar;
859
860 if (!tmpl_is_attr(vpt)) {
861 error:
862 *out = NULL;
863 return -1;
864 }
865
866 leaf = tmpl_attr_list_tail(ar_list);
867 ar = tmpl_attr_list_head(ar_list);
868 if (!ar) goto error;
869 if (skip_list && tmpl_attr_is_list_attr(ar)) ar = tmpl_attr_list_next(ar_list, ar);
870
871 /*
872 * Walk down the tmpl ar stack looking for candidate parent
873 * attributes and then allocating the leaf.
874 */
875 while (true) {
876 if (unlikely(!ar)) goto error;
877 /*
878 * We're not at the leaf, look for a potential parent
879 */
880 if (ar != leaf) {
881 vp = fr_pair_find_by_da(list, NULL, ar->da);
882 /*
883 * HACK - Pretend we didn't see this stupid key field
884 *
885 * If we don't have this, the code creates a key pair
886 * and then horribly mangles its data by adding children
887 * to it.
888 *
889 * We just skip one level down an don't create or update
890 * the key pair.
891 */
892 if (vp && fr_dict_attr_is_key_field(ar->da) && fr_type_is_leaf(vp->data.type)) {
893 ar = tmpl_attr_list_next(ar_list, ar);
894 continue;
895 }
896 }
897 /*
898 * Nothing found, create the pair
899 */
900 if (!vp) {
901 if (fr_pair_append_by_da(pair_ctx, &vp, list, ar->da) < 0) goto error;
902 }
903
904 /*
905 * We're at the leaf, return
906 */
907 if (ar == leaf) {
908 *out = vp;
909 return 0;
910 }
911
912 /*
913 * Prepare for next level
914 */
915 list = &vp->vp_group;
916 pair_ctx = vp;
917 vp = NULL;
918 ar = tmpl_attr_list_next(ar_list, ar);
919 }
920}
921
922/** Insert a value-box to a list, with casting.
923 *
924 * @param list to append to
925 * @param box box to cast / append
926 * @param vpt tmpl with cast.
927 * @return
928 * - <0 for "cast failed"
929 * - 0 for success
930 */
931int tmpl_value_list_insert_tail(fr_value_box_list_t *list, fr_value_box_t *box, tmpl_t const *vpt)
932{
934 (box->type == tmpl_rules_cast(vpt))) {
935 fr_value_box_list_insert_tail(list, box);
936 return 0;
937 }
938
939 if (fr_value_box_cast_in_place(box, box, tmpl_rules_cast(vpt), tmpl_rules_enumv(vpt)) < 0) return -1;
940
941 fr_value_box_list_insert_tail(list, box);
943 return 0;
944}
945
946/** Gets the value of a real or virtual attribute
947 *
948 * @param[in] ctx to allocate boxed value, and buffers in.
949 * @param[out] out Where to write the boxed value.
950 * @param[in] request The current request.
951 * @param[in] vpt Representing the attribute.
952 * @return
953 * - <0 we failed getting a value for the attribute.
954 * - 0 we successfully evaluated the tmpl
955 */
956int tmpl_eval_pair(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
957{
958 fr_pair_t *vp = NULL;
960
961 fr_dcursor_t cursor;
963
964 int ret = 0;
965 fr_value_box_list_t list;
966
968
969 fr_value_box_list_init(&list);
970
971 /*
972 * See if we're dealing with an attribute in the request
973 *
974 * This allows users to manipulate virtual attributes as if
975 * they were real ones.
976 */
977 vp = tmpl_dcursor_init(NULL, NULL, &cc, &cursor, request, vpt);
978
979 /*
980 * We didn't find the VP in a list, check to see if it's
981 * virtual. This allows the caller to "realize" the
982 * attribute, and we then prefer the realized version to
983 * the virtual one.
984 */
985 if (!vp) {
986 /*
987 * Zero count.
988 */
991 if (!value) {
992 oom:
993 fr_strerror_const("Out of memory");
994 ret = -1;
995 goto fail;
996 }
997 value->datum.int32 = 0;
998 fr_value_box_list_insert_tail(&list, value);
999 } /* Fall through to being done */
1000
1001 goto done;
1002 }
1003
1004 switch (tmpl_attr_tail_num(vpt)) {
1005 /*
1006 * Return a count of the VPs.
1007 */
1008 case NUM_COUNT:
1009 {
1010 uint32_t count = 0;
1011
1012 while (vp != NULL) {
1013 count++;
1014 vp = fr_dcursor_next(&cursor);
1015 }
1016
1018 if (!value) goto oom;
1019 value->datum.uint32 = count;
1020 fr_value_box_list_insert_tail(&list, value);
1021 break;
1022 }
1023
1024 /*
1025 * Output multiple #value_box_t, one per attribute.
1026 */
1027 case NUM_ALL:
1028 /*
1029 * Loop over all matching #fr_value_pair
1030 * shallow copying buffers.
1031 */
1032 while (vp != NULL) {
1033 if (fr_type_is_structural(vp->vp_type)) {
1035 if (!value) goto oom;
1036
1037 if (fr_pair_list_copy_to_box(value, &vp->vp_group) < 0) {
1039 goto oom;
1040 }
1041
1042 } else {
1043 value = fr_value_box_alloc(ctx, vp->data.type, vp->da);
1044 if (!value) goto oom;
1045 fr_value_box_copy(value, value, &vp->data);
1046 }
1047
1048 fr_value_box_list_insert_tail(&list, value);
1049 vp = fr_dcursor_next(&cursor);
1050 }
1051 break;
1052
1053 default:
1054 if (!fr_type_is_leaf(vp->vp_type)) {
1055 fr_strerror_const("Invalid data type for evaluation");
1056 goto fail;
1057 }
1058
1059 value = fr_value_box_alloc(ctx, vp->data.type, vp->da);
1060 if (!value) goto oom;
1061
1062 fr_value_box_copy(value, value, &vp->data); /* Also dups taint */
1063 fr_value_box_list_insert_tail(&list, value);
1064 break;
1065 }
1066
1067done:
1068 /*
1069 * Evaluate casts if necessary.
1070 */
1071 if (ret == 0) {
1072 if (tmpl_eval_cast_in_place(&list, request, vpt) < 0) {
1073 fr_value_box_list_talloc_free(&list);
1074 ret = -1;
1075 goto fail;
1076 }
1077
1078 fr_value_box_list_move(out, &list);
1079 }
1080
1081fail:
1082 tmpl_dcursor_clear(&cc);
1084 return ret;
1085}
1086
1087
1088/** Gets the value of a tmpl
1089 *
1090 * The result is returned "raw". The caller must do any escaping it desires.
1091 *
1092 * @param[in] ctx to allocate boxed value, and buffers in.
1093 * @param[out] out Where to write the boxed value.
1094 * @param[in] request The current request.
1095 * @param[in] vpt Representing the tmpl
1096 * @return
1097 * - <0 we failed getting a value for the tmpl
1098 * - 0 we successfully evaluated the tmpl
1099 */
1100int tmpl_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
1101{
1102 char *p;
1104 fr_value_box_list_t list;
1105
1107 fr_strerror_const("Cannot evaluate unresolved tmpl");
1108 return -1;
1109 }
1110
1111 if (tmpl_async_required(vpt)) {
1112 fr_strerror_const("Cannot statically evaluate asynchronous expansions");
1113 return -1;
1114 }
1115
1116 if (tmpl_contains_regex(vpt)) {
1117 fr_strerror_const("Cannot statically evaluate regular expression");
1118 return -1;
1119 }
1120
1121 if (tmpl_is_attr(vpt)) {
1122 return tmpl_eval_pair(ctx, out, request, vpt);
1123 }
1124
1125 if (tmpl_is_data(vpt)) {
1127
1128 fr_value_box_copy(value, value, tmpl_value(vpt)); /* Also dups taint */
1129 goto done;
1130 }
1131
1133
1134 /*
1135 * @todo - respect escaping functions. But the sync
1136 * escaping uses a different method than the async ones.
1137 * And we then also need to escape the output of
1138 * tmpl_eval_pair(), too.
1139 */
1141 if (tmpl_aexpand(value, &p, request, vpt, NULL, NULL) < 0) {
1143 return -1;
1144 }
1145 fr_value_box_bstrndup_shallow(value, NULL, p, talloc_array_length(p) - 1, true);
1146
1147 /*
1148 * Cast the results if necessary.
1149 */
1150done:
1151 fr_value_box_list_init(&list);
1152 fr_value_box_list_insert_tail(&list, value);
1153
1154 if (tmpl_eval_cast_in_place(&list, request, vpt) < 0) {
1155 fr_value_box_list_talloc_free(&list);
1156 return -1;
1157 }
1158
1159 fr_value_box_list_move(out, &list);
1161
1162 return 0;
1163}
1164
1165/** Allocate a uctx for an escaping function
1166 *
1167 * @param[in] request The current request.
1168 * @param[in] escape Describing how to escape tmpl data.
1169 *
1170 * @return the uctx to pass to the escape function.
1171 */
1172static inline void *tmpl_eval_escape_uctx_alloc(request_t *request, tmpl_escape_t const *escape)
1173{
1174 switch (escape->uctx.type) {
1176 return UNCONST(void *, escape->uctx.ptr);
1177
1179 {
1180 void *uctx;
1181
1182 fr_assert_msg(escape->uctx.size > 0, "TMPL_ESCAPE_UCTX_ALLOC must specify uctx.size > 0");
1183 MEM(uctx = talloc_zero_array(NULL, uint8_t, escape->uctx.size));
1184 if (escape->uctx.talloc_type) talloc_set_type(uctx, escape->uctx.talloc_type);
1185 return uctx;
1186 }
1187
1189 fr_assert_msg(escape->uctx.func.alloc, "TMPL_ESCAPE_UCTX_ALLOC_FUNC must specify a non-null alloc.func");
1190 return escape->uctx.func.alloc(request, escape->uctx.func.uctx);
1191
1192 default:
1193 fr_assert_msg(0, "Unknown escape uctx type %u", escape->uctx.type);
1194 return NULL;
1195 }
1196}
1197
1198/** Free a uctx for an escaping function
1199 *
1200 * @param[in] escape Describing how to escape tmpl data.
1201 * @param[in] uctx The uctx to free.
1202 */
1203static inline void tmpl_eval_escape_uctx_free(tmpl_escape_t const *escape, void *uctx)
1204{
1205 switch (escape->uctx.type) {
1207 return;
1208
1210 talloc_free(uctx);
1211 return;
1212
1214 if (escape->uctx.func.free) escape->uctx.func.free(uctx);
1215 return;
1216 }
1217}
1218
1219/** Casts a value or list of values according to the tmpl
1220 *
1221 * @param[in,out] list Where to write the boxed value.
1222 * @param[in] request The current request.
1223 * @param[in] vpt Representing the attribute.
1224 * @return
1225 * - <0 the cast failed
1226 * - 0 we successfully evaluated the tmpl
1227 */
1228int tmpl_eval_cast_in_place(fr_value_box_list_t *list, request_t *request, tmpl_t const *vpt)
1229{
1231 bool did_concat = false;
1232 void *uctx = NULL;
1233
1234 if (fr_type_is_structural(cast)) {
1235 fr_strerror_printf("Cannot cast to structural type '%s'", fr_type_to_str(cast));
1236 return -1;
1237 }
1238
1239 /*
1240 * Quoting around the tmpl means everything
1241 * needs to be concatenated, either as a string
1242 * or octets string.
1243 */
1244 switch (vpt->quote) {
1249 {
1250 ssize_t slen;
1251 fr_value_box_t *vb;
1252
1253 vb = fr_value_box_list_head(list);
1254 if (!vb) return 0;
1255
1257 uctx = tmpl_eval_escape_uctx_alloc(request, &vpt->rules.escape);
1258 /*
1259 * Sets escaped values, so boxes don't get re-escaped
1260 */
1261 if (unlikely(fr_value_box_list_escape_in_place(list, &vpt->rules.escape.box_escape, uctx) < 0)) {
1262 error:
1263 tmpl_eval_escape_uctx_free(&vpt->rules.escape, uctx);
1264 return -1;
1265 }
1266 }
1267
1269 FR_VALUE_BOX_LIST_FREE_BOX, true, SIZE_MAX);
1270 if (slen < 0) goto error;
1272
1273 /*
1274 * If there's no cast, or it's a cast to
1275 * a string, we're done!
1276 *
1277 * Otherwise we now need to re-cast the
1278 * result.
1279 */
1280 if (fr_type_is_null(cast) || fr_type_is_string(cast)) {
1281 success:
1282 tmpl_eval_escape_uctx_free(&vpt->rules.escape, uctx);
1283 return 0;
1284 }
1285
1286 did_concat = true;
1287 }
1288 break;
1289
1290 default:
1291 break;
1292 }
1293
1294 if (fr_type_is_null(cast)) goto success;
1295
1296 /*
1297 * Quoting above handled all concatenation,
1298 * we now need to handle potentially
1299 * multivalued lists.
1300 */
1302 if (fr_value_box_cast_in_place(vb, vb, cast, NULL) < 0) goto error;
1303 }}
1304
1305 /*
1306 * ...and finally, apply the escape function
1307 * if necessary. This is done last so that
1308 * the escape function gets boxes of the type
1309 * it expects.
1310 */
1312 uctx = tmpl_eval_escape_uctx_alloc(request, &vpt->rules.escape);
1313 if (unlikely(fr_value_box_list_escape_in_place(list, &vpt->rules.escape.box_escape, uctx) < 0)) goto error;
1314 }
1315
1316 /*
1317 * If there's no escape function, but there is
1318 * a safe_for value, mark all the boxes up with
1319 * this value.
1320 *
1321 * This is mostly useful for call_env usage in
1322 * modules where certain values are implicitly safe
1323 * for consumption, like SQL statements in the SQL
1324 * module.
1325 */
1326 if (!vpt->rules.escape.box_escape.func && vpt->rules.escape.box_escape.safe_for) {
1327 fr_value_box_list_mark_safe_for(list, vpt->rules.escape.box_escape.safe_for);
1328 }
1329
1331
1333}
1334
1336{
1338
1339 if (vpt->quote != T_BARE_WORD) return FR_TYPE_STRING;
1340
1341 if (tmpl_is_data(vpt)) return tmpl_value_type(vpt);
1342
1343 if (tmpl_is_attr(vpt)) return tmpl_attr_tail_da(vpt)->type;
1344
1346
1347 return FR_TYPE_NULL; /* can't determine it */
1348}
1349
1350
1351static int _tmpl_global_free(UNUSED void *uctx)
1352{
1354
1355 return 0;
1356}
1357
1358static int _tmpl_global_init(UNUSED void *uctx)
1359{
1360 fr_dict_attr_t *da;
1361
1362 if (fr_dict_autoload(tmpl_dict) < 0) {
1363 PERROR("%s", __FUNCTION__);
1364 return -1;
1365 }
1366
1368 fr_assert(da != NULL);
1369
1370 da->type = FR_TYPE_NULL;
1371 tmpl_attr_unspec = da;
1372
1373 return 0;
1374}
1375
1377{
1378 int ret;
1379
1380 fr_atexit_global_once_ret(&ret, _tmpl_global_init, _tmpl_global_free, NULL);
1381
1382 return 0;
1383}
static int context
Definition radmin.c:71
#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 void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:290
#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_dict_autofree(_to_free)
Definition dict.h:869
static fr_slen_t err
Definition dict.h:840
static fr_dict_attr_t * fr_dict_attr_unknown_raw_afrom_num(TALLOC_CTX *ctx, fr_dict_attr_t const *parent, unsigned int attr)
Definition dict.h:586
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2403
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:286
#define fr_dict_autoload(_to_load)
Definition dict.h:866
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:158
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:285
Test enumeration values.
Definition dict_test.h:92
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition dlist.h:1122
#define EXEC_TIMEOUT
Default wait time for exec calls (in seconds).
Definition exec.h:32
int radius_exec_program_legacy(char *out, size_t outlen, request_t *request, char const *cmd, fr_pair_list_t *input_pairs, bool exec_wait, bool shell_escape, fr_time_delta_t timeout)
Execute a program.
#define PERROR(_fmt,...)
Definition log.h:228
#define RWDEBUG2(fmt,...)
Definition log.h:362
#define RPEDEBUG(fmt,...)
Definition log.h:376
#define RDEBUG4(fmt,...)
Definition log.h:344
talloc_free(reap)
size_t(* xlat_escape_legacy_t)(request_t *request, char *out, size_t outlen, char const *in, void *arg)
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_VALUE_BOX
A boxed value.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
int fr_pair_append_by_da(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, fr_dict_attr_t const *da)
Alloc a new fr_pair_t (and append)
Definition pair.c:1472
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2329
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:697
int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add)
Add a VP to the end of the list.
Definition pair.c:1351
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:493
int fr_pair_list_copy_to_box(fr_value_box_t *dst, fr_pair_list_t *from)
Copy the contents of a pair list to a set of value-boxes.
Definition pair.c:2364
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:81
#define REDEBUG(fmt,...)
Definition radclient.h:52
#define RDEBUG(fmt,...)
Definition radclient.h:53
fr_dict_attr_t const * request_attr_request
Definition request.c:43
fr_dict_attr_t const * request_attr_control
Definition request.c:45
fr_dict_attr_t const * request_attr_local
Definition request.c:47
fr_dict_attr_t const * request_attr_state
Definition request.c:46
fr_dict_attr_t const * request_attr_reply
Definition request.c:44
static int16_t tmpl_attr_tail_num(tmpl_t const *vpt)
Return the last attribute reference's attribute number.
Definition tmpl.h:885
#define TMPL_VERIFY(_vpt)
Definition tmpl.h:961
#define tmpl_is_xlat(vpt)
Definition tmpl.h:210
#define tmpl_rules_enumv(_tmpl)
Definition tmpl.h:943
#define tmpl_value(_tmpl)
Definition tmpl.h:937
#define tmpl_contains_regex(vpt)
Definition tmpl.h:226
#define tmpl_is_attr(vpt)
Definition tmpl.h:208
#define NUM_ALL
Definition tmpl.h:391
bool tmpl_async_required(tmpl_t const *vpt)
Return whether or not async is required for this tmpl.
#define tmpl_xlat(_tmpl)
Definition tmpl.h:930
static fr_dict_attr_t const * tmpl_list(tmpl_t const *vpt)
Definition tmpl.h:904
static bool tmpl_attr_is_list_attr(tmpl_attr_t const *ar)
Return true if the tmpl_attr is one of the list types.
Definition tmpl.h:679
#define tmpl_rules_cast(_tmpl)
Definition tmpl.h:942
@ TMPL_TYPE_REGEX_UNCOMPILED
Regex where compilation is possible but hasn't been performed yet.
Definition tmpl.h:158
@ TMPL_TYPE_MAX
Marker for the last tmpl type.
Definition tmpl.h:199
@ TMPL_TYPE_ATTR_UNRESOLVED
An attribute reference that we couldn't resolve but looked valid.
Definition tmpl.h:185
@ 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_REGEX_XLAT_UNRESOLVED
A regular expression with unresolved xlat functions or attribute references.
Definition tmpl.h:197
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:138
@ TMPL_TYPE_REGEX
Compiled (and possibly JIT'd) regular expression.
Definition tmpl.h:154
@ TMPL_TYPE_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:179
@ 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
@ TMPL_TYPE_EXEC_UNRESOLVED
An exec with unresolved xlat function or attribute references.
Definition tmpl.h:189
@ TMPL_TYPE_UNINITIALISED
Uninitialised.
Definition tmpl.h:134
#define NUM_COUNT
Definition tmpl.h:392
#define tmpl_pair_list_and_ctx(_ctx, _head, _request, _ref, _list)
Determine the correct context and list head.
Definition tmpl.h:993
#define tmpl_is_data(vpt)
Definition tmpl.h:206
static fr_slen_t vpt
Definition tmpl.h:1269
#define tmpl_value_type(_tmpl)
Definition tmpl.h:939
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:801
static char const * tmpl_list_name(fr_dict_attr_t const *list, char const *def)
Return the name of a tmpl list or def if list not provided.
Definition tmpl.h:915
@ REQUEST_OUTER
request_t containing the outer layer of the EAP conversation.
Definition tmpl.h:92
@ REQUEST_PARENT
Parent (whatever it is).
Definition tmpl.h:96
@ REQUEST_UNKNOWN
Unknown request.
Definition tmpl.h:97
@ REQUEST_CURRENT
The current request (default).
Definition tmpl.h:91
#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
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:223
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
return count
Definition module.c:155
fr_pair_t * vp
An element in a list of nested attribute references.
Definition tmpl.h:430
fr_dict_attr_t const *_CONST da
Resolved dictionary attribute.
Definition tmpl.h:434
Define manipulation functions for the attribute reference list.
Definition tmpl.h:471
tmpl_request_ref_t _CONST request
Definition tmpl.h:475
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
fr_dict_attr_t const *_CONST da
Dictionary attribute defines the attribute number, vendor and type of the pair.
Definition pair.h:69
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
void tmpl_dcursor_clear(tmpl_dcursor_ctx_t *cc)
Clear any temporary state allocations.
#define tmpl_dcursor_init(_err, _ctx, _cc, _cursor, _request, _vpt)
Maintains state between cursor calls.
#define tmpl_escape_post_concat(_tmpl)
See if we should perform output escaping after concatenation.
#define tmpl_escape_pre_concat(_tmpl)
See if we should perform output escaping before concatenation.
@ TMPL_ESCAPE_UCTX_ALLOC
A new uctx of the specified size and type is allocated and freed when escaping is complete.
Definition tmpl_escape.h:33
@ TMPL_ESCAPE_UCTX_STATIC
A static (to us) is provided by whatever is initialising the tmpl_escape_t.
Definition tmpl_escape.h:31
@ TMPL_ESCAPE_UCTX_ALLOC_FUNC
A new uctx of the specified size and type is allocated and pre-populated by memcpying uctx....
Definition tmpl_escape.h:35
struct tmpl_escape_t::@75 uctx
Escaping rules for tmpls.
Definition tmpl_escape.h:80
int tmpl_find_vp(fr_pair_t **out, request_t *request, tmpl_t const *vpt)
Returns the first VP matching a tmpl_t.
Definition tmpl_eval.c:768
fr_packet_t * tmpl_packet_ptr(request_t *request, fr_dict_attr_t const *list)
Resolve a list to the fr_packet_t holding the HEAD pointer for a fr_pair_t list.
Definition tmpl_eval.c:140
int tmpl_value_list_insert_tail(fr_value_box_list_t *list, fr_value_box_t *box, tmpl_t const *vpt)
Insert a value-box to a list, with casting.
Definition tmpl_eval.c:931
fr_dict_attr_t const * tmpl_attr_unspec
Placeholder attribute for uses of unspecified attribute references.
Definition tmpl_eval.c:55
static int _tmpl_global_free(UNUSED void *uctx)
Definition tmpl_eval.c:1351
static fr_dict_t const * dict_freeradius
Definition tmpl_eval.c:42
static fr_dict_t const * dict_radius
Definition tmpl_eval.c:43
int tmpl_request_ptr(request_t **context, FR_DLIST_HEAD(tmpl_request_list) const *rql)
Resolve a tmpl_request_ref_t to a request_t.
Definition tmpl_eval.c:163
int tmpl_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
Gets the value of a tmpl.
Definition tmpl_eval.c:1100
int tmpl_eval_cast_in_place(fr_value_box_list_t *list, request_t *request, tmpl_t const *vpt)
Casts a value or list of values according to the tmpl.
Definition tmpl_eval.c:1228
fr_pair_list_t * tmpl_list_head(request_t *request, fr_dict_attr_t const *list)
Resolve attribute fr_pair_list_t value to an attribute list.
Definition tmpl_eval.c:70
TALLOC_CTX * tmpl_list_ctx(request_t *request, fr_dict_attr_t const *list)
Return the correct TALLOC_CTX to alloc fr_pair_t in, for a list.
Definition tmpl_eval.c:110
ssize_t _tmpl_to_atype(TALLOC_CTX *ctx, void *out, request_t *request, tmpl_t const *vpt, xlat_escape_legacy_t escape, void const *escape_ctx, fr_type_t dst_type)
Expand a template to a string, allocing a new buffer to hold the string.
Definition tmpl_eval.c:461
fr_type_t tmpl_data_type(tmpl_t const *vpt)
Definition tmpl_eval.c:1335
int tmpl_global_init(void)
Definition tmpl_eval.c:1376
int tmpl_eval_pair(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
Gets the value of a real or virtual attribute.
Definition tmpl_eval.c:956
goto success
Definition tmpl_eval.c:1332
int tmpl_copy_pair_children(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt)
Copy children of pairs matching a tmpl_t in the current request_t.
Definition tmpl_eval.c:720
ssize_t _tmpl_to_type(void *out, uint8_t *buff, size_t bufflen, request_t *request, tmpl_t const *vpt, fr_type_t dst_type)
Expand a tmpl_t to a string writing the result to a buffer.
Definition tmpl_eval.c:276
int tmpl_copy_pairs(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt)
Copy pairs matching a tmpl_t in the current request_t.
Definition tmpl_eval.c:677
static void tmpl_eval_escape_uctx_free(tmpl_escape_t const *escape, void *uctx)
Free a uctx for an escaping function.
Definition tmpl_eval.c:1203
int tmpl_find_or_add_vp(fr_pair_t **out, request_t *request, tmpl_t const *vpt)
Returns the first VP matching a tmpl_t, or if no VPs match, creates a new one.
Definition tmpl_eval.c:797
static int _tmpl_global_init(UNUSED void *uctx)
Definition tmpl_eval.c:1358
fr_dict_autoload_t tmpl_dict[]
Definition tmpl_eval.c:46
static void * tmpl_eval_escape_uctx_alloc(request_t *request, tmpl_escape_t const *escape)
Allocate a uctx for an escaping function.
Definition tmpl_eval.c:1172
fr_type_t tmpl_expanded_type(tmpl_t const *vpt)
Return the native data type of the expression.
Definition tmpl_eval.c:203
int pair_append_by_tmpl_parent(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, tmpl_t const *vpt, bool skip_list)
Allocate and insert a leaf vp from a tmpl_t, building the parent vps if needed.
Definition tmpl_eval.c:853
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_BARE_WORD
Definition token.h:120
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:124
ssize_t xlat_eval_compiled(char *out, size_t outlen, request_t *request, xlat_exp_head_t const *head, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1787
static fr_slen_t head
Definition xlat.h:419
ssize_t xlat_aeval_compiled(TALLOC_CTX *ctx, char **out, request_t *request, xlat_exp_head_t const *head, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1804
fr_type_t xlat_data_type(xlat_exp_head_t const *head)
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_type_is_variable_size(_x)
Definition types.h:384
#define fr_type_is_structural(_x)
Definition types.h:388
#define fr_type_is_string(_x)
Definition types.h:344
#define FR_TYPE_STRUCTURAL
Definition types.h:312
#define fr_type_is_null(_x)
Definition types.h:343
#define fr_type_is_leaf(_x)
Definition types.h:389
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:450
size_t const fr_value_box_field_sizes[]
How many bytes wide each of the value data fields are.
Definition value.c:148
int fr_value_box_strtrim(TALLOC_CTX *ctx, fr_value_box_t *vb)
Trim the length of the string buffer to match the length of the C string.
Definition value.c:4184
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3574
size_t const fr_value_box_offsets[]
Where the value starts in the fr_value_box_t.
Definition value.c:188
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:3962
int fr_value_box_list_escape_in_place(fr_value_box_list_t *list, fr_value_box_escape_t const *escape, void *uctx)
Escape a list of value boxes in place.
Definition value.c:6168
int fr_value_box_cast_in_place(TALLOC_CTX *ctx, fr_value_box_t *vb, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv)
Convert one type of fr_value_box_t to another in place.
Definition value.c:3790
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:5459
void fr_value_box_list_mark_safe_for(fr_value_box_list_t *list, fr_value_box_safe_for_t safe_for)
Set the escaped flag for all value boxes in a list.
Definition value.c:6509
void fr_value_box_strdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4267
int fr_value_box_bstr_alloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Alloc and assign an empty \0 terminated string to a fr_value_box_t.
Definition value.c:4302
int fr_value_box_steal(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t *src)
Copy value data verbatim moving any buffers to the specified context.
Definition value.c:4089
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3945
void fr_value_box_bstrndup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Assign a string to to a fr_value_box_t.
Definition value.c:4463
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_BOX
Free each processed box.
Definition value.h:234
#define fr_value_box_list_foreach_safe(_list_head, _iter)
Definition value.h:223
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:640
static int fr_value_box_memcpy_out(void *out, fr_value_box_t const *vb)
Copy the value of a value box to a field in a C struct.
Definition value.h:787
#define FR_VALUE_BOX_INITIALISER_NULL(_vb)
A static initialiser for stack/globally allocated boxes.
Definition value.h:507
#define VALUE_BOX_VERIFY(_x)
Definition value.h:1318
#define VALUE_BOX_LIST_VERIFY(_x)
Definition value.h:1319
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:651
static size_t char ** out
Definition value.h:1020