The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
main_config.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: df29ef603a4afdce6287a38fce8a14cad0b6f2de $
19 *
20 * @brief Handle the the main server's (radiusd) configuration.
21 * @file src/lib/server/main_config.c
22 *
23 * @copyright 2002,2006-2007 The FreeRADIUS server project
24 * @copyright 2002 Alan DeKok (aland@freeradius.org)
25 */
26
27RCSID("$Id: df29ef603a4afdce6287a38fce8a14cad0b6f2de $")
28
29#include <freeradius-devel/server/cf_file.h>
30#include <freeradius-devel/server/client.h>
31#include <freeradius-devel/server/dependency.h>
32#include <freeradius-devel/server/main_config.h>
33#include <freeradius-devel/server/map_proc.h>
34#include <freeradius-devel/server/modpriv.h>
35#include <freeradius-devel/server/util.h>
36#include <freeradius-devel/server/virtual_servers.h>
37
38
39#include <freeradius-devel/util/conf.h>
40#include <freeradius-devel/util/file.h>
41#include <freeradius-devel/util/hw.h>
42#include <freeradius-devel/util/perm.h>
43#include <freeradius-devel/util/sem.h>
44#include <freeradius-devel/util/pair_legacy.h>
45
46#include <freeradius-devel/unlang/xlat_func.h>
47
48
49
50#ifdef HAVE_SYSLOG_H
51# include <syslog.h>
52#endif
53
54#ifdef HAVE_FCNTL_H
55# include <fcntl.h>
56#endif
57
58main_config_t const *main_config; //!< Main server configuration.
59
60extern fr_log_t debug_log;
61
62fr_log_t debug_log = { .fd = -1, .dst = L_DST_NULL };
63
64/**********************************************************************
65 *
66 * We need to figure out where the logs go, before doing anything
67 * else. This is so that the log messages go to the correct
68 * place.
69 *
70 * BUT, we want the settings from the command line to over-ride
71 * the ones in the configuration file. So, these items are
72 * parsed ONLY if there is no "-l foo" on the command line.
73 *
74 **********************************************************************/
75
76static int reverse_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent,CONF_ITEM *ci, conf_parser_t const *rule);
77static int hostname_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
78
79static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
80static int num_workers_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
81static int num_workers_dflt(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, conf_parser_t const *rule);
82
83static int lib_dir_on_read(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
84
85static int talloc_pool_size_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
86
87static int max_request_time_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
88
89static int name_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
90
91/*
92 * Log destinations
93 */
95 { FR_CONF_OFFSET("destination", main_config_t, log_dest), .dflt = "file" },
96 { FR_CONF_OFFSET("syslog_facility", main_config_t, syslog_facility), .dflt = "daemon",
98 .uctx = &(cf_table_parse_ctx_t){
99 .table = syslog_facility_table,
101 }
102 },
103 { FR_CONF_OFFSET("local_state_dir", main_config_t, local_state_dir), .dflt = "${prefix}/var"},
104 { FR_CONF_OFFSET("logdir", main_config_t, log_dir), .dflt = "${local_state_dir}/log"},
105 { FR_CONF_OFFSET("file", main_config_t, log_file), .dflt = "${logdir}/radius.log" },
106 { FR_CONF_OFFSET("suppress_secrets", main_config_t, suppress_secrets), .dflt = "no" },
108};
109
110/*
111 * Basic configuration for the server.
112 */
114 { FR_CONF_POINTER("log", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) initial_log_config },
115
117};
118
119/*
120 * Basic configuration for the server.
121 */
123 { FR_CONF_OFFSET("prefix", main_config_t, prefix), .dflt = "/usr/local" },
124
127
128 { FR_CONF_OFFSET("libdir", main_config_t, lib_dir), .dflt = "${prefix}/lib",
130
132};
133
134/**********************************************************************
135 *
136 * Now that we've parsed the log destination, AND the security
137 * items, we can parse the rest of the configuration items.
138 *
139 **********************************************************************/
140static const conf_parser_t log_config[] = {
141 { FR_CONF_OFFSET("colourise", main_config_t, do_colourise) },
142 { FR_CONF_OFFSET("line_number", main_config_t, log_line_number) },
143 { FR_CONF_OFFSET("timestamp", main_config_t, log_timestamp) },
146};
147
148
149static const conf_parser_t resources[] = {
150 /*
151 * Don't set a default here. It's set in the code, below. This means that
152 * the config item will *not* get printed out in debug mode, so that no one knows
153 * it exists.
154 */
155 { FR_CONF_OFFSET_FLAGS("talloc_memory_report", CONF_FLAG_HIDDEN, main_config_t, talloc_memory_report) }, /* DO NOT SET DEFAULT */
157};
158
159static const conf_parser_t thread_config[] = {
160 { FR_CONF_OFFSET("num_networks", main_config_t, max_networks), .dflt = STRINGIFY(1),
162 { FR_CONF_OFFSET("num_workers", main_config_t, max_workers), .dflt = STRINGIFY(0),
163 .func = num_workers_parse, .dflt_func = num_workers_dflt },
164
165 { FR_CONF_OFFSET_TYPE_FLAGS("stats_interval", FR_TYPE_TIME_DELTA, CONF_FLAG_HIDDEN, main_config_t, stats_interval) },
166
167#ifdef WITH_TLS
168 { FR_CONF_OFFSET_TYPE_FLAGS("openssl_async_pool_init", FR_TYPE_SIZE, 0, main_config_t, openssl_async_pool_init), .dflt = "64" },
169 { FR_CONF_OFFSET_TYPE_FLAGS("openssl_async_pool_max", FR_TYPE_SIZE, 0, main_config_t, openssl_async_pool_max), .dflt = "1024" },
170#endif
171
173};
174
175/*
176 * Migration configuration.
177 */
181
182#ifndef NDEBUG
183/*
184 * Migration configuration.
185 */
187 { FR_CONF_OFFSET_FLAGS("countup_instructions", CONF_FLAG_HIDDEN, main_config_t, ins_countup) },
188 { FR_CONF_OFFSET_FLAGS("max_instructions", CONF_FLAG_HIDDEN, main_config_t, ins_max) },
190};
191#endif
192
197
199 { FR_CONF_OFFSET("max", main_config_t, worker.max_requests), .dflt = "0" },
200 { FR_CONF_OFFSET("timeout", main_config_t, worker.max_request_time), .dflt = STRINGIFY(MAX_REQUEST_TIME), .func = max_request_time_parse },
201 { FR_CONF_OFFSET_TYPE_FLAGS("talloc_pool_size", FR_TYPE_SIZE, CONF_FLAG_HIDDEN, main_config_t, worker.reuse.child_pool_size), .func = talloc_pool_size_parse }, /* DO NOT SET DEFAULT */
202 { FR_CONF_OFFSET_SUBSECTION("reuse", 0, main_config_t, worker.reuse, request_reuse_config) },
204};
205
206static const conf_parser_t server_config[] = {
207 /*
208 * FIXME: 'prefix' is the ONLY one which should be
209 * configured at compile time. Hard-coding it here is
210 * bad. It will be cleaned up once we clean up the
211 * hard-coded defines for the locations of the various
212 * files.
213 */
214 { FR_CONF_OFFSET("prefix", main_config_t, prefix), .dflt = "/usr/local" },
215 { FR_CONF_OFFSET("local_state_dir", main_config_t, local_state_dir), .dflt = "${prefix}/var"},
216 { FR_CONF_OFFSET("sbin_dir", main_config_t, sbin_dir), .dflt = "${prefix}/sbin"},
217 { FR_CONF_OFFSET("logdir", main_config_t, log_dir), .dflt = "${local_state_dir}/log"},
218 { FR_CONF_OFFSET("run_dir", main_config_t, run_dir), .dflt = "${local_state_dir}/run/${name}"},
219 { FR_CONF_OFFSET("radacctdir", main_config_t, radacct_dir), .dflt = "${logdir}/radacct" },
220 { FR_CONF_OFFSET("panic_action", main_config_t, panic_action) },
221 { FR_CONF_OFFSET("reverse_lookups", main_config_t, reverse_lookups), .dflt = "no", .func = reverse_lookups_parse },
222 { FR_CONF_OFFSET("hostname_lookups", main_config_t, hostname_lookups), .dflt = "yes", .func = hostname_lookups_parse },
223 { FR_CONF_OFFSET("pidfile", main_config_t, pid_file), .dflt = "${run_dir}/radiusd.pid"},
224
225 { FR_CONF_OFFSET_FLAGS("debug_level", CONF_FLAG_HIDDEN, main_config_t, debug_level), .dflt = "0" },
226
227 { FR_CONF_POINTER("request", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) request_config },
228
229 { FR_CONF_POINTER("log", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) log_config },
230
231 { FR_CONF_POINTER("resources", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) resources },
232
233 { FR_CONF_POINTER("thread", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) thread_config, .name2 = CF_IDENT_ANY },
234
235 { FR_CONF_POINTER("migrate", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) migrate_config, .name2 = CF_IDENT_ANY },
236
237#ifndef NDEBUG
238 { FR_CONF_POINTER("interpret", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) interpret_config, .name2 = CF_IDENT_ANY },
239#endif
240
241 { FR_CONF_DEPRECATED("max_requests", main_config_t, worker.max_requests) },
242 { FR_CONF_DEPRECATED("max_request_time", main_config_t, worker.max_request_time) },
243
245};
246
247
248/**********************************************************************
249 *
250 * The next few items are here to allow for switching of users
251 * while still opening the various output files with the correct
252 * permission.
253 *
254 * It's rare (or impossible) to have parse errors for these
255 * configuration items, so we don't worry too much about that.
256 * In contrast, when we parse the rest of the configuration, we
257 * CAN get parse errors. We want THOSE parse errors to go to the
258 * log file, and we want the log file to have the correct
259 * permissions.
260 *
261 **********************************************************************/
263#ifdef HAVE_SETUID
265 { FR_CONF_OFFSET_IS_SET("group", FR_TYPE_VOID, 0, main_config_t, gid), .func = cf_parse_gid },
266#endif
267 { FR_CONF_OFFSET("allow_core_dumps", main_config_t, allow_core_dumps), .dflt = "no" },
268
269#ifdef ENABLE_OPENSSL_VERSION_CHECK
270 { FR_CONF_OFFSET("allow_vulnerable_openssl", main_config_t, allow_vulnerable_openssl), .dflt = "no" },
271#endif
272
273#ifdef WITH_TLS
274 { FR_CONF_OFFSET_IS_SET("openssl_fips_mode", FR_TYPE_BOOL, 0, main_config_t, openssl_fips_mode), .dflt = "no" },
275#endif
276
278};
279
281 { FR_CONF_POINTER("security", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) security_config },
282
283 { FR_CONF_OFFSET("name", main_config_t, name), .func = name_parse }, /* DO NOT SET DEFAULT */
284
285 { FR_CONF_OFFSET("prefix", main_config_t, prefix), .dflt = "/usr/local" },
286 { FR_CONF_OFFSET("local_state_dir", main_config_t, local_state_dir), .dflt = "${prefix}/var"},
287
288 { FR_CONF_OFFSET("run_dir", main_config_t, run_dir), .dflt = "${local_state_dir}/run/${name}"},
289
290 /*
291 * For backwards compatibility.
292 */
293#ifdef HAVE_SETUID
294 { FR_CONF_DEPRECATED("user", main_config_t, uid) },
295 { FR_CONF_DEPRECATED("group", main_config_t, gid) },
296#endif
297 { FR_CONF_DEPRECATED("chroot", main_config_t, NULL) },
298 { FR_CONF_DEPRECATED("allow_core_dumps", main_config_t, NULL) },
300};
301
302static int reverse_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent,
303 CONF_ITEM *ci, conf_parser_t const *rule)
304{
305 int ret;
306
307 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
308
309 memcpy(&fr_reverse_lookups, out, sizeof(fr_reverse_lookups));
310
311 return 0;
312}
313
314static int hostname_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent,
315 CONF_ITEM *ci, conf_parser_t const *rule)
316{
317 int ret;
318
319 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
320
322
323 return 0;
324}
325
326static int talloc_pool_size_parse(TALLOC_CTX *ctx, void *out, void *parent,
327 CONF_ITEM *ci, conf_parser_t const *rule)
328{
329 int ret;
330 size_t value;
331
332 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
333
334 memcpy(&value, out, sizeof(value));
335
336 FR_SIZE_BOUND_CHECK("request.talloc_pool_size", value, >=, (size_t)(2 * 1024));
337 FR_SIZE_BOUND_CHECK("request.talloc_pool_size", value, <=, (size_t)(1024 * 1024));
338
339 memcpy(out, &value, sizeof(value));
340
341 return 0;
342}
343
344static int max_request_time_parse(TALLOC_CTX *ctx, void *out, void *parent,
345 CONF_ITEM *ci, conf_parser_t const *rule)
346{
347 int ret;
349
350 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
351
352 memcpy(&value, out, sizeof(value));
353
354 FR_TIME_DELTA_BOUND_CHECK("request.timeout", value, >=, fr_time_delta_from_sec(5));
355 FR_TIME_DELTA_BOUND_CHECK("request.timeout", value, <=, fr_time_delta_from_sec(120));
356
357 memcpy(out, &value, sizeof(value));
358
359 return 0;
360}
361
362static int lib_dir_on_read(UNUSED TALLOC_CTX *ctx, UNUSED void *out, UNUSED void *parent,
363 CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
364{
365 CONF_PAIR *cp = cf_item_to_pair(ci);
366 char const *value;
367
368 fr_assert(main_config != NULL);
369 value = cf_pair_value(cp);
370 if (value) {
372
373 memcpy(&config, &main_config, sizeof(config)); /* const issues */
374
375 config->lib_dir = value;
376 }
377
378 /*
379 * Initialize the DL infrastructure, which is used by the
380 * config file parser. And also add in the search path.
381 */
383 cf_log_perr(ci, "Failed initializing 'lib_dir'");
384 return -1;
385 }
386
387 return 0;
388}
389
390/** Configured server name takes precedence over default values
391 *
392 */
393static int name_parse(TALLOC_CTX *ctx, void *out, void *parent,
394 CONF_ITEM *ci, conf_parser_t const *rule)
395{
397
398 if (*((char **)out)) {
399 if (config->overwrite_config_name) return 0; /* Don't change */
400
401 talloc_free(*((char **)out)); /* Free existing buffer */
402 }
403
404 return cf_pair_parse_value(ctx, out, parent, ci, rule); /* Set new value */
405}
406
407static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent,
408 CONF_ITEM *ci, conf_parser_t const *rule)
409{
410 int ret;
412
413 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
414
415 memcpy(&value, out, sizeof(value));
416
417 FR_INTEGER_BOUND_CHECK("thread.num_networks", value, ==, 1);
418
419 memcpy(out, &value, sizeof(value));
420
421 return 0;
422}
423
424static inline CC_HINT(always_inline)
426{
428
430 if (value == 0) {
431 cf_log_pwarn(parent, "Failed retrieving core count, defaulting to 1 worker");
432 value = 1;
433 }
434
435 /*
436 * If we've got more than four times
437 * the number of cores as we have
438 * networks, then set the number of
439 * workers to the number of cores
440 * minus networks.
441 *
442 * This ensures at a least a 4:1
443 * ratio of workers to networks,
444 * which seems like a sensible ratio.
445 */
446 else if (value > (conf->max_networks * 4)) {
447 value -= conf->max_networks;
448 }
449
450 return value;
451}
452
453static int num_workers_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
454{
455 int ret;
458
459 if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
460
461 memcpy(&value, out, sizeof(value));
462
463 if (value == 0) value = num_workers_auto(conf, ci);
464
465 /*
466 * If no value is specified, try and
467 * discover it automatically.
468 */
469 FR_INTEGER_BOUND_CHECK("thread.num_workers", value, >=, 1);
470 FR_INTEGER_BOUND_CHECK("thread.num_workers", value, <=, 128);
471
472 memcpy(out, &value, sizeof(value));
473
474 return 0;
475}
476
477static int num_workers_dflt(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, conf_parser_t const *rule)
478{
479 char *strvalue;
482
484 strvalue = talloc_asprintf(NULL, "%u", value);
485 *out = cf_pair_alloc(cs, rule->name1, strvalue, T_OP_EQ, T_BARE_WORD, quote);
486 talloc_free(strvalue);
487
488 /*
489 * Otherwise just create as many
490 * workers as we have cores.
491 */
492 cf_log_info(cs, "Dynamically determined thread.workers = %u", value);
493
494 return 0;
495}
496
497static int xlat_config_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
498{
499 static char const disallowed[] = "%{}\\'\"`";
500 size_t outmax = vb->vb_length * 3;
501 size_t outlen = 0;
502 char escaped[outmax + 1];
503 char const *in, *end;
504 char *out = escaped;
505
506 fr_assert(vb->type == FR_TYPE_STRING);
507 in = vb->vb_strvalue;
508 end = in + vb->vb_length;
509
510 do {
511 /*
512 * Non-printable characters get replaced with their
513 * mime-encoded equivalents.
514 */
515 if ((in[0] < 32)) {
516 snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
517 out += 3;
518 outlen += 3;
519 continue;
520 }
521 if (strchr(disallowed, *in) != NULL) {
522 out[0] = '\\';
523 out[1] = *in;
524 out += 2;
525 outlen += 2;
526 continue;
527 }
528 /*
529 * Allowed character.
530 */
531 *out = *in;
532 out++;
533 outlen++;
534 } while (++in < end);
535 *out = '\0';
536
537 /*
538 * If the output length is greater than the input length
539 * something has been escaped - replace the original string
540 */
541 if (outlen > vb->vb_length) {
542 char *outbuff;
543 if (fr_value_box_bstr_realloc(vb, &outbuff, vb, outlen) < 0) return -1;
544 memcpy(outbuff, escaped, outlen);
545 }
546
547 return 0;
548
549}
550
552 { .required = true, .concat = true, .type = FR_TYPE_STRING, .always_escape = true, .func = xlat_config_escape },
554};
555
556/** xlat to get config values
557 *
558@verbatim
559%config(section.subsection.attribute)
560@endverbatim
561 *
562 * @ingroup xlat_functions
563 */
564static xlat_action_t xlat_config(TALLOC_CTX *ctx, fr_dcursor_t *out,
565 UNUSED xlat_ctx_t const *xctx,
566 request_t *request, fr_value_box_list_t *in)
567{
568 char const *value;
569 CONF_PAIR *cp;
570 CONF_ITEM *ci;
571 fr_value_box_t *in_head = fr_value_box_list_head(in);
572 fr_value_box_t *vb;
573
574 ci = cf_reference_item(main_config->root_cs, main_config->root_cs, in_head->vb_strvalue);
575 if (!ci || !cf_item_is_pair(ci)) {
576 RPEDEBUG("Failed finding configuration item '%s'", in_head->vb_strvalue);
577 return XLAT_ACTION_FAIL;
578 }
579
580 cp = cf_item_to_pair(ci);
581
582 value = cf_pair_value(cp);
583 if (!value) return XLAT_ACTION_DONE;
584
585 MEM(vb = fr_value_box_alloc_null(ctx));
586 fr_value_box_bstrndup(vb, vb, NULL, value, strlen(value), false);
588
589 return XLAT_ACTION_DONE;
590}
591
592#ifdef HAVE_SETUID
593static int mkdir_chown(int fd, char const *path, void *uctx)
594{
595 main_config_t *config = uctx;
596 int ret = 0;
597
598 if ((config->server_uid != (uid_t)-1) || (config->server_gid != (gid_t)-1)) {
599 rad_suid_up();
600 ret = fchown(fd, config->server_uid, config->server_gid);
601 if (ret < 0) fr_strerror_printf("Failed changing ownership on directory \"%s\": %s",
602 path, fr_syserror(errno));
604 }
605
606 return ret;
607}
608/*
609 * Switch UID and GID to what is specified in the config file
610 */
611static int switch_users(main_config_t *config, CONF_SECTION *cs)
612{
613 bool do_suid = false;
614 bool do_sgid = false;
615
616 /*
617 * Get the current maximum for core files. Do this
618 * before anything else so as to ensure it's properly
619 * initialized.
620 */
621 if (fr_set_dumpable_init() < 0) {
622 fr_perror("%s", config->name);
623 return -1;
624 }
625
627 fprintf(stderr, "%s: Error: Failed pushing parse rules for user/group information.\n",
628 config->name);
629 return -1;
630 }
631
632 DEBUG("Parsing security rules to bootstrap UID / GID / etc.");
633 if (cf_section_parse(config, config, cs) < 0) {
634 fprintf(stderr, "%s: Error: Failed to parse user/group information.\n",
635 config->name);
636 return -1;
637 }
638
639 /*
640 * Don't do setuid/setgid if we're in debugging
641 * as non-root.
642 */
643 if (DEBUG_ENABLED && (getuid() != 0)) {
644 WARN("Ignoring configured UID / GID as we're running in debug mode");
645 return 0;
646 }
647#ifdef HAVE_GRP_H
648 /*
649 * Get the correct GID for the server.
650 */
651 config->server_gid = getgid();
652 if (config->gid_is_set && (config->server_gid != config->gid)) {
653 config->server_gid = config->gid;
654 do_sgid = true;
655 }
656#endif
657
658 /*
659 * Get the correct UID for the server.
660 */
661 config->server_uid = getuid();
662 if (config->uid_is_set && (config->server_uid != config->uid)) {
663 /*
664 * We're not the correct user. Go set that.
665 */
666 config->server_uid = config->uid;
667 do_suid = true;
668
669#ifdef HAVE_INITGROUPS
670 {
671 struct passwd *user;
672
673 if (fr_perm_getpwuid(config, &user, config->uid) < 0) {
674 fprintf(stderr, "%s: Failed resolving UID %i: %s\n",
675 config->name, (int)config->uid, fr_syserror(errno));
676 return -1;
677 }
678
679 if (initgroups(user->pw_name, config->server_gid) < 0) {
680 fprintf(stderr, "%s: Cannot initialize supplementary group list "
681 "for user %s: %s\n",
682 config->name, user->pw_name, fr_syserror(errno));
683 talloc_free(user);
684 return -1;
685 }
686
687 talloc_free(user);
688 }
689#endif
690 }
691
692 /*
693 * Set the user/group we're going to use
694 * to check read permissions on configuration files.
695 */
696 cf_file_check_set_uid_gid(config->server_uid ? config->server_uid : (uid_t)-1,
697 config->server_gid ? config->server_gid : (gid_t)-1);
698
699#ifdef HAVE_GRP_H
700 /*
701 * Set the GID. Don't bother checking it.
702 */
703 if (do_sgid) {
704 if (setgid(config->server_gid) < 0) {
705 struct group *group;
706
707 if (fr_perm_getgrgid(config, &group, config->gid) < 0) {
708 fprintf(stderr, "%s: Failed resolving GID %i: %s\n",
709 config->name, (int)config->gid, fr_syserror(errno));
710 return -1;
711 }
712
713 fprintf(stderr, "%s: Failed setting group to %s: %s",
714 config->name, group->gr_name, fr_syserror(errno));
715 return -1;
716 }
717 }
718#endif
719
720 /*
721 * The directories for PID files and logs must exist. We
722 * need to create them if we're told to write files to
723 * those directories.
724 *
725 * Because this creation is new in 3.0.9, it's a soft
726 * fail.
727 *
728 */
729 if (config->write_pid) {
730 /*
731 * Control sockets may be accessible by users
732 * other than the freeradius user, so we need
733 * to allow 'other' to traverse the run
734 * directory.
735 *
736 * The freeradius user should be the only one
737 * allowed to write to this directory however.
738 */
739 if (fr_mkdir(NULL, config->run_dir, -1, 0755, mkdir_chown, config) < 0) {
740 WARN("Failed creating run_dir %s: %s", config->run_dir, fr_syserror(errno));
741 }
742 }
743
744 if ((default_log.dst == L_DST_FILES) && config->log_dir) {
745 /*
746 * Every other Linux daemon allows 'other'
747 * to traverse the log directory. That doesn't
748 * mean the actual files should be world
749 * readable.
750 */
751 if (fr_mkdir(NULL, config->log_dir, -1, 0755, mkdir_chown, config) < 0) {
752 WARN("Failed creating log_dir %s: %s", config->log_dir, fr_syserror(errno));
753 }
754 }
755
756 /*
757 * If we don't already have a log file open, open one
758 * now. We may not have been logging anything yet. The
759 * server normally starts up fairly quietly.
760 */
761 if ((default_log.dst == L_DST_FILES) &&
762 (default_log.fd < 0)) {
763 default_log.fd = open(config->log_file,
764 O_WRONLY | O_APPEND | O_CREAT, 0640);
765 if (default_log.fd < 0) {
766 fprintf(stderr, "%s: Failed to open log file %s: %s\n",
767 config->name, config->log_file, fr_syserror(errno));
768 return -1;
769 }
770 }
771
772 /*
773 * If we need to change UID, ensure that the log files
774 * have the correct owner && group.
775 *
776 * We have to do this because some log files MAY already
777 * have been written as root. We need to change them to
778 * have the correct ownership before proceeding.
779 */
780 if ((do_suid || do_sgid) &&
782 if (fchown(default_log.fd, config->server_uid, config->server_gid) < 0) {
783 fprintf(stderr, "%s: Cannot change ownership of log file %s: %s\n",
784 config->name, config->log_file, fr_syserror(errno));
785 return -1;
786 }
787 }
788
789 /*
790 * Once we're done with all of the privileged work,
791 * permanently change the UID.
792 */
793 if (do_suid) {
794 rad_suid_set_down_uid(config->server_uid);
796 }
797
798 return 0;
799}
800#endif /* HAVE_SETUID */
801
802
803/** Set the server name
804 *
805 * @note Will only add pair if one does not already exist
806 *
807 * @param[in] config to alter.
808 * @param[in] name to set e.g. "radiusd".
809 * @param[in] overwrite_config replace any CONF_PAIRs with this value.
810 */
811void main_config_name_set_default(main_config_t *config, char const *name, bool overwrite_config)
812{
813 if (config->name) {
814 char *p;
815
816 memcpy(&p, &config->name, sizeof(p));
817 talloc_free(p);
818 config->name = NULL;
819 }
821
822 config->overwrite_config_name = overwrite_config;
823}
824
825/** Set the global radius config directory.
826 *
827 * @param[in] config to alter.
828 * @param[in] name to set as dir root e.g. /usr/local/etc/raddb.
829 */
831{
832 if (config->raddb_dir) {
833 talloc_const_free(config->raddb_dir);
834 config->raddb_dir = NULL;
835 }
836 if (name) config->raddb_dir = talloc_typed_strdup(config, name);
837}
838
839/** Clean up the semaphore when the main config is freed
840 *
841 * This helps with permissions issues if the user is switching between
842 * running the process under something like systemd and running it under
843 * debug mode.
844 */
846#ifndef HAVE_SEMAPHORES
847 UNUSED
848#endif
849 main_config_t const *config)
850{
851#ifdef HAVE_SEMAPHORES
852 if (config->multi_proc_sem_id >= 0) fr_sem_close(config->multi_proc_sem_id, NULL);
853#endif
854}
855
856/** Increment the semaphore in the child process so that it's not released when the parent exits
857 *
858 * @param[in] config specifying the path to the main config file.
859 * @return
860 * - 0 on success.
861 * - -1 on failure.
862 */
864#ifndef HAVE_SEMAPHORES
865 UNUSED
866#endif
867 main_config_t const *config)
868{
869#ifdef HAVE_SEMAPHORES
870 return fr_sem_take(config->multi_proc_sem_id, config->multi_proc_sem_path, true);
871#else
872 return 0;
873#endif
874}
875
876/** Check to see if we're the only process using this configuration file (or PID file if specified)
877 *
878 * @param[in] config specifying the path to the main config file.
879 * @return
880 * - 1 if another process is running with this config file
881 * - 0 if no other process is running with this config file.
882 * - -1 on error.
883 */
885{
886 char *path;
887
888#ifdef HAVE_SEMAPHORES
889 int sem_id;
890#endif
891
892 int ret = 0;
893 FILE *fp = NULL;
894 static bool sem_initd;
895
896 if (unlikely(sem_initd)) return 0;
897
898 if (config->write_pid) {
899 fr_assert(config->pid_file);
900 fp = fopen(config->pid_file, "w");
901 if (!fp) {
902 fr_strerror_printf("Refusing to start - Failed creating PID file at \"%s\" - %s",
903 config->pid_file, fr_syserror(errno));
904 return -1;
905 }
906 MEM(path = talloc_typed_strdup(config, config->pid_file));
907 } else {
908 MEM(path = talloc_asprintf(config, "%s/%s.conf", config->raddb_dir, config->name));
909 }
910
911#ifdef HAVE_SEMAPHORES
912 sem_id = fr_sem_get(path, 0,
913 main_config->uid_is_set ? main_config->uid : geteuid(),
914 main_config->gid_is_set ? main_config->gid : getegid(),
915 true, false);
916 if (sem_id < 0) {
917 talloc_free(path);
918 ret = -1;
919 goto done;
920 }
921
922 config->multi_proc_sem_id = -1;
923
924 ret = fr_sem_wait(sem_id, path, true, true);
925 switch (ret) {
926 case 0: /* we have the semaphore */
927 talloc_free(config->multi_proc_sem_path); /* Allow this to be called multiple times */
928 config->multi_proc_sem_id = sem_id;
929 config->multi_proc_sem_path = path;
930 sem_initd = true;
931 break;
932
933 case 1: /* another process has the semaphore */
934 {
935 pid_t pid;
936
937 fr_sem_pid(&pid, sem_id);
938 fr_strerror_printf("Refusing to start - PID %u already running with \"%s\"", pid, path);
939 talloc_free(path);
940 }
941 break;
942
943 default:
944 talloc_free(path);
945 break;
946 }
947done:
948#endif
949 if (fp != NULL) fclose(fp);
950
951 return ret;
952}
953
954/** Set the global dictionary directory.
955 *
956 * @param[in] config to alter.
957 * @param[in] name to set as dict dir root e.g. /usr/local/share/freeradius.
958 */
960{
961 if (config->dict_dir) {
962 talloc_const_free(config->dict_dir);
963 config->dict_dir = NULL;
964 }
965 if (name) config->dict_dir = talloc_typed_strdup(config, name);
966}
967
968/** Allocate a main_config_t struct, setting defaults
969 *
970 */
972{
974
975 config = talloc_zero(ctx, main_config_t);
976 if (!config) {
977 fr_strerror_const("Failed allocating main config");
978 return NULL;
979 }
980
981 /*
982 * Set the defaults from compile time arguments
983 * these can be overridden later on the command line.
984 */
987
989
990 return config;
991}
992
993static int _dlhandle_free(void **dl_handle)
994{
995 dlclose(*dl_handle);
996 return 0;
997}
998
999/*
1000 * Read config files.
1001 *
1002 * This function can ONLY be called from the main server process.
1003 */
1005{
1006 char const *p = NULL;
1007 CONF_SECTION *cs = NULL, *subcs;
1008 struct stat statbuf;
1009 bool can_colourise = false;
1010 char buffer[1024];
1011 xlat_t *xlat;
1012
1013 /*
1014 * Initialize the xlats before we load the configuration files,
1015 * so that we can later call xlat_func_register().
1016 */
1018
1019 if (stat(config->raddb_dir, &statbuf) < 0) {
1020 ERROR("Error checking raddb_dir \"%s\": %s", config->raddb_dir, fr_syserror(errno));
1021 return -1;
1022 }
1023
1024#ifdef S_IWOTH
1025 if ((statbuf.st_mode & S_IWOTH) != 0) {
1026 ERROR("Configuration directory %s is globally writable. "
1027 "Refusing to start due to insecure configuration", config->raddb_dir);
1028 return -1;
1029 }
1030#endif
1031
1032#if 0 && defined(S_IROTH)
1033 if (statbuf.st_mode & S_IROTH != 0) {
1034 ERROR("Configuration directory %s is globally readable. "
1035 "Refusing to start due to insecure configuration", config->raddb_dir);
1036 return -1;
1037 }
1038#endif
1039 INFO("Starting - reading configuration files ...");
1040
1041 cs = cf_section_alloc(NULL, NULL, "main", NULL);
1042 if (!cs) return -1;
1043
1044 /*
1045 * Add a 'feature' subsection off the main config
1046 * We check if it's defined first, as the user may
1047 * have defined their own feature flags, or want
1048 * to manually override the ones set by modules
1049 * or the server.
1050 */
1051 subcs = cf_section_find(cs, "feature", NULL);
1052 if (!subcs) {
1053 subcs = cf_section_alloc(cs, cs, "feature", NULL);
1054 if (!subcs) {
1055 failure:
1056 fr_dict_free(&config->dict, __FILE__);
1057 talloc_free(cs);
1058 return -1;
1059 }
1060 }
1062
1064 PERROR("Failed reading internal dictionaries");
1065 goto failure;
1066 }
1067
1068 /*
1069 * Special-case things. If the output is a TTY, AND
1070 * we're debugging, colourise things. This flag also
1071 * removes the "Debug : " prefix from the log messages.
1072 */
1073 p = getenv("TERM");
1074 if (p && isatty(default_log.fd) && strstr(p, "xterm") && fr_debug_lvl) {
1075 can_colourise = default_log.colourise = true;
1076 } else {
1077 can_colourise = default_log.colourise = false;
1078 }
1079 default_log.line_number = config->log_line_number;
1080
1081 /*
1082 * Add a 'version' subsection off the main config
1083 * We check if it's defined first, this is for
1084 * backwards compatibility.
1085 */
1086 subcs = cf_section_find(cs, "version", NULL);
1087 if (!subcs) {
1088 subcs = cf_section_alloc(cs, cs, "version", NULL);
1089 if (!subcs) goto failure;
1090 }
1092
1093 /*
1094 * @todo - not quite done yet... these dictionaries have
1095 * to be loaded from raddb_dir. But the
1096 * fr_dict_autoload_t has a base_dir pointer
1097 * there... it's probably best to pass raddb_dir into
1098 * fr_dict_autoload() and have it use that instead.
1099 *
1100 * Once that's done, the proto_foo dictionaries SHOULD be
1101 * autoloaded, AND loaded before the configuration files
1102 * are read.
1103 *
1104 * And then all of the modules have to be updated to use
1105 * their local dict pointer, instead of NULL.
1106 */
1107 if (cf_section_rules_push(cs, lib_dir_on_read_config) < 0) goto failure;
1108 if (cf_section_rules_push(cs, virtual_servers_on_read_config) < 0) goto failure;
1109
1110 /*
1111 * Track the status of the configuration.
1112 */
1114
1115 /* Read the configuration file */
1116 snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf", config->raddb_dir, config->name);
1117 if (cf_file_read(cs, buffer) < 0) {
1118 ERROR("Error reading or parsing %s", buffer);
1119 goto failure;
1120 }
1121
1122 /*
1123 * Do any fixups here that might be used in references
1124 */
1125 if (config->name) {
1126 CONF_PAIR *cp;
1127
1128 cp = cf_pair_find(cs, "name");
1129 if (cp){
1130 if (config->overwrite_config_name && (cf_pair_replace(cs, cp, config->name) < 0)) {
1131 ERROR("Failed adding/replacing \"name\" config item");
1132 goto failure;
1133 }
1134 } else {
1136 }
1137 }
1138
1139 if (cf_section_pass2(cs) < 0) goto failure;
1140
1141 /*
1142 * Parse environment variables first.
1143 */
1144 subcs = cf_section_find(cs, "ENV", NULL);
1145 if (subcs) {
1146 char const *attr, *value;
1147 CONF_PAIR *cp;
1148 CONF_ITEM *ci;
1149
1150 for (ci = cf_item_next(subcs, NULL);
1151 ci != NULL;
1152 ci = cf_item_next(subcs, ci)) {
1153 if (cf_item_is_data(ci)) continue;
1154
1155 if (!cf_item_is_pair(ci)) {
1156 cf_log_err(ci, "Unexpected item in ENV section");
1157 goto failure;
1158 }
1159
1160 cp = cf_item_to_pair(ci);
1161 if (cf_pair_operator(cp) != T_OP_EQ) {
1162 cf_log_err(ci, "Invalid operator for item in ENV section");
1163 goto failure;
1164 }
1165
1166 attr = cf_pair_attr(cp);
1167 value = cf_pair_value(cp);
1168 if (!value) {
1169 if (unsetenv(attr) < 0) {
1170 cf_log_err(ci, "Failed deleting environment variable %s: %s",
1171 attr, fr_syserror(errno));
1172 goto failure;
1173 }
1174 } else {
1175 void *handle;
1176 void **handle_p;
1177
1178 if (setenv(attr, value, 1) < 0) {
1179 cf_log_err(ci, "Failed setting environment variable %s: %s",
1180 attr, fr_syserror(errno));
1181 goto failure;
1182 }
1183
1184 /*
1185 * Hacks for LD_PRELOAD.
1186 */
1187 if (strcmp(attr, "LD_PRELOAD") != 0) continue;
1188
1189 handle = dlopen(value, RTLD_NOW | RTLD_GLOBAL);
1190 if (!handle) {
1191 cf_log_err(ci, "Failed loading library %s: %s", value, dlerror());
1192 goto failure;
1193 }
1194
1195 /*
1196 * Wrap the pointer, so we can set a destructor.
1197 */
1198 MEM(handle_p = talloc(NULL, void *));
1199 *handle_p = handle;
1200 talloc_set_destructor(handle_p, _dlhandle_free);
1201 (void) cf_data_add(subcs, handle_p, value, true);
1202 }
1203 } /* loop over pairs in ENV */
1204 } /* there's an ENV subsection */
1205
1206 /*
1207 * Parse log section of main config.
1208 */
1210 fprintf(stderr, "%s: Error: Failed pushing rules for log {} section.\n",
1211 config->name);
1212 goto failure;
1213 }
1214
1215 DEBUG("Parsing initial logging configuration.");
1216 if (cf_section_parse(config, config, cs) < 0) {
1217 fprintf(stderr, "%s: Error: Failed to parse log{} section.\n",
1218 config->name);
1219 goto failure;
1220 }
1221
1222 /*
1223 * If there was no log destination set on the command line,
1224 * set it now.
1225 */
1226 if (default_log.dst == L_DST_NULL) {
1227 if (!config->log_dest) {
1228 fprintf(stderr, "%s: Error: No log destination specified.\n",
1229 config->name);
1230 goto failure;
1231 }
1232
1234
1235 switch (default_log.dst) {
1236 case L_DST_NUM_DEST:
1237 fprintf(stderr, "%s: Error: Unknown log_destination %s\n",
1238 config->name, config->log_dest);
1239 goto failure;
1240
1241#ifdef HAVE_SYSLOG_H
1242 case L_DST_SYSLOG:
1243 /*
1244 * Call openlog only once, when the
1245 * program starts.
1246 */
1247 openlog(config->name, LOG_PID, config->syslog_facility);
1248 break;
1249#endif
1250
1251 case L_DST_FILES:
1252 if (!config->log_file) {
1253 fprintf(stderr, "%s: Error: Specified \"files\" as a log destination, but no log filename was given!\n",
1254 config->name);
1255 goto failure;
1256 }
1257 default_log.file = config->log_file;
1258 break;
1259
1260 default:
1261 break;
1262 }
1263 }
1264
1265 /*
1266 * Only set timestamp logging from the config file if no value was
1267 * specified on the command line.
1268 */
1269 if (config->log_timestamp_is_set && (default_log.timestamp == L_TIMESTAMP_AUTO)) {
1271 }
1272
1273#ifdef HAVE_SETUID
1274 /*
1275 * Switch users as early as possible.
1276 */
1277 if (switch_users(config, cs) < 0) goto failure;
1278#endif
1279
1280 /*
1281 * This also clears the dumpable flag if core dumps
1282 * aren't allowed.
1283 */
1284 if (fr_set_dumpable(config->allow_core_dumps) < 0) PERROR("Failed enabling core dumps");
1285 if (config->allow_core_dumps) INFO("Core dumps are enabled");
1286
1287 /*
1288 * This allows us to figure out where, relative to
1289 * radiusd.conf, the other configuration files exist.
1290 */
1291 if (cf_section_rules_push(cs, server_config) < 0) goto failure;
1292 if (cf_section_rules_push(cs, virtual_servers_config) < 0) goto failure;
1293
1294 DEBUG("Parsing main configuration");
1295 if (cf_section_parse(config, config, cs) < 0) goto failure;
1296
1297 /*
1298 * Reset the colourisation state. The configuration
1299 * files can disable colourisation if the terminal
1300 * supports it. The configuration files *cannot* enable
1301 * colourisation if the terminal window doesn't support
1302 * it.
1303 */
1304 if (can_colourise && !config->do_colourise) {
1305 default_log.colourise = false;
1306 }
1307
1308 /*
1309 * Starting the server, WITHOUT "-x" on the
1310 * command-line: use whatever is in the config
1311 * file.
1312 */
1313 if (fr_debug_lvl == 0) fr_debug_lvl = config->debug_level;
1314
1315 INFO("Switching to configured log settings");
1316
1317 /*
1318 * Free the old configuration items, and replace them
1319 * with the new ones.
1320 *
1321 * Note that where possible, we do atomic switch-overs,
1322 * to ensure that the pointers are always valid.
1323 */
1324 fr_assert(config->root_cs == NULL);
1325
1326 /*
1327 * Redirect stderr/stdout as appropriate.
1328 */
1329 if (log_global_init(&default_log, config->daemonize) < 0) {
1330 cf_log_err(cs, "Failed initializing logging");
1331 goto failure;
1332 }
1333
1334 /*
1335 * Load different logging destinations.
1336 */
1337 for (subcs = cf_section_find_next(cs, NULL, "log", CF_IDENT_ANY);
1338 subcs != NULL;
1339 subcs = cf_section_find_next(cs, subcs, "log", CF_IDENT_ANY)) {
1340 if (!cf_section_name2(subcs)) continue;
1341
1342 if (log_parse_section(subcs) < 0) {
1343 cf_log_err(subcs, "Failed parsing log section: %s", fr_strerror());
1344 goto failure;
1345 }
1346 }
1347
1348 DEBUG2("%s: #### Loading Clients ####", config->name);
1349 if (!client_list_parse_section(cs, 0, false)) goto failure;
1350
1351 /*
1352 * Register the %config(section.subsection) xlat function.
1353 */
1354 if (unlikely((xlat = xlat_func_register(NULL, "config", xlat_config, FR_TYPE_STRING)) == NULL)) goto failure;
1357
1358 config->root_cs = cs; /* Do this last to avoid dangling pointers on error */
1359
1360 /* Clear any unprocessed configuration errors */
1362
1363 return 0;
1364}
1365
1366/*
1367 * Free the configuration. Called only when the server is exiting.
1368 */
1370{
1371 /*
1372 * Frees request specific logging resources which is OK
1373 * because all the requests will have been stopped.
1374 */
1376
1377 /*
1378 * Clean up the configuration data
1379 * structures.
1380 */
1382
1383 /*
1384 * Frees current config and any previous configs.
1385 */
1386 TALLOC_FREE((*config)->root_cs);
1387 fr_dict_free(&(*config)->dict, __FILE__);
1388 TALLOC_FREE(*config);
1389
1390 return 0;
1391}
1392
1394{
1395 int fd, old_fd;
1396
1397 if (default_log.dst != L_DST_FILES) return;
1398
1399 fd = open(config->log_file, O_WRONLY | O_APPEND | O_CREAT, 0640);
1400 if (fd >= 0) {
1401 /*
1402 * Atomic swap. We'd like to keep the old
1403 * FD around so that callers don't
1404 * suddenly find the FD closed, and the
1405 * writes go nowhere. But that's hard to
1406 * do. So... we have the case where a
1407 * log message *might* be lost on HUP.
1408 */
1409 old_fd = default_log.fd;
1410 default_log.fd = fd;
1411 close(old_fd);
1412 }
1413}
1414
1416{
1417 fr_time_t when;
1418
1419 static fr_time_t last_hup = fr_time_wrap(0);
1420
1421 /*
1422 * Re-open the log file. If we can't, then keep logging
1423 * to the old log file.
1424 *
1425 * The "open log file" code is here rather than in log.c,
1426 * because it makes that function MUCH simpler.
1427 */
1429
1430 /*
1431 * Only check the config files every few seconds.
1432 */
1433 when = fr_time();
1434 if (fr_time_gteq(fr_time_add(last_hup, fr_time_delta_from_sec(2)), when)) {
1435 INFO("HUP - Last HUP was too recent. Ignoring");
1436 return;
1437 }
1438 last_hup = when;
1439
1440 INFO("HUP - NYI in version 4"); /* Not yet implemented in v4 */
1441}
1442
1446
1447/*
1448 * Migration function that allows for command-line over-ride of
1449 * data structures which need to be initialized before the
1450 * configuration files are loaded.
1451 *
1452 * This should really only be temporary, until we get rid of flat vs nested.
1453 */
1455{
1456 fr_value_box_t box;
1457 size_t offset;
1458 char const *p;
1459 bool *out;
1460
1461 p = strchr(value, '=');
1462 if (!p) return -1;
1463
1465 if (offset) {
1466 out = (bool *) (((uintptr_t) main_config) + offset);
1467
1468 } else {
1469 return -1;
1470 }
1471
1472 p += 1;
1473
1474 fr_value_box_init(&box, FR_TYPE_BOOL, NULL, false);
1475 if (fr_value_box_from_str(NULL, &box, FR_TYPE_BOOL, NULL,
1476 p, strlen(p), NULL) < 0) {
1477 fr_perror("Invalid boolean \"%s\"", p);
1478 fr_exit(1);
1479 }
1480
1481 *out = box.vb_bool;
1482
1483 return 0;
1484}
1485
1486/*
1487 * Allow other pieces of the code to examine the migration options.
1488 */
1490{
1491 size_t offset;
1492
1493 if (!main_config) return false;
1494
1495 offset = fr_table_value_by_substr(config_arg_table, name, strlen(name), 0);
1496 if (!offset) return false;
1497
1498 return *(bool *) (((uintptr_t) main_config) + offset);
1499}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:485
#define STRINGIFY(x)
Definition build.h:197
#define unlikely(_x)
Definition build.h:383
#define UNUSED
Definition build.h:317
#define NUM_ELEMENTS(_t)
Definition build.h:339
void cf_md5_init(void)
Definition cf_file.c:3222
int cf_file_read(CONF_SECTION *cs, char const *filename)
Definition cf_file.c:3565
int cf_section_pass2(CONF_SECTION *cs)
Definition cf_file.c:969
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
Definition cf_file.c:3745
void cf_file_check_set_uid_gid(uid_t uid, gid_t gid)
Set the euid/egid used when performing file checks.
Definition cf_file.c:671
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1195
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
Definition cf_parse.c:1633
int cf_pair_parse_value(TALLOC_CTX *ctx, void *out, UNUSED void *base, CONF_ITEM *ci, conf_parser_t const *rule)
Parses a CONF_PAIR into a C data type.
Definition cf_parse.c:214
int cf_parse_gid(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
Generic function for resolving GID strings to uid_t values.
Definition cf_parse.c:1700
int cf_parse_uid(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, UNUSED conf_parser_t const *rule)
Generic function for resolving UID strings to uid_t values.
Definition cf_parse.c:1685
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:662
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:616
#define FR_CONF_DEPRECATED(_name, _struct, _field)
conf_parser_t entry which raises an error if a matching CONF_PAIR is found
Definition cf_parse.h:414
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:522
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:284
#define FR_SIZE_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:512
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:694
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:339
char const * name1
Name of the CONF_ITEM to parse.
Definition cf_parse.h:600
#define FR_CONF_OFFSET_IS_SET(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct,...
Definition cf_parse.h:298
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:272
cf_parse_t on_read
Function to call as the item is being read, just after it has been allocated and initialized.
Definition cf_parse.h:619
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:313
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:533
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:428
@ CONF_FLAG_HIDDEN
Used by scripts to omit items from the generated documentation.
Definition cf_parse.h:460
#define FR_CONF_OFFSET_TYPE_FLAGS(_name, _type, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:241
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:599
Common header for all CONF_* types.
Definition cf_priv.h:49
Configuration AVP similar to a fr_pair_t.
Definition cf_priv.h:70
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:101
bool cf_item_is_pair(CONF_ITEM const *ci)
Determine if CONF_ITEM is a CONF_PAIR.
Definition cf_util.c:631
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
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:1266
CONF_SECTION * cf_section_find(CONF_SECTION const *cs, char const *name1, char const *name2)
Find a CONF_SECTION with name1 and optionally name2.
Definition cf_util.c:1027
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:1426
bool cf_item_is_data(CONF_ITEM const *ci)
Determine if CONF_ITEM is CONF_DATA.
Definition cf_util.c:645
fr_token_t cf_pair_operator(CONF_PAIR const *pair)
Return the operator of a pair.
Definition cf_util.c:1595
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
CONF_SECTION * cf_section_find_next(CONF_SECTION const *cs, CONF_SECTION const *prev, char const *name1, char const *name2)
Return the next matching section.
Definition cf_util.c:1048
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1581
int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
Replace pair in a given section with a new pair, of the given value.
Definition cf_util.c:1337
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1565
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:286
#define cf_data_add(_cf, _data, _name, _free)
Definition cf_util.h:252
#define cf_log_info(_cf, _fmt,...)
Definition cf_util.h:288
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:293
#define cf_log_pwarn(_cf, _fmt,...)
Definition cf_util.h:294
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:140
#define CF_IDENT_ANY
Definition cf_util.h:78
static int fr_dcursor_append(fr_dcursor_t *cursor, void *v)
Insert a single item at the end of the list.
Definition dcursor.h:408
static char panic_action[512]
The command to execute when panicking.
Definition debug.c:66
int fr_set_dumpable(bool allow_core_dumps)
Enable or disable core dumps.
Definition debug.c:674
int fr_set_dumpable_init(void)
Get the current maximum for core files.
Definition debug.c:659
#define fr_exit(_x)
Exit, producing a log message in debug builds.
Definition debug.h:220
#define MEM(x)
Definition debug.h:36
void dependency_features_init(CONF_SECTION *cs)
Initialise core feature flags.
Definition dependency.c:183
void dependency_version_numbers_init(CONF_SECTION *cs)
Initialise core version flags.
Definition dependency.c:333
#define ERROR(fmt,...)
Definition dhcpclient.c:41
#define DEBUG(fmt,...)
Definition dhcpclient.c:39
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_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4157
static fr_slen_t in
Definition dict.h:849
Test enumeration values.
Definition dict_test.h:92
#define RTLD_NOW
Definition dl.c:44
dl_module_loader_t * dl_module_loader_init(char const *lib_dir)
Initialise structures needed by the dynamic linker.
Definition dl_module.c:524
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
Definition file.c:219
uint32_t fr_hw_num_cores_active(void)
Definition hw.c:121
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:53
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:52
static bool log_timestamp
Definition log.c:1092
fr_table_num_sorted_t const log_destination_table[]
Definition log.c:186
int log_global_init(fr_log_t *log, bool daemonize)
Initialises the server logging functionality, and the underlying libfreeradius log.
Definition log.c:1265
int log_parse_section(CONF_SECTION *cs)
Parse a named logging section.
Definition log.c:1130
size_t syslog_facility_table_len
Definition log.c:143
fr_table_num_sorted_t const syslog_facility_table[]
Syslog facility table.
Definition log.c:66
void log_global_free(void)
Definition log.c:1301
#define PERROR(_fmt,...)
Definition log.h:228
#define DEBUG_ENABLED
True if global debug level 1 messages are enabled.
Definition log.h:257
#define RPEDEBUG(fmt,...)
Definition log.h:376
void rad_suid_up(void)
Definition util.c:881
void rad_suid_down(void)
Definition util.c:885
void rad_suid_set_down_uid(uid_t uid)
Definition util.c:877
talloc_free(reap)
int fr_debug_lvl
Definition log.c:40
fr_log_t default_log
Definition log.c:288
bool log_dates_utc
Definition log.c:286
@ L_DST_NULL
Discard log messages.
Definition log.h:83
@ L_DST_FILES
Log to a file on disk.
Definition log.h:79
@ L_DST_NUM_DEST
Definition log.h:84
@ L_DST_SYSLOG
Log to syslog.
Definition log.h:80
@ L_TIMESTAMP_ON
Always log timestamps.
Definition log.h:90
@ L_TIMESTAMP_OFF
Never log timestamps.
Definition log.h:91
@ L_TIMESTAMP_AUTO
Timestamp logging preference not specified.
Definition log.h:88
static const conf_parser_t log_config[]
static const conf_parser_t resources[]
int main_config_parse_option(char const *value)
fr_log_t debug_log
Definition main_config.c:62
static size_t config_arg_table_len
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.
static int xlat_config_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
void main_config_raddb_dir_set(main_config_t *config, char const *name)
Set the global radius config directory.
void main_config_exclusive_proc_done(UNUSED main_config_t const *config)
Clean up the semaphore when the main config is freed.
void main_config_hup(main_config_t *config)
static const conf_parser_t thread_config[]
int main_config_exclusive_proc(main_config_t *config)
Check to see if we're the only process using this configuration file (or PID file if specified)
static int lib_dir_on_read(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int num_workers_dflt(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, conf_parser_t const *rule)
void main_config_name_set_default(main_config_t *config, char const *name, bool overwrite_config)
Set the server name.
static int reverse_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static const conf_parser_t switch_users_config[]
static const conf_parser_t server_config[]
static int _dlhandle_free(void **dl_handle)
void hup_logfile(main_config_t *config)
static xlat_arg_parser_t const xlat_config_args[]
static const conf_parser_t security_config[]
static uint32_t num_workers_auto(main_config_t *conf, CONF_ITEM *parent)
static const conf_parser_t initial_log_config[]
Definition main_config.c:94
static fr_table_num_ordered_t config_arg_table[]
static const conf_parser_t request_config[]
static int max_request_time_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static const conf_parser_t migrate_config[]
int main_config_init(main_config_t *config)
static const conf_parser_t initial_server_config[]
static int hostname_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int name_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Configured server name takes precedence over default values.
main_config_t const * main_config
Main server configuration.
Definition main_config.c:58
static const conf_parser_t lib_dir_on_read_config[]
static const conf_parser_t request_reuse_config[]
int main_config_exclusive_proc_child(UNUSED main_config_t const *config)
Increment the semaphore in the child process so that it's not released when the parent exits.
static const conf_parser_t interpret_config[]
static int talloc_pool_size_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int num_workers_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
bool main_config_migrate_option_get(char const *name)
void main_config_dict_dir_set(main_config_t *config, char const *name)
Set the global dictionary directory.
#define MAX_REQUEST_TIME
Default maximum request time.
Definition main_config.h:35
char const * lib_dir
Definition main_config.h:73
CONF_SECTION * root_cs
Root of the server config.
Definition main_config.h:55
Main server configuration.
Definition main_config.h:51
@ FR_TYPE_TIME_DELTA
A period of time measured in nanoseconds.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_VOID
User data.
@ FR_TYPE_BOOL
A truth value.
@ FR_TYPE_SIZE
Unsigned integer capable of representing any memory address on the local system.
unsigned int uint32_t
int fr_perm_getgrgid(TALLOC_CTX *ctx, struct group **out, gid_t gid)
Resolve a gid to a group database entry.
Definition perm.c:329
int fr_perm_getpwuid(TALLOC_CTX *ctx, struct passwd **out, uid_t uid)
Resolve a uid to a passwd entry.
Definition perm.c:203
static const conf_parser_t config[]
Definition base.c:186
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:81
#define DEBUG2(fmt,...)
Definition radclient.h:43
#define WARN(fmt,...)
Definition radclient.h:47
#define INFO(fmt,...)
Definition radict.c:54
static rs_t * conf
Definition radsniff.c:53
static char const * name
static const conf_parser_t xlat_config[]
Definition rlm_rest.c:141
int fr_sem_wait(int sem_id, char const *file, bool undo_on_exit, bool nonblock)
Wait for a semaphore to reach 0, then increment it by 1.
Definition sem.c:252
int fr_sem_close(int sem_id, char const *file)
Remove the semaphore, this helps with permissions issues.
Definition sem.c:329
int fr_sem_pid(pid_t *pid, int sem_id)
Return the PID of the process that last operated on the semaphore.
Definition sem.c:53
int fr_sem_get(char const *file, int proj_id, uid_t uid, gid_t gid, bool check_perm, bool must_exist)
Returns a semid for the semaphore associated with the file.
Definition sem.c:421
int fr_sem_take(int sem_id, char const *file, bool undo_on_exit)
Increment the semaphore by 1.
Definition sem.c:221
#define HAVE_SEMAPHORES
Definition sem.h:32
#define FR_SLAB_CONFIG_CONF_PARSER
conf_parser_t entries to populate user configurable slab values
Definition slab.h:35
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
void client_list_free(void)
Definition client.c:84
fr_client_list_t * client_list_parse_section(CONF_SECTION *section, int proto, TLS_UNUSED bool tls_required)
Definition client.c:469
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
Definition log.h:96
bool colourise
Prefix log messages with VT100 escape codes to change text colour.
Definition log.h:101
fr_log_dst_t dst
Log destination.
Definition log.h:97
bool line_number
Log src file and line number.
Definition log.h:99
int fd
File descriptor to write messages to.
Definition log.h:112
fr_log_timestamp_t timestamp
Prefix log messages with timestamps.
Definition log.h:110
char const * file
Path to log file.
Definition log.h:113
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_table_value_by_str(_table, _name, _def)
Convert a string to a value using a sorted or ordered table.
Definition table.h:653
#define fr_table_value_by_substr(_table, _name, _name_len, _def)
Convert a partial string to a value using an ordered or sorted table.
Definition table.h:693
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:467
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:229
#define fr_time_gteq(_a, _b)
Definition time.h:238
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:590
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
enum fr_token fr_token_t
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
close(uq->fd)
uint8_t required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumend by an xlat function.
Definition xlat.h:145
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:8
static fr_slen_t parent
Definition pair.h:841
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
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
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:5761
int fr_value_box_bstr_realloc(TALLOC_CTX *ctx, char **out, fr_value_box_t *dst, size_t len)
Change the length of a buffer already allocated to a value box.
Definition value.c:4559
int fr_value_box_bstrndup(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_dict_attr_t const *enumv, char const *src, size_t len, bool tainted)
Copy a string to to a fr_value_box_t.
Definition value.c:4603
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:654
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:609
static size_t char ** out
Definition value.h:1023
const conf_parser_t virtual_servers_on_read_config[]
const conf_parser_t virtual_servers_config[]
An xlat calling ctx.
Definition xlat_ctx.h:49
void xlat_func_flags_set(xlat_t *x, xlat_func_flags_t flags)
Specify flags that alter the xlat's behaviour.
Definition xlat_func.c:399
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:363
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function.
Definition xlat_func.c:216
int xlat_func_init(void)
Definition xlat_func.c:547
@ XLAT_FUNC_FLAG_PURE
Definition xlat_func.h:38