The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
unit_test_module.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 * $Id: 719d01370476a980d43e141751dc0abe81fbdb41 $
18 *
19 * @file unit_test_module.c
20 * @brief Module test framework
21 *
22 * @copyright 2000-2018 The FreeRADIUS server project
23 * @copyright 2013 Alan DeKok (aland@freeradius.org)
24 * @copyright 2018 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
25 */
26RCSID("$Id: 719d01370476a980d43e141751dc0abe81fbdb41 $")
27
28#include <freeradius-devel/server/base.h>
29#include <freeradius-devel/server/module_rlm.h>
30#include <freeradius-devel/util/rand.h>
31#include <freeradius-devel/io/listen.h>
32
33#include <freeradius-devel/tls/base.h>
34#include <freeradius-devel/tls/version.h>
35
36#include <freeradius-devel/io/thread.h>
37
38#include <freeradius-devel/unlang/base.h>
39#include <freeradius-devel/unlang/xlat_func.h>
40
41#include <freeradius-devel/protocol/freeradius/freeradius.internal.h>
42
43#ifdef HAVE_GETOPT_H
44# include <getopt.h>
45#endif
46
47#define EXIT_WITH_FAILURE \
48do { \
49 ret = EXIT_FAILURE; \
50 goto cleanup; \
51} while (0)
52
53/*
54 * Global variables.
55 */
56static bool filedone = false;
57static int my_debug_lvl = 0;
58
59char const *radiusd_version = RADIUSD_VERSION_BUILD("unit_test_module");
60
63
64#define PROTOCOL_NAME unit_test_module_dict[1].proto
65
68 { .out = &dict_freeradius, .proto = "freeradius" },
69 { .out = &dict_protocol, .proto = "radius" }, /* hacked in-place with '-p protocol' */
71};
72
75
78 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_protocol },
79 { .out = &attr_net, .name = "Net", .type = FR_TYPE_TLV, .dict = &dict_freeradius },
80
82};
83
84/*
85 * Static functions.
86 */
87static void usage(main_config_t const *config, int status);
88
89static fr_client_t *client_alloc(TALLOC_CTX *ctx, char const *ip, char const *name)
90{
91 CONF_SECTION *cs;
92 fr_client_t *client;
93
94 cs = cf_section_alloc(ctx, NULL, "client", name);
95 MEM(cf_pair_alloc(cs, "ipaddr", ip, T_OP_EQ, T_BARE_WORD, T_BARE_WORD));
96 MEM(cf_pair_alloc(cs, "secret", "supersecret", T_OP_EQ, T_BARE_WORD, T_DOUBLE_QUOTED_STRING));
98 MEM(cf_pair_alloc(cs, "shortname", "test", T_OP_EQ, T_BARE_WORD, T_DOUBLE_QUOTED_STRING));
102
103 client = client_afrom_cs(ctx, cs, NULL, 0);
104 if (!client) {
105 PERROR("Failed creating test client");
106 fr_assert(0);
107 }
108 talloc_steal(client, cs);
109 fr_assert(client);
110
111 return client;
112}
113
115{
116 if (fr_type_is_leaf(vp->vp_type)) {
117 vp->vp_immutable = false;
118
119 return;
120 }
121
123
124 fr_pair_list_foreach(&vp->vp_group, child) {
125 pair_mutable(child);
126 }
127}
128
129static request_t *request_from_internal(TALLOC_CTX *ctx)
130{
131 request_t *request;
132
133 /*
134 * Create and initialize the new request.
135 */
136 request = request_local_alloc_internal(ctx, NULL);
137 if (!request->packet) request->packet = fr_packet_alloc(request, false);
138 if (!request->reply) request->reply = fr_packet_alloc(request, false);
139
140 request->packet->socket = (fr_socket_t){
141 .type = SOCK_DGRAM,
142 .inet = {
143 .src_ipaddr = {
144 .af = AF_INET,
145 .prefix = 32,
146 .addr = {
147 .v4 = {
148 .s_addr = htonl(INADDR_LOOPBACK)
149 }
150 }
151 },
152 .src_port = 18120,
153 .dst_ipaddr = {
154 .af = AF_INET,
155 .prefix = 32,
156 .addr = {
157 .v4 = {
158 .s_addr = htonl(INADDR_LOOPBACK)
159 }
160 }
161 },
162 .dst_port = 1812
163 }
164 };
165
166 request->log.dst = talloc_zero(request, log_dst_t);
167 request->log.dst->func = vlog_request;
168 request->log.dst->uctx = &default_log;
169 request->log.dst->lvl = fr_debug_lvl;
170
171 request->master_state = REQUEST_ACTIVE;
172 request->log.lvl = fr_debug_lvl;
173 request->async = talloc_zero(request, fr_async_t);
174 request->async->request = request;
175
176 if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
177 talloc_free(request);
178 fprintf(stderr, "Failed converting packet IPs to attributes");
179 return NULL;
180 }
181
182 return request;
183}
184
185static request_t *request_from_file(TALLOC_CTX *ctx, FILE *fp, fr_client_t *client, CONF_SECTION *server_cs)
186{
187 fr_pair_t *vp;
188 request_t *request;
189 fr_dcursor_t cursor;
190
191 static int number = 0;
192
193 if (!dict_protocol) {
194 fr_strerror_printf_push("%s dictionary failed to load", PROTOCOL_NAME);
195 return NULL;
196 }
197
198 /*
199 * Create and initialize the new request.
200 */
201 request = request_local_alloc_external(ctx, (&(request_init_args_t){ .namespace = dict_protocol }));
202
203 request->packet = fr_packet_alloc(request, false);
204 if (!request->packet) {
205 oom:
206 fr_strerror_const("No memory");
207 error:
208 talloc_free(request);
209 return NULL;
210 }
211 request->packet->timestamp = fr_time();
212
213 request->reply = fr_packet_alloc(request, false);
214 if (!request->reply) goto oom;
215
216 request->client = client;
217 request->number = number++;
218 request->name = talloc_typed_asprintf(request, "%" PRIu64, request->number);
219 request->master_state = REQUEST_ACTIVE;
220
221 /*
222 * Read packet from fp
223 */
224 if (fr_pair_list_afrom_file(request->request_ctx, dict_protocol, &request->request_pairs, fp, &filedone, true) < 0) {
225 goto error;
226 }
227
228 /*
229 * Pretend that the attributes came in "over the wire".
230 *
231 * @todo - do this only for protocol attributes, and not internal ones?
232 */
233 fr_pair_list_tainted(&request->request_pairs);
234
235 vp = fr_pair_find_by_da(&request->request_pairs, NULL, attr_packet_type);
236 if (!vp) {
237 fr_strerror_printf("Input packet does not specify a Packet-Type");
238 goto error;
239 }
240 /*
241 * Set the defaults for IPs, etc.
242 */
243 request->packet->code = vp->vp_uint32;
244
245 /*
246 * Now delete the packet-type to ensure
247 * the virtual attribute gets used in
248 * the tests.
249 */
250 fr_pair_delete_by_da(&request->request_pairs, attr_packet_type);
251
252 request->packet->socket = (fr_socket_t){
253 .type = SOCK_DGRAM,
254 .inet = {
255 .src_ipaddr = {
256 .af = AF_INET,
257 .prefix = 32,
258 .addr = {
259 .v4 = {
260 .s_addr = htonl(INADDR_LOOPBACK)
261 }
262 }
263 },
264 .src_port = 18120,
265 .dst_ipaddr = {
266 .af = AF_INET,
267 .prefix = 32,
268 .addr = {
269 .v4 = {
270 .s_addr = htonl(INADDR_LOOPBACK)
271 }
272 }
273 },
274 .dst_port = 1812
275 }
276 };
277
278 /*
279 * Fill in the packet header from attributes, and then
280 * re-realize the attributes.
281 */
282 vp = fr_pair_find_by_da(&request->request_pairs, NULL, attr_packet_type);
283 if (vp) request->packet->code = vp->vp_uint32;
284
285 fr_packet_net_from_pairs(request->packet, &request->request_pairs);
286
287 /*
288 * The input might have updated only some of the Net.*
289 * attributes. So for consistency, we create all of them
290 * from the packet header.
291 */
292 if (fr_packet_pairs_from_packet(request->request_ctx, &request->request_pairs, request->packet) < 0) {
293 fr_strerror_const("Failed converting packet IPs to attributes");
294 goto error;
295 }
296
297 /*
298 * For laziness in the tests, allow the Net.* to be mutable
299 */
300 for (vp = fr_pair_dcursor_by_ancestor_init(&cursor, &request->request_pairs, attr_net);
301 vp != NULL;
302 vp = fr_dcursor_next(&cursor)) {
304 }
305
306 if (fr_debug_lvl) {
307 for (vp = fr_pair_dcursor_init(&cursor, &request->request_pairs);
308 vp;
309 vp = fr_dcursor_next(&cursor)) {
310 /*
311 * Take this opportunity to verify all the fr_pair_ts are still valid.
312 */
313 if (!talloc_get_type(vp, fr_pair_t)) {
314 ERROR("Expected fr_pair_t pointer got \"%s\"", talloc_get_name(vp));
315
317 fr_assert(0);
318 }
319
320 fr_log(&default_log, L_DBG, __FILE__, __LINE__, "%pP", vp);
321 }
322 }
323
324 /*
325 * Build the reply template from the request.
326 */
327 fr_socket_addr_swap(&request->reply->socket, &request->packet->socket);
328
329 request->reply->id = request->packet->id;
330 request->reply->code = 0; /* UNKNOWN code */
331 memcpy(request->reply->vector, request->packet->vector, sizeof(request->reply->vector));
332 request->reply->data = NULL;
333 request->reply->data_len = 0;
334
335 /*
336 * Debugging
337 */
338 request->log.dst = talloc_zero(request, log_dst_t);
339 request->log.dst->func = vlog_request;
340 request->log.dst->uctx = &default_log;
341 request->log.dst->lvl = fr_debug_lvl;
342
343 request->master_state = REQUEST_ACTIVE;
344 request->log.lvl = fr_debug_lvl;
345 request->async = talloc_zero(request, fr_async_t);
346 request->async->request = request;
347
348
349 /*
350 * New async listeners
351 */
352 unlang_call_push(NULL, request, server_cs, UNLANG_TOP_FRAME);
353
354 return request;
355}
356
357
358static void print_packet(FILE *fp, fr_packet_t *packet, fr_pair_list_t *list)
359{
360 fr_dict_enum_value_t const *dv;
361 fr_log_t log;
362
363 (void) fr_log_init_fp(&log, fp);
364
366 if (dv) {
367 fr_log(&default_log, L_DBG, __FILE__, __LINE__, "Packet-Type = %s", dv->name);
368 } else {
369 fr_log(&default_log, L_DBG, __FILE__, __LINE__, "Packet-Type = %u", packet->code);
370 }
371
372 fr_pair_list_log(&default_log, 2, list);
373}
374
375/*
376 * A common function for reports of too much text when handling xlat
377 * and xlat_expr in do_xlats().
378 * The convolution deals with the edge case of the line being so long
379 * that it plus the surrounding text from the format could won't fit
380 * in the output sbuff, along with the fact that you don't print the
381 * %d or %.*s. OTOH it does include slen, but...
382 * * the format string is 41 characters minus 6 for %d and %.*s
383 * * given that slen reflects text read from line, once slen is
384 * large enough, we know line will fit
385 */
386static inline CC_HINT(always_inline) void too_much_text(fr_sbuff_t *out, ssize_t slen, fr_sbuff_t *line)
387{
388 char const *format = "ERROR offset %d 'Too much text' ::%.*s::";
389
390 (void) fr_sbuff_in_sprintf(out, format, (int) slen,
391 fr_sbuff_remaining(out) - (strlen(format) - 5),
393}
394
395/*
396 * Read a file composed of xlat's and expected results
397 */
398static bool do_xlats(fr_event_list_t *el, request_t *request, char const *filename, FILE *fp)
399{
400 int lineno = 0;
401 ssize_t len;
402 char line_buff[8192];
403 char output_buff[8192];
404 char unescaped[sizeof(output_buff)];
407
408 static fr_sbuff_escape_rules_t unprintables = {
409 .name = "unprintables",
410 .chr = '\\',
411 .esc = {
414 },
415 .do_utf8 = true,
416 .do_oct = true
417 };
418
419 while (fgets(line_buff, sizeof(line_buff), fp) != NULL) {
420 lineno++;
421
422 line = FR_SBUFF_IN(line_buff, sizeof(line_buff));
423 if (!fr_sbuff_adv_to_chr(&line, SIZE_MAX, '\n')) {
424 if (!feof(fp)) {
425 fprintf(stderr, "%s[%d] Line too long\n", filename, lineno);
426 return false;
427 }
428 } else {
429 fr_sbuff_terminate(&line);
430 }
431 line.end = line.p;
432 fr_sbuff_set_to_start(&line);
433
434 /*
435 * Ignore blank lines and comments
436 */
437 fr_sbuff_adv_past_whitespace(&line, SIZE_MAX, NULL);
438 if (*fr_sbuff_current(&line) < ' ') continue;
439 if (fr_sbuff_is_char(&line, '#')) continue;
440
441 /*
442 * Look for "match", as it needs the output_buffer to be left alone.
443 */
444 if (fr_sbuff_adv_past_str_literal(&line, "match ") > 0) {
445 size_t output_len = strlen(output_buff);
446
447 if (!fr_sbuff_is_str(&line, output_buff, output_len) || (output_len != fr_sbuff_remaining(&line))) {
448 fprintf(stderr, "Mismatch at %s[%u]\n\tgot : %s (%zu)\n\texpected : %s (%zu)\n",
449 filename, lineno, output_buff, output_len, fr_sbuff_current(&line), fr_sbuff_remaining(&line));
450 return false;
451 }
452 continue;
453 }
454
455 /*
456 * The rest of the keywords create output.
457 */
458 output_buff[0] = '\0';
459 out = FR_SBUFF_OUT(output_buff, sizeof(output_buff));
460
461 /*
462 * Look for "xlat"
463 */
464 if (fr_sbuff_adv_past_str_literal(&line, "xlat ") > 0) {
465 ssize_t slen;
466 TALLOC_CTX *xlat_ctx = talloc_init_const("xlat");
467 xlat_exp_head_t *head = NULL;
468 fr_sbuff_parse_rules_t p_rules = { .escapes = &fr_value_unescape_double };
469 tmpl_rules_t t_rules = (tmpl_rules_t) {
470 .attr = {
472 .list_def = request_attr_request,
473 .allow_unresolved = true,
474 },
475 .xlat = {
476 .runtime_el = el,
477 },
478 .at_runtime = true,
479 };
480
481
482 slen = xlat_tokenize(xlat_ctx, &head, &line, &p_rules, &t_rules);
483 if (slen <= 0) {
485 fr_sbuff_in_sprintf(&out, "ERROR offset %d '%s'", (int) -slen, fr_strerror());
486 continue;
487 }
488
489 if (fr_sbuff_remaining(&line) > 0) {
491 too_much_text(&out, slen, &line);
492 continue;
493 }
494
495 len = xlat_eval_compiled(unescaped, sizeof(unescaped), request, head, NULL, NULL);
496 if (len < 0) {
497 char const *err = fr_strerror();
499 (void) fr_sbuff_in_sprintf(&out, "ERROR expanding xlat: %s", *err ? err : "no error provided");
500 continue;
501 }
502
503 /*
504 * Escape the output as if it were a double quoted string.
505 */
506 fr_sbuff_in_escape(&out, unescaped, len, &unprintables);
507
508 TALLOC_FREE(xlat_ctx); /* also frees 'head' */
509 continue;
510 }
511
512 /*
513 * Look for "xlat_expr"
514 */
515 if (fr_sbuff_adv_past_str_literal(&line, "xlat_expr ") > 0) {
516 ssize_t slen;
517 TALLOC_CTX *xlat_ctx = talloc_init_const("xlat");
518 xlat_exp_head_t *head = NULL;
519
521 &line,
522 NULL,
523 &(tmpl_rules_t) {
524 .attr = {
525 .dict_def = dict_protocol,
526 .list_def = request_attr_request,
527 .allow_unresolved = true,
528 },
529 .xlat = {
530 .runtime_el = el,
531 },
532 .at_runtime = true,
533 });
534 if (slen <= 0) {
536 fr_sbuff_in_sprintf(&out, "ERROR offset %d '%s'", (int) -slen - 1, fr_strerror());
537 continue;
538 }
539
540 if (fr_sbuff_remaining(&line) > 0) {
542 too_much_text(&out, slen, &line);
543 continue;
544 }
545
546 if (xlat_resolve(head, NULL) < 0) {
548 (void) fr_sbuff_in_sprintf(&out, "ERROR resolving xlat: %s", fr_strerror());
549 continue;
550 }
551
552 len = xlat_eval_compiled(unescaped, sizeof(unescaped), request, head, NULL, NULL);
553 if (len < 0) {
554 char const *err = fr_strerror();
556 (void) fr_sbuff_in_sprintf(&out, "ERROR expanding xlat: %s", *err ? err : "no error provided");
557 continue;
558 }
559
560 /*
561 * Escape the output as if it were a double quoted string.
562 */
563 fr_sbuff_in_escape(&out, unescaped, len, &unprintables);
564
565 TALLOC_FREE(xlat_ctx); /* also frees 'head' */
566 continue;
567 }
568
569 fprintf(stderr, "Unknown keyword in %s[%d]\n", filename, lineno);
570 return false;
571 }
572
573 return true;
574}
575
576/*
577 * Verify the result of the map.
578 */
579static int map_proc_verify(CONF_SECTION *cs, UNUSED void const *mod_inst, UNUSED void *proc_inst,
580 tmpl_t const *src, UNUSED map_list_t const *maps)
581{
582 if (!src) {
583 cf_log_err(cs, "Missing source");
584
585 return -1;
586 }
587
588 return 0;
589}
590
592 UNUSED request_t *request, UNUSED fr_value_box_list_t *src,
593 UNUSED map_list_t const *maps)
594{
596}
597
598static request_t *request_clone(request_t *old, int number, CONF_SECTION *server_cs)
599{
600 request_t *request;
601
602 request = request_local_alloc_internal(NULL, (&(request_init_args_t){ .namespace = old->proto_dict }));
603 if (!request) return NULL;
604
605 if (!request->packet) request->packet = fr_packet_alloc(request, false);
606 if (!request->reply) request->reply = fr_packet_alloc(request, false);
607
608 memcpy(request->packet, old->packet, sizeof(*request->packet));
609 (void) fr_pair_list_copy(request->request_ctx, &request->request_pairs, &old->request_pairs);
610 request->packet->timestamp = fr_time();
611 request->number = number;
612 request->name = talloc_typed_asprintf(request, "%" PRIu64, request->number);
613
614 unlang_call_push(NULL, request, server_cs, UNLANG_TOP_FRAME);
615
616 request->master_state = REQUEST_ACTIVE;
617
618 return request;
619}
620
621static void cancel_request(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t when, void *uctx)
622{
623 request_t *request = talloc_get_type_abort(uctx, request_t);
625 request->rcode = RLM_MODULE_TIMEOUT;
626}
627
629
630/** Sythentic time source for tests
631 *
632 * This allows us to artificially advance time for tests.
633 */
639 { .required = true, .type = FR_TYPE_TIME_DELTA, .single = true },
641};
642
644
646{
647 request_t *request = talloc_get_type_abort(uctx, request_t);
649}
650
652 UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *in)
653{
654 fr_value_box_t *vb;
655
656 MEM(vb = fr_value_box_alloc(ctx, FR_TYPE_TIME_DELTA, NULL));
657 vb->vb_time_delta = time_offset;
659
660 return XLAT_ACTION_DONE;
661}
662
664 UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
665{
666 fr_value_box_t *delta;
667
668 XLAT_ARGS(in, &delta);
669
670 /*
671 * This ensures we take a pass through the timer list
672 * otherwise the time advances can be ignored.
673 */
675 RPERROR("Failed to add timer");
676 return XLAT_ACTION_FAIL;
677 }
678
680
681 time_offset = fr_time_delta_add(time_offset, delta->vb_time_delta);
682
684
685 unlang_xlat_yield(request, xlat_func_time_advance_resume, NULL, 0, NULL);
686
687 return XLAT_ACTION_YIELD;
688}
689
690/**
691 *
692 * @hidecallgraph
693 */
694int main(int argc, char *argv[])
695{
696 int ret = EXIT_SUCCESS;
697 int c;
698 int count = 1;
699 const char *input_file = NULL;
700 const char *xlat_input_file = NULL;
701 const char *output_file = NULL;
702 const char *filter_file = NULL;
703 FILE *fp = NULL;
704 request_t *request = NULL;
705 fr_pair_t *vp;
706 fr_pair_list_t filter_vps;
707 bool xlat_only = false;
708 fr_event_list_t *el = NULL;
709 fr_client_t *client = NULL;
710 fr_dict_t *dict = NULL;
711 fr_dict_t const *dict_check;
712 char const *receipt_file = NULL;
713
714 xlat_t *time_advance = NULL;
715
716 TALLOC_CTX *autofree;
717 TALLOC_CTX *thread_ctx;
718
719 char *p;
721
722 CONF_SECTION *server_cs;
723
724#ifndef NDEBUG
725 size_t memory_used_before = 0;
726 size_t memory_used_after = 0;
727#endif
728 virtual_server_t const *vs;
729
730 fr_pair_list_init(&filter_vps);
731
732 /*
733 * Must be called first, so the handler is called last
734 */
736
738 thread_ctx = talloc_new(autofree);
739
741 if (!config) {
742 fr_perror("unit_test_module");
743 fr_exit_now(EXIT_FAILURE);
744 }
745
746 p = strrchr(argv[0], FR_DIR_SEP);
747 if (!p) {
748 main_config_name_set_default(config, argv[0], false);
749 } else {
751 }
752
754
755 /*
756 * The test wrapper's timeout arrives as SIGALRM, so the fatal
757 * signal handlers are installed in every build.
758 */
759 if (fr_fault_setup(autofree, getenv("PANIC_ACTION"), argv[0], PANIC_ACTION_SIGNALS) < 0) {
760 fr_perror("%s", config->name);
761 fr_exit_now(EXIT_FAILURE);
762 }
763#ifdef NDEBUG
765#endif
766
767 fr_debug_lvl = 0;
769
770 /*
771 * The tests should have only IPs, not host names.
772 */
774
775 /*
776 * We always log to stdout.
777 */
779 default_log.fd = STDOUT_FILENO;
781
782 /* Process the options. */
783 while ((c = getopt(argc, argv, "c:Cd:D:f:hi:I:mMn:o:p:r:xXz")) != -1) {
784 switch (c) {
785 case 'c':
786 count = atoi(optarg);
787 break;
788
789 case 'C':
790 check_config = true;
791 break;
792
793 case 'd':
795 break;
796
797 case 'D':
799 break;
800
801 case 'f':
802 filter_file = optarg;
803 break;
804
805 case 'h':
806 usage(config, EXIT_SUCCESS);
807 break;
808
809 case 'i':
810 input_file = optarg;
811 break;
812
813 case 'I':
814 xlat_input_file = optarg;
815 xlat_only = true;
816 break;
817
818 case 'M':
819 talloc_enable_leak_report();
820 break;
821
822 case 'n':
823 config->name = optarg;
824 break;
825
826 case 'o':
827 output_file = optarg;
828 break;
829
830 case 'p':
831 PROTOCOL_NAME = optarg;
832 break;
833
834 case 'r':
835 receipt_file = optarg;
836 break;
837
838 case 'X':
839 fr_debug_lvl += 2;
841 break;
842
843 case 'x':
844 fr_debug_lvl++;
845 if (fr_debug_lvl > 2) default_log.print_level = true;
846 break;
847
848 case 'z':
849 my_debug_lvl++;
850 break;
851
852 default:
853 usage(config, EXIT_FAILURE);
854 break;
855 }
856 }
857
858 if (receipt_file && (fr_unlink(receipt_file) < 0)) {
859 fr_perror("%s", config->name);
861 }
862
863#ifdef WITH_TLS
864 /*
865 * Mismatch between build time OpenSSL and linked SSL, better to die
866 * here than segfault later.
867 */
869
870 /*
871 * Initialising OpenSSL once, here, is safer than having individual modules do it.
872 * Must be called before display_version to ensure relevant engines are loaded.
873 *
874 * fr_openssl_init() must be called before *ANY* OpenSSL functions are used, which is why
875 * it's called so early.
876 */
877 if (fr_openssl_init() < 0) EXIT_WITH_FAILURE;
878#endif
879
881
882 /*
883 * Mismatch between the binary and the libraries it depends on
884 */
886 fr_perror("%s", config->name);
887 ret = EXIT_FAILURE;
888 goto cleanup;
889 }
890
891 /*
892 * Initialize the DL infrastructure, which is used by the
893 * config file parser.
894 */
895 modules_init(config->lib_dir);
896
897 if (!fr_dict_global_ctx_init(NULL, true, config->dict_dir)) {
898 fr_perror("%s", config->name);
900 }
901
903 fr_perror("%s", config->name);
905 }
906
907#ifdef WITH_TLS
908 if (fr_tls_dict_init() < 0) EXIT_WITH_FAILURE;
909#endif
910
911 /*
912 * Load the custom dictionary
913 */
914 if (fr_dict_read(dict, config->confdir, FR_DICTIONARY_FILE) == -1) {
915 PERROR("Failed to initialize the dictionaries");
917 }
918
920 fr_perror("%s", config->name);
922 }
924 fr_perror("%s", config->name);
926 }
927
928 if (request_global_init() < 0) {
929 fr_perror("unit_test_module");
931 }
932
933 if (map_proc_register(NULL, NULL, "test-fail", mod_map_proc, map_proc_verify, 0, 0) < 0) {
935 }
936
937 /*
938 * Needed for the triggers. Which are always run-time expansions.
939 */
940 if (main_loop_init() < 0) {
941 PERROR("Failed initialising main event loop");
943 }
944 /*
945 * Initialise the interpreter, registering operations.
946 * This initialises
947 */
948 if (unlang_global_init() < 0) {
949 fr_perror("%s", config->name);
951 }
952
953 time_advance = xlat_func_register(NULL, "time.advance", xlat_func_time_advance, FR_TYPE_VOID);
954 if (!time_advance) EXIT_WITH_FAILURE;
956
957 /*
958 * Ensure that we load the correct virtual server for the
959 * protocol, if necessary.
960 */
961 if (!getenv("PROTOCOL")) {
962 setenv("PROTOCOL", PROTOCOL_NAME, true);
963 }
964
965 /*
966 * Setup the global structures for module lists
967 */
968 if (modules_rlm_init() < 0) {
969 fr_perror("%s", config->name);
971 }
972 if (virtual_servers_init() < 0) {
973 fr_perror("%s", config->name);
975 }
976
977 if (main_config_init(config) < 0) {
979 }
980
981 /*
982 * Create a dummy client on 127.0.0.1, if one doesn't already exist.
983 */
984 client = client_find(NULL, &(fr_ipaddr_t) { .af = AF_INET, .prefix = 32, .addr.v4.s_addr = htonl(INADDR_LOOPBACK) },
985 IPPROTO_IP);
986 if (!client) {
987 client = client_alloc(NULL, "127.0.0.1", "test");
988 client_add(NULL, client);
989 }
990
991 if (server_init(config->root_cs, config->confdir, dict) < 0) EXIT_WITH_FAILURE;
992
993 vs = virtual_server_find("default");
994 if (!vs) {
995 ERROR("Cannot find virtual server 'default'");
997 }
998
999 server_cs = virtual_server_cs(vs);
1000
1001 /*
1002 * Do some sanity checking.
1003 */
1004 dict_check = virtual_server_dict_by_name("default");
1005 if (!dict_check || !fr_dict_compatible(dict_check, dict_protocol)) {
1006 ERROR("Virtual server namespace does not match requested namespace '%s'", PROTOCOL_NAME);
1008 }
1009
1010 /*
1011 * Everything seems to have loaded OK, exit gracefully.
1012 */
1013 if (check_config) {
1014 DEBUG("Configuration appears to be OK");
1015 goto cleanup;
1016 }
1017
1018 /*
1019 * Get the main event list.
1020 */
1022 fr_assert(el != NULL);
1024
1026
1027 /*
1028 * Simulate thread specific instantiation
1029 */
1031 if (fr_thread_instantiate(thread_ctx, el) < 0) {
1032 fr_perror("%s", config->name);
1034 }
1035
1036 if (modules_rlm_coord_attach(el) < 0) {
1037 fr_perror("%s", config->name);
1039 }
1040
1041 if (fr_coord_pre_event_insert(el) < 0) {
1042 fr_strerror_const("Failed adding coordinator pre-check to event list");
1044 }
1045
1046 if (fr_coord_post_event_insert(el) < 0) {
1047 fr_strerror_const("Failed adding coordinator pre-check to event list");
1049 }
1050
1051 /*
1052 * Set the panic action (if required)
1053 */
1054 {
1055 char const *panic_action = NULL;
1056
1057 panic_action = getenv("PANIC_ACTION");
1058 if (!panic_action) panic_action = config->panic_action;
1059
1061 fr_perror("%s", config->name);
1063 }
1064 }
1065
1066 setlinebuf(stdout); /* unbuffered output */
1067
1068#ifndef NDEBUG
1069 memory_used_before = talloc_total_size(autofree);
1070#endif
1071
1072 if (!input_file && !xlat_only) input_file = "-";
1073
1074 if (input_file) {
1075 if (strcmp(input_file, "-") == 0) {
1076 fp = stdin;
1077 } else {
1078 fp = fopen(input_file, "r");
1079 if (!fp) {
1080 fprintf(stderr, "Failed reading %s: %s\n",
1081 input_file, fr_syserror(errno));
1083 }
1084 }
1085
1086 /*
1087 * Grab the VPs from stdin, or from the file.
1088 */
1089 request = request_from_file(autofree, fp, client, server_cs);
1090 if (!request) {
1091 fr_perror("Failed reading input from %s", input_file);
1093 }
1094 } else {
1096 }
1097
1098 /*
1099 * For simplicity, read xlat's.
1100 */
1101 if (xlat_only) {
1102 if (fp && (fp != stdin)) fclose(fp);
1103
1104 fp = fopen(xlat_input_file, "r");
1105 if (!fp) {
1106 fprintf(stderr, "Failed reading %s: %s\n",
1107 xlat_input_file, fr_syserror(errno));
1109 }
1110
1111 if (!do_xlats(el, request, xlat_input_file, fp)) ret = EXIT_FAILURE;
1112 fclose(fp);
1113 goto cleanup;
1114 }
1115
1116 /*
1117 * No filter file, OR there's no more input, OR we're
1118 * reading from a file, and it's different from the
1119 * filter file.
1120 */
1121 if (!filter_file || filedone ||
1122 ((input_file != NULL) && (strcmp(filter_file, input_file) != 0))) {
1123 if (output_file) {
1124 if (fp && (fp != stdin)) fclose(fp);
1125 fp = NULL;
1126 }
1127 filedone = false;
1128 }
1129
1130 /*
1131 * There is a filter file. If necessary, open it. If we
1132 * already are reading it via "input_file", then we don't
1133 * need to re-open it.
1134 */
1135 if (filter_file) {
1136 if (!fp) {
1137 fp = fopen(filter_file, "r");
1138 if (!fp) {
1139 fprintf(stderr, "Failed reading %s: %s\n", filter_file, fr_syserror(errno));
1141 }
1142 }
1143
1144 if (fr_pair_list_afrom_file(request->request_ctx, dict_protocol, &filter_vps, fp, &filedone, true) < 0) {
1145 fr_perror("Failed reading attributes from %s", filter_file);
1147 }
1148
1149 /*
1150 * Filter files can't be empty.
1151 */
1152 if (fr_pair_list_empty(&filter_vps)) {
1153 fr_perror("No attributes in filter file %s", filter_file);
1155 }
1156
1157 /*
1158 * FIXME: loop over input packets.
1159 */
1160 fclose(fp);
1161 }
1162
1163 if (count == 1) {
1164 fr_timer_in(request, el->tl, &request->timeout, config->worker.max_request_time, false, cancel_request, request);
1166
1167 } else {
1168 int i;
1169 request_t *cached = request;
1170
1171 for (i = 0; i < count; i++) {
1172#ifndef NDEBUG
1173 size_t request_used_before, request_used_after;
1174#endif
1175
1176 request = request_clone(cached, i, server_cs);
1177
1178#ifndef NDEBUG
1179 request_used_before = talloc_total_size(autofree);
1180
1181 /*
1182 * Artificially limit the number of instructions which are run.
1183 */
1184 if (config->ins_max) {
1185 if (config->ins_countup) {
1186 request->ins_max = i + 1;
1187 } else {
1188 request->ins_max = config->ins_max;
1189 }
1190
1191 if (request->ins_max < 10) request->ins_max = 10;
1192
1193 request->ins_count = 0;
1194 }
1195#endif
1196
1197 fr_timer_in(request, el->tl, &request->timeout, config->worker.max_request_time, false, cancel_request, request);
1199 talloc_free(request);
1200
1201#ifndef NDEBUG
1202 request_used_after = talloc_total_size(autofree);
1203 fr_assert(request_used_after == request_used_before);
1204#endif
1205 }
1206
1207 request = cached;
1208 }
1209
1210 if (!output_file || (strcmp(output_file, "-") == 0)) {
1211 fp = stdout;
1212 } else {
1213 fp = fopen(output_file, "w");
1214 if (!fp) {
1215 fprintf(stderr, "Failed writing %s: %s\n", output_file, fr_syserror(errno));
1216 goto cleanup;
1217 }
1218 }
1219
1220 print_packet(fp, request->reply, &request->reply_pairs);
1221
1222 if (output_file) fclose(fp);
1223
1224 /*
1225 * Update the list with the response type, so that it can
1226 * be matched in filters.
1227 *
1228 * Some state machines already include a response Packet-Type
1229 * so we need to try and update it, else we end up with two!
1230 */
1231 if (!fr_pair_list_empty(&filter_vps)) {
1232 fr_pair_t const *failed[2];
1233
1235 vp->vp_uint32 = request->reply->code;
1236
1237 if (!fr_pair_validate(failed, &filter_vps, &request->reply_pairs)) {
1238 fr_pair_validate_debug(failed);
1239
1240 fr_perror("Output file %s does not match attributes in filter %s",
1241 output_file ? output_file : "-", filter_file);
1242 fr_fprintf_pair_list(stderr, &filter_vps);
1243 ret = EXIT_FAILURE;
1244 goto cleanup;
1245 }
1246 }
1247
1248 INFO("Exiting normally");
1249
1250cleanup:
1251 talloc_free(request);
1252
1253 /*
1254 * No leaks.
1255 */
1256#ifndef NDEBUG
1257 memory_used_after = talloc_total_size(autofree);
1258 if (memory_used_after != memory_used_before) {
1259 printf("WARNING: May have leaked memory (%zd - %zd = %zd)\n",
1260 memory_used_after, memory_used_before, memory_used_after - memory_used_before);
1261 }
1262#endif
1263
1264 map_proc_unregister("test-fail");
1265
1266 /*
1267 * Free thread data
1268 */
1269 talloc_free(thread_ctx);
1270
1272
1273 /*
1274 * Ensure all thread local memory is cleaned up
1275 * at the appropriate time. This emulates what's
1276 * done with worker/network threads in the
1277 * scheduler.
1278 */
1280
1281 /*
1282 * Give processes a chance to exit
1283 */
1285
1287
1288 /*
1289 * Ensure all thread local memory is cleaned up
1290 * at the appropriate time. This emulates what's
1291 * done with worker/network threads in the
1292 * scheduler.
1293 */
1295
1296 server_free();
1297
1298 /*
1299 * Virtual servers need to be freed before modules
1300 * as state entries containing data with module-specific
1301 * destructors may exist.
1302 */
1304
1305 /*
1306 * Free modules, this needs to be done explicitly
1307 * because some libraries used by modules use atexit
1308 * handlers registered after ours, and they may deinit
1309 * themselves before we free the modules and cause
1310 * crashes on exit.
1311 */
1313
1314 /*
1315 * And now nothing should be left anywhere except the
1316 * parsed configuration items.
1317 */
1319
1320#ifdef WITH_TLS
1321 fr_tls_dict_free();
1322#endif
1323
1324 /*
1325 * Free any autoload dictionaries
1326 */
1328
1329 /*
1330 * Free our explicitly loaded internal dictionary
1331 */
1332 if (fr_dict_free(&dict, __FILE__) < 0) {
1333 fr_perror("unit_test_module - dict");
1334 ret = EXIT_FAILURE;
1335 }
1336
1337 /*
1338 * Free any openssl resources and the TLS dictionary
1339 */
1340#ifdef WITH_TLS
1341 fr_openssl_free();
1342#endif
1343
1344 if (receipt_file && (ret == EXIT_SUCCESS) && (fr_touch(NULL, receipt_file, 0644, true, 0755) <= 0)) {
1345 fr_perror("unit_test_module");
1346 ret = EXIT_FAILURE;
1347 }
1348
1349 if (talloc_free(autofree) < 0) {
1350 fr_perror("unit_test_module - autofree");
1351 ret = EXIT_FAILURE;
1352 }
1353
1354 /*
1355 * Ensure our atexit handlers run before any other
1356 * atexit handlers registered by third party libraries.
1357 */
1359
1360 return ret;
1361}
1362
1363
1364/*
1365 * Display the syntax for starting this program.
1366 */
1367static NEVER_RETURNS void usage(main_config_t const *config, int status)
1368{
1369 FILE *output = status ? stderr : stdout;
1370
1371 fprintf(output, "Usage: %s [options]\n", config->name);
1372 fprintf(output, "Options:\n");
1373 fprintf(output, " -c <count> Run packets through the interpreter <count> times\n");
1374 fprintf(output, " -C Check configuration and exit.\n");
1375 fprintf(output, " -d <confdir> Configuration file directory. (defaults to " CONFDIR ").");
1376 fprintf(output, " -D <dict_dir> Dictionary files are in \"dict_dir/*\".\n");
1377 fprintf(output, " -f <file> Filter reply against attributes in 'file'.\n");
1378 fprintf(output, " -h Print this help message.\n");
1379 fprintf(output, " -i <file> File containing request attributes.\n");
1380 fprintf(output, " -m On SIGINT or SIGQUIT exit cleanly instead of immediately.\n");
1381 fprintf(output, " -n <name> Read ${confdir}/name.conf instead of ${confdir}/radiusd.conf.\n");
1382 fprintf(output, " -o <file> Output file for the reply.\n");
1383 fprintf(output, " -p <radius|...> Define which protocol namespace is used to read the file\n");
1384 fprintf(output, " Use radius, dhcpv4, or dhcpv6\n");
1385 fprintf(output, " -X Turn on full debugging.\n");
1386 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
1387 fprintf(output, " -r <receipt_file> Create the <receipt_file> as a 'success' exit.\n");
1388
1389 fr_exit_now(status);
1390}
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition atexit.c:179
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition atexit.c:310
#define fr_atexit_thread_trigger_all(...)
Definition atexit.h:236
#define RCSID(id)
Definition build.h:560
#define NEVER_RETURNS
Should be placed before the function return type.
Definition build.h:382
#define unlikely(_x)
Definition build.h:455
#define UNUSED
Definition build.h:384
unlang_action_t unlang_call_push(unlang_result_t *p_result, request_t *request, CONF_SECTION *server_cs, bool top_frame)
Push a virtual server CONF_SECTION as a call frame onto the stack.
Definition call.c:151
bool check_config
Definition cf_file.c:61
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
CONF_PAIR * cf_pair_alloc(CONF_SECTION *parent, char const *attr, char const *value, fr_token_t op, fr_token_t lhs_quote, fr_token_t rhs_quote)
Allocate a CONF_PAIR.
Definition cf_util.c:1444
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:345
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:201
TALLOC_CTX * autofree
Definition common.c:29
fr_dict_t * dict
Definition common.c:31
int fr_coords_create(TALLOC_CTX *ctx, fr_event_list_t *el)
Start coordinators in single threaded mode.
Definition coord.c:592
void fr_coords_destroy(void)
Clean up coordinators in single threaded mode.
Definition coord.c:574
int fr_coord_post_event_insert(fr_event_list_t *el)
Insert instance specific post-event callbacks.
Definition coord.c:832
int fr_coord_pre_event_insert(fr_event_list_t *el)
Insert instance specific pre-event callbacks.
Definition coord.c:807
static void * fr_dcursor_next(fr_dcursor_t *cursor)
Advanced the cursor to the next item.
Definition dcursor.h:288
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:406
static char panic_action[512]
The command to execute when panicking.
Definition debug.c:66
void fr_disable_null_tracking_on_free(TALLOC_CTX *ctx)
Disable the null tracking context when a talloc chunk is freed.
Definition debug.c:1034
int fr_log_talloc_report(TALLOC_CTX const *ctx)
Generate a talloc memory report for a context and print to stderr/stdout.
Definition debug.c:975
void fr_talloc_fault_setup(void)
Register talloc fault handlers.
Definition debug.c:1050
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program, unsigned long fault_signals)
Registers signal handlers to execute panic_action on fatal signal.
Definition debug.c:1074
#define PANIC_ACTION_SIGNALS
Definition debug.h:128
#define MEM(x)
Definition debug.h:38
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:267
void dependency_version_print(void)
Definition dependency.c:364
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
static NEVER_RETURNS void usage(void)
Definition dhcpclient.c:113
#define fr_dict_autofree(_to_free)
Definition dict.h:915
static fr_slen_t err
Definition dict.h:882
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *dict_subdir, char const *dependent))
(Re-)Initialize the special internal dictionary
bool fr_dict_compatible(fr_dict_t const *dict1, fr_dict_t const *dict2)
See if two dictionaries have the same end parent.
Definition dict_util.c:2867
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:292
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:305
int fr_dict_read(fr_dict_t *dict, char const *dict_dir, char const *filename))
Read supplementary attribute definitions into an existing dictionary.
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4327
fr_dict_enum_value_t const * fr_dict_enum_by_value(fr_dict_attr_t const *da, fr_value_box_t const *value)
Lookup the structure representing an enum value in a fr_dict_attr_t.
Definition dict_util.c:3647
int fr_dict_attr_autoload(fr_dict_attr_autoload_t const *to_load)
Process a dict_attr_autoload element to load/verify a dictionary attribute.
Definition dict_util.c:4393
#define fr_dict_autoload(_to_load)
Definition dict.h:912
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:311
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:4711
char const * name
Enum name.
Definition dict.h:254
static fr_slen_t in
Definition dict.h:882
Specifies an attribute which must be present for the module to function.
Definition dict.h:291
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:304
Value of an enumerated attribute.
Definition dict.h:253
Definition dwarf.c:563
talloc_free(hp)
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:52
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:51
IPv4/6 prefix.
void unlang_interpret_mark_runnable(request_t *request)
Mark a request as resumable.
Definition interpret.c:2008
void unlang_interpret_signal(request_t *request, fr_signal_t action)
Send a signal (usually stop) to a request.
Definition interpret.c:1789
fr_event_list_t * unlang_interpret_event_list(request_t *request)
Get the event list for the current interpreter.
Definition interpret.c:2538
#define UNLANG_TOP_FRAME
Definition interpret.h:36
rlm_rcode_t unlang_interpret_synchronous(fr_event_list_t *el, request_t *request)
Execute an unlang section synchronously.
int server_init(CONF_SECTION *cs, char const *conf_dir, fr_dict_t *dict)
Initialize src/lib/server/.
Definition base.c:42
void server_free(void)
Free src/lib/server/.
Definition base.c:137
Describes a host allowed to send packets to the server.
Definition client.h:80
void vlog_request(fr_log_type_t type, fr_log_lvl_t lvl, request_t *request, char const *file, int line, char const *fmt, va_list ap, void *uctx)
Send a log message to its destination, possibly including fields from the request.
Definition log.c:293
#define PERROR(_fmt,...)
Definition log.h:233
#define RPERROR(fmt,...)
Definition log.h:319
Definition log.h:70
int fr_packet_pairs_from_packet(TALLOC_CTX *ctx, fr_pair_list_t *list, fr_packet_t const *packet)
Allocate a "Net." struct with src/dst host and port.
Definition packet.c:91
void fr_packet_net_from_pairs(fr_packet_t *packet, fr_pair_list_t const *list)
Convert pairs to information in a packet.
Definition packet.c:162
int unlang_global_init(void)
Definition base.c:158
#define fr_time()
Definition event.c:60
unsigned int fr_event_list_reap_signal(fr_event_list_t *el, fr_time_delta_t timeout, int signal)
Send a signal to all the processes we have in our reap list, and reap them.
Definition event.c:1716
Stores all information relating to an event list.
Definition event.c:377
int fr_unlink(char const *filename)
Remove a regular file from the filesystem.
Definition file.c:366
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:322
int fr_debug_lvl
Definition log.c:41
fr_log_t default_log
Definition log.c:306
int fr_log_init_fp(fr_log_t *log, FILE *fp)
Initialise a file logging destination to a FILE*.
Definition log.c:1103
#define fr_log(_log, _lvl, _file, _line, _fmt,...)
Definition log.h:172
@ L_DST_STDOUT
Log to stdout.
Definition log.h:75
@ L_DBG
Only displayed when debugging is enabled.
Definition log.h:56
fr_packet_t * fr_packet_alloc(TALLOC_CTX *ctx, bool new_vector)
Allocate a new fr_packet_t.
Definition packet.c:38
Minimal data structure to use the new code.
Definition listen.h:63
int main_config_free(main_config_t **config)
main_config_t * main_config_alloc(TALLOC_CTX *ctx)
Allocate a main_config_t struct, setting defaults.
void main_config_name_set_default(main_config_t *config, char const *name, bool overwrite_config)
Set the server name.
int main_config_init(main_config_t *config)
void main_config_confdir_set(main_config_t *config, char const *name)
Set the global radius config directory.
void main_config_dict_dir_set(main_config_t *config, char const *name)
Set the global dictionary directory.
Main server configuration.
Definition main_config.h:51
fr_event_list_t * main_loop_event_list(void)
Return the main loop event list.
Definition main_loop.c:166
int main_loop_init(void)
Initialise the main event loop, setting up signal handlers.
Definition main_loop.c:255
void main_loop_free(void)
Definition main_loop.c:191
int map_proc_unregister(char const *name)
Unregister a map processor by name.
Definition map_proc.c:179
int map_proc_register(TALLOC_CTX *ctx, void const *mod_inst, char const *name, map_proc_func_t evaluate, map_proc_instantiate_t instantiate, size_t inst_size, fr_value_box_safe_for_t literals_safe_for)
Register a map processor.
Definition map_proc.c:127
Temporary structure to hold arguments for map calls.
Definition map_proc.h:52
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_TLV
Contains nested attributes.
@ FR_TYPE_UINT32
32 Bit unsigned integer.
@ FR_TYPE_VOID
User data.
long int ssize_t
int modules_rlm_coord_attach(fr_event_list_t *el)
Runs the coord_attach method of all registered backend modules.
Definition module_rlm.c:995
int modules_rlm_free(void)
Cleanup all global structures.
int modules_rlm_init(void)
Initialise the module list structure.
void fr_pair_list_tainted(fr_pair_list_t *list)
Mark up a list of VPs as tainted.
Definition pair.c:3424
int fr_pair_list_copy(TALLOC_CTX *ctx, fr_pair_list_t *to, fr_pair_list_t const *from)
Duplicate a list of pairs.
Definition pair.c:2326
void fr_pair_validate_debug(fr_pair_t const *failed[2])
Write an error to the library errorbuff detailing the mismatch.
Definition pair.c:2096
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:707
int fr_pair_delete_by_da(fr_pair_list_t *list, fr_dict_attr_t const *da)
Delete matching pairs from the specified list.
Definition pair.c:1696
bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pair_list_t *list)
Uses fr_pair_cmp to verify all fr_pair_ts in list match the filter defined by check.
Definition pair.c:2131
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
int fr_pair_list_afrom_file(TALLOC_CTX *ctx, fr_dict_t const *dict, fr_pair_list_t *out, FILE *fp, bool *pfiledone, bool allow_exec)
Read valuepairs from the fp up to End-Of-File.
static const conf_parser_t config[]
Definition base.c:162
#define fr_assert(_expr)
Definition rad_assert.h:37
#define RDEBUG(fmt,...)
#define INFO(fmt,...)
Definition radict.c:63
static bool cleanup
Definition radsniff.c:59
#define RETURN_UNLANG_FAIL
Definition rcode.h:63
@ RLM_MODULE_TIMEOUT
Module (or section) timed out.
Definition rcode.h:56
fr_dict_attr_t const * request_attr_request
Definition request.c:43
int request_global_init(void)
Definition request.c:598
#define request_local_alloc_internal(_ctx, _args)
Allocate a new internal request outside of the request pool.
Definition request.h:344
#define request_local_alloc_external(_ctx, _args)
Allocate a new external request outside of the request pool.
Definition request.h:336
@ REQUEST_ACTIVE
Request is active (running or runnable)
Definition request.h:89
Optional arguments for initialising requests.
Definition request.h:288
static char const * name
ssize_t fr_sbuff_in_escape(fr_sbuff_t *sbuff, char const *in, size_t inlen, fr_sbuff_escape_rules_t const *e_rules)
Print an escaped string to an sbuff.
Definition sbuff.c:1636
char * fr_sbuff_adv_to_chr(fr_sbuff_t *sbuff, size_t len, char c)
Wind position to first instance of specified char.
Definition sbuff.c:2046
ssize_t fr_sbuff_in_sprintf(fr_sbuff_t *sbuff, char const *fmt,...)
Print using a fmt string to an sbuff.
Definition sbuff.c:1611
#define fr_sbuff_adv_past_str_literal(_sbuff, _needle)
#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_is_char(_sbuff_or_marker, _c)
#define SBUFF_CHAR_UNPRINTABLES_EXTENDED
#define fr_sbuff_remaining(_sbuff_or_marker)
#define FR_SBUFF_OUT(_start, _len_or_end)
#define SBUFF_CHAR_UNPRINTABLES_LOW
void fr_schedule_worker_id_set(int id)
Explicitly set the worker id for the current thread.
Definition schedule.c:119
#define pair_update_reply(_attr, _da)
Return or allocate a fr_pair_t in the reply list.
Definition pair.h:129
tmpl_attr_rules_t attr
Rules/data for parsing attribute references.
Definition tmpl.h:339
struct tmpl_rules_s tmpl_rules_t
Definition tmpl.h:233
Optional arguments passed to vp_tmpl functions.
Definition tmpl.h:336
@ FR_SIGNAL_CANCEL
Request has been cancelled.
Definition signal.h:40
fr_client_t * client_afrom_cs(TALLOC_CTX *ctx, CONF_SECTION *cs, CONF_SECTION *server_cs, size_t extra)
Allocate a new client from a config section.
Definition client.c:736
fr_client_t * client_find(fr_client_list_t const *clients, fr_ipaddr_t const *ipaddr, int proto)
Definition client.c:370
bool client_add(fr_client_list_t *clients, fr_client_t *client)
Add a client to a fr_client_list_t.
Definition client.c:182
void modules_init(char const *lib_dir)
Perform global initialisation for modules.
Definition module.c:1952
fr_pair_t * vp
Definition log.h:93
fr_log_dst_t dst
Log destination.
Definition log.h:94
int fd
File descriptor to write messages to.
Definition log.h:109
bool print_level
sometimes we don't want log levels printed
Definition log.h:103
fr_dict_t const * dict_def
Default dictionary to use with unqualified attribute references.
Definition tmpl.h:273
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:546
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:55
int fr_thread_instantiate(TALLOC_CTX *ctx, fr_event_list_t *el)
Instantiate thread-specific data for modules, virtual servers, xlats, unlang, and TLS.
Definition thread.c:165
int fr_time_start(void)
Initialize the local time.
Definition time.c:150
static fr_time_delta_t fr_time_delta_add(fr_time_delta_t a, fr_time_delta_t b)
Definition time.h:255
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_delta_wrap(_time)
Definition time.h:152
static fr_time_t fr_time_add_delta_time(fr_time_delta_t a, fr_time_t b)
Definition time.h:180
static fr_unix_time_t fr_time_to_unix_time(fr_time_t when)
Convert an fr_time_t (internal time) to our version of unix time (wallclock time)
Definition time.h:688
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
void fr_timer_list_set_time_func(fr_timer_list_t *tl, fr_event_time_source_t func)
Override event list time source.
Definition timer.c:1205
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define fr_timer_in(...)
Definition timer.h:87
int fr_openssl_version_consistent(void)
Definition version.c:246
@ T_BARE_WORD
Definition token.h:118
@ T_OP_EQ
Definition token.h:81
@ T_DOUBLE_QUOTED_STRING
Definition token.h:119
static char const * receipt_file
static fr_event_list_t * el
static bool filedone
int main(int argc, char *argv[])
static request_t * request_clone(request_t *old, int number, CONF_SECTION *server_cs)
static fr_dict_attr_t const * attr_packet_type
#define PROTOCOL_NAME
static fr_timer_t * time_advance_timer
static fr_client_t * client_alloc(TALLOC_CTX *ctx, char const *ip, char const *name)
static fr_dict_t const * dict_freeradius
static fr_dict_attr_t const * attr_net
static void pair_mutable(fr_pair_t *vp)
static fr_dict_t const * dict_protocol
fr_dict_attr_autoload_t unit_test_module_dict_attr[]
char const * radiusd_version
static xlat_action_t xlat_func_time_advance(UNUSED TALLOC_CTX *ctx, UNUSED fr_dcursor_t *out, UNUSED xlat_ctx_t const *xctx, UNUSED request_t *request, fr_value_box_list_t *in)
static request_t * request_from_file(TALLOC_CTX *ctx, FILE *fp, fr_client_t *client, CONF_SECTION *server_cs)
static int my_debug_lvl
static xlat_action_t xlat_func_time_advance_resume(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)
static fr_time_delta_t time_offset
#define EXIT_WITH_FAILURE
static xlat_arg_parser_t const xlat_func_time_advance_args[]
static unlang_action_t mod_map_proc(unlang_result_t *p_result, UNUSED map_ctx_t const *mpctx, UNUSED request_t *request, UNUSED fr_value_box_list_t *src, UNUSED map_list_t const *maps)
static int map_proc_verify(CONF_SECTION *cs, UNUSED void const *mod_inst, UNUSED void *proc_inst, tmpl_t const *src, UNUSED map_list_t const *maps)
static void too_much_text(fr_sbuff_t *out, ssize_t slen, fr_sbuff_t *line)
static void print_packet(FILE *fp, fr_packet_t *packet, fr_pair_list_t *list)
fr_dict_autoload_t unit_test_module_dict[]
static fr_time_t _synthetic_time_source(void)
Sythentic time source for tests.
static void time_advance_resume(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
static void cancel_request(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t when, void *uctx)
static bool do_xlats(fr_event_list_t *el, request_t *request, char const *filename, FILE *fp)
static request_t * request_from_internal(TALLOC_CTX *ctx)
static unsigned count
Definition unittest.c:47
xlat_action_t unlang_xlat_yield(request_t *request, xlat_func_t resume, xlat_func_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition xlat.c:543
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.
ssize_t xlat_eval_compiled(char *out, size_t outlen, request_t *request, xlat_exp_head_t const *head, xlat_escape_legacy_t escape, void const *escape_ctx))
Definition xlat_eval.c:1903
static fr_slen_t head
Definition xlat.h:421
#define XLAT_ARGS(_list,...)
Populate local variables with value boxes from the input list.
Definition xlat.h:384
unsigned int required
Argument must be present, and non-empty.
Definition xlat.h:147
int xlat_resolve(xlat_exp_head_t *head, xlat_res_rules_t const *xr_rules)
Walk over an xlat tree recursively, resolving any unresolved functions or references.
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:171
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_YIELD
An xlat function pushed a resume frame onto the stack.
Definition xlat.h:42
@ XLAT_ACTION_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:3178
Definition for a single argument consumed by an xlat function.
Definition xlat.h:146
#define FR_DICTIONARY_FILE
Definition conf.h:6
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:7
unsigned int code
Packet code (type).
Definition packet.h:61
void fr_fprintf_pair_list(FILE *fp, fr_pair_list_t const *list)
Definition pair_print.c:485
bool fr_pair_list_empty(fr_pair_list_t const *list)
Is a valuepair list empty.
#define fr_pair_list_foreach(_list_head, _iter)
Iterate over the contents of a fr_pair_list_t.
Definition pair.h:279
#define fr_pair_list_log(_log, _lvl, _list)
Definition pair.h:864
#define fr_pair_dcursor_init(_cursor, _list)
Initialises a special dcursor with callbacks that will maintain the attr sublists correctly.
Definition pair.h:604
#define fr_pair_dcursor_by_ancestor_init(_cursor, _list, _da)
Initialise a cursor that will return only attributes descended from the specified fr_dict_attr_t.
Definition pair.h:657
static void fr_socket_addr_swap(fr_socket_t *dst, fr_socket_t const *src)
Swap src/dst information of a fr_socket_t.
Definition socket.h:126
Holds information necessary for binding or connecting to a socket.
Definition socket.h:60
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:558
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
#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(_msg)
Definition strerror.h:223
#define fr_type_is_structural(_x)
Definition types.h:392
#define fr_type_is_leaf(_x)
Definition types.h:393
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_VERSION_BUILD(_x)
Create a version string for a utility in the suite of FreeRADIUS utilities.
Definition version.h:58
#define RADIUSD_MAGIC_NUMBER
Definition version.h:81
fr_sbuff_unescape_rules_t const fr_value_unescape_double
Definition value.c:271
#define fr_value_box_alloc(_ctx, _type, _enumv)
Allocate a value box of a specific type.
Definition value.h:644
#define fr_box_time_delta(_val)
Definition value.h:366
#define fr_box_uint32(_val)
Definition value.h:335
int format(printf, 5, 0))
static size_t char ** out
Definition value.h:1030
#define fr_box_date(_val)
Definition value.h:347
fr_dict_t const * virtual_server_dict_by_name(char const *virtual_server)
Return the namespace for the named virtual server.
virtual_server_t const * virtual_server_find(char const *name)
Return virtual server matching the specified name.
CONF_SECTION * virtual_server_cs(virtual_server_t const *vs)
Return the configuration section for a virtual server.
int virtual_servers_init(void)
Performs global initialisation for the virtual server code.
int virtual_servers_free(void)
static TALLOC_CTX * xlat_ctx
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:374
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:225