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