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