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 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;
61 static const char *uid_name = NULL;
62 static const char *gid_name = NULL;
63 static int allow_core_dumps = 0;
66 * These are not used anywhere else..
68 static const char *localstatedir = NULL;
69 static const char *prefix = NULL;
70 static char *syslog_facility = NULL;
71 static const LRAD_NAME_NUMBER str2fac[] = {
82 { "daemon", LOG_DAEMON },
100 { "authpriv", LOG_AUTHPRIV },
106 { "local0", LOG_LOCAL0 },
109 { "local1", LOG_LOCAL1 },
112 { "local2", LOG_LOCAL2 },
115 { "local3", LOG_LOCAL3 },
118 { "local4", LOG_LOCAL4 },
121 { "local5", LOG_LOCAL5 },
124 { "local6", LOG_LOCAL6 },
127 { "local7", LOG_LOCAL7 },
133 * Security configuration for the server.
135 static const CONF_PARSER security_config[] = {
136 { "max_attributes", PW_TYPE_INTEGER, 0, &librad_max_attributes, Stringify(0) },
137 { "reject_delay", PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
138 { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
139 { NULL, -1, 0, NULL, NULL }
144 * syslog configuration for the server.
146 static const CONF_PARSER log_config[] = {
147 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
148 { NULL, -1, 0, NULL, NULL }
153 * A mapping of configuration file names to internal variables
155 static const CONF_PARSER server_config[] = {
157 * FIXME: 'prefix' is the ONLY one which should be
158 * configured at compile time. Hard-coding it here is
159 * bad. It will be cleaned up once we clean up the
160 * hard-coded defines for the locations of the various
163 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
164 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
165 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
166 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
167 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
168 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &librad_dodns, "no" },
169 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
170 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
171 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
172 #ifdef DELETE_BLOCKED_REQUESTS
173 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
175 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &allow_core_dumps, "no" },
176 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
178 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
179 { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
180 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
181 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
182 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
183 { "user", PW_TYPE_STRING_PTR, 0, &uid_name, NULL},
184 { "group", PW_TYPE_STRING_PTR, 0, &gid_name, NULL},
185 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
187 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
189 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
190 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
191 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
192 { NULL, -1, 0, NULL, NULL }
196 #define MAX_ARGV (256)
198 * Xlat for %{config:section.subsection.attribute}
200 static int xlat_config(void *instance, REQUEST *request,
201 char *fmt, char *out,
203 RADIUS_ESCAPE_STRING func)
208 const char *from, *value;
212 char *argv[MAX_ARGV];
214 request = request; /* -Wunused */
215 instance = instance; /* -Wunused */
221 * Split the string into argv's BEFORE doing radius_xlat...
234 if (argc >= (MAX_ARGV - 1)) break;
237 * Copy the argv over to our buffer.
240 if (to >= myfmt + sizeof(myfmt) - 1) {
241 return 0; /* no error msg */
246 if (from[1] == '{') {
249 length = rad_copy_variable(to, from);
255 } else { /* FIXME: catch %%{ ? */
262 radlog(L_ERR, "config: Unexpected nested '[' in \"%s\"", fmt);
271 radlog(L_ERR, "config: Unbalanced ']' in \"%s\"", fmt);
274 if (from[1] != '.') {
275 radlog(L_ERR, "config: Unexpected text after ']' in \"%s\"", fmt);
284 if (flag == 0) break;
292 if ((*from == '.') && (flag == 0)) {
296 } /* end of string, or found a period */
299 radlog(L_ERR, "config: Unbalanced '[' in \"%s\"", fmt);
303 *(to++) = '\0'; /* terminate the string. */
307 * Expand each string, as appropriate
310 left = sizeof(argv_buf);
311 for (i = 0; i < argc; i++) {
315 * Don't touch argv's which won't be translated.
317 if (strchr(argv[i], '%') == NULL) continue;
319 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
322 * Fail to be backwards compatible.
324 * It's yucky, but it won't break anything,
325 * and it won't cause security problems.
342 cs = request->root->config;
345 * Root through section & subsection references.
346 * The last entry of argv MUST be the CONF_PAIR.
348 for (i = 0; i < argc - 1; i++) {
353 * FIXME: What about RADIUS attributes containing '['?
355 name2 = strchr(argv[i], '[');
357 char *p = strchr(name2, ']');
358 rad_assert(p != NULL);
359 rad_assert(p[1] =='\0');
366 subcs = cf_section_sub_find_name2(cs, argv[i],
369 radlog(L_ERR, "config: section \"%s %s {}\" not found while dereferencing \"%s\"", argv[i], name2, fmt);
373 subcs = cf_section_sub_find(cs, argv[i]);
375 radlog(L_ERR, "config: section \"%s {}\" not found while dereferencing \"%s\"", argv[i], fmt);
380 } /* until argc - 1 */
383 * This can now have embedded periods in it.
385 cp = cf_pair_find(cs, argv[argc - 1]);
387 radlog(L_ERR, "config: item \"%s\" not found while dereferencing \"%s\"", argv[argc], fmt);
392 * Ensure that we only copy what's necessary.
394 * If 'outlen' is too small, then the output is chopped to fit.
396 value = cf_pair_value(cp);
398 if (outlen > strlen(value)) {
399 outlen = strlen(value) + 1;
403 return func(out, outlen, value);
408 * Xlat for %{client:foo}
410 static int xlat_client(UNUSED void *instance, REQUEST *request,
411 char *fmt, char *out,
413 UNUSED RADIUS_ESCAPE_STRING func)
415 const char *value = NULL;
418 if (!fmt || !out || (outlen < 1)) return 0;
420 if (!request || !request->client) {
425 cp = cf_pair_find(request->client->cs, fmt);
426 if (!cp || !(value = cf_pair_value(cp))) {
431 strlcpy(out, value, outlen);
437 * Recursively make directories.
439 static int r_mkdir(const char *part)
441 char *ptr, parentdir[500];
444 if (stat(part, &st) == 0)
447 ptr = strrchr(part, '/');
452 snprintf(parentdir, (ptr - part)+1, "%s", part);
454 if (r_mkdir(parentdir) != 0)
457 if (mkdir(part, 0770) != 0) {
458 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
466 * Checks if the log directory is writeable by a particular user.
468 static int radlogdir_iswritable(const char *effectiveuser)
471 struct passwd *pwent;
474 if (!radlog_dir || radlog_dir[0] != '/')
477 if (r_mkdir(radlog_dir) != 0)
480 /* FIXME: do we have this function? */
481 if (strstr(radlog_dir, "radius") == NULL)
484 /* we have a logdir that mentions 'radius', so it's probably
485 * safe to chown the immediate directory to be owned by the normal
486 * process owner. we gotta do it before we give up root. -chad
489 if (!effectiveuser) {
494 pwent = getpwnam(effectiveuser);
496 if (pwent == NULL) /* uh oh! */
499 if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
508 * Switch UID and GID to what is specified in the config file
510 static int switch_users(void)
512 int did_setuid = FALSE;
514 #ifdef HAVE_SYS_RESOURCE_H
515 struct rlimit core_limits;
520 if (gid_name != NULL) {
523 gr = getgrnam(gid_name);
525 if (errno == ENOMEM) {
526 radlog(L_ERR, "Cannot switch to Group %s: out of memory", gid_name);
528 radlog(L_ERR, "Cannot switch group; %s doesn't exist", gid_name);
532 server_gid = gr->gr_gid;
533 if (setgid(server_gid) < 0) {
534 radlog(L_ERR, "Failed setting Group to %s: %s",
535 gid_name, strerror(errno));
539 server_gid = getgid();
545 if (uid_name != NULL) {
548 pw = getpwnam(uid_name);
550 if (errno == ENOMEM) {
551 radlog(L_ERR, "Cannot switch to User %s: out of memory", uid_name);
553 radlog(L_ERR, "Cannot switch user; %s doesn't exist", uid_name);
557 server_uid = pw->pw_uid;
558 #ifdef HAVE_INITGROUPS
559 if (initgroups(uid_name, server_gid) < 0) {
560 if (errno != EPERM) {
561 radlog(L_ERR, "Failed setting supplementary groups for User %s: %s", uid_name, strerror(errno));
566 if (setuid(server_uid) < 0) {
567 radlog(L_ERR, "Failed setting User to %s: %s", uid_name, strerror(errno));
572 * Now core dumps are disabled on most secure systems.
578 #ifdef HAVE_SYS_RESOURCE_H
579 /* Get the current maximum for core files. */
580 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
581 radlog(L_ERR, "Failed to get current core limit: %s", strerror(errno));
587 * Core dumps are allowed if we're in debug mode, OR
588 * we've allowed them, OR we did a setuid (which turns
591 * Otherwise, disable core dumps for security.
594 if (!(debug_flag || allow_core_dumps || did_setuid)) {
595 #ifdef HAVE_SYS_RESOURCE_H
596 struct rlimit no_core;
598 no_core.rlim_cur = 0;
599 no_core.rlim_max = 0;
601 if (setrlimit(RLIMIT_CORE, &no_core) < 0) {
602 radlog(L_ERR, "Failed disabling core dumps: %s",
609 * Otherwise, re-enable core dumps if we're
610 * running as a daemon, AND core dumps are
611 * allowed, AND we changed UID's.
613 } else if ((debug_flag == 0) && allow_core_dumps && did_setuid) {
615 * Set the dumpable flag.
617 #ifdef HAVE_SYS_PRCTL_H
618 #ifdef PR_SET_DUMPABLE
619 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
620 radlog(L_ERR,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
627 * Reset the core dump limits again, just to
628 * double check that they haven't changed.
630 #ifdef HAVE_SYS_RESOURCE_H
631 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
632 radlog(L_ERR, "Cannot update core dump limit: %s",
638 radlog(L_INFO, "Core dumps are enabled.");
641 * Else we're debugging (so core dumps are enabled)
642 * OR we're not debugging, AND "allow_core_dumps == FALSE",
643 * OR we're not debugging, AND core dumps are allowed,
644 * BUT we didn't call setuid, so we haven't changed the
645 * core dump capabilities inherited from the parent shell.
648 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
650 * We've probably written to the log file already as
651 * root.root, so if we have switched users, we've got to
652 * update the ownership of the file.
654 if ((debug_flag == 0) &&
655 (mainconfig.radlog_dest == RADLOG_FILES) &&
656 (mainconfig.log_file != NULL)) {
657 chown(mainconfig.log_file, server_uid, server_gid);
664 static const LRAD_NAME_NUMBER str2dest[] = {
665 { "null", RADLOG_NULL },
666 { "files", RADLOG_FILES },
667 { "syslog", RADLOG_SYSLOG },
668 { "stdout", RADLOG_STDOUT },
669 { "stderr", RADLOG_STDERR },
670 { NULL, RADLOG_NUM_DEST }
677 * This function can ONLY be called from the main server process.
679 int read_mainconfig(int reload)
681 static int old_debug_level = -1;
683 CONF_SECTION *cs, *templates;
686 if (stat(radius_dir, &statbuf) < 0) {
687 radlog(L_ERR, "Errors reading %s: %s",
688 radius_dir, strerror(errno));
693 if ((statbuf.st_mode & S_IWOTH) != 0) {
694 radlog(L_ERR, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
701 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
702 radlog(L_ERR, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
709 radlog(L_INFO, "Starting - reading configuration files ...");
711 radlog(L_INFO, "Reloading - reading configuration files...");
714 /* Read the configuration file */
715 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
716 radius_dir, mainconfig.radiusd_conf);
717 if ((cs = cf_file_read(buffer)) == NULL) {
718 radlog(L_ERR, "Errors reading %s", buffer);
723 * Add templates to each kind of subsection.
725 templates = cf_section_sub_find(cs, "templates");
727 CONF_SECTION *ts, *mycs;
730 * Loop over the templates, adding them to the
731 * sections in the main configuration file.
733 for (ts = cf_subsection_find_next(templates, NULL, NULL);
735 ts = cf_subsection_find_next(templates, ts, NULL)) {
736 const char *name1 = cf_section_name1(ts);
739 * Loop over sections in the main config
740 * file, adding templats.
742 for (mycs = cf_subsection_find_next(cs, NULL, name1);
744 mycs = cf_subsection_find_next(cs, mycs, name1)) {
747 value = cf_section_value_find(mycs, "template");
751 tts = cf_section_sub_find_name2(templates,
755 radlog(L_ERR, "%s[%d]: Section refers to non-existent template \"%s\"",
756 cf_section_filename(mycs), cf_section_lineno(mycs), value);
759 cf_section_template(mycs, tts);
761 cf_section_template(mycs, ts);
768 * Debug flag 1 MAY go to files.
769 * Debug flag 2 ALWAYS goes to stdout
771 * Parse the log_destination before printing anything else.
772 * All messages before this MUST be errors, which log.c
773 * will print to stderr, since log_file is NULL, too.
775 if (debug_flag < 2) {
777 char *radlog_dest = NULL;
779 rcode = cf_item_parse(cs, "log_destination",
780 PW_TYPE_STRING_PTR, &radlog_dest,
782 if (rcode < 0) return -1;
784 mainconfig.radlog_dest = lrad_str2int(str2dest, radlog_dest, RADLOG_NUM_DEST);
785 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
786 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
789 cf_section_free(&cs);
793 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
794 static const CONF_PARSER syslog_config[] = {
795 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
796 { NULL, -1, 0, NULL, NULL }
798 cf_section_parse(cs, NULL, syslog_config);
801 * Make sure syslog_facility isn't NULL before using it
803 if (!syslog_facility) {
804 fprintf(stderr, "radiusd: Error: Unknown syslog chosen but no facility spedified\n");
806 cf_section_free(&cs);
809 mainconfig.syslog_facility = lrad_str2int(str2fac, syslog_facility, -1);
810 if (mainconfig.syslog_facility < 0) {
811 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
814 free(syslog_facility);
815 cf_section_free(&cs);
820 if (mainconfig.radlog_dest == RADLOG_FILES) {
821 static const CONF_PARSER file_config[] = {
822 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
823 { NULL, -1, 0, NULL, NULL }
826 cf_section_parse(cs, NULL, file_config);
831 mainconfig.radlog_dest = RADLOG_STDOUT;
832 mainconfig.radlog_fd = STDOUT_FILENO;
835 /* Initialize the dictionary */
836 DEBUG2("including dictionary file %s/%s", radius_dir, RADIUS_DICTIONARY);
837 if (dict_init(radius_dir, RADIUS_DICTIONARY) != 0) {
838 radlog(L_ERR, "Errors reading dictionary: %s",
844 * This allows us to figure out where, relative to
845 * radiusd.conf, the other configuration files exist.
847 cf_section_parse(cs, NULL, server_config);
851 * Merge the old with the new.
856 newcs = cf_section_sub_find(cs, "modules");
857 oldcs = cf_section_sub_find(mainconfig.config, "modules");
858 if (newcs && oldcs) {
859 if (!cf_section_migrate(newcs, oldcs)) {
860 radlog(L_ERR, "Fatal error migrating configuration data");
868 * Free the old configuration items, and replace them
871 * Note that where possible, we do atomic switch-overs,
872 * to ensure that the pointers are always valid.
874 cf_section_free(&mainconfig.config);
875 mainconfig.config = cs;
877 clients_parse_section(cs);
879 DEBUG2("radiusd: #### Loading Realms and Home Servers ####");
881 if (!realms_init(cs)) {
886 * Register the %{config:section.subsection} xlat function.
888 xlat_register("config", xlat_config, NULL);
889 xlat_register("client", xlat_client, NULL);
892 * Reload: change debug flag if it's changed in the
893 * configuration file.
896 if (mainconfig.debug_level != old_debug_level) {
897 debug_flag = mainconfig.debug_level;
900 } else if (debug_flag == 0) {
903 * Starting the server, WITHOUT "-x" on the
904 * command-line: use whatever's in the config
907 debug_flag = mainconfig.debug_level;
909 librad_debug = debug_flag;
910 old_debug_level = mainconfig.debug_level;
913 * Go update our behaviour, based on the configuration
918 * The first time around, ensure that we can write to the
923 * We need root to do mkdir() and chown(), so we
924 * do this before giving up root.
926 radlogdir_iswritable(uid_name);
930 * We should really switch users earlier in the process.
932 if (!switch_users()) exit(1);
935 * Sanity check the configuration for internal
938 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
939 mainconfig.reject_delay = mainconfig.cleanup_delay;
941 if (mainconfig.reject_delay < 0) mainconfig.reject_delay = 0;
943 /* Reload the modules. */
944 if (setup_modules(reload, mainconfig.config) < 0) {
945 radlog(L_ERR, "Errors initializing modules");
953 * Free the configuration. Called only when the server is exiting.
955 int free_mainconfig(void)
958 * Clean up the configuration data
961 cf_section_free(&mainconfig.config);
962 free(mainconfig.radiusd_conf);
964 listen_free(&mainconfig.listen);