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/autoconf.h>
32 #ifdef HAVE_NETINET_IN_H
33 #include <netinet/in.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
40 #include <freeradius-devel/radiusd.h>
41 #include <freeradius-devel/rad_assert.h>
42 #include <freeradius-devel/modules.h>
44 #include <sys/resource.h>
50 #ifdef HAVE_SYS_PRTCL_H
51 #include <sys/prctl.h>
58 struct main_config_t mainconfig;
61 * Temporary local variables for parsing the configuration
64 static uid_t server_uid;
65 static gid_t server_gid;
68 * These are not used anywhere else..
70 static const char *localstatedir = NULL;
71 static const char *prefix = NULL;
72 static char *syslog_facility = NULL;
73 static const LRAD_NAME_NUMBER str2fac[] = {
84 { "daemon", LOG_DAEMON },
102 { "authpriv", LOG_AUTHPRIV },
108 { "local0", LOG_LOCAL0 },
111 { "local1", LOG_LOCAL1 },
114 { "local2", LOG_LOCAL2 },
117 { "local3", LOG_LOCAL3 },
120 { "local4", LOG_LOCAL4 },
123 { "local5", LOG_LOCAL5 },
126 { "local6", LOG_LOCAL6 },
129 { "local7", LOG_LOCAL7 },
135 * Map the proxy server configuration parameters to variables.
137 static const CONF_PARSER proxy_config[] = {
138 { "retry_delay", PW_TYPE_INTEGER, 0, &mainconfig.proxy_retry_delay, Stringify(RETRY_DELAY) },
139 { "retry_count", PW_TYPE_INTEGER, 0, &mainconfig.proxy_retry_count, Stringify(RETRY_COUNT) },
140 { "default_fallback", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_fallback, "no" },
141 { "dead_time", PW_TYPE_INTEGER, 0, &mainconfig.proxy_dead_time, Stringify(DEAD_TIME) },
142 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN, 0, &mainconfig.wake_all_if_all_dead, "no" },
143 { "proxy_fail_type", PW_TYPE_STRING_PTR, 0, &mainconfig.proxy_fail_type, NULL},
144 { NULL, -1, 0, NULL, NULL }
149 * Security configuration for the server.
151 static const CONF_PARSER security_config[] = {
152 { "max_attributes", PW_TYPE_INTEGER, 0, &librad_max_attributes, Stringify(0) },
153 { "reject_delay", PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
154 { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
155 { NULL, -1, 0, NULL, NULL }
160 * syslog configuration for the server.
162 static const CONF_PARSER log_config[] = {
163 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
164 { NULL, -1, 0, NULL, NULL }
169 * A mapping of configuration file names to internal variables
171 static const CONF_PARSER server_config[] = {
173 * FIXME: 'prefix' is the ONLY one which should be
174 * configured at compile time. Hard-coding it here is
175 * bad. It will be cleaned up once we clean up the
176 * hard-coded defines for the locations of the various
179 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
180 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
181 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
182 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
183 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
184 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &librad_dodns, "no" },
186 { "snmp", PW_TYPE_BOOLEAN, 0, &mainconfig.do_snmp, "no" },
188 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
189 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
190 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
191 #ifdef DELETE_BLOCKED_REQUESTS
192 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
194 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &mainconfig.allow_core_dumps, "no" },
195 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
197 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
198 { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
199 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
200 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
201 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
202 { "user", PW_TYPE_STRING_PTR, 0, &mainconfig.uid_name, NULL},
203 { "group", PW_TYPE_STRING_PTR, 0, &mainconfig.gid_name, NULL},
204 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
206 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
208 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
209 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
210 { "proxy", PW_TYPE_SUBSECTION, 0, NULL, (const void *) proxy_config },
211 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
212 { NULL, -1, 0, NULL, NULL }
216 #define MAX_ARGV (256)
218 * Xlat for %{config:section.subsection.attribute}
220 static int xlat_config(void *instance, REQUEST *request,
221 char *fmt, char *out,
223 RADIUS_ESCAPE_STRING func)
228 const char *from, *value;
232 char *argv[MAX_ARGV];
234 request = request; /* -Wunused */
235 instance = instance; /* -Wunused */
241 * Split the string into argv's BEFORE doing radius_xlat...
254 if (argc >= (MAX_ARGV - 1)) break;
257 * Copy the argv over to our buffer.
260 if (to >= myfmt + sizeof(myfmt) - 1) {
261 return 0; /* no error msg */
266 if (from[1] == '{') {
269 length = rad_copy_variable(to, from);
275 } else { /* FIXME: catch %%{ ? */
282 radlog(L_ERR, "config: Unexpected nested '[' in \"%s\"", fmt);
291 radlog(L_ERR, "config: Unbalanced ']' in \"%s\"", fmt);
294 if (from[1] != '.') {
295 radlog(L_ERR, "config: Unexpected text after ']' in \"%s\"", fmt);
304 if (flag == 0) break;
312 if ((*from == '.') && (flag == 0)) {
316 } /* end of string, or found a period */
319 radlog(L_ERR, "config: Unbalanced '[' in \"%s\"", fmt);
323 *(to++) = '\0'; /* terminate the string. */
327 * Expand each string, as appropriate
330 left = sizeof(argv_buf);
331 for (i = 0; i < argc; i++) {
335 * Don't touch argv's which won't be translated.
337 if (strchr(argv[i], '%') == NULL) continue;
339 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
342 * Fail to be backwards compatible.
344 * It's yucky, but it won't break anything,
345 * and it won't cause security problems.
362 cs = cf_section_find(NULL); /* get top-level section */
365 * Root through section & subsection references.
366 * The last entry of argv MUST be the CONF_PAIR.
368 for (i = 0; i < argc - 1; i++) {
373 * FIXME: What about RADIUS attributes containing '['?
375 name2 = strchr(argv[i], '[');
377 char *p = strchr(name2, ']');
378 rad_assert(p != NULL);
379 rad_assert(p[1] =='\0');
386 subcs = cf_section_sub_find_name2(cs, argv[i],
389 radlog(L_ERR, "config: section \"%s %s {}\" not found while dereferencing \"%s\"", argv[i], name2, fmt);
393 subcs = cf_section_sub_find(cs, argv[i]);
395 radlog(L_ERR, "config: section \"%s {}\" not found while dereferencing \"%s\"", argv[i], fmt);
400 } /* until argc - 1 */
403 * This can now have embedded periods in it.
405 cp = cf_pair_find(cs, argv[argc - 1]);
407 radlog(L_ERR, "config: item \"%s\" not found while dereferencing \"%s\"", argv[argc], fmt);
412 * Ensure that we only copy what's necessary.
414 * If 'outlen' is too small, then the output is chopped to fit.
416 value = cf_pair_value(cp);
418 if (outlen > strlen(value)) {
419 outlen = strlen(value) + 1;
423 return func(out, outlen, value);
428 * Recursively make directories.
430 static int r_mkdir(const char *part)
432 char *ptr, parentdir[500];
435 if (stat(part, &st) == 0)
438 ptr = strrchr(part, '/');
443 snprintf(parentdir, (ptr - part)+1, "%s", part);
445 if (r_mkdir(parentdir) != 0)
448 if (mkdir(part, 0770) != 0) {
449 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
457 * Checks if the log directory is writeable by a particular user.
459 static int radlogdir_iswritable(const char *effectiveuser)
461 struct passwd *pwent;
463 if (!radlog_dir || radlog_dir[0] != '/')
466 if (r_mkdir(radlog_dir) != 0)
469 /* FIXME: do we have this function? */
470 if (strstr(radlog_dir, "radius") == NULL)
473 /* we have a logdir that mentions 'radius', so it's probably
474 * safe to chown the immediate directory to be owned by the normal
475 * process owner. we gotta do it before we give up root. -chad
478 if (!effectiveuser) {
482 pwent = getpwnam(effectiveuser);
484 if (pwent == NULL) /* uh oh! */
487 if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
495 * Switch UID and GID to what is specified in the config file
497 static int switch_users(void)
499 struct rlimit core_limits;
502 if (mainconfig.gid_name != NULL) {
505 gr = getgrnam(mainconfig.gid_name);
507 if (errno == ENOMEM) {
508 radlog(L_ERR|L_CONS, "Cannot switch to Group %s: out of memory", mainconfig.gid_name);
510 radlog(L_ERR|L_CONS, "Cannot switch group; %s doesn't exist", mainconfig.gid_name);
514 server_gid = gr->gr_gid;
515 if (setgid(server_gid) < 0) {
516 radlog(L_ERR|L_CONS, "Failed setting Group to %s: %s",
517 mainconfig.gid_name, strerror(errno));
521 server_gid = getgid();
525 if (mainconfig.uid_name != NULL) {
528 pw = getpwnam(mainconfig.uid_name);
530 if (errno == ENOMEM) {
531 radlog(L_ERR|L_CONS, "Cannot switch to User %s: out of memory", mainconfig.uid_name);
533 radlog(L_ERR|L_CONS, "Cannot switch user; %s doesn't exist", mainconfig.uid_name);
537 server_uid = pw->pw_uid;
538 #ifdef HAVE_INITGROUPS
539 if (initgroups(mainconfig.uid_name, server_gid) < 0) {
540 if (errno != EPERM) {
541 radlog(L_ERR|L_CONS, "Failed setting supplementary groups for User %s: %s", mainconfig.uid_name, strerror(errno));
546 if (setuid(server_uid) < 0) {
547 radlog(L_ERR|L_CONS, "Failed setting User to %s: %s", mainconfig.uid_name, strerror(errno));
552 /* Get the current maximum for core files. */
553 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
554 radlog(L_ERR|L_CONS, "Failed to get current core limit: %s", strerror(errno));
558 if (mainconfig.allow_core_dumps) {
559 #ifdef HAVE_SYS_PRTCL_H
560 #ifdef PR_SET_DUMPABLE
561 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
562 radlog(L_ERR|L_CONS,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
568 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
569 radlog(L_ERR|L_CONS, "Cannot update core dump limit: %s",
574 * If we're running as a daemon, and core
575 * dumps are enabled, log that information.
577 } else if ((core_limits.rlim_cur != 0) && !debug_flag)
578 radlog(L_INFO|L_CONS, "Core dumps are enabled.");
580 } else if (!debug_flag) {
582 * Not debugging. Set the core size to zero, to
583 * prevent security breaches. i.e. People
584 * reading passwords from the 'core' file.
586 struct rlimit limits;
589 limits.rlim_max = core_limits.rlim_max;
591 if (setrlimit(RLIMIT_CORE, &limits) < 0) {
592 radlog(L_ERR|L_CONS, "Cannot disable core dumps: %s",
599 * We've probably written to the log file already as
600 * root.root, so if we have switched users, we've got to
601 * update the ownership of the file.
603 if ((debug_flag == 0) &&
604 (mainconfig.radlog_dest == RADLOG_FILES) &&
605 (mainconfig.log_file != NULL)) {
606 chown(mainconfig.log_file, server_uid, server_gid);
613 static const LRAD_NAME_NUMBER str2dest[] = {
614 { "null", RADLOG_NULL },
615 { "files", RADLOG_FILES },
616 { "syslog", RADLOG_SYSLOG },
617 { "stdout", RADLOG_STDOUT },
618 { "stderr", RADLOG_STDERR },
619 { NULL, RADLOG_NUM_DEST }
626 * This function can ONLY be called from the main server process.
628 int read_mainconfig(int reload)
630 static int old_debug_level = -1;
632 CONF_SECTION *cs, *oldcs;
633 rad_listen_t *listener;
636 if (stat(radius_dir, &statbuf) < 0) {
637 radlog(L_ERR|L_CONS, "Errors reading %s: %s",
638 radius_dir, strerror(errno));
642 if ((statbuf.st_mode & S_IWOTH) != 0) {
643 radlog(L_ERR|L_CONS, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
649 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
650 radlog(L_ERR|L_CONS, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
655 /* Read the configuration file */
656 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
657 radius_dir, mainconfig.radiusd_conf);
658 if ((cs = cf_file_read(buffer)) == NULL) {
659 radlog(L_ERR|L_CONS, "Errors reading %s", buffer);
664 * Debug flag 1 MAY go to files.
665 * Debug flag 2 ALWAYS goes to stdout
667 * Parse the log_destination before printing anything else.
668 * All messages before this MUST be errors, which log.c
669 * will print to stderr, since log_file is NULL, too.
671 if (debug_flag < 2) {
673 char *radlog_dest = NULL;
675 rcode = cf_item_parse(cs, "log_destination",
676 PW_TYPE_STRING_PTR, &radlog_dest,
678 if (rcode < 0) return -1;
680 mainconfig.radlog_dest = lrad_str2int(str2dest, radlog_dest, RADLOG_NUM_DEST);
681 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
682 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
685 cf_section_free(&cs);
689 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
690 static const CONF_PARSER syslog_config[] = {
691 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
692 { NULL, -1, 0, NULL, NULL }
694 cf_section_parse(cs, NULL, syslog_config);
697 * Make sure syslog_facility isn't NULL before using it
699 if (!syslog_facility) {
700 fprintf(stderr, "radiusd: Error: Unknown syslog chosen but no facility spedified\n");
702 cf_section_free(&cs);
705 mainconfig.syslog_facility = lrad_str2int(str2fac, syslog_facility, -1);
706 if (mainconfig.syslog_facility < 0) {
707 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
710 free(syslog_facility);
711 cf_section_free(&cs);
716 if (mainconfig.radlog_dest == RADLOG_FILES) {
717 static const CONF_PARSER file_config[] = {
718 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
719 { NULL, -1, 0, NULL, NULL }
722 cf_section_parse(cs, NULL, file_config);
727 mainconfig.radlog_dest = RADLOG_STDOUT;
728 mainconfig.radlog_fd = STDOUT_FILENO;
731 radlog(L_INFO, "%s", radiusd_version);
733 radlog(L_INFO, "Starting - reading configuration files ...");
735 radlog(L_INFO, "Reloading - reading configuration files...");
738 /* Initialize the dictionary */
739 DEBUG2("read_config_files: reading dictionary");
740 if (dict_init(radius_dir, RADIUS_DICTIONARY) != 0) {
741 radlog(L_ERR|L_CONS, "Errors reading dictionary: %s",
747 * This allows us to figure out where, relative to
748 * radiusd.conf, the other configuration files exist.
750 cf_section_parse(cs, NULL, server_config);
754 * Merge the old with the new.
759 newcs = cf_section_sub_find(cs, "modules");
760 oldcs = cf_section_sub_find(mainconfig.config, "modules");
761 if (newcs && oldcs) {
762 if (!cf_section_migrate(newcs, oldcs)) {
763 radlog(L_ERR|L_CONS, "Fatal error migrating configuration data");
771 * Free the old configuration items, and replace them
774 * Note that where possible, we do atomic switch-overs,
775 * to ensure that the pointers are always valid.
777 oldcs = mainconfig.config;
778 mainconfig.config = cs;
779 cf_section_free(&oldcs);
781 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
782 radius_dir, mainconfig.radiusd_conf);
783 if (!realms_init(buffer)) {
788 * Register the %{config:section.subsection} xlat function.
790 xlat_register("config", xlat_config, NULL);
793 * Set the libraries debugging flag to whatever the main
794 * flag is. Note that on a SIGHUP, to turn the debugging
795 * off, we do other magic.
797 * Increase the debug level, if the configuration file
798 * says to, OR, if we're decreasing the debug from what it
799 * was before, allow that, too.
801 if ((mainconfig.debug_level > debug_flag) ||
802 (mainconfig.debug_level <= old_debug_level)) {
803 debug_flag = mainconfig.debug_level;
805 librad_debug = debug_flag;
806 old_debug_level = mainconfig.debug_level;
809 * Go update our behaviour, based on the configuration
814 * The first time around, ensure that we can write to the
819 * We need root to do mkdir() and chown(), so we
820 * do this before giving up root.
822 radlogdir_iswritable(mainconfig.uid_name);
826 * We should really switch users earlier in the process.
831 * Sanity check the configuration for internal
834 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
835 mainconfig.reject_delay = mainconfig.cleanup_delay;
839 * Initialize the old "bind_address" and "port", first.
844 * Read the list of listeners.
846 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
847 radius_dir, mainconfig.radiusd_conf);
848 if (listen_init(buffer, &listener) < 0) {
853 radlog(L_ERR|L_CONS, "Server is not configured to listen on any ports. Exiting.");
857 listen_free(&mainconfig.listen);
858 mainconfig.listen = listener;
861 * Walk through the listeners. If we're listening on acct
862 * or auth, read in the clients files, else ignore them.
864 for (listener = mainconfig.listen;
866 listener = listener->next) {
867 if ((listener->type == RAD_LISTEN_AUTH) ||
868 (listener->type == RAD_LISTEN_ACCT)) {
873 if (listener != NULL) {
874 RADCLIENT_LIST *clients, *old_clients;
877 * Create the new clients first, and add them
878 * to the CONF_SECTION, where they're automagically
879 * freed if anything goes wrong.
881 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
882 radius_dir, mainconfig.radiusd_conf);
883 clients = clients_parse_section(buffer, mainconfig.config);
889 * Free the old trees AFTER replacing them with
892 old_clients = mainconfig.clients;
893 mainconfig.clients = clients;
894 clients_free(old_clients);
897 /* Reload the modules. */
898 DEBUG2("radiusd: entering modules setup");
899 if (setup_modules(reload) < 0) {
900 radlog(L_ERR|L_CONS, "Errors setting up modules");
907 * Free the configuration. Called only when the server is exiting.
909 int free_mainconfig(void)
912 * Clean up the configuration data
915 cf_section_free(&mainconfig.config);
916 free(mainconfig.radiusd_conf);
918 listen_free(&mainconfig.listen);