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