2 * mainconf.c Handle the server's configuration.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2002,2006-2007 The FreeRADIUS server project
21 * Copyright 2002 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
45 #ifdef HAVE_SYS_PRCTL_H
46 #include <sys/prctl.h>
53 #ifdef HAVE_SYS_STAT_H
61 struct main_config_t mainconfig;
62 char *request_log_file = NULL;
63 char *debug_log_file = NULL;
64 char *debug_condition = NULL;
67 * Temporary local variables for parsing the configuration
72 static gid_t server_gid;
73 static const char *uid_name = NULL;
74 static const char *gid_name = NULL;
76 static const char *chroot_dir = NULL;
77 static int allow_core_dumps = 0;
78 static const char *radlog_dest = NULL;
81 * These are not used anywhere else..
83 static const char *localstatedir = NULL;
84 static const char *prefix = NULL;
85 static char *syslog_facility = NULL;
86 static const FR_NAME_NUMBER str2fac[] = {
97 { "daemon", LOG_DAEMON },
100 { "auth", LOG_AUTH },
106 { "news", LOG_NEWS },
109 { "uucp", LOG_UUCP },
112 { "cron", LOG_CRON },
115 { "authpriv", LOG_AUTHPRIV },
121 { "local0", LOG_LOCAL0 },
124 { "local1", LOG_LOCAL1 },
127 { "local2", LOG_LOCAL2 },
130 { "local3", LOG_LOCAL3 },
133 { "local4", LOG_LOCAL4 },
136 { "local5", LOG_LOCAL5 },
139 { "local6", LOG_LOCAL6 },
142 { "local7", LOG_LOCAL7 },
148 * Security configuration for the server.
150 static const CONF_PARSER security_config[] = {
151 { "max_attributes", PW_TYPE_INTEGER, 0, &fr_max_attributes, Stringify(0) },
152 { "reject_delay", PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
153 { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
154 { NULL, -1, 0, NULL, NULL }
159 * Logging configuration for the server.
161 static const CONF_PARSER logdest_config[] = {
162 { "destination", PW_TYPE_STRING_PTR, 0, &radlog_dest, "files" },
163 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
165 { "file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
166 { "requests", PW_TYPE_STRING_PTR, -1, &request_log_file, NULL },
167 { "debug_file", PW_TYPE_STRING_PTR, -1, &debug_log_file, NULL },
168 { NULL, -1, 0, NULL, NULL }
172 static const CONF_PARSER serverdest_config[] = {
173 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) logdest_config },
174 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, NULL },
175 { "log_destination", PW_TYPE_STRING_PTR, -1, &radlog_dest, NULL },
176 { NULL, -1, 0, NULL, NULL }
180 static const CONF_PARSER log_config_nodest[] = {
181 { "stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
183 { "auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
184 { "auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
185 { "auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
187 { NULL, -1, 0, NULL, NULL }
192 * A mapping of configuration file names to internal variables
194 static const CONF_PARSER server_config[] = {
196 * FIXME: 'prefix' is the ONLY one which should be
197 * configured at compile time. Hard-coding it here is
198 * bad. It will be cleaned up once we clean up the
199 * hard-coded defines for the locations of the various
202 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
203 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
204 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
205 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
206 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
207 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &fr_dns_lookups, "no" },
208 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
209 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
210 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
211 #ifdef DELETE_BLOCKED_REQUESTS
212 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
214 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &allow_core_dumps, "no" },
216 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
217 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
219 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
222 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
224 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config_nodest },
227 * People with old configs will have these. They are listed
228 * AFTER the "log" section, so if they exist in radiusd.conf,
229 * it will prefer "log_foo = bar" to "log { foo = bar }".
230 * They're listed with default values of NULL, so that if they
231 * DON'T exist in radiusd.conf, then the previously parsed
232 * values for "log { foo = bar}" will be used.
234 { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, NULL },
235 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, NULL },
236 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, NULL },
237 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names, NULL },
239 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
241 { NULL, -1, 0, NULL, NULL }
244 #define MAX_ARGV (256)
247 static size_t config_escape_func(char *out, size_t outlen, const char *in)
250 static const char *disallowed = "%{}\\'\"`";
254 * Non-printable characters get replaced with their
255 * mime-encoded equivalents.
258 if (outlen <= 3) break;
260 snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
267 } else if (strchr(disallowed, *in) != NULL) {
268 if (outlen <= 2) break;
280 * Only one byte left.
300 * Xlat for %{config:section.subsection.attribute}
302 static size_t xlat_config(void *instance, REQUEST *request,
303 char *fmt, char *out,
305 RADIUS_ESCAPE_STRING func)
312 request = request; /* -Wunused */
313 instance = instance; /* -Wunused */
318 if (!radius_xlat(buffer, sizeof(buffer), fmt, request, config_escape_func)) {
322 ci = cf_reference_item(request->root->config,
323 request->root->config, buffer);
324 if (!ci || !cf_item_is_pair(ci)) {
329 cp = cf_itemtopair(ci);
332 * Ensure that we only copy what's necessary.
334 * If 'outlen' is too small, then the output is chopped to fit.
336 value = cf_pair_value(cp);
338 if (outlen > strlen(value)) {
339 outlen = strlen(value) + 1;
343 return func(out, outlen, value);
348 * Xlat for %{client:foo}
350 static size_t xlat_client(UNUSED void *instance, REQUEST *request,
351 char *fmt, char *out,
353 UNUSED RADIUS_ESCAPE_STRING func)
355 const char *value = NULL;
358 if (!fmt || !out || (outlen < 1)) return 0;
360 if (!request || !request->client) {
365 cp = cf_pair_find(request->client->cs, fmt);
366 if (!cp || !(value = cf_pair_value(cp))) {
371 strlcpy(out, value, outlen);
377 * Recursively make directories.
379 static int r_mkdir(const char *part)
381 char *ptr, parentdir[500];
384 if (stat(part, &st) == 0)
387 ptr = strrchr(part, FR_DIR_SEP);
392 snprintf(parentdir, (ptr - part)+1, "%s", part);
394 if (r_mkdir(parentdir) != 0)
397 if (mkdir(part, 0770) != 0) {
398 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
407 int did_setuid = FALSE;
410 * Do chroot, if requested.
412 * Switch UID and GID to what is specified in the config file
414 static int switch_users(CONF_SECTION *cs)
418 #ifdef HAVE_SYS_RESOURCE_H
419 struct rlimit core_limits;
423 * Don't do chroot/setuid/setgid if we're in debugging
426 if (debug_flag && (getuid() != 0)) return 1;
430 cp = cf_pair_find(cs, "group");
431 if (cp) gid_name = cf_pair_value(cp);
435 DEBUG2("group = %s", gid_name);
436 gr = getgrnam(gid_name);
438 fprintf(stderr, "%s: Cannot get ID for group %s: %s\n",
439 progname, gid_name, strerror(errno));
442 server_gid = gr->gr_gid;
444 server_gid = getgid();
450 cp = cf_pair_find(cs, "user");
451 if (cp) uid_name = cf_pair_value(cp);
455 DEBUG2("user = %s", uid_name);
456 pw = getpwnam(uid_name);
458 fprintf(stderr, "%s: Cannot get passwd entry for user %s: %s\n",
459 progname, uid_name, strerror(errno));
462 server_uid = pw->pw_uid;
463 #ifdef HAVE_INITGROUPS
464 if (initgroups(uid_name, server_gid) < 0) {
465 fprintf(stderr, "%s: Cannot initialize supplementary group list for user %s: %s\n",
466 progname, uid_name, strerror(errno));
471 server_uid = getuid();
475 cp = cf_pair_find(cs, "chroot");
476 if (cp) chroot_dir = cf_pair_value(cp);
478 DEBUG2("chroot = %s", chroot_dir);
479 if (chroot(chroot_dir) < 0) {
480 fprintf(stderr, "%s: Failed to perform chroot %s: %s",
481 progname, chroot_dir, strerror(errno));
486 * Note that we leave chdir alone. It may be
487 * OUTSIDE of the root. This allows us to read
488 * the configuration from "-d ./etc/raddb", with
489 * the chroot as "./chroot/" for example. After
490 * the server has been loaded, it does a "cd
491 * ${logdir}" below, so that core files (if any)
492 * go to a logging directory.
494 * This also allows the configuration of the
495 * server to be outside of the chroot. If the
496 * server is statically linked, then the only
497 * things needed inside of the chroot are the
498 * logging directories.
500 radlog(L_INFO, "performing chroot to %s\n", chroot_dir);
505 if (gid_name && (setgid(server_gid) < 0)) {
506 fprintf(stderr, "%s: Failed setting group to %s: %s",
507 progname, gid_name, strerror(errno));
515 #ifndef HAVE_SETRESUID
517 * Fake out setresuid with something that's close.
519 #define setresuid(_a, _b, _c) setuid(_b)
522 if (setresuid(-1, server_uid, geteuid()) < 0) {
523 fprintf(stderr, "%s: Failed switching uid: %s\n",
524 progname, strerror(errno));
528 if (geteuid() != server_uid) {
529 fprintf(stderr, "%s: Failed switching uid: UID is incorrect\n",
536 * Now core dumps are disabled on most secure systems.
542 * Double check that we can write to the log directory.
544 * If we can't, don't start, as we can't log any errors!
546 if ((mainconfig.radlog_dest == RADLOG_FILES) &&
547 (mainconfig.log_file != NULL)) {
548 int fd = open(mainconfig.log_file,
549 O_WRONLY | O_APPEND | O_CREAT, 0640);
551 fprintf(stderr, "%s: Cannot write to log file %s: %s\n",
552 progname, mainconfig.log_file, strerror(errno));
558 * After this it's safe to call radlog(), as it's going
559 * to the right place.
563 #ifdef HAVE_SYS_RESOURCE_H
564 /* Get the current maximum for core files. */
565 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
566 radlog(L_ERR, "Failed to get current core limit: %s", strerror(errno));
572 * Core dumps are allowed if we're in debug mode, OR
573 * we've allowed them, OR we did a setuid (which turns
576 * Otherwise, disable core dumps for security.
579 if (!(debug_flag || allow_core_dumps || did_setuid)) {
580 #ifdef HAVE_SYS_RESOURCE_H
581 struct rlimit no_core;
583 no_core.rlim_cur = 0;
584 no_core.rlim_max = 0;
586 if (setrlimit(RLIMIT_CORE, &no_core) < 0) {
587 radlog(L_ERR, "Failed disabling core dumps: %s",
594 * Otherwise, re-enable core dumps if we're
595 * running as a daemon, AND core dumps are
596 * allowed, AND we changed UID's.
598 } else if ((debug_flag == 0) && allow_core_dumps && did_setuid) {
600 * Set the dumpable flag.
602 #ifdef HAVE_SYS_PRCTL_H
603 #ifdef PR_SET_DUMPABLE
604 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
605 radlog(L_ERR,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
612 * Reset the core dump limits again, just to
613 * double check that they haven't changed.
615 #ifdef HAVE_SYS_RESOURCE_H
616 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
617 radlog(L_ERR, "Cannot update core dump limit: %s",
623 radlog(L_INFO, "Core dumps are enabled.");
626 * Else we're debugging (so core dumps are enabled)
627 * OR we're not debugging, AND "allow_core_dumps == FALSE",
628 * OR we're not debugging, AND core dumps are allowed,
629 * BUT we didn't call setuid, so we haven't changed the
630 * core dump capabilities inherited from the parent shell.
638 static const FR_NAME_NUMBER str2dest[] = {
639 { "null", RADLOG_NULL },
640 { "files", RADLOG_FILES },
641 { "syslog", RADLOG_SYSLOG },
642 { "stdout", RADLOG_STDOUT },
643 { "stderr", RADLOG_STDERR },
644 { NULL, RADLOG_NUM_DEST }
651 * This function can ONLY be called from the main server process.
653 int read_mainconfig(int reload)
655 const char *p = NULL;
656 static int old_debug_level = -1;
662 if (stat(radius_dir, &statbuf) < 0) {
663 radlog(L_ERR, "Errors reading %s: %s",
664 radius_dir, strerror(errno));
669 if ((statbuf.st_mode & S_IWOTH) != 0) {
670 radlog(L_ERR, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
677 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
678 radlog(L_ERR, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
685 radlog(L_INFO, "Starting - reading configuration files ...");
687 radlog(L_INFO, "Reloading - reading configuration files...");
690 /* Read the configuration file */
691 snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf",
692 radius_dir, mainconfig.name);
693 if ((cs = cf_file_read(buffer)) == NULL) {
694 radlog(L_ERR, "Errors reading %s", buffer);
699 * If there was no log destination set on the command line,
702 if (mainconfig.radlog_dest == RADLOG_NULL) {
703 if (cf_section_parse(cs, NULL, serverdest_config) < 0) {
704 fprintf(stderr, "radiusd: Error: Failed to parse log{} section.\n");
705 cf_section_free(&cs);
710 fprintf(stderr, "radiusd: Error: No log destination specified.\n");
711 cf_section_free(&cs);
715 mainconfig.radlog_dest = fr_str2int(str2dest, radlog_dest,
717 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
718 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
720 cf_section_free(&cs);
724 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
726 * Make sure syslog_facility isn't NULL
729 if (!syslog_facility) {
730 fprintf(stderr, "radiusd: Error: Syslog chosen but no facility was specified\n");
731 cf_section_free(&cs);
734 mainconfig.syslog_facility = fr_str2int(str2fac, syslog_facility, -1);
735 if (mainconfig.syslog_facility < 0) {
736 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
738 cf_section_free(&cs);
746 * We should really switch users earlier in the process.
748 if (!switch_users(cs)) exit(1);
751 /* Initialize the dictionary */
752 cp = cf_pair_find(cs, "dictionary");
753 if (cp) p = cf_pair_value(cp);
754 if (!p) p = radius_dir;
755 DEBUG2("including dictionary file %s/%s", p, RADIUS_DICTIONARY);
756 if (dict_init(p, RADIUS_DICTIONARY) != 0) {
757 radlog(L_ERR, "Errors reading dictionary: %s",
763 * This allows us to figure out where, relative to
764 * radiusd.conf, the other configuration files exist.
766 cf_section_parse(cs, NULL, server_config);
769 * Free the old configuration items, and replace them
772 * Note that where possible, we do atomic switch-overs,
773 * to ensure that the pointers are always valid.
775 cf_section_free(&mainconfig.config);
776 mainconfig.config = cs;
778 if (!clients_parse_section(cs)) {
782 DEBUG2("%s: #### Loading Realms and Home Servers ####", mainconfig.name);
784 if (!realms_init(cs)) {
789 * Register the %{config:section.subsection} xlat function.
791 xlat_register("config", xlat_config, NULL);
792 xlat_register("client", xlat_client, NULL);
795 * Reload: change debug flag if it's changed in the
796 * configuration file.
799 if (mainconfig.debug_level != old_debug_level) {
800 debug_flag = mainconfig.debug_level;
803 } else if (debug_flag == 0) {
806 * Starting the server, WITHOUT "-x" on the
807 * command-line: use whatever's in the config
810 debug_flag = mainconfig.debug_level;
812 fr_debug_flag = debug_flag;
813 old_debug_level = mainconfig.debug_level;
816 * Go update our behaviour, based on the configuration
821 * Sanity check the configuration for internal
824 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
825 mainconfig.reject_delay = mainconfig.cleanup_delay;
827 if (mainconfig.reject_delay < 0) mainconfig.reject_delay = 0;
829 /* Reload the modules. */
830 if (setup_modules(reload, mainconfig.config) < 0) {
831 radlog(L_ERR, "Errors initializing modules");
836 if (chdir(radlog_dir) < 0) {
837 radlog(L_ERR, "Failed to 'chdir %s' after chroot: %s",
838 radlog_dir, strerror(errno));
847 * Free the configuration. Called only when the server is exiting.
849 int free_mainconfig(void)
852 * Clean up the configuration data
855 cf_section_free(&mainconfig.config);
857 listen_free(&mainconfig.listen);