The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
xlat_builtin.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: 81653076243971dcb57c7bb8d3310cc8429bdc5e $
19 *
20 * @file xlat_builtin.c
21 * @brief String expansion ("translation"). Baked in expansions.
22 *
23 * @copyright 2000,2006 The FreeRADIUS server project
24 * @copyright 2000 Alan DeKok (aland@freeradius.org)
25 */
26RCSID("$Id: 81653076243971dcb57c7bb8d3310cc8429bdc5e $")
27
28/**
29 * @defgroup xlat_functions xlat expansion functions
30 */
31#include <freeradius-devel/server/base.h>
32#include <freeradius-devel/server/tmpl_dcursor.h>
33#include <freeradius-devel/unlang/xlat_priv.h>
34
35#include <freeradius-devel/io/test_point.h>
36
37#include <freeradius-devel/util/base16.h>
38
39#ifdef HAVE_OPENSSL_EVP_H
40# include <freeradius-devel/tls/openssl_user_macros.h>
41# include <openssl/evp.h>
42#endif
43
44#include <sys/stat.h>
45#include <fcntl.h>
46
47static char const hextab[] = "0123456789abcdef";
48static TALLOC_CTX *xlat_ctx;
49
50typedef struct {
52 fr_dict_t const *dict; //!< Restrict xlat to this namespace
54
55/** Copy an argument from the input list to the output cursor.
56 *
57 * For now we just move it. This utility function will let us have
58 * value-box cursors as input arguments.
59 *
60 * @param[in] ctx talloc ctx
61 * @param[out] out where the value-box will be stored
62 * @param[in] in input value-box list
63 * @param[in] vb the argument to copy
64 */
65void xlat_arg_copy_out(TALLOC_CTX *ctx, fr_dcursor_t *out, fr_value_box_list_t *in, fr_value_box_t *vb)
66{
67 fr_value_box_list_remove(in, vb);
68 if (talloc_parent(vb) != ctx) {
69 (void) talloc_steal(ctx, vb);
70 }
72}
73
74/*
75 * Regular xlat functions
76 */
78 { .single = true, .type = FR_TYPE_INT8 },
80};
81
82/** Dynamically change the debugging level for the current request
83 *
84 * Example:
85@verbatim
86%debug(3)
87@endverbatim
88 *
89 * @ingroup xlat_functions
90 */
92 UNUSED xlat_ctx_t const *xctx,
93 request_t *request, fr_value_box_list_t *args)
94{
95 int level = 0;
96 fr_value_box_t *vb, *lvl_vb;
97
98 XLAT_ARGS(args, &lvl_vb);
99
100 /*
101 * Expand to previous (or current) level
102 */
103 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_INT8, NULL));
104 vb->vb_int8 = request->log.lvl;
106
107 /*
108 * Assume we just want to get the current value and NOT set it to 0
109 */
110 if (!lvl_vb) goto done;
111
112 level = lvl_vb->vb_int8;
113 if (level == 0) {
114 request->log.lvl = RAD_REQUEST_LVL_NONE;
115 } else {
116 if (level > L_DBG_LVL_MAX) level = L_DBG_LVL_MAX;
117 request->log.lvl = level;
118 }
119
120done:
121 return XLAT_ACTION_DONE;
122}
123
124
125static void xlat_debug_attr_vp(request_t *request, fr_pair_t const *vp,
126 fr_dict_attr_t const *da);
127
128static void xlat_debug_attr_list(request_t *request, fr_pair_list_t const *list,
129 fr_dict_attr_t const *parent)
130{
131 fr_pair_t *vp;
132
133 for (vp = fr_pair_list_next(list, NULL);
134 vp != NULL;
135 vp = fr_pair_list_next(list, vp)) {
136 xlat_debug_attr_vp(request, vp, parent);
137 }
138}
139
140
145
146static void xlat_debug_attr_vp(request_t *request, fr_pair_t const *vp,
147 fr_dict_attr_t const *parent)
148{
149 fr_dict_vendor_t const *vendor;
151 size_t i;
152 ssize_t slen;
153 fr_sbuff_t sbuff;
154 char buffer[1024];
155
156 sbuff = FR_SBUFF_OUT(buffer, sizeof(buffer));
157
158 /*
159 * Squash the names down if necessary.
160 */
161 if (!RDEBUG_ENABLED3) {
162 slen = fr_pair_print_name(&sbuff, parent, &vp);
163 } else {
164 slen = fr_sbuff_in_sprintf(&sbuff, "%s %s ", vp->da->name, fr_tokens[vp->op]);
165 }
166 if (slen <= 0) return;
167
168 switch (vp->vp_type) {
170 RIDEBUG2("%s{", buffer);
171 RINDENT();
172 xlat_debug_attr_list(request, &vp->vp_group, vp->da);
173 REXDENT();
174 RIDEBUG2("}");
175 break;
176
177 default:
178 RIDEBUG2("%s%pV", buffer, &vp->data);
179 }
180
181 if (!RDEBUG_ENABLED3) return;
182
183 RINDENT();
184 RIDEBUG3("da : %p", vp->da);
185 RIDEBUG3("is_raw : %pV", fr_box_bool(vp->vp_raw));
186 RIDEBUG3("is_unknown : %pV", fr_box_bool(vp->da->flags.is_unknown));
187
188 if (RDEBUG_ENABLED3) {
189 RIDEBUG3("parent : %s (%p)", vp->da->parent->name, vp->da->parent);
190 } else {
191 RIDEBUG2("parent : %s", vp->da->parent->name);
192 }
193 RIDEBUG3("attr : %u", vp->da->attr);
194 vendor = fr_dict_vendor_by_da(vp->da);
195 if (vendor) RIDEBUG2("vendor : %u (%s)", vendor->pen, vendor->name);
196 RIDEBUG3("type : %s", fr_type_to_str(vp->vp_type));
197
198 switch (vp->vp_type) {
199 case FR_TYPE_LEAF:
200 if (fr_box_is_variable_size(&vp->data)) {
201 RIDEBUG3("length : %zu", vp->vp_length);
202 }
203 RIDEBUG3("tainted : %pV", fr_box_bool(vp->data.tainted));
204 break;
205 default:
206 break;
207 }
208
209 if (!RDEBUG_ENABLED4) {
210 REXDENT();
211 return;
212 }
213
214 for (i = 0; i < fr_type_table_len; i++) {
215 int pad;
216
217 fr_value_box_t *dst = NULL;
218
219 type = &fr_type_table[i];
220
221 if ((fr_type_t) type->value == vp->vp_type) goto next_type;
222
223 /*
224 * Don't cast TO structural, or FROM structural types.
225 */
226 if (!fr_type_is_leaf(type->value) || !fr_type_is_leaf(vp->vp_type)) goto next_type;
227
228 MEM(dst = fr_value_box_acopy(NULL, &vp->data));
229
230 /* We expect some to fail */
231 if (fr_value_box_cast_in_place(dst, dst, type->value, NULL) < 0) {
232 goto next_type;
233 }
234
235 if ((pad = (11 - type->name.len)) < 0) pad = 0;
236
237 RINDENT();
238 RDEBUG4("as %s%*s: %pV", type->name.str, pad, " ", dst);
239 REXDENT();
240
241 next_type:
242 talloc_free(dst);
243 }
244
245 REXDENT();
246}
247
248/** Common function to move boxes from input list to output list
249 *
250 * This can be used to implement safe_for functions, as the xlat framework
251 * can be used for concatenation, casting, and marking up output boxes as
252 * safe_for.
253 */
255 UNUSED xlat_ctx_t const *xctx,
256 UNUSED request_t *request, fr_value_box_list_t *args)
257{
259 xlat_arg_copy_out(ctx, out, args, vb);
260 }
261
262 return XLAT_ACTION_DONE;
263}
264
265/** Print out attribute info
266 *
267 * Prints out all instances of a current attribute, or all attributes in a list.
268 *
269 * At higher debugging levels, also prints out alternative decodings of the same
270 * value. This is helpful to determine types for unknown attributes of long
271 * passed vendors, or just crazy/broken NAS.
272 *
273 * This expands to a zero length string.
274 *
275 * Example:
276@verbatim
277%pairs.debug(&request)
278@endverbatim
279 *
280 * @ingroup xlat_functions
281 */
283 UNUSED xlat_ctx_t const *xctx,
284 request_t *request, fr_value_box_list_t *args)
285{
286 fr_pair_t *vp;
287 fr_dcursor_t *cursor;
288 fr_value_box_t *in_head;
289
290 XLAT_ARGS(args, &in_head);
291
292 if (!RDEBUG_ENABLED2) return XLAT_ACTION_DONE; /* NOOP if debugging isn't enabled */
293
294 cursor = fr_value_box_get_cursor(in_head);
295
296 RDEBUG("Attributes matching \"%s\"", in_head->vb_cursor_name);
297
298 RINDENT();
299 for (vp = fr_dcursor_current(cursor);
300 vp;
301 vp = fr_dcursor_next(cursor)) {
302 xlat_debug_attr_vp(request, vp, NULL);
303 }
304 REXDENT();
305
306 return XLAT_ACTION_DONE;
307}
308
309#ifdef __clang__
310#pragma clang diagnostic ignored "-Wgnu-designator"
311#endif
312
313#define FR_FILENAME_SAFE_FOR ((uintptr_t) filename_xlat_escape)
314
315static int CC_HINT(nonnull(2,3)) filename_xlat_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
316{
317 fr_sbuff_t *out = NULL;
318 fr_value_box_entry_t entry;
319
321
322 /*
323 * Integers are just numbers, so they don't need to be escaped.
324 *
325 * Except that FR_TYPE_INTEGER includes 'date' and 'time_delta', which is annoying.
326 *
327 * 'octets' get printed as hex, so they don't need to be escaped.
328 */
329 switch (vb->type) {
330 case FR_TYPE_BOOL:
331 case FR_TYPE_UINT8:
332 case FR_TYPE_UINT16:
333 case FR_TYPE_UINT32:
334 case FR_TYPE_UINT64:
335 case FR_TYPE_INT8:
336 case FR_TYPE_INT16:
337 case FR_TYPE_INT32:
338 case FR_TYPE_INT64:
339 case FR_TYPE_SIZE:
340 case FR_TYPE_OCTETS:
341 return 0;
342
343 case FR_TYPE_NON_LEAF:
344 fr_assert(0);
345 return -1;
346
347 case FR_TYPE_DATE:
349 case FR_TYPE_IFID:
350 case FR_TYPE_ETHERNET:
351 case FR_TYPE_FLOAT32:
352 case FR_TYPE_FLOAT64:
359 case FR_TYPE_ATTR:
360 /*
361 * Printing prefixes etc. does NOT result in the escape function being called! So
362 * instead, we cast the results to a string, and then escape the string.
363 */
364 if (fr_value_box_cast_in_place(vb, vb, FR_TYPE_STRING, NULL) < 0) return -1;
365
367 break;
368
369 case FR_TYPE_STRING:
370 /*
371 * Note that we set ".always_escape" in the function arguments, so that we get called for
372 * IP addresses. Otherwise, the xlat evaluator and/or the list_concat_as_string
373 * functions won't call us. And the expansion will return IP addresses with '/' in them.
374 * Which is not what we want.
375 */
377
378 /*
379 * If the tainted string has a leading '.', then escape _all_ periods in it. This is so that we
380 * don't accidentally allow a "safe" value to end with '/', and then an "unsafe" value contains
381 * "..", and we now have a directory traversal attack.
382 *
383 * The escape rules will escape '/' in unsafe strings, so there's no possibility for an unsafe
384 * string to either end with a '/', or to contain "/.." itself.
385 *
386 * Allowing '.' in the middle of the string means we can have filenames based on realms, such as
387 * "log/aland@freeradius.org".
388 */
389 if (vb->vb_strvalue[0] == '.') {
391 } else {
393 }
394
395 break;
396 }
397
398 entry = vb->entry;
400 (void) fr_value_box_bstrndup(vb, vb, NULL, fr_sbuff_start(out), fr_sbuff_used(out), false);
401 vb->entry = entry;
402
403 return 0;
404}
405
407 { .required = true, .concat = true, .type = FR_TYPE_STRING,
408 .func = filename_xlat_escape, .safe_for = FR_FILENAME_SAFE_FOR, .always_escape = true },
410};
411
413 { .required = true, .concat = true, .type = FR_TYPE_STRING,
414 .func = filename_xlat_escape, .safe_for = FR_FILENAME_SAFE_FOR, .always_escape = true },
415 { .required = false, .type = FR_TYPE_UINT32 },
417};
418
419
421 UNUSED xlat_ctx_t const *xctx,
422 UNUSED request_t *request, fr_value_box_list_t *args)
423{
424 fr_value_box_t *dst, *vb;
425 char const *filename;
426 struct stat buf;
427
428 XLAT_ARGS(args, &vb);
429 fr_assert(vb->type == FR_TYPE_STRING);
430 filename = vb->vb_strvalue;
431
432 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
434
435 dst->vb_bool = (stat(filename, &buf) == 0);
436
437 return XLAT_ACTION_DONE;
438}
439
440
442 UNUSED xlat_ctx_t const *xctx,
443 request_t *request, fr_value_box_list_t *args)
444{
445 fr_value_box_t *dst, *vb;
446 char const *filename;
447 ssize_t len;
448 int fd;
449 char *p, buffer[256];
450
451 XLAT_ARGS(args, &vb);
452 fr_assert(vb->type == FR_TYPE_STRING);
453 filename = vb->vb_strvalue;
454
455 fd = open(filename, O_RDONLY);
456 if (fd < 0) {
457 REDEBUG3("Failed opening file %s - %s", filename, fr_syserror(errno));
458 return XLAT_ACTION_FAIL;
459 }
460
461 len = read(fd, buffer, sizeof(buffer));
462 if (len < 0) {
463 REDEBUG3("Failed reading file %s - %s", filename, fr_syserror(errno));
464 close(fd);
465 return XLAT_ACTION_FAIL;
466 }
467
468 /*
469 * Find the first CR/LF, but bail if we get any weird characters.
470 */
471 for (p = buffer; p < (buffer + len); p++) {
472 if ((*p == '\r') || (*p == '\n')) {
473 break;
474 }
475
476 if ((*p < ' ') && (*p != '\t')) {
477 invalid:
478 REDEBUG("Invalid text in file %s", filename);
479 close(fd);
480 return XLAT_ACTION_FAIL;
481 }
482 }
483
484 if ((p - buffer) > len) goto invalid;
485 close(fd);
486
487 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL));
488 if (fr_value_box_bstrndup(dst, dst, NULL, buffer, p - buffer, false) < 0) {
489 talloc_free(dst);
490 return XLAT_ACTION_FAIL;
491 }
492
494
495 return XLAT_ACTION_DONE;
496}
497
498
500 UNUSED xlat_ctx_t const *xctx,
501 request_t *request, fr_value_box_list_t *args)
502{
503 fr_value_box_t *dst, *vb;
504 char const *filename;
505 struct stat buf;
506
507 XLAT_ARGS(args, &vb);
508 fr_assert(vb->type == FR_TYPE_STRING);
509 filename = vb->vb_strvalue;
510
511 if (stat(filename, &buf) < 0) {
512 REDEBUG3("Failed checking file %s - %s", filename, fr_syserror(errno));
513 return XLAT_ACTION_FAIL;
514 }
515
516 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL)); /* off_t is signed, but file sizes shouldn't be negative */
518
519 dst->vb_uint64 = buf.st_size;
520
521 return XLAT_ACTION_DONE;
522}
523
524
526 UNUSED xlat_ctx_t const *xctx,
527 request_t *request, fr_value_box_list_t *args)
528{
529 fr_value_box_t *dst, *vb, *num = NULL;
530 char const *filename;
531 ssize_t len;
532 off_t offset;
533 int fd;
534 int crlf, stop = 1;
535 char *p, *end, *found, buffer[256];
536
537 XLAT_ARGS(args, &vb, &num);
538 fr_assert(vb->type == FR_TYPE_STRING);
539 filename = vb->vb_strvalue;
540
541 fd = open(filename, O_RDONLY);
542 if (fd < 0) {
543 REDEBUG3("Failed opening file %s - %s", filename, fr_syserror(errno));
544 return XLAT_ACTION_FAIL;
545 }
546
547 offset = lseek(fd, 0, SEEK_END);
548 if (offset < 0) {
549 REDEBUG3("Failed seeking to end of file %s - %s", filename, fr_syserror(errno));
550 goto fail;
551 }
552
553 if (offset > (off_t) sizeof(buffer)) {
554 offset -= sizeof(buffer);
555 } else {
556 offset = 0;
557 }
558
559 if (lseek(fd, offset, SEEK_SET) < 0) {
560 REDEBUG3("Failed seeking backwards from end of file %s - %s", filename, fr_syserror(errno));
561 goto fail;
562 }
563
564 len = read(fd, buffer, sizeof(buffer));
565 if (len < 0) {
566 fail:
567 REDEBUG3("Failed reading file %s - %s", filename, fr_syserror(errno));
568 close(fd);
569 return XLAT_ACTION_FAIL;
570 }
571 close(fd);
572
573 found = buffer;
574 end = buffer + len;
575
576 /*
577 * No data, OR just one CR / LF, we print it all out.
578 */
579 if (len <= 1) goto done;
580
581 /*
582 * Clamp number of lines to a reasonable value. They
583 * still all have to fit into 256 characters, though.
584 *
585 * @todo - have a large thread-local temporary buffer for this stuff.
586 */
587 if (num) {
588 fr_assert(num->type == FR_TYPE_GROUP);
589 fr_assert(fr_value_box_list_num_elements(&num->vb_group) == 1);
590
591 num = fr_value_box_list_head(&num->vb_group);
592 fr_assert(num->type == FR_TYPE_UINT32);
593
594 if (!num->vb_uint32) {
595 stop = 1;
596
597 } else if (num->vb_uint32 <= 16) {
598 stop = num->vb_uint32;
599
600 } else {
601 stop = 16;
602 }
603 } else {
604 stop = 1;
605 }
606
607 p = end - 1;
608 crlf = 0;
609
610 /*
611 * Skip any trailing CRLF first.
612 */
613 while (p > buffer) {
614 /*
615 * Could be CRLF, or just LF.
616 */
617 if (*p == '\n') {
618 end = p;
619 p--;
620 if (p == buffer) {
621 goto done;
622 }
623 if (*p >= ' ') {
624 break;
625 }
626 }
627
628 if (*p == '\r') {
629 end = p;
630 p--;
631 break;
632 }
633
634 /*
635 * We've found CR, LF, or CRLF. The previous
636 * thing is either raw text, or is another CR/LF.
637 */
638 break;
639 }
640
641 found = p;
642
643 while (p > buffer) {
644 crlf++;
645
646 /*
647 * If the current line is empty, we can stop.
648 */
649 if ((crlf == stop) && (*found < ' ')) {
650 found++;
651 goto done;
652 }
653
654 while (*p >= ' ') {
655 found = p;
656 p--;
657 if (p == buffer) {
658 found = buffer;
659 goto done;
660 }
661 }
662 if (crlf == stop) {
663 break;
664 }
665
666 /*
667 * Check again for CRLF.
668 */
669 if (*p == '\n') {
670 p--;
671 if (p == buffer) {
672 break;
673 }
674 if (*p >= ' ') {
675 continue;
676 }
677 }
678
679 if (*p == '\r') {
680 p--;
681 if (p == buffer) {
682 break;
683 }
684 continue;
685 }
686 }
687
688done:
689
690 /*
691 * @todo - return a _list_ of value-boxes, one for each line in the file.
692 * Which means chopping off each CRLF in the file
693 */
694
695 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL));
696 if (fr_value_box_bstrndup(dst, dst, NULL, found, (size_t) (end - found), false) < 0) {
697 talloc_free(dst);
698 return XLAT_ACTION_FAIL;
699 }
700
702
703 return XLAT_ACTION_DONE;
704}
705
707 { .required = true, .concat = true, .type = FR_TYPE_STRING,
708 .func = filename_xlat_escape, .safe_for = FR_FILENAME_SAFE_FOR, .always_escape = true },
709 { .required = true, .type = FR_TYPE_SIZE, .single = true },
711};
712
714 UNUSED xlat_ctx_t const *xctx,
715 request_t *request, fr_value_box_list_t *args)
716{
717 fr_value_box_t *dst, *vb, *max_size;
718 char const *filename;
719 ssize_t len;
720 int fd;
721 struct stat buf;
723
724 XLAT_ARGS(args, &vb, &max_size);
725 fr_assert(vb->type == FR_TYPE_STRING);
726 filename = vb->vb_strvalue;
727
728 fd = open(filename, O_RDONLY);
729 if (fd < 0) {
730 RPERROR("Failed opening file %s - %s", filename, fr_syserror(errno));
731 return XLAT_ACTION_FAIL;
732 }
733
734 if (fstat(fd, &buf) < 0) {
735 RPERROR("Failed checking file %s - %s", filename, fr_syserror(errno));
736 fail:
737 close(fd);
738 return XLAT_ACTION_FAIL;
739 }
740
741 if ((size_t)buf.st_size > max_size->vb_size) {
742 RPERROR("File larger than specified maximum (%"PRIu64" vs %zu)", buf.st_size, max_size->vb_size);
743 goto fail;
744 }
745
746 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_OCTETS, NULL));
747 fr_value_box_mem_alloc(dst, &buffer, dst, NULL, buf.st_size, true);
748
749 len = read(fd, buffer, buf.st_size);
750 if (len < 0) {
751 RPERROR("Failed reading file %s - %s", filename, fr_syserror(errno));
752 talloc_free(dst);
753 goto fail;
754 }
755 close(fd);
756
757 if (len < buf.st_size) {
758 RPERROR("Failed reading all of file %s", filename);
759 talloc_free(dst);
760 return XLAT_ACTION_FAIL;
761 }
762
764
765 return XLAT_ACTION_DONE;
766}
767
769 UNUSED xlat_ctx_t const *xctx,
770 request_t *request, fr_value_box_list_t *args)
771{
772 fr_value_box_t *dst, *vb;
773 char const *filename;
774
775 XLAT_ARGS(args, &vb);
776 fr_assert(vb->type == FR_TYPE_STRING);
777 filename = vb->vb_strvalue;
778
779 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
781
782 dst->vb_bool = (unlink(filename) == 0);
783 if (!dst->vb_bool) {
784 REDEBUG3("Failed unlinking file %s - %s", filename, fr_syserror(errno));
785 }
786
787 return XLAT_ACTION_DONE;
788}
789
791 request_t *request, fr_value_box_list_t *args)
792{
793 fr_value_box_t *dst, *vb;
794 char const *filename;
795 int fd;
796
797 XLAT_ARGS(args, &vb);
798 fr_assert(vb->type == FR_TYPE_STRING);
799 filename = vb->vb_strvalue;
800
801 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
803
804 fd = open(filename, O_CREAT | O_WRONLY, 0600);
805 if (fd < 0) {
806 dst->vb_bool = false;
807 REDEBUG3("Failed touching file %s - %s", filename, fr_syserror(errno));
808 return XLAT_ACTION_DONE;
809 }
810 dst->vb_bool = true;
811
812 close(fd);
813
814 return XLAT_ACTION_DONE;
815}
816
818 request_t *request, fr_value_box_list_t *args)
819{
820 fr_value_box_t *dst, *vb;
821 char const *dirname;
822
823 XLAT_ARGS(args, &vb);
824 fr_assert(vb->type == FR_TYPE_STRING);
825 dirname = vb->vb_strvalue;
826
827 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
829
830 dst->vb_bool = (fr_mkdir(NULL, dirname, -1, 0700, NULL, NULL) == 0);
831 if (!dst->vb_bool) {
832 REDEBUG3("Failed creating directory %s - %s", dirname, fr_syserror(errno));
833 }
834
835 return XLAT_ACTION_DONE;
836}
837
839 request_t *request, fr_value_box_list_t *args)
840{
841 fr_value_box_t *dst, *vb;
842 char const *dirname;
843
844 XLAT_ARGS(args, &vb);
845 fr_assert(vb->type == FR_TYPE_STRING);
846 dirname = vb->vb_strvalue;
847
848 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
850
851 dst->vb_bool = (rmdir(dirname) == 0);
852 if (!dst->vb_bool) {
853 REDEBUG3("Failed removing directory %s - %s", dirname, fr_syserror(errno));
854 }
855
856 return XLAT_ACTION_DONE;
857}
858
860 { .required = true, .type = FR_TYPE_VOID },
861 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .type = FR_TYPE_VOID },
863};
864
866 UNUSED xlat_ctx_t const *xctx,
867 UNUSED request_t *request, fr_value_box_list_t *in)
868{
869 fr_value_box_t *vb;
870
872 while ((vb = fr_value_box_list_pop_head(in)) != NULL) {
874 }
875
876 return XLAT_ACTION_DONE;
877}
878
880 UNUSED xlat_ctx_t const *xctx,
881 UNUSED request_t *request, fr_value_box_list_t *in)
882{
883 fr_value_box_t *vb;
884
885 while ((vb = fr_value_box_list_pop_head(in)) != NULL) {
886 fr_value_box_t *child;
887
888 fr_assert(vb->type == FR_TYPE_GROUP);
889
890 while ((child = fr_value_box_list_pop_head(&vb->vb_group)) != NULL) {
891 child->tainted = true;
893
894 fr_dcursor_append(out, child);
895 }
896 }
897
898 return XLAT_ACTION_DONE;
899}
900
902 { .required = true, .type = FR_TYPE_STRING },
903 { .required = true, .concat = true, .type = FR_TYPE_STRING },
905};
906
907/** Split a string into multiple new strings based on a delimiter
908 *
909@verbatim
910%explode(<string>, <delim>)
911@endverbatim
912 *
913 * Example:
914@verbatim
915update request {
916 &Tmp-String-1 := "a,b,c"
917}
918"%concat(%explode(%{Tmp-String-1}, ','), '|')" == "a|b|c"g
919@endverbatim
920 *
921 * @ingroup xlat_functions
922 */
924 UNUSED xlat_ctx_t const *xctx,
925 request_t *request, fr_value_box_list_t *args)
926{
928 fr_value_box_list_t *list;
929 fr_value_box_t *delim_vb;
930 ssize_t delim_len;
931 char const *delim;
932 fr_value_box_t *string, *vb;
933
934 XLAT_ARGS(args, &strings, &delim_vb);
935
936 list = &strings->vb_group;
937
938 /* coverity[dereference] */
939 if (delim_vb->vb_length == 0) {
940 REDEBUG("Delimiter must be greater than zero characters");
941 return XLAT_ACTION_FAIL;
942 }
943
944 delim = delim_vb->vb_strvalue;
945 delim_len = delim_vb->vb_length;
946
947 while ((string = fr_value_box_list_pop_head(list))) {
948 fr_sbuff_t sbuff = FR_SBUFF_IN(string->vb_strvalue, string->vb_length);
949 fr_sbuff_marker_t m_start;
950
951 /*
952 * If the delimiter is not in the string, just move to the output
953 */
954 if (!fr_sbuff_adv_to_str(&sbuff, SIZE_MAX, delim, delim_len)) {
955 fr_dcursor_append(out, string);
956 continue;
957 }
958
959 fr_sbuff_set_to_start(&sbuff);
960 fr_sbuff_marker(&m_start, &sbuff);
961
962 while (fr_sbuff_remaining(&sbuff)) {
963 if (fr_sbuff_adv_to_str(&sbuff, SIZE_MAX, delim, delim_len)) {
964 /*
965 * If there's nothing before the delimiter skip
966 */
967 if (fr_sbuff_behind(&m_start) == 0) goto advance;
968
969 MEM(vb = fr_value_box_alloc_null(ctx));
970 fr_value_box_bstrndup(vb, vb, NULL, fr_sbuff_current(&m_start),
971 fr_sbuff_behind(&m_start), false);
972 fr_value_box_safety_copy(vb, string);
974
975 advance:
976 fr_sbuff_advance(&sbuff, delim_len);
977 fr_sbuff_set(&m_start, &sbuff);
978 continue;
979 }
980
981 fr_sbuff_set_to_end(&sbuff);
982 MEM(vb = fr_value_box_alloc_null(ctx));
983 fr_value_box_bstrndup(vb, vb, NULL, fr_sbuff_current(&m_start),
984 fr_sbuff_behind(&m_start), false);
985
986 fr_value_box_safety_copy(vb, string);
988 break;
989 }
990 talloc_free(string);
991 }
992
993 return XLAT_ACTION_DONE;
994}
995
996/** Mark one or more attributes as immutable
997 *
998 * Example:
999@verbatim
1000%pairs.immutable(request.State[*])
1001@endverbatim
1002 *
1003 * @ingroup xlat_functions
1004 */
1006 UNUSED xlat_ctx_t const *xctx,
1007 request_t *request, fr_value_box_list_t *args)
1008{
1009 fr_pair_t *vp;
1010 fr_dcursor_t *cursor;
1011 fr_value_box_t *in_head;
1012
1013 XLAT_ARGS(args, &in_head);
1014
1015 cursor = fr_value_box_get_cursor(in_head);
1016
1017 RDEBUG("Attributes matching \"%s\"", in_head->vb_cursor_name);
1018
1019 RINDENT();
1020 for (vp = fr_dcursor_current(cursor);
1021 vp;
1022 vp = fr_dcursor_next(cursor)) {
1024 }
1025 REXDENT();
1026
1027 return XLAT_ACTION_DONE;
1028}
1029
1031 { .required = true, .single = true, .type = FR_TYPE_VOID },
1033};
1034
1035/** Print data as integer, not as VALUE.
1036 *
1037 * Example:
1038@verbatim
1039update request {
1040 &Tmp-IP-Address-0 := "127.0.0.5"
1041}
1042%integer(%{Tmp-IP-Address-0}) == 2130706437
1043@endverbatim
1044 * @ingroup xlat_functions
1045 */
1047 UNUSED xlat_ctx_t const *xctx,
1048 request_t *request, fr_value_box_list_t *args)
1049{
1050 fr_value_box_t *in_vb;
1051 char const *p;
1052
1053 XLAT_ARGS(args, &in_vb);
1054
1055 fr_strerror_clear(); /* Make sure we don't print old errors */
1056
1057 fr_value_box_list_remove(args, in_vb);
1058
1059 switch (in_vb->type) {
1060 default:
1061 error:
1062 RPEDEBUG("Failed converting %pR (%s) to an integer", in_vb,
1063 fr_type_to_str(in_vb->type));
1064 talloc_free(in_vb);
1065 return XLAT_ACTION_FAIL;
1066
1067 case FR_TYPE_NUMERIC:
1068 /*
1069 * Ensure enumeration is NULL so that the integer
1070 * version of a box is returned
1071 */
1072 in_vb->enumv = NULL;
1073
1074 /*
1075 * FR_TYPE_DATE and FR_TYPE_TIME_DELTA need to be cast
1076 * to int64_t so that they're printed in a
1077 * numeric format.
1078 */
1079 if ((in_vb->type == FR_TYPE_DATE) || (in_vb->type == FR_TYPE_TIME_DELTA)) {
1080 if (fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_INT64, NULL) < 0) goto error;
1081 }
1082 break;
1083
1084 case FR_TYPE_STRING:
1085 /*
1086 * Strings are always zero terminated. They may
1087 * also have zeros in the middle, but if that
1088 * happens, the caller will only get the part up
1089 * to the first zero.
1090 *
1091 * We check for negative numbers, just to be
1092 * nice.
1093 */
1094 for (p = in_vb->vb_strvalue; *p != '\0'; p++) {
1095 if (*p == '-') break;
1096 }
1097
1098 if (*p == '-') {
1099 if (fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_INT64, NULL) < 0) goto error;
1100 } else {
1101 if (fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT64, NULL) < 0) goto error;
1102 }
1103 break;
1104
1105 case FR_TYPE_OCTETS:
1106 if (in_vb->vb_length > sizeof(uint64_t)) {
1107 fr_strerror_printf("Expected octets length <= %zu, got %zu", sizeof(uint64_t), in_vb->vb_length);
1108 goto error;
1109 }
1110
1111 if (in_vb->vb_length > sizeof(uint32_t)) {
1112 if (unlikely(fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT64, NULL) < 0)) goto error;
1113 } else if (in_vb->vb_length > sizeof(uint16_t)) {
1114 if (unlikely(fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT32, NULL) < 0)) goto error;
1115 } else if (in_vb->vb_length > sizeof(uint8_t)) {
1116 if (unlikely(fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT16, NULL) < 0)) goto error;
1117 } else {
1118 if (unlikely(fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT8, NULL) < 0)) goto error;
1119 }
1120
1121 break;
1122
1123 case FR_TYPE_IPV4_ADDR:
1125 if (fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT32, NULL) < 0) goto error;
1126 break;
1127
1128 case FR_TYPE_ETHERNET:
1129 if (fr_value_box_cast_in_place(ctx, in_vb, FR_TYPE_UINT64, NULL) < 0) goto error;
1130 break;
1131
1132 case FR_TYPE_IPV6_ADDR:
1134 {
1135 uint128_t ipv6int;
1136 char buff[40];
1137 fr_value_box_t *vb;
1138
1139 /*
1140 * Needed for correct alignment (as flagged by ubsan)
1141 */
1142 memcpy(&ipv6int, &in_vb->vb_ipv6addr, sizeof(ipv6int));
1143
1144 fr_snprint_uint128(buff, sizeof(buff), ntohlll(ipv6int));
1145
1146 MEM(vb = fr_value_box_alloc_null(ctx));
1147 fr_value_box_bstrndup(vb, vb, NULL, buff, strlen(buff), false);
1149 talloc_free(in_vb);
1150 return XLAT_ACTION_DONE;
1151 }
1152 }
1153
1154 fr_dcursor_append(out, in_vb);
1155
1156 return XLAT_ACTION_DONE;
1157}
1158
1160 { .concat = true, .type = FR_TYPE_STRING },
1162};
1163
1164/** Log something at INFO level.
1165 *
1166 * Example:
1167@verbatim
1168%log("This is an informational message")
1169@endverbatim
1170 *
1171 * @ingroup xlat_functions
1172 */
1174 UNUSED xlat_ctx_t const *xctx,
1175 request_t *request, fr_value_box_list_t *args)
1176{
1177 fr_value_box_t *vb;
1178
1179 XLAT_ARGS(args, &vb);
1180
1181 if (!vb) return XLAT_ACTION_DONE;
1182
1183 RINFO("%s", vb->vb_strvalue);
1184
1185 return XLAT_ACTION_DONE;
1186}
1187
1188
1189/** Log something at DEBUG level.
1190 *
1191 * Example:
1192@verbatim
1193%log.debug("This is a message")
1194@endverbatim
1195 *
1196 * @ingroup xlat_functions
1197 */
1199 UNUSED xlat_ctx_t const *xctx,
1200 request_t *request, fr_value_box_list_t *args)
1201{
1202 fr_value_box_t *vb;
1203
1204 XLAT_ARGS(args, &vb);
1205
1206 if (!vb) return XLAT_ACTION_DONE;
1207
1208 RDEBUG("%s", vb->vb_strvalue);
1209
1210 return XLAT_ACTION_DONE;
1211}
1212
1213
1214/** Log something at DEBUG level.
1215 *
1216 * Example:
1217@verbatim
1218%log.err("Big error here")
1219@endverbatim
1220 *
1221 * @ingroup xlat_functions
1222 */
1224 UNUSED xlat_ctx_t const *xctx,
1225 request_t *request, fr_value_box_list_t *args)
1226{
1227 fr_value_box_t *vb;
1228
1229 XLAT_ARGS(args, &vb);
1230
1231 if (!vb) return XLAT_ACTION_DONE;
1232
1233 REDEBUG("%s", vb->vb_strvalue);
1234
1235 return XLAT_ACTION_DONE;
1236}
1237
1238
1239/** Log something at WARN level.
1240 *
1241 * Example:
1242@verbatim
1243%log.warn("Maybe something bad happened")
1244@endverbatim
1245 *
1246 * @ingroup xlat_functions
1247 */
1249 UNUSED xlat_ctx_t const *xctx,
1250 request_t *request, fr_value_box_list_t *args)
1251{
1252 fr_value_box_t *vb;
1253
1254 XLAT_ARGS(args, &vb);
1255
1256 if (!vb) return XLAT_ACTION_DONE;
1257
1258 RWDEBUG("%s", vb->vb_strvalue);
1259
1260 return XLAT_ACTION_DONE;
1261}
1262
1263static int _log_dst_free(fr_log_t *log)
1264{
1265 close(log->fd);
1266 return 0;
1267}
1268
1270 { .required = false, .type = FR_TYPE_STRING, .concat = true },
1271 { .required = false, .type = FR_TYPE_UINT32, .single = true },
1272 { .required = false, .type = FR_TYPE_STRING, .concat = true },
1274};
1275
1276/** Change the log destination to the named one
1277 *
1278 * Example:
1279@verbatim
1280%log.destination('foo')
1281@endverbatim
1282 *
1283 * @ingroup xlat_functions
1284 */
1286 UNUSED xlat_ctx_t const *xctx,
1287 request_t *request, fr_value_box_list_t *args)
1288{
1289 fr_value_box_t *dst, *lvl, *file;
1290 fr_log_t *log, *dbg;
1291 uint32_t level = 2;
1292
1293 XLAT_ARGS(args, &dst, &lvl, &file);
1294
1295 if (!dst || !*dst->vb_strvalue) {
1296 request_log_prepend(request, NULL, L_DBG_LVL_DISABLE);
1297 return XLAT_ACTION_DONE;
1298 }
1299
1300 log = log_dst_by_name(dst->vb_strvalue);
1301 if (!log) return XLAT_ACTION_FAIL;
1302
1303 if (lvl) level = lvl->vb_uint32;
1304
1305 if (!file || ((log->dst != L_DST_NULL) && (log->dst != L_DST_FILES))) {
1306 request_log_prepend(request, log, level);
1307 return XLAT_ACTION_DONE;
1308 }
1309
1310 /*
1311 * Clone it.
1312 */
1313 MEM(dbg = talloc_memdup(request, log, sizeof(*log)));
1314 dbg->parent = log;
1315
1316 /*
1317 * Open the new filename.
1318 */
1319 dbg->dst = L_DST_FILES;
1320 dbg->file = talloc_strdup(dbg, file->vb_strvalue);
1321 dbg->fd = open(dbg->file, O_WRONLY | O_CREAT | O_CLOEXEC, 0600);
1322 if (dbg->fd < 0) {
1323 REDEBUG("Failed opening %s - %s", dbg->file, fr_syserror(errno));
1324 talloc_free(dbg);
1325 return XLAT_ACTION_DONE;
1326 }
1327
1328 /*
1329 * Ensure that we close the file handle when done.
1330 */
1331 talloc_set_destructor(dbg, _log_dst_free);
1332
1333 request_log_prepend(request, dbg, level);
1334 return XLAT_ACTION_DONE;
1335}
1336
1337
1339 { .required = true, .type = FR_TYPE_STRING },
1341};
1342
1343/** Processes fmt as a map string and applies it to the current request
1344 *
1345 * e.g.
1346@verbatim
1347%map("User-Name := 'foo'")
1348@endverbatim
1349 *
1350 * Allows sets of modifications to be cached and then applied.
1351 * Useful for processing generic attributes from LDAP.
1352 *
1353 * @ingroup xlat_functions
1354 */
1356 UNUSED xlat_ctx_t const *xctx,
1357 request_t *request, fr_value_box_list_t *args)
1358{
1359 map_t *map = NULL;
1360 int ret;
1361 fr_value_box_t *fmt_vb;
1362 fr_value_box_t *vb;
1363
1364 tmpl_rules_t attr_rules = {
1365 .attr = {
1366 .dict_def = request->local_dict,
1367 .list_def = request_attr_request,
1368 },
1369 .xlat = {
1370 .runtime_el = unlang_interpret_event_list(request)
1371 }
1372 };
1373
1374 XLAT_ARGS(args, &fmt_vb);
1375
1376 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
1377 vb->vb_bool = false; /* Default fail value - changed to true on success */
1379
1380 fr_value_box_list_foreach(&fmt_vb->vb_group, fmt) {
1381 if (map_afrom_attr_str(request, &map, fmt->vb_strvalue, &attr_rules, &attr_rules) < 0) {
1382 RPEDEBUG("Failed parsing \"%s\" as map", fmt_vb->vb_strvalue);
1383 return XLAT_ACTION_FAIL;
1384 }
1385
1386 switch (map->lhs->type) {
1387 case TMPL_TYPE_ATTR:
1388 case TMPL_TYPE_XLAT:
1389 break;
1390
1391 default:
1392 REDEBUG("Unexpected type %s in left hand side of expression",
1393 tmpl_type_to_str(map->lhs->type));
1394 return XLAT_ACTION_FAIL;
1395 }
1396
1397 switch (map->rhs->type) {
1398 case TMPL_TYPE_ATTR:
1399 case TMPL_TYPE_EXEC:
1400 case TMPL_TYPE_DATA:
1403 case TMPL_TYPE_XLAT:
1404 break;
1405
1406 default:
1407 REDEBUG("Unexpected type %s in right hand side of expression",
1408 tmpl_type_to_str(map->rhs->type));
1409 return XLAT_ACTION_FAIL;
1410 }
1411
1412 RINDENT();
1413 ret = map_to_request(request, map, map_to_vp, NULL);
1414 REXDENT();
1415 talloc_free(map);
1416 if (ret < 0) return XLAT_ACTION_FAIL;
1417 }
1418
1419 vb->vb_bool = true;
1420 return XLAT_ACTION_DONE;
1421}
1422
1423
1425 { .required = true, .concat = true, .type = FR_TYPE_STRING },
1427};
1428
1429/** Calculate number of seconds until the next n hour(s), day(s), week(s), year(s).
1430 *
1431 * For example, if it were 16:18 %time.next(1h) would expand to 2520.
1432 *
1433 * The envisaged usage for this function is to limit sessions so that they don't
1434 * cross billing periods. The output of the xlat should be combined with %rand() to create
1435 * some jitter, unless the desired effect is every subscriber on the network
1436 * re-authenticating at the same time.
1437 *
1438 * @ingroup xlat_functions
1439 */
1441 UNUSED xlat_ctx_t const *xctx,
1442 request_t *request, fr_value_box_list_t *args)
1443{
1444 unsigned long num;
1445
1446 char const *p;
1447 char *q;
1448 time_t now;
1449 struct tm *local, local_buff;
1450 fr_value_box_t *in_head;
1451 fr_value_box_t *vb;
1452
1453 XLAT_ARGS(args, &in_head);
1454
1455 /*
1456 * We want to limit based on _now_, not on when they logged in.
1457 */
1458 now = time(NULL);
1459 local = localtime_r(&now, &local_buff);
1460
1461 p = in_head->vb_strvalue;
1462
1463 num = strtoul(p, &q, 10);
1464 if ((num == ULONG_MAX) || !q || *q == '\0') {
1465 REDEBUG("<int> must be followed by time period (h|d|w|m|y)");
1466 return XLAT_ACTION_FAIL;
1467 }
1468 if (num == 0) {
1469 REDEBUG("<int> must be greater than zero");
1470 return XLAT_ACTION_FAIL;
1471 }
1472
1473 if (p == q) {
1474 num = 1;
1475 } else {
1476 p += q - p;
1477 }
1478
1479 local->tm_sec = 0;
1480 local->tm_min = 0;
1481
1482 switch (*p) {
1483 case 'h':
1484 local->tm_hour += num;
1485 break;
1486
1487 case 'd':
1488 local->tm_hour = 0;
1489 local->tm_mday += num;
1490 break;
1491
1492 case 'w':
1493 local->tm_hour = 0;
1494 local->tm_mday += (7 - local->tm_wday) + (7 * (num-1));
1495 break;
1496
1497 case 'm':
1498 local->tm_hour = 0;
1499 local->tm_mday = 1;
1500 local->tm_mon += num;
1501 break;
1502
1503 case 'y':
1504 local->tm_hour = 0;
1505 local->tm_mday = 1;
1506 local->tm_mon = 0;
1507 local->tm_year += num;
1508 break;
1509
1510 default:
1511 REDEBUG("Invalid time period '%c', must be h|d|w|m|y", *p);
1512 return XLAT_ACTION_FAIL;
1513 }
1514
1515 MEM(vb = fr_value_box_alloc_null(ctx));
1516 fr_value_box_uint64(vb, NULL, (uint64_t)(mktime(local) - now), false);
1518 return XLAT_ACTION_DONE;
1519}
1520
1525
1526/** Just serves to push the result up the stack
1527 *
1528 */
1530 xlat_ctx_t const *xctx,
1531 UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
1532{
1533 xlat_eval_rctx_t *rctx = talloc_get_type_abort(xctx->rctx, xlat_eval_rctx_t);
1535
1536 talloc_free(rctx);
1537
1538 return xa;
1539}
1540
1542 { .required = true, .concat = true, .type = FR_TYPE_STRING },
1544};
1545
1546/** Dynamically evaluate an expansion string
1547 *
1548 * @ingroup xlat_functions
1549 */
1551 UNUSED xlat_ctx_t const *xctx,
1552 request_t *request, fr_value_box_list_t *args)
1553{
1554 /*
1555 * These are escaping rules applied to the
1556 * input string. They're mostly here to
1557 * allow \% and \\ to work.
1558 *
1559 * Everything else should be passed in as
1560 * unescaped data.
1561 */
1562 static fr_sbuff_unescape_rules_t const escape_rules = {
1563 .name = "xlat",
1564 .chr = '\\',
1565 .subs = {
1566 ['%'] = '%',
1567 ['\\'] = '\\',
1568 },
1569 .do_hex = false,
1570 .do_oct = false
1571 };
1572
1573 xlat_eval_rctx_t *rctx;
1574 fr_value_box_t *arg = fr_value_box_list_head(args);
1575
1576 XLAT_ARGS(args, &arg);
1577
1578 MEM(rctx = talloc_zero(unlang_interpret_frame_talloc_ctx(request), xlat_eval_rctx_t));
1579
1580 /*
1581 * Parse the input as a literal expansion
1582 */
1583 if (xlat_tokenize_expression(rctx,
1584 &rctx->ex,
1585 &FR_SBUFF_IN(arg->vb_strvalue, arg->vb_length),
1586 &(fr_sbuff_parse_rules_t){
1587 .escapes = &escape_rules
1588 },
1589 &(tmpl_rules_t){
1590 .attr = {
1591 .dict_def = request->local_dict,
1592 .list_def = request_attr_request,
1593 .allow_unknown = false,
1594 .allow_unresolved = false,
1595 .allow_foreign = false,
1596 },
1597 .xlat = {
1598 .runtime_el = unlang_interpret_event_list(request),
1599 },
1600 .at_runtime = true
1601 }) < 0) {
1602 RPEDEBUG("Failed parsing expansion");
1603 error:
1604 talloc_free(rctx);
1605 return XLAT_ACTION_FAIL;
1606 }
1607
1608 /*
1609 * Call the resolution function so we produce
1610 * good errors about what function was
1611 * unresolved.
1612 */
1613 if (rctx->ex->flags.needs_resolving &&
1614 (xlat_resolve(rctx->ex, &(xlat_res_rules_t){ .allow_unresolved = false }) < 0)) {
1615 RPEDEBUG("Unresolved expansion functions in expansion");
1616 goto error;
1617
1618 }
1619
1620 if (unlang_xlat_yield(request, xlat_eval_resume, NULL, 0, rctx) != XLAT_ACTION_YIELD) goto error;
1621
1622 if (unlang_xlat_push(ctx, &rctx->last_result, (fr_value_box_list_t *)out->dlist,
1623 request, rctx->ex, UNLANG_SUB_FRAME) < 0) goto error;
1624
1626}
1627
1629 { .required = true, .type = FR_TYPE_STRING },
1630 { .required = true, .single = true, .type = FR_TYPE_UINT64 },
1631 { .concat = true, .type = FR_TYPE_STRING },
1633};
1634
1635/** lpad a string
1636 *
1637@verbatim
1638%lpad(%{Attribute-Name}, <length> [, <fill>])
1639@endverbatim
1640 *
1641 * Example: (User-Name = "foo")
1642@verbatim
1643%lpad(%{User-Name}, 5 'x') == "xxfoo"
1644@endverbatim
1645 *
1646 * @ingroup xlat_functions
1647 */
1649 UNUSED xlat_ctx_t const *xctx,
1650 request_t *request, fr_value_box_list_t *args)
1651{
1652 fr_value_box_t *values;
1653 fr_value_box_t *pad;
1655
1656 fr_value_box_list_t *list;
1657
1658 size_t pad_len;
1659
1660 char const *fill_str = NULL;
1661 size_t fill_len = 0;
1662
1663 fr_value_box_t *in = NULL;
1664
1665 XLAT_ARGS(args, &values, &pad, &fill);
1666
1667 /* coverity[dereference] */
1668 list = &values->vb_group;
1669 /* coverity[dereference] */
1670 pad_len = (size_t)pad->vb_uint64;
1671
1672 /*
1673 * Fill is optional
1674 */
1675 if (fill) {
1676 fill_str = fill->vb_strvalue;
1677 fill_len = talloc_strlen(fill_str);
1678 }
1679
1680 if (fill_len == 0) {
1681 fill_str = " ";
1682 fill_len = 1;
1683 }
1684
1685 while ((in = fr_value_box_list_pop_head(list))) {
1686 size_t len = talloc_strlen(in->vb_strvalue);
1687 size_t remaining;
1688 char *buff;
1689 fr_sbuff_t sbuff;
1690 fr_sbuff_marker_t m_data;
1691
1693
1694 if (len >= pad_len) continue;
1695
1696 if (fr_value_box_bstr_realloc(in, &buff, in, pad_len) < 0) {
1697 RPEDEBUG("Failed reallocing input data");
1698 return XLAT_ACTION_FAIL;
1699 }
1700
1701 fr_sbuff_init_in(&sbuff, buff, pad_len);
1702 fr_sbuff_marker(&m_data, &sbuff);
1703
1704 /*
1705 * ...nothing to move if the input
1706 * string is empty.
1707 */
1708 if (len > 0) {
1709 fr_sbuff_advance(&m_data, pad_len - len); /* Mark where we want the data to go */
1710 fr_sbuff_move(&FR_SBUFF(&m_data), &FR_SBUFF(&sbuff), len); /* Shift the data */
1711 }
1712
1713 if (fill_len == 1) {
1714 memset(fr_sbuff_current(&sbuff), *fill_str, fr_sbuff_ahead(&m_data));
1715 continue;
1716 }
1717
1718 /*
1719 * Copy fill as a repeating pattern
1720 */
1721 while ((remaining = fr_sbuff_ahead(&m_data))) {
1722 size_t to_copy = remaining >= fill_len ? fill_len : remaining;
1723 memcpy(fr_sbuff_current(&sbuff), fill_str, to_copy); /* avoid \0 termination */
1724 fr_sbuff_advance(&sbuff, to_copy);
1725 }
1726 fr_sbuff_set_to_end(&sbuff);
1727 fr_sbuff_terminate(&sbuff); /* Move doesn't re-terminate */
1728 }
1729
1730 return XLAT_ACTION_DONE;
1731}
1732
1733/** Right pad a string
1734 *
1735@verbatim
1736%rpad(%{Attribute-Name}, <length> [, <fill>])
1737@endverbatim
1738 *
1739 * Example: (User-Name = "foo")
1740@verbatim
1741%rpad(%{User-Name}, 5 'x') == "fooxx"
1742@endverbatim
1743 *
1744 * @ingroup xlat_functions
1745 */
1747 UNUSED xlat_ctx_t const *xctx,
1748 request_t *request, fr_value_box_list_t *args)
1749{
1750 fr_value_box_t *values;
1751 fr_value_box_list_t *list;
1752 fr_value_box_t *pad;
1753 /* coverity[dereference] */
1754 size_t pad_len;
1756 char const *fill_str = NULL;
1757 size_t fill_len = 0;
1758
1759 fr_value_box_t *in = NULL;
1760
1761 XLAT_ARGS(args, &values, &pad, &fill);
1762
1763 list = &values->vb_group;
1764 pad_len = (size_t)pad->vb_uint64;
1765
1766 /*
1767 * Fill is optional
1768 */
1769 if (fill) {
1770 fill_str = fill->vb_strvalue;
1771 fill_len = talloc_strlen(fill_str);
1772 }
1773
1774 if (fill_len == 0) {
1775 fill_str = " ";
1776 fill_len = 1;
1777 }
1778
1779 while ((in = fr_value_box_list_pop_head(list))) {
1780 size_t len = talloc_strlen(in->vb_strvalue);
1781 size_t remaining;
1782 char *buff;
1783 fr_sbuff_t sbuff;
1784
1786
1787 if (len >= pad_len) continue;
1788
1789 if (fr_value_box_bstr_realloc(in, &buff, in, pad_len) < 0) {
1790 fail:
1791 RPEDEBUG("Failed reallocing input data");
1792 return XLAT_ACTION_FAIL;
1793 }
1794
1795 fr_sbuff_init_in(&sbuff, buff, pad_len);
1796 fr_sbuff_advance(&sbuff, len);
1797
1798 if (fill_len == 1) {
1799 memset(fr_sbuff_current(&sbuff), *fill_str, fr_sbuff_remaining(&sbuff));
1800 continue;
1801 }
1802
1803 /*
1804 * Copy fill as a repeating pattern
1805 */
1806 while ((remaining = fr_sbuff_remaining(&sbuff))) {
1807 if (fr_sbuff_in_bstrncpy(&sbuff, fill_str, remaining >= fill_len ? fill_len : remaining) < 0) {
1808 goto fail;
1809 }
1810 }
1811 }
1812
1813 return XLAT_ACTION_DONE;
1814}
1815
1817 { .required = true, .concat = true, .type = FR_TYPE_OCTETS },
1819};
1820
1821/** Encode string or attribute as base64
1822 *
1823 * Example:
1824@verbatim
1825%base64.encode("foo") == "Zm9v"
1826@endverbatim
1827 *
1828 * @ingroup xlat_functions
1829 */
1831 UNUSED xlat_ctx_t const *xctx,
1832 request_t *request, fr_value_box_list_t *args)
1833{
1834 size_t alen;
1835 ssize_t elen;
1836 char *buff;
1837 fr_value_box_t *vb;
1839
1840 XLAT_ARGS(args, &in);
1841
1842 alen = FR_BASE64_ENC_LENGTH(in->vb_length);
1843
1844 MEM(vb = fr_value_box_alloc_null(ctx));
1845 if (fr_value_box_bstr_alloc(vb, &buff, vb, NULL, alen, false) < 0) {
1846 talloc_free(vb);
1847 return XLAT_ACTION_FAIL;
1848 }
1849
1850 elen = fr_base64_encode(&FR_SBUFF_OUT(buff, talloc_array_length(buff)),
1851 &FR_DBUFF_TMP(in->vb_octets, in->vb_length), true);
1852 if (elen < 0) {
1853 RPEDEBUG("Base64 encoding failed");
1854 talloc_free(vb);
1855 return XLAT_ACTION_FAIL;
1856 }
1857 fr_assert((size_t)elen <= alen);
1860
1861 return XLAT_ACTION_DONE;
1862}
1863
1865 { .required = true, .concat = true, .type = FR_TYPE_OCTETS },
1867};
1868
1869/** Decode base64 string
1870 *
1871 * Example:
1872@verbatim
1873%base64.decode("Zm9v") == "foo"
1874@endverbatim
1875 *
1876 * @ingroup xlat_functions
1877 */
1879 UNUSED xlat_ctx_t const *xctx,
1880 request_t *request, fr_value_box_list_t *args)
1881{
1882 size_t alen;
1883 ssize_t declen = 0;
1884 uint8_t *decbuf;
1885 fr_value_box_t *vb;
1887
1888 XLAT_ARGS(args, &in);
1889
1890 /*
1891 * Pass empty arguments through
1892 *
1893 * FR_BASE64_DEC_LENGTH produces 2 for empty strings...
1894 */
1895 if (in->vb_length == 0) {
1896 xlat_arg_copy_out(ctx, out, args, in);
1897 return XLAT_ACTION_DONE;
1898 }
1899
1900 alen = FR_BASE64_DEC_LENGTH(in->vb_length);
1901 MEM(vb = fr_value_box_alloc_null(ctx));
1902 if (alen > 0) {
1903 MEM(fr_value_box_mem_alloc(vb, &decbuf, vb, NULL, alen, false) == 0);
1904 declen = fr_base64_decode(&FR_DBUFF_TMP(decbuf, alen),
1905 &FR_SBUFF_IN(in->vb_strvalue, in->vb_length), true, true);
1906 if (declen < 0) {
1907 RPEDEBUG("Base64 string invalid");
1908 talloc_free(vb);
1909 return XLAT_ACTION_FAIL;
1910 }
1911
1912 MEM(fr_value_box_mem_realloc(vb, NULL, vb, declen) == 0);
1913 }
1914
1917
1918 return XLAT_ACTION_DONE;
1919}
1920
1922 { .required = true, .type = FR_TYPE_STRING },
1924};
1925
1926/** Convert hex string to binary
1927 *
1928 * Example:
1929@verbatim
1930%bin("666f6f626172") == "foobar"
1931@endverbatim
1932 *
1933 * @see #xlat_func_hex
1934 *
1935 * @ingroup xlat_functions
1936 */
1938 UNUSED xlat_ctx_t const *xctx,
1939 request_t *request, fr_value_box_list_t *args)
1940{
1941 fr_value_box_t *result;
1942 char const *p, *end;
1943 uint8_t *bin;
1944 size_t len, outlen;
1946 fr_value_box_t *list, *hex;
1947
1948 XLAT_ARGS(args, &list);
1949
1950 while ((hex = fr_value_box_list_pop_head(&list->vb_group))) {
1951 len = hex->vb_length;
1952 if ((len > 1) && (len & 0x01)) {
1953 REDEBUG("Input data length must be >1 and even, got %zu", len);
1954 return XLAT_ACTION_FAIL;
1955 }
1956
1957 p = hex->vb_strvalue;
1958 end = p + len;
1959
1960 /*
1961 * Look for 0x at the start of the string, and ignore if we see it.
1962 */
1963 if ((p[0] == '0') && (p[1] == 'x')) {
1964 p += 2;
1965 len -=2;
1966 }
1967
1968 /*
1969 * Zero length octets string
1970 */
1971 if (p == end) continue;
1972
1973 outlen = len / 2;
1974
1975 MEM(result = fr_value_box_alloc_null(ctx));
1976 MEM(fr_value_box_mem_alloc(result, &bin, result, NULL, outlen, false) == 0);
1977 fr_base16_decode(&err, &FR_DBUFF_TMP(bin, outlen), &FR_SBUFF_IN(p, end - p), true);
1978 if (err) {
1979 REDEBUG2("Invalid hex string");
1980 talloc_free(result);
1981 return XLAT_ACTION_FAIL;
1982 }
1983
1985 fr_dcursor_append(out, result);
1986 }
1987
1988 return XLAT_ACTION_DONE;
1989}
1990
1992 { .required = true, .single = true, .type = FR_TYPE_TIME_DELTA },
1994};
1995
1996/** Block for the specified duration
1997 *
1998 * This is for developer use only to simulate blocking, synchronous I/O.
1999 * For normal use, use the %delay() xlat instead.
2000 *
2001 * Example:
2002@verbatim
2003%block(1s)
2004@endverbatim
2005 *
2006 * @ingroup xlat_functions
2007 */
2009 UNUSED xlat_ctx_t const *xctx,
2010 UNUSED request_t *request, fr_value_box_list_t *args)
2011{
2012 fr_value_box_t *delay;
2013 fr_value_box_t *vb;
2014 struct timespec ts_in, ts_remain = {};
2015
2016 XLAT_ARGS(args, &delay);
2017
2018 ts_in = fr_time_delta_to_timespec(delay->vb_time_delta);
2019
2020 (void)nanosleep(&ts_in, &ts_remain);
2021
2022 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
2023 vb->vb_time_delta = fr_time_delta_sub(delay->vb_time_delta,
2024 fr_time_delta_from_timespec(&ts_remain));
2026
2027 return XLAT_ACTION_DONE;
2028}
2029
2031 { .required = true, .single = true, .type = FR_TYPE_VOID },
2032 { .type = FR_TYPE_VOID },
2033 { .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .type = FR_TYPE_VOID },
2035};
2036
2037/** Cast one or more output value-boxes to the given type
2038 *
2039 * First argument of is type to cast to.
2040 *
2041 * Example:
2042@verbatim
2043%cast('string', %{request[*]}) results in all of the input boxes being cast to string/
2044@endverbatim
2045 *
2046 * @ingroup xlat_functions
2047 */
2049 UNUSED xlat_ctx_t const *xctx,
2050 request_t *request, fr_value_box_list_t *args)
2051{
2053 fr_value_box_t *arg;
2055 fr_dict_attr_t const *time_res = NULL;
2056
2057 XLAT_ARGS(args, &name);
2058
2059 /*
2060 * Get the type, which can be in one of a few formats.
2061 */
2062 if (fr_type_is_numeric(name->type)) {
2064 RPEDEBUG("Failed parsing '%pV' as a numerical data type", name);
2065 return XLAT_ACTION_FAIL;
2066 }
2067 type = name->vb_uint8;
2068
2069 } else {
2070 if (name->type != FR_TYPE_STRING) {
2072 RPEDEBUG("Failed parsing '%pV' as a string data type", name);
2073 return XLAT_ACTION_FAIL;
2074 }
2075 }
2076
2078 if (type == FR_TYPE_NULL) {
2079 if ((time_res = xlat_time_res_attr(name->vb_strvalue)) == NULL) {
2080 RDEBUG("Unknown data type '%s'", name->vb_strvalue);
2081 return XLAT_ACTION_FAIL;
2082 }
2083
2085 }
2086 }
2087
2088 (void) fr_value_box_list_pop_head(args);
2089
2090 /*
2091 * When we cast nothing to a string / octets, the result is an empty string/octets.
2092 */
2093 if (unlikely(!fr_value_box_list_head(args))) {
2094 if ((type == FR_TYPE_STRING) || (type == FR_TYPE_OCTETS)) {
2095 fr_value_box_t *dst;
2096
2097 MEM(dst = fr_value_box_alloc(ctx, type, NULL));
2098 fr_dcursor_append(out, dst);
2099 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
2100
2101 return XLAT_ACTION_DONE;
2102 }
2103
2104 RDEBUG("No data for cast to '%s'", fr_type_to_str(type));
2105 return XLAT_ACTION_FAIL;
2106 }
2107
2108 /*
2109 * Cast to string means *print* to string.
2110 */
2111 if (type == FR_TYPE_STRING) {
2112 fr_sbuff_t *agg;
2113 fr_value_box_t *dst;
2114
2116
2117 FR_SBUFF_TALLOC_THREAD_LOCAL(&agg, 256, SIZE_MAX);
2118
2119 MEM(dst = fr_value_box_alloc_null(ctx));
2121
2122 if (fr_value_box_list_concat_as_string(dst, agg, args, NULL, 0, NULL,
2124 RPEDEBUG("Failed concatenating string");
2125 return XLAT_ACTION_FAIL;
2126 }
2127
2128 fr_value_box_bstrndup(dst, dst, NULL, fr_sbuff_start(agg), fr_sbuff_used(agg), false);
2129 fr_dcursor_append(out, dst);
2130 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
2131
2132 return XLAT_ACTION_DONE;
2133 }
2134
2135 /*
2136 * Copy inputs to outputs, casting them along the way.
2137 */
2138 arg = NULL;
2139 while ((arg = fr_value_box_list_next(args, arg)) != NULL) {
2140 fr_value_box_t *vb, *p;
2141
2142 fr_assert(arg->type == FR_TYPE_GROUP);
2143
2144 vb = fr_value_box_list_head(&arg->vb_group);
2145 while (vb) {
2146 p = fr_value_box_list_remove(&arg->vb_group, vb);
2147
2148 if (fr_value_box_cast_in_place(vb, vb, type, time_res) < 0) {
2149 RPEDEBUG("Failed casting %pV to data type '%s'", vb, fr_type_to_str(type));
2150 return XLAT_ACTION_FAIL;
2151 }
2153 vb = fr_value_box_list_next(&arg->vb_group, p);
2154 }
2155 }
2156 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
2157
2158 return XLAT_ACTION_DONE;
2159}
2160
2162 { .required = true, .type = FR_TYPE_VOID },
2163 { .concat = true, .type = FR_TYPE_STRING },
2165};
2166
2167/** Concatenate string representation of values of given attributes using separator
2168 *
2169 * First argument of is the list of attributes to concatenate, followed
2170 * by an optional separator
2171 *
2172 * Example:
2173@verbatim
2174%concat(%{request.[*]}, ',') == "<attr1value>,<attr2value>,<attr3value>,..."
2175%concat(%{Tmp-String-0[*]}, '. ') == "<str1value>. <str2value>. <str3value>. ..."
2176%concat(%join(%{User-Name}, %{Calling-Station-Id}), ', ') == "bob, aa:bb:cc:dd:ee:ff"
2177@endverbatim
2178 *
2179 * @ingroup xlat_functions
2180 */
2182 UNUSED xlat_ctx_t const *xctx,
2183 request_t *request, fr_value_box_list_t *args)
2184{
2185 fr_value_box_t *result;
2186 fr_value_box_t *list;
2187 fr_value_box_t *separator;
2188 fr_value_box_list_t *to_concat;
2189 char *buff;
2190 char const *sep;
2191
2192 XLAT_ARGS(args, &list, &separator);
2193
2194 sep = (separator) ? separator->vb_strvalue : "";
2195 to_concat = &list->vb_group;
2196
2197 result = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL);
2198 if (!result) {
2199 error:
2200 RPEDEBUG("Failed concatenating input");
2201 return XLAT_ACTION_FAIL;
2202 }
2203
2204 buff = fr_value_box_list_aprint(result, to_concat, sep, NULL);
2205 if (!buff) goto error;
2206
2208
2209 fr_dcursor_append(out, result);
2210
2211 return XLAT_ACTION_DONE;
2212}
2213
2215 { .required = true, .type = FR_TYPE_OCTETS },
2217};
2218
2219/** Print data as hex, not as VALUE.
2220 *
2221 * Example:
2222@verbatim
2223%hex("foobar") == "666f6f626172"
2224@endverbatim
2225 *
2226 * @see #xlat_func_bin
2227 *
2228 * @ingroup xlat_functions
2229 */
2231 UNUSED xlat_ctx_t const *xctx,
2232 UNUSED request_t *request, fr_value_box_list_t *args)
2233{
2234 char *new_buff;
2235 fr_value_box_t *list, *bin;
2236 fr_value_box_t safety;
2237
2238 XLAT_ARGS(args, &list);
2239
2240 while ((bin = fr_value_box_list_pop_head(&list->vb_group))) {
2241 fr_value_box_safety_copy(&safety, bin);
2242
2243 /*
2244 * Use existing box, but with new buffer
2245 */
2246 MEM(new_buff = talloc_zero_array(bin, char, (bin->vb_length * 2) + 1));
2247 if (bin->vb_length) {
2248 fr_base16_encode(&FR_SBUFF_OUT(new_buff, (bin->vb_length * 2) + 1),
2249 &FR_DBUFF_TMP(bin->vb_octets, bin->vb_length));
2251 fr_value_box_strdup_shallow(bin, NULL, new_buff, false);
2252 /*
2253 * Zero length binary > zero length hex string
2254 */
2255 } else {
2257 fr_value_box_strdup(bin, bin, NULL, "", false);
2258 }
2259
2260 fr_value_box_safety_copy(bin, &safety);
2261 fr_dcursor_append(out, bin);
2262 }
2263
2264 return XLAT_ACTION_DONE;
2265}
2266
2271
2272static xlat_action_t xlat_hmac(TALLOC_CTX *ctx, fr_dcursor_t *out,
2273 fr_value_box_list_t *args, uint8_t *digest, int digest_len, hmac_type type)
2274{
2275 fr_value_box_t *vb, *data, *key;
2276
2277 XLAT_ARGS(args, &data, &key);
2278
2279 if (type == HMAC_MD5) {
2280 /* coverity[dereference] */
2281 fr_hmac_md5(digest, data->vb_octets, data->vb_length, key->vb_octets, key->vb_length);
2282 } else if (type == HMAC_SHA1) {
2283 /* coverity[dereference] */
2284 fr_hmac_sha1(digest, data->vb_octets, data->vb_length, key->vb_octets, key->vb_length);
2285 }
2286
2287 MEM(vb = fr_value_box_alloc_null(ctx));
2288 fr_value_box_memdup(vb, vb, NULL, digest, digest_len, false);
2289
2291
2292 return XLAT_ACTION_DONE;
2293}
2294
2296 { .required = true, .concat = true, .type = FR_TYPE_STRING },
2297 { .required = true, .concat = true, .type = FR_TYPE_STRING },
2299};
2300
2301/** Generate the HMAC-MD5 of a string or attribute
2302 *
2303 * Example:
2304@verbatim
2305%hmacmd5('foo', 'bar') == "0x31b6db9e5eb4addb42f1a6ca07367adc"
2306@endverbatim
2307 *
2308 * @ingroup xlat_functions
2309 */
2311 UNUSED xlat_ctx_t const *xctx,
2312 UNUSED request_t *request, fr_value_box_list_t *in)
2313{
2314 uint8_t digest[MD5_DIGEST_LENGTH];
2315 return xlat_hmac(ctx, out, in, digest, MD5_DIGEST_LENGTH, HMAC_MD5);
2316}
2317
2318
2319/** Generate the HMAC-SHA1 of a string or attribute
2320 *
2321 * Example:
2322@verbatim
2323%hmacsha1('foo', 'bar') == "0x85d155c55ed286a300bd1cf124de08d87e914f3a"
2324@endverbatim
2325 *
2326 * @ingroup xlat_functions
2327 */
2329 UNUSED xlat_ctx_t const *xctx,
2330 UNUSED request_t *request, fr_value_box_list_t *in)
2331{
2333 return xlat_hmac(ctx, out, in, digest, SHA1_DIGEST_LENGTH, HMAC_SHA1);
2334}
2335
2337 { .required = true, .type = FR_TYPE_VOID },
2338 { .variadic = XLAT_ARG_VARIADIC_EMPTY_SQUASH, .type = FR_TYPE_VOID },
2340};
2341
2342/** Join a series of arguments to form a single list
2343 *
2344 * null boxes are not preserved.
2345 */
2347 UNUSED xlat_ctx_t const *xctx,
2348 UNUSED request_t *request, fr_value_box_list_t *in)
2349{
2351 fr_assert(arg->type == FR_TYPE_GROUP);
2352
2353 fr_value_box_list_foreach(&arg->vb_group, vb) {
2354 xlat_arg_copy_out(ctx, out, &arg->vb_group, vb);
2355 }
2356 }
2357 return XLAT_ACTION_DONE;
2358}
2359
2360static void ungroup(fr_dcursor_t *out, fr_value_box_list_t *in)
2361{
2362 fr_value_box_t *vb;
2363
2364 while ((vb = fr_value_box_list_pop_head(in)) != NULL) {
2365 if (vb->type != FR_TYPE_GROUP) {
2367 continue;
2368 }
2369 talloc_free(vb);
2370 }
2371}
2372
2373/** Ungroups all of its arguments into one flat list.
2374 *
2375 */
2377 UNUSED xlat_ctx_t const *xctx,
2378 UNUSED request_t *request, fr_value_box_list_t *in)
2379{
2380 fr_value_box_t *arg = NULL;
2381
2382 while ((arg = fr_value_box_list_next(in, arg)) != NULL) {
2383 fr_assert(arg->type == FR_TYPE_GROUP);
2384
2385 ungroup(out, &arg->vb_group);
2386 }
2387 return XLAT_ACTION_DONE;
2388}
2389
2391 { .single = true, .variadic = XLAT_ARG_VARIADIC_EMPTY_KEEP, .type = FR_TYPE_VOID },
2393};
2394
2395/** Return the on-the-wire size of the boxes in bytes
2396 *
2397 * skips null values
2398 *
2399 * Example:
2400@verbatim
2401%length(foobar) == 6
2402%length(%bin("0102030005060708")) == 8
2403@endverbatim
2404 *
2405 * @see #xlat_func_strlen
2406 *
2407 * @ingroup xlat_functions
2408 */
2410 UNUSED xlat_ctx_t const *xctx,
2411 UNUSED request_t *request, fr_value_box_list_t *in)
2412
2413{
2416
2417 MEM(my = fr_value_box_alloc(ctx, FR_TYPE_SIZE, NULL));
2418 if (!fr_type_is_null(vb->type)) my->vb_size = fr_value_box_network_length(vb);
2420 }
2421
2422 return XLAT_ACTION_DONE;
2423}
2424
2425
2427 { .concat = true, .type = FR_TYPE_OCTETS },
2429};
2430
2431/** Calculate the MD4 hash of a string or attribute.
2432 *
2433 * Example:
2434@verbatim
2435%md4("foo") == "0ac6700c491d70fb8650940b1ca1e4b2"
2436@endverbatim
2437 *
2438 * @ingroup xlat_functions
2439 */
2441 UNUSED xlat_ctx_t const *xctx,
2442 UNUSED request_t *request, fr_value_box_list_t *args)
2443{
2444 uint8_t digest[MD4_DIGEST_LENGTH];
2445 fr_value_box_t *vb;
2446 fr_value_box_t *in_head;
2447
2448 XLAT_ARGS(args, &in_head);
2449
2450 if (in_head) {
2451 fr_md4_calc(digest, in_head->vb_octets, in_head->vb_length);
2452 } else {
2453 /* Digest of empty string */
2454 fr_md4_calc(digest, NULL, 0);
2455 }
2456
2457 MEM(vb = fr_value_box_alloc_null(ctx));
2458 fr_value_box_memdup(vb, vb, NULL, digest, sizeof(digest), false);
2459
2461 VALUE_BOX_LIST_VERIFY((fr_value_box_list_t *)out->dlist);
2462
2463 return XLAT_ACTION_DONE;
2464}
2465
2467 { .concat = true, .type = FR_TYPE_OCTETS },
2469};
2470
2471/** Calculate the MD5 hash of a string or attribute.
2472 *
2473 * Example:
2474@verbatim
2475%md5("foo") == "acbd18db4cc2f85cedef654fccc4a4d8"
2476@endverbatim
2477 *
2478 * @ingroup xlat_functions
2479 */
2481 UNUSED xlat_ctx_t const *xctx,
2482 UNUSED request_t *request, fr_value_box_list_t *args)
2483{
2484 uint8_t digest[MD5_DIGEST_LENGTH];
2485 fr_value_box_t *vb;
2486 fr_value_box_t *in_head;
2487
2488 XLAT_ARGS(args, &in_head);
2489
2490 if (in_head) {
2491 fr_md5_calc(digest, in_head->vb_octets, in_head->vb_length);
2492 } else {
2493 /* Digest of empty string */
2494 fr_md5_calc(digest, NULL, 0);
2495 }
2496
2497 MEM(vb = fr_value_box_alloc_null(ctx));
2498 fr_value_box_memdup(vb, vb, NULL, digest, sizeof(digest), false);
2499
2501
2502 return XLAT_ACTION_DONE;
2503}
2504
2505
2506/** Encode attributes as a series of string attribute/value pairs
2507 *
2508 * This is intended to serialize one or more attributes as a comma
2509 * delimited string.
2510 *
2511 * Example:
2512@verbatim
2513%pairs.print(request.[*]) == 'User-Name = "foo"User-Password = "bar"'
2514%concat(%pairs.print.print(request.[*]), ', ') == 'User-Name = "foo", User-Password = "bar"'
2515@endverbatim
2516 *
2517 * @see #xlat_func_concat
2518 *
2519 * @ingroup xlat_functions
2520 */
2522 UNUSED xlat_ctx_t const *xctx,
2523 request_t *request, fr_value_box_list_t *args)
2524{
2525 fr_pair_t *vp;
2526 fr_dcursor_t *cursor;
2527 fr_value_box_t *vb;
2528 fr_value_box_t *in_head;
2529
2530 XLAT_ARGS(args, &in_head);
2531
2532 cursor = fr_value_box_get_cursor(in_head);
2533
2534 for (vp = fr_dcursor_current(cursor);
2535 vp;
2536 vp = fr_dcursor_next(cursor)) {
2537 char *buff;
2538
2539 MEM(vb = fr_value_box_alloc_null(ctx));
2540 if (unlikely(fr_pair_aprint(vb, &buff, NULL, vp) < 0)) {
2541 RPEDEBUG("Failed printing pair");
2542 talloc_free(vb);
2543 return XLAT_ACTION_FAIL;
2544 }
2545
2546 fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, buff, false);
2548
2549 VALUE_BOX_VERIFY(vb);
2550 }
2551
2552 return XLAT_ACTION_DONE;
2553}
2554
2556 { .required = true, .single = true, .type = FR_TYPE_UINT32 },
2558};
2559
2560/** Generate a random integer value
2561 *
2562 * For "N = %rand(MAX)", 0 <= N < MAX
2563 *
2564 * Example:
2565@verbatim
2566%rand(100) == 42
2567@endverbatim
2568 *
2569 * @ingroup xlat_functions
2570 */
2572 UNUSED xlat_ctx_t const *xctx,
2573 UNUSED request_t *request, fr_value_box_list_t *in)
2574{
2575 int64_t result;
2576 fr_value_box_t *vb;
2577 fr_value_box_t *in_head = fr_value_box_list_head(in);
2578
2579 result = in_head->vb_uint32;
2580
2581 /* Make sure it isn't too big */
2582 if (result > (1 << 30)) result = (1 << 30);
2583
2584 result *= fr_rand(); /* 0..2^32-1 */
2585 result >>= 32;
2586
2587 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
2588 vb->vb_uint64 = result;
2589
2591
2592 return XLAT_ACTION_DONE;
2593}
2594
2596 { .required = true, .concat = true, .type = FR_TYPE_STRING },
2598};
2599
2600/** Generate a string of random chars
2601 *
2602 * Build strings of random chars, useful for generating tokens and passcodes
2603 * Format similar to String::Random.
2604 *
2605 * Format characters may include the following, and may be
2606 * preceded by a repetition count:
2607 * - "c" lowercase letters
2608 * - "C" uppercase letters
2609 * - "n" numbers
2610 * - "a" alphanumeric
2611 * - "!" punctuation
2612 * - "." alphanumeric + punctuation
2613 * - "s" alphanumeric + "./"
2614 * - "o" characters suitable for OTP (easily confused removed)
2615 * - "b" binary data
2616 *
2617 * Example:
2618@verbatim
2619%randstr("CCCC!!cccnnn") == "IPFL>{saf874"
2620%randstr("42o") == "yHdupUwVbdHprKCJRYfGbaWzVwJwUXG9zPabdGAhM9"
2621%hex(%randstr("bbbb")) == "a9ce04f3"
2622%hex(%randstr("8b")) == "fe165529f9f66839"
2623@endverbatim
2624 * @ingroup xlat_functions
2625 */
2627 UNUSED xlat_ctx_t const *xctx,
2628 request_t *request, fr_value_box_list_t *args)
2629{
2630 /*
2631 * Lookup tables for randstr char classes
2632 */
2633 static char randstr_punc[] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
2634 static char randstr_salt[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmopqrstuvwxyz/.";
2635
2636 /*
2637 * Characters humans rarely confuse. Reduces char set considerably
2638 * should only be used for things such as one time passwords.
2639 */
2640 static char randstr_otp[] = "469ACGHJKLMNPQRUVWXYabdfhijkprstuvwxyz";
2641
2642 char const *p, *start, *end;
2643 char *endptr;
2644 char *buff_p;
2645 unsigned int result;
2646 unsigned int reps;
2647 size_t outlen = 0;
2648 fr_value_box_t* vb;
2649 fr_value_box_t *in_head;
2650
2651 XLAT_ARGS(args, &in_head);
2652
2653 /** Max repetitions of a single character class
2654 *
2655 */
2656#define REPETITION_MAX 1024
2657
2658 start = p = in_head->vb_strvalue;
2659 end = p + in_head->vb_length;
2660
2661 /*
2662 * Calculate size of output
2663 */
2664 while (p < end) {
2665 /*
2666 * Repetition modifiers.
2667 *
2668 * We limit it to REPETITION_MAX, because we don't want
2669 * utter stupidity.
2670 */
2671 if (isdigit((uint8_t) *p)) {
2672 reps = strtol(p, &endptr, 10);
2673 if (reps > REPETITION_MAX) reps = REPETITION_MAX;
2674 outlen += reps;
2675 p = endptr;
2676 } else {
2677 outlen++;
2678 }
2679 p++;
2680 }
2681
2682 MEM(vb = fr_value_box_alloc_null(ctx));
2683 MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, outlen, false) == 0);
2684
2685 /* Reset p to start position */
2686 p = start;
2687
2688 while (p < end) {
2689 size_t i;
2690
2691 if (isdigit((uint8_t) *p)) {
2692 reps = strtol(p, &endptr, 10);
2693 if (reps > REPETITION_MAX) {
2694 reps = REPETITION_MAX;
2695 RMARKER(L_WARN, L_DBG_LVL_2, start, p - start,
2696 "Forcing repetition to %u", (unsigned int)REPETITION_MAX);
2697 }
2698 p = endptr;
2699 } else {
2700 reps = 1;
2701 }
2702
2703 for (i = 0; i < reps; i++) {
2704 result = fr_rand();
2705 switch (*p) {
2706 /*
2707 * Lowercase letters
2708 */
2709 case 'c':
2710 *buff_p++ = 'a' + (result % 26);
2711 break;
2712
2713 /*
2714 * Uppercase letters
2715 */
2716 case 'C':
2717 *buff_p++ = 'A' + (result % 26);
2718 break;
2719
2720 /*
2721 * Numbers
2722 */
2723 case 'n':
2724 *buff_p++ = '0' + (result % 10);
2725 break;
2726
2727 /*
2728 * Alpha numeric
2729 */
2730 case 'a':
2731 *buff_p++ = randstr_salt[result % (sizeof(randstr_salt) - 3)];
2732 break;
2733
2734 /*
2735 * Punctuation
2736 */
2737 case '!':
2738 *buff_p++ = randstr_punc[result % (sizeof(randstr_punc) - 1)];
2739 break;
2740
2741 /*
2742 * Alpha numeric + punctuation
2743 */
2744 case '.':
2745 *buff_p++ = '!' + (result % 95);
2746 break;
2747
2748 /*
2749 * Alpha numeric + salt chars './'
2750 */
2751 case 's':
2752 *buff_p++ = randstr_salt[result % (sizeof(randstr_salt) - 1)];
2753 break;
2754
2755 /*
2756 * Chars suitable for One Time Password tokens.
2757 * Alpha numeric with easily confused char pairs removed.
2758 */
2759 case 'o':
2760 *buff_p++ = randstr_otp[result % (sizeof(randstr_otp) - 1)];
2761 break;
2762
2763 /*
2764 * Binary data - Copy between 1-4 bytes at a time
2765 */
2766 case 'b':
2767 {
2768 size_t copy = (reps - i) > sizeof(result) ? sizeof(result) : reps - i;
2769
2770 memcpy(buff_p, (uint8_t *)&result, copy);
2771 buff_p += copy;
2772 i += (copy - 1); /* Loop +1 */
2773 }
2774 break;
2775
2776 default:
2777 REDEBUG("Invalid character class '%c'", *p);
2778 talloc_free(vb);
2779
2780 return XLAT_ACTION_FAIL;
2781 }
2782 }
2783
2784 p++;
2785 }
2786
2787 *buff_p++ = '\0';
2788
2790
2791 return XLAT_ACTION_DONE;
2792}
2793
2794/** Convert a UUID in an array of uint32_t to the conventional string representation.
2795 */
2796static int uuid_print_vb(fr_value_box_t *vb, uint32_t vals[4])
2797{
2798 char buffer[36];
2799 int i, j = 0;
2800
2801#define UUID_CHARS(_v, _num) for (i = 0; i < _num; i++) { \
2802 buffer[j++] = fr_base16_alphabet_encode_lc[(uint8_t)((vals[_v] & 0xf0000000) >> 28)]; \
2803 vals[_v] = vals[_v] << 4; \
2804 }
2805
2806 UUID_CHARS(0, 8)
2807 buffer[j++] = '-';
2808 UUID_CHARS(1, 4)
2809 buffer[j++] = '-';
2810 UUID_CHARS(1, 4);
2811 buffer[j++] = '-';
2812 UUID_CHARS(2, 4);
2813 buffer[j++] = '-';
2814 UUID_CHARS(2, 4);
2815 UUID_CHARS(3, 8);
2816
2817 return fr_value_box_bstrndup(vb, vb, NULL, buffer, sizeof(buffer), false);
2818}
2819
2820static inline void uuid_set_version(uint32_t vals[4], uint8_t version)
2821{
2822 /*
2823 * The version is indicated by the upper 4 bits of byte 7 - the 3rd byte of vals[1]
2824 */
2825 vals[1] = (vals[1] & 0xffff0fff) | (((uint32_t)version & 0x0f) << 12);
2826}
2827
2828static inline void uuid_set_variant(uint32_t vals[4], uint8_t variant)
2829{
2830 /*
2831 * The variant is indicated by the first 1, 2 or 3 bits of byte 9
2832 * The number of bits is determined by the variant.
2833 */
2834 switch (variant) {
2835 case 0:
2836 vals[2] = vals[2] & 0x7fffffff;
2837 break;
2838
2839 case 1:
2840 vals[2] = (vals[2] & 0x3fffffff) | 0x80000000;
2841 break;
2842
2843 case 2:
2844 vals[2] = (vals[2] & 0x3fffffff) | 0xc0000000;
2845 break;
2846
2847 case 3:
2848 vals[2] = vals[2] | 0xe0000000;
2849 break;
2850 }
2851}
2852
2853/** Generate a version 4 UUID
2854 *
2855 * Version 4 UUIDs are all random except the version and variant fields
2856 *
2857 * Example:
2858@verbatim
2859%uuid.v4 == "cba48bda-641c-42ae-8173-d97aa04f888a"
2860@endverbatim
2861 * @ingroup xlat_functions
2862 */
2863static xlat_action_t xlat_func_uuid_v4(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx,
2864 UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
2865{
2866 fr_value_box_t *vb;
2867 uint32_t vals[4];
2868 int i;
2869
2870 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL));
2871
2872 /*
2873 * A type 4 UUID is all random except a few bits.
2874 * Start with 128 bits of random.
2875 */
2876 for (i = 0; i < 4; i++) vals[i] = fr_rand();
2877
2878 /*
2879 * Set the version and variant fields
2880 */
2881 uuid_set_version(vals, 4);
2882 uuid_set_variant(vals, 1);
2883
2884 if (uuid_print_vb(vb, vals) < 0) {
2885 talloc_free(vb);
2886 return XLAT_ACTION_FAIL;
2887 }
2888
2890 return XLAT_ACTION_DONE;
2891}
2892
2893/** Generate a version 7 UUID
2894 *
2895 * Version 7 UUIDs use 48 bits of unix millisecond epoch and 74 bits of random
2896 *
2897 * Example:
2898@verbatim
2899%uuid.v7 == "019a58d8-8524-7342-aa07-c0fa2bba6a4e"
2900@endverbatim
2901 * @ingroup xlat_functions
2902 */
2903static xlat_action_t xlat_func_uuid_v7(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx,
2904 UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
2905{
2906 fr_value_box_t *vb;
2907 uint32_t vals[4];
2908 int i;
2909 uint64_t now;
2910
2911 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_STRING, NULL));
2912
2913 /*
2914 * A type 7 UUID has random data from bit 48
2915 * Start with random from bit 32 - since fr_rand is uint32
2916 */
2917 for (i = 1; i < 4; i++) vals[i] = fr_rand();
2918
2919 /*
2920 * The millisecond epoch fills the first 48 bits
2921 */
2922 now = fr_time_to_msec(fr_time());
2923 now = now << 16;
2924 vals[0] = now >> 32;
2925 vals[1] = (vals[1] & 0x0000ffff) | (now & 0xffff0000);
2926
2927 /*
2928 * Set the version and variant fields
2929 */
2930 uuid_set_version(vals, 7);
2931 uuid_set_variant(vals, 1);
2932
2933 if (uuid_print_vb(vb, vals) < 0) return XLAT_ACTION_FAIL;
2934
2936 return XLAT_ACTION_DONE;
2937}
2938
2940 { .required = true, .type = FR_TYPE_UINT64 },
2941 { .required = false, .type = FR_TYPE_UINT64 },
2942 { .required = false, .type = FR_TYPE_UINT64 },
2944};
2945
2946/** Generate a range of uint64 numbers
2947 *
2948 * Example:
2949@verbatim
2950%range(end) - 0..end
2951%rang(start, end)
2952%range(start,end, step)
2953@endverbatim
2954 * @ingroup xlat_functions
2955 */
2957 UNUSED xlat_ctx_t const *xctx,
2958 request_t *request, fr_value_box_list_t *args)
2959{
2960 fr_value_box_t *start_vb, *end_vb, *step_vb;
2961 fr_value_box_t *dst;
2962 uint64_t i, start, end, step;
2963
2964 XLAT_ARGS(args, &start_vb, &end_vb, &step_vb);
2965
2966 if (step_vb) {
2967 start = fr_value_box_list_head(&start_vb->vb_group)->vb_uint64;
2968 end = fr_value_box_list_head(&end_vb->vb_group)->vb_uint64;
2969 step = fr_value_box_list_head(&step_vb->vb_group)->vb_uint64;
2970
2971 } else if (end_vb) {
2972 start = fr_value_box_list_head(&start_vb->vb_group)->vb_uint64;
2973 end = fr_value_box_list_head(&end_vb->vb_group)->vb_uint64;
2974 step = 1;
2975
2976 } else {
2977 start = 0;
2978 end = fr_value_box_list_head(&start_vb->vb_group)->vb_uint64;
2979 step = 1;
2980 }
2981
2982 if (end <= start) {
2983 REDEBUG("Invalid range - 'start' must be less than 'end'");
2984 return XLAT_ACTION_FAIL;
2985 }
2986
2987 if (!step) {
2988 REDEBUG("Invalid range - 'step' must be greater than zero");
2989 return XLAT_ACTION_FAIL;
2990 }
2991
2992 if (step > (end - start)) {
2993 REDEBUG("Invalid range - 'step' must allow for at least one result");
2994 return XLAT_ACTION_FAIL;
2995 }
2996
2997 if (((end - start) / step) > 1000) {
2998 REDEBUG("Invalid range - Too many results");
2999 return XLAT_ACTION_FAIL;
3000 }
3001
3002 for (i = start; i < end; i += step) {
3003 MEM(dst = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
3004 dst->vb_uint64 = i;
3005 fr_dcursor_append(out, dst);
3006 }
3007
3008 return XLAT_ACTION_DONE;
3009}
3010
3011static int CC_HINT(nonnull(2,3)) regex_xlat_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
3012{
3013 ssize_t slen;
3014 fr_sbuff_t *out = NULL;
3015 fr_value_box_entry_t entry;
3016
3017 FR_SBUFF_TALLOC_THREAD_LOCAL(&out, 256, 4096);
3018
3019 slen = fr_value_box_print(out, vb, &regex_escape_rules);
3020 if (slen < 0) return -1;
3021
3022 entry = vb->entry;
3024 (void) fr_value_box_bstrndup(vb, vb, NULL, fr_sbuff_start(out), fr_sbuff_used(out), false);
3025 vb->entry = entry;
3026
3027 return 0;
3028}
3029
3034
3035
3036/** Get named subcapture value from previous regex
3037 *
3038 * Example:
3039@verbatim
3040if ("foo" =~ /^(?<name>.*)/) {
3041 noop
3042}
3043%regex.match(name) == "foo"
3044@endverbatim
3045 *
3046 * @ingroup xlat_functions
3047 */
3049 UNUSED xlat_ctx_t const *xctx,
3050 request_t *request, fr_value_box_list_t *in)
3051{
3052 fr_value_box_t *in_head = fr_value_box_list_head(in);
3053
3054 /*
3055 * Find the first child of the first argument group
3056 */
3057 fr_value_box_t *arg = fr_value_box_list_head(&in_head->vb_group);
3058
3059 /*
3060 * Return the complete capture if no other capture is specified
3061 */
3062 if (!arg) {
3063 fr_value_box_t *vb;
3064
3065 MEM(vb = fr_value_box_alloc_null(ctx));
3066 if (regex_request_to_sub(vb, vb, request, 0) < 0) {
3067 REDEBUG2("No previous regex capture");
3068 talloc_free(vb);
3069 return XLAT_ACTION_FAIL;
3070 }
3071
3073
3074 return XLAT_ACTION_DONE;
3075 }
3076
3077 switch (arg->type) {
3078 /*
3079 * If the input is an integer value then get an
3080 * arbitrary subcapture index.
3081 */
3082 case FR_TYPE_NUMERIC:
3083 {
3084 fr_value_box_t idx;
3085 fr_value_box_t *vb;
3086
3087 if (fr_value_box_list_next(in, in_head)) {
3088 REDEBUG("Only one subcapture argument allowed");
3089 return XLAT_ACTION_FAIL;
3090 }
3091
3092 if (fr_value_box_cast(NULL, &idx, FR_TYPE_UINT32, NULL, arg) < 0) {
3093 RPEDEBUG("Bad subcapture index");
3094 return XLAT_ACTION_FAIL;
3095 }
3096
3097 MEM(vb = fr_value_box_alloc_null(ctx));
3098 if (regex_request_to_sub(vb, vb, request, idx.vb_uint32) < 0) {
3099 REDEBUG2("No previous numbered regex capture group '%u'", idx.vb_uint32);
3100 talloc_free(vb);
3101 return XLAT_ACTION_DONE;
3102 }
3104
3105 return XLAT_ACTION_DONE;
3106 }
3107
3108 default:
3109#if defined(HAVE_REGEX_PCRE) || defined(HAVE_REGEX_PCRE2)
3110 {
3111 fr_value_box_t *vb;
3112
3113 /*
3114 * Concatenate all input
3115 */
3117 arg, &in_head->vb_group, FR_TYPE_STRING,
3119 SIZE_MAX) < 0) {
3120 RPEDEBUG("Failed concatenating input");
3121 return XLAT_ACTION_FAIL;
3122 }
3123
3124 MEM(vb = fr_value_box_alloc_null(ctx));
3125 if (regex_request_to_sub_named(vb, vb, request, arg->vb_strvalue) < 0) {
3126 REDEBUG2("No previous named regex capture group '%s'", arg->vb_strvalue);
3127 talloc_free(vb);
3128 return XLAT_ACTION_DONE; /* NOT an error, just an empty result */
3129 }
3131
3132 return XLAT_ACTION_DONE;
3133 }
3134#else
3135 RDEBUG("Named regex captures are not supported (they require libpcre2)");
3136 return XLAT_ACTION_FAIL;
3137#endif
3138 }
3139}
3140
3142 { .concat = true, .type = FR_TYPE_OCTETS },
3144};
3145
3146/** Calculate the SHA1 hash of a string or attribute.
3147 *
3148 * Example:
3149@verbatim
3150%sha1(foo) == "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
3151@endverbatim
3152 *
3153 * @ingroup xlat_functions
3154 */
3156 UNUSED xlat_ctx_t const *xctx,
3157 UNUSED request_t *request, fr_value_box_list_t *args)
3158{
3160 fr_sha1_ctx sha1_ctx;
3161 fr_value_box_t *vb;
3162 fr_value_box_t *in_head;
3163
3164 XLAT_ARGS(args, &in_head);
3165
3166 fr_sha1_init(&sha1_ctx);
3167 if (in_head) {
3168 fr_sha1_update(&sha1_ctx, in_head->vb_octets, in_head->vb_length);
3169 } else {
3170 /* sha1 of empty string */
3171 fr_sha1_update(&sha1_ctx, NULL, 0);
3172 }
3173 fr_sha1_final(digest, &sha1_ctx);
3174
3175 MEM(vb = fr_value_box_alloc_null(ctx));
3176 fr_value_box_memdup(vb, vb, NULL, digest, sizeof(digest), false);
3177
3179
3180 return XLAT_ACTION_DONE;
3181}
3182
3183/** Calculate any digest supported by OpenSSL EVP_MD
3184 *
3185 * Example:
3186@verbatim
3187%sha2_256(foo) == "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
3188@endverbatim
3189 *
3190 * @ingroup xlat_functions
3191 */
3192#ifdef HAVE_OPENSSL_EVP_H
3193static xlat_action_t xlat_evp_md(TALLOC_CTX *ctx, fr_dcursor_t *out,
3194 UNUSED xlat_ctx_t const *xctx,
3195 UNUSED request_t *request, fr_value_box_list_t *args, EVP_MD const *md)
3196{
3197 uint8_t digest[EVP_MAX_MD_SIZE];
3198 unsigned int digestlen;
3199 EVP_MD_CTX *md_ctx;
3200 fr_value_box_t *vb;
3201 fr_value_box_t *in_head;
3202
3203 XLAT_ARGS(args, &in_head);
3204
3205 md_ctx = EVP_MD_CTX_create();
3206 EVP_DigestInit_ex(md_ctx, md, NULL);
3207 if (in_head) {
3208 EVP_DigestUpdate(md_ctx, in_head->vb_octets, in_head->vb_length);
3209 } else {
3210 EVP_DigestUpdate(md_ctx, NULL, 0);
3211 }
3212 EVP_DigestFinal_ex(md_ctx, digest, &digestlen);
3213 EVP_MD_CTX_destroy(md_ctx);
3214
3215 MEM(vb = fr_value_box_alloc_null(ctx));
3216 fr_value_box_memdup(vb, vb, NULL, digest, digestlen, false);
3217
3219
3220 return XLAT_ACTION_DONE;
3221}
3222
3223# define EVP_MD_XLAT(_md, _md_func) \
3224static xlat_action_t xlat_func_##_md(TALLOC_CTX *ctx, fr_dcursor_t *out,\
3225 xlat_ctx_t const *xctx, \
3226 request_t *request,\
3227 fr_value_box_list_t *in)\
3228{\
3229 return xlat_evp_md(ctx, out, xctx, request, in, EVP_##_md_func());\
3230}
3231
3232EVP_MD_XLAT(sha2_224, sha224)
3233EVP_MD_XLAT(sha2_256, sha256)
3234EVP_MD_XLAT(sha2_384, sha384)
3235EVP_MD_XLAT(sha2_512, sha512)
3236
3237/*
3238 * OpenWRT's OpenSSL library doesn't contain these by default
3239 */
3240#ifdef HAVE_EVP_BLAKE2S256
3241EVP_MD_XLAT(blake2s_256, blake2s256)
3242#endif
3243
3244#ifdef HAVE_EVP_BLAKE2B512
3245EVP_MD_XLAT(blake2b_512, blake2b512)
3246#endif
3247
3248EVP_MD_XLAT(sha3_224, sha3_224)
3249EVP_MD_XLAT(sha3_256, sha3_256)
3250EVP_MD_XLAT(sha3_384, sha3_384)
3251EVP_MD_XLAT(sha3_512, sha3_512)
3252#endif
3253
3254
3256 { .required = true, .concat = true, .type = FR_TYPE_STRING },
3258};
3259
3261 { .concat = true, .type = FR_TYPE_STRING },
3263};
3264
3265/** Print length of given string
3266 *
3267 * Example:
3268@verbatim
3269%strlen(foo) == 3
3270@endverbatim
3271 *
3272 * @see #xlat_func_length
3273 *
3274 * @ingroup xlat_functions
3275 */
3277 UNUSED xlat_ctx_t const *xctx,
3278 UNUSED request_t *request, fr_value_box_list_t *args)
3279{
3280 fr_value_box_t *vb;
3281 fr_value_box_t *in_head;
3282
3283 XLAT_ARGS(args, &in_head);
3284
3285 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_SIZE, NULL));
3286
3287 if (!in_head) {
3288 vb->vb_size = 0;
3289 } else {
3290 vb->vb_size = strlen(in_head->vb_strvalue);
3291 }
3292
3294
3295 return XLAT_ACTION_DONE;
3296}
3297
3299 { .concat = true, .type = FR_TYPE_STRING, .required = true, },
3300 { .single = true, .type = FR_TYPE_BOOL },
3302};
3303
3304/** Return whether a string has only printable chars
3305 *
3306 * This function returns true if the input string contains UTF8 sequences and printable chars.
3307 *
3308 * @note "\t" and " " are considered unprintable chars, unless the second argument(relaxed) is true.
3309 *
3310 * Example:
3311@verbatim
3312%str.printable("🍉abcdef🍓") == true
3313%str.printable("\000\n\r\t") == false
3314%str.printable("\t abcd", yes) == true
3315@endverbatim
3316 *
3317 * @ingroup xlat_functions
3318 */
3320 UNUSED xlat_ctx_t const *xctx,
3321 UNUSED request_t *request, fr_value_box_list_t *args)
3322{
3323 fr_value_box_t *vb;
3324 fr_value_box_t *str;
3325 fr_value_box_t *relaxed_vb;
3326 uint8_t const *p, *end;
3327 bool relaxed = false;
3328
3329 XLAT_ARGS(args, &str, &relaxed_vb);
3330
3331 if (relaxed_vb) relaxed = relaxed_vb->vb_bool;
3332
3333 p = (uint8_t const *)str->vb_strvalue;
3334 end = p + str->vb_length;
3335
3336 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
3338 vb->vb_bool = false;
3339
3340 do {
3341 size_t clen;
3342
3343 if ((*p < '!') &&
3344 (!relaxed || ((*p != '\t') && (*p != ' ')))) return XLAT_ACTION_DONE;
3345
3346 if (*p == 0x7f) return XLAT_ACTION_DONE;
3347
3348 clen = fr_utf8_char(p, end - p);
3349 if (clen == 0) return XLAT_ACTION_DONE;
3350 p += clen;
3351 } while (p < end);
3352
3353 vb->vb_bool = true;
3354
3355 return XLAT_ACTION_DONE;
3356}
3357
3359 { .concat = true, .type = FR_TYPE_STRING },
3361};
3362
3363/** Return whether a string is valid UTF-8
3364 *
3365 * This function returns true if the input string is valid UTF-8, false otherwise.
3366 *
3367 * Example:
3368@verbatim
3369%str.utf8(🍉🥝🍓) == true
3370%str.utf8(🍉\xff🍓) == false
3371@endverbatim
3372 *
3373 * @ingroup xlat_functions
3374 */
3376 UNUSED xlat_ctx_t const *xctx,
3377 UNUSED request_t *request, fr_value_box_list_t *args)
3378{
3379 fr_value_box_t *vb;
3380 fr_value_box_t *in_head;
3381
3382 XLAT_ARGS(args, &in_head);
3383
3384 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
3385 vb->vb_bool = (fr_utf8_str((uint8_t const *)in_head->vb_strvalue,
3386 in_head->vb_length) >= 0);
3387
3389
3390 return XLAT_ACTION_DONE;
3391}
3392
3394 { .single = true, .required = true, .type = FR_TYPE_VOID },
3395 { .single = true, .required = true, .type = FR_TYPE_INT32 },
3396 { .single = true, .type = FR_TYPE_INT32 },
3398};
3399
3400/** Extract a substring from string / octets data
3401 *
3402 * Non string / octets data is cast to a string.
3403 *
3404 * Second parameter is start position, optional third parameter is length
3405 * Negative start / length count from RHS of data.
3406 *
3407 * Example: (User-Name = "hello")
3408@verbatim
3409%substr(&User-Name, 1, 3) == 'ell'
3410@endverbatim
3411 *
3412 * @ingroup xlat_functions
3413 */
3414static xlat_action_t xlat_func_substr(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx,
3415 request_t *request, fr_value_box_list_t *args)
3416{
3417 fr_value_box_t *in = NULL, *start_vb, *len_vb, *vb;
3418 int32_t start, end, len;
3419
3420 XLAT_ARGS(args, &in, &start_vb, &len_vb);
3421
3422 switch (in->type) {
3423 case FR_TYPE_OCTETS:
3424 case FR_TYPE_STRING:
3425 break;
3426
3427 default:
3429 RPEDEBUG("Failed casting value to string");
3430 return XLAT_ACTION_FAIL;
3431 }
3432 break;
3433 }
3434
3435 if (start_vb->vb_int32 > (int32_t)in->vb_length) return XLAT_ACTION_DONE;
3436
3437 if (start_vb->vb_int32 < 0) {
3438 start = in->vb_length + start_vb->vb_int32;
3439 if (start < 0) start = 0;
3440 } else {
3441 start = start_vb->vb_int32;
3442 }
3443
3444 if (len_vb) {
3445 if (len_vb->vb_int32 < 0) {
3446 end = in->vb_length + len_vb->vb_int32;
3447 if (end < 0) return XLAT_ACTION_DONE;
3448 } else {
3449 end = start + len_vb->vb_int32;
3450 if (end > (int32_t)in->vb_length) end = in->vb_length;
3451 }
3452 } else {
3453 end = in->vb_length;
3454 }
3455
3456 if (start >= end) return XLAT_ACTION_DONE;
3457
3458 MEM(vb = fr_value_box_alloc(ctx, in->type, NULL));
3459
3460 len = end - start;
3461 switch (in->type) {
3462 case FR_TYPE_STRING:
3463 fr_value_box_bstrndup(vb, vb, NULL, &in->vb_strvalue[start], len, false);
3464 break;
3465 case FR_TYPE_OCTETS:
3466 {
3467 uint8_t *buf;
3468 fr_value_box_mem_alloc(vb, &buf, vb, NULL, len, false);
3469 memcpy(buf, &in->vb_octets[start], len);
3470 }
3471 break;
3472
3473 default: /* 'in' was cast to #FR_TYPE_STRING */
3474 fr_assert(0);
3475 }
3476
3479
3480 return XLAT_ACTION_DONE;
3481}
3482
3483#ifdef HAVE_REGEX_PCRE2
3484/** Cache statically compiled expressions
3485 */
3486typedef struct {
3487 regex_t *pattern;
3488 fr_regex_flags_t flags;
3489} xlat_subst_regex_inst_t;
3490
3491/** Pre-compile regexes where possible
3492 */
3493static int xlat_instantiate_subst_regex(xlat_inst_ctx_t const *xctx)
3494{
3495 xlat_subst_regex_inst_t *inst = talloc_get_type_abort(xctx->inst, xlat_subst_regex_inst_t);
3496 xlat_exp_t *patt_exp;
3497 fr_sbuff_t sbuff;
3498 fr_sbuff_marker_t start_m, end_m;
3499
3500 /* args #2 (pattern) */
3501 patt_exp = fr_dlist_next(&xctx->ex->call.args->dlist, fr_dlist_head(&xctx->ex->call.args->dlist));
3502 fr_assert(patt_exp && patt_exp->type == XLAT_GROUP); /* args must be groups */
3503
3504 /* If there are dynamic expansions, we can't pre-compile */
3505 if (!xlat_is_literal(patt_exp->group)) return 0;
3506 fr_assert(fr_dlist_num_elements(&patt_exp->group->dlist) == 1);
3507
3508 patt_exp = fr_dlist_head(&patt_exp->group->dlist);
3509
3510 /* We can only pre-compile strings */
3511 if (!fr_type_is_string(patt_exp->data.type)) return 0;
3512
3513 sbuff = FR_SBUFF_IN(patt_exp->data.vb_strvalue, patt_exp->data.vb_length);
3514
3515 /* skip any whitesapce */
3516 fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, 0);
3517
3518 /* Is the next char a forward slash? */
3519 if (fr_sbuff_next_if_char(&sbuff, '/')) {
3520 fr_slen_t slen;
3521
3522 fr_sbuff_marker(&start_m, &sbuff);
3523
3524 if (!fr_sbuff_adv_to_chr(&sbuff, SIZE_MAX, '/')) return 0; /* Not a regex */
3525
3526 fr_sbuff_marker(&end_m, &sbuff);
3527 fr_sbuff_next(&sbuff); /* skip trailing slash */
3528
3529 if (fr_sbuff_remaining(&sbuff)) {
3530 slen = regex_flags_parse(NULL, &inst->flags,
3531 &sbuff,
3532 NULL, true);
3533 if (slen < 0) {
3534 PERROR("Failed parsing regex flags in \"%s\"", patt_exp->data.vb_strvalue);
3535 return -1;
3536 }
3537 }
3538
3539 if (regex_compile(inst, &inst->pattern,
3540 fr_sbuff_current(&start_m), fr_sbuff_current(&end_m) - fr_sbuff_current(&start_m),
3541 &inst->flags, true, false) <= 0) {
3542 PERROR("Failed compiling regex \"%s\"", patt_exp->data.vb_strvalue);
3543 return -1;
3544 }
3545 }
3546 /* No... then it's not a regex */
3547
3548 return 0;
3549}
3550
3551/** Perform regex substitution TODO CHECK
3552 *
3553 * Called when %subst() pattern begins with "/"
3554 *
3555@verbatim
3556%subst(<subject>, /<regex>/[flags], <replace>)
3557@endverbatim
3558 *
3559 * Example: (User-Name = "foo")
3560@verbatim
3561%subst(%{User-Name}, /oo.*$/, 'un') == "fun"
3562@endverbatim
3563 *
3564 * @note References can be specified in the replacement string with $<ref>
3565 *
3566 * @see #xlat_func_subst
3567 *
3568 * @ingroup xlat_functions
3569 */
3570static int xlat_func_subst_regex(TALLOC_CTX *ctx, fr_dcursor_t *out,
3571 xlat_ctx_t const *xctx, request_t *request,
3572 fr_value_box_list_t *args)
3573{
3574 xlat_subst_regex_inst_t const *inst = talloc_get_type_abort_const(xctx->inst, xlat_subst_regex_inst_t);
3575 fr_sbuff_t sbuff;
3576 fr_sbuff_marker_t start_m, end_m;
3577 char *buff;
3578 ssize_t slen;
3579 regex_t *pattern, *our_pattern = NULL;
3580 fr_regex_flags_t const *flags;
3581 fr_regex_flags_t our_flags = {};
3582 fr_value_box_t *vb;
3583 fr_value_box_t *subject_vb;
3584 fr_value_box_t *regex_vb;
3585 fr_value_box_t *rep_vb;
3586
3587 XLAT_ARGS(args, &subject_vb, &regex_vb, &rep_vb);
3588
3589 /*
3590 * Was not pre-compiled, so we need to compile it now
3591 */
3592 if (!inst->pattern) {
3593 sbuff = FR_SBUFF_IN(regex_vb->vb_strvalue, regex_vb->vb_length);
3594 if (fr_sbuff_len(&sbuff) == 0) {
3595 REDEBUG("Regex must not be empty");
3596 return XLAT_ACTION_FAIL;
3597 }
3598
3599 fr_sbuff_next(&sbuff); /* skip leading slash */
3600 fr_sbuff_marker(&start_m, &sbuff);
3601
3602 if (!fr_sbuff_adv_to_chr(&sbuff, SIZE_MAX, '/')) return 1; /* Not a regex */
3603
3604 fr_sbuff_marker(&end_m, &sbuff);
3605 fr_sbuff_next(&sbuff); /* skip trailing slash */
3606
3607 slen = regex_flags_parse(NULL, &our_flags, &sbuff, NULL, true);
3608 if (slen < 0) {
3609 RPEDEBUG("Failed parsing regex flags");
3610 return -1;
3611 }
3612
3613 /*
3614 * Process the substitution
3615 */
3616 if (regex_compile(NULL, &our_pattern,
3617 fr_sbuff_current(&start_m), fr_sbuff_current(&end_m) - fr_sbuff_current(&start_m),
3618 &our_flags, true, true) <= 0) {
3619 RPEDEBUG("Failed compiling regex");
3620 return -1;
3621 }
3622 pattern = our_pattern;
3623 flags = &our_flags;
3624 } else {
3625 pattern = inst->pattern;
3626 flags = &inst->flags;
3627 }
3628
3629 MEM(vb = fr_value_box_alloc_null(ctx));
3630 if (regex_substitute(vb, &buff, 0, pattern, flags,
3631 subject_vb->vb_strvalue, subject_vb->vb_length,
3632 rep_vb->vb_strvalue, rep_vb->vb_length, NULL) < 0) {
3633 RPEDEBUG("Failed performing substitution");
3634 talloc_free(vb);
3635 talloc_free(pattern);
3636 return -1;
3637 }
3638 fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, buff, false);
3639
3640 fr_value_box_safety_copy(vb, subject_vb);
3641 fr_value_box_safety_merge(vb, rep_vb);
3642
3644
3645 talloc_free(our_pattern);
3646
3647 return 0;
3648}
3649#endif
3650
3652 { .required = true, .concat = true, .type = FR_TYPE_STRING },
3653 { .required = true, .concat = true, .type = FR_TYPE_STRING },
3654 { .required = true, .concat = true, .type = FR_TYPE_STRING },
3656};
3657
3658/** Perform regex substitution
3659 *
3660@verbatim
3661%subst(<subject>, <pattern>, <replace>)
3662@endverbatim
3663 *
3664 * Example: (User-Name = "foobar")
3665@verbatim
3666%subst(%{User-Name}, 'oo', 'un') == "funbar"
3667@endverbatim
3668 *
3669 * @see xlat_func_subst_regex
3670 *
3671 * @ingroup xlat_functions
3672 */
3674#ifdef HAVE_REGEX_PCRE2
3675 xlat_ctx_t const *xctx,
3676#else
3677 UNUSED xlat_ctx_t const *xctx,
3678#endif
3679 request_t *request, fr_value_box_list_t *args)
3680{
3681 char const *p, *q, *end;
3682 char *vb_str;
3683
3684 char const *pattern, *rep;
3685 size_t pattern_len, rep_len;
3686
3687 fr_value_box_t *rep_vb, *vb;
3688 fr_value_box_t *subject_vb;
3689 fr_value_box_t *pattern_vb;
3690
3691 XLAT_ARGS(args, &subject_vb, &pattern_vb, &rep_vb);
3692
3693 /* coverity[dereference] */
3694 pattern = pattern_vb->vb_strvalue;
3695 if (*pattern == '/') {
3696#ifdef HAVE_REGEX_PCRE2
3697 switch (xlat_func_subst_regex(ctx, out, xctx, request, args)) {
3698 case 0:
3699 return XLAT_ACTION_DONE;
3700
3701 case 1:
3702 /* Not a regex, fall through */
3703 break;
3704
3705 case -1:
3706 return XLAT_ACTION_FAIL;
3707 }
3708#else
3709 if (memchr(pattern, '/', pattern_vb->vb_length - 1)) {
3710 REDEBUG("regex based substitutions require libpcre2. "
3711 "Check ${features.regex-pcre2} to determine support");
3712 }
3713 return XLAT_ACTION_FAIL;
3714#endif
3715 }
3716
3717 /*
3718 * Check for empty pattern
3719 */
3720 pattern_len = pattern_vb->vb_length;
3721 if (pattern_len == 0) {
3722 REDEBUG("Empty pattern");
3723 return XLAT_ACTION_FAIL;
3724 }
3725
3726 rep = rep_vb->vb_strvalue;
3727 rep_len = rep_vb->vb_length;
3728
3729 p = subject_vb->vb_strvalue;
3730 end = p + subject_vb->vb_length;
3731
3732 MEM(vb = fr_value_box_alloc_null(ctx));
3733 vb_str = talloc_bstrndup(vb, "", 0);
3734
3735 while (p < end) {
3736 q = memmem(p, end - p, pattern, pattern_len);
3737 if (!q) {
3738 MEM(vb_str = talloc_bstr_append(vb, vb_str, p, end - p));
3739 break;
3740 }
3741
3742 if (q > p) MEM(vb_str = talloc_bstr_append(vb, vb_str, p, q - p));
3743 if (rep_len) MEM(vb_str = talloc_bstr_append(vb, vb_str, rep, rep_len));
3744 p = q + pattern_len;
3745 }
3746
3747 if (fr_value_box_bstrdup_buffer_shallow(NULL, vb, NULL, vb_str, false) < 0) {
3748 RPEDEBUG("Failed creating output box");
3749 talloc_free(vb);
3750 return XLAT_ACTION_FAIL;
3751 }
3752
3753 fr_value_box_safety_copy(vb, subject_vb);
3754 fr_value_box_safety_merge(vb, rep_vb);
3755
3757
3758 return XLAT_ACTION_DONE;
3759}
3760
3761/*
3762 * Debug builds only, we don't want to allow unsanitised inputs to crash the server
3763 */
3764#ifndef NDEBUG
3766 { .single = true, .required = true, .type = FR_TYPE_STRING },
3768};
3769
3771 UNUSED xlat_ctx_t const *xctx, request_t *request,
3772 fr_value_box_list_t *args)
3773{
3774 static fr_table_num_sorted_t const signal_table[] = {
3775 { L("break"), SIGTRAP }, /* Save flailing at the keyboard */
3776 { L("BREAK"), SIGTRAP },
3777 { L("SIGABRT"), SIGABRT },
3778 { L("SIGALRM"), SIGALRM },
3779#ifdef SIGBUS
3780 { L("SIGBUS"), SIGBUS },
3781#endif
3782 { L("SIGCHLD"), SIGCHLD },
3783 { L("SIGCONT"), SIGCONT },
3784 { L("SIGFPE"), SIGFPE },
3785 { L("SIGHUP"), SIGHUP },
3786 { L("SIGILL"), SIGILL },
3787 { L("SIGINT"), SIGINT },
3788 { L("SIGKILL"), SIGKILL },
3789 { L("SIGPIPE"), SIGPIPE },
3790#ifdef SIGPOLL
3791 { L("SIGPOLL"), SIGPOLL },
3792#endif
3793 { L("SIGPROF"), SIGPROF },
3794 { L("SIGQUIT"), SIGQUIT },
3795 { L("SIGSEGV"), SIGSEGV },
3796 { L("SIGSTOP"), SIGSTOP },
3797#ifdef SIGSYS
3798 { L("SIGSYS"), SIGSYS },
3799#endif
3800 { L("SIGTERM"), SIGTERM },
3801#ifdef SIGTRAP
3802 { L("SIGTRAP"), SIGTRAP },
3803#endif
3804 { L("SIGTSTP"), SIGTSTP },
3805 { L("SIGTTIN"), SIGTTIN },
3806 { L("SIGTTOU"), SIGTTOU },
3807 { L("SIGURG"), SIGURG },
3808 { L("SIGUSR1"), SIGUSR1 },
3809 { L("SIGUSR2"), SIGUSR2 },
3810 { L("SIGVTALRM"), SIGVTALRM },
3811 { L("SIGXCPU"), SIGXCPU },
3812 { L("SIGXFSZ"), SIGXFSZ }
3813 };
3814 static size_t signal_table_len = NUM_ELEMENTS(signal_table);
3815
3816 fr_value_box_t *signal_vb;
3817 int signal;
3818
3819 XLAT_ARGS(args, &signal_vb);
3820
3821 signal = fr_table_value_by_substr(signal_table, signal_vb->vb_strvalue, signal_vb->vb_length, -1);
3822 if (signal < 0) {
3823 RERROR("Invalid signal \"%pV\"", signal_vb);
3824 return XLAT_ACTION_FAIL;
3825 }
3826 if (raise(signal) < 0) {
3827 RERROR("Failed raising signal %d: %s", signal, strerror(errno));
3828 return XLAT_ACTION_FAIL;
3829 }
3830 return XLAT_ACTION_DONE;
3831}
3832#endif
3833
3835 { .required = false, .single = true, .type = FR_TYPE_STRING },
3837};
3838
3839/** Return the time as a #FR_TYPE_DATE
3840 *
3841 * Note that all operations are UTC.
3842 *
3843@verbatim
3844%time()
3845@endverbatim
3846 *
3847 * Example:
3848@verbatim
3849update reply {
3850 &Reply-Message := "%{%time(now) - %time(request)}"
3851}
3852@endverbatim
3853 *
3854 * @ingroup xlat_functions
3855 */
3857 UNUSED xlat_ctx_t const *xctx,
3858 request_t *request, fr_value_box_list_t *args)
3859{
3860 fr_value_box_t *arg;
3861 fr_value_box_t *vb;
3863
3864 XLAT_ARGS(args, &arg);
3865
3866 if (!arg || (strcmp(arg->vb_strvalue, "now") == 0)) {
3868
3869 } else if (strcmp(arg->vb_strvalue, "request") == 0) {
3870 value = fr_time_to_unix_time(request->packet->timestamp);
3871
3872 } else if (strcmp(arg->vb_strvalue, "offset") == 0) {
3873 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
3874 vb->vb_time_delta = fr_time_gmtoff();
3875 goto append;
3876
3877 } else if (strcmp(arg->vb_strvalue, "dst") == 0) {
3878 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
3879 vb->vb_bool = fr_time_is_dst();
3880 goto append;
3881
3882 } else if (strcmp(arg->vb_strvalue, "mday_offset") == 0) {
3883 struct tm tm;
3884 fr_unix_time_t unix_time = fr_time_to_unix_time(request->packet->timestamp);
3885 time_t when = fr_unix_time_to_sec(unix_time);
3886 int64_t nsec;
3887
3888 gmtime_r(&when, &tm);
3889
3890 nsec = (int64_t) 86400 * (tm.tm_mday - 1);
3891 nsec += when % 86400;
3892 nsec *= NSEC;
3893 nsec += fr_unix_time_unwrap(unix_time) % NSEC;
3894
3895 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
3896 vb->vb_time_delta = fr_time_delta_wrap(nsec);
3897 goto append;
3898
3899 } else if (strcmp(arg->vb_strvalue, "wday_offset") == 0) {
3900 struct tm tm;
3901 fr_unix_time_t unix_time = fr_time_to_unix_time(request->packet->timestamp);
3902 time_t when = fr_unix_time_to_sec(unix_time);
3903 int64_t nsec;
3904
3905 gmtime_r(&when, &tm);
3906
3907 nsec = (int64_t) 86400 * tm.tm_wday;
3908 nsec += when % 86400;
3909 nsec *= NSEC;
3910 nsec += fr_unix_time_unwrap(unix_time) % NSEC;
3911
3912 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
3913 vb->vb_time_delta = fr_time_delta_wrap(nsec);
3914 goto append;
3915
3916 } else if (fr_unix_time_from_str(&value, arg->vb_strvalue, FR_TIME_RES_SEC) < 0) {
3917 REDEBUG("Invalid time specification '%s'", arg->vb_strvalue);
3918 return XLAT_ACTION_FAIL;
3919 }
3920
3921 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_DATE, NULL));
3922 vb->vb_date = value;
3923
3924append:
3926
3927 return XLAT_ACTION_DONE;
3928}
3929
3930/** Return the current time as a #FR_TYPE_DATE
3931 *
3932 * Note that all operations are UTC.
3933 *
3934@verbatim
3935%time.now()
3936@endverbatim
3937 *
3938 * Example:
3939@verbatim
3940update reply {
3941 &Reply-Message := "%{%time.now() - %time.request()}"
3942}
3943@endverbatim
3944 *
3945 * @ingroup xlat_functions
3946 */
3948 UNUSED xlat_ctx_t const *xctx,
3949 UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
3950{
3951 fr_value_box_t *vb;
3952
3953 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_DATE, NULL));
3954 vb->vb_date = fr_time_to_unix_time(fr_time());
3955
3957
3958 return XLAT_ACTION_DONE;
3959}
3960
3961/** Return the request receive time as a #FR_TYPE_DATE
3962 *
3963 * Note that all operations are UTC.
3964 *
3965@verbatim
3966%time.request()
3967@endverbatim
3968 *
3969 * Example:
3970@verbatim
3971update reply {
3972 &Reply-Message := "%{%time.now() - %time.request()}"
3973}
3974@endverbatim
3975 *
3976 * @ingroup xlat_functions
3977 */
3979 UNUSED xlat_ctx_t const *xctx,
3980 request_t *request, UNUSED fr_value_box_list_t *args)
3981{
3982 fr_value_box_t *vb;
3983
3984 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_DATE, NULL));
3985 vb->vb_date = fr_time_to_unix_time(request->packet->timestamp);
3986
3988
3989 return XLAT_ACTION_DONE;
3990}
3991
3992
3993/** Return the current time offset from gmt
3994 *
3995 * @ingroup xlat_functions
3996 */
3998 UNUSED xlat_ctx_t const *xctx,
3999 UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
4000{
4001 fr_value_box_t *vb;
4002
4003 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
4004 vb->vb_time_delta = fr_time_gmtoff();
4005
4007
4008 return XLAT_ACTION_DONE;
4009}
4010
4011
4012/** Return whether we are in daylight savings or not
4013 *
4014 * @ingroup xlat_functions
4015 */
4017 UNUSED xlat_ctx_t const *xctx,
4018 UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
4019{
4020 fr_value_box_t *vb;
4021
4022 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_BOOL, NULL));
4023 vb->vb_bool = fr_time_is_dst();
4024
4026
4027 return XLAT_ACTION_DONE;
4028}
4029
4030
4031/** Change case of a string
4032 *
4033 * If upper is true, change to uppercase, otherwise, change to lowercase
4034 */
4036 UNUSED request_t *request, fr_value_box_list_t *args, bool upper)
4037{
4038 char *p;
4039 char const *end;
4040 fr_value_box_t *vb;
4041
4042 XLAT_ARGS(args, &vb);
4043
4044 p = UNCONST(char *, vb->vb_strvalue);
4045 end = p + vb->vb_length;
4046
4047 while (p < end) {
4048 *(p) = upper ? toupper ((uint8_t) *(p)) : tolower((uint8_t) *(p));
4049 p++;
4050 }
4051
4052 xlat_arg_copy_out(ctx, out, args, vb);
4053
4054 return XLAT_ACTION_DONE;
4055}
4056
4058 { .required = true, .concat = true, .type = FR_TYPE_STRING },
4060};
4061
4062
4063/** Convert a string to lowercase
4064 *
4065 * Example:
4066@verbatim
4067%tolower("Bar") == "bar"
4068@endverbatim
4069 *
4070 * Probably only works for ASCII
4071 *
4072 * @ingroup xlat_functions
4073 */
4075 UNUSED xlat_ctx_t const *xctx,
4076 request_t *request, fr_value_box_list_t *in)
4077{
4078 return xlat_change_case(ctx, out, request, in, false);
4079}
4080
4081
4082/** Convert a string to uppercase
4083 *
4084 * Example:
4085@verbatim
4086%toupper("Foo") == "FOO"
4087@endverbatim
4088 *
4089 * Probably only works for ASCII
4090 *
4091 * @ingroup xlat_functions
4092 */
4094 UNUSED xlat_ctx_t const *xctx,
4095 request_t *request, fr_value_box_list_t *in)
4096{
4097 return xlat_change_case(ctx, out, request, in, true);
4098}
4099
4100
4102 { .required = true, .concat = true, .type = FR_TYPE_STRING },
4104};
4105
4106/** URLencode special characters
4107 *
4108 * Example:
4109@verbatim
4110%urlquote("http://example.org/") == "http%3A%47%47example.org%47"
4111@endverbatim
4112 *
4113 * @ingroup xlat_functions
4114 */
4116 UNUSED xlat_ctx_t const *xctx,
4117 UNUSED request_t *request, fr_value_box_list_t *args)
4118{
4119 char const *p, *end;
4120 char *buff_p;
4121 size_t outlen = 0;
4122 fr_value_box_t *vb;
4123 fr_value_box_t *in_head;
4124
4125 XLAT_ARGS(args, &in_head);
4126
4127 p = in_head->vb_strvalue;
4128 end = p + in_head->vb_length;
4129
4130 /*
4131 * Calculate size of output
4132 */
4133 while (p < end) {
4134 if (isalnum(*p) ||
4135 *p == '-' ||
4136 *p == '_' ||
4137 *p == '.' ||
4138 *p == '~') {
4139 outlen++;
4140 } else {
4141 outlen += 3;
4142 }
4143 p++;
4144 }
4145
4146 MEM(vb = fr_value_box_alloc_null(ctx));
4147 MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, outlen, false) == 0);
4148 fr_value_box_safety_copy(vb, in_head);
4149
4150 /* Reset p to start position */
4151 p = in_head->vb_strvalue;
4152
4153 while (p < end) {
4154 if (isalnum(*p)) {
4155 *buff_p++ = *p++;
4156 continue;
4157 }
4158
4159 switch (*p) {
4160 case '-':
4161 case '_':
4162 case '.':
4163 case '~':
4164 *buff_p++ = *p++;
4165 break;
4166
4167 default:
4168 /* MUST be upper case hex to be compliant */
4169 snprintf(buff_p, 4, "%%%02X", (uint8_t) *p++); /* %XX */
4170
4171 buff_p += 3;
4172 }
4173 }
4174
4175 *buff_p = '\0';
4176
4177 // @todo - mark as safe for URL?
4179
4180 return XLAT_ACTION_DONE;
4181}
4182
4183
4185 { .required = true, .concat = true, .type = FR_TYPE_STRING },
4187};
4188
4189/** URLdecode special characters
4190 *
4191 * @note Remember to escape % with %% in strings, else xlat will try to parse it.
4192 *
4193 * Example:
4194@verbatim
4195%urlunquote("http%%3A%%47%%47example.org%%47") == "http://example.org/"
4196@endverbatim
4197 *
4198 * @ingroup xlat_functions
4199 */
4201 UNUSED xlat_ctx_t const *xctx,
4202 request_t *request, fr_value_box_list_t *args)
4203{
4204 char const *p, *end;
4205 char *buff_p;
4206 char *c1, *c2;
4207 size_t outlen = 0;
4208 fr_value_box_t *vb;
4209 fr_value_box_t *in_head;
4210
4211 XLAT_ARGS(args, &in_head);
4212
4213 p = in_head->vb_strvalue;
4214 end = p + in_head->vb_length;
4215
4216 /*
4217 * Calculate size of output
4218 */
4219 while (p < end) {
4220 if (*p == '%') {
4221 if (!p[1] || !p[2]) {
4222 REMARKER(in_head->vb_strvalue, p - in_head->vb_strvalue, "Invalid %% sequence");
4223 return XLAT_ACTION_FAIL;
4224 }
4225 p += 3;
4226 } else {
4227 p++;
4228 }
4229 outlen++;
4230 }
4231
4232 MEM(vb = fr_value_box_alloc_null(ctx));
4233 MEM(fr_value_box_bstr_alloc(vb, &buff_p, vb, NULL, outlen, false) == 0);
4234 fr_value_box_safety_copy(vb, in_head);
4235
4236 /* Reset p to start position */
4237 p = in_head->vb_strvalue;
4238
4239 while (p < end) {
4240 if (*p != '%') {
4241 *buff_p++ = *p++;
4242 continue;
4243 }
4244 /* Is a % char */
4245
4246 /* Don't need \0 check, as it won't be in the hextab */
4247 if (!(c1 = memchr(hextab, tolower((uint8_t) *++p), 16)) ||
4248 !(c2 = memchr(hextab, tolower((uint8_t) *++p), 16))) {
4249 REMARKER(in_head->vb_strvalue, p - in_head->vb_strvalue, "Non-hex char in %% sequence");
4250 talloc_free(vb);
4251
4252 return XLAT_ACTION_FAIL;
4253 }
4254 p++;
4255 *buff_p++ = ((c1 - hextab) << 4) + (c2 - hextab);
4256 }
4257
4258 *buff_p = '\0';
4260
4261 return XLAT_ACTION_DONE;
4262}
4263
4265 { .required = true, .type = FR_TYPE_VOID },
4266 { .single = true, .type = FR_TYPE_ATTR },
4268};
4269
4270/** Decode any protocol attribute / options
4271 *
4272 * Creates protocol-specific attributes based on the given binary option data
4273 *
4274 * Example:
4275@verbatim
4276%dhcpv4.decode(%{Tmp-Octets-0})
4277@endverbatim
4278 *
4279 * @ingroup xlat_functions
4280 */
4282 xlat_ctx_t const *xctx,
4283 request_t *request, fr_value_box_list_t *in)
4284{
4285 int decoded;
4286 fr_value_box_t *vb, *in_head, *root_da;
4287 void *decode_ctx = NULL;
4288 xlat_pair_decode_uctx_t const *decode_uctx = talloc_get_type_abort(*(void * const *)xctx->inst, xlat_pair_decode_uctx_t);
4289 fr_test_point_pair_decode_t const *tp_decode = decode_uctx->tp_decode;
4290 fr_pair_t *vp = NULL;
4291 bool created = false;
4292
4293 XLAT_ARGS(in, &in_head, &root_da);
4294
4295 fr_assert(in_head->type == FR_TYPE_GROUP);
4296
4297 if (decode_uctx->dict && decode_uctx->dict != request->proto_dict) {
4298 REDEBUG2("Can't call %%%s() when in %s namespace", xctx->ex->call.func->name,
4299 fr_dict_root(request->proto_dict)->name);
4300 return XLAT_ACTION_FAIL;
4301 }
4302
4303 if (root_da) {
4304 int ret;
4305 if (!fr_type_is_structural(root_da->vb_attr->type)) {
4306 REDEBUG2("Decoding context must be a structural attribute reference");
4307 return XLAT_ACTION_FAIL;
4308 }
4309 ret = fr_pair_update_by_da_parent(fr_pair_list_parent(&request->request_pairs), &vp, root_da->vb_attr);
4310 if (ret < 0) {
4311 REDEBUG2("Failed creating decoding root pair");
4312 return XLAT_ACTION_FAIL;
4313 }
4314 if (ret == 0) created = true;
4315 }
4316
4317 if (tp_decode->test_ctx) {
4318 if (tp_decode->test_ctx(&decode_ctx, ctx, request->proto_dict, root_da ? root_da->vb_attr : NULL) < 0) {
4319 goto fail;
4320 }
4321 }
4322
4323 decoded = xlat_decode_value_box_list(root_da ? vp : request->request_ctx,
4324 root_da ? &vp->vp_group : &request->request_pairs,
4325 request, decode_ctx, tp_decode->func, &in_head->vb_group);
4326 if (decoded <= 0) {
4327 talloc_free(decode_ctx);
4328 RPERROR("Protocol decoding failed");
4329 fail:
4330 if (created) fr_pair_delete(&request->request_pairs, vp);
4331 return XLAT_ACTION_FAIL;
4332 }
4333
4334 /*
4335 * Create a value box to hold the decoded count, and add
4336 * it to the output list.
4337 */
4338 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_UINT32, NULL));
4339 vb->vb_uint32 = decoded;
4341
4342 talloc_free(decode_ctx);
4343 return XLAT_ACTION_DONE;
4344}
4345
4347 { .required = true, .single = true, .type = FR_TYPE_IPV4_PREFIX },
4349};
4350
4351/** Calculate the subnet mask from a IPv4 prefix
4352 *
4353 * Example:
4354@verbatim
4355%ip.v4.netmask(%{Network-Prefix})
4356@endverbatim
4357 *
4358 * @ingroup xlat_functions
4359 */
4361 UNUSED request_t *request, fr_value_box_list_t *args)
4362{
4363 fr_value_box_t *subnet, *vb;
4364 XLAT_ARGS(args, &subnet);
4365
4366 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_IPV4_ADDR, NULL));
4367
4368 switch (subnet->vb_ip.prefix) {
4369 case 0:
4370 vb->vb_ipv4addr = 0;
4371 break;
4372
4373 case 32:
4374 vb->vb_ipv4addr = 0xffffffff;
4375 break;
4376
4377 default:
4378 vb->vb_ipv4addr = htonl((uint32_t)0xffffffff << (32 - subnet->vb_ip.prefix));
4379 break;
4380 }
4381
4383
4384 return XLAT_ACTION_DONE;
4385}
4386
4387/** Calculate the broadcast address from a IPv4 prefix
4388 *
4389 * Example:
4390@verbatim
4391%ip.v4.broadcast(%{Network-Prefix})
4392@endverbatim
4393 *
4394 * @ingroup xlat_functions
4395 */
4397 UNUSED request_t *request, fr_value_box_list_t *args)
4398{
4399 fr_value_box_t *subnet, *vb;
4400 XLAT_ARGS(args, &subnet);
4401
4402 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_IPV4_ADDR, NULL));
4403 vb->vb_ipv4addr = htonl( ntohl(subnet->vb_ipv4addr) | ((uint32_t)0xffffffff >> subnet->vb_ip.prefix));
4405
4406 return XLAT_ACTION_DONE;
4407}
4408
4410{
4411 *(void **) mctx->inst = mctx->uctx;
4412 return 0;
4413}
4414
4420
4421/** Encode protocol attributes / options
4422 *
4423 * Returns octet string created from the provided pairs
4424 *
4425 * Example:
4426@verbatim
4427%dhcpv4.encode(&request[*])
4428@endverbatim
4429 *
4430 * @ingroup xlat_functions
4431 */
4433 xlat_ctx_t const *xctx,
4434 request_t *request, fr_value_box_list_t *args)
4435{
4436 fr_pair_t *vp;
4437 fr_dcursor_t *cursor;
4438 bool tainted = false, encode_children = false;
4439 fr_value_box_t *encoded;
4440
4441 fr_dbuff_t *dbuff;
4442 ssize_t len = 0;
4443 fr_value_box_t *in_head, *root_da;
4444 void *encode_ctx = NULL;
4445 fr_test_point_pair_encode_t const *tp_encode;
4446
4447 FR_DBUFF_TALLOC_THREAD_LOCAL(&dbuff, 2048, SIZE_MAX);
4448
4449 XLAT_ARGS(args, &in_head, &root_da);
4450
4451 memcpy(&tp_encode, xctx->inst, sizeof(tp_encode)); /* const issues */
4452
4453 cursor = fr_value_box_get_cursor(in_head);
4454
4455 /*
4456 * Create the encoding context.
4457 */
4458 if (tp_encode->test_ctx) {
4459 if (tp_encode->test_ctx(&encode_ctx, cursor, request->proto_dict, root_da ? root_da->vb_attr : NULL) < 0) {
4460 return XLAT_ACTION_FAIL;
4461 }
4462 }
4463
4464 if (root_da) {
4465 if (!fr_type_is_structural(root_da->vb_attr->type)) {
4466 REDEBUG2("Encoding context must be a structural attribute reference");
4467 return XLAT_ACTION_FAIL;
4468 }
4469 vp = fr_dcursor_current(cursor);
4470 if (!fr_dict_attr_common_parent(root_da->vb_attr, vp->da, true) && (root_da->vb_attr != vp->da)) {
4471 REDEBUG2("%s is not a child of %s", vp->da->name, root_da->vb_attr->name);
4472 return XLAT_ACTION_FAIL;
4473 }
4474 if (root_da->vb_attr == vp->da) encode_children = true;
4475 }
4476
4477 /*
4478 * Loop over the attributes, encoding them.
4479 */
4480 RDEBUG2("Encoding attributes");
4481
4482 if (RDEBUG_ENABLED2) {
4483 RINDENT();
4484 for (vp = fr_dcursor_current(cursor);
4485 vp != NULL;
4486 vp = fr_dcursor_next(cursor)) {
4487 RDEBUG2("%pP", vp);
4488 }
4489 REXDENT();
4490 }
4491
4492 /*
4493 * Encoders advance the cursor, so we just need to feed
4494 * in the next pair. This was originally so we could
4495 * extend the output buffer, but with dbuffs that's
4496 * no longer necessary... we might want to refactor this
4497 * in future.
4498 */
4499 for (vp = fr_dcursor_head(cursor);
4500 vp != NULL;
4501 vp = fr_dcursor_current(cursor)) {
4502 /*
4503 *
4504 * Don't check for internal attributes, the
4505 * encoders can skip them if they need to, and the
4506 * internal encoder can encode anything, as can
4507 * things like CBOR.
4508 *
4509 * Don't check the dictionaries. By definition,
4510 * vp->da->dict==request->proto_dict, OR else we're
4511 * using the internal encoder and encoding a real
4512 * protocol.
4513 *
4514 * However, we likely still want a
4515 * dictionary-specific "is encodable" function,
4516 * as AKA/SIM and DHCPv6 encode "bool"s only if
4517 * their value is true.
4518 */
4519 if (encode_children) {
4520 fr_dcursor_t child_cursor;
4521
4523
4524 /*
4525 * If we're given an encoding context which is the
4526 * same as the DA returned by the cursor, that means
4527 * encode the children.
4528 */
4529 fr_pair_dcursor_init(&child_cursor, &vp->vp_group);
4530 while (fr_dcursor_current(&child_cursor)) {
4531 len = tp_encode->func(dbuff, &child_cursor, encode_ctx);
4532 if (len < 0) break;
4533 }
4534 fr_dcursor_next(cursor);
4535 } else {
4536 len = tp_encode->func(dbuff, cursor, encode_ctx);
4537 }
4538 if (len < 0) {
4539 RPEDEBUG("Protocol encoding failed");
4540 return XLAT_ACTION_FAIL;
4541 }
4542
4543 tainted |= vp->vp_tainted;
4544 }
4545
4546 /*
4547 * Pass the options string back to the caller.
4548 */
4549 MEM(encoded = fr_value_box_alloc_null(ctx));
4550 fr_value_box_memdup(encoded, encoded, NULL, fr_dbuff_start(dbuff), fr_dbuff_used(dbuff), tainted);
4551 fr_dcursor_append(out, encoded);
4552
4553 return XLAT_ACTION_DONE;
4554}
4555
4556static int xlat_protocol_register_by_name(dl_t *dl, char const *name, fr_dict_t const *dict)
4557{
4558 fr_test_point_pair_decode_t *tp_decode;
4559 fr_test_point_pair_encode_t *tp_encode;
4560 xlat_pair_decode_uctx_t *decode_uctx;
4561 xlat_t *xlat;
4562 char buffer[256+32];
4563
4564 /*
4565 * See if there's a decode function for it.
4566 */
4567 snprintf(buffer, sizeof(buffer), "%s_tp_decode_pair", name);
4568 tp_decode = dlsym(dl->handle, buffer);
4569 if (tp_decode) {
4570 snprintf(buffer, sizeof(buffer), "%s.decode", name);
4571
4572 /* May be called multiple times, so just skip protocols we've already registered */
4573 if (xlat_func_find(buffer, -1)) return 1;
4574
4575 if (unlikely((xlat = xlat_func_register(NULL, buffer, xlat_pair_decode, FR_TYPE_UINT32)) == NULL)) return -1;
4577 decode_uctx = talloc(xlat, xlat_pair_decode_uctx_t);
4578 decode_uctx->tp_decode = tp_decode;
4579 decode_uctx->dict = dict;
4580 /* coverity[suspicious_sizeof] */
4583 }
4584
4585 /*
4586 * See if there's an encode function for it.
4587 */
4588 snprintf(buffer, sizeof(buffer), "%s_tp_encode_pair", name);
4589 tp_encode = dlsym(dl->handle, buffer);
4590 if (tp_encode) {
4591 snprintf(buffer, sizeof(buffer), "%s.encode", name);
4592
4593 if (xlat_func_find(buffer, -1)) return 1;
4594
4595 if (unlikely((xlat = xlat_func_register(NULL, buffer, xlat_pair_encode, FR_TYPE_OCTETS)) == NULL)) return -1;
4597 /* coverity[suspicious_sizeof] */
4600 }
4601
4602 return 0;
4603}
4604
4605static int xlat_protocol_register(fr_dict_t const *dict)
4606{
4607 dl_t *dl = fr_dict_dl(dict);
4608 char *p, name[256];
4609
4610 /*
4611 * No library for this protocol, skip it.
4612 *
4613 * Protocol TEST has no libfreeradius-test, so that's OK.
4614 */
4615 if (!dl) return 0;
4616
4617 strlcpy(name, fr_dict_root(dict)->name, sizeof(name));
4618 for (p = name; *p != '\0'; p++) {
4619 *p = tolower((uint8_t) *p);
4620 }
4621
4622 return xlat_protocol_register_by_name(dl, name, dict != fr_dict_internal() ? dict : NULL);
4623}
4624
4626
4628{
4629 dl_t *dl;
4630
4631 cbor_loader = dl_loader_init(NULL, NULL, false, false);
4632 if (!cbor_loader) return 0;
4633
4634 dl = dl_by_name(cbor_loader, "libfreeradius-cbor", NULL, false);
4635 if (!dl) return 0;
4636
4637 if (xlat_protocol_register_by_name(dl, "cbor", NULL) < 0) return -1;
4638
4639 return 0;
4640}
4641
4642
4643/** Register xlats for any loaded dictionaries
4644 */
4646{
4647 fr_dict_t *dict;
4649
4650 for (dict = fr_dict_global_ctx_iter_init(&iter);
4651 dict != NULL;
4652 dict = fr_dict_global_ctx_iter_next(&iter)) {
4653 if (xlat_protocol_register(dict) < 0) return -1;
4654 }
4655
4656 /*
4657 * And the internal protocol, too.
4658 */
4659 if (xlat_protocol_register(fr_dict_internal()) < 0) return -1;
4660
4661 /*
4662 * And cbor stuff
4663 */
4664 if (xlat_protocol_register_cbor() < 0) return -1;
4665
4666 return 0;
4667}
4668
4669/** De-register all xlat functions we created
4670 *
4671 */
4672static int _xlat_global_free(UNUSED void *uctx)
4673{
4674 TALLOC_FREE(xlat_ctx);
4678
4679 return 0;
4680}
4681
4682/** Global initialisation for xlat
4683 *
4684 * @note Free memory with #xlat_free
4685 *
4686 * @return
4687 * - 0 on success.
4688 * - -1 on failure.
4689 *
4690 * @hidecallgraph
4691 */
4692static int _xlat_global_init(UNUSED void *uctx)
4693{
4694 xlat_t *xlat;
4695
4696 xlat_ctx = talloc_init("xlat");
4697 if (!xlat_ctx) return -1;
4698
4699 if (xlat_func_init() < 0) return -1;
4700
4701 /*
4702 * Lookup attributes used by virtual xlat expansions.
4703 */
4704 if (xlat_eval_init() < 0) return -1;
4705
4706 /*
4707 * Registers async xlat operations in the `unlang` interpreter.
4708 */
4710
4711 /*
4712 * These are all "pure" functions.
4713 */
4714#define XLAT_REGISTER_ARGS(_xlat, _func, _return_type, _args) \
4715do { \
4716 if (unlikely((xlat = xlat_func_register(xlat_ctx, _xlat, _func, _return_type)) == NULL)) return -1; \
4717 xlat_func_args_set(xlat, _args); \
4718 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
4719} while (0)
4720
4721#define XLAT_NEW(_x) xlat->replaced_with = _x
4722
4724
4727 XLAT_NEW("str.concat");
4728
4731 XLAT_NEW("str.split");
4732
4734
4737 XLAT_NEW("hmac.md5");
4738
4741 XLAT_NEW("hmac.sha1");
4742
4744 xlat->deprecated = true;
4745
4748 xlat->deprecated = true;
4749
4751
4754 XLAT_NEW("str.lpad");
4755
4758 XLAT_NEW("str.rpad");
4759
4762 XLAT_NEW("str.substr");
4763
4766
4767 /*
4768 * The inputs to these functions are variable.
4769 */
4770#undef XLAT_REGISTER_ARGS
4771#define XLAT_REGISTER_ARGS(_xlat, _func, _return_type, _args) \
4772do { \
4773 if (unlikely((xlat = xlat_func_register(xlat_ctx, _xlat, _func, _return_type)) == NULL)) return -1; \
4774 xlat_func_args_set(xlat, _args); \
4775 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); \
4776} while (0)
4777
4778#undef XLAT_REGISTER_VOID
4779#define XLAT_REGISTER_VOID(_xlat, _func, _return_type) \
4780do { \
4781 if (unlikely((xlat = xlat_func_register(xlat_ctx, _xlat, _func, _return_type)) == NULL)) return -1; \
4782 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_INTERNAL); \
4783} while (0)
4784
4788 XLAT_NEW("pairs.debug");
4789
4799
4801 XLAT_NEW("pairs.immutable");
4803
4809
4811 XLAT_NEW("time.next");
4813
4815 XLAT_NEW("pairs.print");
4817
4819
4821#ifdef HAVE_REGEX_PCRE2
4822 xlat_func_instantiate_set(xlat, xlat_instantiate_subst_regex, xlat_subst_regex_inst_t, NULL, NULL);
4823#endif
4825 XLAT_NEW("str.subst");
4826#ifdef HAVE_REGEX_PCRE2
4827 xlat_func_instantiate_set(xlat, xlat_instantiate_subst_regex, xlat_subst_regex_inst_t, NULL, NULL);
4828#endif
4829
4830#ifndef NDEBUG
4832#endif
4833
4839
4844
4847 XLAT_NEW("str.rand");
4848
4851
4853
4854 if (unlikely((xlat = xlat_func_register(xlat_ctx, "untaint", xlat_func_untaint, FR_TYPE_VOID)) == NULL)) return -1;
4857
4858 if (unlikely((xlat = xlat_func_register(xlat_ctx, "taint", xlat_func_taint, FR_TYPE_VOID)) == NULL)) return -1;
4861
4862 /*
4863 * All of these functions are pure.
4864 */
4865#define XLAT_REGISTER_PURE(_xlat, _func, _return_type, _arg) \
4866do { \
4867 if (unlikely((xlat = xlat_func_register(xlat_ctx, _xlat, _func, _return_type)) == NULL)) return -1; \
4868 xlat_func_args_set(xlat, _arg); \
4869 xlat_func_flags_set(xlat, XLAT_FUNC_FLAG_PURE | XLAT_FUNC_FLAG_INTERNAL); \
4870} while (0)
4871
4876 XLAT_NEW("hash.md4");
4877
4880 XLAT_NEW("hash.md4");
4881
4882 if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex.match", xlat_func_regex, FR_TYPE_STRING)) == NULL)) return -1;
4885 if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex", xlat_func_regex, FR_TYPE_STRING)) == NULL)) return -1;
4888 XLAT_NEW("regex.match");
4889
4890 {
4891 static xlat_arg_parser_t const xlat_regex_safe_args[] = {
4892 { .type = FR_TYPE_STRING, .variadic = true, .concat = true },
4894 };
4895
4896 static xlat_arg_parser_t const xlat_regex_escape_args[] = {
4897 { .type = FR_TYPE_STRING,
4898 .func = regex_xlat_escape, .safe_for = FR_REGEX_SAFE_FOR, .always_escape = true,
4899 .variadic = true, .concat = true },
4901 };
4902
4903 if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex.safe",
4904 xlat_transparent, FR_TYPE_STRING)) == NULL)) return -1;
4906 xlat_func_args_set(xlat, xlat_regex_safe_args);
4907 xlat_func_safe_for_set(xlat, FR_REGEX_SAFE_FOR);
4908
4909 if (unlikely((xlat = xlat_func_register(xlat_ctx, "regex.escape",
4910 xlat_transparent, FR_TYPE_STRING)) == NULL)) return -1;
4912 xlat_func_args_set(xlat, xlat_regex_escape_args);
4913 xlat_func_safe_for_set(xlat, FR_REGEX_SAFE_FOR);
4914 }
4915
4916#define XLAT_REGISTER_HASH(_name, _func) do { \
4917 XLAT_REGISTER_PURE("hash." _name, _func, FR_TYPE_OCTETS, xlat_func_sha_arg); \
4918 XLAT_REGISTER_PURE(_name, _func, FR_TYPE_OCTETS, xlat_func_sha_arg); \
4919 XLAT_NEW("hash." _name); \
4920 } while (0)
4921
4923
4924#ifdef HAVE_OPENSSL_EVP_H
4925 XLAT_REGISTER_HASH("sha2_224", xlat_func_sha2_224);
4926 XLAT_REGISTER_HASH("sha2_256", xlat_func_sha2_256);
4927 XLAT_REGISTER_HASH("sha2_384", xlat_func_sha2_384);
4928 XLAT_REGISTER_HASH("sha2_512", xlat_func_sha2_512);
4929 XLAT_REGISTER_HASH("sha2", xlat_func_sha2_256);
4930
4931# ifdef HAVE_EVP_BLAKE2S256
4932 XLAT_REGISTER_HASH("blake2s_256", xlat_func_blake2s_256);
4933# endif
4934# ifdef HAVE_EVP_BLAKE2B512
4935 XLAT_REGISTER_HASH("blake2b_512", xlat_func_blake2b_512);
4936# endif
4937
4938 XLAT_REGISTER_HASH("sha3_224", xlat_func_sha3_224);
4939 XLAT_REGISTER_HASH("sha3_256", xlat_func_sha3_256);
4940 XLAT_REGISTER_HASH("sha3_384", xlat_func_sha3_384);
4941 XLAT_REGISTER_HASH("sha3_512", xlat_func_sha3_512);
4942 XLAT_REGISTER_HASH("sha3", xlat_func_sha3_256);
4943#endif
4944
4946 xlat->deprecated = true;
4948 XLAT_NEW("length");
4949
4952
4955 XLAT_NEW("str.lower");
4956
4959 XLAT_NEW("str.upper");
4960
4963 XLAT_NEW("url.quote");
4964
4967 XLAT_NEW("url.unquote");
4968
4970
4972}
4973
4975{
4976 int ret;
4977 fr_atexit_global_once_ret(&ret, _xlat_global_init, _xlat_global_free, NULL);
4978 return ret;
4979}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
va_list args
Definition acutest.h:770
static int const char * fmt
Definition acutest.h:573
#define fr_base16_encode(_out, _in)
Definition base16.h:54
#define fr_base16_decode(_err, _out, _in, _no_trailing)
Definition base16.h:92
#define fr_base64_encode(_out, _in, _add_padding)
Definition base64.h:71
#define fr_base64_decode(_out, _in, _expect_padding, _no_trailing)
Definition base64.h:78
#define FR_BASE64_DEC_LENGTH(_inlen)
Definition base64.h:41
#define FR_BASE64_ENC_LENGTH(_inlen)
Encode/decode binary data using printable characters (base64 format)
Definition base64.h:40
static dl_t * dl
Definition fuzzer.c:41
static bool stop
Definition radmin.c:68
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define RCSID(id)
Definition build.h:506
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:228
#define unlikely(_x)
Definition build.h:402
#define UNUSED
Definition build.h:336
#define NUM_ELEMENTS(_t)
Definition build.h:358
#define fr_dbuff_used(_dbuff_or_marker)
Return the number of bytes remaining between the start of the dbuff or marker and the current positio...
Definition dbuff.h:775
#define fr_dbuff_start(_dbuff_or_marker)
Return the 'start' position of a dbuff or marker.
Definition dbuff.h:906
#define FR_DBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
Create a function local and thread local extensible dbuff.
Definition dbuff.h:564
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:522
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
static void * fr_dcursor_head(fr_dcursor_t *cursor)
Rewind cursor to the start of the list.
Definition dcursor.h:232
#define MEM(x)
Definition debug.h:46
fr_dict_t * fr_dict_global_ctx_iter_next(fr_dict_global_ctx_iter_t *iter)
Definition dict_util.c:4892
char const * name
Vendor name.
Definition dict.h:274
fr_dict_attr_t const * fr_dict_attr_common_parent(fr_dict_attr_t const *a, fr_dict_attr_t const *b, bool is_ancestor)
Find a common ancestor that two TLV type attributes share.
Definition dict_util.c:2313
static fr_slen_t err
Definition dict.h:882
fr_dict_t * fr_dict_global_ctx_iter_init(fr_dict_global_ctx_iter_t *iter)
Iterate protocols by name.
Definition dict_util.c:4885
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2665
dl_t * fr_dict_dl(fr_dict_t const *dict)
Definition dict_util.c:2675
uint32_t pen
Private enterprise number.
Definition dict.h:270
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4928
static fr_slen_t in
Definition dict.h:882
fr_dict_vendor_t const * fr_dict_vendor_by_da(fr_dict_attr_t const *da)
Look up a vendor by one of its child attributes.
Definition dict_util.c:2900
Private enterprise.
Definition dict.h:269
Test enumeration values.
Definition dict_test.h:92
dl_loader_t * dl_loader_init(TALLOC_CTX *ctx, void *uctx, bool uctx_free, bool defer_symbol_init)
Initialise structures needed by the dynamic linker.
Definition dl.c:900
dl_t * dl_by_name(dl_loader_t *dl_loader, char const *name, void *uctx, bool uctx_free)
Search for a dl's shared object in various locations.
Definition dl.c:470
A dynamic loader.
Definition dl.c:81
void * handle
Handle returned by dlopen.
Definition dl.h:61
Module handle.
Definition dl.h:57
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static unsigned int fr_dlist_num_elements(fr_dlist_head_t const *head)
Return the number of elements in the dlist.
Definition dlist.h:921
static void * fr_dlist_next(fr_dlist_head_t const *list_head, void const *ptr)
Get the next item in a list.
Definition dlist.h:537
void fr_bio_shutdown & my
Definition fd_errno.h:70
static xlat_action_t xlat_func_time_now(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
Return the current time as a FR_TYPE_DATE.
static xlat_action_t xlat_func_next_time(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Calculate number of seconds until the next n hour(s), day(s), week(s), year(s).
static xlat_action_t xlat_func_lpad(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
lpad a string
static xlat_action_t xlat_func_bin(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Convert hex string to binary.
static xlat_action_t xlat_func_pairs_debug(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Print out attribute info.
static xlat_action_t xlat_func_subst(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Perform regex substitution.
static xlat_action_t xlat_func_urlunquote(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
URLdecode special characters.
static xlat_action_t xlat_pair_decode(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Decode any protocol attribute / options.
static xlat_action_t xlat_func_base64_decode(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Decode base64 string.
static xlat_action_t xlat_func_hmac_md5(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Generate the HMAC-MD5 of a string or attribute.
static xlat_action_t xlat_func_base64_encode(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Encode string or attribute as base64.
static xlat_action_t xlat_func_log_info(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Log something at INFO level.
static xlat_action_t xlat_func_log_warn(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Log something at WARN level.
static xlat_action_t xlat_func_map(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Processes fmt as a map string and applies it to the current request.
static xlat_action_t xlat_func_debug(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Dynamically change the debugging level for the current request.
static xlat_action_t xlat_func_log_debug(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Log something at DEBUG level.
static xlat_action_t xlat_func_log_dst(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Change the log destination to the named one.
static xlat_arg_parser_t const xlat_func_string_arg[]
Calculate any digest supported by OpenSSL EVP_MD.
static xlat_action_t xlat_func_block(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Block for the specified duration.
static xlat_action_t xlat_func_concat(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Concatenate string representation of values of given attributes using separator.
static xlat_action_t xlat_func_urlquote(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
URLencode special characters.
static xlat_action_t xlat_func_rpad(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Right pad a string.
static xlat_action_t xlat_func_md4(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Calculate the MD4 hash of a string or attribute.
static xlat_action_t xlat_func_explode(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Split a string into multiple new strings based on a delimiter.
static xlat_action_t xlat_func_pairs_print(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Encode attributes as a series of string attribute/value pairs.
static xlat_action_t xlat_func_time_request(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, UNUSED fr_value_box_list_t *args)
Return the request receive time as a FR_TYPE_DATE.
static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Get named subcapture value from previous regex.
static xlat_action_t xlat_func_substr(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Extract a substring from string / octets data.
static xlat_action_t xlat_func_length(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Return the on-the-wire size of the boxes in bytes.
static xlat_action_t xlat_func_immutable_attr(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Mark one or more attributes as immutable.
static xlat_action_t xlat_func_rand(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Generate a random integer value.
static xlat_action_t xlat_pair_encode(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Encode protocol attributes / options.
static xlat_action_t xlat_func_log_err(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Log something at DEBUG level.
static xlat_action_t xlat_func_hmac_sha1(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Generate the HMAC-SHA1 of a string or attribute.
static xlat_action_t xlat_func_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Dynamically evaluate an expansion string.
static xlat_action_t xlat_func_time_is_dst(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
Return whether we are in daylight savings or not.
static xlat_action_t xlat_func_integer(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Print data as integer, not as VALUE.
static xlat_action_t xlat_func_time(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Return the time as a FR_TYPE_DATE.
static xlat_action_t xlat_func_toupper(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Convert a string to uppercase.
static xlat_action_t xlat_func_uuid_v7(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
Generate a version 7 UUID.
static xlat_action_t xlat_func_uuid_v4(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
Generate a version 4 UUID.
static xlat_action_t xlat_func_cast(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Cast one or more output value-boxes to the given type.
static xlat_action_t xlat_func_hex(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Print data as hex, not as VALUE.
static xlat_action_t xlat_func_md5(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Calculate the MD5 hash of a string or attribute.
static xlat_action_t xlat_func_subnet_netmask(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Calculate the subnet mask from a IPv4 prefix.
static xlat_action_t xlat_func_sha1(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Calculate the SHA1 hash of a string or attribute.
static xlat_action_t xlat_func_str_printable(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Return whether a string has only printable chars.
static xlat_action_t xlat_func_range(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Generate a range of uint64 numbers.
static xlat_action_t xlat_func_randstr(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
Generate a string of random chars.
static xlat_action_t xlat_func_tolower(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *in)
Convert a string to lowercase.
static xlat_action_t xlat_func_subnet_broadcast(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Calculate the broadcast address from a IPv4 prefix.
static xlat_action_t xlat_func_str_utf8(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Return whether a string is valid UTF-8.
static xlat_action_t xlat_func_time_offset(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *args)
Return the current time offset from gmt.
static xlat_action_t xlat_func_strlen(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Print length of given string.
Stores the state of the current iteration operation.
Definition hash.h:41
talloc_free(hp)
int fr_hmac_md5(uint8_t digest[MD5_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal MD5 implementation.
Definition hmac_md5.c:119
int fr_hmac_sha1(uint8_t digest[static SHA1_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal SHA1 implementation.
Definition hmac_sha1.c:123
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:1681
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2053
#define UNLANG_SUB_FRAME
Definition interpret.h:37
fr_log_t * log_dst_by_name(char const *name)
Get a logging destination by name.
Definition log.c:1082
#define PERROR(_fmt,...)
Definition log.h:228
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:455
#define RWDEBUG(fmt,...)
Definition log.h:373
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
Definition log.h:347
#define REDEBUG3(fmt,...)
Definition log.h:385
#define RERROR(fmt,...)
Definition log.h:310
#define RPERROR(fmt,...)
Definition log.h:314
#define REMARKER(_str, _marker_idx, _marker,...)
Output string with error marker, showing where format error occurred.
Definition log.h:510
#define RINFO(fmt,...)
Definition log.h:308
#define RMARKER(_type, _lvl, _str, _marker_idx, _marker,...)
Output string with error marker, showing where format error occurred.
Definition log.h:481
#define RPEDEBUG(fmt,...)
Definition log.h:388
#define RDEBUG4(fmt,...)
Definition log.h:356
#define RDEBUG_ENABLED4
True if request debug level 1-4 messages are enabled.
Definition log.h:348
#define RIDEBUG2(fmt,...)
Definition log.h:364
#define REDEBUG2(fmt,...)
Definition log.h:384
#define RIDEBUG3(fmt,...)
Definition log.h:365
#define RINDENT()
Indent R* messages by one level.
Definition log.h:442
int map_to_vp(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, map_t const *map, UNUSED void *uctx)
Convert a map to a fr_pair_t.
Definition map.c:1604
int map_to_request(request_t *request, map_t const *map, radius_map_getvalue_t func, void *ctx)
Convert map_t to fr_pair_t (s) and add them to a request_t.
Definition map.c:1884
int map_afrom_attr_str(TALLOC_CTX *ctx, map_t **out, char const *vp_str, tmpl_rules_t const *lhs_rules, tmpl_rules_t const *rhs_rules)
Convert a value pair string to valuepair map.
Definition map.c:1433
#define fr_time()
Definition event.c:60
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
Definition file.c:218
const fr_sbuff_escape_rules_t fr_filename_escape
Definition file.c:916
const fr_sbuff_escape_rules_t fr_filename_escape_dots
Definition file.c:932
@ L_DST_NULL
Discard log messages.
Definition log.h:80
@ L_DST_FILES
Log to a file on disk.
Definition log.h:76
@ L_DBG_LVL_DISABLE
Don't print messages.
Definition log.h:65
@ L_DBG_LVL_2
2nd highest priority debug messages (-xx | -X).
Definition log.h:68
@ L_DBG_LVL_MAX
Lowest priority debug messages (-xxxxx | -Xxxx).
Definition log.h:71
@ L_WARN
Warning.
Definition log.h:54
void fr_md4_calc(uint8_t out[static MD4_DIGEST_LENGTH], uint8_t const *in, size_t inlen)
Calculate the MD4 hash of the contents of a buffer.
Definition md4.c:473
#define MD4_DIGEST_LENGTH
Definition md4.h:22
#define MD5_DIGEST_LENGTH
unsigned short uint16_t
fr_type_t
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_FLOAT32
Single precision floating point.
@ FR_TYPE_IPV4_ADDR
32 Bit IPv4 Address.
@ FR_TYPE_INT8
8 Bit signed integer.
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_IPV6_PREFIX
IPv6 Prefix.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_UINT16
16 Bit unsigned integer.
@ FR_TYPE_INT64
64 Bit signed integer.
@ FR_TYPE_INT16
16 Bit signed integer.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_COMBO_IP_PREFIX
IPv4 or IPv6 address prefix depending on length.
@ FR_TYPE_UINT8
8 Bit unsigned integer.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_INT32
32 Bit signed integer.
@ FR_TYPE_UINT64
64 Bit unsigned integer.
@ FR_TYPE_IPV6_ADDR
128 Bit IPv6 Address.
@ FR_TYPE_IPV4_PREFIX
IPv4 Prefix.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_SIZE
Unsigned integer capable of representing any memory address on the local system.
@ FR_TYPE_COMBO_IP_ADDR
IPv4 or IPv6 address depending on length.
@ FR_TYPE_IFID
Interface ID.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_GROUP
A grouping of other attributes.
@ FR_TYPE_FLOAT64
Double precision floating point.
unsigned int uint32_t
long int ssize_t
void fr_md5_calc(uint8_t out[static MD5_DIGEST_LENGTH], uint8_t const *in, size_t inlen)
Perform a single digest operation on a single input buffer.
unsigned char uint8_t
ssize_t fr_slen_t
long long int off_t
unsigned long int size_t
fr_sbuff_parse_error_t
size_t fr_snprint_uint128(char *out, size_t outlen, uint128_t const num)
Write 128bit unsigned integer to buffer.
Definition misc.c:401
struct tm * gmtime_r(time_t const *l_clock, struct tm *result)
Definition missing.c:205
struct tm * localtime_r(time_t const *l_clock, struct tm *result)
Definition missing.c:162
fr_pair_t * fr_pair_list_parent(fr_pair_list_t const *list)
Return a pointer to the parent pair which contains this list.
Definition pair.c:970
int fr_pair_update_by_da_parent(fr_pair_t *parent, fr_pair_t **out, fr_dict_attr_t const *da)
Return the first fr_pair_t matching the fr_dict_attr_t or alloc a new fr_pair_t and its subtree (and ...
Definition pair.c:1602
int fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp)
Remove fr_pair_t from a list and free.
Definition pair.c:1833
fr_slen_t fr_utf8_str(uint8_t const *str, ssize_t inlen)
Validate a complete UTF8 string.
Definition print.c:153
size_t fr_utf8_char(uint8_t const *str, ssize_t inlen)
Checks for utf-8, taken from http://www.w3.org/International/questions/qa-forms-utf-8.
Definition print.c:39
static fr_internal_encode_ctx_t encode_ctx
#define fr_assert(_expr)
Definition rad_assert.h:37
#define REDEBUG(fmt,...)
#define RDEBUG_ENABLED2()
#define RDEBUG2(fmt,...)
#define RDEBUG(fmt,...)
static bool done
Definition radclient.c:80
#define fill(_expr)
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:104
fr_dict_attr_t const * request_attr_request
Definition request.c:43
void request_log_prepend(request_t *request, fr_log_t *log_dst, fr_log_lvl_t lvl)
Prepend another logging destination to the list.
Definition request.c:92
#define RAD_REQUEST_LVL_NONE
No debug messages should be printed.
Definition request.h:312
static char const * name
char * fr_sbuff_adv_to_str(fr_sbuff_t *sbuff, size_t len, char const *needle, size_t needle_len)
Wind position to the first instance of the specified needle.
Definition sbuff.c:2033
char * fr_sbuff_adv_to_chr(fr_sbuff_t *sbuff, size_t len, char c)
Wind position to first instance of specified char.
Definition sbuff.c:1997
ssize_t fr_sbuff_in_bstrncpy(fr_sbuff_t *sbuff, char const *str, size_t len)
Copy bytes into the sbuff up to the first \0.
Definition sbuff.c:1496
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
Definition sbuff.c:1605
bool fr_sbuff_next_if_char(fr_sbuff_t *sbuff, char c)
Return true if the current char matches, and if it does, advance.
Definition sbuff.c:2129
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_set(_dst, _src)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define fr_sbuff_current(_sbuff_or_marker)
char const * name
Name for rule set to aid we debugging.
Definition sbuff.h:209
#define FR_SBUFF(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_init_in(_out, _start, _len_or_end)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define fr_sbuff_len(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define fr_sbuff_move(_out, _in, _len)
#define fr_sbuff_used(_sbuff_or_marker)
#define fr_sbuff_behind(_sbuff_or_marker)
#define fr_sbuff_ahead(_sbuff_or_marker)
#define FR_SBUFF_TALLOC_THREAD_LOCAL(_out, _init, _max)
Set of parsing rules for *unescape_until functions.
static char const * tmpl_type_to_str(tmpl_type_t type)
Return a static string containing the type name.
Definition tmpl.h:638
@ 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_DATA_UNRESOLVED
Unparsed literal string.
Definition tmpl.h:179
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:339
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:336
void fr_sha1_init(fr_sha1_ctx *context)
Definition sha1.c:93
void fr_sha1_final(uint8_t digest[static SHA1_DIGEST_LENGTH], fr_sha1_ctx *context)
Definition sha1.c:141
void fr_sha1_update(fr_sha1_ctx *context, uint8_t const *in, size_t len)
Definition sha1.c:105
#define SHA1_DIGEST_LENGTH
Definition sha1.h:29
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:41
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
PRIVATE void strings()
eap_aka_sim_process_conf_t * inst
fr_aka_sim_id_type_t type
fr_pair_t * vp
size_t strlcpy(char *dst, char const *src, size_t siz)
Definition strlcpy.c:34
Definition log.h:93
fr_log_t * parent
Log destination this was cloned from.
Definition log.h:118
fr_log_dst_t dst
Log destination.
Definition log.h:94
int fd
File descriptor to write messages to.
Definition log.h:109
char const * file
Path to log file.
Definition log.h:110
Value pair map.
Definition map.h:77
tmpl_t * lhs
Typically describes the attribute to add, modify or compare.
Definition map.h:78
tmpl_t * rhs
Typically describes a literal value or a src attribute to copy or compare.
Definition map.h:79
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
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
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_value_by_substr(_table, _name, _name_len, _def)
Convert a partial string to a value using an ordered or sorted table.
Definition table.h:693
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:617
char * talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len)
Append a bstr to a bstr.
Definition talloc.c:645
#define talloc_get_type_abort_const
Definition talloc.h:110
#define talloc_strdup(_ctx, _str)
Definition talloc.h:142
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:136
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:86
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:94
fr_pair_decode_t func
Decoder for pairs.
Definition test_point.h:87
fr_pair_encode_t func
Encoder for pairs.
Definition test_point.h:95
Entry point for pair decoders.
Definition test_point.h:85
Entry point for pair encoders.
Definition test_point.h:93
bool fr_time_is_dst(void)
Whether or not we're daylight savings.
Definition time.c:1217
int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_res_t hint)
Convert string in various formats to a fr_unix_time_t.
Definition time.c:799
fr_time_delta_t fr_time_gmtoff(void)
Get the offset to gmt.
Definition time.c:1209
#define fr_time_delta_to_timespec(_delta)
Convert a delta to a timespec.
Definition time.h:666
static int64_t fr_time_to_msec(fr_time_t when)
Convert an fr_time_t (internal time) to number of msec since the unix epoch (wallclock time)
Definition time.h:711
static int64_t fr_unix_time_to_sec(fr_unix_time_t delta)
Definition time.h:506
#define fr_time_delta_wrap(_time)
Definition time.h:152
@ FR_TIME_RES_SEC
Definition time.h:50
#define NSEC
Definition time.h:379
static uint64_t fr_unix_time_unwrap(fr_unix_time_t time)
Definition time.h:161
static fr_time_delta_t fr_time_delta_sub(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:261
static fr_unix_time_t fr_time_to_unix_time(fr_time_t when)
Convert an fr_time_t (internal time) to our version of unix time (wallclock time)
Definition time.h:688
static fr_time_delta_t fr_time_delta_from_timespec(struct timespec const *ts)
Definition time.h:614
"Unix" time.
Definition time.h:95
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:80
xlat_action_t unlang_xlat_yield(request_t *request, xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition xlat.c:543
int unlang_xlat_push(TALLOC_CTX *ctx, unlang_result_t *p_result, fr_value_box_list_t *out, request_t *request, xlat_exp_head_t const *xlat, bool top_frame)
Push a pre-compiled xlat onto the stack for evaluation.
Definition xlat.c:269
void unlang_xlat_init(void)
Register xlat operation with the interpreter.
Definition xlat.c:805
fr_type_t type
Type to cast argument to.
Definition xlat.h:155
bool xlat_is_literal(xlat_exp_head_t const *head)
Check to see if the expansion consists entirely of value-box elements.
#define XLAT_ARG_PARSER_CURSOR
Definition xlat.h:162
unsigned int concat
Concat boxes together.
Definition xlat.h:147
@ XLAT_ARG_VARIADIC_EMPTY_KEEP
Empty argument groups are left alone, and either passed through as empty groups or null boxes.
Definition xlat.h:137
@ XLAT_ARG_VARIADIC_EMPTY_SQUASH
Empty argument groups are removed.
Definition xlat.h:136
xlat_arg_parser_variadic_t variadic
All additional boxes should be processed using this definition.
Definition xlat.h:153
#define XLAT_RESULT_SUCCESS(_p_result)
Definition xlat.h:500
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition xlat.h:383
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:146
unsigned int single
Argument must only contain a single box.
Definition xlat.h:148
int xlat_resolve(xlat_exp_head_t *head, xlat_res_rules_t const *xr_rules)
Walk over an xlat tree recursively, resolving any unresolved functions or references.
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_YIELD
An xlat function pushed a resume frame onto the stack.
Definition xlat.h:42
@ XLAT_ACTION_PUSH_UNLANG
An xlat function pushed an unlang frame onto the unlang stack.
Definition xlat.h:39
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
fr_slen_t xlat_tokenize_expression(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules))
Definition xlat_expr.c:3165
Definition for a single argument consumed by an xlat function.
Definition xlat.h:145
static fr_slen_t fr_pair_aprint(TALLOC_CTX *ctx, char **out, fr_dict_attr_t const *parent, fr_pair_t const *vp) 1(fr_pair_print
fr_pair_t * fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item))
Get the next item in a valuepair list after a specific entry.
Definition pair_inline.c:69
static void fr_pair_set_immutable(fr_pair_t *vp)
Definition pair.h:699
static fr_slen_t quote ssize_t fr_pair_print_name(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t const **vp_p)
Print an attribute name.
Definition pair_print.c:136
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:604
static fr_slen_t parent
Definition pair.h:858
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
size_t fr_type_table_len
Definition types.c:87
#define fr_type_is_structural(_x)
Definition types.h:392
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:83
#define FR_TYPE_NON_LEAF
Definition types.h:318
#define fr_type_is_string(_x)
Definition types.h:348
#define fr_type_is_numeric(_x)
Definition types.h:382
#define FR_TYPE_STRUCTURAL
Definition types.h:316
#define fr_type_is_null(_x)
Definition types.h:347
#define fr_type_is_leaf(_x)
Definition types.h:393
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
Definition types.h:454
#define FR_TYPE_LEAF
Definition types.h:317
#define FR_TYPE_NUMERIC
Definition types.h:306
size_t fr_value_box_network_length(fr_value_box_t const *value)
Get the size of the value held by the fr_value_box_t.
Definition value.c:1409
void fr_value_box_mark_unsafe(fr_value_box_t *vb)
Mark a value-box as "unsafe".
Definition value.c:7285
ssize_t fr_value_box_list_concat_as_string(fr_value_box_t *safety, fr_sbuff_t *sbuff, fr_value_box_list_t *list, char const *sep, size_t sep_len, fr_sbuff_escape_rules_t const *e_rules, fr_value_box_list_action_t proc_action, fr_value_box_safe_for_t safe_for, bool flatten)
Concatenate a list of value boxes together.
Definition value.c:6373
ssize_t fr_value_box_print(fr_sbuff_t *out, fr_value_box_t const *data, fr_sbuff_escape_rules_t const *e_rules)
Print one boxed value to a string.
Definition value.c:6090
int fr_value_box_mem_alloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, fr_dict_attr_t const *enumv, size_t len, bool tainted)
Pre-allocate an octets buffer for filling by the caller.
Definition value.c:4970
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:3931
char * fr_value_box_list_aprint(TALLOC_CTX *ctx, fr_value_box_list_t const *list, char const *delim, fr_sbuff_escape_rules_t const *e_rules)
Concatenate the string representations of a list of value boxes together.
Definition value.c:6972
int fr_value_box_mem_realloc(TALLOC_CTX *ctx, uint8_t **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:5003
void fr_value_box_list_untaint(fr_value_box_list_t *head)
Untaint every list member (and their children)
Definition value.c:7169
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:4181
void fr_value_box_clear_value(fr_value_box_t *data)
Clear/free any existing value.
Definition value.c:4316
int fr_value_box_strdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Copy a nul terminated string to a fr_value_box_t.
Definition value.c:4604
void fr_value_box_safety_copy_changed(fr_value_box_t *out, fr_value_box_t const *in)
Copy the safety values from one box to another.
Definition value.c:7328
void fr_value_box_safety_merge(fr_value_box_t *out, fr_value_box_t const *in)
Merge safety results.
Definition value.c:7337
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:4714
void fr_value_box_safety_copy(fr_value_box_t *out, fr_value_box_t const *in)
Copy the safety values from one box to another.
Definition value.c:7315
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:4749
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:4362
bool fr_value_box_list_tainted(fr_value_box_list_t const *head)
Check to see if any list members (or their children) are tainted.
Definition value.c:7138
int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4782
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4823
int fr_value_box_bstrdup_buffer_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, bool tainted)
Assign a talloced buffer containing a nul terminated string to a box, but don't copy it.
Definition value.c:4931
int fr_value_box_memdup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, uint8_t const *src, size_t len, bool tainted)
Copy a buffer to a fr_value_box_t.
Definition value.c:5064
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:6589
@ FR_VALUE_BOX_LIST_FREE
Definition value.h:238
@ FR_VALUE_BOX_LIST_FREE_BOX
Free each processed box.
Definition value.h:235
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1093
static fr_slen_t data
Definition value.h:1340
static fr_value_box_t * fr_value_box_acopy(TALLOC_CTX *ctx, fr_value_box_t const *src)
Copy an existing box, allocating a new box to hold its contents.
Definition value.h:744
#define fr_value_box_is_safe_for(_box, _safe_for)
Definition value.h:1100
#define fr_box_is_variable_size(_x)
Definition value.h:464
#define fr_value_box_get_cursor(_dst)
Definition value.h:1261
#define VALUE_BOX_VERIFY(_x)
Definition value.h:1370
#define VALUE_BOX_LIST_VERIFY(_x)
Definition value.h:1371
int nonnull(2, 5))
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:655
#define fr_value_box_list_foreach(_list_head, _iter)
Definition value.h:224
static size_t char ** out
Definition value.h:1030
#define fr_box_bool(_val)
Definition value.h:331
#define FR_VALUE_BOX_SAFE_FOR_ANY
Definition value.h:173
static xlat_arg_parser_t const xlat_func_bin_arg[]
static int xlat_protocol_register_cbor(void)
static xlat_arg_parser_t const xlat_func_map_arg[]
static xlat_action_t xlat_func_file_tail(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
#define XLAT_REGISTER_VOID(_xlat, _func, _return_type)
static xlat_arg_parser_t const xlat_func_log_dst_args[]
static xlat_arg_parser_t const xlat_func_taint_args[]
static xlat_arg_parser_t const xlat_func_time_args[]
static xlat_arg_parser_t const xlat_func_base64_encode_arg[]
unlang_result_t last_result
static xlat_action_t xlat_change_case(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED request_t *request, fr_value_box_list_t *args, bool upper)
Change case of a string.
static int _log_dst_free(fr_log_t *log)
static xlat_arg_parser_t const xlat_pair_encode_args[]
static xlat_action_t xlat_hmac(TALLOC_CTX *ctx, fr_dcursor_t *out, fr_value_box_list_t *args, uint8_t *digest, int digest_len, hmac_type type)
static xlat_arg_parser_t const xlat_func_signal_raise_args[]
static void xlat_debug_attr_vp(request_t *request, fr_pair_t const *vp, fr_dict_attr_t const *da)
static xlat_arg_parser_t const xlat_func_log_arg[]
static xlat_action_t xlat_func_file_mkdir(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
static xlat_arg_parser_t const xlat_func_sha_arg[]
static xlat_arg_parser_t const xlat_func_cast_args[]
static int xlat_pair_dencode_instantiate(xlat_inst_ctx_t const *mctx)
xlat_action_t xlat_transparent(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
Common function to move boxes from input list to output list.
hmac_type
@ HMAC_MD5
@ HMAC_SHA1
static xlat_arg_parser_t const xlat_func_hex_arg[]
static xlat_arg_parser_t const xlat_func_substr_args[]
static xlat_action_t xlat_func_file_exists(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *args)
static xlat_action_t xlat_func_file_head(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
static xlat_arg_parser_t const xlat_func_block_args[]
static xlat_arg_parser_t const xlat_func_subnet_args[]
#define XLAT_REGISTER_PURE(_xlat, _func, _return_type, _arg)
static xlat_arg_parser_t const xlat_func_str_printable_arg[]
static xlat_arg_parser_t const xlat_func_randstr_arg[]
static xlat_arg_parser_t const xlat_func_eval_arg[]
static xlat_arg_parser_t const xlat_func_subst_args[]
static xlat_arg_parser_t const xlat_func_explode_args[]
int xlat_protocols_register(void)
Register xlats for any loaded dictionaries.
static xlat_arg_parser_t const xlat_func_str_utf8_arg[]
#define REPETITION_MAX
static dl_loader_t * cbor_loader
static xlat_arg_parser_t const xlat_change_case_arg[]
static xlat_arg_parser_t const xlat_func_strlen_arg[]
static int xlat_protocol_register(fr_dict_t const *dict)
static xlat_arg_parser_t const xlat_func_md5_arg[]
int xlat_global_init(void)
static xlat_arg_parser_t const xlat_func_urlquote_arg[]
static xlat_arg_parser_t const xlat_pair_cursor_args[]
static xlat_action_t xlat_func_file_size(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
static void ungroup(fr_dcursor_t *out, fr_value_box_list_t *in)
static xlat_arg_parser_t const xlat_func_md4_arg[]
static int regex_xlat_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
static xlat_arg_parser_t const xlat_func_join_args[]
#define XLAT_NEW(_x)
static xlat_action_t xlat_eval_resume(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
Just serves to push the result up the stack.
static xlat_action_t xlat_func_taint(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
#define XLAT_REGISTER_HASH(_name, _func)
static xlat_arg_parser_t const xlat_func_debug_args[]
static char const hextab[]
#define FR_FILENAME_SAFE_FOR
static xlat_action_t xlat_func_signal_raise(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
fr_test_point_pair_decode_t * tp_decode
static xlat_arg_parser_t const xlat_func_pad_args[]
static int uuid_print_vb(fr_value_box_t *vb, uint32_t vals[4])
Convert a UUID in an array of uint32_t to the conventional string representation.
static xlat_arg_parser_t const xlat_func_urlunquote_arg[]
static xlat_action_t xlat_func_file_touch(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
fr_dict_t const * dict
Restrict xlat to this namespace.
static xlat_arg_parser_t const xlat_pair_decode_args[]
static xlat_arg_parser_t const xlat_func_rand_arg[]
static void uuid_set_variant(uint32_t vals[4], uint8_t variant)
static int filename_xlat_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
static xlat_arg_parser_t const xlat_func_concat_args[]
static xlat_action_t xlat_func_join(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Join a series of arguments to form a single list.
static xlat_arg_parser_t const xlat_func_file_name_count_args[]
void xlat_arg_copy_out(TALLOC_CTX *ctx, fr_dcursor_t *out, fr_value_box_list_t *in, fr_value_box_t *vb)
Copy an argument from the input list to the output cursor.
static xlat_arg_parser_t const xlat_func_range_arg[]
static xlat_arg_parser_t const xlat_func_integer_args[]
static int _xlat_global_init(UNUSED void *uctx)
Global initialisation for xlat.
#define XLAT_REGISTER_ARGS(_xlat, _func, _return_type, _args)
static xlat_action_t xlat_func_untaint(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
static xlat_action_t xlat_func_file_cat(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
static xlat_action_t xlat_func_file_rm(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
xlat_exp_head_t * ex
static xlat_arg_parser_t const xlat_func_length_args[]
static xlat_action_t xlat_func_ungroup(UNUSED TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
Ungroups all of its arguments into one flat list.
static int xlat_protocol_register_by_name(dl_t *dl, char const *name, fr_dict_t const *dict)
static xlat_arg_parser_t const xlat_func_file_cat_args[]
static void uuid_set_version(uint32_t vals[4], uint8_t version)
static xlat_action_t xlat_func_file_rmdir(TALLOC_CTX *ctx, fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, request_t *request, fr_value_box_list_t *args)
#define UUID_CHARS(_v, _num)
static void xlat_debug_attr_list(request_t *request, fr_pair_list_t const *list, fr_dict_attr_t const *parent)
static xlat_arg_parser_t const xlat_func_file_name_args[]
static TALLOC_CTX * xlat_ctx
static xlat_arg_parser_t const xlat_func_next_time_args[]
static int _xlat_global_free(UNUSED void *uctx)
De-register all xlat functions we created.
static xlat_arg_parser_t const xlat_func_base64_decode_arg[]
static xlat_arg_parser_t const xlat_hmac_args[]
static xlat_arg_parser_t const xlat_func_regex_args[]
void * rctx
Resume context.
Definition xlat_ctx.h:54
xlat_exp_t const * ex
Tokenized expression.
Definition xlat_ctx.h:55
xlat_exp_t * ex
Tokenized expression to use in expansion.
Definition xlat_ctx.h:64
void const * inst
xlat instance data.
Definition xlat_ctx.h:50
void * uctx
Passed to the registration function.
Definition xlat_ctx.h:66
void * inst
xlat instance data to populate.
Definition xlat_ctx.h:63
An xlat calling ctx.
Definition xlat_ctx.h:49
An xlat instantiation ctx.
Definition xlat_ctx.h:62
fr_dict_attr_t const * xlat_time_res_attr(char const *res)
Definition xlat_eval.c:127
int xlat_eval_init(void)
Definition xlat_eval.c:1998
void xlat_eval_free(void)
Definition xlat_eval.c:2020
int xlat_register_expressions(void)
Definition xlat_expr.c:1861
void xlat_func_free(void)
Definition xlat_func.c:563
void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags)
Specify flags that alter the xlat's behaviour.
Definition xlat_func.c:399
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:363
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function.
Definition xlat_func.c:216
int xlat_func_init(void)
Definition xlat_func.c:547
xlat_t * xlat_func_find(char const *in, ssize_t inlen)
Definition xlat_func.c:77
#define xlat_func_instantiate_set(_xlat, _instantiate, _inst_struct, _detach, _uctx)
Set a callback for global instantiation of xlat functions.
Definition xlat_func.h:93
#define xlat_func_safe_for_set(_xlat, _escaped)
Set the escaped values for output boxes.
Definition xlat_func.h:82
@ XLAT_FUNC_FLAG_PURE
Definition xlat_func.h:38
@ XLAT_FUNC_FLAG_INTERNAL
Definition xlat_func.h:39
int xlat_decode_value_box_list(TALLOC_CTX *ctx, fr_pair_list_t *out, request_t *request, void *decode_ctx, fr_pair_decode_t decode, fr_value_box_list_t *in)
Decode all of the value boxes into the output cursor.
Definition xlat_pair.c:90
@ XLAT_GROUP
encapsulated string of xlats
Definition xlat_priv.h:116
bool deprecated
this function was deprecated
Definition xlat_priv.h:68
xlat_type_t _CONST type
type of this expansion.
Definition xlat_priv.h:155
An xlat expansion node.
Definition xlat_priv.h:148