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_PRTCL_H
46 #include <sys/prctl.h>
53 struct main_config_t mainconfig;
56 * Temporary local variables for parsing the configuration
59 static uid_t server_uid;
60 static gid_t server_gid;
63 * These are not used anywhere else..
65 static const char *localstatedir = NULL;
66 static const char *prefix = NULL;
67 static char *syslog_facility = NULL;
68 static const LRAD_NAME_NUMBER str2fac[] = {
79 { "daemon", LOG_DAEMON },
97 { "authpriv", LOG_AUTHPRIV },
103 { "local0", LOG_LOCAL0 },
106 { "local1", LOG_LOCAL1 },
109 { "local2", LOG_LOCAL2 },
112 { "local3", LOG_LOCAL3 },
115 { "local4", LOG_LOCAL4 },
118 { "local5", LOG_LOCAL5 },
121 { "local6", LOG_LOCAL6 },
124 { "local7", LOG_LOCAL7 },
130 * Security configuration for the server.
132 static const CONF_PARSER security_config[] = {
133 { "max_attributes", PW_TYPE_INTEGER, 0, &librad_max_attributes, Stringify(0) },
134 { "reject_delay", PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
135 { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
136 { NULL, -1, 0, NULL, NULL }
141 * syslog configuration for the server.
143 static const CONF_PARSER log_config[] = {
144 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
145 { NULL, -1, 0, NULL, NULL }
150 * A mapping of configuration file names to internal variables
152 static const CONF_PARSER server_config[] = {
154 * FIXME: 'prefix' is the ONLY one which should be
155 * configured at compile time. Hard-coding it here is
156 * bad. It will be cleaned up once we clean up the
157 * hard-coded defines for the locations of the various
160 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
161 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
162 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
163 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
164 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
165 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &librad_dodns, "no" },
166 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
167 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
168 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
169 #ifdef DELETE_BLOCKED_REQUESTS
170 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
172 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &mainconfig.allow_core_dumps, "no" },
173 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
175 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
176 { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
177 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
178 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
179 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
180 { "user", PW_TYPE_STRING_PTR, 0, &mainconfig.uid_name, NULL},
181 { "group", PW_TYPE_STRING_PTR, 0, &mainconfig.gid_name, NULL},
182 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
184 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
186 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
187 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
188 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
189 { NULL, -1, 0, NULL, NULL }
193 #define MAX_ARGV (256)
195 * Xlat for %{config:section.subsection.attribute}
197 static int xlat_config(void *instance, REQUEST *request,
198 char *fmt, char *out,
200 RADIUS_ESCAPE_STRING func)
205 const char *from, *value;
209 char *argv[MAX_ARGV];
211 request = request; /* -Wunused */
212 instance = instance; /* -Wunused */
218 * Split the string into argv's BEFORE doing radius_xlat...
231 if (argc >= (MAX_ARGV - 1)) break;
234 * Copy the argv over to our buffer.
237 if (to >= myfmt + sizeof(myfmt) - 1) {
238 return 0; /* no error msg */
243 if (from[1] == '{') {
246 length = rad_copy_variable(to, from);
252 } else { /* FIXME: catch %%{ ? */
259 radlog(L_ERR, "config: Unexpected nested '[' in \"%s\"", fmt);
268 radlog(L_ERR, "config: Unbalanced ']' in \"%s\"", fmt);
271 if (from[1] != '.') {
272 radlog(L_ERR, "config: Unexpected text after ']' in \"%s\"", fmt);
281 if (flag == 0) break;
289 if ((*from == '.') && (flag == 0)) {
293 } /* end of string, or found a period */
296 radlog(L_ERR, "config: Unbalanced '[' in \"%s\"", fmt);
300 *(to++) = '\0'; /* terminate the string. */
304 * Expand each string, as appropriate
307 left = sizeof(argv_buf);
308 for (i = 0; i < argc; i++) {
312 * Don't touch argv's which won't be translated.
314 if (strchr(argv[i], '%') == NULL) continue;
316 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
319 * Fail to be backwards compatible.
321 * It's yucky, but it won't break anything,
322 * and it won't cause security problems.
339 cs = cf_section_find(NULL); /* get top-level section */
342 * Root through section & subsection references.
343 * The last entry of argv MUST be the CONF_PAIR.
345 for (i = 0; i < argc - 1; i++) {
350 * FIXME: What about RADIUS attributes containing '['?
352 name2 = strchr(argv[i], '[');
354 char *p = strchr(name2, ']');
355 rad_assert(p != NULL);
356 rad_assert(p[1] =='\0');
363 subcs = cf_section_sub_find_name2(cs, argv[i],
366 radlog(L_ERR, "config: section \"%s %s {}\" not found while dereferencing \"%s\"", argv[i], name2, fmt);
370 subcs = cf_section_sub_find(cs, argv[i]);
372 radlog(L_ERR, "config: section \"%s {}\" not found while dereferencing \"%s\"", argv[i], fmt);
377 } /* until argc - 1 */
380 * This can now have embedded periods in it.
382 cp = cf_pair_find(cs, argv[argc - 1]);
384 radlog(L_ERR, "config: item \"%s\" not found while dereferencing \"%s\"", argv[argc], fmt);
389 * Ensure that we only copy what's necessary.
391 * If 'outlen' is too small, then the output is chopped to fit.
393 value = cf_pair_value(cp);
395 if (outlen > strlen(value)) {
396 outlen = strlen(value) + 1;
400 return func(out, outlen, value);
405 * Recursively make directories.
407 static int r_mkdir(const char *part)
409 char *ptr, parentdir[500];
412 if (stat(part, &st) == 0)
415 ptr = strrchr(part, '/');
420 snprintf(parentdir, (ptr - part)+1, "%s", part);
422 if (r_mkdir(parentdir) != 0)
425 if (mkdir(part, 0770) != 0) {
426 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
434 * Checks if the log directory is writeable by a particular user.
436 static int radlogdir_iswritable(const char *effectiveuser)
439 struct passwd *pwent;
442 if (!radlog_dir || radlog_dir[0] != '/')
445 if (r_mkdir(radlog_dir) != 0)
448 /* FIXME: do we have this function? */
449 if (strstr(radlog_dir, "radius") == NULL)
452 /* we have a logdir that mentions 'radius', so it's probably
453 * safe to chown the immediate directory to be owned by the normal
454 * process owner. we gotta do it before we give up root. -chad
457 if (!effectiveuser) {
462 pwent = getpwnam(effectiveuser);
464 if (pwent == NULL) /* uh oh! */
467 if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
476 * Switch UID and GID to what is specified in the config file
478 static int switch_users(void)
480 #ifdef HAVE_SYS_RESOURCE_H
481 struct rlimit core_limits;
486 if (mainconfig.gid_name != NULL) {
489 gr = getgrnam(mainconfig.gid_name);
491 if (errno == ENOMEM) {
492 radlog(L_ERR, "Cannot switch to Group %s: out of memory", mainconfig.gid_name);
494 radlog(L_ERR, "Cannot switch group; %s doesn't exist", mainconfig.gid_name);
498 server_gid = gr->gr_gid;
499 if (setgid(server_gid) < 0) {
500 radlog(L_ERR, "Failed setting Group to %s: %s",
501 mainconfig.gid_name, strerror(errno));
505 server_gid = getgid();
511 if (mainconfig.uid_name != NULL) {
514 pw = getpwnam(mainconfig.uid_name);
516 if (errno == ENOMEM) {
517 radlog(L_ERR, "Cannot switch to User %s: out of memory", mainconfig.uid_name);
519 radlog(L_ERR, "Cannot switch user; %s doesn't exist", mainconfig.uid_name);
523 server_uid = pw->pw_uid;
524 #ifdef HAVE_INITGROUPS
525 if (initgroups(mainconfig.uid_name, server_gid) < 0) {
526 if (errno != EPERM) {
527 radlog(L_ERR, "Failed setting supplementary groups for User %s: %s", mainconfig.uid_name, strerror(errno));
532 if (setuid(server_uid) < 0) {
533 radlog(L_ERR, "Failed setting User to %s: %s", mainconfig.uid_name, strerror(errno));
539 #ifdef HAVE_SYS_RESOURCE_H
540 /* Get the current maximum for core files. */
541 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
542 radlog(L_ERR, "Failed to get current core limit: %s", strerror(errno));
547 if (mainconfig.allow_core_dumps) {
548 #ifdef HAVE_SYS_PRTCL_H
549 #ifdef PR_SET_DUMPABLE
550 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
551 radlog(L_ERR,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
557 #ifdef HAVE_SYS_RESOURCE_H
558 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
559 radlog(L_ERR, "Cannot update core dump limit: %s",
564 * If we're running as a daemon, and core
565 * dumps are enabled, log that information.
567 } else if ((core_limits.rlim_cur != 0) && !debug_flag)
568 radlog(L_INFO, "Core dumps are enabled.");
571 } else if (!debug_flag) {
572 #ifdef HAVE_SYS_RESOURCE_H
574 * Not debugging. Set the core size to zero, to
575 * prevent security breaches. i.e. People
576 * reading passwords from the 'core' file.
578 struct rlimit limits;
581 limits.rlim_max = core_limits.rlim_max;
583 if (setrlimit(RLIMIT_CORE, &limits) < 0) {
584 radlog(L_ERR, "Cannot disable core dumps: %s",
591 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
593 * We've probably written to the log file already as
594 * root.root, so if we have switched users, we've got to
595 * update the ownership of the file.
597 if ((debug_flag == 0) &&
598 (mainconfig.radlog_dest == RADLOG_FILES) &&
599 (mainconfig.log_file != NULL)) {
600 chown(mainconfig.log_file, server_uid, server_gid);
607 static const LRAD_NAME_NUMBER str2dest[] = {
608 { "null", RADLOG_NULL },
609 { "files", RADLOG_FILES },
610 { "syslog", RADLOG_SYSLOG },
611 { "stdout", RADLOG_STDOUT },
612 { "stderr", RADLOG_STDERR },
613 { NULL, RADLOG_NUM_DEST }
620 * This function can ONLY be called from the main server process.
622 int read_mainconfig(int reload)
624 static int old_debug_level = -1;
626 CONF_SECTION *cs, *templates;
627 rad_listen_t *listener;
630 if (stat(radius_dir, &statbuf) < 0) {
631 radlog(L_ERR, "Errors reading %s: %s",
632 radius_dir, strerror(errno));
637 if ((statbuf.st_mode & S_IWOTH) != 0) {
638 radlog(L_ERR, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
645 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
646 radlog(L_ERR, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
652 /* Read the configuration file */
653 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
654 radius_dir, mainconfig.radiusd_conf);
655 if ((cs = cf_file_read(buffer)) == NULL) {
656 radlog(L_ERR, "Errors reading %s", buffer);
661 * Add templates to each kind of subsection.
663 templates = cf_section_sub_find(cs, "templates");
665 CONF_SECTION *ts, *mycs;
668 * Loop over the templates, adding them to the
669 * sections in the main configuration file.
671 for (ts = cf_subsection_find_next(templates, NULL, NULL);
673 ts = cf_subsection_find_next(templates, ts, NULL)) {
674 const char *name1 = cf_section_name1(ts);
677 * Loop over sections in the main config
678 * file, adding templats.
680 for (mycs = cf_subsection_find_next(cs, NULL, name1);
682 mycs = cf_subsection_find_next(cs, mycs, name1)) {
685 value = cf_section_value_find(mycs, "template");
689 tts = cf_section_sub_find_name2(templates,
693 radlog(L_ERR, "%s[%d]: Section refers to non-existent template \"%s\"",
694 cf_section_filename(mycs), cf_section_lineno(mycs), value);
697 cf_section_template(mycs, tts);
699 cf_section_template(mycs, ts);
706 * Debug flag 1 MAY go to files.
707 * Debug flag 2 ALWAYS goes to stdout
709 * Parse the log_destination before printing anything else.
710 * All messages before this MUST be errors, which log.c
711 * will print to stderr, since log_file is NULL, too.
713 if (debug_flag < 2) {
715 char *radlog_dest = NULL;
717 rcode = cf_item_parse(cs, "log_destination",
718 PW_TYPE_STRING_PTR, &radlog_dest,
720 if (rcode < 0) return -1;
722 mainconfig.radlog_dest = lrad_str2int(str2dest, radlog_dest, RADLOG_NUM_DEST);
723 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
724 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
727 cf_section_free(&cs);
731 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
732 static const CONF_PARSER syslog_config[] = {
733 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
734 { NULL, -1, 0, NULL, NULL }
736 cf_section_parse(cs, NULL, syslog_config);
739 * Make sure syslog_facility isn't NULL before using it
741 if (!syslog_facility) {
742 fprintf(stderr, "radiusd: Error: Unknown syslog chosen but no facility spedified\n");
744 cf_section_free(&cs);
747 mainconfig.syslog_facility = lrad_str2int(str2fac, syslog_facility, -1);
748 if (mainconfig.syslog_facility < 0) {
749 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
752 free(syslog_facility);
753 cf_section_free(&cs);
758 if (mainconfig.radlog_dest == RADLOG_FILES) {
759 static const CONF_PARSER file_config[] = {
760 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
761 { NULL, -1, 0, NULL, NULL }
764 cf_section_parse(cs, NULL, file_config);
769 mainconfig.radlog_dest = RADLOG_STDOUT;
770 mainconfig.radlog_fd = STDOUT_FILENO;
774 radlog(L_INFO, "Starting - reading configuration files ...");
776 radlog(L_INFO, "Reloading - reading configuration files...");
779 /* Initialize the dictionary */
780 DEBUG2("read_config_files: reading dictionary");
781 if (dict_init(radius_dir, RADIUS_DICTIONARY) != 0) {
782 radlog(L_ERR, "Errors reading dictionary: %s",
788 * This allows us to figure out where, relative to
789 * radiusd.conf, the other configuration files exist.
791 cf_section_parse(cs, NULL, server_config);
795 * Merge the old with the new.
800 newcs = cf_section_sub_find(cs, "modules");
801 oldcs = cf_section_sub_find(mainconfig.config, "modules");
802 if (newcs && oldcs) {
803 if (!cf_section_migrate(newcs, oldcs)) {
804 radlog(L_ERR, "Fatal error migrating configuration data");
812 * Free the old configuration items, and replace them
815 * Note that where possible, we do atomic switch-overs,
816 * to ensure that the pointers are always valid.
818 cf_section_free(&mainconfig.config);
819 mainconfig.config = cs;
821 if (!realms_init(cs)) {
826 * Register the %{config:section.subsection} xlat function.
828 xlat_register("config", xlat_config, NULL);
831 * Reload: change debug flag if it's changed in the
832 * configuration file.
835 if (mainconfig.debug_level != old_debug_level) {
836 debug_flag = mainconfig.debug_level;
839 } else if (debug_flag == 0) {
842 * Starting the server, WITHOUT "-x" on the
843 * command-line: use whatever's in the config
846 debug_flag = mainconfig.debug_level;
848 librad_debug = debug_flag;
849 old_debug_level = mainconfig.debug_level;
852 * Go update our behaviour, based on the configuration
857 * The first time around, ensure that we can write to the
862 * We need root to do mkdir() and chown(), so we
863 * do this before giving up root.
865 radlogdir_iswritable(mainconfig.uid_name);
869 * We should really switch users earlier in the process.
871 if (!switch_users()) exit(1);
874 * Sanity check the configuration for internal
877 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
878 mainconfig.reject_delay = mainconfig.cleanup_delay;
880 if (mainconfig.reject_delay < 0) mainconfig.reject_delay = 0;
883 * Initialize the old "bind_address" and "port", first.
888 * Read the list of listeners.
890 if (listen_init(cs, &listener) < 0) {
895 radlog(L_ERR, "Server is not configured to listen on any ports. Exiting.");
899 listen_free(&mainconfig.listen);
900 mainconfig.listen = listener;
903 * Walk through the listeners. If we're listening on acct
904 * or auth, read in the clients files, else ignore them.
906 for (listener = mainconfig.listen;
908 listener = listener->next) {
909 if ((listener->type == RAD_LISTEN_AUTH) ||
910 (listener->type == RAD_LISTEN_ACCT)) {
915 if (listener != NULL) {
916 RADCLIENT_LIST *clients;
919 * Create the new clients first, and add them
920 * to the CONF_SECTION, where they're automagically
921 * freed if anything goes wrong.
923 clients = clients_parse_section(mainconfig.config);
929 * The old clients are already NULL, because they
930 * are in a configuration section, and the client
931 * "free" function was added by clients parse
934 * Note that because we require at least one
935 * client in the main configuration file, any
936 * clients added by SQL will be inserted into
937 * that, and automatically freed.
939 mainconfig.clients = clients;
942 /* Reload the modules. */
943 DEBUG2("radiusd: entering modules setup");
944 if (setup_modules(reload, mainconfig.config) < 0) {
945 radlog(L_ERR, "Errors setting up modules");
952 * Free the configuration. Called only when the server is exiting.
954 int free_mainconfig(void)
957 * Clean up the configuration data
960 cf_section_free(&mainconfig.config);
961 free(mainconfig.radiusd_conf);
963 listen_free(&mainconfig.listen);