The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
unit_test_attribute.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: d3493bff53f35d10ec60c5722408223df9f14b0a $
19 *
20 * @file unit_test_attribute.c
21 * @brief Provides a test harness for various internal libraries and functions.
22 *
23 * @copyright 2019 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 * @copyright 2010 Alan DeKok (aland@freeradius.org)
25 */
26RCSID("$Id: d3493bff53f35d10ec60c5722408223df9f14b0a $")
27
28typedef struct request_s request_t;
29
30#include <freeradius-devel/io/test_point.h>
31#include <freeradius-devel/server/cf_parse.h>
32#include <freeradius-devel/server/cf_util.h>
33#include <freeradius-devel/server/command.h>
34#include <freeradius-devel/server/dependency.h>
35#include <freeradius-devel/server/dl_module.h>
36#include <freeradius-devel/server/log.h>
37#include <freeradius-devel/server/map.h>
38#include <freeradius-devel/server/tmpl.h>
39#ifdef WITH_TLS
40# include <freeradius-devel/tls/base.h>
41#endif
42#include <freeradius-devel/unlang/base.h>
43#include <freeradius-devel/unlang/xlat.h>
44#include <freeradius-devel/unlang/xlat_func.h>
45#include <freeradius-devel/util/atexit.h>
46#include <freeradius-devel/util/base64.h>
47#include <freeradius-devel/util/calc.h>
48#include <freeradius-devel/util/conf.h>
49#include <freeradius-devel/util/dict.h>
50#include <freeradius-devel/util/dns.h>
51#include <freeradius-devel/util/file.h>
52#include <freeradius-devel/util/log.h>
53#include <freeradius-devel/util/misc.h>
54#include <freeradius-devel/util/pair_legacy.h>
55#include <freeradius-devel/util/sha1.h>
56#include <freeradius-devel/util/syserror.h>
57
58#include <ctype.h>
59
60#ifdef __clangd__
61# undef HAVE_SANITIZER_LSAN_INTERFACE_H
62#endif
63#ifdef HAVE_SANITIZER_LSAN_INTERFACE_H
64# include <sanitizer/asan_interface.h>
65#endif
66
67#ifdef HAVE_GETOPT_H
68# include <getopt.h>
69#endif
70
71#include <assert.h>
72#include <fcntl.h>
73#include <libgen.h>
74#include <limits.h>
75#include <sys/file.h>
76#include <sys/stat.h>
77#include <sys/wait.h>
78
79#ifndef HAVE_SANITIZER_LSAN_INTERFACE_H
80# define ASAN_POISON_MEMORY_REGION(_start, _end)
81# define ASAN_UNPOISON_MEMORY_REGION(_start, _end)
82#endif
83
84#define EXIT_WITH_FAILURE \
85do { \
86 ret = EXIT_FAILURE; \
87 goto cleanup; \
88} while (0)
89
90#define COMMAND_OUTPUT_MAX 8192
91
92#define RETURN_OK(_len) \
93 do { \
94 result->rcode = RESULT_OK; \
95 result->file = __FILE__; \
96 result->line = __LINE__; \
97 return (_len); \
98 } while (0)
99
100#define RETURN_OK_WITH_ERROR() \
101 do { \
102 result->rcode = RESULT_OK; \
103 result->file = __FILE__; \
104 result->line = __LINE__; \
105 result->error_to_data = true; \
106 return 0; \
107 } while (0)
108
109#define RETURN_NOOP(_len) \
110 do { \
111 result->rcode = RESULT_NOOP; \
112 result->file = __FILE__; \
113 result->line = __LINE__; \
114 return (_len); \
115 } while (0)
116
117#define RETURN_SKIP_FILE() \
118 do { \
119 result->rcode = RESULT_SKIP_FILE; \
120 result->file = __FILE__; \
121 result->line = __LINE__; \
122 return 0; \
123 } while (0)
124
125#define RETURN_PARSE_ERROR(_offset) \
126 do { \
127 result->rcode = RESULT_PARSE_ERROR; \
128 result->offset = _offset; \
129 result->file = __FILE__; \
130 result->line = __LINE__; \
131 return 0; \
132 } while (0)
133
134#define RETURN_COMMAND_ERROR() \
135 do { \
136 result->rcode = RESULT_COMMAND_ERROR; \
137 result->file = __FILE__; \
138 result->line = __LINE__; \
139 return 0; \
140 } while (0)
141
142#define RETURN_MISMATCH(_len) \
143 do { \
144 result->rcode = RESULT_MISMATCH; \
145 result->file = __FILE__; \
146 result->line = __LINE__; \
147 return (_len); \
148 } while (0)
149
150#define RETURN_EXIT(_ret) \
151 do { \
152 result->rcode = RESULT_EXIT; \
153 result->ret = _ret; \
154 result->file = __FILE__; \
155 result->line = __LINE__; \
156 return 0; \
157 } while (0)
158
159/** Default buffer size for a command_file_ctx_t
160 *
161 */
162#define DEFAULT_BUFFER_SIZE 1024
163
164typedef enum {
165 RESULT_OK = 0, //!< Not an error - Result as expected.
166 RESULT_NOOP, //!< Not an error - Did nothing...
167 RESULT_SKIP_FILE, //!< Not an error - Skip the rest of this file, or until we
168 ///< reach an "eof" command.
169 RESULT_PARSE_ERROR, //!< Fatal error - Command syntax error.
170 RESULT_COMMAND_ERROR, //!< Fatal error - Command operation error.
171 RESULT_MISMATCH, //!< Fatal error - Result didn't match what we expected.
172 RESULT_EXIT, //!< Stop processing files and exit.
174
176 { L("command-error"), RESULT_COMMAND_ERROR },
177 { L("exit"), RESULT_EXIT },
178 { L("ok"), RESULT_OK },
179 { L("parse-error"), RESULT_PARSE_ERROR },
180 { L("result-mismatch"), RESULT_MISMATCH },
181 { L("skip-file"), RESULT_SKIP_FILE },
182};
184
185typedef struct {
186 TALLOC_CTX *tmp_ctx; //!< Temporary context to hold buffers
187 ///< in this
188 union {
189 size_t offset; //!< Where we failed parsing the command.
190 int ret; //!< What code we should exit with.
191 };
192 char const *file;
193 int line;
197
198/** Configuration parameters passed to command functions
199 *
200 */
201typedef struct {
202 fr_dict_t *dict; //!< Dictionary to "reset" to.
203 fr_dict_gctx_t const *dict_gctx; //!< Dictionary gctx to "reset" to.
204 char const *raddb_dir;
205 char const *dict_dir;
206 char const *fuzzer_dir; //!< Where to write fuzzer files.
207 CONF_SECTION *features; //!< Enabled features.
209
210typedef struct {
211 TALLOC_CTX *tmp_ctx; //!< Talloc context for test points.
212
213 char *path; //!< Current path we're operating in.
214 char const *filename; //!< Current file we're operating on.
215 uint32_t lineno; //!< Current line number.
216
217 uint32_t test_count; //!< How many tests we've executed in this file.
218 ssize_t last_ret; //!< Last return value.
219
220 uint8_t *buffer; //!< Temporary resizable buffer we use for
221 ///< holding non-string data.
222 uint8_t *buffer_start; //!< Where the non-poisoned region of the buffer starts.
223 uint8_t *buffer_end; //!< Where the non-poisoned region of the buffer ends.
224
225 tmpl_rules_t tmpl_rules; //!< To pass to parsing functions.
226 fr_dict_t *test_internal_dict; //!< Internal dictionary of test_gctx.
227 fr_dict_gctx_t const *test_gctx; //!< Dictionary context for test dictionaries.
228
229 int fuzzer_dir; //!< File descriptor pointing to a a directory to
230 ///< write fuzzer output.
233
234
235typedef struct {
236 fr_dlist_t entry; //!< Entry in the dlist.
237 uint32_t start; //!< Start of line range.
238 uint32_t end; //!< End of line range.
240
241/** Command to execute
242 *
243 * @param[out] result Of executing the command.
244 * @param[in] cc Information about the file being processed.
245 * @param[in,out] data Output of this command, or the previous command.
246 * @param[in] data_used Length of data in the data buffer.
247 * @param[in] in Command text to process.
248 * @param[in] inlen Length of the remainder of the command to process.
249 */
251 size_t data_used, char *in, size_t inlen);
252
253typedef struct {
255 char const *usage;
256 char const *description;
258
260 { .required = true, .single = true, .type = FR_TYPE_STRING },
262};
263
267
269 UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request,
270 UNUSED fr_value_box_list_t *in)
271{
272 return XLAT_ACTION_DONE;
273}
274
275static char proto_name_prev[128];
276static dl_t *dl;
277static dl_loader_t *dl_loader = NULL;
278
279static fr_event_list_t *el = NULL;
280
281static char const *write_filename = NULL;
282static FILE *write_fp = NULL;
283
284size_t process_line(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen);
285static int process_file(bool *exit_now, TALLOC_CTX *ctx,
286 command_config_t const *config, const char *root_dir, char const *filename, fr_dlist_head_t *lines);
287
288#ifdef HAVE_SANITIZER_LSAN_INTERFACE_H
289# define BUFF_POISON_START 1024
290# define BUFF_POISON_END 1024
291
292/** Unpoison the start and end regions of the buffer
293 *
294 */
295static int _free_buffer(uint8_t *buff)
296{
297 size_t size = talloc_array_length(buff) - (BUFF_POISON_START + BUFF_POISON_END);
298
301
302 return 0;
303}
304#else
305# define BUFF_POISON_START 0
306# define BUFF_POISON_END 0
307#endif
308
309/** Allocate a special buffer with poisoned memory regions at the start and end
310 *
311 */
312static int poisoned_buffer_allocate(TALLOC_CTX *ctx, uint8_t **buff, size_t size)
313{
314 uint8_t *our_buff = *buff;
315
316 if (our_buff) {
317 /*
318 * If it's already the correct length
319 * don't bother re-allocing the buffer,
320 * just memset it to zero.
321 */
322 if ((size + BUFF_POISON_START + BUFF_POISON_END) == talloc_array_length(our_buff)) {
323 memset(our_buff + BUFF_POISON_START, 0, size);
324 return 0;
325 }
326
327 talloc_free(our_buff); /* Destructor de-poisons */
328 *buff = NULL;
329 }
330
331 our_buff = talloc_array(ctx, uint8_t, size + BUFF_POISON_START + BUFF_POISON_END);
332 if (!our_buff) return -1;
333
334#ifdef HAVE_SANITIZER_LSAN_INTERFACE_H
335 talloc_set_destructor(our_buff, _free_buffer);
336
337 /*
338 * Poison regions before and after the buffer
339 */
342#endif
343
344 *buff = our_buff;
345
346 return 0;
347}
348#define POISONED_BUFFER_START(_p) ((_p) + BUFF_POISON_START)
349#define POISONED_BUFFER_END(_p) ((_p) + BUFF_POISON_START + (talloc_array_length(_p) - (BUFF_POISON_START + BUFF_POISON_END)))
350
351static void mismatch_print(command_file_ctx_t *cc, char const *command,
352 char *expected, size_t expected_len, char *got, size_t got_len,
353 bool print_diff)
354{
355 char *g, *e;
356
357 ERROR("%s failed %s/%s:%d", command, cc->path, cc->filename, cc->lineno);
358
359 if (!print_diff) {
360 ERROR(" got : %.*s", (int) got_len, got);
361 ERROR(" expected : %.*s", (int) expected_len, expected);
362 } else {
363 g = got;
364 e = expected;
365
366 while (*g && *e && (*g == *e)) {
367 g++;
368 e++;
369 }
370
371 if (expected_len < 100) {
372 char const *spaces = " ";
373
374 ERROR(" got : %.*s", (int) got_len, got);
375 ERROR(" expected : %.*s", (int) expected_len, expected);
376 ERROR(" %.*s^ differs here (%zu)", (int) (e - expected), spaces, e - expected);
377 } else {
378 size_t glen, elen;
379
380 elen = strlen(e);
381 if (elen > 40) elen = 40;
382 glen = strlen(g);
383 if (glen > 40) glen = 40;
384
385 ERROR("(%zu) ... %.*s ... ", e - expected, (int) elen, e);
386 ERROR("(%zu) ... %.*s ... ", e - expected, (int) glen, g);
387 }
388 }
389}
390
391/** Print hex string to buffer
392 *
393 */
394static inline CC_HINT(nonnull) size_t hex_print(char *out, size_t outlen, uint8_t const *in, size_t inlen)
395{
396 char *p = out;
397 char *end = p + outlen;
398 size_t i;
399
400 if (inlen == 0) {
401 *p = '\0';
402 return 0;
403 }
404
405 for (i = 0; i < inlen; i++) {
406 size_t len;
407
408 len = snprintf(p, end - p, "%02x ", in[i]);
409 if (is_truncated(len, end - p)) return 0;
410
411 p += len;
412 }
413
414 *(--p) = '\0';
415
416 return p - out;
417}
418
419/** Concatenate error stack
420 */
421static inline size_t strerror_concat(char *out, size_t outlen)
422{
423 char *end = out + outlen;
424 char *p = out;
425 char const *err;
426
427 while ((p < end) && (err = fr_strerror_pop())) {
428 if (*fr_strerror_peek()) {
429 p += snprintf(p, end - p, "%s: ", err);
430 } else {
431 p += strlcpy(p, err, end - p);
432 }
433 }
434
435 return p - out;
436}
437
438static inline CC_HINT(nonnull) int dump_fuzzer_data(int fd_dir, char const *text, uint8_t const *data, size_t data_len)
439{
440 fr_sha1_ctx ctx;
442 char digest_str[(SHA1_DIGEST_LENGTH * 2) + 1];
443 int file_fd;
444
445 fr_sha1_init(&ctx);
446 fr_sha1_update(&ctx, (uint8_t const *)text, strlen(text));
447 fr_sha1_final(digest, &ctx);
448
449 /*
450 * We need to use the url alphabet as the standard
451 * one contains forwarded slashes which openat
452 * doesn't like.
453 */
454 fr_base64_encode_nstd(&FR_SBUFF_OUT(digest_str, sizeof(digest_str)), &FR_DBUFF_TMP(digest, sizeof(digest)),
456
457 file_fd = openat(fd_dir, digest_str, O_RDWR | O_CREAT | O_TRUNC,
458 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
459 if (file_fd < 0) {
460 fr_strerror_printf("Failed opening or creating corpus seed file \"%s\": %s",
461 digest_str, fr_syserror(errno));
462 return -1;
463 }
464
465 if (flock(file_fd, LOCK_EX) < 0) {
466 fr_strerror_printf("Failed locking corpus seed file \"%s\": %s",
467 digest_str, fr_syserror(errno));
468 return -1;
469 }
470
471 while (data_len) {
472 ssize_t ret;
473
474 ret = write(file_fd, data, data_len);
475 if (ret < 0) {
476 fr_strerror_printf("Failed writing to corpus seed file \"%s\": %s",
477 digest_str, fr_syserror(errno));
478 (void)flock(file_fd, LOCK_UN);
479 unlinkat(fd_dir, digest_str, 0);
480 return -1;
481 }
482 data_len -= ret;
483 data += ret;
484 }
485 (void)flock(file_fd, LOCK_UN);
486
487 return 0;
488}
489
490/*
491 * End of hacks for xlat
492 *
493 **********************************************************************/
494
495static ssize_t encode_tlv(char *buffer, uint8_t *output, size_t outlen);
496
497static char const hextab[] = "0123456789abcdef";
498
499static ssize_t encode_data_string(char *buffer, uint8_t *output, size_t outlen)
500{
501 ssize_t slen = 0;
502 char *p;
503
504 p = buffer + 1;
505
506 while (*p && (outlen > 0)) {
507 if (*p == '"') {
508 return slen;
509 }
510
511 if (*p != '\\') {
512 *(output++) = *(p++);
513 outlen--;
514 slen++;
515 continue;
516 }
517
518 switch (p[1]) {
519 default:
520 *(output++) = p[1];
521 break;
522
523 case 'n':
524 *(output++) = '\n';
525 break;
526
527 case 'r':
528 *(output++) = '\r';
529 break;
530
531 case 't':
532 *(output++) = '\t';
533 break;
534 }
535
536 outlen--;
537 slen++;
538 }
539
540 ERROR("String is not terminated");
541 return 0;
542}
543
544static ssize_t encode_data_tlv(char *buffer, char **endptr, uint8_t *output, size_t outlen)
545{
546 int depth = 0;
547 ssize_t slen;
548 char *p;
549
550 for (p = buffer; *p != '\0'; p++) {
551 if (*p == '{') depth++;
552 if (*p == '}') {
553 depth--;
554 if (depth == 0) break;
555 }
556 }
557
558 if (*p != '}') {
559 ERROR("No trailing '}' in string starting with \"%s\"", buffer);
560 return 0;
561 }
562
563 *endptr = p + 1;
564 *p = '\0';
565
566 p = buffer + 1;
568
569 slen = encode_tlv(p, output, outlen);
570 if (slen <= 0) return 0;
571
572 return slen;
573}
574
575static ssize_t hex_to_bin(uint8_t *out, size_t outlen, char *in, size_t inlen)
576{
577 char *p = in;
578 char *end = in + inlen;
579 uint8_t *out_p = out, *out_end = out_p + outlen;
580
581 while (p < end) {
582 char *c1, *c2;
583
584 if (out_p >= out_end) {
585 fr_strerror_const("Would overflow output buffer");
586 return -(p - in);
587 }
588
590
591 if (!*p) break;
592
593 c1 = memchr(hextab, tolower((uint8_t) *p++), sizeof(hextab));
594 if (!c1) {
595 bad_input:
596 fr_strerror_printf("Invalid hex data starting at \"%s\"", p);
597 return -(p - in);
598 }
599
600 c2 = memchr(hextab, tolower((uint8_t)*p++), sizeof(hextab));
601 if (!c2) goto bad_input;
602
603 *out_p++ = ((c1 - hextab) << 4) + (c2 - hextab);
604 }
605
606 return out_p - out;
607}
608
609
610static ssize_t encode_data(char *p, uint8_t *output, size_t outlen)
611{
612 ssize_t slen;
613
614 if (!isspace((uint8_t) *p)) {
615 ERROR("Invalid character following attribute definition");
616 return 0;
617 }
618
620
621 if (*p == '{') {
622 size_t sublen;
623 char *q;
624
625 slen = 0;
626
627 do {
629 if (!*p) {
630 if (slen == 0) {
631 ERROR("No data");
632 return 0;
633 }
634
635 break;
636 }
637
638 sublen = encode_data_tlv(p, &q, output, outlen);
639 if (sublen <= 0) return 0;
640
641 slen += sublen;
642 output += sublen;
643 outlen -= sublen;
644 p = q;
645 } while (*q);
646
647 return slen;
648 }
649
650 if (*p == '"') {
651 slen = encode_data_string(p, output, outlen);
652 return slen;
653 }
654
655 slen = hex_to_bin(output, outlen, p, strlen(p));
656 if (slen <= 0) {
657 fr_strerror_const_push("Empty hex string");
658 return slen;
659 }
660
661 return slen;
662}
663
664static int decode_attr(char *buffer, char **endptr)
665{
666 long attr;
667
668 attr = strtol(buffer, endptr, 10);
669 if (*endptr == buffer) {
670 ERROR("No valid number found in string starting with \"%s\"", buffer);
671 return 0;
672 }
673
674 if (!**endptr) {
675 ERROR("Nothing follows attribute number");
676 return 0;
677 }
678
679 if ((attr <= 0) || (attr > 256)) {
680 ERROR("Attribute number is out of valid range");
681 return 0;
682 }
683
684 return (int) attr;
685}
686
687static int decode_vendor(char *buffer, char **endptr)
688{
689 long vendor;
690
691 if (*buffer != '.') {
692 ERROR("Invalid separator before vendor id");
693 return 0;
694 }
695
696 vendor = strtol(buffer + 1, endptr, 10);
697 if (*endptr == (buffer + 1)) {
698 ERROR("No valid vendor number found");
699 return 0;
700 }
701
702 if (!**endptr) {
703 ERROR("Nothing follows vendor number");
704 return 0;
705 }
706
707 if ((vendor <= 0) || (vendor > (1 << 24))) {
708 ERROR("Vendor number is out of valid range");
709 return 0;
710 }
711
712 if (**endptr != '.') {
713 ERROR("Invalid data following vendor number");
714 return 0;
715 }
716 (*endptr)++;
717
718 return (int) vendor;
719}
720
721static ssize_t encode_tlv(char *buffer, uint8_t *output, size_t outlen)
722{
723 int attr;
724 ssize_t slen;
725 char *p;
726
727 attr = decode_attr(buffer, &p);
728 if (attr == 0) return 0;
729
730 output[0] = attr;
731 output[1] = 2;
732
733 if (*p == '.') {
734 p++;
735 slen = encode_tlv(p, output + 2, outlen - 2);
736
737 } else {
738 slen = encode_data(p, output + 2, outlen - 2);
739 }
740
741 if (slen <= 0) return slen;
742 if (slen > (255 - 2)) {
743 ERROR("TLV data is too long");
744 return 0;
745 }
746
747 output[1] += slen;
748
749 return slen + 2;
750}
751
752static ssize_t encode_vsa(char *buffer, uint8_t *output, size_t outlen)
753{
754 int vendor;
755 ssize_t slen;
756 char *p;
757
758 vendor = decode_vendor(buffer, &p);
759 if (vendor == 0) return 0;
760
761 output[0] = 0;
762 output[1] = (vendor >> 16) & 0xff;
763 output[2] = (vendor >> 8) & 0xff;
764 output[3] = vendor & 0xff;
765
766 slen = encode_tlv(p, output + 4, outlen - 4);
767 if (slen <= 0) return slen;
768 if (slen > (255 - 6)) {
769 ERROR("VSA data is too long");
770 return 0;
771 }
772
773 return slen + 4;
774}
775
776static ssize_t encode_evs(char *buffer, uint8_t *output, size_t outlen)
777{
778 int vendor;
779 int attr;
780 ssize_t slen;
781 char *p;
782
783 vendor = decode_vendor(buffer, &p);
784 if (vendor == 0) return 0;
785
786 attr = decode_attr(p, &p);
787 if (attr == 0) return 0;
788
789 output[0] = 0;
790 output[1] = (vendor >> 16) & 0xff;
791 output[2] = (vendor >> 8) & 0xff;
792 output[3] = vendor & 0xff;
793 output[4] = attr;
794
795 slen = encode_data(p, output + 5, outlen - 5);
796 if (slen <= 0) return slen;
797
798 return slen + 5;
799}
800
801static ssize_t encode_extended(char *buffer, uint8_t *output, size_t outlen)
802{
803 int attr;
804 ssize_t slen;
805 char *p;
806
807 attr = decode_attr(buffer, &p);
808 if (attr == 0) return 0;
809
810 output[0] = attr;
811
812 if (attr == 26) {
813 slen = encode_evs(p, output + 1, outlen - 1);
814 } else {
815 slen = encode_data(p, output + 1, outlen - 1);
816 }
817 if (slen <= 0) return slen;
818 if (slen > (255 - 3)) {
819 ERROR("Extended Attr data is too long");
820 return 0;
821 }
822
823 return slen + 1;
824}
825
826static ssize_t encode_long_extended(char *buffer, uint8_t *output, size_t outlen)
827{
828 int attr;
829 ssize_t slen, total;
830 char *p;
831
832 attr = decode_attr(buffer, &p);
833 if (attr == 0) return 0;
834
835 /* output[0] is the extended attribute */
836 output[1] = 4;
837 output[2] = attr;
838 output[3] = 0;
839
840 if (attr == 26) {
841 slen = encode_evs(p, output + 4, outlen - 4);
842 if (slen <= 0) return slen;
843
844 output[1] += 5;
845 slen -= 5;
846 } else {
847 slen = encode_data(p, output + 4, outlen - 4);
848 }
849 if (slen <= 0) return slen;
850
851 total = 0;
852 while (1) {
853 int sublen = 255 - output[1];
854
855 if (slen <= sublen) {
856 output[1] += slen;
857 total += output[1];
858 break;
859 }
860
861 slen -= sublen;
862
863 memmove(output + 255 + 4, output + 255, slen);
864 memcpy(output + 255, output, 4);
865
866 output[1] = 255;
867 output[3] |= 0x80;
868
869 output += 255;
870 output[1] = 4;
871 total += 255;
872 }
873
874 return total;
875}
876
877static ssize_t encode_rfc(char *buffer, uint8_t *output, size_t outlen)
878{
879 int attr;
880 ssize_t slen, sublen;
881 char *p;
882
883 attr = decode_attr(buffer, &p);
884 if (attr == 0) return 0;
885
886 slen = 2;
887 output[0] = attr;
888 output[1] = 2;
889
890 if (attr == 26) {
891 sublen = encode_vsa(p, output + 2, outlen - 2);
892
893 } else if ((attr < 241) || (attr > 246)) {
894 sublen = encode_data(p, output + 2, outlen - 2);
895
896 } else {
897 if (*p != '.') {
898 ERROR("Invalid data following attribute number");
899 return 0;
900 }
901
902 if (attr < 245) {
903 sublen = encode_extended(p + 1, output + 2, outlen - 2);
904 } else {
905 /*
906 * Not like the others!
907 */
908 return encode_long_extended(p + 1, output, outlen);
909 }
910 }
911 if (sublen <= 0) return sublen;
912 if (sublen > (255 -2)) {
913 ERROR("RFC Data is too long");
914 return 0;
915 }
916
917 output[1] += sublen;
918 return slen + sublen;
919}
920
921
922static void unload_proto_library(void)
923{
924 TALLOC_FREE(dl);
925}
926
927static ssize_t load_proto_library(char const *proto_name)
928{
929 char dl_name[128];
930
931 if (strcmp(proto_name_prev, proto_name) != 0) {
932 /*
933 * Ensure the old proto library is unloaded
934 */
936
937 snprintf(dl_name, sizeof(dl_name), "libfreeradius-%s", proto_name);
938 if (dl) TALLOC_FREE(dl);
939
940 dl = dl_by_name(dl_loader, dl_name, NULL, false);
941 if (!dl) {
942 fr_perror("Failed to link to library \"%s\"", dl_name);
944 return 0;
945 }
946
947 strlcpy(proto_name_prev, proto_name, sizeof(proto_name_prev));
948 }
949
950 return strlen(proto_name);
951}
952
953static ssize_t load_test_point_by_command(void **symbol, char *command, char const *dflt_symbol)
954{
955 char buffer[256];
956 char const *p, *q;
957 void *dl_symbol;
958
959 if (!dl) {
960 fr_strerror_printf("No protocol library loaded. Specify library with \"load <proto name>\"");
961 return 0;
962 }
963
964 p = command;
965
966 /*
967 * Use the dflt_symbol name as the test point
968 */
969 if ((*p == '.') && (q = strchr(p, ' ')) && (q != (p + 1)) && ((size_t)(q - p) < sizeof(buffer))) {
970 p++;
971 strlcpy(buffer, p, (q - p) + 1);
972 p = q + 1;
973 } else {
974 snprintf(buffer, sizeof(buffer), "%s_%s", proto_name_prev, dflt_symbol);
975 }
976
977 dl_symbol = dlsym(dl->handle, buffer);
978 if (!dl_symbol) {
979 fr_strerror_printf("Test point (symbol \"%s\") not exported by library", buffer);
981 return 0;
982 }
983 *symbol = dl_symbol;
984
985 return p - command;
986}
987
989{
990 if (cc->tmpl_rules.attr.dict_def) {
992 }
993
994 return cc->config->dict;
995}
996
997/** Common dictionary load function
998 *
999 * Callers call fr_dict_global_ctx_set to set the context
1000 * the dictionaries will be loaded into.
1001 */
1002static int dictionary_load_common(command_result_t *result, command_file_ctx_t *cc, char const *in, char const *default_subdir)
1003{
1004 char const *dir;
1005 char *q;
1006 char const *name;
1007 char *tmp = NULL;
1008 int ret;
1009 fr_dict_t *dict;
1010
1011 if (in[0] == '\0') {
1012 fr_strerror_const("Missing dictionary name");
1014 }
1015
1016 /*
1017 * Decrease ref count if we're loading in a new dictionary
1018 */
1019 if (cc->tmpl_rules.attr.dict_def) {
1021 }
1022
1023 q = strchr(in, ' ');
1024 if (q) {
1025 name = tmp = talloc_bstrndup(NULL, in, q - in);
1026 q++;
1027 dir = q;
1028 } else {
1029 name = in;
1030 dir = default_subdir;
1031 }
1032
1033 ret = fr_dict_protocol_afrom_file(&dict, name, dir, __FILE__);
1034 talloc_free(tmp);
1035 if (ret < 0) RETURN_COMMAND_ERROR();
1036
1037 cc->tmpl_rules.attr.dict_def = dict;
1038 cc->tmpl_rules.attr.namespace = fr_dict_root(dict);
1039
1040 /*
1041 * Dump the dictionary if we're in super debug mode
1042 */
1044
1045 RETURN_OK(0);
1046}
1047
1048static size_t parse_typed_value(command_result_t *result, fr_value_box_t *box, char const **out, char const *in, size_t inlen)
1049{
1051 size_t match_len;
1052 ssize_t slen;
1053 char const *p;
1054 fr_sbuff_t sbuff;
1055
1056 /*
1057 * Parse data types
1058 */
1060 if (fr_type_is_null(type)) {
1062 }
1063 fr_assert(match_len < inlen);
1064
1065 p = in + match_len;
1067 *out = p;
1068
1069 /*
1070 * As a hack, allow most things to be inside
1071 * double-quoted strings. This is really only for dates,
1072 * which are space-delimited.
1073 */
1074 if (*p == '"'){
1075 p++;
1076 sbuff = FR_SBUFF_IN(p, strlen(p));
1077 slen = fr_value_box_from_substr(box, box, FR_TYPE_STRING, NULL,
1078 &sbuff,
1080 if (slen < 0) {
1082 }
1083
1084 p += fr_sbuff_used(&sbuff);
1085 if (*p != '"') {
1087 }
1088 p++;
1089
1090 if (type != FR_TYPE_STRING) {
1091 if (fr_value_box_cast_in_place(box, box, type, NULL) < 0) {
1093 }
1094 }
1095
1096 } else {
1097 sbuff = FR_SBUFF_IN(p, strlen(p));
1098
1099 slen = fr_value_box_from_substr(box, box, type, NULL,
1100 &sbuff,
1102 if (slen < 0) {
1104 }
1105 p += fr_sbuff_used(&sbuff);
1106 }
1108
1109 RETURN_OK(p - in);
1110}
1111
1112static fr_cmd_t *command_head = NULL;
1113
1114static int command_func(UNUSED FILE *fp, UNUSED FILE *fp_err, UNUSED void *ctx, UNUSED fr_cmd_info_t const *info)
1115{
1116 return 0;
1117}
1118
1119static int command_walk(UNUSED void *ctx, fr_cmd_walk_info_t *info)
1120{
1121 int i;
1122
1123 for (i = 0; i < info->num_parents; i++) {
1124 printf("%s ", info->parents[i]);
1125 }
1126
1127 printf(":%s ", info->name);
1128 if (info->syntax) printf("%s", info->syntax);
1129 printf("%s", "");
1130
1131 return 1;
1132}
1133
1134static void command_print(void)
1135{
1136 void *walk_ctx = NULL;
1137
1138 printf("Command hierarchy --------");
1140
1141 printf("Command list --------");
1142 while (fr_command_walk(command_head, &walk_ctx, NULL, command_walk) == 1) {
1143 // do nothing
1144 }
1145}
1146
1147#define CLEAR_TEST_POINT(_cc) \
1148do { \
1149 talloc_free_children((_cc)->tmp_ctx); \
1150 tp = NULL; \
1151} while (0)
1152
1153/** Placeholder function for comments
1154 *
1155 */
1157 UNUSED char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
1158{
1159 return 0;
1160}
1161
1162/** Execute another test file
1163 *
1164 */
1166 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
1167{
1168 char *q;
1169 bool exit_now = false;
1170 int ret;
1171
1172 if (write_fp) {
1173 fprintf(stderr, "Can't do $INCLUDE with -w %s\n", write_filename);
1174 RETURN_EXIT(1);
1175 }
1176
1177 q = strrchr(cc->path, '/');
1178 if (q) {
1179 *q = '\0';
1180 ret = process_file(&exit_now, cc->tmp_ctx, cc->config, cc->path, in, NULL);
1181 if (exit_now || (ret != 0)) RETURN_EXIT(ret);
1182 *q = '/';
1183 RETURN_OK(0);
1184 }
1185
1186 ret = process_file(&exit_now, cc->tmp_ctx, cc->config, NULL, in, NULL);
1187 if (exit_now || (ret != 0)) RETURN_EXIT(ret);
1188
1189 RETURN_OK(0);
1190}
1191
1192/** Determine if unresolved attributes are allowed
1193 *
1194 */
1196 UNUSED char *data, UNUSED size_t data_used, char *in, size_t inlen)
1197{
1198 fr_sbuff_t our_in = FR_SBUFF_IN(in, inlen);
1199 bool res;
1200
1201 if (fr_sbuff_out_bool(&res, &our_in) == 0) {
1202 fr_strerror_printf("Invalid boolean value, must be \"yes\" or \"no\"");
1204 }
1206
1207 RETURN_OK(0);
1208}
1209
1210static const fr_token_t token2op[UINT8_MAX + 1] = {
1211 [ '+' ] = T_ADD,
1212 [ '-' ] = T_SUB,
1213 [ '*' ] = T_MUL,
1214 [ '/' ] = T_DIV,
1215 [ '^' ] = T_XOR,
1216 [ '.' ] = T_ADD,
1217 [ '&' ] = T_AND,
1218 [ '|' ] = T_OR,
1219 [ '%' ] = T_MOD,
1220};
1221
1222/** Perform calculations
1223 *
1224 */
1226 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1227{
1228 fr_value_box_t *a, *b, *out;
1229 size_t match_len;
1231 fr_token_t op;
1232 char const *p, *value, *end;
1233 size_t slen;
1234 bool assignment;
1235
1236 a = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1237 b = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1238
1239 p = in;
1240 end = in + inlen;
1241
1242 match_len = parse_typed_value(result, a, &value, p, end - p);
1243 if (match_len == 0) return 0; /* errors have already been updated */
1244
1245 p += match_len;
1247
1248 op = fr_table_value_by_longest_prefix(&match_len, fr_tokens_table, p, end - p, T_INVALID);
1249 if (op != T_INVALID) {
1250 p += match_len;
1251 assignment = fr_assignment_op[op];
1252
1253 } else {
1254 op = token2op[(uint8_t) p[0]];
1255 if (op == T_INVALID) {
1256 fr_strerror_printf("Unknown operator '%c'", p[0]);
1258 }
1259 p++;
1260
1261 assignment = false;
1262 }
1264
1265 match_len = parse_typed_value(result, b, &value, p, end - p);
1266 if (match_len == 0) return 0;
1267
1268 p += match_len;
1270
1271 if (assignment) {
1272 if (fr_value_calc_assignment_op(cc->tmp_ctx, a, op, b) < 0) {
1274 }
1275 out = a;
1276
1277 } else {
1278 out = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1279
1280 /*
1281 * If there's no output data type, then the code tries to
1282 * figure one out automatically.
1283 */
1284 if (!*p) {
1286 } else {
1287 if (strncmp(p, "->", 2) != 0) RETURN_PARSE_ERROR(0);
1288 p += 2;
1290
1293 fr_value_box_init(out, type, NULL, false);
1294 }
1295
1296 if (fr_value_calc_binary_op(cc->tmp_ctx, out, type, a, op, b) < 0) {
1298 }
1299 }
1300
1302 if (slen <= 0) RETURN_OK_WITH_ERROR();
1303
1304 RETURN_OK(slen);
1305}
1306
1307/** Perform calculations on multi-valued ops
1308 *
1309 */
1311 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1312{
1313 fr_value_box_t *group, *a, *out;
1314 size_t match_len;
1316 fr_token_t op;
1317 char const *p, *value, *end;
1318 size_t slen;
1319
1320 group = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1321 fr_value_box_init(group, FR_TYPE_GROUP, NULL, false);
1322
1323 p = in;
1324 end = in + inlen;
1325
1326 /*
1327 * Multi-valued operations
1328 */
1329 op = token2op[(uint8_t) p[0]];
1330 if (op == T_INVALID) {
1331 fr_strerror_printf("Unknown operator '%c'", p[0]);
1333 }
1334 p++;
1335
1336 while (p < end) {
1338
1339 a = talloc_zero(group, fr_value_box_t);
1340
1341 match_len = parse_typed_value(result, a, &value, p, end - p);
1342 if (match_len == 0) return 0; /* errors have already been updated */
1343
1344 fr_value_box_list_insert_tail(&group->vb_group, a);
1345
1346 p += match_len;
1347
1348 if (strncmp(p, "->", 2) == 0) break;
1349 }
1350
1351 out = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1353
1354 if (strncmp(p, "->", 2) != 0) RETURN_PARSE_ERROR(0);
1355 p += 2;
1357
1360
1361
1362 if (fr_value_calc_nary_op(cc->tmp_ctx, out, type, op, group) < 0) {
1364 }
1365
1367 if (slen <= 0) RETURN_OK_WITH_ERROR();
1368
1369 RETURN_OK(slen);
1370}
1371
1372/** Perform casting
1373 *
1374 */
1376 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1377{
1378 fr_value_box_t *a, *out;
1379 size_t match_len;
1381 char const *p, *value, *end;
1382 size_t slen;
1383
1384 a = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1385
1386 p = in;
1387 end = in + inlen;
1388
1389 match_len = parse_typed_value(result, a, &value, p, end - p);
1390 if (match_len == 0) return 0; /* errors have already been updated */
1391
1392 p += match_len;
1394
1395 out = talloc_zero(cc->tmp_ctx, fr_value_box_t);
1396
1397 if (strncmp(p, "->", 2) != 0) RETURN_PARSE_ERROR(0);
1398 p += 2;
1400
1403 fr_value_box_init(out, type, NULL, false);
1404
1405 if (fr_value_box_cast(out, out, type, NULL, a) < 0) {
1407 }
1408
1410 if (slen <= 0) RETURN_OK_WITH_ERROR();
1411
1412 RETURN_OK(slen);
1413}
1414
1415/** Change the working directory
1416 *
1417 */
1419 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1420{
1421 TALLOC_FREE(cc->path); /* Free old directories */
1422
1423 cc->path = fr_realpath(cc->tmp_ctx, in, inlen);
1424 if (!cc->path) RETURN_COMMAND_ERROR();
1425
1427
1428 RETURN_OK(talloc_array_length(cc->path) - 1);
1429}
1430
1431/*
1432 * Clear the data buffer
1433 */
1435 char *data, size_t UNUSED data_used, UNUSED char *in, UNUSED size_t inlen)
1436{
1437 memset(data, 0, COMMAND_OUTPUT_MAX);
1438 RETURN_NOOP(0);
1439}
1440
1441/*
1442 * Add a command by talloc'ing a table for it.
1443 */
1445 char *data, size_t UNUSED data_used, char *in, UNUSED size_t inlen)
1446{
1447 char *p, *name;
1448 char *parent = NULL;
1449 fr_cmd_table_t *table;
1450 char buffer[8192];
1451
1452 table = talloc_zero(cc->tmp_ctx, fr_cmd_table_t);
1453
1454 strlcpy(buffer, in, sizeof(buffer));
1455
1456 p = strchr(buffer, ':');
1457 if (!p) {
1458 fr_strerror_const("no ':name' specified");
1460 }
1461
1462 *p = '\0';
1463 p++;
1464
1465 parent = talloc_strdup(cc->tmp_ctx, in);
1466
1467 /*
1468 * Set the name and try to find the syntax.
1469 */
1470 name = p;
1472
1473 if (isspace((uint8_t) *p)) {
1474 *p = '\0';
1475 p++;
1476 }
1477
1479
1480 if (*p) {
1481 table->syntax = talloc_strdup(table, p);
1482 }
1483 table->parent = parent;
1484 table->name = name;
1485 table->help = NULL;
1486 table->func = command_func;
1487 table->tab_expand = NULL;
1488 table->read_only = true;
1489
1490 if (fr_command_add(table, &command_head, NULL, NULL, table) < 0) {
1491 fr_strerror_const_push("ERROR: Failed adding command");
1493 }
1494
1496
1498}
1499
1500/*
1501 * Do tab completion on a command
1502 */
1504 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
1505{
1506 int i;
1507 int num_expansions;
1508 char const *expansions[CMD_MAX_ARGV];
1509 char *p = data, *end = p + COMMAND_OUTPUT_MAX, **argv;
1510 fr_cmd_info_t info;
1511 size_t len;
1512
1513 info.argc = 0;
1514 info.max_argc = CMD_MAX_ARGV;
1515 info.argv = talloc_zero_array(cc->tmp_ctx, char const *, CMD_MAX_ARGV);
1516 info.box = talloc_zero_array(cc->tmp_ctx, fr_value_box_t *, CMD_MAX_ARGV);
1517
1518 memcpy(&argv, &info.argv, sizeof(argv)); /* const issues */
1519 info.argc = fr_dict_str_to_argv(in, argv, CMD_MAX_ARGV);
1520 if (info.argc <= 0) {
1521 fr_strerror_const("Failed splitting input");
1522 RETURN_PARSE_ERROR(-(info.argc));
1523 }
1524
1525 num_expansions = fr_command_tab_expand(cc->tmp_ctx, command_head, &info, CMD_MAX_ARGV, expansions);
1526
1527 len = snprintf(p, end - p, "%d - ", num_expansions);
1528 if (is_truncated(len, end - p)) {
1529 oob:
1530 fr_strerror_const("Out of output buffer space for radmin command");
1532 }
1533 p += len;
1534
1535 for (i = 0; i < num_expansions; i++) {
1536 len = snprintf(p, end - p, "'%s', ", expansions[i]);
1537 if (is_truncated(len, end - p)) goto oob;
1538 p += len;
1539 }
1540
1541 /*
1542 * Remove the trailing ", "
1543 */
1544 if (num_expansions > 0) {
1545 p -= 2;
1546 *p = '\0';
1547 }
1548
1549 return p - data;
1550}
1551
1552/** Parse and reprint a condition
1553 *
1554 */
1556 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1557{
1558 ssize_t slen;
1559 CONF_SECTION *cs;
1560 size_t len;
1561 xlat_exp_head_t *head = NULL;
1562
1563 cs = cf_section_alloc(NULL, NULL, "if", "condition");
1564 if (!cs) {
1565 fr_strerror_const("Out of memory");
1567 }
1568 cf_filename_set(cs, cc->filename);
1569 cf_lineno_set(cs, cc->lineno);
1570
1572
1573 slen = xlat_tokenize_condition(cc->tmp_ctx, &head, &FR_SBUFF_IN(in, inlen), NULL, &cc->tmpl_rules);
1574 if (slen == 0) {
1575 fr_strerror_printf_push_head("ERROR failed to parse any input");
1576 talloc_free(cs);
1578 }
1579
1580 if (slen < 0) {
1581 fr_strerror_printf_push_head("ERROR offset %d", (int) -slen - 1);
1582 talloc_free(cs);
1584 }
1585
1586 if ((size_t) slen < inlen) {
1587 len = snprintf(data, COMMAND_OUTPUT_MAX, "ERROR passed in %zu, returned %zd", inlen, slen);
1588
1589 } else {
1591 }
1592
1594 talloc_free(cs);
1595
1596 RETURN_OK(len);
1597}
1598
1600 char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
1601{
1602 size_t len;
1603
1604 len = snprintf(data, COMMAND_OUTPUT_MAX, "%u", cc->test_count);
1605 if (is_truncated(len, COMMAND_OUTPUT_MAX)) {
1606 fr_strerror_const("Command count would overflow data buffer (shouldn't happen)");
1608 }
1609
1610 RETURN_OK(len);
1611}
1612
1614 char *data, size_t data_used, char *in, size_t inlen)
1615{
1616 fr_test_point_pair_decode_t *tp = NULL;
1617 void *decode_ctx = NULL;
1618 char *p;
1619 uint8_t *to_dec;
1620 uint8_t *to_dec_end;
1621 ssize_t slen;
1622
1623 fr_dict_attr_t const *da;
1624 fr_pair_t *head;
1625
1626 da = fr_dict_attr_by_name(NULL, fr_dict_root(fr_dict_internal()), "request");
1627 fr_assert(da != NULL);
1628 head = fr_pair_afrom_da(cc->tmp_ctx, da);
1629 if (!head) {
1630 fr_strerror_const_push("Failed allocating memory");
1632 }
1633
1634 p = in;
1635
1636 slen = load_test_point_by_command((void **)&tp, in, "tp_decode_pair");
1637 if (!tp) {
1638 fr_strerror_const_push("Failed locating decoder testpoint");
1640 }
1641
1642 p += slen;
1644
1645 if (tp->test_ctx && (tp->test_ctx(&decode_ctx, cc->tmp_ctx, dictionary_current(cc)) < 0)) {
1646 fr_strerror_const_push("Failed initialising decoder testpoint");
1648 }
1649
1650 /*
1651 * Hack because we consume more of the command string
1652 * so we need to check this again.
1653 */
1654 if (*p == '-') {
1655 p = data;
1656 inlen = data_used;
1657 }
1658
1659 /*
1660 * Decode hex from input text
1661 */
1663 if (slen <= 0) {
1664 CLEAR_TEST_POINT(cc);
1665 RETURN_PARSE_ERROR(-(slen));
1666 }
1667
1668 to_dec = (uint8_t *)data;
1669 to_dec_end = to_dec + slen;
1670
1672
1673 /*
1674 * Run the input data through the test
1675 * point to produce fr_pair_ts.
1676 */
1677 while (to_dec < to_dec_end) {
1678 slen = tp->func(head, &head->vp_group, cc->tmpl_rules.attr.namespace,
1679 (uint8_t *)to_dec, (to_dec_end - to_dec), decode_ctx);
1680 cc->last_ret = slen;
1681 if (slen <= 0) {
1683 CLEAR_TEST_POINT(cc);
1685 }
1686 if ((size_t)slen > (size_t)(to_dec_end - to_dec)) {
1687 fr_perror("%s: Internal sanity check failed at %d", __FUNCTION__, __LINE__);
1689 CLEAR_TEST_POINT(cc);
1691 }
1692 to_dec += slen;
1693 }
1694
1695 /*
1696 * Clear any spurious errors
1697 */
1700
1701 /*
1702 * Output may be an error, and we ignore
1703 * it if so.
1704 */
1705 slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head->vp_group);
1706 if (slen <= 0) {
1708 }
1709
1710 CLEAR_TEST_POINT(cc);
1711 RETURN_OK(slen);
1712}
1713
1715 char *data, size_t data_used, char *in, size_t inlen)
1716{
1718 void *decode_ctx = NULL;
1719 char *p;
1720 uint8_t *to_dec;
1721 uint8_t *to_dec_end;
1722 ssize_t slen;
1723
1724 fr_dict_attr_t const *da;
1725 fr_pair_t *head;
1726
1727 da = fr_dict_attr_by_name(NULL, fr_dict_root(fr_dict_internal()), "request");
1728 fr_assert(da != NULL);
1729 head = fr_pair_afrom_da(cc->tmp_ctx, da);
1730 if (!head) {
1731 fr_strerror_const_push("Failed allocating memory");
1733 }
1734
1735 p = in;
1736
1737 slen = load_test_point_by_command((void **)&tp, in, "tp_decode_proto");
1738 if (!tp) {
1739 fr_strerror_const_push("Failed locating decoder testpoint");
1741 }
1742
1743 p += slen;
1745
1746 if (tp->test_ctx && (tp->test_ctx(&decode_ctx, cc->tmp_ctx, dictionary_current(cc)) < 0)) {
1747 fr_strerror_const_push("Failed initialising decoder testpoint");
1749 }
1750
1751 /*
1752 * Hack because we consume more of the command string
1753 * so we need to check this again.
1754 */
1755 if (*p == '-') {
1756 p = data;
1757 inlen = data_used;
1758 }
1759
1760 /*
1761 * Decode hex from input text
1762 */
1764 if (slen <= 0) {
1765 CLEAR_TEST_POINT(cc);
1766 RETURN_PARSE_ERROR(-(slen));
1767 }
1768
1769 to_dec = (uint8_t *)data;
1770 to_dec_end = to_dec + slen;
1771
1773
1774 slen = tp->func(head, &head->vp_group,
1775 (uint8_t *)to_dec, (to_dec_end - to_dec), decode_ctx);
1776 cc->last_ret = slen;
1777 if (slen <= 0) {
1779 CLEAR_TEST_POINT(cc);
1781 }
1782
1783 /*
1784 * Clear any spurious errors
1785 */
1788
1789 /*
1790 * Output may be an error, and we ignore
1791 * it if so.
1792 */
1793
1794 /*
1795 * Print the pairs.
1796 */
1797 slen = fr_pair_list_print(&FR_SBUFF_OUT(data, COMMAND_OUTPUT_MAX), NULL, &head->vp_group);
1798 if (slen <= 0) {
1799 fr_assert(0);
1801 }
1802
1803 CLEAR_TEST_POINT(cc);
1804 RETURN_OK(slen);
1805}
1806
1807/** Parse a dictionary attribute, writing "ok" to the data buffer is everything was ok
1808 *
1809 */
1811 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
1812{
1814
1816}
1817
1818/** Print the currently loaded dictionary
1819 *
1820 */
1822 UNUSED char *data, size_t data_used, UNUSED char *in, UNUSED size_t inlen)
1823{
1825
1826 /*
1827 * Don't modify the contents of the data buffer
1828 */
1829 RETURN_OK(data_used);
1830}
1831
1832static CC_HINT(nonnull)
1834 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
1835{
1836 size_t need;
1837 ssize_t ret;
1838 char *p, *next;
1839 uint8_t *enc_p;
1840 char buffer[8192];
1841
1842 strlcpy(buffer, in, sizeof(buffer));
1843
1844 p = buffer;
1845 next = strchr(p, ',');
1846 if (next) *next = 0;
1847
1848 enc_p = cc->buffer_start;
1849
1850 while (true) {
1851 fr_value_box_t *box = talloc_zero(NULL, fr_value_box_t);
1852
1854
1855 if (fr_value_box_from_str(box, box, FR_TYPE_STRING, NULL,
1856 p, strlen(p),
1858 talloc_free(box);
1860 }
1861
1862 ret = fr_dns_label_from_value_box(&need,
1863 cc->buffer_start, cc->buffer_end - cc->buffer_start, enc_p, true, box, NULL);
1864 talloc_free(box);
1865
1866 if (ret < 0) RETURN_OK_WITH_ERROR();
1867
1868 if (ret == 0) RETURN_OK(snprintf(data, COMMAND_OUTPUT_MAX, "need=%zd", need));
1869
1870 enc_p += ret;
1871
1872 /*
1873 * Go to the next input string
1874 */
1875 if (!next) break;
1876
1877 p = next + 1;
1878 next = strchr(p, ',');
1879 if (next) *next = 0;
1880 }
1881
1882 if ((cc->fuzzer_dir >= 0) &&
1883 (dump_fuzzer_data(cc->fuzzer_dir, in, cc->buffer_start, enc_p - cc->buffer_start) < 0)) {
1885 }
1886
1887 RETURN_OK(hex_print(data, COMMAND_OUTPUT_MAX, cc->buffer_start, enc_p - cc->buffer_start));
1888}
1889
1891 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1892{
1893 ssize_t slen, total, i, outlen;
1894 char *out, *end;
1895 fr_value_box_t *box = talloc_zero(NULL, fr_value_box_t);
1896
1897 /*
1898 * Decode hex from input text
1899 */
1900 total = hex_to_bin(cc->buffer_start, cc->buffer_end - cc->buffer_start, in, inlen);
1901 if (total <= 0) RETURN_PARSE_ERROR(-total);
1902
1903 out = data;
1904 end = data + COMMAND_OUTPUT_MAX;
1905
1906 for (i = 0; i < total; i += slen) {
1907 slen = fr_dns_label_to_value_box(box, box, cc->buffer_start, total, cc->buffer_start + i, false, NULL);
1908 if (slen <= 0) {
1909 error:
1910 talloc_free(box);
1912 }
1913
1914 /*
1915 * Separate names by commas
1916 */
1917 if (i > 0) *(out++) = ',';
1918
1919 /*
1920 * We don't print it with quotes.
1921 */
1922 outlen = fr_value_box_print(&FR_SBUFF_OUT(out, end - out), box, NULL);
1923 if (outlen <= 0) goto error;
1924 out += outlen;
1925
1926 fr_value_box_clear(box);
1927 }
1928
1929 talloc_free(box);
1930 RETURN_OK(out - data);
1931}
1932
1934 char *data, UNUSED size_t data_used, char *in, size_t inlen)
1935{
1936 fr_test_point_pair_encode_t *tp = NULL;
1937
1938 fr_dcursor_t cursor;
1939 void *encode_ctx = NULL;
1940 ssize_t slen;
1941 char *p = in;
1942
1943 uint8_t *enc_p, *enc_end;
1945 fr_pair_t *vp;
1946 bool truncate = false;
1947
1948 size_t iterations = 0;
1949 fr_pair_parse_t root, relative;
1950
1952
1953 slen = load_test_point_by_command((void **)&tp, p, "tp_encode_pair");
1954 if (!tp) {
1955 fr_strerror_const_push("Failed locating encode testpoint");
1956 CLEAR_TEST_POINT(cc);
1958 }
1959
1960 p += ((size_t)slen);
1962
1963 /*
1964 * The truncate torture test.
1965 *
1966 * Increase the buffer one byte at a time until all items in the cursor
1967 * have been encoded.
1968 *
1969 * The poisoned region at the end of the buffer will detect overruns
1970 * if we're running with asan.
1971 *
1972 */
1973 if (strncmp(p, "truncate", sizeof("truncate") - 1) == 0) {
1974 truncate = true;
1975 p += sizeof("truncate") - 1;
1977 }
1978
1979 if (tp->test_ctx && (tp->test_ctx(&encode_ctx, cc->tmp_ctx, dictionary_current(cc)) < 0)) {
1980 fr_strerror_const_push("Failed initialising encoder testpoint");
1981 CLEAR_TEST_POINT(cc);
1983 }
1984
1985 root = (fr_pair_parse_t) {
1986 .ctx = cc->tmp_ctx,
1987 .da = cc->tmpl_rules.attr.namespace,
1988 .list = &head,
1989 };
1990 relative = (fr_pair_parse_t) { };
1991
1992 slen = fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(p, inlen - (p - in)));
1993 if (slen <= 0) {
1994 CLEAR_TEST_POINT(cc);
1996 }
1997
1999
2000 /*
2001 * Outer loop implements truncate test
2002 */
2003 do {
2004 enc_p = cc->buffer_start;
2005 enc_end = truncate ? cc->buffer_start + iterations++ : cc->buffer_end;
2006
2007 if (truncate) {
2008#ifdef HAVE_SANITIZER_LSAN_INTERFACE_H
2009 /*
2010 * Poison the region between the subset of the buffer
2011 * we're using and the end of the buffer.
2012 */
2013 ASAN_POISON_MEMORY_REGION(enc_end, (cc->buffer_end) - enc_end);
2014
2015 DEBUG("%s[%d]: Iteration %zu - Safe region %p-%p (%zu bytes), "
2016 "poisoned region %p-%p (%zu bytes)", cc->filename, cc->lineno, iterations - 1,
2017 enc_p, enc_end, enc_end - enc_p, enc_end, cc->buffer_end, cc->buffer_end - enc_end);
2018#else
2019 DEBUG("%s[%d]: Iteration %zu - Allowed region %p-%p (%zu bytes)",
2020 cc->filename, cc->lineno, iterations - 1, enc_p, enc_end, enc_end - enc_p);
2021#endif
2022 }
2023
2024 for (vp = fr_pair_dcursor_iter_init(&cursor, &head,
2026 dictionary_current(cc));
2027 vp;
2028 vp = fr_dcursor_current(&cursor)) {
2029 slen = tp->func(&FR_DBUFF_TMP(enc_p, enc_end), &cursor, encode_ctx);
2030 cc->last_ret = slen;
2031
2032 if (truncate) DEBUG("%s[%d]: Iteration %zu - Result %zd%s%s",
2033 cc->filename, cc->lineno, iterations - 1, slen,
2034 *fr_strerror_peek() != '\0' ? " - " : "",
2035 *fr_strerror_peek() != '\0' ? fr_strerror_peek() : "");
2036 if (slen < 0) break;
2037
2038 /*
2039 * Encoder indicated it encoded too much data
2040 */
2041 if (slen > (enc_end - enc_p)) {
2042 fr_strerror_printf("Expected returned encoded length <= %zu bytes, got %zu bytes",
2043 (enc_end - enc_p), (size_t)slen);
2044#ifdef HAVE_SANITIZER_LSAN_INTERFACE_H
2045 if (truncate) ASAN_UNPOISON_MEMORY_REGION(enc_end, (cc->buffer_end) - enc_end);
2046#endif
2048 CLEAR_TEST_POINT(cc);
2050 }
2051
2052 enc_p += slen;
2053
2054 if (slen == 0) break;
2055
2056 }
2057
2058#ifdef HAVE_SANITIZER_LSAN_INTERFACE_H
2059 /*
2060 * un-poison the region between the subset of the buffer
2061 * we're using and the end of the buffer.
2062 */
2063 if (truncate) ASAN_UNPOISON_MEMORY_REGION(enc_end, (cc->buffer_end) - enc_end);
2064#endif
2065 /*
2066 * We consumed all the VPs, so presumably encoded the
2067 * complete pair list.
2068 */
2069 if (!vp) break;
2070 } while (truncate && (enc_end < cc->buffer_end));
2071
2072 /*
2073 * Last iteration result in an error
2074 */
2075 if (slen < 0) {
2077 CLEAR_TEST_POINT(cc);
2079 }
2080
2081 /*
2082 * Clear any spurious errors
2083 */
2085
2087
2088 CLEAR_TEST_POINT(cc);
2089
2090 if ((cc->fuzzer_dir >= 0) &&
2091 (dump_fuzzer_data(cc->fuzzer_dir, p, cc->buffer_start, enc_p - cc->buffer_start) < 0)) {
2093 }
2094
2096}
2097
2098/** Encode a RADIUS attribute writing the result to the data buffer as space separated hexits
2099 *
2100 */
2102 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2103{
2104 size_t len;
2105 char buffer[8192];
2106
2107 strlcpy(buffer, in, sizeof(buffer));
2108
2109 len = encode_rfc(buffer, cc->buffer_start, cc->buffer_end - cc->buffer_start);
2110 if (len <= 0) RETURN_PARSE_ERROR(0);
2111
2112 if (len >= (size_t)(cc->buffer_end - cc->buffer_start)) {
2113 fr_strerror_const("Encoder output would overflow output buffer");
2115 }
2116
2118}
2119
2120/** Parse a list of pairs
2121 *
2122 */
2124 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2125{
2126 ssize_t slen;
2128 bool done = false;
2129 char *filename;
2130 FILE *fp;
2131
2132 filename = talloc_asprintf(cc->tmp_ctx, "%s/%s", cc->path, in);
2133
2134 fp = fopen(filename, "r");
2135 talloc_free(filename);
2136
2137 if (!fp) {
2138 fr_strerror_printf("Failed opening %s - %s", in, fr_syserror(errno));
2140 }
2141
2144 fclose(fp);
2145 if (slen < 0) {
2147 }
2148
2149 /*
2150 * Print the pairs.
2151 */
2153 if (slen <= 0) {
2154 fr_assert(0);
2156 }
2157
2158 if (!done) {
2159 strlcpy(data + slen, "!DONE", COMMAND_OUTPUT_MAX - slen);
2160 slen += 5;
2161 }
2162
2164
2165 RETURN_OK(slen);
2166}
2167
2168
2170 char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
2171{
2173}
2174
2176 char *data, UNUSED size_t data_used, char *in, size_t inlen)
2177{
2179
2180 void *encode_ctx = NULL;
2181 ssize_t slen;
2182 char *p = in;
2183
2185 fr_pair_parse_t root, relative;
2186
2188
2189 slen = load_test_point_by_command((void **)&tp, p, "tp_encode_proto");
2190 if (!tp) {
2191 fr_strerror_const_push("Failed locating encode testpoint");
2192 CLEAR_TEST_POINT(cc);
2194 }
2195
2196 p += ((size_t)slen);
2198 if (tp->test_ctx && (tp->test_ctx(&encode_ctx, cc->tmp_ctx, dictionary_current(cc)) < 0)) {
2199 fr_strerror_const_push("Failed initialising encoder testpoint");
2200 CLEAR_TEST_POINT(cc);
2202 }
2203
2204 root = (fr_pair_parse_t) {
2205 .ctx = cc->tmp_ctx,
2206 .da = cc->tmpl_rules.attr.namespace,
2207 .list = &head,
2208 };
2209 relative = (fr_pair_parse_t) { };
2210
2211 slen = fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(p, inlen - (p - in)));
2212 if (slen <= 0) {
2213 CLEAR_TEST_POINT(cc);
2215 }
2216
2217 slen = tp->func(cc->tmp_ctx, &head, cc->buffer_start, cc->buffer_end - cc->buffer_start, encode_ctx);
2219 cc->last_ret = slen;
2220 if (slen < 0) {
2221 CLEAR_TEST_POINT(cc);
2223 }
2224 /*
2225 * Clear any spurious errors
2226 */
2228
2229 CLEAR_TEST_POINT(cc);
2230
2231 if ((cc->fuzzer_dir >= 0) &&
2232 (dump_fuzzer_data(cc->fuzzer_dir, p, cc->buffer_start, slen) < 0)) {
2234 }
2235
2237}
2238
2239/** Command eof
2240 *
2241 * Mark the end of a test file if we're reading from stdin.
2242 *
2243 * Doesn't actually do anything, is just a placeholder for the command processing loop.
2244 */
2246 UNUSED char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
2247{
2248 return 0;
2249}
2250
2251/** Enable fuzzer output
2252 *
2253 * Any commands that produce potentially useful corpus seed data will write that out data
2254 * to files in the specified directory, using the md5 of the text input at as the file name.
2255 *
2256 */
2258 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2259{
2260 int fd;
2261 struct stat sdir;
2262 char *fuzzer_dir;
2263 bool retry_dir = true;
2264
2265 /*
2266 * Close any open fuzzer output dirs
2267 */
2268 if (cc->fuzzer_dir >= 0) {
2269 close(cc->fuzzer_dir);
2270 cc->fuzzer_dir = -1;
2271 }
2272
2273 if (in[0] == '\0') {
2274 fr_strerror_const("Missing directory name");
2276 }
2277
2278 fuzzer_dir = talloc_asprintf(cc->tmp_ctx, "%s/%s",
2279 cc->config->fuzzer_dir ? cc->config->fuzzer_dir : cc->path, in);
2280
2281again:
2282 fd = open(fuzzer_dir, O_RDONLY);
2283 if (fd < 0) {
2284 if (mkdir(fuzzer_dir, 0777) == 0) {
2285 fd = open(fuzzer_dir, O_RDONLY);
2286 if (fd >= 0) goto stat;
2287 /*
2288 * Prevent race if multiple unit_test_attribute instances
2289 * attempt to create the same output dir.
2290 */
2291 } else if ((errno == EEXIST) && retry_dir) {
2292 retry_dir = false; /* Only allow this once */
2293 goto again;
2294 }
2295
2296 fr_strerror_printf("fuzzer-out \"%s\" doesn't exist: %s", fuzzer_dir, fr_syserror(errno));
2298 }
2299
2300stat:
2301 if (fstat(fd, &sdir) < 0) {
2302 close(fd);
2303 fr_strerror_printf("failed statting fuzzer-out \"%s\": %s", fuzzer_dir, fr_syserror(errno));
2305 }
2306
2307 if (!(sdir.st_mode & S_IFDIR)) {
2308 close(fd);
2309 fr_strerror_printf("fuzzer-out \"%s\" is not a directory", fuzzer_dir);
2311 }
2312 cc->fuzzer_dir = fd;
2313 talloc_free(fuzzer_dir);
2314
2315 return 0;
2316}
2317
2318/** Exit gracefully with the specified code
2319 *
2320 */
2322 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2323{
2324 if (!*in) RETURN_EXIT(0);
2325
2326 RETURN_EXIT(atoi(in));
2327}
2328
2330 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2331{
2332 char *name, *tmp = NULL;
2333 char const *dir;
2334 char *q;
2335 int ret;
2336
2338
2339 if (in[0] == '\0') {
2340 fr_strerror_const("Missing dictionary name");
2342 }
2343
2344 q = strchr(in, ' ');
2345 if (q) {
2346 name = tmp = talloc_bstrndup(NULL, in, q - in);
2347 q++;
2348 dir = q;
2349 } else {
2350 name = in;
2351 dir = cc->path;
2352 }
2353
2355 talloc_free(tmp);
2356 if (ret < 0) RETURN_COMMAND_ERROR();
2357
2358 RETURN_OK(0);
2359}
2360
2361
2362/** Compare the data buffer to an expected value
2363 *
2364 */
2366 char *data, size_t data_used, char *in, size_t inlen)
2367{
2368 if (strcmp(in, data) != 0) {
2369 if (write_fp) {
2370 strcpy(in, data);
2371 RETURN_OK(data_used);
2372 }
2373
2374 mismatch_print(cc, "match", in, inlen, data, data_used, true);
2375 RETURN_MISMATCH(data_used);
2376 }
2377
2378 /*
2379 * We didn't actually write anything, but this
2380 * keeps the contents of the data buffer around
2381 * for the next command to operate on.
2382 */
2383 RETURN_OK(data_used);
2384}
2385
2386/** Compare the data buffer against an expected expression
2387 *
2388 */
2390 char *data, size_t data_used, char *in, size_t inlen)
2391{
2392 ssize_t slen;
2393 regex_t *regex;
2394 int ret;
2395
2396 slen = regex_compile(cc->tmp_ctx, &regex, in, inlen, NULL, false, true);
2397 if (slen <= 0) RETURN_COMMAND_ERROR();
2398
2399 ret = regex_exec(regex, data, data_used, NULL);
2400 talloc_free(regex);
2401
2402 switch (ret) {
2403 case -1:
2404 default:
2406
2407 case 0:
2408 mismatch_print(cc, "match-regex", in, inlen, data, data_used, false);
2409 RETURN_MISMATCH(data_used);
2410
2411 case 1:
2412 RETURN_OK(data_used);
2413 }
2414}
2415
2416/** Artificially limit the maximum packet size.
2417 *
2418 */
2420 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2421{
2422 unsigned long size;
2423 char *end;
2424
2426
2427 if (*in != '\0') {
2428 size = strtoul(in, &end, 10);
2429 if ((size == ULONG_MAX) || *end || (size >= 65536)) {
2430 fr_strerror_const_push("Invalid integer");
2432 }
2433 } else {
2434 size = DEFAULT_BUFFER_SIZE;
2435 }
2436
2437 if (poisoned_buffer_allocate(cc, &cc->buffer, size) < 0) RETURN_EXIT(1);
2440
2441 RETURN_OK(snprintf(data, COMMAND_OUTPUT_MAX, "%ld", size));
2442}
2443
2444/** Set or clear migration flags.
2445 *
2446 */
2448 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2449{
2450 char *p;
2451 bool *out;
2452
2454 p = in;
2455
2456 if (strncmp(p, "xlat_new_functions", sizeof("xlat_new_functions") - 1) == 0) {
2457 p += sizeof("xlat_new_functions") - 1;
2459
2460 } else {
2461 fr_strerror_const("Unknown migration flag");
2463 }
2464
2466 if (*p != '=') {
2467 fr_strerror_const("Missing '=' after flag");
2469 }
2470 p++;
2471
2473 if ((strcmp(p, "yes") == 0) || (strcmp(p, "true") == 0) || (strcmp(p, "1") == 0)) {
2474 *out = true;
2475
2476 } else if ((strcmp(p, "no") == 0) || (strcmp(p, "false") == 0) || (strcmp(p, "0") == 0)) {
2477 *out = false;
2478
2479 } else {
2480 fr_strerror_const("Invalid value for flag");
2482 }
2483
2484 RETURN_OK(0);
2485}
2486
2487/** Skip the test file if we're missing a particular feature
2488 *
2489 */
2491 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2492{
2493 CONF_PAIR *cp;
2494
2495 if (in[0] == '\0') {
2496 fr_strerror_printf("Prerequisite syntax is \"need-feature <feature>\". "
2497 "Use -f to print features");
2499 }
2500
2501 cp = cf_pair_find(cc->config->features, in);
2502 if (!cp || (strcmp(cf_pair_value(cp), "yes") != 0)) {
2503 DEBUG("Skipping, missing feature \"%s\"", in);
2505 }
2506
2507 RETURN_NOOP(0);
2508}
2509
2510/** Negate the result of a match command or any command which returns "OK"
2511 *
2512 */
2514 char *data, size_t data_used, char *in, size_t inlen)
2515{
2516 data_used = process_line(result, cc, data, data_used, in, inlen);
2517 switch (result->rcode) {
2518 /*
2519 * OK becomes a command error
2520 */
2521 case RESULT_OK:
2522 ERROR("%s[%d]: %.*s: returned 'ok', where we expected 'result-mismatch'",
2523 cc->filename, cc->lineno, (int) inlen, in);
2524 RETURN_MISMATCH(data_used);
2525
2526 /*
2527 * Mismatch becomes OK
2528 */
2529 case RESULT_MISMATCH:
2530 RETURN_OK(data_used);
2531
2532 /*
2533 * The rest are unchanged...
2534 */
2535 default:
2536 break;
2537 }
2538
2539 return data_used;
2540}
2541
2542/** Parse an print an attribute pair or pair list.
2543 *
2544 */
2546 char *data, UNUSED size_t data_used, char *in, size_t inlen)
2547{
2549 ssize_t slen;
2550 fr_dict_t const *dict = dictionary_current(cc);
2551 fr_pair_parse_t root, relative;
2552
2554
2555 root = (fr_pair_parse_t) {
2556 .ctx = cc->tmp_ctx,
2557 .da = fr_dict_root(dict),
2558 .list = &head,
2559 };
2560 relative = (fr_pair_parse_t) { };
2561
2562 slen = fr_pair_list_afrom_substr(&root, &relative, &FR_SBUFF_IN(in, inlen));
2563 if (slen <= 0) {
2564// fr_strerror_printf_push_head("ERROR offset %d", (int) -slen);
2567 }
2568
2569 /*
2570 * Output may be an error, and we ignore
2571 * it if so.
2572 */
2573
2575 if (slen <= 0) {
2578 }
2579
2581 RETURN_OK(slen);
2582}
2583
2584/** Dynamically load a protocol library
2585 *
2586 */
2588 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2589{
2590 ssize_t slen;
2591
2592 if (*in == '\0') {
2593 fr_strerror_printf("Load syntax is \"proto <lib_name>\"");
2595 }
2596
2598 slen = load_proto_library(in);
2599 if (slen <= 0) RETURN_PARSE_ERROR(-(slen));
2600
2601 RETURN_OK(0);
2602}
2603
2605 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2606{
2608 return dictionary_load_common(result, cc, in, NULL);
2609}
2610
2612 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2613{
2614 fr_dict_t const *dict = dictionary_current(cc);
2615 fr_dict_attr_t const *root_da = fr_dict_root(dict);
2616 fr_dict_attr_t const *new_root;
2617
2618 if (is_whitespace(in) || (*in == '\0')) {
2619 new_root = fr_dict_root(dict);
2620 } else {
2621 new_root = fr_dict_attr_by_name(NULL, fr_dict_root(dict), in);
2622 if (!new_root) {
2623 fr_strerror_printf("dictionary attribute \"%s\" not found in %s", in, root_da->name);
2625 }
2626 }
2627
2628 cc->tmpl_rules.attr.namespace = new_root;
2629
2630 RETURN_OK(0);
2631}
2632
2633/** Touch a file to indicate a test completed
2634 *
2635 */
2637 UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2638{
2639 if (fr_unlink(in) < 0) RETURN_COMMAND_ERROR();
2640 if (fr_touch(NULL, in, 0644, true, 0755) <= 0) RETURN_COMMAND_ERROR();
2641
2642 RETURN_OK(0);
2643}
2644
2645/** Callback for a tmpl rule parser
2646 *
2647 */
2648typedef ssize_t(*command_tmpl_rule_func)(TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value);
2649
2651{
2652 bool res;
2653 ssize_t slen;
2654
2655 slen = fr_sbuff_out_bool(&res, value);
2656 rules->attr.allow_foreign = res;
2657 return slen;
2658}
2659
2661{
2662 bool res;
2663 ssize_t slen;
2664
2665 slen = fr_sbuff_out_bool(&res, value);
2666 rules->attr.allow_unknown = res;
2667 return slen;
2668}
2669
2671{
2672 bool res;
2673 ssize_t slen;
2674
2675 slen = fr_sbuff_out_bool(&res, value);
2676 rules->attr.allow_unresolved = res;
2677 return slen;
2678}
2679
2681{
2683 fr_slen_t slen;
2684
2686 &rules->attr.namespace,
2687 rules->attr.dict_def ? fr_dict_root(rules->attr.dict_def) :
2689 value, NULL);
2691 return slen;
2692}
2693
2695{
2696 ssize_t slen;
2697
2699
2700 if (slen == 0) {
2701 fr_strerror_printf("Invalid list specifier \"%pV\"",
2703 }
2704
2705 return slen;
2706}
2707
2709{
2710 fr_slen_t slen;
2711
2712 slen = tmpl_request_ref_list_afrom_substr(ctx, NULL,
2713 &rules->attr.request_def,
2714 value);
2715 if (slen < 0) {
2716 fr_strerror_printf("Invalid request specifier \"%pV\"",
2718 }
2719
2720 return slen;
2721}
2722
2724 UNUSED char *data, UNUSED size_t data_used, char *in, size_t inlen)
2725{
2726 fr_sbuff_t sbuff = FR_SBUFF_IN(in, inlen);
2727 ssize_t slen;
2729 void *res;
2730
2731 static fr_table_ptr_sorted_t tmpl_rule_func_table[] = {
2732 { L("allow_foreign"), (void *)command_tmpl_rule_allow_foreign },
2733 { L("allow_unknown"), (void *)command_tmpl_rule_allow_unknown },
2734 { L("allow_unresolved"), (void *)command_tmpl_rule_allow_unresolved },
2735 { L("attr_parent"), (void *)command_tmpl_rule_attr_parent },
2736 { L("list_def"), (void *)command_tmpl_rule_list_def },
2737 { L("request_def"), (void *)command_tmpl_rule_request_def }
2738 };
2739 static size_t tmpl_rule_func_table_len = NUM_ELEMENTS(tmpl_rule_func_table);
2740
2741 while (fr_sbuff_extend(&sbuff)) {
2742 fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL);
2743
2744 fr_sbuff_out_by_longest_prefix(&slen, &res, tmpl_rule_func_table, &sbuff, NULL);
2745 if (res == NULL) {
2746 fr_strerror_printf("Specified rule \"%pV\" is invalid",
2749 }
2750 func = (command_tmpl_rule_func)res; /* -Wpedantic */
2751
2752 fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL);
2753
2754 if (!fr_sbuff_next_if_char(&sbuff, '=')) {
2755 fr_strerror_printf("Expected '=' after rule identifier, got \"%pV\"",
2758 }
2759
2760 fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL);
2761
2762 if (func(cc->tmp_ctx, &cc->tmpl_rules, &sbuff) <= 0) RETURN_COMMAND_ERROR();
2763 }
2764
2765 return fr_sbuff_used(&sbuff);
2766}
2767
2769 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2770{
2771 fr_value_box_t *box = talloc_zero(NULL, fr_value_box_t);
2772 fr_value_box_t *box2;
2773 char const *value;
2774 size_t match_len;
2775 ssize_t slen;
2777
2778 match_len = parse_typed_value(result, box, &value, in, strlen(in));
2779 if (match_len == 0) {
2780 talloc_free(box);
2781 return 0; /* errors have already been updated */
2782 }
2783
2784 type = box->type;
2785
2786 /*
2787 * Don't print dates with enclosing quotation marks.
2788 */
2789 if (type != FR_TYPE_DATE) {
2792 } else {
2794 }
2795 if (slen <= 0) {
2796 talloc_free(box);
2798 }
2799
2800 /*
2801 * Behind the scenes, parse the data
2802 * string. We should get the same value
2803 * box as last time.
2804 */
2805 box2 = talloc_zero(NULL, fr_value_box_t);
2806 if (fr_value_box_from_str(box2, box2, type, NULL,
2807 data, slen,
2809 talloc_free(box2);
2810 talloc_free(box);
2812 }
2813
2814 /*
2815 * They MUST be identical
2816 */
2817 if (fr_value_box_cmp(box, box2) != 0) {
2818 fr_strerror_const("ERROR value box reparsing failed. Results not identical");
2819 fr_strerror_printf_push("out: %pV (as string %.*s)", box2, (int) slen, data);
2820 fr_strerror_printf_push("in: %pV (from string %s)", box, value);
2821 talloc_free(box2);
2822 talloc_free(box);
2824 }
2825
2826 /*
2827 * Store <type><value str...>
2828 */
2829 if (cc->fuzzer_dir >= 0) {
2830 char fuzzer_buffer[1024];
2831 char *fuzzer_p = fuzzer_buffer, *fuzzer_end = fuzzer_p + sizeof(fuzzer_buffer);
2832
2833 *fuzzer_p++ = (uint8_t)type; /* Fuzzer uses first byte for type */
2834
2835 strlcpy(fuzzer_p, data, slen > fuzzer_end - fuzzer_p ? fuzzer_end - fuzzer_p : slen);
2836
2837 if (dump_fuzzer_data(cc->fuzzer_dir, fuzzer_buffer,
2838 (uint8_t *)fuzzer_buffer, strlen(fuzzer_buffer)) < 0) {
2840 }
2841 }
2842
2843 talloc_free(box2);
2844 talloc_free(box);
2845 RETURN_OK(slen);
2846}
2847
2849 char *data, size_t data_used, char *in, size_t inlen)
2850{
2851 int fd;
2852 char *path;
2853 bool locked = false;
2854
2855 path = talloc_bstrndup(cc->tmp_ctx, in, inlen);
2856
2857 fd = open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
2858 if (fd < 0) {
2859 fr_strerror_printf("Failed opening \"%s\": %s", path, fr_syserror(errno));
2860 error:
2861 talloc_free(path);
2862 if (fd >= 0) {
2863 if (locked) (void)flock(fd, LOCK_UN);
2864 close(fd);
2865 }
2867 }
2868
2869 if (flock(fd, LOCK_EX) < 0) {
2870 fr_strerror_printf("Failed locking \"%s\": %s", path, fr_syserror(errno));
2871 goto error;
2872 }
2873 locked = true;
2874
2875 while (data_used) {
2876 ssize_t ret;
2877 ret = write(fd, data, data_used);
2878 if (ret < 0) {
2879 fr_strerror_printf("Failed writing to \"%s\": %s", path, fr_syserror(errno));
2880 goto error;
2881 }
2882 data_used -= ret;
2883 data += ret;
2884 }
2885 (void)flock(fd, LOCK_UN);
2886 talloc_free(path);
2887 close(fd);
2888
2889 RETURN_OK(data_used);
2890}
2891
2892/** Parse an reprint and xlat expansion
2893 *
2894 */
2896 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2897{
2898 ssize_t slen;
2899 xlat_exp_head_t *head = NULL;
2900 size_t input_len = strlen(in), escaped_len;
2901 fr_sbuff_parse_rules_t p_rules = { .escapes = &fr_value_unescape_double };
2902
2903 slen = xlat_tokenize(cc->tmp_ctx, &head, &FR_SBUFF_IN(in, input_len), &p_rules,
2904 &(tmpl_rules_t) {
2905 .attr = {
2906 .dict_def = dictionary_current(cc),
2907 .list_def = request_attr_request,
2908 .allow_unresolved = cc->tmpl_rules.attr.allow_unresolved
2909 },
2910 .xlat = cc->tmpl_rules.xlat,
2911 });
2912 if (slen == 0) {
2913 fr_strerror_printf_push_head("ERROR failed to parse any input");
2915 }
2916
2917 if (slen < 0) {
2918 fr_strerror_printf_push_head("ERROR offset %d", (int) -slen - 1);
2919
2920 return_error:
2922 }
2923
2924 if (((size_t) slen != input_len)) {
2925 fr_strerror_printf_push_head("offset %d 'Too much text'", (int) slen);
2926 goto return_error;
2927 }
2928
2930 RETURN_OK(escaped_len);
2931}
2932
2933/** Parse and reprint an xlat expression expansion
2934 *
2935 */
2937 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2938{
2939 ssize_t dec_len;
2940 xlat_exp_head_t *head = NULL;
2941 size_t input_len = strlen(in), escaped_len;
2942// fr_sbuff_parse_rules_t p_rules = { .escapes = &fr_value_unescape_double };
2943
2944 dec_len = xlat_tokenize_expression(cc->tmp_ctx, &head, &FR_SBUFF_IN(in, input_len), NULL,
2945 &(tmpl_rules_t) {
2946 .attr = {
2947 .dict_def = dictionary_current(cc),
2948 .allow_unresolved = cc->tmpl_rules.attr.allow_unresolved,
2949 .list_def = request_attr_request,
2950 }
2951 });
2952 if (dec_len <= 0) {
2953 fr_strerror_printf_push_head("ERROR offset %d", (int) -dec_len);
2954
2955 return_error:
2957 }
2958
2959 if (((size_t) dec_len != input_len)) {
2960 fr_strerror_printf_push_head("Passed in %zu characters, but only parsed %zd characters", input_len, dec_len);
2961 goto return_error;
2962 }
2963
2965 RETURN_OK(escaped_len);
2966}
2967
2968/** Parse, purify, and reprint an xlat expression expansion
2969 *
2970 */
2972 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
2973{
2974 ssize_t slen;
2975 xlat_exp_head_t *head = NULL;
2976 size_t input_len = strlen(in), escaped_len;
2977 tmpl_rules_t t_rules = (tmpl_rules_t) {
2978 .attr = {
2980 .allow_unresolved = cc->tmpl_rules.attr.allow_unresolved,
2981 .list_def = request_attr_request,
2982 },
2983 .xlat = cc->tmpl_rules.xlat,
2984 .at_runtime = true,
2985 };
2986
2987 if (!el) {
2988 fr_strerror_const("Flag '-p' not used. xlat_purify is disabled");
2989 goto return_error;
2990 }
2991 t_rules.xlat.runtime_el = el;
2992
2993 slen = xlat_tokenize_expression(cc->tmp_ctx, &head, &FR_SBUFF_IN(in, input_len), NULL, &t_rules);
2994 if (slen == 0) {
2995 fr_strerror_printf_push_head("ERROR failed to parse any input");
2997 }
2998
2999 if (slen < 0) {
3000 fr_strerror_printf_push_head("ERROR offset %d", (int) -slen - 1);
3001 return_error:
3003 }
3004
3005 if (((size_t) slen != input_len)) {
3006 fr_strerror_printf_push_head("Passed in %zu characters, but only parsed %zd characters", input_len, slen);
3007 goto return_error;
3008 }
3009
3010 if (fr_debug_lvl > 2) {
3011 DEBUG("Before purify --------------------------------------------------");
3013 }
3014
3015 if (xlat_purify(head, NULL) < 0) {
3016 fr_strerror_printf_push_head("ERROR purifying node - %s", fr_strerror());
3017 goto return_error;
3018 }
3019
3020 if (fr_debug_lvl > 2) {
3021 DEBUG("After purify --------------------------------------------------");
3023 }
3024
3026 RETURN_OK(escaped_len);
3027}
3028
3029
3030/** Parse an reprint and xlat argv expansion
3031 *
3032 */
3034 char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
3035{
3036 int i, argc;
3037 char *p;
3038 ssize_t slen;
3039 xlat_exp_head_t *head = NULL;
3040 xlat_exp_head_t **argv;
3041 size_t len;
3042 size_t input_len = strlen(in);
3043 char buff[1024];
3044
3045 slen = xlat_tokenize_argv(cc->tmp_ctx, &head, &FR_SBUFF_IN(in, input_len),
3046 NULL, NULL,
3047 &(tmpl_rules_t) {
3048 .attr = {
3049 .dict_def = dictionary_current(cc),
3050 .list_def = request_attr_request,
3051 .allow_unresolved = cc->tmpl_rules.attr.allow_unresolved
3052 },
3053 }, true);
3054 if (slen <= 0) {
3055 fr_strerror_printf_push_head("ERROR offset %d", (int) -slen);
3057 }
3058
3059 argc = xlat_flatten_to_argv(cc->tmp_ctx, &argv, head);
3060 if (argc <= 0) {
3061 fr_strerror_printf_push("ERROR in argument %d", (int) -argc);
3063 }
3064
3065 for (i = 0, p = data; i < argc; i++) {
3066 (void) xlat_print(&FR_SBUFF_OUT(buff, sizeof(buff)), argv[i], NULL);
3067
3068 len = snprintf(p, data + COMMAND_OUTPUT_MAX - p, "[%d]{ %s }, ", i, buff);
3069 p += len;
3070 }
3071
3072 p -= 2;
3073 *p = '\0';
3074
3075 RETURN_OK(p - data);
3076}
3077
3079 { L("#"), &(command_entry_t){
3080 .func = command_comment,
3081 .usage = "#<string>",
3082 .description = "A comment - not processed"
3083 }},
3084 { L("$INCLUDE "), &(command_entry_t){
3085 .func = command_include,
3086 .usage = "$INCLUDE <relative_path>",
3087 .description = "Execute a test file"
3088 }},
3089 { L("allow-unresolved "), &(command_entry_t){
3091 .usage = "allow-unresolved yes|no",
3092 .description = "Allow or disallow unresolved attributes in xlats and references"
3093 }},
3094 { L("calc "), &(command_entry_t){
3095 .func = command_calc,
3096 .usage = "calc <type1> <value1> <operator> <type2> <value2> -> <output-type>",
3097 .description = "Perform calculations on value boxes",
3098 }},
3099 { L("calc_nary "), &(command_entry_t){
3100 .func = command_calc_nary,
3101 .usage = "calc_nary op <type1> <value1> <type2> <value2> ... -> <output-type>",
3102 .description = "Perform calculations on value boxes",
3103 }},
3104 { L("cast "), &(command_entry_t){
3105 .func = command_cast,
3106 .usage = "cast (type) <value> -> <output-type>",
3107 .description = "Perform calculations on value boxes",
3108 }},
3109 { L("cd "), &(command_entry_t){
3110 .func = command_cd,
3111 .usage = "cd <path>",
3112 .description = "Change the directory for loading dictionaries and $INCLUDEs, writing the full path into the data buffer on success"
3113 }},
3114 { L("clear"), &(command_entry_t){
3115 .func = command_clear,
3116 .usage = "clear",
3117 .description = "Explicitly zero out the contents of the data buffer"
3118 }},
3119 { L("command add "), &(command_entry_t){
3120 .func = command_radmin_add,
3121 .usage = "command add <string>",
3122 .description = "Add a command to a radmin command tree"
3123 }},
3124 { L("command tab "), &(command_entry_t){
3125 .func = command_radmin_tab,
3126 .usage = "command tab <string>",
3127 .description = "Test a tab completion against a radmin command tree"
3128 }},
3129 { L("condition "), &(command_entry_t){
3131 .usage = "condition <string>",
3132 .description = "Parse and reprint a condition, writing the normalised condition to the data buffer on success"
3133 }},
3134 { L("count"), &(command_entry_t){
3135 .func = command_count,
3136 .usage = "count",
3137 .description = "Write the number of executed tests to the data buffer. A test is any command that should return 'ok'"
3138 }},
3139 { L("decode-dns-label "), &(command_entry_t){
3141 .usage = "decode-dns-label (-|<hex_string>)",
3142 .description = "Decode one or more DNS labels, writing the decoded strings to the data buffer.",
3143 }},
3144 { L("decode-pair"), &(command_entry_t){
3145 .func = command_decode_pair,
3146 .usage = "decode-pair[.<testpoint_symbol>] (-|<hex_string>)",
3147 .description = "Produce an attribute value pair from a binary value using a specified protocol decoder. Protocol must be loaded with \"load <protocol>\" first",
3148 }},
3149 { L("decode-proto"), &(command_entry_t){
3150 .func = command_decode_proto,
3151 .usage = "decode-proto[.<testpoint_symbol>] (-|<hex string>)",
3152 .description = "Decode a packet as attribute value pairs from a binary value using a specified protocol decoder. Protocol must be loaded with \"load <protocol>\" first",
3153 }},
3154 { L("dictionary "), &(command_entry_t){
3156 .usage = "dictionary <string>",
3157 .description = "Parse dictionary attribute definition, writing \"ok\" to the data buffer if successful",
3158 }},
3159 { L("dictionary-dump"), &(command_entry_t){
3161 .usage = "dictionary-dump",
3162 .description = "Print the contents of the currently active dictionary to stdout",
3163 }},
3164 { L("encode-dns-label "), &(command_entry_t){
3166 .usage = "encode-dns-label (-|string[,string])",
3167 .description = "Encode one or more DNS labels, writing a hex string to the data buffer.",
3168 }},
3169 { L("encode-pair"), &(command_entry_t){
3170 .func = command_encode_pair,
3171 .usage = "encode-pair[.<testpoint_symbol>] [truncate] (-|<attribute> = <value>[,<attribute = <value>])",
3172 .description = "Encode one or more attribute value pairs, writing a hex string to the data buffer. Protocol must be loaded with \"load <protocol>\" first",
3173 }},
3174 { L("encode-proto"), &(command_entry_t){
3175 .func = command_encode_proto,
3176 .usage = "encode-proto[.<testpoint_symbol>] (-|<attribute> = <value>[,<attribute = <value>])",
3177 .description = "Encode one or more attributes as a packet, writing a hex string to the data buffer. Protocol must be loaded with \"proto <protocol>\" first"
3178 }},
3179 { L("eof"), &(command_entry_t){
3180 .func = command_eof,
3181 .usage = "eof",
3182 .description = "Mark the end of a 'virtual' file. Used to prevent 'need-feature' skipping all the content of a command stream or file",
3183 }},
3184 { L("exit"), &(command_entry_t){
3185 .func = command_exit,
3186 .usage = "exit[ <num>]",
3187 .description = "Exit with the specified error number. If no <num> is provided, process will exit with 0"
3188 }},
3189 { L("fuzzer-out"), &(command_entry_t){
3190 .func = command_fuzzer_out,
3191 .usage = "fuzzer-out <dir>",
3192 .description = "Write encode-pair, encode-proto, and encode-dns-label output, and value input as separate files in the specified directory. Text input will be sha1 hashed and base64 encoded to create the filename",
3193 }},
3194 { L("load-dictionary "),&(command_entry_t){
3196 .usage = "load-dictionary <name> [<dir>]",
3197 .description = "Load an additional dictionary from the same directory as the input file. "
3198 "Optionally you can specify a full path via <dir>. ",
3199 }},
3200 { L("match"), &(command_entry_t){
3201 .func = command_match,
3202 .usage = "match <string>",
3203 .description = "Compare the contents of the data buffer with an expected value"
3204 }},
3205 { L("match-regex "), &(command_entry_t){
3206 .func = command_match_regex,
3207 .usage = "match-regex <regex>",
3208 .description = "Compare the contents of the data buffer with a regular expression"
3209 }},
3210 { L("max-buffer-size"), &(command_entry_t){
3212 .usage = "max-buffer-size[ <integer>]",
3213 .description = "Limit the maximum temporary buffer space available for any command which uses it"
3214 }},
3215 { L("migrate "), &(command_entry_t){
3216 .func = command_migrate,
3217 .usage = "migrate <flag>=<value>",
3218 .description = "Set migration flag"
3219 }},
3220 { L("need-feature "), &(command_entry_t){
3221 .func = command_need_feature,
3222 .usage = "need-feature <feature>",
3223 .description = "Skip the contents of the current file, or up to the next \"eof\" command if a particular feature is not available"
3224 }},
3225 { L("no "), &(command_entry_t){
3226 .func = command_no,
3227 .usage = "no ...",
3228 .description = "Negate the result of a command returning 'ok'"
3229 }},
3230 { L("pair "), &(command_entry_t){
3231 .func = command_pair,
3232 .usage = "pair ... data ...",
3233 .description = "Parse a list of pairs",
3234 }},
3235 { L("proto "), &(command_entry_t){
3236 .func = command_proto,
3237 .usage = "proto <protocol>",
3238 .description = "Switch the active protocol to the one specified, unloading the previous protocol",
3239 }},
3240 { L("proto-dictionary "),&(command_entry_t){
3242 .usage = "proto-dictionary <proto_name> [<proto_dir>]",
3243 .description = "Switch the active dictionary. Root is set to the default dictionary path, or the one specified with -d. <proto_dir> is relative to the root.",
3244 }},
3245
3246
3247 { L("proto-dictionary-root "), &(command_entry_t){
3249 .usage = "proto-dictionary-root[ <root_attribute>]",
3250 .description = "Set the root attribute for the current protocol dictionary. "
3251 "If no attribute name is provided, the root will be reset to the root of the current dictionary",
3252 }},
3253 { L("raw "), &(command_entry_t){
3254 .func = command_encode_raw,
3255 .usage = "raw <string>",
3256 .description = "Create nested attributes from OID strings and values"
3257 }},
3258 { L("read_file "), &(command_entry_t){
3259 .func = command_read_file,
3260 .usage = "read_file <filename>",
3261 .description = "Read a list of pairs from a file",
3262 }},
3263 { L("returned"), &(command_entry_t){
3264 .func = command_returned,
3265 .usage = "returned",
3266 .description = "Print the returned value to the data buffer"
3267 }},
3268
3269 { L("tmpl-rules "), &(command_entry_t){
3270 .func = command_tmpl_rules,
3271 .usage = "tmpl-rule [allow_foreign=yes] [allow_unknown=yes|no] [allow_unresolved=yes|no] [attr_parent=<oid>] [list_def=request|reply|control|session-state] [request_def=current|outer|parent]",
3272 .description = "Alter the tmpl parsing rules for subsequent tmpl parsing commands in the same command context"
3273 }},
3274 { L("touch "), &(command_entry_t){
3275 .func = command_touch,
3276 .usage = "touch <file>",
3277 .description = "Touch a file, updating its created timestamp. Useful for marking the completion of a series of tests"
3278 }},
3279 { L("value "), &(command_entry_t){
3281 .usage = "value <type> <string>",
3282 .description = "Parse a value of a given type from its presentation form, print it, then parse it again (checking printed/parsed versions match), writing printed form to the data buffer"
3283 }},
3284 { L("write "), &(command_entry_t){
3285 .func = command_write,
3286 .usage = "write <file>",
3287 .description = "Write the contents of the data buffer (as a raw binary string) to the specified file"
3288 }},
3289 { L("xlat "), &(command_entry_t){
3290 .func = command_xlat_normalise,
3291 .usage = "xlat <string>",
3292 .description = "Parse then print an xlat expansion, writing the normalised xlat expansion to the data buffer"
3293 }},
3294
3295 { L("xlat_argv "), &(command_entry_t){
3296 .func = command_xlat_argv,
3297 .usage = "xlat_argv <string>",
3298 .description = "Parse then print an xlat expansion argv, writing the normalised xlat expansion arguments to the data buffer"
3299 }},
3300
3301 { L("xlat_expr "), &(command_entry_t){
3302 .func = command_xlat_expr,
3303 .usage = "xlat_expr <string>",
3304 .description = "Parse then print an xlat expression, writing the normalised xlat expansion to the data buffer"
3305 }},
3306
3307 { L("xlat_purify "), &(command_entry_t){
3308 .func = command_xlat_purify,
3309 .usage = "xlat_purify <string>",
3310 .description = "Parse, purify, then print an xlat expression, writing the normalised xlat expansion to the data buffer"
3311 }},
3312
3313};
3315
3316size_t process_line(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used,
3317 char *in, UNUSED size_t inlen)
3318{
3319
3320 command_entry_t *command;
3321 size_t match_len;
3322 char *p;
3323
3324 p = in;
3326
3327 /*
3328 * Skip empty lines and comments.
3329 */
3330 if (!*p || (*p == '#')) {
3331 /*
3332 * Dump the input to the output.
3333 */
3334 if (write_fp) {
3335 fputs(in, write_fp);
3336 fputs("\n", write_fp);
3337 }
3338
3339 RETURN_NOOP(data_used);
3340 }
3341
3342 DEBUG2("%s[%d]: %s", cc->filename, cc->lineno, p);
3343
3344 /*
3345 * Look up the command by longest prefix
3346 */
3347 command = fr_table_value_by_longest_prefix(&match_len, commands, p, -1, NULL);
3348 if (!command) {
3349 fr_strerror_printf("Unknown command: %s", p);
3351 }
3352
3353 p += match_len; /* Jump to after the command */
3354 fr_skip_whitespace(p); /* Skip any whitespace */
3355
3356 /*
3357 * Feed the data buffer in as the command
3358 */
3359 if ((p[0] == '-') && ((p[1] == ' ') || (p[1] == '\0'))) {
3360 data_used = command->func(result, cc, data, data_used, data, data_used);
3361 }
3362 else {
3363 data_used = command->func(result, cc, data, data_used, p, strlen(p));
3364 }
3365
3366 /*
3367 * Dump the contents of the error stack
3368 * to the data buffer.
3369 *
3370 * This is then what's checked in
3371 * subsequent match commands.
3372 */
3373 if (result->error_to_data) data_used = strerror_concat(data, COMMAND_OUTPUT_MAX);
3374
3375 fr_assert((size_t)data_used < COMMAND_OUTPUT_MAX);
3376 data[data_used] = '\0'; /* Ensure the data buffer is \0 terminated */
3377
3378 if (data_used) {
3379 DEBUG2("%s[%d]: --> %s (%zu bytes in buffer)", cc->filename, cc->lineno,
3380 fr_table_str_by_value(command_rcode_table, result->rcode, "<INVALID>"), data_used);
3381 } else {
3382 DEBUG2("%s[%d]: --> %s", cc->filename, cc->lineno,
3383 fr_table_str_by_value(command_rcode_table, result->rcode, "<INVALID>"));
3384 }
3385
3386 /*
3387 * Dump the input to the output.
3388 */
3389 if (write_fp) {
3390 fputs(in, write_fp);
3391 fputs("\n", write_fp);
3392 };
3393
3394 talloc_free_children(cc->tmp_ctx);
3395
3396 return data_used;
3397}
3398
3400{
3401 if (fr_dict_free(&cc->test_internal_dict, __FILE__) < 0) {
3402 fr_perror("unit_test_attribute");
3403 return -1;
3404 }
3405 if (fr_dict_global_ctx_free(cc->test_gctx) < 0) {
3406 fr_perror("unit_test_attribute");
3407 return -1;
3408 }
3409 if (cc->fuzzer_dir >= 0) {
3410 close(cc->fuzzer_dir);
3411 cc->fuzzer_dir = -1;
3412 }
3413 return 0;
3414}
3415
3417 command_config_t const *config, char const *path, char const *filename)
3418{
3420
3421 cc = talloc_zero(ctx, command_file_ctx_t);
3422 talloc_set_destructor(cc, _command_ctx_free);
3423
3424 cc->tmp_ctx = talloc_named_const(ctx, 0, "tmp_ctx");
3425 cc->path = talloc_strdup(cc, path);
3426 cc->filename = filename;
3427 cc->config = config;
3428
3429 /*
3430 * Allocate a special buffer with poisoned regions
3431 * at either end.
3432 */
3434 talloc_free(cc);
3435 return NULL;
3436 }
3439
3440 /*
3441 * Initialise a special temporary dictionary context
3442 *
3443 * Any protocol dictionaries loaded by "test-dictionary"
3444 * go in this context, and don't affect the main
3445 * dictionary context.
3446 */
3447 cc->test_gctx = fr_dict_global_ctx_init(cc, false, cc->config->dict_dir);
3448 if (!cc->test_gctx) {
3449 fr_perror("Failed allocating test dict_gctx");
3450 return NULL;
3451 }
3452
3455 fr_perror("Failed loading test dict_gctx internal dictionary");
3456 return NULL;
3457 }
3458
3459 fr_dict_global_ctx_dir_set(cc->path); /* Load new dictionaries relative to the test file */
3461
3462 cc->fuzzer_dir = -1;
3463
3465 cc->tmpl_rules.attr.namespace = fr_dict_root(cc->config->dict);
3466 cc->tmpl_rules.attr.allow_unresolved = false; /* tests have to use real attributes */
3467
3468 return cc;
3469}
3470
3471static void command_ctx_reset(command_file_ctx_t *cc, TALLOC_CTX *ctx)
3472{
3473 talloc_free(cc->tmp_ctx);
3474 cc->tmp_ctx = talloc_named_const(ctx, 0, "tmp_ctx");
3475 cc->test_count = 0;
3476
3477 if (fr_dict_free(&cc->test_internal_dict, __FILE__) < 0) {
3478 fr_perror("unit_test_attribute");
3479 }
3480
3481 if (fr_dict_global_ctx_free(cc->test_gctx) < 0) fr_perror("unit_test_attribute");
3482
3483 cc->test_gctx = fr_dict_global_ctx_init(cc, false, cc->config->dict_dir);
3485 fr_perror("Failed loading test dict_gctx internal dictionary");
3486 }
3487
3488 if (cc->fuzzer_dir >= 0) {
3489 close(cc->fuzzer_dir);
3490 cc->fuzzer_dir = -1;
3491 }
3492}
3493
3494static int process_file(bool *exit_now, TALLOC_CTX *ctx, command_config_t const *config,
3495 const char *root_dir, char const *filename, fr_dlist_head_t *lines)
3496{
3497 int ret = 0;
3498 FILE *fp; /* File we're reading from */
3499 char buffer[8192]; /* Command buffer */
3500 char data[COMMAND_OUTPUT_MAX + 1]; /* Data written by previous command */
3501 ssize_t data_used = 0; /* How much data the last command wrote */
3502 static char path[PATH_MAX] = "";
3503 command_line_range_t *lr = NULL;
3504 bool opened_fp = false;
3505
3507
3508 cc = command_ctx_alloc(ctx, config, root_dir, filename);
3509
3510 /*
3511 * Open the file, or stdin
3512 */
3513 if (strcmp(filename, "-") == 0) {
3514 fp = stdin;
3515 filename = "<stdin>";
3516 } else {
3517 if (root_dir && *root_dir) {
3518 snprintf(path, sizeof(path), "%s/%s", root_dir, filename);
3519 } else {
3520 strlcpy(path, filename, sizeof(path));
3521 }
3522
3523 fp = fopen(path, "r");
3524 if (!fp) {
3525 ERROR("Error opening test file \"%s\": %s", path, fr_syserror(errno));
3526 ret = -1;
3527 goto finish;
3528 }
3529
3530 filename = path;
3531 opened_fp = true;
3532 }
3533
3534 if (lines && !fr_dlist_empty(lines)) lr = fr_dlist_head(lines);
3535
3536 /*
3537 * Loop over lines in the file or stdin
3538 */
3539 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
3540 command_result_t result = { .rcode = RESULT_OK }; /* Reset to OK */
3541 char *p = strchr(buffer, '\n');
3542
3544 cc->lineno++; /* The first line of the file becomes line 1 */
3545
3546 if (lr) {
3547 if (cc->lineno > lr->end) {
3548 lr = fr_dlist_next(lines, lr);
3549 if (!lr) goto finish;
3550 }
3551
3552 if (cc->lineno < lr->start) continue;
3553 }
3554
3555 if (!p) {
3556 if (!feof(fp)) {
3557 ERROR("Line %d too long in %s/%s", cc->lineno, cc->path, cc->filename);
3558 ret = -1;
3559 goto finish;
3560 }
3561 } else {
3562 *p = '\0';
3563 }
3564
3565 data_used = process_line(&result, cc, data, data_used, buffer, strlen(buffer));
3566 switch (result.rcode) {
3567 /*
3568 * Command completed successfully
3569 */
3570 case RESULT_OK:
3571 cc->test_count++;
3572 continue;
3573
3574 /*
3575 * Did nothing (not a test)
3576 */
3577 case RESULT_NOOP:
3578 continue;
3579
3580 /*
3581 * If this is a file, then break out of the loop
3582 * and cleanup, otherwise we need to find the
3583 * EOF marker in the input stream.
3584 */
3585 case RESULT_SKIP_FILE:
3586 if (fp != stdin) goto finish;
3587
3588 /*
3589 * Skip over the input stream until we
3590 * find an eof command, or the stream
3591 * is closed.
3592 */
3593 while (fgets(buffer, sizeof(buffer), fp) != NULL) {
3594 command_entry_t *command;
3595 size_t match_len;
3596
3597 command = fr_table_value_by_longest_prefix(&match_len, commands, buffer, -1, NULL);
3598 if (!command) {
3599 ERROR("%s[%d]: Unknown command: %s", cc->path, cc->lineno, p);
3600 ret = -1;
3601 goto finish;
3602 }
3603
3604 if (command->func == command_eof) {
3605 command_ctx_reset(cc, ctx);
3606 break;
3607 }
3608 }
3609 goto finish;
3610
3611 /*
3612 * Fatal error parsing a command
3613 */
3614 case RESULT_PARSE_ERROR:
3616 fr_perror("%s[%d]", filename, cc->lineno);
3617 ret = -1;
3618 goto finish;
3619
3620 /*
3621 * Result didn't match what we expected
3622 */
3623 case RESULT_MISMATCH:
3624 {
3625 ret = EXIT_FAILURE;
3626 goto finish;
3627 }
3628
3629 case RESULT_EXIT:
3630 ret = result.ret;
3631 *exit_now = true;
3632 goto finish;
3633
3634 default:
3635 /*
3636 * If this happens, fix the damn command.
3637 */
3638 fr_assert_msg(false, "Command exited with invalid return code (%i)", result.rcode);
3639 ret = -1;
3640 goto finish;
3641 }
3642 }
3643
3644finish:
3645 /* The explicit check is to quiet clang_analyzer */
3646 if (opened_fp) fclose(fp);
3647
3648 /*
3649 * Free any residual resources we loaded.
3650 */
3651 if (cc && (fr_dict_const_free(&cc->tmpl_rules.attr.dict_def, __FILE__) < 0)) {
3652 fr_perror("unit_test_attribute");
3653 ret = -1;
3654 }
3655
3656 fr_dict_global_ctx_set(config->dict_gctx); /* Switch back to the main dict ctx */
3658 talloc_free(cc);
3659
3660 return ret;
3661}
3662
3663static void usage(char const *name)
3664{
3665 INFO("usage: %s [options] (-|<filename>[:<lines>] [ <filename>[:<lines>]])", name);
3666 INFO("options:");
3667 INFO(" -d <raddb> Set user dictionary path (defaults to " RADDBDIR ").");
3668 INFO(" -D <dictdir> Set main dictionary path (defaults to " DICTDIR ").");
3669 INFO(" -x Debugging mode.");
3670 INFO(" -f Print features.");
3671 INFO(" -c Print commands.");
3672 INFO(" -h Print help text.");
3673 INFO(" -M Show talloc memory report.");
3674 INFO(" -p Allow xlat_purify");
3675 INFO(" -r <receipt_file> Create the <receipt_file> as a 'success' exit.");
3676 INFO(" -w <output_file> Write 'corrected' output to <output_file>.");
3677 INFO("Where <filename> is a file containing one or more commands and '-' indicates commands should be read from stdin.");
3678 INFO("Ranges of <lines> may be specified in the format <start>[-[<end>]][,]");
3679}
3680
3681static void features_print(CONF_SECTION *features)
3682{
3683 CONF_PAIR *cp;
3684
3685 INFO("features:");
3686 for (cp = cf_pair_find(features, CF_IDENT_ANY);
3687 cp;
3688 cp = cf_pair_find_next(features, cp, CF_IDENT_ANY)) {
3689 INFO(" %s %s", cf_pair_attr(cp), cf_pair_value(cp));
3690 }
3691}
3692
3693static void commands_print(void)
3694{
3695 size_t i;
3696
3697 INFO("commands:");
3698 for (i = 0; i < commands_len; i++) {
3699 INFO(" %s:", ((command_entry_t const *)commands[i].value)->usage);
3700 INFO(" %s.", ((command_entry_t const *)commands[i].value)->description);
3701 INFO("%s", "");
3702 }
3703}
3704
3705static int line_ranges_parse(TALLOC_CTX *ctx, fr_dlist_head_t *out, fr_sbuff_t *in)
3706{
3707 static bool tokens[UINT8_MAX + 1] = { [','] = true , ['-'] = true };
3708 uint32_t max = 0;
3711
3712 while (fr_sbuff_extend(in)) {
3713 fr_sbuff_adv_past_whitespace(in, SIZE_MAX, NULL);
3714
3715 MEM(lr = talloc_zero(ctx, command_line_range_t));
3717
3718 fr_sbuff_out(&err, &lr->start, in);
3719 if (err != FR_SBUFF_PARSE_OK) {
3720 ERROR("Invalid line start number");
3721 error:
3723 return -1;
3724 }
3725 if (max > lr->start) {
3726 ERROR("Out of order line numbers (%u > %u) not allowed", max, lr->start);
3727 goto error;
3728 } else {
3729 max = lr->start;
3730 }
3731 lr->end = lr->start; /* Default to a single line */
3732 fr_sbuff_adv_past_whitespace(in, SIZE_MAX, NULL);
3733
3734 again:
3735 if (!fr_sbuff_extend(in)) break;
3736 if (!fr_sbuff_is_in_charset(in, tokens)) {
3737 ERROR("Unexpected text \"%pV\"",
3739 goto error;
3740 }
3741
3742 fr_sbuff_switch(in, '\0') {
3743 /*
3744 * More ranges...
3745 */
3746 case ',':
3747 fr_sbuff_next(in);
3748 fr_sbuff_adv_past_whitespace(in, SIZE_MAX, NULL);
3749 continue;
3750
3751 /*
3752 * <start>-<end>
3753 */
3754 case '-':
3755 {
3756 fr_sbuff_next(in);
3757 fr_sbuff_adv_past_whitespace(in, SIZE_MAX, NULL);
3758
3759 /*
3760 * A bare '-' with no number means
3761 * run all remaining lines.
3762 */
3763 if (fr_sbuff_extend(in) == 0) {
3764 lr->end = UINT32_MAX;
3765 return 0;
3766 }
3767
3768 fr_sbuff_out(&err, &lr->end, in);
3769 if (err != FR_SBUFF_PARSE_OK) {
3770 ERROR("Invalid line end number");
3771 goto error;
3772 }
3773 if (lr->end < lr->start) {
3774 ERROR("Line end must be >= line start (%u < %u)", lr->end, lr->start);
3775 goto error;
3776 }
3777 if (max > lr->end) {
3778 ERROR("Out of order line numbers (%u > %u) not allowed", max, lr->end);
3779 goto error;
3780 } else {
3781 max = lr->end;
3782 }
3783 fr_sbuff_adv_past_whitespace(in, SIZE_MAX, NULL);
3784 }
3785 goto again;
3786 }
3787 }
3788
3789 return 0;
3790}
3791
3792/**
3793 *
3794 * @hidecallgraph
3795 */
3796int main(int argc, char *argv[])
3797{
3798 int c;
3799 char const *receipt_file = NULL;
3800 CONF_SECTION *cs;
3801 int ret = EXIT_SUCCESS;
3802 TALLOC_CTX *autofree;
3803 TALLOC_CTX *thread_ctx;
3804 bool exit_now = false;
3805
3807 .raddb_dir = RADDBDIR,
3808 .dict_dir = DICTDIR
3809 };
3810
3811 char const *name;
3812 bool do_features = false;
3813 bool do_commands = false;
3814 bool do_usage = false;
3815 bool allow_purify = false;
3816 xlat_t *xlat;
3817
3818 /*
3819 * Must be called first, so the handler is called last
3820 */
3822
3824 thread_ctx = talloc_new(autofree);
3825
3826#ifndef NDEBUG
3827 if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0]) < 0) {
3828 fr_perror("unit_test_attribute");
3829 goto cleanup;
3830 }
3831#else
3833#endif
3834
3835 /*
3836 * Sync wallclock and cpu time so that we can find
3837 * uses of fr_time_[to|from]_* where
3838 * fr_unix_time_[to|from]_* should be used.
3839 *
3840 * If the wallclock/cpu offset is 0, then both sets
3841 * of macros produce the same result.
3842 */
3843 fr_time_start();
3844
3845 /*
3846 * Allocate a root config section so we can write
3847 * out features and versions.
3848 */
3849 MEM(cs = cf_section_alloc(autofree, NULL, "unit_test_attribute", NULL));
3850 MEM(config.features = cf_section_alloc(cs, cs, "feature", NULL));
3851 dependency_features_init(config.features); /* Add build time features to the config section */
3852
3853 name = argv[0];
3854
3856 default_log.fd = STDOUT_FILENO;
3857 default_log.print_level = false;
3858
3859 while ((c = getopt(argc, argv, "cd:D:F:fxMhpr:S:w:")) != -1) switch (c) {
3860 case 'c':
3861 do_commands = true;
3862 break;
3863
3864 case 'd':
3865 config.raddb_dir = optarg;
3866 break;
3867
3868 case 'D':
3869 config.dict_dir = optarg;
3870 break;
3871
3872 case 'F':
3873 config.fuzzer_dir = optarg;
3874 break;
3875
3876 case 'f':
3877 do_features = true;
3878 break;
3879
3880 case 'x':
3881 fr_debug_lvl++;
3882 if (fr_debug_lvl > 2) default_log.print_level = true;
3883 break;
3884
3885 case 'M':
3886 talloc_enable_leak_report();
3887 break;
3888
3889 case 'r':
3890 receipt_file = optarg;
3891 break;
3892
3893 case 'p':
3894 allow_purify = true;
3895 break;
3896
3897 case 'S':
3898 fprintf(stderr, "Invalid option to -S\n");
3900
3901 case 'w':
3902 write_filename = optarg;
3903 break;
3904
3905 case 'h':
3906 default:
3907 do_usage = true; /* Just set a flag, so we can process extra -x args */
3908 break;
3909 }
3910 argc -= (optind - 1);
3911 argv += (optind - 1);
3912
3913 if (do_usage) usage(name);
3914 if (do_features) features_print(config.features);
3915 if (do_commands) commands_print();
3916 if (do_usage || do_features || do_commands) {
3917 ret = EXIT_SUCCESS;
3918 goto cleanup;
3919 }
3920
3921 if (receipt_file && (fr_unlink(receipt_file) < 0)) {
3922 fr_perror("unit_test_attribute");
3924 }
3925
3926 /*
3927 * Mismatch between the binary and the libraries it depends on
3928 */
3930 fr_perror("unit_test_attribute");
3932 }
3933
3934#ifdef WITH_TLS
3935 /*
3936 * OpenSSL can only be initialised once during the lifetime
3937 * of a process. Initialise it here so that we don't attempt
3938 * to unload and load it multiple times.
3939 */
3940 if (fr_openssl_init() < 0) {
3941 fr_perror("unit_test_attribute");
3943 }
3944#endif
3945
3946 modules_init(NULL);
3947
3948 dl_loader = dl_loader_init(autofree, NULL, false, false);
3949 if (!dl_loader) {
3950 fr_perror("unit_test_attribute");
3952 }
3953
3954 config.dict_gctx = fr_dict_global_ctx_init(NULL, true, config.dict_dir);
3955 if (!config.dict_gctx) {
3956 fr_perror("unit_test_attribute");
3958 }
3959
3961 fr_perror("unit_test_attribute");
3963 }
3964
3965 /*
3966 * Always needed so we can load the list attributes
3967 * otherwise the tmpl_tokenize code fails.
3968 */
3969 if (request_global_init() < 0) {
3970 fr_perror("unit_test_attribute");
3972 }
3973
3974 /*
3975 * Initialise the interpreter, registering operations.
3976 * Needed because some keywords also register xlats.
3977 */
3978 if (unlang_global_init() < 0) {
3979 fr_perror("unit_test_attribute");
3981 }
3982
3983 /*
3984 * Create a dummy event list
3985 */
3986 if (allow_purify) {
3987 el = fr_event_list_alloc(autofree, NULL, NULL);
3988 fr_assert(el != NULL);
3989
3990 /*
3991 * Simulate thread specific instantiation
3992 */
3994 if (xlat_thread_instantiate(thread_ctx, el) < 0) EXIT_WITH_FAILURE;
3995 }
3996
3997 unlang_thread_instantiate(thread_ctx);
3998
3999 xlat = xlat_func_register(NULL, "test", xlat_test, FR_TYPE_NULL);
4000 if (!xlat) {
4001 ERROR("Failed registering xlat");
4003 }
4005
4006 /*
4007 * And again WITHOUT arguments.
4008 */
4009 xlat = xlat_func_register(NULL, "test_no_args", xlat_test, FR_TYPE_NULL);
4010 if (!xlat) {
4011 ERROR("Failed registering xlat");
4013 }
4015
4016 /*
4017 * Disable hostname lookups, so we don't produce spurious DNS
4018 * queries, and there's no chance of spurious failures if
4019 * it takes a long time to get a response.
4020 */
4022
4023 /*
4024 * Read tests from stdin
4025 */
4026 if (argc < 2) {
4027 if (write_filename) {
4028 ERROR("Can't use '-w' with stdin");
4030 }
4031
4032 ret = process_file(&exit_now, autofree, &config, name, "-", NULL);
4033
4034 /*
4035 * ...or process each file in turn.
4036 */
4037 } else {
4038 int i;
4039
4040 if (write_filename) {
4041 if (argc != 2) { /* program name and file to write */
4042 ERROR("Can't use '-w' with multiple filenames");
4044 }
4045
4046 write_fp = fopen(write_filename, "w");
4047 if (!write_fp) {
4048 ERROR("Failed opening %s: %s", write_filename, strerror(errno));
4050 }
4051 }
4052
4053 /*
4054 * Loop over all input files.
4055 */
4056 for (i = 1; i < argc; i++) {
4057 char *dir = NULL, *file;
4058 fr_sbuff_t in = FR_SBUFF_IN(argv[i], strlen(argv[i]));
4060 L("/"),
4061 L(":")
4062 );
4063 fr_sbuff_marker_t file_start, file_end, dir_end;
4064 fr_dlist_head_t lines;
4065
4066 fr_sbuff_marker(&file_start, &in);
4067 fr_sbuff_marker(&file_end, &in);
4068 fr_sbuff_marker(&dir_end, &in);
4069 fr_sbuff_set(&file_end, fr_sbuff_end(&in));
4070
4071 fr_dlist_init(&lines, command_line_range_t, entry);
4072
4073 while (fr_sbuff_extend(&in)) {
4074 fr_sbuff_adv_until(&in, SIZE_MAX, &dir_sep, '\0');
4075
4076 fr_sbuff_switch(&in, '\0') {
4077 case '/':
4078 fr_sbuff_set(&dir_end, &in);
4079 fr_sbuff_advance(&in, 1);
4080 fr_sbuff_set(&file_start, &in);
4081 break;
4082
4083 case ':':
4084 fr_sbuff_set(&file_end, &in);
4085 fr_sbuff_advance(&in, 1);
4086 if (line_ranges_parse(autofree, &lines, &in) < 0) EXIT_WITH_FAILURE;
4087 break;
4088
4089 default:
4090 fr_sbuff_set(&file_end, &in);
4091 break;
4092 }
4093 }
4094
4096 fr_sbuff_current(&file_start), fr_sbuff_diff(&file_end, &file_start));
4097 if (fr_sbuff_used(&dir_end)) dir = talloc_bstrndup(autofree,
4099 fr_sbuff_used(&dir_end));
4100
4101 ret = process_file(&exit_now, autofree, &config, dir, file, &lines);
4102 talloc_free(dir);
4104 fr_dlist_talloc_free(&lines);
4105
4106 if ((ret != 0) || exit_now) break;
4107 }
4108
4109 if (write_fp) {
4110 fclose(write_fp);
4111 if (rename(write_filename, argv[1]) < 0) {
4112 ERROR("Failed renaming %s: %s", write_filename, strerror(errno));
4114 }
4115 }
4116 }
4117
4118 /*
4119 * Try really hard to free any allocated
4120 * memory, so we get clean talloc reports.
4121 */
4122cleanup:
4123 /*
4124 * Ensure all thread local memory is cleaned up
4125 * at the appropriate time. This emulates what's
4126 * done with worker/network threads in the
4127 * scheduler.
4128 */
4130
4131#ifdef WITH_TLS
4132 fr_openssl_free();
4133#endif
4134
4135 /*
4136 * dl_loader check needed as talloc_free
4137 * returns -1 on failure.
4138 */
4139 if (dl_loader && (talloc_free(dl_loader) < 0)) {
4140 fr_perror("unit_test_attribute - dl_loader - "); /* Print free order issues */
4142 }
4143 if (fr_dict_free(&config.dict, __FILE__) < 0) {
4144 fr_perror("unit_test_attribute");
4146 }
4147
4148 if (receipt_file && (ret == EXIT_SUCCESS) && (fr_touch(NULL, receipt_file, 0644, true, 0755) <= 0)) {
4149 fr_perror("unit_test_attribute");
4151 }
4152
4153 /*
4154 * Explicitly free the autofree context
4155 * to make errors less confusing.
4156 */
4157 if (talloc_free(autofree) < 0) {
4158 fr_perror("unit_test_attribute");
4160 }
4161
4162 /*
4163 * Ensure our atexit handlers run before any other
4164 * atexit handlers registered by third party libraries.
4165 */
4167
4168 return ret;
4169}
static int const char char buffer[256]
Definition acutest.h:576
int const char * file
Definition acutest.h:702
strcpy(log_entry->msg, buffer)
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition atexit.c:160
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition atexit.c:286
#define fr_atexit_thread_trigger_all(...)
Definition atexit.h:233
ssize_t fr_base64_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, bool add_padding, char const alphabet[static UINT8_MAX])
Base 64 encode binary data.
Definition base64.c:326
char const fr_base64_url_alphabet_encode[UINT8_MAX]
Definition base64.c:173
#define CMD_MAX_ARGV
Definition radmin.c:150
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
int fr_value_calc_nary_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t type, fr_token_t op, fr_value_box_t const *group)
Calculate DST = OP { A, B, C, ... }.
Definition calc.c:2269
int fr_value_calc_assignment_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_token_t op, fr_value_box_t const *src)
Calculate DST OP SRC.
Definition calc.c:2420
int fr_value_calc_binary_op(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t hint, fr_value_box_t const *a, fr_token_t op, fr_value_box_t const *b)
Calculate DST = A OP B.
Definition calc.c:1924
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
CONF_PAIR * cf_pair_find_next(CONF_SECTION const *cs, CONF_PAIR const *prev, char const *attr)
Find a pair with a name matching attr, after specified pair.
Definition cf_util.c:1452
CONF_PAIR * cf_pair_find(CONF_SECTION const *cs, char const *attr)
Search for a CONF_PAIR with a specific name.
Definition cf_util.c:1438
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1593
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1577
#define cf_lineno_set(_ci, _lineno)
Definition cf_util.h:131
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:140
#define cf_filename_set(_ci, _filename)
Definition cf_util.h:128
#define CF_IDENT_ANY
Definition cf_util.h:78
int fr_command_walk(fr_cmd_t *head, void **walk_ctx, void *ctx, fr_cmd_walk_t callback)
Walk over a command hierarchy.
Definition command.c:1027
void fr_command_debug(FILE *fp, fr_cmd_t *head)
Definition command.c:1610
int fr_command_add(TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, void *ctx, fr_cmd_table_t const *table)
Add one command to the global command tree.
Definition command.c:733
int fr_command_tab_expand(TALLOC_CTX *ctx, fr_cmd_t *head, fr_cmd_info_t *info, int max_expansions, char const **expansions)
Get the commands && help at a particular level.
Definition command.c:1298
char const ** parents
Definition command.h:66
char const * help
help text
Definition command.h:55
int argc
current argument count
Definition command.h:39
fr_cmd_func_t func
function to process this command
Definition command.h:56
char const * syntax
Definition command.h:68
char const * syntax
e.g. "STRING"
Definition command.h:54
fr_value_box_t ** box
value_box version of commands.
Definition command.h:43
bool read_only
Definition command.h:58
char const * parent
e.g. "show module"
Definition command.h:52
fr_cmd_tab_t tab_expand
tab expand things in the syntax string
Definition command.h:57
int max_argc
maximum number of arguments
Definition command.h:40
char const * name
e.g. "stats"
Definition command.h:53
char const ** argv
text version of commands
Definition command.h:42
char const * name
Definition command.h:67
#define FR_DBUFF_TMP(_start, _len_or_end)
Creates a compound literal to pass into functions which accept a dbuff.
Definition dbuff.h:514
static void * fr_dcursor_current(fr_dcursor_t *cursor)
Return the item the cursor current points to.
Definition dcursor.h:337
void fr_disable_null_tracking_on_free(TALLOC_CTX *ctx)
Disable the null tracking context when a talloc chunk is freed.
Definition debug.c:1207
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
Definition debug.c:1242
#define fr_assert_msg(_x, _msg,...)
Calls panic_action ifndef NDEBUG, else logs error and causes the server to exit immediately with code...
Definition debug.h:210
#define MEM(x)
Definition debug.h:36
void dependency_features_init(CONF_SECTION *cs)
Initialise core feature flags.
Definition dependency.c:183
static char const * spaces
Definition dependency.c:370
#define ERROR(fmt,...)
Definition dhcpclient.c:41
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
static NEVER_RETURNS void usage(void)
Definition dhcpclient.c:114
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir)
Initialise the global protocol hashes.
Definition dict_util.c:4394
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name, char const *dependent)
(Re-)Initialize the special internal dictionary
int fr_dict_global_ctx_dir_set(char const *dict_dir)
Allow the default dict dir to be changed after initialisation.
Definition dict_util.c:4485
static fr_slen_t err
Definition dict.h:833
fr_dict_attr_t const * fr_dict_attr_by_name(fr_dict_attr_err_t *err, fr_dict_attr_t const *parent, char const *attr))
Locate a fr_dict_attr_t by its name.
Definition dict_util.c:3265
void fr_dict_debug(FILE *fp, fr_dict_t const *dict)
Definition dict_print.c:266
fr_slen_t fr_dict_attr_by_oid_substr(fr_dict_attr_err_t *err, fr_dict_attr_t const **out, fr_dict_attr_t const *parent, fr_sbuff_t *in, fr_sbuff_term_t const *tt))
Resolve an attribute using an OID string.
Definition dict_util.c:2326
int fr_dict_protocol_afrom_file(fr_dict_t **out, char const *proto_name, char const *proto_dir, char const *dependent)
(Re)-initialize a protocol dictionary
int fr_dict_str_to_argv(char *str, char **argv, int max_argc)
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2402
void fr_dict_global_ctx_set(fr_dict_gctx_t const *gctx)
Set a new, active, global dictionary context.
Definition dict_util.c:4455
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4026
int fr_dict_const_free(fr_dict_t const **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4010
fr_dict_t const * fr_dict_internal(void)
Definition dict_util.c:4612
int fr_dict_global_ctx_free(fr_dict_gctx_t const *gctx)
Explicitly free all data associated with a global dictionary context.
Definition dict_util.c:4471
int fr_dict_parse_str(fr_dict_t *dict, char *buf, fr_dict_attr_t const *parent)
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename)
Read supplementary attribute definitions into an existing dictionary.
fr_dict_attr_err_t
Errors returned by attribute lookup functions.
Definition dict.h:294
@ FR_DICT_ATTR_OK
No error.
Definition dict.h:295
static fr_slen_t in
Definition dict.h:833
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:885
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:62
Module handle.
Definition dl.h:58
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:260
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:486
static void fr_dlist_talloc_free(fr_dlist_head_t *head)
Free all items in a doubly linked list (with talloc)
Definition dlist.h:908
static bool fr_dlist_empty(fr_dlist_head_t const *list_head)
Check whether a list has any items.
Definition dlist.h:501
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:378
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:555
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
ssize_t fr_dns_label_from_value_box(size_t *need, uint8_t *buf, size_t buf_len, uint8_t *where, bool compression, fr_value_box_t const *value, fr_dns_labels_t *lb)
Encode a single value box of type string, serializing its contents to a dns label.
Definition dns.c:639
ssize_t fr_dns_label_to_value_box(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, size_t len, uint8_t const *label, bool tainted, fr_dns_labels_t *lb)
Decode a fr_value_box_t from one DNS label.
Definition dns.c:1225
int fr_unlink(char const *filename)
Remove a regular file from the filesystem.
Definition file.c:367
ssize_t fr_touch(int *fd_out, char const *filename, mode_t mode, bool mkdir, mode_t dir_mode)
Create an empty file.
Definition file.c:323
char * fr_realpath(TALLOC_CTX *ctx, char const *path, ssize_t len)
Convenience wrapper around realpath.
Definition file.c:284
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:52
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:51
int unlang_global_init(void)
Definition base.c:133
talloc_free(reap)
fr_event_list_t * fr_event_list_alloc(TALLOC_CTX *ctx, fr_event_status_cb_t status, void *status_uctx)
Initialise a new event list.
Definition event.c:2523
Stores all information relating to an event list.
Definition event.c:377
int fr_debug_lvl
Definition log.c:40
FILE * fr_log_fp
Definition log.c:39
fr_log_t default_log
Definition log.c:288
@ L_DST_STDOUT
Log to stdout.
Definition log.h:78
fr_type_t
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_MAX
Number of defined data types.
@ FR_TYPE_NULL
Invalid (uninitialised) attribute type.
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
@ FR_TYPE_GROUP
A grouping of other attributes.
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
ssize_t fr_slen_t
unsigned long int size_t
#define UINT8_MAX
fr_sbuff_parse_error_t
@ FR_SBUFF_PARSE_OK
No error.
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
static bool is_whitespace(char const *value)
Check whether the string is all whitespace.
Definition misc.h:86
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
Definition misc.h:59
fr_pair_t * fr_pair_afrom_da(TALLOC_CTX *ctx, fr_dict_attr_t const *da)
Dynamically allocate a new attribute and assign a fr_dict_attr_t.
Definition pair.c:285
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
fr_slen_t fr_pair_list_afrom_substr(fr_pair_parse_t const *root, fr_pair_parse_t *relative, fr_sbuff_t *in)
Parse a fr_pair_list_t from a substring.
int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone)
Read valuepairs from the fp up to End-Of-File.
struct fr_pair_parse_s fr_pair_parse_t
TALLOC_CTX * ctx
Definition pair_legacy.h:43
#define is_truncated(_ret, _max)
Definition print.h:48
static const conf_parser_t config[]
Definition base.c:183
void * fr_proto_next_encodable(fr_dlist_head_t *list, void *current, void *uctx)
Implements the default iterator to encode pairs belonging to a specific dictionary that are not inter...
Definition proto.c:100
static fr_internal_encode_ctx_t encode_ctx
#define fr_assert(_expr)
Definition rad_assert.h:38
static TALLOC_CTX * autofree
static bool done
Definition radclient.c:81
#define DEBUG2(fmt,...)
Definition radclient.h:43
#define INFO(fmt,...)
Definition radict.c:54
static bool cleanup
Definition radsniff.c:60
fr_dict_attr_t const * request_attr_request
Definition request.c:43
int request_global_init(void)
Definition request.c:589
static char const * name
fr_slen_t fr_sbuff_out_bool(bool *out, fr_sbuff_t *in)
See if the string contains a truth value.
Definition sbuff.c:1111
size_t fr_sbuff_adv_until(fr_sbuff_t *sbuff, size_t len, fr_sbuff_term_t const *tt, char escape_chr)
Wind position until we hit a character in the terminal set.
Definition sbuff.c:1854
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:2090
#define fr_sbuff_start(_sbuff_or_marker)
#define fr_sbuff_out_by_longest_prefix(_match_len, _out, _table, _sbuff, _def)
#define fr_sbuff_set(_dst, _src)
#define fr_sbuff_diff(_a, _b)
#define FR_SBUFF_IN(_start, _len_or_end)
#define fr_sbuff_adv_past_whitespace(_sbuff, _len, _tt)
#define fr_sbuff_current(_sbuff_or_marker)
#define FR_SBUFF_TERMS(...)
Initialise a terminal structure with a list of sorted strings.
Definition sbuff.h:192
#define fr_sbuff_extend(_sbuff_or_marker)
#define FR_SBUFF_ERROR_RETURN(_sbuff_or_marker)
#define fr_sbuff_end(_sbuff_or_marker)
#define fr_sbuff_advance(_sbuff_or_marker, _len)
#define fr_sbuff_out(_err, _out, _in)
#define fr_sbuff_switch(_sbuff_or_marker, _eob)
#define fr_sbuff_remaining(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define fr_sbuff_used(_sbuff_or_marker)
Set of terminal elements.
tmpl_xlat_rules_t xlat
Rules/data for parsing xlats.
Definition tmpl.h:336
bool new_functions
new function syntax
Definition tmpl.h:326
fr_slen_t tmpl_request_ref_list_afrom_substr(TALLOC_CTX *ctx, tmpl_attr_error_t *err, FR_DLIST_HEAD(tmpl_request_list) _CONST **out, fr_sbuff_t *in)
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:335
struct tmpl_rules_s tmpl_rules_t
Definition tmpl.h:233
fr_slen_t tmpl_attr_list_from_substr(fr_dict_attr_t const **da_p, fr_sbuff_t *in)
Parse one a single list reference.
fr_event_list_t * runtime_el
The eventlist to use for runtime instantiation of xlats.
Definition tmpl.h:324
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:332
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
void modules_init(char const *lib_dir)
Perform global initialisation for modules.
Definition module.c:1904
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
fr_log_dst_t dst
Log destination.
Definition log.h:97
int fd
File descriptor to write messages to.
Definition log.h:112
bool print_level
sometimes we don't want log levels printed
Definition log.h:106
fr_dict_attr_t const * list_def
Default list to use with unqualified attribute reference.
Definition tmpl.h:295
uint8_t allow_unresolved
Allow attributes that look valid but were not found in the dictionaries.
Definition tmpl.h:304
uint8_t allow_foreign
Allow arguments not found in dict_def.
Definition tmpl.h:312
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
uint8_t allow_unknown
Allow unknown attributes i.e.
Definition tmpl.h:301
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_longest_prefix(_match_len, _table, _name, _name_len, _def)
Find the longest string match using a sorted or ordered table.
Definition table.h:732
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
An element in a lexicographically sorted array of name to ptr mappings.
Definition table.h:65
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:560
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:51
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:85
fr_tp_proto_decode_t func
Decoder for proto layer.
Definition test_point.h:68
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:93
fr_dcursor_iter_t next_encodable
Iterator to use to select attributes to encode.
Definition test_point.h:95
fr_tp_proto_encode_t func
Encoder for proto layer.
Definition test_point.h:76
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:75
fr_pair_decode_t func
Decoder for pairs.
Definition test_point.h:86
fr_pair_encode_t func
Encoder for pairs.
Definition test_point.h:94
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:67
Entry point for pair decoders.
Definition test_point.h:84
Entry point for pair encoders.
Definition test_point.h:92
Entry point for protocol decoders.
Definition test_point.h:66
Entry point for protocol encoders.
Definition test_point.h:74
int fr_time_start(void)
Initialize the local time.
Definition time.c:150
const bool fr_assignment_op[T_TOKEN_LAST]
Definition token.c:167
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:32
enum fr_token fr_token_t
@ T_AND
Definition token.h:55
@ T_INVALID
Definition token.h:39
@ T_SUB
Definition token.h:52
@ T_XOR
Definition token.h:58
@ T_DIV
Definition token.h:54
@ T_MOD
Definition token.h:60
@ T_ADD
Definition token.h:51
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_MUL
Definition token.h:53
@ T_OR
Definition token.h:56
close(uq->fd)
static int command_func(UNUSED FILE *fp, UNUSED FILE *fp_err, UNUSED void *ctx, UNUSED fr_cmd_info_t const *info)
#define RETURN_OK(_len)
int fuzzer_dir
File descriptor pointing to a a directory to write fuzzer output.
#define POISONED_BUFFER_START(_p)
static size_t commands_len
static int dump_fuzzer_data(int fd_dir, char const *text, uint8_t const *data, size_t data_len)
static size_t command_touch(command_result_t *result, UNUSED command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Touch a file to indicate a test completed.
uint32_t start
Start of line range.
static ssize_t encode_data_string(char *buffer, uint8_t *output, size_t outlen)
static dl_loader_t * dl_loader
#define BUFF_POISON_START
static ssize_t encode_extended(char *buffer, uint8_t *output, size_t outlen)
int main(int argc, char *argv[])
static size_t command_decode_dns_label(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
static size_t command_decode_proto(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
static void unload_proto_library(void)
static size_t command_xlat_normalise(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Parse an reprint and xlat expansion.
uint32_t test_count
How many tests we've executed in this file.
static int decode_vendor(char *buffer, char **endptr)
static size_t command_proto_dictionary(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
static void commands_print(void)
#define POISONED_BUFFER_END(_p)
static char proto_name_prev[128]
TALLOC_CTX * tmp_ctx
Temporary context to hold buffers in this.
static size_t hex_print(char *out, size_t outlen, uint8_t const *in, size_t inlen)
Print hex string to buffer.
static size_t command_exit(command_result_t *result, UNUSED command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Exit gracefully with the specified code.
static ssize_t hex_to_bin(uint8_t *out, size_t outlen, char *in, size_t inlen)
#define RETURN_OK_WITH_ERROR()
static size_t command_xlat_purify(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Parse, purify, and reprint an xlat expression expansion.
char const * filename
Current file we're operating on.
static void mismatch_print(command_file_ctx_t *cc, char const *command, char *expected, size_t expected_len, char *got, size_t got_len, bool print_diff)
static size_t command_tmpl_rules(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, size_t inlen)
static size_t command_fuzzer_out(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Enable fuzzer output.
static ssize_t encode_rfc(char *buffer, uint8_t *output, size_t outlen)
static dl_t * dl
static void command_ctx_reset(command_file_ctx_t *cc, TALLOC_CTX *ctx)
static int dictionary_load_common(command_result_t *result, command_file_ctx_t *cc, char const *in, char const *default_subdir)
Common dictionary load function.
static size_t command_include(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Execute another test file.
static size_t command_proto_dictionary_root(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
static ssize_t command_tmpl_rule_request_def(TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
static ssize_t command_tmpl_rule_allow_unresolved(UNUSED TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
uint32_t lineno
Current line number.
ssize_t last_ret
Last return value.
#define ASAN_POISON_MEMORY_REGION(_start, _end)
static void features_print(CONF_SECTION *features)
static size_t command_returned(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
static xlat_arg_parser_t const xlat_test_args[]
static int decode_attr(char *buffer, char **endptr)
static fr_dict_t * dictionary_current(command_file_ctx_t *cc)
static size_t command_encode_pair(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
fr_dlist_t entry
Entry in the dlist.
uint8_t * buffer_end
Where the non-poisoned region of the buffer ends.
static fr_table_ptr_sorted_t commands[]
#define RETURN_NOOP(_len)
TALLOC_CTX * tmp_ctx
Talloc context for test points.
static size_t command_radmin_tab(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
static size_t command_calc_nary(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
Perform calculations on multi-valued ops.
static size_t command_dictionary_dump(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, size_t data_used, UNUSED char *in, UNUSED size_t inlen)
Print the currently loaded dictionary.
fr_dict_gctx_t const * test_gctx
Dictionary context for test dictionaries.
static size_t command_proto(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Dynamically load a protocol library.
static fr_cmd_t * command_head
static size_t command_write(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
char * path
Current path we're operating in.
static int poisoned_buffer_allocate(TALLOC_CTX *ctx, uint8_t **buff, size_t size)
Allocate a special buffer with poisoned memory regions at the start and end.
#define DEFAULT_BUFFER_SIZE
Default buffer size for a command_file_ctx_t.
static FILE * write_fp
static size_t strerror_concat(char *out, size_t outlen)
Concatenate error stack.
static size_t command_match_regex(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
Compare the data buffer against an expected expression.
static size_t command_encode_raw(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Encode a RADIUS attribute writing the result to the data buffer as space separated hexits.
#define RETURN_PARSE_ERROR(_offset)
static ssize_t command_tmpl_rule_allow_unknown(UNUSED TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
command_config_t const * config
static int _command_ctx_free(command_file_ctx_t *cc)
#define COMMAND_OUTPUT_MAX
static size_t command_allow_unresolved(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, size_t inlen)
Determine if unresolved attributes are allowed.
static size_t command_read_file(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Parse a list of pairs.
static int process_file(bool *exit_now, TALLOC_CTX *ctx, command_config_t const *config, const char *root_dir, char const *filename, fr_dlist_head_t *lines)
static size_t command_cast(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
Perform casting.
static ssize_t encode_tlv(char *buffer, uint8_t *output, size_t outlen)
#define BUFF_POISON_END
static int command_walk(UNUSED void *ctx, fr_cmd_walk_info_t *info)
static size_t command_condition_normalise(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
Parse and reprint a condition.
static size_t command_count(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
static fr_table_num_sorted_t command_rcode_table[]
static size_t command_max_buffer_size(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Artificially limit the maximum packet size.
static const fr_token_t token2op[UINT8_MAX+1]
tmpl_rules_t tmpl_rules
To pass to parsing functions.
size_t(* command_func_t)(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
Command to execute.
#define RETURN_MISMATCH(_len)
uint32_t end
End of line range.
static char const hextab[]
static command_file_ctx_t * command_ctx_alloc(TALLOC_CTX *ctx, command_config_t const *config, char const *path, char const *filename)
static size_t command_encode_proto(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
static size_t command_load_dictionary(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
static ssize_t load_test_point_by_command(void **symbol, char *command, char const *dflt_symbol)
static void command_print(void)
#define RETURN_EXIT(_ret)
static fr_event_list_t * el
static size_t command_decode_pair(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
static size_t command_xlat_expr(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Parse and reprint an xlat expression expansion.
char const * fuzzer_dir
Where to write fuzzer files.
#define EXIT_WITH_FAILURE
static size_t command_pair(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
Parse an print an attribute pair or pair list.
CONF_SECTION * features
Enabled features.
static size_t command_calc(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
Perform calculations.
static size_t command_migrate(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Set or clear migration flags.
static size_t command_dictionary_attribute_parse(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Parse a dictionary attribute, writing "ok" to the data buffer is everything was ok.
static size_t command_xlat_argv(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Parse an reprint and xlat argv expansion.
#define CLEAR_TEST_POINT(_cc)
static ssize_t encode_data(char *p, uint8_t *output, size_t outlen)
#define RETURN_COMMAND_ERROR()
static size_t command_eof(UNUSED command_result_t *result, UNUSED command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
Command eof.
static size_t command_match(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
Compare the data buffer to an expected value.
#define ASAN_UNPOISON_MEMORY_REGION(_start, _end)
#define RETURN_SKIP_FILE()
static ssize_t command_tmpl_rule_attr_parent(UNUSED TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
command_rcode_t rcode
fr_dict_gctx_t const * dict_gctx
Dictionary gctx to "reset" to.
static ssize_t command_tmpl_rule_list_def(UNUSED TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
static ssize_t command_tmpl_rule_allow_foreign(UNUSED TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
static size_t parse_typed_value(command_result_t *result, fr_value_box_t *box, char const **out, char const *in, size_t inlen)
static ssize_t encode_vsa(char *buffer, uint8_t *output, size_t outlen)
size_t process_line(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
static ssize_t load_proto_library(char const *proto_name)
@ RESULT_MISMATCH
Fatal error - Result didn't match what we expected.
@ RESULT_COMMAND_ERROR
Fatal error - Command operation error.
@ RESULT_NOOP
Not an error - Did nothing...
@ RESULT_OK
Not an error - Result as expected.
@ RESULT_EXIT
Stop processing files and exit.
@ RESULT_SKIP_FILE
Not an error - Skip the rest of this file, or until we reach an "eof" command.
@ RESULT_PARSE_ERROR
Fatal error - Command syntax error.
static int line_ranges_parse(TALLOC_CTX *ctx, fr_dlist_head_t *out, fr_sbuff_t *in)
static size_t command_cd(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, size_t inlen)
Change the working directory.
static size_t command_comment(UNUSED command_result_t *result, UNUSED command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, UNUSED char *in, UNUSED size_t inlen)
Placeholder function for comments.
uint8_t * buffer_start
Where the non-poisoned region of the buffer starts.
static ssize_t encode_data_tlv(char *buffer, char **endptr, uint8_t *output, size_t outlen)
static size_t command_radmin_add(command_result_t *result, command_file_ctx_t *cc, char *data, size_t UNUSED data_used, char *in, UNUSED size_t inlen)
static size_t command_need_feature(command_result_t *result, command_file_ctx_t *cc, UNUSED char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Skip the test file if we're missing a particular feature.
static ssize_t encode_long_extended(char *buffer, uint8_t *output, size_t outlen)
ssize_t(* command_tmpl_rule_func)(TALLOC_CTX *ctx, tmpl_rules_t *rules, fr_sbuff_t *value)
Callback for a tmpl rule parser.
static size_t command_clear(command_result_t *result, UNUSED command_file_ctx_t *cc, char *data, size_t UNUSED data_used, UNUSED char *in, UNUSED size_t inlen)
fr_dict_t * test_internal_dict
Internal dictionary of test_gctx.
static ssize_t encode_evs(char *buffer, uint8_t *output, size_t outlen)
static size_t command_value_box_normalise(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
static xlat_action_t xlat_test(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
fr_dict_t * dict
Dictionary to "reset" to.
static char const * write_filename
static xlat_arg_parser_t const xlat_test_no_args[]
uint8_t * buffer
Temporary resizable buffer we use for holding non-string data.
static size_t command_no(command_result_t *result, command_file_ctx_t *cc, char *data, size_t data_used, char *in, size_t inlen)
Negate the result of a match command or any command which returns "OK".
static size_t command_rcode_table_len
static size_t command_encode_dns_label(command_result_t *result, command_file_ctx_t *cc, char *data, UNUSED size_t data_used, char *in, UNUSED size_t inlen)
Configuration parameters passed to command functions.
int unlang_thread_instantiate(TALLOC_CTX *ctx)
Create thread-specific data structures for unlang.
Definition compile.c:5164
fr_slen_t xlat_tokenize_condition(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:3058
fr_slen_t xlat_print(fr_sbuff_t *in, xlat_exp_head_t const *node, fr_sbuff_escape_rules_t const *e_rules)
Reconstitute an xlat expression from its constituent nodes.
void xlat_debug_head(xlat_exp_head_t const *head)
bool required
Argument must be present, and non-empty.
Definition xlat.h:145
fr_slen_t xlat_tokenize(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)
Tokenize an xlat expansion.
static fr_slen_t head
Definition xlat.h:416
int xlat_instantiate(void)
Call instantiation functions for all registered, "permanent" xlats.
Definition xlat_inst.c:511
fr_slen_t xlat_tokenize_argv(TALLOC_CTX *ctx, xlat_exp_head_t **head, fr_sbuff_t *in, xlat_t const *xlat, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, bool spaces))
Tokenize an xlat expansion into a series of XLAT_TYPE_CHILD arguments.
int xlat_thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el)
Create thread specific instance tree and create thread instances.
Definition xlat_inst.c:442
int xlat_purify(xlat_exp_head_t *head, unlang_interpret_t *intp)
Purify an xlat.
int xlat_flatten_to_argv(TALLOC_CTX *ctx, xlat_exp_head_t ***argv, xlat_exp_head_t *head)
Turn am xlat list into an argv[] array, and nuke the input list.
Definition xlat_eval.c:1739
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:166
xlat_action_t
Definition xlat.h:37
@ 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:3030
Definition for a single argument consumend by an xlat function.
Definition xlat.h:144
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:8
#define fr_pair_dcursor_iter_init(_cursor, _list, _iter, _uctx)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:569
void fr_pair_list_free(fr_pair_list_t *list)
Free memory used by a valuepair list.
#define PAIR_LIST_VERIFY(_x)
Definition pair.h:194
ssize_t fr_pair_list_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_list_t const *list)
Print a pair list.
Definition pair_print.c:240
static fr_slen_t parent
Definition pair.h:845
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:732
char const * fr_strerror_peek(void)
Get the last library error.
Definition strerror.c:626
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
char const * fr_strerror_pop(void)
Pop the last library error.
Definition strerror.c:680
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const_push(_msg)
Definition strerror.h:227
#define fr_strerror_printf_push_head(_fmt,...)
Add a message to an existing stack of messages at the head.
Definition strerror.h:104
#define fr_strerror_const(_msg)
Definition strerror.h:223
fr_table_num_ordered_t const fr_type_table[]
Map data types to names representing those types.
Definition types.c:31
#define fr_type_is_null(_x)
Definition types.h:326
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition version.c:40
#define RADIUSD_MAGIC_NUMBER
Definition version.h:81
fr_sbuff_escape_rules_t fr_value_escape_double
Definition value.c:349
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:5487
int fr_value_box_cast(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_value_box_t const *src)
Convert one type of fr_value_box_t to another.
Definition value.c:3574
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
Definition value.c:722
fr_sbuff_parse_rules_t const value_parse_rules_bareword_unquoted
Default formatting rules.
Definition value.c:479
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:3795
ssize_t fr_value_box_from_str(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, char const *in, size_t inlen, fr_sbuff_unescape_rules_t const *erules)
Definition value.c:5450
ssize_t fr_value_box_print_quoted(fr_sbuff_t *out, fr_value_box_t const *data, fr_token_t quote)
Print one boxed value to a string with quotes (where needed)
Definition value.c:5675
fr_sbuff_parse_rules_t const value_parse_rules_double_quoted
Definition value.c:546
void fr_value_box_clear(fr_value_box_t *data)
Clear/free any existing value and metadata.
Definition value.c:3946
fr_sbuff_unescape_rules_t fr_value_unescape_double
Definition value.c:265
ssize_t fr_value_box_from_substr(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_type_t dst_type, fr_dict_attr_t const *dst_enumv, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *rules)
Convert string value to a fr_value_box_t type.
Definition value.c:4890
#define fr_value_box_mark_safe_for(_box, _safe_for)
Definition value.h:1063
static fr_slen_t data
Definition value.h:1274
#define fr_box_strvalue_len(_val, _len)
Definition value.h:297
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1012
int nonnull(2, 5))
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:598
static size_t char ** out
Definition value.h:1012
#define FR_VALUE_BOX_SAFE_FOR_ANY
Definition value.h:166
An xlat calling ctx.
Definition xlat_ctx.h:49
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:362
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