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