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: 02c3c829f7ec0873af33af841ae2a9fbe1d1c61c $
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: 02c3c829f7ec0873af33af841ae2a9fbe1d1c61c $")
28
29#include <freeradius-devel/server/cf_file.h>
30#include <freeradius-devel/server/client.h>
31#include <freeradius-devel/server/dependency.h>
32#include <freeradius-devel/server/main_config.h>
33#include <freeradius-devel/server/map_proc.h>
34#include <freeradius-devel/server/modpriv.h>
35#include <freeradius-devel/server/util.h>
36#include <freeradius-devel/server/virtual_servers.h>
37
38
39#include <freeradius-devel/util/conf.h>
40#include <freeradius-devel/util/file.h>
41#include <freeradius-devel/util/hw.h>
42#include <freeradius-devel/util/perm.h>
43#include <freeradius-devel/util/sem.h>
44#include <freeradius-devel/util/pair_legacy.h>
45
46#include <freeradius-devel/unlang/xlat_func.h>
47
48
49
50#ifdef HAVE_SYSLOG_H
51# include <syslog.h>
52#endif
53
54#ifdef HAVE_FCNTL_H
55# include <fcntl.h>
56#endif
57
58main_config_t const *main_config; //!< Main server configuration.
59
60extern fr_log_t debug_log;
61
62fr_log_t debug_log = { .fd = -1, .dst = L_DST_NULL };
63
64/**********************************************************************
65 *
66 * We need to figure out where the logs go, before doing anything
67 * else. This is so that the log messages go to the correct
68 * place.
69 *
70 * BUT, we want the settings from the command line to over-ride
71 * the ones in the configuration file. So, these items are
72 * parsed ONLY if there is no "-l foo" on the command line.
73 *
74 **********************************************************************/
75
76static int reverse_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent,CONF_ITEM *ci, conf_parser_t const *rule);
77static int hostname_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
78
79static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
80static int num_workers_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
81static int num_workers_dflt(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, conf_parser_t const *rule);
82
83static int lib_dir_on_read(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
84
85static int talloc_pool_size_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
86
87static int max_request_time_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
88
89static int name_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule);
90
91/*
92 * Log destinations
93 */
95 { FR_CONF_OFFSET("destination", main_config_t, log_dest), .dflt = "file" },
96 { FR_CONF_OFFSET("syslog_facility", main_config_t, syslog_facility), .dflt = "daemon",
98 .uctx = &(cf_table_parse_ctx_t){
99 .table = syslog_facility_table,
101 }
102 },
103 { FR_CONF_OFFSET("local_state_dir", main_config_t, local_state_dir), .dflt = "${prefix}/var"},
104 { FR_CONF_OFFSET("logdir", main_config_t, log_dir), .dflt = "${local_state_dir}/log"},
105 { FR_CONF_OFFSET("file", main_config_t, log_file), .dflt = "${logdir}/radius.log" },
106 { FR_CONF_OFFSET("suppress_secrets", main_config_t, suppress_secrets), .dflt = "no" },
108};
109
110/*
111 * Basic configuration for the server.
112 */
114 { FR_CONF_POINTER("log", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) initial_log_config },
115
117};
118
119/*
120 * Basic configuration for the server.
121 */
123 { FR_CONF_OFFSET("prefix", main_config_t, prefix), .dflt = "/usr/local" },
124
127
128 { FR_CONF_OFFSET("libdir", main_config_t, lib_dir), .dflt = "${prefix}/lib",
130
132};
133
134/**********************************************************************
135 *
136 * Now that we've parsed the log destination, AND the security
137 * items, we can parse the rest of the configuration items.
138 *
139 **********************************************************************/
140static const conf_parser_t log_config[] = {
141 { FR_CONF_OFFSET("colourise", main_config_t, do_colourise) },
142 { FR_CONF_OFFSET("line_number", main_config_t, log_line_number) },
143 { FR_CONF_OFFSET("timestamp", main_config_t, log_timestamp) },
146};
147
148
149static const conf_parser_t resources[] = {
150 /*
151 * Don't set a default here. It's set in the code, below. This means that
152 * the config item will *not* get printed out in debug mode, so that no one knows
153 * it exists.
154 */
155 { FR_CONF_OFFSET_FLAGS("talloc_memory_report", CONF_FLAG_HIDDEN, main_config_t, talloc_memory_report) }, /* DO NOT SET DEFAULT */
157};
158
159static const conf_parser_t thread_config[] = {
160 { FR_CONF_OFFSET("num_networks", main_config_t, max_networks), .dflt = STRINGIFY(1),
162 { FR_CONF_OFFSET("num_workers", main_config_t, max_workers), .dflt = STRINGIFY(0),
163 .func = num_workers_parse, .dflt_func = num_workers_dflt },
164
165 { FR_CONF_OFFSET_TYPE_FLAGS("stats_interval", FR_TYPE_TIME_DELTA, CONF_FLAG_HIDDEN, main_config_t, stats_interval) },
166
167#ifdef WITH_TLS
168 { FR_CONF_OFFSET_TYPE_FLAGS("openssl_async_pool_init", FR_TYPE_SIZE, 0, main_config_t, openssl_async_pool_init), .dflt = "64" },
169 { FR_CONF_OFFSET_TYPE_FLAGS("openssl_async_pool_max", FR_TYPE_SIZE, 0, main_config_t, openssl_async_pool_max), .dflt = "1024" },
170#endif
171
173};
174
175/*
176 * Migration configuration.
177 */
181
182#ifndef NDEBUG
183/*
184 * Migration configuration.
185 */
187 { FR_CONF_OFFSET_FLAGS("countup_instructions", CONF_FLAG_HIDDEN, main_config_t, ins_countup) },
188 { FR_CONF_OFFSET_FLAGS("max_instructions", CONF_FLAG_HIDDEN, main_config_t, ins_max) },
190};
191#endif
192
197
199 { FR_CONF_OFFSET("max", main_config_t, worker.max_requests), .dflt = "0" },
200 { FR_CONF_OFFSET("timeout", main_config_t, worker.max_request_time), .dflt = STRINGIFY(MAX_REQUEST_TIME), .func = max_request_time_parse },
201 { FR_CONF_OFFSET_TYPE_FLAGS("talloc_pool_size", FR_TYPE_SIZE, CONF_FLAG_HIDDEN, main_config_t, worker.reuse.child_pool_size), .func = talloc_pool_size_parse }, /* DO NOT SET DEFAULT */
202 { FR_CONF_OFFSET_SUBSECTION("reuse", 0, main_config_t, worker.reuse, request_reuse_config) },
204};
205
206static const conf_parser_t server_config[] = {
207 /*
208 * FIXME: 'prefix' is the ONLY one which should be
209 * configured at compile time. Hard-coding it here is
210 * bad. It will be cleaned up once we clean up the
211 * hard-coded defines for the locations of the various
212 * files.
213 */
214 { FR_CONF_OFFSET("prefix", main_config_t, prefix), .dflt = "/usr/local" },
215 { FR_CONF_OFFSET("local_state_dir", main_config_t, local_state_dir), .dflt = "${prefix}/var"},
216 { FR_CONF_OFFSET("sbin_dir", main_config_t, sbin_dir), .dflt = "${prefix}/sbin"},
217 { FR_CONF_OFFSET("logdir", main_config_t, log_dir), .dflt = "${local_state_dir}/log"},
218 { FR_CONF_OFFSET("run_dir", main_config_t, run_dir), .dflt = "${local_state_dir}/run/${name}"},
219 { FR_CONF_OFFSET("radacctdir", main_config_t, radacct_dir), .dflt = "${logdir}/radacct" },
220 { FR_CONF_OFFSET("panic_action", main_config_t, panic_action) },
221 { FR_CONF_OFFSET("reverse_lookups", main_config_t, reverse_lookups), .dflt = "no", .func = reverse_lookups_parse },
222 { FR_CONF_OFFSET("hostname_lookups", main_config_t, hostname_lookups), .dflt = "yes", .func = hostname_lookups_parse },
223 { FR_CONF_OFFSET("pidfile", main_config_t, pid_file), .dflt = "${run_dir}/radiusd.pid"},
224
225 { FR_CONF_OFFSET_FLAGS("debug_level", CONF_FLAG_HIDDEN, main_config_t, debug_level), .dflt = "0" },
226
227 { FR_CONF_POINTER("request", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) request_config },
228
229 { FR_CONF_POINTER("log", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) log_config },
230
231 { FR_CONF_POINTER("resources", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) resources },
232
233 { FR_CONF_POINTER("thread", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) thread_config, .name2 = CF_IDENT_ANY },
234
235 { FR_CONF_POINTER("migrate", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) migrate_config, .name2 = CF_IDENT_ANY },
236
237#ifndef NDEBUG
238 { FR_CONF_POINTER("interpret", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) interpret_config, .name2 = CF_IDENT_ANY },
239#endif
240
241 { FR_CONF_DEPRECATED("max_requests", main_config_t, worker.max_requests) },
242 { FR_CONF_DEPRECATED("max_request_time", main_config_t, worker.max_request_time) },
243
245};
246
247
248/**********************************************************************
249 *
250 * The next few items are here to allow for switching of users
251 * while still opening the various output files with the correct
252 * permission.
253 *
254 * It's rare (or impossible) to have parse errors for these
255 * configuration items, so we don't worry too much about that.
256 * In contrast, when we parse the rest of the configuration, we
257 * CAN get parse errors. We want THOSE parse errors to go to the
258 * log file, and we want the log file to have the correct
259 * permissions.
260 *
261 **********************************************************************/
263#ifdef HAVE_SETUID
265 { FR_CONF_OFFSET_IS_SET("group", FR_TYPE_VOID, 0, main_config_t, gid), .func = cf_parse_gid },
266#endif
267 { FR_CONF_OFFSET("allow_core_dumps", main_config_t, allow_core_dumps), .dflt = "no" },
268
269#ifdef ENABLE_OPENSSL_VERSION_CHECK
270 { FR_CONF_OFFSET("allow_vulnerable_openssl", main_config_t, allow_vulnerable_openssl), .dflt = "no" },
271#endif
272
273#ifdef WITH_TLS
274 { FR_CONF_OFFSET_IS_SET("openssl_fips_mode", FR_TYPE_BOOL, 0, main_config_t, openssl_fips_mode), .dflt = "no" },
275#endif
276
277 { FR_CONF_OFFSET_IS_SET("chdir", FR_TYPE_STRING, 0, main_config_t, chdir), },
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("request.talloc_pool_size", value, >=, (size_t)(2 * 1024));
339 FR_SIZE_BOUND_CHECK("request.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("request.timeout", value, >=, fr_time_delta_from_sec(5));
357 FR_TIME_DELTA_BOUND_CHECK("request.timeout", 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 RPEDEBUG("Failed finding configuration item '%s'", in_head->vb_strvalue);
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 * Switch UID and GID to what is specified in the config file
612 */
613static int switch_users(main_config_t *config, CONF_SECTION *cs)
614{
615 bool do_suid = false;
616 bool do_sgid = false;
617
618 /*
619 * Get the current maximum for core files. Do this
620 * before anything else so as to ensure it's properly
621 * initialized.
622 */
623 if (fr_set_dumpable_init() < 0) {
624 fr_perror("%s", config->name);
625 return -1;
626 }
627
629 fprintf(stderr, "%s: Error: Failed pushing parse rules for user/group information.\n",
630 config->name);
631 return -1;
632 }
633
634 DEBUG("Parsing security rules to bootstrap UID / GID / etc.");
635 if (cf_section_parse(config, config, cs) < 0) {
636 fprintf(stderr, "%s: Error: Failed to parse user/group information.\n",
637 config->name);
638 return -1;
639 }
640
641 /*
642 * Don't do setuid/setgid if we're in debugging
643 * as non-root.
644 */
645 if (DEBUG_ENABLED && (getuid() != 0)) {
646 WARN("Ignoring configured UID / GID as we're running in debug mode");
647 return 0;
648 }
649#ifdef HAVE_GRP_H
650 /*
651 * Get the correct GID for the server.
652 */
653 config->server_gid = getgid();
654 if (config->gid_is_set && (config->server_gid != config->gid)) {
655 config->server_gid = config->gid;
656 do_sgid = true;
657 }
658#endif
659
660 /*
661 * Get the correct UID for the server.
662 */
663 config->server_uid = getuid();
664 if (config->uid_is_set && (config->server_uid != config->uid)) {
665 /*
666 * We're not the correct user. Go set that.
667 */
668 config->server_uid = config->uid;
669 do_suid = true;
670
671#ifdef HAVE_INITGROUPS
672 {
673 struct passwd *user;
674
675 if (fr_perm_getpwuid(config, &user, config->uid) < 0) {
676 fprintf(stderr, "%s: Failed resolving UID %i: %s\n",
677 config->name, (int)config->uid, fr_syserror(errno));
678 return -1;
679 }
680
681 if (initgroups(user->pw_name, config->server_gid) < 0) {
682 fprintf(stderr, "%s: Cannot initialize supplementary group list "
683 "for user %s: %s\n",
684 config->name, user->pw_name, fr_syserror(errno));
685 talloc_free(user);
686 return -1;
687 }
688
689 talloc_free(user);
690 }
691#endif
692 }
693
694 /*
695 * Set the user/group we're going to use
696 * to check read permissions on configuration files.
697 */
698 cf_file_check_set_uid_gid(config->server_uid ? config->server_uid : (uid_t)-1,
699 config->server_gid ? config->server_gid : (gid_t)-1);
700
701#ifdef HAVE_GRP_H
702 /*
703 * Set the GID. Don't bother checking it.
704 */
705 if (do_sgid) {
706 if (setgid(config->server_gid) < 0) {
707 struct group *group;
708
709 if (fr_perm_getgrgid(config, &group, config->gid) < 0) {
710 fprintf(stderr, "%s: Failed resolving GID %i: %s\n",
711 config->name, (int)config->gid, fr_syserror(errno));
712 return -1;
713 }
714
715 fprintf(stderr, "%s: Failed setting group to %s: %s",
716 config->name, group->gr_name, fr_syserror(errno));
717 return -1;
718 }
719 }
720#endif
721
722 /*
723 * The directories for PID files and logs must exist. We
724 * need to create them if we're told to write files to
725 * those directories.
726 *
727 * Because this creation is new in 3.0.9, it's a soft
728 * fail.
729 *
730 */
731 if (config->write_pid) {
732 /*
733 * Control sockets may be accessible by users
734 * other than the freeradius user, so we need
735 * to allow 'other' to traverse the run
736 * directory.
737 *
738 * The freeradius user should be the only one
739 * allowed to write to this directory however.
740 */
741 if (fr_mkdir(NULL, config->run_dir, -1, 0755, mkdir_chown, config) < 0) {
742 WARN("Failed creating run_dir %s: %s", config->run_dir, fr_syserror(errno));
743 }
744 }
745
746 if ((default_log.dst == L_DST_FILES) && config->log_dir) {
747 /*
748 * Every other Linux daemon allows 'other'
749 * to traverse the log directory. That doesn't
750 * mean the actual files should be world
751 * readable.
752 */
753 if (fr_mkdir(NULL, config->log_dir, -1, 0755, mkdir_chown, config) < 0) {
754 WARN("Failed creating log_dir %s: %s", config->log_dir, fr_syserror(errno));
755 }
756 }
757
758 /*
759 * If we don't already have a log file open, open one
760 * now. We may not have been logging anything yet. The
761 * server normally starts up fairly quietly.
762 */
763 if ((default_log.dst == L_DST_FILES) &&
764 (default_log.fd < 0)) {
765 default_log.fd = open(config->log_file,
766 O_WRONLY | O_APPEND | O_CREAT, 0640);
767 if (default_log.fd < 0) {
768 fprintf(stderr, "%s: Failed to open log file %s: %s\n",
769 config->name, config->log_file, fr_syserror(errno));
770 return -1;
771 }
772 }
773
774 /*
775 * If we need to change UID, ensure that the log files
776 * have the correct owner && group.
777 *
778 * We have to do this because some log files MAY already
779 * have been written as root. We need to change them to
780 * have the correct ownership before proceeding.
781 */
782 if ((do_suid || do_sgid) &&
784 if (fchown(default_log.fd, config->server_uid, config->server_gid) < 0) {
785 fprintf(stderr, "%s: Cannot change ownership of log file %s: %s\n",
786 config->name, config->log_file, fr_syserror(errno));
787 return -1;
788 }
789 }
790
791 /*
792 * Once we're done with all of the privileged work,
793 * permanently change the UID.
794 */
795 if (do_suid) {
796 rad_suid_set_down_uid(config->server_uid);
798 }
799
800 return 0;
801}
802#endif /* HAVE_SETUID */
803
804
805/** Set the server name
806 *
807 * @note Will only add pair if one does not already exist
808 *
809 * @param[in] config to alter.
810 * @param[in] name to set e.g. "radiusd".
811 * @param[in] overwrite_config replace any CONF_PAIRs with this value.
812 */
813void main_config_name_set_default(main_config_t *config, char const *name, bool overwrite_config)
814{
815 if (config->name) {
816 char *p;
817
818 memcpy(&p, &config->name, sizeof(p));
819 talloc_free(p);
820 config->name = NULL;
821 }
823
824 config->overwrite_config_name = overwrite_config;
825}
826
827/** Set the global radius config directory.
828 *
829 * @param[in] config to alter.
830 * @param[in] name to set as dir root e.g. /usr/local/etc/raddb.
831 */
833{
834 if (config->raddb_dir) {
835 talloc_const_free(config->raddb_dir);
836 config->raddb_dir = NULL;
837 }
838 if (name) config->raddb_dir = talloc_typed_strdup(config, name);
839}
840
841/** Clean up the semaphore when the main config is freed
842 *
843 * This helps with permissions issues if the user is switching between
844 * running the process under something like systemd and running it under
845 * debug mode.
846 */
848#ifndef HAVE_SEMAPHORES
849 UNUSED
850#endif
851 main_config_t const *config)
852{
853#ifdef HAVE_SEMAPHORES
854 if (config->multi_proc_sem_id >= 0) fr_sem_close(config->multi_proc_sem_id, NULL);
855#endif
856}
857
858/** Increment the semaphore in the child process so that it's not released when the parent exits
859 *
860 * @param[in] config specifying the path to the main config file.
861 * @return
862 * - 0 on success.
863 * - -1 on failure.
864 */
866#ifndef HAVE_SEMAPHORES
867 UNUSED
868#endif
869 main_config_t const *config)
870{
871#ifdef HAVE_SEMAPHORES
872 return fr_sem_take(config->multi_proc_sem_id, config->multi_proc_sem_path, true);
873#else
874 return 0;
875#endif
876}
877
878/** Check to see if we're the only process using this configuration file (or PID file if specified)
879 *
880 * @param[in] config specifying the path to the main config file.
881 * @return
882 * - 1 if another process is running with this config file
883 * - 0 if no other process is running with this config file.
884 * - -1 on error.
885 */
887{
888 char *path;
889
890#ifdef HAVE_SEMAPHORES
891 int sem_id;
892#endif
893
894 int ret = 0;
895 FILE *fp = NULL;
896 static bool sem_initd;
897
898 if (unlikely(sem_initd)) return 0;
899
900 if (config->write_pid) {
901 fr_assert(config->pid_file);
902 fp = fopen(config->pid_file, "w");
903 if (!fp) {
904 fr_strerror_printf("Refusing to start - Failed creating PID file at \"%s\" - %s",
905 config->pid_file, fr_syserror(errno));
906 return -1;
907 }
908 MEM(path = talloc_typed_strdup(config, config->pid_file));
909 } else {
910 MEM(path = talloc_asprintf(config, "%s/%s.conf", config->raddb_dir, config->name));
911 }
912
913#ifdef HAVE_SEMAPHORES
914 sem_id = fr_sem_get(path, 0,
915 main_config->uid_is_set ? main_config->uid : geteuid(),
916 main_config->gid_is_set ? main_config->gid : getegid(),
917 true, false);
918 if (sem_id < 0) {
919 talloc_free(path);
920 ret = -1;
921 goto done;
922 }
923
924 config->multi_proc_sem_id = -1;
925
926 ret = fr_sem_wait(sem_id, path, true, true);
927 switch (ret) {
928 case 0: /* we have the semaphore */
929 talloc_free(config->multi_proc_sem_path); /* Allow this to be called multiple times */
930 config->multi_proc_sem_id = sem_id;
931 config->multi_proc_sem_path = path;
932 sem_initd = true;
933 break;
934
935 case 1: /* another process has the semaphore */
936 {
937 pid_t pid;
938
939 fr_sem_pid(&pid, sem_id);
940 fr_strerror_printf("Refusing to start - PID %u already running with \"%s\"", pid, path);
941 talloc_free(path);
942 }
943 break;
944
945 default:
946 talloc_free(path);
947 break;
948 }
949done:
950#endif
951 if (fp != NULL) fclose(fp);
952
953 return ret;
954}
955
956/** Set the global dictionary directory.
957 *
958 * @param[in] config to alter.
959 * @param[in] name to set as dict dir root e.g. /usr/local/share/freeradius.
960 */
962{
963 if (config->dict_dir) {
964 talloc_const_free(config->dict_dir);
965 config->dict_dir = NULL;
966 }
967 if (name) config->dict_dir = talloc_typed_strdup(config, name);
968}
969
970/** Allocate a main_config_t struct, setting defaults
971 *
972 */
974{
976
977 config = talloc_zero(ctx, main_config_t);
978 if (!config) {
979 fr_strerror_const("Failed allocating main config");
980 return NULL;
981 }
982
983 /*
984 * Set the defaults from compile time arguments
985 * these can be overridden later on the command line.
986 */
989
991
992 return config;
993}
994
995static int _dlhandle_free(void **dl_handle)
996{
997 dlclose(*dl_handle);
998 return 0;
999}
1000
1001/*
1002 * Read config files.
1003 *
1004 * This function can ONLY be called from the main server process.
1005 */
1007{
1008 char const *p = NULL;
1009 CONF_SECTION *cs = NULL, *subcs;
1010 struct stat statbuf;
1011 bool can_colourise = false;
1012 char buffer[1024];
1013 xlat_t *xlat;
1014
1015 /*
1016 * Initialize the xlats before we load the configuration files,
1017 * so that we can later call xlat_func_register().
1018 */
1020
1021 if (stat(config->raddb_dir, &statbuf) < 0) {
1022 ERROR("Error checking raddb_dir \"%s\": %s", config->raddb_dir, fr_syserror(errno));
1023 return -1;
1024 }
1025
1026#ifdef S_IWOTH
1027 if ((statbuf.st_mode & S_IWOTH) != 0) {
1028 ERROR("Configuration directory %s is globally writable. "
1029 "Refusing to start due to insecure configuration", config->raddb_dir);
1030 return -1;
1031 }
1032#endif
1033
1034#if 0 && defined(S_IROTH)
1035 if (statbuf.st_mode & S_IROTH != 0) {
1036 ERROR("Configuration directory %s is globally readable. "
1037 "Refusing to start due to insecure configuration", config->raddb_dir);
1038 return -1;
1039 }
1040#endif
1041 INFO("Starting - reading configuration files ...");
1042
1043 cs = cf_section_alloc(NULL, NULL, "main", NULL);
1044 if (!cs) return -1;
1045
1046 /*
1047 * Add a 'feature' subsection off the main config
1048 * We check if it's defined first, as the user may
1049 * have defined their own feature flags, or want
1050 * to manually override the ones set by modules
1051 * or the server.
1052 */
1053 subcs = cf_section_find(cs, "feature", NULL);
1054 if (!subcs) {
1055 subcs = cf_section_alloc(cs, cs, "feature", NULL);
1056 if (!subcs) {
1057 failure:
1058 fr_dict_free(&config->dict, __FILE__);
1059 talloc_free(cs);
1060 return -1;
1061 }
1062 }
1064
1066 PERROR("Failed reading internal dictionaries");
1067 goto failure;
1068 }
1069
1070 /*
1071 * Special-case things. If the output is a TTY, AND
1072 * we're debugging, colourise things. This flag also
1073 * removes the "Debug : " prefix from the log messages.
1074 */
1075 p = getenv("TERM");
1076 if (p && isatty(default_log.fd) && strstr(p, "xterm") && fr_debug_lvl) {
1077 can_colourise = default_log.colourise = true;
1078 } else {
1079 can_colourise = default_log.colourise = false;
1080 }
1081 default_log.line_number = config->log_line_number;
1082
1083 /*
1084 * Add a 'version' subsection off the main config
1085 * We check if it's defined first, this is for
1086 * backwards compatibility.
1087 */
1088 subcs = cf_section_find(cs, "version", NULL);
1089 if (!subcs) {
1090 subcs = cf_section_alloc(cs, cs, "version", NULL);
1091 if (!subcs) goto failure;
1092 }
1094
1095 /*
1096 * @todo - not quite done yet... these dictionaries have
1097 * to be loaded from raddb_dir. But the
1098 * fr_dict_autoload_t has a base_dir pointer
1099 * there... it's probably best to pass raddb_dir into
1100 * fr_dict_autoload() and have it use that instead.
1101 *
1102 * Once that's done, the proto_foo dictionaries SHOULD be
1103 * autoloaded, AND loaded before the configuration files
1104 * are read.
1105 *
1106 * And then all of the modules have to be updated to use
1107 * their local dict pointer, instead of NULL.
1108 */
1109 if (cf_section_rules_push(cs, lib_dir_on_read_config) < 0) goto failure;
1110 if (cf_section_rules_push(cs, virtual_servers_on_read_config) < 0) goto failure;
1111
1112 /*
1113 * Track the status of the configuration.
1114 */
1116
1117 /* Read the configuration file */
1118 snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf", config->raddb_dir, config->name);
1119 if (cf_file_read(cs, buffer) < 0) {
1120 ERROR("Error reading or parsing %s", buffer);
1121 goto failure;
1122 }
1123
1124 /*
1125 * Do any fixups here that might be used in references
1126 */
1127 if (config->name) {
1128 CONF_PAIR *cp;
1129
1130 cp = cf_pair_find(cs, "name");
1131 if (cp){
1132 if (config->overwrite_config_name && (cf_pair_replace(cs, cp, config->name) < 0)) {
1133 ERROR("Failed adding/replacing \"name\" config item");
1134 goto failure;
1135 }
1136 } else {
1138 }
1139 }
1140
1141 if (cf_section_pass2(cs) < 0) goto failure;
1142
1143 /*
1144 * Parse environment variables first.
1145 */
1146 subcs = cf_section_find(cs, "ENV", NULL);
1147 if (subcs) {
1148 char const *attr, *value;
1149 CONF_PAIR *cp;
1150 CONF_ITEM *ci;
1151
1152 for (ci = cf_item_next(subcs, NULL);
1153 ci != NULL;
1154 ci = cf_item_next(subcs, ci)) {
1155 if (cf_item_is_data(ci)) continue;
1156
1157 if (!cf_item_is_pair(ci)) {
1158 cf_log_err(ci, "Unexpected item in ENV section");
1159 goto failure;
1160 }
1161
1162 cp = cf_item_to_pair(ci);
1163 if (cf_pair_operator(cp) != T_OP_EQ) {
1164 cf_log_err(ci, "Invalid operator for item in ENV section");
1165 goto failure;
1166 }
1167
1168 attr = cf_pair_attr(cp);
1169 value = cf_pair_value(cp);
1170 if (!value) {
1171 if (unsetenv(attr) < 0) {
1172 cf_log_err(ci, "Failed deleting environment variable %s: %s",
1173 attr, fr_syserror(errno));
1174 goto failure;
1175 }
1176 } else {
1177 void *handle;
1178 void **handle_p;
1179
1180 if (setenv(attr, value, 1) < 0) {
1181 cf_log_err(ci, "Failed setting environment variable %s: %s",
1182 attr, fr_syserror(errno));
1183 goto failure;
1184 }
1185
1186 /*
1187 * Hacks for LD_PRELOAD.
1188 */
1189 if (strcmp(attr, "LD_PRELOAD") != 0) continue;
1190
1191 handle = dlopen(value, RTLD_NOW | RTLD_GLOBAL);
1192 if (!handle) {
1193 cf_log_err(ci, "Failed loading library %s: %s", value, dlerror());
1194 goto failure;
1195 }
1196
1197 /*
1198 * Wrap the pointer, so we can set a destructor.
1199 */
1200 MEM(handle_p = talloc(NULL, void *));
1201 *handle_p = handle;
1202 talloc_set_destructor(handle_p, _dlhandle_free);
1203 (void) cf_data_add(subcs, handle_p, value, true);
1204 }
1205 } /* loop over pairs in ENV */
1206 } /* there's an ENV subsection */
1207
1208 /*
1209 * Parse log section of main config.
1210 */
1212 fprintf(stderr, "%s: Error: Failed pushing rules for log {} section.\n",
1213 config->name);
1214 goto failure;
1215 }
1216
1217 DEBUG("Parsing initial logging configuration.");
1218 if (cf_section_parse(config, config, cs) < 0) {
1219 fprintf(stderr, "%s: Error: Failed to parse log{} section.\n",
1220 config->name);
1221 goto failure;
1222 }
1223
1224 /*
1225 * If there was no log destination set on the command line,
1226 * set it now.
1227 */
1228 if (default_log.dst == L_DST_NULL) {
1229 if (!config->log_dest) {
1230 fprintf(stderr, "%s: Error: No log destination specified.\n",
1231 config->name);
1232 goto failure;
1233 }
1234
1236
1237 switch (default_log.dst) {
1238 case L_DST_NUM_DEST:
1239 fprintf(stderr, "%s: Error: Unknown log_destination %s\n",
1240 config->name, config->log_dest);
1241 goto failure;
1242
1243#ifdef HAVE_SYSLOG_H
1244 case L_DST_SYSLOG:
1245 /*
1246 * Call openlog only once, when the
1247 * program starts.
1248 */
1249 openlog(config->name, LOG_PID, config->syslog_facility);
1250 break;
1251#endif
1252
1253 case L_DST_FILES:
1254 if (!config->log_file) {
1255 fprintf(stderr, "%s: Error: Specified \"files\" as a log destination, but no log filename was given!\n",
1256 config->name);
1257 goto failure;
1258 }
1259 default_log.file = config->log_file;
1260 break;
1261
1262 default:
1263 break;
1264 }
1265 }
1266
1267 /*
1268 * Only set timestamp logging from the config file if no value was
1269 * specified on the command line.
1270 */
1271 if (config->log_timestamp_is_set && (default_log.timestamp == L_TIMESTAMP_AUTO)) {
1273 }
1274
1275#ifdef HAVE_SETUID
1276 /*
1277 * Switch users as early as possible.
1278 */
1279 if (switch_users(config, cs) < 0) goto failure;
1280#endif
1281
1282 /*
1283 * This also clears the dumpable flag if core dumps
1284 * aren't allowed.
1285 */
1286 if (fr_set_dumpable(config->allow_core_dumps) < 0) PERROR("Failed enabling core dumps");
1287 if (config->allow_core_dumps) INFO("Core dumps are enabled");
1288
1289 /*
1290 * This allows us to figure out where, relative to
1291 * radiusd.conf, the other configuration files exist.
1292 */
1293 if (cf_section_rules_push(cs, server_config) < 0) goto failure;
1294 if (cf_section_rules_push(cs, virtual_servers_config) < 0) goto failure;
1295
1296 DEBUG("Parsing main configuration");
1297 if (cf_section_parse(config, config, cs) < 0) goto failure;
1298
1299 /*
1300 * Reset the colourisation state. The configuration
1301 * files can disable colourisation if the terminal
1302 * supports it. The configuration files *cannot* enable
1303 * colourisation if the terminal window doesn't support
1304 * it.
1305 */
1306 if (can_colourise && !config->do_colourise) {
1307 default_log.colourise = false;
1308 }
1309
1310 /*
1311 * Starting the server, WITHOUT "-x" on the
1312 * command-line: use whatever is in the config
1313 * file.
1314 */
1315 if (fr_debug_lvl == 0) fr_debug_lvl = config->debug_level;
1316
1317 INFO("Switching to configured log settings");
1318
1319 /*
1320 * Free the old configuration items, and replace them
1321 * with the new ones.
1322 *
1323 * Note that where possible, we do atomic switch-overs,
1324 * to ensure that the pointers are always valid.
1325 */
1326 fr_assert(config->root_cs == NULL);
1327
1328 /*
1329 * Redirect stderr/stdout as appropriate.
1330 */
1331 if (log_global_init(&default_log, config->daemonize) < 0) {
1332 cf_log_err(cs, "Failed initializing logging");
1333 goto failure;
1334 }
1335
1336 /*
1337 * Load different logging destinations.
1338 */
1339 for (subcs = cf_section_find_next(cs, NULL, "log", CF_IDENT_ANY);
1340 subcs != NULL;
1341 subcs = cf_section_find_next(cs, subcs, "log", CF_IDENT_ANY)) {
1342 if (!cf_section_name2(subcs)) continue;
1343
1344 if (log_parse_section(subcs) < 0) {
1345 cf_log_err(subcs, "Failed parsing log section: %s", fr_strerror());
1346 goto failure;
1347 }
1348 }
1349
1350 DEBUG2("%s: #### Loading Clients ####", config->name);
1351 if (!client_list_parse_section(cs, 0, false)) goto failure;
1352
1353 /*
1354 * Register the %config(section.subsection) xlat function.
1355 */
1356 if (unlikely((xlat = xlat_func_register(NULL, "config", xlat_config, FR_TYPE_STRING)) == NULL)) goto failure;
1359
1360 config->root_cs = cs; /* Do this last to avoid dangling pointers on error */
1361
1362 /* Clear any unprocessed configuration errors */
1364
1365 return 0;
1366}
1367
1368/*
1369 * Free the configuration. Called only when the server is exiting.
1370 */
1372{
1373 /*
1374 * Frees request specific logging resources which is OK
1375 * because all the requests will have been stopped.
1376 */
1378
1379 /*
1380 * Clean up the configuration data
1381 * structures.
1382 */
1384
1385 /*
1386 * Frees current config and any previous configs.
1387 */
1388 TALLOC_FREE((*config)->root_cs);
1389 fr_dict_free(&(*config)->dict, __FILE__);
1390 TALLOC_FREE(*config);
1391
1392 return 0;
1393}
1394
1396{
1397 int fd, old_fd;
1398
1399 if (default_log.dst != L_DST_FILES) return;
1400
1401 fd = open(config->log_file, O_WRONLY | O_APPEND | O_CREAT, 0640);
1402 if (fd >= 0) {
1403 /*
1404 * Atomic swap. We'd like to keep the old
1405 * FD around so that callers don't
1406 * suddenly find the FD closed, and the
1407 * writes go nowhere. But that's hard to
1408 * do. So... we have the case where a
1409 * log message *might* be lost on HUP.
1410 */
1411 old_fd = default_log.fd;
1412 default_log.fd = fd;
1413 close(old_fd);
1414 }
1415}
1416
1418{
1419 fr_time_t when;
1420
1421 static fr_time_t last_hup = fr_time_wrap(0);
1422
1423 /*
1424 * Re-open the log file. If we can't, then keep logging
1425 * to the old log file.
1426 *
1427 * The "open log file" code is here rather than in log.c,
1428 * because it makes that function MUCH simpler.
1429 */
1431
1432 /*
1433 * Only check the config files every few seconds.
1434 */
1435 when = fr_time();
1436 if (fr_time_gteq(fr_time_add(last_hup, fr_time_delta_from_sec(2)), when)) {
1437 INFO("HUP - Last HUP was too recent. Ignoring");
1438 return;
1439 }
1440 last_hup = when;
1441
1442 INFO("HUP - NYI in version 4"); /* Not yet implemented in v4 */
1443}
1444
1448
1449/*
1450 * Migration function that allows for command-line over-ride of
1451 * data structures which need to be initialized before the
1452 * configuration files are loaded.
1453 *
1454 * This should really only be temporary, until we get rid of flat vs nested.
1455 */
1457{
1458 fr_value_box_t box;
1459 size_t offset;
1460 char const *p;
1461 bool *out;
1462
1463 p = strchr(value, '=');
1464 if (!p) return -1;
1465
1467 if (offset) {
1468 out = (bool *) (((uintptr_t) main_config) + offset);
1469
1470 } else {
1471 return -1;
1472 }
1473
1474 p += 1;
1475
1476 fr_value_box_init(&box, FR_TYPE_BOOL, NULL, false);
1477 if (fr_value_box_from_str(NULL, &box, FR_TYPE_BOOL, NULL,
1478 p, strlen(p), NULL) < 0) {
1479 fr_perror("Invalid boolean \"%s\"", p);
1480 fr_exit(1);
1481 }
1482
1483 *out = box.vb_bool;
1484
1485 return 0;
1486}
1487
1488/*
1489 * Allow other pieces of the code to examine the migration options.
1490 */
1492{
1493 size_t offset;
1494
1495 if (!main_config) return false;
1496
1497 offset = fr_table_value_by_substr(config_arg_table, name, strlen(name), 0);
1498 if (!offset) return false;
1499
1500 return *(bool *) (((uintptr_t) main_config) + offset);
1501}
static int const char char buffer[256]
Definition acutest.h:576
#define RCSID(id)
Definition build.h:485
#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:3222
int cf_file_read(CONF_SECTION *cs, char const *filename)
Definition cf_file.c:3565
int cf_section_pass2(CONF_SECTION *cs)
Definition cf_file.c:969
CONF_ITEM * cf_reference_item(CONF_SECTION const *parent_cs, CONF_SECTION const *outer_cs, char const *ptr)
Definition cf_file.c:3745
void cf_file_check_set_uid_gid(uid_t uid, gid_t gid)
Set the euid/egid used when performing file checks.
Definition cf_file.c:671
int cf_section_parse(TALLOC_CTX *ctx, void *base, CONF_SECTION *cs)
Parse a configuration section into user-supplied variables.
Definition cf_parse.c:1195
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:1633
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:214
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:1700
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:1685
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:660
cf_parse_t func
Override default parsing behaviour for the specified type with a custom parsing function.
Definition cf_parse.h:614
#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:412
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:520
#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:283
#define FR_SIZE_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:510
#define cf_section_rules_push(_cs, _rule)
Definition cf_parse.h:692
#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:337
char const * name1
Name of the CONF_ITEM to parse.
Definition cf_parse.h:598
#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:297
#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:271
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:617
#define FR_CONF_OFFSET_SUBSECTION(_name, _flags, _struct, _field, _subcs)
conf_parser_t which populates a sub-struct using a CONF_SECTION
Definition cf_parse.h:312
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:531
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:426
@ CONF_FLAG_HIDDEN
Used by scripts to omit items from the generated documentation.
Definition cf_parse.h:458
#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:597
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:631
char const * cf_section_name2(CONF_SECTION const *cs)
Return the second identifier of a CONF_SECTION.
Definition cf_util.c:1184
CONF_ITEM * cf_section_to_item(CONF_SECTION const *cs)
Cast a CONF_SECTION to a CONF_ITEM.
Definition cf_util.c:737
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:1266
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:1027
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:1426
bool cf_item_is_data(CONF_ITEM const *ci)
Determine if CONF_ITEM is CONF_DATA.
Definition cf_util.c:645
fr_token_t cf_pair_operator(CONF_PAIR const *pair)
Return the operator of a pair.
Definition cf_util.c:1595
CONF_PAIR * cf_item_to_pair(CONF_ITEM const *ci)
Cast a CONF_ITEM to a CONF_PAIR.
Definition cf_util.c:663
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:1048
char const * cf_pair_value(CONF_PAIR const *pair)
Return the value of a CONF_PAIR.
Definition cf_util.c:1581
int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
Replace pair in a given section with a new pair, of the given value.
Definition cf_util.c:1337
char const * cf_pair_attr(CONF_PAIR const *pair)
Return the attr of a CONF_PAIR.
Definition cf_util.c:1565
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:286
#define cf_data_add(_cf, _data, _name, _free)
Definition cf_util.h:252
#define cf_log_info(_cf, _fmt,...)
Definition cf_util.h:288
#define cf_item_next(_parent, _curr)
Definition cf_util.h:92
#define cf_log_perr(_cf, _fmt,...)
Definition cf_util.h:293
#define cf_log_pwarn(_cf, _fmt,...)
Definition cf_util.h:294
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:140
#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:408
static char panic_action[512]
The command to execute when panicking.
Definition debug.c:66
int fr_set_dumpable(bool allow_core_dumps)
Enable or disable core dumps.
Definition debug.c:674
int fr_set_dumpable_init(void)
Get the current maximum for core files.
Definition debug.c:659
#define fr_exit(_x)
Exit, producing a log message in debug builds.
Definition debug.h:220
#define MEM(x)
Definition debug.h:36
void dependency_features_init(CONF_SECTION *cs)
Initialise core feature flags.
Definition dependency.c:183
void dependency_version_numbers_init(CONF_SECTION *cs)
Initialise core version flags.
Definition dependency.c:333
#define ERROR(fmt,...)
Definition dhcpclient.c: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:4161
static fr_slen_t in
Definition dict.h:861
Test enumeration values.
Definition dict_test.h:92
#define RTLD_NOW
Definition dl.c:44
dl_module_loader_t * dl_module_loader_init(char const *lib_dir)
Initialise structures needed by the dynamic linker.
Definition dl_module.c:524
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:121
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:53
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:52
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
#define RPEDEBUG(fmt,...)
Definition log.h:376
void rad_suid_up(void)
Definition util.c:901
void rad_suid_down(void)
Definition util.c:905
void rad_suid_set_down_uid(uid_t uid)
Definition util.c:897
talloc_free(reap)
int fr_debug_lvl
Definition log.c:40
fr_log_t default_log
Definition log.c:288
bool log_dates_utc
Definition log.c:286
@ 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:62
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[]
Definition main_config.c:94
static fr_table_num_ordered_t config_arg_table[]
static const conf_parser_t request_config[]
static int max_request_time_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
static const conf_parser_t migrate_config[]
int main_config_init(main_config_t *config)
static const conf_parser_t initial_server_config[]
static int hostname_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
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:58
static const conf_parser_t lib_dir_on_read_config[]
static const conf_parser_t request_reuse_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:73
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:186
#define fr_assert(_expr)
Definition rad_assert.h:38
static bool done
Definition radclient.c:81
#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:141
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
#define FR_SLAB_CONFIG_CONF_PARSER
conf_parser_t entries to populate user configurable slab values
Definition slab.h:35
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
void client_list_free(void)
Definition client.c:84
fr_client_list_t * client_list_parse_section(CONF_SECTION *section, int proto, TLS_UNUSED bool tls_required)
Definition client.c:469
#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:467
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:229
#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)
uint8_t required
Argument must be present, and non-empty.
Definition xlat.h:146
#define XLAT_ARG_PARSER_TERMINATOR
Definition xlat.h:170
xlat_action_t
Definition xlat.h:37
@ XLAT_ACTION_FAIL
An xlat function failed.
Definition xlat.h:44
@ XLAT_ACTION_DONE
We're done evaluating this level of nesting.
Definition xlat.h:43
Definition for a single argument consumend by an xlat function.
Definition xlat.h:145
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:8
static fr_slen_t parent
Definition pair.h:841
char const * fr_strerror(void)
Get the last library error.
Definition strerror.c:553
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:732
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_const(_msg)
Definition strerror.h:223
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:5754
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:4545
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:4589
#define fr_value_box_alloc_null(_ctx)
Allocate a value box for later use with a value assignment function.
Definition value.h:654
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:609
static size_t char ** out
Definition value.h:1023
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:399
int xlat_func_args_set(xlat_t *x, xlat_arg_parser_t const args[])
Register the arguments of an xlat.
Definition xlat_func.c:363
xlat_t * xlat_func_register(TALLOC_CTX *ctx, char const *name, xlat_func_t func, fr_type_t return_type)
Register an xlat function.
Definition xlat_func.c:216
int xlat_func_init(void)
Definition xlat_func.c:547
@ XLAT_FUNC_FLAG_PURE
Definition xlat_func.h:38