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