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: dcfc4eaac9c6bb260b08fbab119265dd2cfa1442 $
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: dcfc4eaac9c6bb260b08fbab119265dd2cfa1442 $")
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/dlist.h>
40#include <freeradius-devel/util/proto.h>
41#include <freeradius-devel/util/value.h>
42#include <freeradius-devel/util/edit.h>
43#include <freeradius-devel/util/token.h>
44#include <freeradius-devel/util/types.h>
45
46#include <talloc.h>
47
49static fr_dict_t const *dict_radius;
50
53 { .out = &dict_freeradius, .proto = "freeradius" },
54 { .out = &dict_radius, .proto = "radius" }, /* @todo - remove RADIUS from the server core... */
55 { NULL }
56};
57
58/** Placeholder attribute for uses of unspecified attribute references
59 */
62
63
64/** Resolve attribute #fr_pair_list_t value to an attribute list.
65 *
66 * The value returned is a pointer to the pointer of the HEAD of a #fr_pair_t list in the
67 * #request_t. If the head of the list changes, the pointer will still be valid.
68 *
69 * @param[in] request containing the target lists.
70 * @param[in] list #fr_pair_list_t value to resolve to #fr_pair_t list. Will be NULL if list
71 * name couldn't be resolved.
72 * @return a pointer to the HEAD of a list in the #request_t.
73 *
74 * @see tmpl_dcursor_init
75 */
77{
78 if (!request) return NULL;
79
80 if (list == request_attr_request) {
81 if (!request->packet) return NULL;
82 return &request->request_pairs;
83 }
84
85 if (list == request_attr_reply) {
86 if (!request->reply) return NULL;
87 return &request->reply_pairs;
88 }
89
90 if (list == request_attr_control) return &request->control_pairs;
91
92 if (list == request_attr_state) return &request->session_state_pairs;
93
94 if (list == request_attr_local) return &request->local_pairs;
95
96 RWDEBUG2("List \"%s\" is not available", tmpl_list_name(list, "<INVALID>"));
97
98 return NULL;
99}
100
101/** Return the correct TALLOC_CTX to alloc #fr_pair_t in, for a list
102 *
103 * Allocating new #fr_pair_t in the context of a #request_t is usually wrong.
104 * #fr_pair_t should be allocated in the context of a #fr_packet_t, so that if the
105 * #fr_packet_t is freed before the #request_t, the associated #fr_pair_t lists are
106 * freed too.
107 *
108 * @param[in] request containing the target lists.
109 * @param[in] list #fr_pair_list_t value to resolve to TALLOC_CTX.
110 * @return
111 * - TALLOC_CTX on success.
112 * - NULL on failure.
113 *
114 * @see tmpl_pair_list
115 */
116TALLOC_CTX *tmpl_list_ctx(request_t *request, fr_dict_attr_t const *list)
117{
118 if (!request) return NULL;
119
120 if (list == request_attr_request) return request->request_ctx;
121
122 if (list == request_attr_reply) return request->reply_ctx;
123
124 if (list == request_attr_control) return request->control_ctx;
125
126 if (list == request_attr_state) return request->session_state_ctx;
127
128 if (list == request_attr_local) return request->local_ctx;
129
130 return NULL;
131}
132
133/** Resolve a list to the #fr_packet_t holding the HEAD pointer for a #fr_pair_t list
134 *
135 * Returns a pointer to the #fr_packet_t that holds the HEAD pointer of a given list,
136 * for the current #request_t.
137 *
138 * @param[in] request To resolve list in.
139 * @param[in] list #fr_pair_list_t value to resolve to #fr_packet_t.
140 * @return
141 * - #fr_packet_t on success.
142 * - NULL on failure.
143 *
144 * @see tmpl_pair_list
145 */
147{
148 if (list == request_attr_request) return request->packet;
149
150 if (list == request_attr_reply) return request->reply;
151
152 return NULL;
153}
154
155/** Resolve a #tmpl_request_ref_t to a #request_t.
156 *
157 * Sometimes #request_t structs may be chained to each other, as is the case
158 * when internally proxying EAP. This function resolves a #tmpl_request_ref_t
159 * to a #request_t higher in the chain than the current #request_t.
160 *
161 * @see tmpl_pair_list
162 * @param[in,out] context #request_t to start resolving from, and where to write
163 * a pointer to the resolved #request_t back to.
164 * @param[in] rql list of request qualifiers to follow.
165 * @return
166 * - 0 if request is valid in this context.
167 * - -1 if request is not valid in this context.
168 */
169int tmpl_request_ptr(request_t **context, FR_DLIST_HEAD(tmpl_request_list) const *rql)
170{
171 tmpl_request_t *rr = NULL;
172 request_t *request = *context;
173
174 while ((rr = tmpl_request_list_next(rql, rr))) {
175 switch (rr->request) {
176 case REQUEST_CURRENT:
177 continue; /* noop */
178
179 case REQUEST_PARENT: /* Navigate up one level */
180 if (!request->parent) return -1;
181 request = request->parent;
182 break;
183
184 case REQUEST_OUTER: /* Navigate to the outermost request */
185 if (!request->parent) return -1;
186 while (request->parent) request = request->parent;
187 break;
188
189 case REQUEST_UNKNOWN:
190 default:
191 fr_assert(0);
192 return -1;
193 }
194 }
195
196 *context = request;
197
198 return 0;
199}
200
201/** Return the native data type of the expression
202 *
203 * @param[in] vpt to determine the type of.
204 * @return
205 * - FR_TYPE_NULL if the type of the #tmpl_t can't be determined.
206 * - The data type we'd expect the #tmpl_t to produce at runtime
207 * when expanded.
208 */
210{
211 /*
212 * Regexes can't be expanded
213 */
215
216 /*
217 * Casts take precedence over everything.
218 */
220
221 /*
222 * Anything that's not a bare word will
223 * be a string unless there's a casting
224 * operator.
225 */
226 if (vpt->quote != T_BARE_WORD) return FR_TYPE_STRING;
227
228 switch (vpt->type) {
229 case TMPL_TYPE_ATTR:
230 return tmpl_attr_tail_da(vpt)->type;
231
232 case TMPL_TYPE_DATA:
233 return tmpl_value_type(vpt);
234
235 case TMPL_TYPE_XLAT:
236 case TMPL_TYPE_EXEC:
237 return FR_TYPE_STRING;
238
239 default:
240 break;
241 }
242
243 return FR_TYPE_NULL;
244}
245
246/** Expand a #tmpl_t to a string writing the result to a buffer
247 *
248 * The intended use of #tmpl_expand and #tmpl_aexpand is for modules to easily convert a #tmpl_t
249 * provided by the conf parser, into a usable value.
250 * The value returned should be raw and undoctored for #FR_TYPE_STRING and #FR_TYPE_OCTETS types,
251 * and the printable (string) version of the data for all others.
252 *
253 * Depending what arguments are passed, either copies the value to buff, or writes a pointer
254 * to a string buffer to out. This allows the most efficient access to the value resolved by
255 * the #tmpl_t, avoiding unnecessary string copies.
256 *
257 * @note This function is used where raw string values are needed, which may mean the string
258 * returned may be binary data or contain unprintable chars. #fr_snprint or #fr_asprint
259 * should be used before using these values in debug statements. #is_printable can be used to
260 * check if the string only contains printable chars.
261 *
262 * @param[out] out Where to write a pointer to the string buffer. On return may
263 * point to buff if buff was used to store the value. Otherwise will
264 * point to a #fr_value_box_t buffer, or the name of the template.
265 * Must not be NULL.
266 * @param[out] buff Expansion buffer, may be NULL except for the following types:
267 * - #TMPL_TYPE_EXEC
268 * - #TMPL_TYPE_XLAT
269 * @param[in] bufflen Length of expansion buffer. Must be >= 2.
270 * @param[in] request Current request.
271 * @param[in] vpt to expand. Must be one of the following types:
272 * - #TMPL_TYPE_DATA_UNRESOLVED
273 * - #TMPL_TYPE_EXEC
274 * - #TMPL_TYPE_XLAT
275 * - #TMPL_TYPE_ATTR
276 * - #TMPL_TYPE_DATA
277 * @param dst_type FR_TYPE_* matching out pointer. @see tmpl_expand.
278 * @return
279 * - -1 on failure.
280 * - The length of data written out.
281 */
283 uint8_t *buff, size_t bufflen,
284 request_t *request,
285 tmpl_t const *vpt,
286 fr_type_t dst_type)
287{
288 fr_value_box_t value_to_cast = FR_VALUE_BOX_INITIALISER_NULL(value_to_cast);
289 fr_value_box_t value_from_cast = FR_VALUE_BOX_INITIALISER_NULL(value_from_cast);
290 fr_value_box_t const *to_cast = &value_to_cast;
291 fr_value_box_t const *from_cast = &value_from_cast;
292
293 fr_pair_t *vp = NULL;
294
295 fr_type_t src_type = FR_TYPE_NULL;
296
297 ssize_t slen = -1; /* quiet compiler */
298
300
301 fr_assert(!buff || (bufflen >= 2));
302
303 switch (vpt->type) {
305 RDEBUG4("EXPAND TMPL UNRESOLVED");
306 fr_value_box_bstrndup_shallow(&value_to_cast, NULL, vpt->name, vpt->len, false);
307 src_type = FR_TYPE_STRING;
308 break;
309
310 case TMPL_TYPE_EXEC:
311 {
312 RDEBUG4("EXPAND TMPL EXEC");
313 if (!buff) {
314 fr_strerror_const("Missing expansion buffer for EXEC");
315 return -1;
316 }
317
318 if (radius_exec_program_legacy((char *)buff, bufflen, request, vpt->name, NULL,
319 true, false, fr_time_delta_from_sec(EXEC_TIMEOUT)) != 0) return -1;
320 fr_value_box_strdup_shallow(&value_to_cast, NULL, (char *)buff, true);
321 src_type = FR_TYPE_STRING;
322 }
323 break;
324
325 case TMPL_TYPE_XLAT:
326 {
327 RDEBUG4("EXPAND TMPL XLAT PARSED");
328
329 /* No EXPAND <xlat> here as the xlat code does it */
330
331 if (!buff) {
332 fr_strerror_const("Missing expansion buffer for XLAT_STRUCT");
333 return -1;
334 }
335 /* Error in expansion, this is distinct from zero length expansion */
336 slen = xlat_eval_compiled((char *)buff, bufflen, request, tmpl_xlat(vpt), NULL, NULL);
337 if (slen < 0) return slen;
338
339 fr_value_box_bstrndup_shallow(&value_to_cast, NULL, (char *)buff, slen, true);
340 src_type = FR_TYPE_STRING;
341 }
342 break;
343
344 case TMPL_TYPE_ATTR:
345 {
346 int ret;
347
348 RDEBUG4("EXPAND TMPL ATTR");
349 ret = tmpl_find_vp(&vp, request, vpt);
350 if (ret < 0) return -2;
351
352 to_cast = &vp->data;
353 src_type = vp->vp_type;
354 }
355 break;
356
357 case TMPL_TYPE_DATA:
358 RDEBUG4("EXPAND TMPL DATA");
359 to_cast = tmpl_value(vpt);
360 src_type = tmpl_value_type(vpt);
361 break;
362
363 /*
364 * We should never be expanding these.
365 */
367 case TMPL_TYPE_NULL:
371 case TMPL_TYPE_REGEX:
375 case TMPL_TYPE_MAX:
376 fr_assert(0);
377 return -1;
378 }
379
380 /*
381 * Deal with casts.
382 */
383 switch (src_type) {
384 case FR_TYPE_STRING:
385 switch (dst_type) {
386 case FR_TYPE_STRING:
387 case FR_TYPE_OCTETS:
388 from_cast = to_cast;
389 break;
390
391 default:
392 break;
393 }
394 break;
395
396 case FR_TYPE_OCTETS:
397 switch (dst_type) {
398 /*
399 * Need to use the expansion buffer for this conversion as
400 * we need to add a \0 terminator.
401 */
402 case FR_TYPE_STRING:
403 if (!buff) {
404 fr_strerror_const("Missing expansion buffer for octet->string cast");
405 return -1;
406 }
407 if (bufflen <= to_cast->vb_length) {
408 fr_strerror_printf("Expansion buffer too small. "
409 "Have %zu bytes, need %zu bytes", bufflen,
410 to_cast->vb_length + 1);
411 return -1;
412 }
413 memcpy(buff, to_cast->vb_octets, to_cast->vb_length);
414 buff[to_cast->vb_length] = '\0';
415
416 fr_value_box_bstrndup_shallow(&value_from_cast, NULL,
417 (char *)buff, to_cast->vb_length, true);
418 break;
419
420 /*
421 * Just copy the pointer. Length does not include \0.
422 */
423 case FR_TYPE_OCTETS:
424 from_cast = to_cast;
425 break;
426
427 default:
428 break;
429 }
430 break;
431
432 default:
433 {
434 int ret;
435 TALLOC_CTX *ctx;
436
437 /*
438 * Same type, just set from_cast to to_cast and copy the value.
439 */
440 if (src_type == dst_type) {
441 from_cast = to_cast;
442 break;
443 }
444
445 MEM(ctx = talloc_new(request));
446
447 from_cast = &value_from_cast;
448
449 /*
450 * Data type conversion...
451 */
452 ret = fr_value_box_cast(ctx, &value_from_cast, dst_type, NULL, to_cast);
453 if (ret < 0) goto error;
454
455
456 /*
457 * For the dynamic types we need to copy the output
458 * to the buffer. Really we need a version of fr_value_box_cast
459 * that works with buffers, but it's not a high priority...
460 */
461 switch (dst_type) {
462 case FR_TYPE_STRING:
463 if (!buff) {
464 fr_strerror_const("Missing expansion buffer to store cast output");
465 error:
466 talloc_free(ctx);
467 return -1;
468 }
469 if (from_cast->vb_length >= bufflen) {
470 fr_strerror_printf("Expansion buffer too small. "
471 "Have %zu bytes, need %zu bytes", bufflen,
472 from_cast->vb_length + 1);
473 goto error;
474 }
475 memcpy(buff, from_cast->vb_strvalue, from_cast->vb_length);
476 buff[from_cast->vb_length] = '\0';
477
478 fr_value_box_bstrndup_shallow(&value_from_cast, NULL,
479 (char *)buff, from_cast->vb_length, from_cast->tainted);
480 break;
481
482 case FR_TYPE_OCTETS:
483 if (!buff) {
484 fr_strerror_const("Missing expansion buffer to store cast output");
485 goto error;
486 }
487 if (from_cast->vb_length > bufflen) {
488 fr_strerror_printf("Expansion buffer too small. "
489 "Have %zu bytes, need %zu bytes", bufflen, from_cast->vb_length);
490 goto error;
491 }
492 memcpy(buff, from_cast->vb_octets, from_cast->vb_length);
493 fr_value_box_memdup_shallow(&value_from_cast, NULL,
494 buff, from_cast->vb_length, from_cast->tainted);
495 break;
496
497 default:
498 break;
499 }
500
501 talloc_free(ctx); /* Free any dynamically allocated memory from the cast */
502 }
503 }
504
505 RDEBUG4("Copying %zu bytes to %p from offset %zu",
507
508 fr_value_box_memcpy_out(out, from_cast);
509
510 return from_cast->vb_length;
511}
512
513/** Expand a template to a string, allocing a new buffer to hold the string
514 *
515 * The intended use of #tmpl_expand and #tmpl_aexpand is for modules to easily convert a #tmpl_t
516 * provided by the conf parser, into a usable value.
517 * The value returned should be raw and undoctored for #FR_TYPE_STRING and #FR_TYPE_OCTETS types,
518 * and the printable (string) version of the data for all others.
519 *
520 * This function will always duplicate values, whereas #tmpl_expand may return a pointer to an
521 * existing buffer.
522 *
523 * @note This function is used where raw string values are needed, which may mean the string
524 * returned may be binary data or contain unprintable chars. #fr_snprint or #fr_asprint should
525 * be used before using these values in debug statements. #is_printable can be used to check
526 * if the string only contains printable chars.
527 *
528 * @note The type (char or uint8_t) can be obtained with talloc_get_type, and may be used as a
529 * hint as to how to process or print the data.
530 *
531 * @param ctx to allocate new buffer in.
532 * @param out Where to write pointer to the new buffer.
533 * @param request Current request.
534 * @param vpt to expand. Must be one of the following types:
535 * - #TMPL_TYPE_DATA_UNRESOLVED
536 * - #TMPL_TYPE_EXEC
537 * - #TMPL_TYPE_XLAT
538 * - #TMPL_TYPE_ATTR
539 * - #TMPL_TYPE_DATA
540 * @param escape xlat escape function (only used for TMPL_TYPE_XLAT_UNRESOLVED_* types).
541 * @param escape_ctx xlat escape function data (only used for TMPL_TYPE_XLAT_UNRESOLVED_* types).
542 * @param dst_type FR_TYPE_* matching out pointer. @see tmpl_aexpand.
543 * @return
544 * - -1 on failure.
545 * - The length of data written to buff, or pointed to by out.
546 */
547ssize_t _tmpl_to_atype(TALLOC_CTX *ctx, void *out,
548 request_t *request,
549 tmpl_t const *vpt,
550 xlat_escape_legacy_t escape, void const *escape_ctx,
551 fr_type_t dst_type)
552{
553 fr_value_box_t *to_cast = NULL;
554 fr_value_box_t from_cast;
555
556 fr_pair_t *vp = NULL;
558 bool needs_dup = false;
559
560 ssize_t slen = -1;
561 int ret;
562
563 TALLOC_CTX *tmp_ctx = talloc_new(ctx);
564
566
567 switch (vpt->type) {
569 RDEBUG4("EXPAND TMPL DATA UNRESOLVED");
570
571 fr_value_box_bstrndup_shallow(&value, NULL, vpt->name, vpt->len, false);
572 to_cast = &value;
573 needs_dup = true;
574 break;
575
576 case TMPL_TYPE_EXEC:
577 {
578 char *buff;
579
580 RDEBUG4("EXPAND TMPL EXEC");
581
582 MEM(fr_value_box_bstr_alloc(tmp_ctx, &buff, &value, NULL, 1024, true));
583 if (radius_exec_program_legacy(buff, 1024, request, vpt->name, NULL,
584 true, false, fr_time_delta_from_sec(EXEC_TIMEOUT)) != 0) {
585 error:
586 talloc_free(tmp_ctx);
587 return slen;
588 }
589 fr_value_box_strtrim(tmp_ctx, &value);
590 to_cast = &value;
591 }
592 break;
593
595 {
596 fr_value_box_t tmp;
597 fr_type_t src_type = FR_TYPE_STRING;
598 char *result;
599
600 RDEBUG4("EXPAND TMPL XLAT");
601
602 /* Error in expansion, this is distinct from zero length expansion */
603 slen = xlat_aeval(tmp_ctx, &result, request, vpt->name, escape, escape_ctx);
604 if (slen < 0) goto error;
605
606 /*
607 * Undo any of the escaping that was done by the
608 * xlat expansion function.
609 *
610 * @fixme We need a way of signalling xlat not to escape things.
611 */
612 ret = fr_value_box_from_str(tmp_ctx, &tmp, src_type, NULL,
613 result, (size_t)slen,
614 NULL);
615 if (ret < 0) goto error;
616
617 fr_value_box_bstrndup_shallow(&value, NULL, tmp.vb_strvalue, tmp.vb_length, tmp.tainted);
618 to_cast = &value;
619 }
620 break;
621
622 case TMPL_TYPE_XLAT:
624 {
625 fr_value_box_t tmp;
626 fr_type_t src_type = FR_TYPE_STRING;
627 char *result;
628
629 RDEBUG4("EXPAND TMPL XLAT STRUCT");
630 /* No EXPAND xlat here as the xlat code does it */
631
632 /* Error in expansion, this is distinct from zero length expansion */
633 slen = xlat_aeval_compiled(tmp_ctx, &result, request, tmpl_xlat(vpt), escape, escape_ctx);
634 if (slen < 0) goto error;
635
636 /*
637 * Undo any of the escaping that was done by the
638 * xlat expansion function.
639 *
640 * @fixme We need a way of signalling xlat not to escape things.
641 */
642 ret = fr_value_box_from_str(tmp_ctx, &tmp, src_type, NULL,
643 result, (size_t)slen,
644 NULL);
645 if (ret < 0) goto error;
646
647 fr_value_box_bstrndup_shallow(&value, NULL, tmp.vb_strvalue, tmp.vb_length, tmp.tainted);
648 to_cast = &value;
649 }
650 break;
651
652 case TMPL_TYPE_ATTR:
653 RDEBUG4("EXPAND TMPL ATTR");
654
655 ret = tmpl_find_vp(&vp, request, vpt);
656 if (ret < 0) {
657 talloc_free(tmp_ctx);
658 return -2;
659 }
660
661 fr_assert(vp);
662
663 to_cast = &vp->data;
664 switch (to_cast->type) {
665 case FR_TYPE_STRING:
666 case FR_TYPE_OCTETS:
667 fr_assert(to_cast->datum.ptr);
668 needs_dup = true;
669 break;
670
671 default:
672 break;
673 }
674 break;
675
676 case TMPL_TYPE_DATA:
677 {
678 RDEBUG4("EXPAND TMPL DATA");
679
680 to_cast = UNCONST(fr_value_box_t *, tmpl_value(vpt));
681 switch (to_cast->type) {
682 case FR_TYPE_STRING:
683 case FR_TYPE_OCTETS:
684 fr_assert(to_cast->datum.ptr);
685 needs_dup = true;
686 break;
687
688 default:
689 break;
690 }
691 }
692 break;
693
694 /*
695 * We should never be expanding these.
696 */
698 case TMPL_TYPE_NULL:
700 case TMPL_TYPE_REGEX:
704 case TMPL_TYPE_MAX:
705 fr_assert(0);
706 goto error;
707 }
708
709 /*
710 * Special case where we just copy the boxed value
711 * directly instead of casting it.
712 */
713 if (dst_type == FR_TYPE_VALUE_BOX) {
714 fr_value_box_t **vb_out = (fr_value_box_t **)out;
715 fr_type_t cast_type;
716
717 MEM(*vb_out = fr_value_box_alloc_null(ctx));
718
719 cast_type = tmpl_rules_cast(vpt);
720 if (cast_type == FR_TYPE_NULL) {
721 ret = needs_dup ? fr_value_box_copy(*vb_out, *vb_out, to_cast) : fr_value_box_steal(*vb_out, *vb_out, to_cast);
722 } else {
723 ret = fr_value_box_cast(ctx, *vb_out, cast_type, NULL, to_cast);
724 }
725
726 talloc_free(tmp_ctx);
727 if (ret < 0) {
728 RPEDEBUG("Failed copying data to output box");
729 TALLOC_FREE(*vb_out);
730 return -1;
731 }
732 VALUE_BOX_VERIFY(*vb_out);
733 return 0;
734 }
735
736 /*
737 * Don't dup the buffers unless we need to.
738 */
739 if ((to_cast->type != dst_type) || needs_dup) {
740 ret = fr_value_box_cast(ctx, &from_cast, dst_type, NULL, to_cast);
741 if (ret < 0) goto error;
742 } else {
743 switch (to_cast->type) {
744 case FR_TYPE_OCTETS:
745 case FR_TYPE_STRING:
746 /*
747 * Ensure we don't free the output buffer when the
748 * tmp_ctx is freed.
749 */
750 if (value.datum.ptr && (talloc_parent(value.datum.ptr) == tmp_ctx)) {
751 (void)talloc_reparent(tmp_ctx, ctx, value.datum.ptr);
752 }
753 break;
754
755 default:
756 break;
757 }
758 fr_value_box_copy_shallow(NULL, &from_cast, to_cast);
759 }
760
761 RDEBUG4("Copying %zu bytes to %p from offset %zu",
762 fr_value_box_field_sizes[dst_type], *((void **)out), fr_value_box_offsets[dst_type]);
763
764 fr_value_box_memcpy_out(out, &from_cast);
765
766 /*
767 * Frees any memory allocated for temporary buffers
768 * in this function.
769 */
770 talloc_free(tmp_ctx);
771
772 return from_cast.vb_length;
773}
774
775/** Copy pairs matching a #tmpl_t in the current #request_t
776 *
777 * @param ctx to allocate new #fr_pair_t in.
778 * @param out Where to write the copied #fr_pair_t (s).
779 * @param request The current #request_t.
780 * @param vpt specifying the #fr_pair_t type or list to copy.
781 * Must be one of the following types:
782 * - #TMPL_TYPE_ATTR
783 * @return
784 * - -1 if no matching #fr_pair_t could be found.
785 * - -2 if list could not be found (doesn't exist in current #request_t).
786 * - -3 if context could not be found (no parent #request_t available).
787 * - -4 on memory allocation error.
788 */
789int tmpl_copy_pairs(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt)
790{
791 fr_pair_t *vp;
792 fr_dcursor_t from;
794 int err;
795
797
799
800 for (vp = tmpl_dcursor_init(&err, NULL, &cc, &from, request, vpt);
801 vp;
802 vp = fr_dcursor_next(&from)) {
803 vp = fr_pair_copy(ctx, vp);
804 if (!vp) {
806 fr_strerror_const("Out of memory");
807 err = -4;
808 break;
809 }
811 }
813
814 return err;
815}
816
817
818/** Copy children of pairs matching a #tmpl_t in the current #request_t
819 *
820 * @param ctx to allocate new #fr_pair_t in.
821 * @param out Where to write the copied #fr_pair_t (s).
822 * @param request The current #request_t.
823 * @param vpt specifying the #fr_pair_t type or list to copy.
824 * Must be one of the following types:
825 * - #TMPL_TYPE_ATTR
826 * @return
827 * - -1 if no matching #fr_pair_t could be found.
828 * - -2 if list could not be found (doesn't exist in current #request_t).
829 * - -3 if context could not be found (no parent #request_t available).
830 * - -4 on memory allocation error.
831 */
832int tmpl_copy_pair_children(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, tmpl_t const *vpt)
833{
834 fr_pair_t *vp;
835 fr_dcursor_t from;
837 int err;
838
840
842
844
845 for (vp = tmpl_dcursor_init(&err, NULL, &cc, &from, request, vpt);
846 vp;
847 vp = fr_dcursor_next(&from)) {
848 switch (vp->vp_type) {
850 if (fr_pair_list_copy(ctx, out, &vp->vp_group) < 0) {
851 err = -4;
852 goto done;
853 }
854 break;
855
856 default:
857 continue;
858 }
859 }
860done:
862
863 return err;
864}
865
866
867/** Returns the first VP matching a #tmpl_t
868 *
869 * @param[out] out where to write the retrieved vp.
870 * @param[in] request The current #request_t.
871 * @param[in] vpt specifying the #fr_pair_t type to find.
872 * Must be one of the following types:
873 * - #TMPL_TYPE_ATTR
874 * @return
875 * - 0 on success (found matching #fr_pair_t).
876 * - -1 if no matching #fr_pair_t could be found.
877 * - -2 if list could not be found (doesn't exist in current #request_t).
878 * - -3 if context could not be found (no parent #request_t available).
879 */
881{
882 fr_dcursor_t cursor;
884 fr_pair_t *vp;
885 int err;
886
888
889 vp = tmpl_dcursor_init(&err, request, &cc, &cursor, request, vpt);
891
892 if (out) *out = vp;
893
894 return err;
895}
896
897/** Returns the first VP matching a #tmpl_t, or if no VPs match, creates a new one.
898 *
899 * @param[out] out where to write the retrieved or created vp.
900 * @param[in] request The current #request_t.
901 * @param[in] vpt specifying the #fr_pair_t type to retrieve or create. Must be #TMPL_TYPE_ATTR.
902 * @return
903 * - 1 on success a pair was created.
904 * - 0 on success a pair was found.
905 * - -1 if a new #fr_pair_t couldn't be found or created.
906 * - -2 if list could not be found (doesn't exist in current #request_t).
907 * - -3 if context could not be found (no parent #request_t available).
908 */
910{
911 fr_dcursor_t cursor;
913 fr_pair_t *vp;
914 int err;
915
918
919 *out = NULL;
920
921 vp = tmpl_dcursor_init(&err, NULL, &cc, &cursor, request, vpt);
923
924 switch (err) {
925 case 0:
926 *out = vp;
927 return 0;
928
929 case -1:
930 {
931 TALLOC_CTX *ctx;
933
934 tmpl_pair_list_and_ctx(ctx, head, request, tmpl_request(vpt), tmpl_list(vpt));
935 if (!head) return -1;
936
937 if (pair_append_by_tmpl_parent(ctx, &vp, head, vpt, true) < 0) return -1;
938
939 *out = vp;
940 }
941 return 1;
942
943 default:
944 return err;
945 }
946}
947
948/** Allocate and insert a leaf vp from a tmpl_t, building the parent vps if needed.
949 *
950 * This is the simple case - just add a vp at the first place where
951 * the parents exist, or create the parents, with no attempt to handle filters.
952 *
953 * It is functionally equivalent to fr_pair_append_by_da_parent() but
954 * uses a tmpl_t to build the nested structure rather than a fr_dict_attr_t.
955 *
956 * @param[in] ctx to allocate new pair(s) in
957 * @param[out] out Leaf pair we allocated.
958 * @param[in] list to insert into.
959 * @param[in] vpt tmpl representing the attribute to add.
960 * @param[in] skip_list skip list attr ref at the head of the tmpl.
961 * @return
962 * - 0 on success.
963 * - -1 on failure.
964 */
965int pair_append_by_tmpl_parent(TALLOC_CTX *ctx, fr_pair_t **out, fr_pair_list_t *list, tmpl_t const *vpt, bool skip_list)
966{
967 fr_pair_t *vp = NULL;
968 TALLOC_CTX *pair_ctx = ctx;
969 tmpl_attr_t *ar, *leaf;
970 tmpl_attr_list_head_t const *ar_list = &vpt->data.attribute.ar;
971
972 if (!tmpl_is_attr(vpt)) {
973 error:
974 *out = NULL;
975 return -1;
976 }
977
978 leaf = tmpl_attr_list_tail(ar_list);
979 ar = tmpl_attr_list_head(ar_list);
980 if (!ar) goto error;
981 if (skip_list && tmpl_attr_is_list_attr(ar)) ar = tmpl_attr_list_next(ar_list, ar);
982
983 /*
984 * Walk down the tmpl ar stack looking for candidate parent
985 * attributes and then allocating the leaf.
986 */
987 while (true) {
988 if (unlikely(!ar)) goto error;
989 /*
990 * We're not at the leaf, look for a potential parent
991 */
992 if (ar != leaf) {
993 vp = fr_pair_find_by_da(list, NULL, ar->da);
994 /*
995 * HACK - Pretend we didn't see this stupid key field
996 *
997 * If we don't have this, the code creates a key pair
998 * and then horribly mangles its data by adding children
999 * to it.
1000 *
1001 * We just skip one level down an don't create or update
1002 * the key pair.
1003 */
1004 if (vp && fr_dict_attr_is_key_field(ar->da) && fr_type_is_leaf(vp->data.type)) {
1005 ar = tmpl_attr_list_next(ar_list, ar);
1006 continue;
1007 }
1008 }
1009 /*
1010 * Nothing found, create the pair
1011 */
1012 if (!vp) {
1013 if (fr_pair_append_by_da(pair_ctx, &vp, list, ar->da) < 0) goto error;
1014 }
1015
1016 /*
1017 * We're at the leaf, return
1018 */
1019 if (ar == leaf) {
1020 *out = vp;
1021 return 0;
1022 }
1023
1024 /*
1025 * Prepare for next level
1026 */
1027 list = &vp->vp_group;
1028 pair_ctx = vp;
1029 vp = NULL;
1030 ar = tmpl_attr_list_next(ar_list, ar);
1031 }
1032}
1033
1034/** Insert a value-box to a list, with casting.
1035 *
1036 * @param list to append to
1037 * @param box box to cast / append
1038 * @param vpt tmpl with cast.
1039 * @return
1040 * - <0 for "cast failed"
1041 * - 0 for success
1042 */
1043int tmpl_value_list_insert_tail(fr_value_box_list_t *list, fr_value_box_t *box, tmpl_t const *vpt)
1044{
1046 (box->type == tmpl_rules_cast(vpt))) {
1047 fr_value_box_list_insert_tail(list, box);
1048 return 0;
1049 }
1050
1051 if (fr_value_box_cast_in_place(box, box, tmpl_rules_cast(vpt), tmpl_rules_enumv(vpt)) < 0) return -1;
1052
1053 fr_value_box_list_insert_tail(list, box);
1055 return 0;
1056}
1057
1058/** Gets the value of a real or virtual attribute
1059 *
1060 * @param[in] ctx to allocate boxed value, and buffers in.
1061 * @param[out] out Where to write the boxed value.
1062 * @param[in] request The current request.
1063 * @param[in] vpt Representing the attribute.
1064 * @return
1065 * - <0 we failed getting a value for the attribute.
1066 * - 0 we successfully evaluated the tmpl
1067 */
1068int tmpl_eval_pair(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
1069{
1070 fr_pair_t *vp = NULL;
1072
1073 fr_dcursor_t cursor;
1075
1076 int ret = 0;
1077 fr_value_box_list_t list;
1078
1080
1081 fr_value_box_list_init(&list);
1082
1083 /*
1084 * See if we're dealing with an attribute in the request
1085 *
1086 * This allows users to manipulate virtual attributes as if
1087 * they were real ones.
1088 */
1089 vp = tmpl_dcursor_init(NULL, NULL, &cc, &cursor, request, vpt);
1090
1091 /*
1092 * We didn't find the VP in a list, check to see if it's
1093 * virtual. This allows the caller to "realize" the
1094 * attribute, and we then prefer the realized version to
1095 * the virtual one.
1096 */
1097 if (!vp) {
1098 /*
1099 * Zero count.
1100 */
1103 if (!value) {
1104 oom:
1105 fr_strerror_const("Out of memory");
1106 ret = -1;
1107 goto fail;
1108 }
1109 value->datum.int32 = 0;
1110 fr_value_box_list_insert_tail(&list, value);
1111 } /* Fall through to being done */
1112
1113 goto done;
1114 }
1115
1116 switch (tmpl_attr_tail_num(vpt)) {
1117 /*
1118 * Return a count of the VPs.
1119 */
1120 case NUM_COUNT:
1121 {
1122 uint32_t count = 0;
1123
1124 while (vp != NULL) {
1125 count++;
1126 vp = fr_dcursor_next(&cursor);
1127 }
1128
1130 if (!value) goto oom;
1131 value->datum.uint32 = count;
1132 fr_value_box_list_insert_tail(&list, value);
1133 break;
1134 }
1135
1136 /*
1137 * Output multiple #value_box_t, one per attribute.
1138 */
1139 case NUM_ALL:
1140 /*
1141 * Loop over all matching #fr_value_pair
1142 * shallow copying buffers.
1143 */
1144 while (vp != NULL) {
1145 if (fr_type_is_structural(vp->vp_type)) {
1147 if (!value) goto oom;
1148
1149 if (fr_pair_list_copy_to_box(value, &vp->vp_group) < 0) {
1151 goto oom;
1152 }
1153
1154 } else {
1155 value = fr_value_box_alloc(ctx, vp->data.type, vp->da);
1156 if (!value) goto oom;
1157 fr_value_box_copy(value, value, &vp->data);
1158 }
1159
1160 fr_value_box_list_insert_tail(&list, value);
1161 vp = fr_dcursor_next(&cursor);
1162 }
1163 break;
1164
1165 default:
1166 if (!fr_type_is_leaf(vp->vp_type)) {
1167 fr_strerror_const("Invalid data type for evaluation");
1168 goto fail;
1169 }
1170
1171 value = fr_value_box_alloc(ctx, vp->data.type, vp->da);
1172 if (!value) goto oom;
1173
1174 fr_value_box_copy(value, value, &vp->data); /* Also dups taint */
1175 fr_value_box_list_insert_tail(&list, value);
1176 break;
1177 }
1178
1179done:
1180 /*
1181 * Evaluate casts if necessary.
1182 */
1183 if (ret == 0) {
1184 if (tmpl_eval_cast_in_place(&list, request, vpt) < 0) {
1185 fr_value_box_list_talloc_free(&list);
1186 ret = -1;
1187 goto fail;
1188 }
1189
1190 fr_value_box_list_move(out, &list);
1191 }
1192
1193fail:
1194 tmpl_dcursor_clear(&cc);
1196 return ret;
1197}
1198
1199
1200/** Gets the value of a tmpl
1201 *
1202 * The result is returned "raw". The caller must do any escaping it desires.
1203 *
1204 * @param[in] ctx to allocate boxed value, and buffers in.
1205 * @param[out] out Where to write the boxed value.
1206 * @param[in] request The current request.
1207 * @param[in] vpt Representing the tmpl
1208 * @return
1209 * - <0 we failed getting a value for the tmpl
1210 * - 0 we successfully evaluated the tmpl
1211 */
1212int tmpl_eval(TALLOC_CTX *ctx, fr_value_box_list_t *out, request_t *request, tmpl_t const *vpt)
1213{
1214 char *p;
1216 fr_value_box_list_t list;
1217
1219 fr_strerror_const("Cannot evaluate unresolved tmpl");
1220 return -1;
1221 }
1222
1223 if (tmpl_async_required(vpt)) {
1224 fr_strerror_const("Cannot statically evaluate asynchronous expansions");
1225 return -1;
1226 }
1227
1228 if (tmpl_contains_regex(vpt)) {
1229 fr_strerror_const("Cannot statically evaluate regular expression");
1230 return -1;
1231 }
1232
1233 if (tmpl_is_attr(vpt)) {
1234 return tmpl_eval_pair(ctx, out, request, vpt);
1235 }
1236
1237 if (tmpl_is_data(vpt)) {
1239
1240 fr_value_box_copy(value, value, tmpl_value(vpt)); /* Also dups taint */
1241 goto done;
1242 }
1243
1245
1246 /*
1247 * @todo - respect escaping functions. But the sync
1248 * escaping uses a different method than the async ones.
1249 * And we then also need to escape the output of
1250 * tmpl_eval_pair(), too.
1251 */
1253 if (tmpl_aexpand(value, &p, request, vpt, NULL, NULL) < 0) {
1255 return -1;
1256 }
1257 fr_value_box_bstrndup_shallow(value, NULL, p, talloc_array_length(p) - 1, true);
1258
1259 /*
1260 * Cast the results if necessary.
1261 */
1262done:
1263 fr_value_box_list_init(&list);
1264 fr_value_box_list_insert_tail(&list, value);
1265
1266 if (tmpl_eval_cast_in_place(&list, request, vpt) < 0) {
1267 fr_value_box_list_talloc_free(&list);
1268 return -1;
1269 }
1270
1271 fr_value_box_list_move(out, &list);
1273
1274 return 0;
1275}
1276
1277/** Allocate a uctx for an escaping function
1278 *
1279 * @param[in] request The current request.
1280 * @param[in] escape Describing how to escape tmpl data.
1281 *
1282 * @return the uctx to pass to the escape function.
1283 */
1284static inline void *tmpl_eval_escape_uctx_alloc(request_t *request, tmpl_escape_t const *escape)
1285{
1286 switch (escape->uctx.type) {
1288 return UNCONST(void *, escape->uctx.ptr);
1289
1291 {
1292 void *uctx;
1293
1294 fr_assert_msg(escape->uctx.size > 0, "TMPL_ESCAPE_UCTX_ALLOC must specify uctx.size > 0");
1295 MEM(uctx = talloc_zero_array(NULL, uint8_t, escape->uctx.size));
1296 if (escape->uctx.talloc_type) talloc_set_type(uctx, escape->uctx.talloc_type);
1297 return uctx;
1298 }
1299
1301 fr_assert_msg(escape->uctx.func.alloc, "TMPL_ESCAPE_UCTX_ALLOC_FUNC must specify a non-null alloc.func");
1302 return escape->uctx.func.alloc(request, escape->uctx.func.uctx);
1303
1304 default:
1305 fr_assert_msg(0, "Unknown escape uctx type %u", escape->uctx.type);
1306 return NULL;
1307 }
1308}
1309
1310/** Free a uctx for an escaping function
1311 *
1312 * @param[in] escape Describing how to escape tmpl data.
1313 * @param[in] uctx The uctx to free.
1314 */
1315static inline void tmpl_eval_escape_uctx_free(tmpl_escape_t const *escape, void *uctx)
1316{
1317 switch (escape->uctx.type) {
1319 return;
1320
1322 talloc_free(uctx);
1323 return;
1324
1326 if (escape->uctx.func.free) escape->uctx.func.free(uctx);
1327 return;
1328 }
1329}
1330
1331/** Casts a value or list of values according to the tmpl
1332 *
1333 * @param[in,out] list Where to write the boxed value.
1334 * @param[in] request The current request.
1335 * @param[in] vpt Representing the attribute.
1336 * @return
1337 * - <0 the cast failed
1338 * - 0 we successfully evaluated the tmpl
1339 */
1340int tmpl_eval_cast_in_place(fr_value_box_list_t *list, request_t *request, tmpl_t const *vpt)
1341{
1343 bool did_concat = false;
1344 void *uctx = NULL;
1345
1346 if (fr_type_is_structural(cast)) {
1347 fr_strerror_printf("Cannot cast to structural type '%s'", fr_type_to_str(cast));
1348 return -1;
1349 }
1350
1351 /*
1352 * Quoting around the tmpl means everything
1353 * needs to be concatenated, either as a string
1354 * or octets string.
1355 */
1356 switch (vpt->quote) {
1361 {
1362 ssize_t slen;
1363 fr_value_box_t *vb;
1364
1365 vb = fr_value_box_list_head(list);
1366 if (!vb) return 0;
1367
1369 uctx = tmpl_eval_escape_uctx_alloc(request, &vpt->rules.escape);
1370 /*
1371 * Sets escaped values, so boxes don't get re-escaped
1372 */
1373 if (unlikely(fr_value_box_list_escape_in_place(list, &vpt->rules.escape.box_escape, uctx) < 0)) {
1374 error:
1375 tmpl_eval_escape_uctx_free(&vpt->rules.escape, uctx);
1376 return -1;
1377 }
1378 }
1379
1381 FR_VALUE_BOX_LIST_FREE_BOX, true, SIZE_MAX);
1382 if (slen < 0) goto error;
1384
1385 /*
1386 * If there's no cast, or it's a cast to
1387 * a string, we're done!
1388 *
1389 * Otherwise we now need to re-cast the
1390 * result.
1391 */
1392 if (fr_type_is_null(cast) || fr_type_is_string(cast)) {
1393 success:
1394 tmpl_eval_escape_uctx_free(&vpt->rules.escape, uctx);
1395 return 0;
1396 }
1397
1398 did_concat = true;
1399 }
1400 break;
1401
1402 default:
1403 break;
1404 }
1405
1406 if (fr_type_is_null(cast)) goto success;
1407
1408 /*
1409 * Quoting above handled all concatenation,
1410 * we now need to handle potentially
1411 * multivalued lists.
1412 */
1414 if (fr_value_box_cast_in_place(vb, vb, cast, NULL) < 0) goto error;
1415 }}
1416
1417 /*
1418 * ...and finally, apply the escape function
1419 * if necessary. This is done last so that
1420 * the escape function gets boxes of the type
1421 * it expects.
1422 */
1424 uctx = tmpl_eval_escape_uctx_alloc(request, &vpt->rules.escape);
1425 if (unlikely(fr_value_box_list_escape_in_place(list, &vpt->rules.escape.box_escape, uctx) < 0)) goto error;
1426 }
1427
1428 /*
1429 * If there's no escape function, but there is
1430 * a safe_for value, mark all the boxes up with
1431 * this value.
1432 *
1433 * This is mostly useful for call_env usage in
1434 * modules where certain values are implicitly safe
1435 * for consumption, like SQL statements in the SQL
1436 * module.
1437 */
1438 if (!vpt->rules.escape.box_escape.func && vpt->rules.escape.box_escape.safe_for) {
1439 fr_value_box_list_mark_safe_for(list, vpt->rules.escape.box_escape.safe_for);
1440 }
1441
1443
1445}
1446
1448{
1450
1451 if (tmpl_is_data(vpt)) return tmpl_value_type(vpt);
1452
1453 if (tmpl_is_attr(vpt)) return tmpl_attr_tail_da(vpt)->type;
1454
1456
1457 return FR_TYPE_NULL; /* can't determine it */
1458}
1459
1460
1461static int _tmpl_global_free(UNUSED void *uctx)
1462{
1464
1465 return 0;
1466}
1467
1468static int _tmpl_global_init(UNUSED void *uctx)
1469{
1470 fr_dict_attr_t *da;
1471
1472 if (fr_dict_autoload(tmpl_dict) < 0) {
1473 PERROR("%s", __FUNCTION__);
1474 return -1;
1475 }
1476
1478 fr_assert(da != NULL);
1479
1480 da->type = FR_TYPE_NULL;
1481 tmpl_attr_unspec = da;
1482
1483 return 0;
1484}
1485
1487{
1488 int ret;
1489
1490 fr_atexit_global_once_ret(&ret, _tmpl_global_init, _tmpl_global_free, NULL);
1491
1492 return 0;
1493}
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:288
#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:860
static fr_slen_t err
Definition dict.h:831
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:585
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2407
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:285
#define fr_dict_autoload(_to_load)
Definition dict.h:857
#define fr_dict_attr_is_key_field(_da)
Definition dict.h:157
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:284
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_OCTETS
Raw octets.
@ 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:1468
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:2321
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:695
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:1347
fr_pair_t * fr_pair_copy(TALLOC_CTX *ctx, fr_pair_t const *vp)
Copy a single valuepair.
Definition pair.c:491
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:2356
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:80
fr_dict_attr_t const * request_attr_request
Definition request.c:45
fr_dict_attr_t const * request_attr_control
Definition request.c:47
fr_dict_attr_t const * request_attr_local
Definition request.c:49
fr_dict_attr_t const * request_attr_state
Definition request.c:48
fr_dict_attr_t const * request_attr_reply
Definition request.c:46
static int16_t tmpl_attr_tail_num(tmpl_t const *vpt)
Return the last attribute reference's attribute number.
Definition tmpl.h:890
#define TMPL_VERIFY(_vpt)
Definition tmpl.h:966
#define tmpl_is_xlat(vpt)
Definition tmpl.h:215
#define tmpl_rules_enumv(_tmpl)
Definition tmpl.h:948
#define tmpl_value(_tmpl)
Definition tmpl.h:942
#define tmpl_contains_regex(vpt)
Definition tmpl.h:231
#define tmpl_is_attr(vpt)
Definition tmpl.h:213
#define NUM_ALL
Definition tmpl.h:396
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:935
static fr_dict_attr_t const * tmpl_list(tmpl_t const *vpt)
Definition tmpl.h:909
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:684
#define tmpl_rules_cast(_tmpl)
Definition tmpl.h:947
@ TMPL_TYPE_REGEX_UNCOMPILED
Regex where compilation is possible but hasn't been performed yet.
Definition tmpl.h:162
@ TMPL_TYPE_MAX
Marker for the last tmpl type.
Definition tmpl.h:203
@ TMPL_TYPE_ATTR_UNRESOLVED
An attribute reference that we couldn't resolve but looked valid.
Definition tmpl.h:189
@ TMPL_TYPE_ATTR
Reference to one or more attributes.
Definition tmpl.h:146
@ TMPL_TYPE_XLAT
Pre-parsed xlat expansion.
Definition tmpl.h:150
@ TMPL_TYPE_NULL
Has no value.
Definition tmpl.h:138
@ TMPL_TYPE_EXEC
Callout to an external script or program.
Definition tmpl.h:154
@ TMPL_TYPE_REGEX_XLAT_UNRESOLVED
A regular expression with unresolved xlat functions or attribute references.
Definition tmpl.h:201
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:142
@ TMPL_TYPE_REGEX
Compiled (and possibly JIT'd) regular expression.
Definition tmpl.h:158
@ TMPL_TYPE_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:183
@ TMPL_TYPE_XLAT_UNRESOLVED
A xlat expansion with unresolved xlat functions or attribute references.
Definition tmpl.h:197
@ TMPL_TYPE_REGEX_XLAT
A regex containing xlat expansions.
Definition tmpl.h:166
@ TMPL_TYPE_EXEC_UNRESOLVED
An exec with unresolved xlat function or attribute references.
Definition tmpl.h:193
@ TMPL_TYPE_UNINITIALISED
Uninitialised.
Definition tmpl.h:134
#define NUM_COUNT
Definition tmpl.h:397
#define tmpl_pair_list_and_ctx(_ctx, _head, _request, _ref, _list)
Determine the correct context and list head.
Definition tmpl.h:998
#define tmpl_is_data(vpt)
Definition tmpl.h:211
static fr_slen_t vpt
Definition tmpl.h:1274
#define tmpl_value_type(_tmpl)
Definition tmpl.h:944
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:806
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:920
@ 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:1067
#define tmpl_needs_resolving(vpt)
Definition tmpl.h:228
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
return count
Definition module.c:163
fr_pair_t * vp
An element in a list of nested attribute references.
Definition tmpl.h:435
fr_dict_attr_t const *_CONST da
Resolved dictionary attribute.
Definition tmpl.h:439
Define manipulation functions for the attribute reference list.
Definition tmpl.h:476
tmpl_request_ref_t _CONST request
Definition tmpl.h:480
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
Functions which we wish were included in the standard talloc distribution.
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:880
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:146
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:1043
fr_dict_attr_t const * tmpl_attr_unspec
Placeholder attribute for uses of unspecified attribute references.
Definition tmpl_eval.c:61
static int _tmpl_global_free(UNUSED void *uctx)
Definition tmpl_eval.c:1461
static fr_dict_t const * dict_freeradius
Definition tmpl_eval.c:48
static fr_dict_t const * dict_radius
Definition tmpl_eval.c:49
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:169
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:1212
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:1340
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:76
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:116
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:547
fr_type_t tmpl_data_type(tmpl_t const *vpt)
Definition tmpl_eval.c:1447
int tmpl_global_init(void)
Definition tmpl_eval.c:1486
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:1068
goto success
Definition tmpl_eval.c:1444
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:832
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:282
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:789
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:1315
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:909
static int _tmpl_global_init(UNUSED void *uctx)
Definition tmpl_eval.c:1468
fr_dict_autoload_t tmpl_dict[]
Definition tmpl_eval.c:52
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:1284
fr_type_t tmpl_expanded_type(tmpl_t const *vpt)
Return the native data type of the expression.
Definition tmpl_eval.c:209
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:965
@ 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:1522
static fr_slen_t head
Definition xlat.h:418
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:1539
ssize_t xlat_aeval(TALLOC_CTX *ctx, char **out, request_t *request, char const *fmt, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1530
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_structural(_x)
Definition types.h:371
#define fr_type_is_string(_x)
Definition types.h:327
#define FR_TYPE_STRUCTURAL
Definition types.h:296
#define fr_type_is_null(_x)
Definition types.h:326
#define fr_type_is_leaf(_x)
Definition types.h:372
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:433
size_t const fr_value_box_field_sizes[]
How many bytes wide each of the value data fields are.
Definition value.c:149
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:3983
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:3370
size_t const fr_value_box_offsets[]
Where the value starts in the fr_value_box_t.
Definition value.c:189
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:3759
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:5949
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:3591
void fr_value_box_memdup_shallow(fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Assign a buffer to a box, but don't copy it.
Definition value.c:4499
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:3864
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:5246
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:6290
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:4066
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:4101
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:3888
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:4262
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:5734
@ FR_VALUE_BOX_LIST_FREE_BOX
Free each processed box.
Definition value.h:229
#define fr_value_box_list_foreach_safe(_list_head, _iter)
Definition value.h:218
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:632
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:779
#define vb_length
Definition value.h:283
#define FR_VALUE_BOX_INITIALISER_NULL(_vb)
A static initialiser for stack/globally allocated boxes.
Definition value.h:499
#define VALUE_BOX_VERIFY(_x)
Definition value.h:1304
#define VALUE_BOX_LIST_VERIFY(_x)
Definition value.h:1305
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:643
static size_t char ** out
Definition value.h:1012